|
|
@ -53,8 +53,8 @@ |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</template> |
|
|
|
</template> |
|
|
|
|
|
|
|
|
|
|
|
<script lang="tsx"> |
|
|
|
<script lang="tsx" setup> |
|
|
|
import { defineComponent, reactive, watchEffect, toRefs } from 'vue'; |
|
|
|
import { ref, watchEffect } from 'vue'; |
|
|
|
import { cloneDeep } from 'lodash-es'; |
|
|
|
import { cloneDeep } from 'lodash-es'; |
|
|
|
import DraggableTransitionGroup from './draggable-transition-group.vue'; |
|
|
|
import DraggableTransitionGroup from './draggable-transition-group.vue'; |
|
|
|
import CompRender from './comp-render'; |
|
|
|
import CompRender from './comp-render'; |
|
|
@ -67,196 +67,176 @@ |
|
|
|
import { useModal } from '@/visual-editor/hooks/useModal'; |
|
|
|
import { useModal } from '@/visual-editor/hooks/useModal'; |
|
|
|
import { generateNanoid } from '@/visual-editor/utils'; |
|
|
|
import { generateNanoid } from '@/visual-editor/utils'; |
|
|
|
|
|
|
|
|
|
|
|
export default defineComponent({ |
|
|
|
defineOptions({ |
|
|
|
name: 'SimulatorEditor', |
|
|
|
name: 'SimulatorEditor', |
|
|
|
components: { |
|
|
|
}); |
|
|
|
DraggableTransitionGroup, |
|
|
|
|
|
|
|
CompRender, |
|
|
|
|
|
|
|
SlotItem, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
emits: ['on-selected'], |
|
|
|
|
|
|
|
setup() { |
|
|
|
|
|
|
|
const { currentPage, setCurrentBlock } = useVisualData(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { globalProperties } = useGlobalProperties(); |
|
|
|
const { currentPage, setCurrentBlock } = useVisualData(); |
|
|
|
|
|
|
|
|
|
|
|
const state = reactive({ |
|
|
|
const { globalProperties } = useGlobalProperties(); |
|
|
|
drag: false, |
|
|
|
|
|
|
|
}); |
|
|
|
const drag = ref(false); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @description 操作当前页面样式表 |
|
|
|
* @description 操作当前页面样式表 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
watchEffect(() => { |
|
|
|
watchEffect(() => { |
|
|
|
const { bgImage, bgColor } = currentPage.value.config; |
|
|
|
const { bgImage, bgColor } = currentPage.value.config; |
|
|
|
const bodyStyleStr = ` |
|
|
|
const bodyStyleStr = ` |
|
|
|
.simulator-editor-content { |
|
|
|
.simulator-editor-content { |
|
|
|
background-color: ${bgColor}; |
|
|
|
background-color: ${bgColor}; |
|
|
|
background-image: url(${bgImage}); |
|
|
|
background-image: url(${bgImage}); |
|
|
|
}`; |
|
|
|
}`; |
|
|
|
const styleSheets = document.styleSheets[0]; |
|
|
|
const styleSheets = document.styleSheets[0]; |
|
|
|
const firstCssRule = document.styleSheets[0].cssRules[0]; |
|
|
|
const firstCssRule = document.styleSheets[0].cssRules[0]; |
|
|
|
const isExistContent = firstCssRule.cssText.includes('.simulator-editor-content'); |
|
|
|
const isExistContent = firstCssRule.cssText.includes('.simulator-editor-content'); |
|
|
|
if (isExistContent) { |
|
|
|
if (isExistContent) { |
|
|
|
styleSheets.deleteRule(0); |
|
|
|
styleSheets.deleteRule(0); |
|
|
|
} |
|
|
|
} |
|
|
|
styleSheets.insertRule(bodyStyleStr); |
|
|
|
styleSheets.insertRule(bodyStyleStr); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
//递归实现 |
|
|
|
//递归实现 |
|
|
|
//@leafId 为你要查找的id, |
|
|
|
//@leafId 为你要查找的id, |
|
|
|
//@nodes 为原始Json数据 |
|
|
|
//@nodes 为原始Json数据 |
|
|
|
//@path 供递归使用,不要赋值 |
|
|
|
//@path 供递归使用,不要赋值 |
|
|
|
const findPathByLeafId = ( |
|
|
|
const findPathByLeafId = ( |
|
|
|
leafId, |
|
|
|
leafId, |
|
|
|
nodes: VisualEditorBlockData[] = [], |
|
|
|
nodes: VisualEditorBlockData[] = [], |
|
|
|
path: VisualEditorBlockData[] = [], |
|
|
|
path: VisualEditorBlockData[] = [], |
|
|
|
) => { |
|
|
|
) => { |
|
|
|
for (let i = 0; i < nodes.length; i++) { |
|
|
|
for (let i = 0; i < nodes.length; i++) { |
|
|
|
const tmpPath = path.concat(); |
|
|
|
const tmpPath = path.concat(); |
|
|
|
tmpPath.push(nodes[i]); |
|
|
|
tmpPath.push(nodes[i]); |
|
|
|
if (leafId == nodes[i]._vid) { |
|
|
|
if (leafId == nodes[i]._vid) { |
|
|
|
return tmpPath; |
|
|
|
return tmpPath; |
|
|
|
} |
|
|
|
} |
|
|
|
const slots = nodes[i].props?.slots || {}; |
|
|
|
const slots = nodes[i].props?.slots || {}; |
|
|
|
const keys = Object.keys(slots); |
|
|
|
const keys = Object.keys(slots); |
|
|
|
for (let j = 0; j < keys.length; j++) { |
|
|
|
for (let j = 0; j < keys.length; j++) { |
|
|
|
const children = slots[keys[j]]?.children; |
|
|
|
const children = slots[keys[j]]?.children; |
|
|
|
if (children) { |
|
|
|
if (children) { |
|
|
|
const findResult = findPathByLeafId(leafId, children, tmpPath); |
|
|
|
const findResult = findPathByLeafId(leafId, children, tmpPath); |
|
|
|
if (findResult) { |
|
|
|
if (findResult) { |
|
|
|
return findResult; |
|
|
|
return findResult; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
// 给当前点击的组件设置聚焦 |
|
|
|
}; |
|
|
|
const handleSlotsFocus = (block: VisualEditorBlockData, _vid: string) => { |
|
|
|
|
|
|
|
const slots = block.props?.slots || {}; |
|
|
|
|
|
|
|
if (Object.keys(slots).length > 0) { |
|
|
|
|
|
|
|
Object.keys(slots).forEach((key) => { |
|
|
|
|
|
|
|
slots[key]?.children?.forEach((item) => { |
|
|
|
|
|
|
|
item.focusWithChild = false; |
|
|
|
|
|
|
|
item.focus = item._vid == _vid; |
|
|
|
|
|
|
|
if (item.focus) { |
|
|
|
|
|
|
|
const arr = findPathByLeafId(_vid, currentPage.value.blocks); |
|
|
|
|
|
|
|
arr.forEach((n) => (n.focusWithChild = true)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (Object.keys(item.props?.slots || {}).length) { |
|
|
|
|
|
|
|
handleSlotsFocus(item, _vid); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 选择要操作的组件 |
|
|
|
// 给当前点击的组件设置聚焦 |
|
|
|
const selectComp = (element: VisualEditorBlockData) => { |
|
|
|
const handleSlotsFocus = (block: VisualEditorBlockData, _vid: string) => { |
|
|
|
setCurrentBlock(element); |
|
|
|
const slots = block.props?.slots || {}; |
|
|
|
currentPage.value.blocks.forEach((block) => { |
|
|
|
if (Object.keys(slots).length > 0) { |
|
|
|
block.focus = element._vid == block._vid; |
|
|
|
Object.keys(slots).forEach((key) => { |
|
|
|
block.focusWithChild = false; |
|
|
|
slots[key]?.children?.forEach((item) => { |
|
|
|
handleSlotsFocus(block, element._vid); |
|
|
|
item.focusWithChild = false; |
|
|
|
element.focusWithChild = false; |
|
|
|
item.focus = item._vid == _vid; |
|
|
|
|
|
|
|
if (item.focus) { |
|
|
|
|
|
|
|
const arr = findPathByLeafId(_vid, currentPage.value.blocks); |
|
|
|
|
|
|
|
arr.forEach((n) => (n.focusWithChild = true)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (Object.keys(item.props?.slots || {}).length) { |
|
|
|
|
|
|
|
handleSlotsFocus(item, _vid); |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
// 选择要操作的组件 |
|
|
|
* 删除组件 |
|
|
|
const selectComp = (element: VisualEditorBlockData) => { |
|
|
|
*/ |
|
|
|
setCurrentBlock(element); |
|
|
|
const deleteComp = ( |
|
|
|
currentPage.value.blocks.forEach((block) => { |
|
|
|
block: VisualEditorBlockData, |
|
|
|
block.focus = element._vid == block._vid; |
|
|
|
parentBlocks = currentPage.value.blocks, |
|
|
|
block.focusWithChild = false; |
|
|
|
) => { |
|
|
|
handleSlotsFocus(block, element._vid); |
|
|
|
console.log(block, 'block'); |
|
|
|
element.focusWithChild = false; |
|
|
|
const index = parentBlocks.findIndex((item) => item._vid == block._vid); |
|
|
|
}); |
|
|
|
if (index != -1) { |
|
|
|
}; |
|
|
|
delete globalProperties.$$refs[parentBlocks[index]._vid]; |
|
|
|
|
|
|
|
const delTarget = parentBlocks.splice(index, 1)[0]; |
|
|
|
|
|
|
|
if (delTarget.focus) { |
|
|
|
|
|
|
|
setCurrentBlock({} as VisualEditorBlockData); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const onContextmenuBlock = ( |
|
|
|
/** |
|
|
|
e: MouseEvent, |
|
|
|
* 删除组件 |
|
|
|
block: VisualEditorBlockData, |
|
|
|
*/ |
|
|
|
parentBlocks = currentPage.value.blocks, |
|
|
|
const deleteComp = (block: VisualEditorBlockData, parentBlocks = currentPage.value.blocks) => { |
|
|
|
) => { |
|
|
|
console.log(block, 'block'); |
|
|
|
$$dropdown({ |
|
|
|
const index = parentBlocks.findIndex((item) => item._vid == block._vid); |
|
|
|
reference: e, |
|
|
|
if (index != -1) { |
|
|
|
content: () => ( |
|
|
|
delete globalProperties.$$refs[parentBlocks[index]._vid]; |
|
|
|
<> |
|
|
|
const delTarget = parentBlocks.splice(index, 1)[0]; |
|
|
|
<DropdownOption |
|
|
|
if (delTarget.focus) { |
|
|
|
label="复制节点" |
|
|
|
setCurrentBlock({} as VisualEditorBlockData); |
|
|
|
icon="el-icon-document-copy" |
|
|
|
} |
|
|
|
{...{ |
|
|
|
} |
|
|
|
onClick: () => { |
|
|
|
}; |
|
|
|
const index = parentBlocks.findIndex((item) => item._vid == block._vid); |
|
|
|
|
|
|
|
if (index != -1) { |
|
|
|
const onContextmenuBlock = ( |
|
|
|
const setBlockVid = (block: VisualEditorBlockData) => { |
|
|
|
e: MouseEvent, |
|
|
|
block._vid = `vid_${generateNanoid()}`; |
|
|
|
block: VisualEditorBlockData, |
|
|
|
block.focus = false; |
|
|
|
parentBlocks = currentPage.value.blocks, |
|
|
|
const slots = block?.props?.slots || {}; |
|
|
|
) => { |
|
|
|
const slotKeys = Object.keys(slots); |
|
|
|
$$dropdown({ |
|
|
|
if (slotKeys.length) { |
|
|
|
reference: e, |
|
|
|
slotKeys.forEach((slotKey) => { |
|
|
|
content: () => ( |
|
|
|
slots[slotKey]?.children?.forEach((child) => setBlockVid(child)); |
|
|
|
<> |
|
|
|
}); |
|
|
|
<DropdownOption |
|
|
|
} |
|
|
|
label="复制节点" |
|
|
|
}; |
|
|
|
icon="el-icon-document-copy" |
|
|
|
const blockCopy = cloneDeep(parentBlocks[index]); |
|
|
|
{...{ |
|
|
|
setBlockVid(blockCopy); |
|
|
|
onClick: () => { |
|
|
|
parentBlocks.splice(index + 1, 0, blockCopy); |
|
|
|
const index = parentBlocks.findIndex((item) => item._vid == block._vid); |
|
|
|
|
|
|
|
if (index != -1) { |
|
|
|
|
|
|
|
const setBlockVid = (block: VisualEditorBlockData) => { |
|
|
|
|
|
|
|
block._vid = `vid_${generateNanoid()}`; |
|
|
|
|
|
|
|
block.focus = false; |
|
|
|
|
|
|
|
const slots = block?.props?.slots || {}; |
|
|
|
|
|
|
|
const slotKeys = Object.keys(slots); |
|
|
|
|
|
|
|
if (slotKeys.length) { |
|
|
|
|
|
|
|
slotKeys.forEach((slotKey) => { |
|
|
|
|
|
|
|
slots[slotKey]?.children?.forEach((child) => setBlockVid(child)); |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
const blockCopy = cloneDeep(parentBlocks[index]); |
|
|
|
|
|
|
|
setBlockVid(blockCopy); |
|
|
|
|
|
|
|
parentBlocks.splice(index + 1, 0, blockCopy); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}} |
|
|
|
|
|
|
|
/> |
|
|
|
|
|
|
|
<DropdownOption |
|
|
|
|
|
|
|
label="查看节点" |
|
|
|
|
|
|
|
icon="el-icon-view" |
|
|
|
|
|
|
|
{...{ |
|
|
|
|
|
|
|
onClick: () => |
|
|
|
|
|
|
|
useModal({ |
|
|
|
|
|
|
|
title: '节点信息', |
|
|
|
|
|
|
|
footer: null, |
|
|
|
|
|
|
|
props: { |
|
|
|
|
|
|
|
width: 600, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}} |
|
|
|
content: () => ( |
|
|
|
/> |
|
|
|
<MonacoEditor |
|
|
|
<DropdownOption |
|
|
|
code={JSON.stringify(block)} |
|
|
|
label="查看节点" |
|
|
|
layout={{ width: 530, height: 600 }} |
|
|
|
icon="el-icon-view" |
|
|
|
vid={block._vid} |
|
|
|
{...{ |
|
|
|
/> |
|
|
|
onClick: () => |
|
|
|
), |
|
|
|
useModal({ |
|
|
|
}), |
|
|
|
title: '节点信息', |
|
|
|
}} |
|
|
|
footer: null, |
|
|
|
/> |
|
|
|
props: { |
|
|
|
<DropdownOption |
|
|
|
width: 600, |
|
|
|
label="删除节点" |
|
|
|
}, |
|
|
|
icon="el-icon-delete" |
|
|
|
content: () => ( |
|
|
|
{...{ |
|
|
|
<MonacoEditor |
|
|
|
onClick: () => deleteComp(block, parentBlocks), |
|
|
|
code={JSON.stringify(block)} |
|
|
|
}} |
|
|
|
layout={{ width: 530, height: 600 }} |
|
|
|
/> |
|
|
|
vid={block._vid} |
|
|
|
</> |
|
|
|
/> |
|
|
|
), |
|
|
|
), |
|
|
|
}); |
|
|
|
}), |
|
|
|
}; |
|
|
|
}} |
|
|
|
|
|
|
|
/> |
|
|
|
|
|
|
|
<DropdownOption |
|
|
|
|
|
|
|
label="删除节点" |
|
|
|
|
|
|
|
icon="el-icon-delete" |
|
|
|
|
|
|
|
{...{ |
|
|
|
|
|
|
|
onClick: () => deleteComp(block, parentBlocks), |
|
|
|
|
|
|
|
}} |
|
|
|
|
|
|
|
/> |
|
|
|
|
|
|
|
</> |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
...toRefs(state), |
|
|
|
|
|
|
|
currentPage, |
|
|
|
|
|
|
|
deleteComp, |
|
|
|
|
|
|
|
selectComp, |
|
|
|
|
|
|
|
onContextmenuBlock, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
</script> |
|
|
|
</script> |
|
|
|
<style lang="scss" scoped> |
|
|
|
<style lang="scss" scoped> |
|
|
|
@import './func.scss'; |
|
|
|
@import './func.scss'; |
|
|
|