|
|
@@ -1,5 +1,15 @@
|
|
|
+import { ref } from 'vue'
|
|
|
import { defineStore } from 'pinia'
|
|
|
+
|
|
|
+import { bfsWalk } from 'simple-mind-map/src/utils'
|
|
|
+import { moveToPosition } from '@/utils'
|
|
|
+import { klona } from 'klona'
|
|
|
+import { v4 } from 'uuid'
|
|
|
+
|
|
|
import { useProjectStore } from '@/store/modules/project'
|
|
|
+import { useAppStore } from './app'
|
|
|
+
|
|
|
+import type { BaseWidget } from '@/types/baseWidget'
|
|
|
|
|
|
type AlignType =
|
|
|
| 'left'
|
|
|
@@ -14,6 +24,8 @@ type AlignType =
|
|
|
| 'hspace'
|
|
|
export const useActionStore = defineStore('action', () => {
|
|
|
const projectStore = useProjectStore()
|
|
|
+ const appStore = useAppStore()
|
|
|
+ const clipboard = ref<BaseWidget[]>()
|
|
|
|
|
|
/**
|
|
|
* 对齐组件
|
|
|
@@ -152,11 +164,209 @@ export const useActionStore = defineStore('action', () => {
|
|
|
* 层级调整
|
|
|
* @param type 层级调整类型
|
|
|
*/
|
|
|
- const onLevel = (type: 'up' | 'down' | 'top' | 'bottom') => {}
|
|
|
+ const onLevel = (type: 'up' | 'down' | 'top' | 'bottom') => {
|
|
|
+ let widgetIds = projectStore.activeWidgets.map((widget) => widget.id)
|
|
|
+
|
|
|
+ bfsWalk(projectStore.activePage, (child) => {
|
|
|
+ // 对子节点遍历排序
|
|
|
+ child?.children?.forEach((item, index) => {
|
|
|
+ if (widgetIds.includes(item.id)) {
|
|
|
+ switch (type) {
|
|
|
+ case 'up': {
|
|
|
+ if (index > 0) {
|
|
|
+ moveToPosition(child.children, index, index - 1)
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ case 'down': {
|
|
|
+ if (index < child.children.length - 1) {
|
|
|
+ moveToPosition(child.children, index, index + 1)
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ case 'top': {
|
|
|
+ if (index > 0) {
|
|
|
+ moveToPosition(child.children, index, 0)
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ case 'bottom': {
|
|
|
+ if (index < child.children.length - 1) {
|
|
|
+ moveToPosition(child.children, index, child.children.length - index)
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 跑完一轮 隐藏位置调整的
|
|
|
+ widgetIds = widgetIds.filter((id) => id !== item.id)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除页面
|
|
|
+ * @param pageId 页面ID
|
|
|
+ */
|
|
|
+ const onDeletePage = (pageId: string) => {
|
|
|
+ projectStore.project?.screens.forEach((screen) => {
|
|
|
+ screen.pages = screen.pages.filter((page) => page.id !== pageId)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据控件ID删除控件
|
|
|
+ * @param widgetId 控件ID
|
|
|
+ */
|
|
|
+ const onDeleteById = (widgetId: string) => {
|
|
|
+ projectStore.project?.screens.forEach((screen) => {
|
|
|
+ bfsWalk(screen.pages, (child) => {
|
|
|
+ const index = child?.children?.findIndex((item) => item.id === widgetId) ?? -1
|
|
|
+ if (index !== -1) {
|
|
|
+ child.children.splice(index, 1)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除控件
|
|
|
+ */
|
|
|
+ const onDelete = () => {
|
|
|
+ const widgetIds = projectStore.activeWidgets.map((widget) => widget.id)
|
|
|
+ if (!widgetIds.length) return
|
|
|
+
|
|
|
+ projectStore.project?.screens.forEach((screen) => {
|
|
|
+ bfsWalk(screen.pages, (child) => {
|
|
|
+ const index = child?.children?.findIndex((item) => widgetIds.includes(item.id)) ?? -1
|
|
|
+ if (index !== -1) {
|
|
|
+ child.children.splice(index, 1)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 复制
|
|
|
+ *
|
|
|
+ */
|
|
|
+ const onCopy = () => {
|
|
|
+ clipboard.value = klona(projectStore.activeWidgets)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 复用
|
|
|
+ */
|
|
|
+ const onCopyFrom = () => {
|
|
|
+ const widgetIds = projectStore.activeWidgets.map((widget) => widget.id)
|
|
|
+ if (!widgetIds.length) return
|
|
|
+
|
|
|
+ bfsWalk(projectStore.activePage, (child) => {
|
|
|
+ const obj = child?.children?.find((item) => widgetIds.includes(item.id))
|
|
|
+ if (obj) {
|
|
|
+ const newWidget = klona(obj)
|
|
|
+ newWidget.id = v4()
|
|
|
+ if (!newWidget.isCopy) {
|
|
|
+ newWidget.isCopy = true
|
|
|
+ projectStore.project?.widgets?.push(newWidget)
|
|
|
+ }
|
|
|
+ obj.copyFrom = newWidget.id
|
|
|
+ obj.isCopy = true
|
|
|
+ // 复制一份复用的
|
|
|
+ child.children.unshift({
|
|
|
+ ...newWidget,
|
|
|
+ id: v4()
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 锁定/解锁
|
|
|
+ */
|
|
|
+ const onLock = (lock: boolean) => {
|
|
|
+ const widgetIds = projectStore.activeWidgets.map((widget) => widget.id)
|
|
|
+ if (!widgetIds.length) return
|
|
|
+
|
|
|
+ bfsWalk(projectStore.activePage, (child) => {
|
|
|
+ const obj = child?.children?.find((item) => widgetIds.includes(item.id))
|
|
|
+ if (obj) {
|
|
|
+ obj.locked = lock
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 粘贴
|
|
|
+ */
|
|
|
+ const onPaste = () => {
|
|
|
+ if (!clipboard.value?.length) return
|
|
|
+
|
|
|
+ const list = projectStore.activeWidgets?.[0]?.children || projectStore.activePage?.children
|
|
|
+ clipboard.value.forEach((obj) => {
|
|
|
+ obj.x += 10
|
|
|
+ obj.y += 10
|
|
|
+ const newWidget = klona(obj)
|
|
|
+ list.unshift({
|
|
|
+ ...newWidget,
|
|
|
+ id: v4(),
|
|
|
+ events: []
|
|
|
+ })
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 剪切
|
|
|
+ */
|
|
|
+ const onCut = () => {
|
|
|
+ const widgetIds = projectStore.activeWidgets.map((widget) => widget.id)
|
|
|
+ if (!widgetIds.length) return
|
|
|
+ clipboard.value = []
|
|
|
+ bfsWalk(projectStore.activePage, (child) => {
|
|
|
+ const index = child?.children?.findIndex((item) => widgetIds.includes(item.id)) ?? -1
|
|
|
+ if (index != -1) {
|
|
|
+ clipboard.value?.push(child.children[index])
|
|
|
+ child.children.splice(index, 1)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 隐藏/显示
|
|
|
+ */
|
|
|
+ const onHidden = (hidden: boolean) => {
|
|
|
+ const widgetIds = projectStore.activeWidgets.map((widget) => widget.id)
|
|
|
+ if (!widgetIds.length) return
|
|
|
+
|
|
|
+ bfsWalk(projectStore.activePage, (child) => {
|
|
|
+ const obj = child?.children?.find((item) => widgetIds.includes(item.id))
|
|
|
+ if (obj) {
|
|
|
+ obj.hidden = hidden
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 显示事件
|
|
|
+ */
|
|
|
+ const onShowEvent = () => {
|
|
|
+ appStore.showComposite = true
|
|
|
+ appStore.compositeTabAcitve = 'event'
|
|
|
+ }
|
|
|
|
|
|
return {
|
|
|
onAlign,
|
|
|
onMatchSize,
|
|
|
- onLevel
|
|
|
+ onLevel,
|
|
|
+ onDeletePage,
|
|
|
+ onDelete,
|
|
|
+ onDeleteById,
|
|
|
+ onCopy,
|
|
|
+ onCopyFrom,
|
|
|
+ onLock,
|
|
|
+ onPaste,
|
|
|
+ onCut,
|
|
|
+ onHidden,
|
|
|
+ onShowEvent
|
|
|
}
|
|
|
})
|