|
1 | 1 | import Element from '../../components/core/models/element'
|
2 |
| -import { getEditorConfigForEditingElement } from '../../utils/element' |
| 2 | +import { getEditorConfigForEditingElement, swapZindex } from '../../utils/element' |
3 | 3 |
|
4 | 4 | // actions
|
5 | 5 | export const actions = {
|
@@ -38,23 +38,54 @@ export const mutations = {
|
38 | 38 | }
|
39 | 39 | },
|
40 | 40 | elementManager (state, { type, value }) {
|
| 41 | + const { editingPage, editingElement } = state |
| 42 | + const elements = editingPage.elements |
| 43 | + const len = elements.length |
| 44 | + |
41 | 45 | switch (type) {
|
42 | 46 | case 'add':
|
| 47 | + value = { |
| 48 | + ...value, |
| 49 | + zindex: len + 1 |
| 50 | + } |
43 | 51 | const element = new Element(value)
|
44 |
| - state.editingPage.elements.push(element) |
| 52 | + elements.push(element) |
45 | 53 | break
|
46 | 54 | case 'copy':
|
47 |
| - state.editingPage.elements.push(state.editingElement.clone()) |
| 55 | + elements.push(state.editingElement.clone()) |
48 | 56 | break
|
49 | 57 | 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 | + }) |
55 | 76 | state.editingPage.elements = newElements
|
56 | 77 | }
|
57 | 78 | 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 |
58 | 89 | default:
|
59 | 90 | }
|
60 | 91 | },
|
|
0 commit comments