Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复空页面执行右键删除和右键复制时控制台报错的问题 #103

Merged
merged 11 commits into from
Nov 30, 2023
57 changes: 39 additions & 18 deletions packages/canvas/src/components/container/CanvasMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
<li
v-for="(item, index) in menus"
:key="index"
:class="item.items ? 'li-item' : ''"
:class="{
'li-item': item.items,
'li-item-disabled': actionDisabled(item)
}"
@click="doOperation(item)"
@mouseover="current = item"
@mouseover="onShowChildrenMenu(item)"
>
<div>
<span>{{ item.name }}</span>
Expand Down Expand Up @@ -98,7 +101,8 @@ export default {
items: [
{ name: '文字提示', code: 'wrap', value: 'TinyTooltip' },
{ name: '弹出框', code: 'wrap', value: 'TinyPopover' }
]
],
code: 'addParent'
},
{ name: '删除', code: 'del' },
{ name: '复制', code: 'copy' },
Expand All @@ -111,10 +115,10 @@ export default {

const operations = {
del() {
removeNodeById(getCurrent().schema.id)
removeNodeById(getCurrent().schema?.id)
gargameljyh marked this conversation as resolved.
Show resolved Hide resolved
},
copy() {
copyNode(getCurrent().schema.id)
copyNode(getCurrent().schema?.id)
},
config() {
useLayout().activeSetting('props')
Expand All @@ -128,17 +132,19 @@ export default {
wrap({ value, name }) {
const componentName = value || name
const { schema, parent } = getCurrent()
const index = parent.children.indexOf(schema)
const wrapSchema = {
componentName,
id: null,
props: {},
children: [schema]
if (schema && parent) {
const index = parent.children.indexOf(schema)
const wrapSchema = {
componentName,
id: null,
props: {},
children: [schema]
}

parent.children.splice(index, 1, wrapSchema)

getController().addHistory()
}

parent.children.splice(index, 1, wrapSchema)

getController().addHistory()
},
createBlock() {
if (useCanvas().isSaved()) {
Expand All @@ -152,16 +158,25 @@ export default {
}
}

const actionDisabled = (actionItem) => {
const actions = ['del', 'copy', 'addParent']
return actions.includes(actionItem.code) && !getCurrent().schema?.id
}

const onShowChildrenMenu = (menuItem) => {
current.value = !actionDisabled(menuItem) ? menuItem : null
}

const close = () => {
boxVisibility.value = false
}
const doOperation = (item) => {
if (item.check && !item.check?.()) {
if ((item.check && !item.check?.()) || actionDisabled(item)) {
return
}

if (item?.code) {
operations[item.code](item)
operations[item.code]?.(item)
gargameljyh marked this conversation as resolved.
Show resolved Hide resolved
closeMenu()
}
}
Expand All @@ -173,7 +188,9 @@ export default {
boxVisibility,
close,
current,
menuDom
menuDom,
actionDisabled,
onShowChildrenMenu
}
}
}
Expand All @@ -196,6 +213,10 @@ export default {
.li-item {
border-bottom: 1px solid var(--ti-lowcode-canvas-menu-border-color);
}
.li-item-disabled {
cursor: not-allowed;
color: var(--ti-lowcode-canvas-menu-item-disabled-color);
}
li {
& > div {
display: flex;
Expand Down
3 changes: 3 additions & 0 deletions packages/canvas/src/components/container/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
})

export const getContext = () => {
return getRenderer().getContext()

Check failure on line 55 in packages/canvas/src/components/container/container.js

View workflow job for this annotation

GitHub Actions / push-check

'getRenderer' was used before it was defined
}

// 记录拖拽状态
Expand Down Expand Up @@ -110,7 +110,7 @@
// 如果element存在表示在iframe内部拖拽
dragState.element = element
dragState.offset = { offsetX, offsetY, horizontal, vertical, width, height, x, y }
clearHover()

Check failure on line 113 in packages/canvas/src/components/container/container.js

View workflow job for this annotation

GitHub Actions / push-check

'clearHover' was used before it was defined
}

export const clearLineState = () => {
Expand All @@ -130,7 +130,7 @@

// 重置拖拽插入位置状态
clearLineState()
smoothScroll.stop()

Check failure on line 133 in packages/canvas/src/components/container/container.js

View workflow job for this annotation

GitHub Actions / push-check

'smoothScroll' was used before it was defined
}

export const getOffset = (element) => {
Expand Down Expand Up @@ -209,9 +209,9 @@
return
}

removeNode(getNode(id, true))

Check failure on line 212 in packages/canvas/src/components/container/container.js

View workflow job for this annotation

GitHub Actions / push-check

'getNode' was used before it was defined
clearSelect()

Check failure on line 213 in packages/canvas/src/components/container/container.js

View workflow job for this annotation

GitHub Actions / push-check

'clearSelect' was used before it was defined
getController().addHistory()

Check failure on line 214 in packages/canvas/src/components/container/container.js

View workflow job for this annotation

GitHub Actions / push-check

'getController' was used before it was defined
canvasState.emit('remove')
}

Expand All @@ -237,19 +237,22 @@
}
}

select && setTimeout(() => selectNode(node.data.id))

Check failure on line 240 in packages/canvas/src/components/container/container.js

View workflow job for this annotation

GitHub Actions / push-check

'selectNode' was used before it was defined

getController().addHistory()

Check failure on line 242 in packages/canvas/src/components/container/container.js

View workflow job for this annotation

GitHub Actions / push-check

'getController' was used before it was defined
}

export const addComponent = (data, position) => {
const { schema, parent } = getCurrent()

Check failure on line 246 in packages/canvas/src/components/container/container.js

View workflow job for this annotation

GitHub Actions / push-check

'getCurrent' was used before it was defined

insertNode({ node: schema, parent, data }, position)
}

export const copyNode = (id) => {
if (!id) {
return
}
const { node, parent } = getNode(id, true)

Check failure on line 255 in packages/canvas/src/components/container/container.js

View workflow job for this annotation

GitHub Actions / push-check

'getNode' was used before it was defined

inserAfter({ parent, node, data: copyObject(node) })
getController().addHistory()
Expand Down