Skip to content

Commit c784e56

Browse files
committed
feat(page): support copy, delete page
1 parent abd9e62 commit c784e56

File tree

7 files changed

+33
-11
lines changed

7 files changed

+33
-11
lines changed

README.en.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ English | [简体中文](./README.md)
1515
- [x] Edit Element (Canvas)
1616
- [x] Copy Element (Canvas)
1717
- [x] Delete Element (Canvas)
18-
- [ ] Page: add, copy, delete
19-
- [x] Preview
18+
- [x] Edit Page
19+
- [x] Copy Page
20+
- [x] Delete Page
21+
- [x] Quick Preview
22+
- [x] Undo、Redo
2023

2124
2. Plugin System
2225

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
- [x] 元素: 复制(画布)
2020
- [x] 元素: 删除(画布)
2121
- [x] 元素: 编辑(画布)
22-
- [ ] 页面:新增、复制、删除
22+
- [x] 页面:新增
23+
- [x] 页面:复制
24+
- [x] 页面:删除
2325
- [x] 快速预览
2426
- [x] 撤销、重做
2527

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ export default {
7979
</a>
8080
<a-menu slot="overlay" onClick={({ key }) => { this.pageManager({ type: key }) }}>
8181
<a-menu-item key="add"><a-icon type="user" />新增页面</a-menu-item>
82-
{/* <a-menu-item key="copy"><a-icon type="user" />复制页面</a-menu-item> */}
83-
{/* <a-menu-item key="delete"><a-icon type="user" />删除页面</a-menu-item> */}
82+
<a-menu-item key="copy"><a-icon type="user" />复制页面</a-menu-item>
83+
<a-menu-item key="delete"><a-icon type="user" />删除页面</a-menu-item>
8484
</a-menu>
8585
</a-dropdown>
8686
</span>

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const clone = (value) => JSON.parse(JSON.stringify(value))
12

23
const defaultProps = {
34
top: 100,
@@ -16,14 +17,18 @@ class Element {
1617
this.name = ele.name
1718
this.uuid = +new Date()
1819
/**
20+
* #!zh:
1921
* 之前版本代码:https://github.com/ly525/luban-h5/blob/a7875cbc73c0d18bc2459985ca3ce1d4dc44f141/front-end/h5/src/components/core/models/element.js#L21
2022
* 1.之前的版本为:this.pluginProps = {}, 改为下面的版本
2123
* 是因为要支持[复制画布上的元素],所以需要先使用 ele.pluginProps 进行初始化(也就是拷贝之前的元素的值)
2224
*
2325
* 2. 移除 this.init() 原因是:如果是 复制元素,则 init 会把 copy 的值重新覆盖为初始值,copy 无效
26+
*
27+
* 3. 为何需要 clone,因为会有 element.clone() 以及 page.clone(),
28+
* element.pluginProps 和 elementcommonStyle 是引用类型,如果不做 deep_clone 可能会出现意外错误
2429
*/
25-
this.pluginProps = ele.pluginProps || this.getDefaultPluginProps(ele.editorConfig || {})
26-
this.commonStyle = ele.commonStyle || this.getDefaultCommonStyle()
30+
this.pluginProps = clone(ele.pluginProps) || this.getDefaultPluginProps(ele.editorConfig || {})
31+
this.commonStyle = clone(ele.commonStyle) || this.getDefaultCommonStyle()
2732
this.events = []
2833
}
2934

@@ -74,7 +79,7 @@ class Element {
7479
clone () {
7580
return new Element({
7681
name: this.name,
77-
pluginProps: JSON.parse(JSON.stringify(this.pluginProps)),
82+
pluginProps: this.pluginProps,
7883
commonStyle: {
7984
...this.commonStyle,
8085
top: this.commonStyle.top + 20,
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import Element from '../models/element'
2+
13
class Page {
24
constructor (page = {}) {
5+
this.uuid = +new Date()
36
this.elements = page.elements || []
47
}
8+
9+
clone () {
10+
const elements = this.elements.map(element => new Element(element))
11+
return new Page({ elements })
12+
}
513
}
614

715
export default Page

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ export const mutations = {
2626
state.work.pages.push(state.editingPage.clone())
2727
break
2828
case 'delete':
29+
if (state.work.pages.length === 1) return // #!zh: 作品中至少需要保留一个页面,TODO 需要在页面中提示用户此信息
30+
2931
const { work, editingPage } = state
30-
let index = work.pages.findIndex(e => e.uuid === editingPage.uuid)
32+
let index = work.pages.findIndex(page => page.uuid === editingPage.uuid)
3133
if (index !== -1) {
32-
let newPages = work.slice()
34+
let newPages = work.pages.slice()
3335
newPages.splice(index, 1)
3436
state.work.pages = newPages
3537
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// import Work from '../../components/core/models/work'
22
import Element from '../../components/core/models/element'
33
import strapi from '../../utils/strapi'
4+
import Page from '../../components/core/models/page'
45

56
export const actions = {
67
previewWork ({ commit }, payload = {}) {
@@ -32,8 +33,9 @@ export const actions = {
3233
// mutations
3334
export const mutations = {
3435
setWork (state, work) {
35-
work.pages.forEach(page => {
36+
work.pages = work.pages.map(page => {
3637
page.elements = page.elements.map(element => new Element(element))
38+
return new Page(page)
3739
})
3840
state.work = work
3941
},

0 commit comments

Comments
 (0)