Skip to content

Commit f6ceedd

Browse files
committed
fix: change element zindex on edit canvas
1 parent a43ae78 commit f6ceedd

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ const contextmenuOptions = [
1212
},
1313
{
1414
label: '置顶',
15-
value: 'bring2Top'
15+
value: 'move2Top'
1616
},
1717
{
1818
label: '置底',
19-
value: 'bring2Bottom'
19+
value: 'move2Bottom'
2020
},
2121
{
2222
label: '上移',
@@ -203,6 +203,7 @@ export default {
203203
}}
204204
>
205205
{h(element.name, data)}
206+
{element.commonStyle.zindex}
206207
</Shape>
207208
)
208209
})

front-end/h5/src/components/core/models/element.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ class Element {
2828
* element.pluginProps 和 elementcommonStyle 是引用类型,如果不做 deep_clone 可能会出现意外错误
2929
*/
3030
this.pluginProps = (typeof ele.pluginProps === 'object' && clone(ele.pluginProps)) || this.getDefaultPluginProps(ele.editorConfig || {})
31-
this.commonStyle = (typeof ele.commonStyle === 'object' && clone(ele.commonStyle)) || this.getDefaultCommonStyle()
31+
const commonStyle = (typeof ele.commonStyle === 'object' && clone(ele.commonStyle)) || this.getDefaultCommonStyle()
32+
this.commonStyle = {
33+
...commonStyle,
34+
zindex: ele.zindex
35+
}
36+
3237
this.events = []
3338
}
3439

@@ -76,8 +81,9 @@ class Element {
7681

7782
}
7883

79-
clone () {
84+
clone ({ zindex }) {
8085
return new Element({
86+
zindex,
8187
name: this.name,
8288
pluginProps: this.pluginProps,
8389
commonStyle: {

front-end/h5/src/store/modules/element.js

+39-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Element from '../../components/core/models/element'
2-
import { getEditorConfigForEditingElement } from '../../utils/element'
2+
import { getEditorConfigForEditingElement, swapZindex } from '../../utils/element'
33

44
// actions
55
export const actions = {
@@ -38,23 +38,54 @@ export const mutations = {
3838
}
3939
},
4040
elementManager (state, { type, value }) {
41+
const { editingPage, editingElement } = state
42+
const elements = editingPage.elements
43+
const len = elements.length
44+
4145
switch (type) {
4246
case 'add':
47+
value = {
48+
...value,
49+
zindex: len + 1
50+
}
4351
const element = new Element(value)
44-
state.editingPage.elements.push(element)
52+
elements.push(element)
4553
break
4654
case 'copy':
47-
state.editingPage.elements.push(state.editingElement.clone())
55+
elements.push(state.editingElement.clone())
4856
break
4957
case 'delete':
50-
const { editingPage, editingElement } = state
51-
let index = editingPage.elements.findIndex(e => e.uuid === editingElement.uuid)
52-
if (index !== -1) {
53-
let newElements = editingPage.elements.slice()
54-
newElements.splice(index, 1)
58+
{
59+
const index = elements.findIndex(e => e.uuid === editingElement.uuid)
60+
if (index !== -1) {
61+
let newElements = elements.slice()
62+
newElements.splice(index, 1)
63+
state.editingPage.elements = newElements
64+
}
65+
}
66+
break
67+
case 'move2Top':
68+
case 'move2Bottom':
69+
{
70+
const index = elements.findIndex(e => e.uuid === editingElement.uuid)
71+
elements.splice(index, 1)
72+
const newElements = type === 'move2Top' ? [...elements, editingElement] : [editingElement, ...elements]
73+
newElements.forEach((ele, i, arr) => {
74+
ele.commonStyle.zindex = i + 1
75+
})
5576
state.editingPage.elements = newElements
5677
}
5778
break
79+
case 'addZindex':
80+
case 'minusZindex':
81+
const maxZindex = elements.length
82+
const eleZindex = editingElement.commonStyle.zindex
83+
if (eleZindex === maxZindex || eleZindex === 1) return
84+
85+
const flag = type === 'addZindex' ? 1 : -1
86+
const swapElement = elements.find(ele => ele.commonStyle.zindex === eleZindex + flag * 1)
87+
swapZindex(editingElement, swapElement)
88+
break
5889
default:
5990
}
6091
},

front-end/h5/src/utils/element.js

+7
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ export function getEditorConfigForEditingElement (elementName) {
55
// TODO 为何直接 return new Ctor() 并将其赋值给 vuex 的 state 会报错:Cannot convert a Symbol value to a string
66
return new Ctor().$options.editorConfig
77
}
8+
9+
const styleKey = 'commonStyle'
10+
export function swapZindex (x, y) {
11+
const tmp = y[styleKey].zindex
12+
y[styleKey].zindex = x[styleKey].zindex
13+
x[styleKey].zindex = tmp
14+
}

0 commit comments

Comments
 (0)