Commit f6ceedddfccd8c9bc917d58ebb688de2c7351917
1 parent
a43ae787
fix: change element zindex on edit canvas
Showing
4 changed files
with
57 additions
and
12 deletions
front-end/h5/src/components/core/editor/canvas/edit.js
| ... | ... | @@ -12,11 +12,11 @@ const contextmenuOptions = [ |
| 12 | 12 | }, |
| 13 | 13 | { |
| 14 | 14 | label: '置顶', |
| 15 | - value: 'bring2Top' | |
| 15 | + value: 'move2Top' | |
| 16 | 16 | }, |
| 17 | 17 | { |
| 18 | 18 | label: '置底', |
| 19 | - value: 'bring2Bottom' | |
| 19 | + value: 'move2Bottom' | |
| 20 | 20 | }, |
| 21 | 21 | { |
| 22 | 22 | label: '上移', |
| ... | ... | @@ -203,6 +203,7 @@ export default { |
| 203 | 203 | }} |
| 204 | 204 | > |
| 205 | 205 | {h(element.name, data)} |
| 206 | + {element.commonStyle.zindex} | |
| 206 | 207 | </Shape> |
| 207 | 208 | ) |
| 208 | 209 | }) | ... | ... |
front-end/h5/src/components/core/models/element.js
| ... | ... | @@ -28,7 +28,12 @@ class Element { |
| 28 | 28 | * element.pluginProps 和 elementcommonStyle 是引用类型,如果不做 deep_clone 可能会出现意外错误 |
| 29 | 29 | */ |
| 30 | 30 | 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 | + | |
| 32 | 37 | this.events = [] |
| 33 | 38 | } |
| 34 | 39 | |
| ... | ... | @@ -76,8 +81,9 @@ class Element { |
| 76 | 81 | |
| 77 | 82 | } |
| 78 | 83 | |
| 79 | - clone () { | |
| 84 | + clone ({ zindex }) { | |
| 80 | 85 | return new Element({ |
| 86 | + zindex, | |
| 81 | 87 | name: this.name, |
| 82 | 88 | pluginProps: this.pluginProps, |
| 83 | 89 | commonStyle: { | ... | ... |
front-end/h5/src/store/modules/element.js
| 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 | }, | ... | ... |
front-end/h5/src/utils/element.js
| ... | ... | @@ -5,3 +5,10 @@ export function getEditorConfigForEditingElement (elementName) { |
| 5 | 5 | // TODO 为何直接 return new Ctor() 并将其赋值给 vuex 的 state 会报错:Cannot convert a Symbol value to a string |
| 6 | 6 | return new Ctor().$options.editorConfig |
| 7 | 7 | } |
| 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 | +} | ... | ... |