Skip to content

Commit a4b13f8

Browse files
committed
feat: add the right-click menu, step1: just show menu
1 parent a7875cb commit a4b13f8

File tree

1 file changed

+44
-2
lines changed
  • front-end/h5/src/components/core/editor/canvas

1 file changed

+44
-2
lines changed

front-end/h5/src/components/core/editor/canvas/edit.js

+44-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ export default {
33
props: ['elements', 'editingElement', 'handleClickElementProp', 'handleClickCanvasProp'],
44
data: () => ({
55
vLines: [],
6-
hLines: []
6+
hLines: [],
7+
contextmenuPos: []
78
}),
89
methods: {
910
// TODO #!zh: 优化代码
@@ -70,6 +71,21 @@ export default {
7071
this.calcX(left)
7172
this.calcY(top)
7273
},
74+
bindContextMenu (e) {
75+
e.preventDefault() // 不显示默认的右击菜单
76+
if (
77+
e.target.classList.contains('element-on-edit-canvas') ||
78+
e.target.parentElement.classList.contains('element-on-edit-canvas')
79+
) {
80+
const { x, y } = this.$el.getBoundingClientRect()
81+
this.contextmenuPos = [e.clientX - x, e.clientY - y]
82+
} else {
83+
this.hideContextMenu()
84+
}
85+
},
86+
hideContextMenu () {
87+
this.contextmenuPos = []
88+
},
7389
/**
7490
* #!zh: renderCanvas 渲染中间画布
7591
* elements
@@ -82,7 +98,13 @@ export default {
8298
<div
8399
style={{ height: '100%', position: 'relative' }}
84100
class="canvas-editor-wrapper"
85-
onClick={this.handleClickCanvasProp}
101+
onClick={(e) => {
102+
this.hideContextMenu()
103+
this.handleClickCanvasProp(e)
104+
}}
105+
onContextmenu={e => {
106+
this.bindContextMenu(e)
107+
}}
86108
>
87109
{
88110
elements.map((element, index) => {
@@ -131,6 +153,26 @@ export default {
131153
<div class="h-line" style={{ top: `${line.top}px` }}></div>
132154
))
133155
}
156+
{
157+
this.contextmenuPos.length
158+
? <a-menu
159+
style={{
160+
left: this.contextmenuPos[0] + 'px',
161+
top: this.contextmenuPos[1] + 'px',
162+
userSelect: 'none',
163+
position: 'absolute',
164+
zIndex: 999
165+
}}
166+
>
167+
<a-menu-item data-command='copyEditingElement'>复制</a-menu-item>
168+
<a-menu-item data-command="deleteEditingElement">删除</a-menu-item>
169+
<a-menu-item data-command="bringLayer2Front">置顶</a-menu-item>
170+
<a-menu-item data-command="bringLayer2End">置底</a-menu-item>
171+
<a-menu-item data-command="addLayerZindex">上移</a-menu-item>
172+
<a-menu-item data-command="subtractLayerZindex">下移</a-menu-item>
173+
</a-menu>
174+
: null
175+
}
134176
</div>
135177
)
136178
}

0 commit comments

Comments
 (0)