Skip to content

Commit 9e44e40

Browse files
committed
feat: support copy plugin on canvas
1 parent 42c972b commit 9e44e40

File tree

4 files changed

+99
-21
lines changed

4 files changed

+99
-21
lines changed

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

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
import { mapState, mapActions } from 'vuex'
22
import Shape from '../../support/shape'
33

4+
const contextmenuOptions = [
5+
{
6+
label: '复制',
7+
value: 'copy'
8+
},
9+
{
10+
label: '删除',
11+
value: 'delete'
12+
},
13+
{
14+
label: '置顶',
15+
value: 'bring2Top'
16+
},
17+
{
18+
label: '置底',
19+
value: 'bring2Bottom'
20+
},
21+
{
22+
label: '上移',
23+
value: 'addZindex'
24+
},
25+
{
26+
label: '下移',
27+
value: 'minusZindex'
28+
}
29+
]
30+
431
export default {
532
props: ['elements', 'handleClickElementProp', 'handleClickCanvasProp'],
633
data: () => ({
@@ -17,7 +44,8 @@ export default {
1744
...mapActions('element', [
1845
'setEditingElement', // -> this.foo()
1946
'setElementPosition', // -> this.foo()
20-
'setElementShape' // -> this.foo()
47+
'setElementShape', // -> this.foo()
48+
'elementManager'
2149
]),
2250
// TODO #!zh: 优化代码
2351
// generate vertical line
@@ -187,6 +215,9 @@ export default {
187215
{
188216
this.contextmenuPos.length
189217
? <a-menu
218+
onSelect={({ item, key, selectedKeys }) => {
219+
this.elementManager({ type: key })
220+
}}
190221
style={{
191222
left: this.contextmenuPos[0] + 'px',
192223
top: this.contextmenuPos[1] + 'px',
@@ -195,12 +226,7 @@ export default {
195226
zIndex: 999
196227
}}
197228
>
198-
<a-menu-item data-command='copyEditingElement'>复制</a-menu-item>
199-
<a-menu-item data-command="deleteEditingElement">删除</a-menu-item>
200-
<a-menu-item data-command="bringLayer2Front">置顶</a-menu-item>
201-
<a-menu-item data-command="bringLayer2End">置底</a-menu-item>
202-
<a-menu-item data-command="addLayerZindex">上移</a-menu-item>
203-
<a-menu-item data-command="subtractLayerZindex">下移</a-menu-item>
229+
{ contextmenuOptions.map(option => <a-menu-item key={option.value} data-command={option.value}>{option.label}</a-menu-item>) }
204230
</a-menu>
205231
: null
206232
}

front-end/h5/src/components/core/editor/index.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Vue from 'vue'
2-
import Element from '../models/element'
2+
import { mapState, mapActions } from 'vuex'
3+
// import Element from '../models/element'
4+
35
import '../styles/index.scss'
46

57
import RenderEditCanvas from './canvas/edit'
@@ -30,10 +32,19 @@ export default {
3032
data: () => ({
3133
activeMenuKey: 'pluginList',
3234
pages: [],
33-
elements: [],
35+
// elements: [],
3436
isPreviewMode: false
3537
}),
38+
computed: {
39+
...mapState('element', {
40+
editingElement: state => state.editingElement,
41+
elements: state => state.elementsOfCurrentPage
42+
})
43+
},
3644
methods: {
45+
...mapActions('element', [
46+
'elementManager'
47+
]),
3748
getEditorConfig (pluginName) {
3849
// const pluginCtor = Vue.options[pluginName]
3950
// const pluginCtor = this.$options.components[pluginName]
@@ -49,7 +60,11 @@ export default {
4960
const zindex = this.elements.length + 1
5061
// const defaultPropsValue = this.getPropsDefaultValue(name)
5162
const editorConfig = this.getEditorConfig(name)
52-
this.elements.push(new Element({ name, zindex, editorConfig }))
63+
// this.elements.push(new Element({ name, zindex, editorConfig }))
64+
this.elementManager({
65+
type: 'add',
66+
value: { name, zindex, editorConfig }
67+
})
5368
}
5469
},
5570
render (h) {

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

+29-10
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,34 @@ class Element {
1616
this.name = ele.name
1717
this.uuid = +new Date()
1818
this.editorConfig = ele.editorConfig || {}
19-
this.pluginProps = {}
20-
this.commonStyle = {}
21-
this.init()
19+
/**
20+
* 之前版本代码:https://github.com/ly525/luban-h5/blob/a7875cbc73c0d18bc2459985ca3ce1d4dc44f141/front-end/h5/src/components/core/models/element.js#L21
21+
* 1.之前的版本为:this.pluginProps = {}, 改为下面的版本
22+
* 是因为要支持[复制画布上的元素],所以需要先使用 ele.pluginProps 进行初始化(也就是拷贝之前的元素的值)
23+
*
24+
* 2. 移除 this.init() 原因是:如果是 复制元素,则 init 会把 copy 的值重新覆盖为初始值,copy 无效
25+
*/
26+
this.pluginProps = ele.pluginProps || this.getDefaultPluginProps()
27+
this.commonStyle = ele.commonStyle || this.getDefaultCommonStyle()
2228
}
2329

24-
init () {
25-
const commonStyle = this.commonStyle
26-
// init common props
27-
Object.keys(defaultProps).forEach(key => {
28-
commonStyle[key] = defaultProps[key]
29-
})
30+
getDefaultCommonStyle () {
31+
return { ...defaultProps }
32+
}
3033

34+
getDefaultPluginProps () {
3135
// init prop of plugin
3236
const propConf = this.editorConfig.propsConfig
37+
const pluginProps = {}
3338
Object.keys(propConf).forEach(key => {
3439
// #6
3540
if (key === 'name') {
3641
console.warn('Please do not use {name} as plugin prop')
3742
return
3843
}
39-
this.pluginProps[key] = propConf[key].defaultPropValue
44+
pluginProps[key] = propConf[key].defaultPropValue
4045
})
46+
return pluginProps
4147
}
4248

4349
getStyle (position = 'static') {
@@ -64,6 +70,19 @@ class Element {
6470
getData () {
6571

6672
}
73+
74+
clone () {
75+
return new Element({
76+
name: this.name,
77+
editorConfig: this.editorConfig,
78+
pluginProps: JSON.parse(JSON.stringify(this.pluginProps)),
79+
commonStyle: {
80+
...this.commonStyle,
81+
top: this.commonStyle.top + 20,
82+
left: this.commonStyle.left + 20
83+
}
84+
})
85+
}
6786
}
6887

6988
export default Element

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// initial state
2+
import Element from '../../components/core/models/element'
3+
24
const state = {
3-
editingElement: null
5+
editingElement: null,
6+
elementsOfCurrentPage: []
47
}
58

69
// getters
@@ -18,6 +21,9 @@ const actions = {
1821
},
1922
setElementShape ({ commit }, payload) {
2023
commit('setElementCommonStyle', payload)
24+
},
25+
elementManager ({ commit }, payload) {
26+
commit('elementManager', payload)
2127
}
2228
}
2329

@@ -31,6 +37,18 @@ const mutations = {
3137
...state.editingElement.commonStyle,
3238
...payload
3339
}
40+
},
41+
elementManager (state, { type, value }) {
42+
switch (type) {
43+
case 'add':
44+
const element = new Element(value)
45+
state.elementsOfCurrentPage.push(element)
46+
break
47+
case 'copy':
48+
state.elementsOfCurrentPage.push(state.editingElement.clone())
49+
break
50+
default:
51+
}
3452
}
3553
}
3654

0 commit comments

Comments
 (0)