Commit da6e73a2e3f90b8eaccbbd8fadce924184b54a49

Authored by ly525
1 parent b751aa6d

use vuex to manage the state of editor

front-end/h5/src/components/core/editor/canvas/edit.js
... ... @@ -36,16 +36,14 @@ export default {
36 36 contextmenuPos: []
37 37 }),
38 38 computed: {
39   - ...mapState('element', {
40   - editingElement: state => state.editingElement
41   - })
  39 + ...mapState('editor', ['editingElement'])
42 40 },
43 41 methods: {
44   - ...mapActions('element', [
45   - 'setEditingElement', // -> this.foo()
46   - 'setElementPosition', // -> this.foo()
47   - 'setElementShape', // -> this.foo()
48   - 'recordElementRect', // -> this.foo()
  42 + ...mapActions('editor', [
  43 + 'setEditingElement',
  44 + 'setElementPosition',
  45 + 'setElementShape',
  46 + 'recordElementRect',
49 47 'elementManager'
50 48 ]),
51 49 // TODO #!zh: 优化代码
... ...
front-end/h5/src/components/core/editor/edit-panel/action.js
... ... @@ -4,13 +4,13 @@ export default {
4 4 data: () => ({
5 5 }),
6 6 computed: {
7   - ...mapState('element', {
8   - editingElement: state => state.editingElement
9   - })
  7 + ...mapState('editor', [
  8 + 'editingElement'
  9 + ])
10 10 },
11 11 methods: {
12   - ...mapActions('element', [
13   - 'setEditingElement' // -> this.foo()
  12 + ...mapActions('editor', [
  13 + 'setEditingElement'
14 14 ])
15 15 },
16 16 created () {},
... ...
front-end/h5/src/components/core/editor/edit-panel/props.js
... ... @@ -2,13 +2,11 @@ import { mapState, mapActions } from 'vuex'
2 2  
3 3 export default {
4 4 computed: {
5   - ...mapState('element', {
6   - editingElement: state => state.editingElement
7   - })
  5 + ...mapState('editor', ['editingElement'])
8 6 },
9 7 methods: {
10   - ...mapActions('element', [
11   - 'setEditingElement' // -> this.foo()
  8 + ...mapActions('editor', [
  9 + 'setEditingElement'
12 10 ]),
13 11 /**
14 12 * 将插件属性的 自定义增强编辑器注入 属性编辑面板中
... ...
front-end/h5/src/components/core/editor/edit-panel/script.js
... ... @@ -32,13 +32,13 @@ export default {
32 32 }`
33 33 }),
34 34 computed: {
35   - ...mapState('element', {
36   - editingElement: state => state.editingElement
37   - })
  35 + ...mapState('editor', [
  36 + 'editingElement'
  37 + ])
38 38 },
39 39 methods: {
40   - ...mapActions('element', [
41   - 'setEditingElement' // -> this.foo()
  40 + ...mapActions('editor', [
  41 + 'setEditingElement'
42 42 ]),
43 43 mixinScript () {
44 44 // TODO mixin script
... ...
front-end/h5/src/components/core/editor/index.js
... ... @@ -34,20 +34,21 @@ export default {
34 34 components: {},
35 35 data: () => ({
36 36 activeMenuKey: 'pluginList',
37   - pages: [],
38   - // elements: [],
39 37 isPreviewMode: false,
40 38 activeTabKey: '属性'
41 39 }),
42 40 computed: {
43   - ...mapState('element', {
  41 + ...mapState('editor', {
44 42 editingElement: state => state.editingElement,
45   - elements: state => state.elementsOfCurrentPage
  43 + elements: state => state.editingPage.elements,
  44 + pages: state => state.work.pages
46 45 })
47 46 },
48 47 methods: {
49   - ...mapActions('element', [
50   - 'elementManager'
  48 + ...mapActions('editor', [
  49 + 'elementManager',
  50 + 'saveWork',
  51 + 'createWork'
51 52 ]),
52 53 getEditorConfig (pluginName) {
53 54 // const pluginCtor = Vue.options[pluginName]
... ... @@ -90,7 +91,7 @@ export default {
90 91 </a-button-group>
91 92 </a-menu-item>
92 93 <a-menu-item key="1" class="transparent-bg"><a-button type="primary" size="small">预览</a-button></a-menu-item>
93   - <a-menu-item key="2" class="transparent-bg"><a-button size="small">保存</a-button></a-menu-item>
  94 + <a-menu-item key="2" class="transparent-bg"><a-button size="small" onClick={() => this.saveWork()}>保存</a-button></a-menu-item>
94 95 <a-menu-item key="3" class="transparent-bg"><a-button size="small">发布</a-button></a-menu-item>
95 96 </a-menu>
96 97 </a-layout-header>
... ... @@ -167,5 +168,13 @@ export default {
167 168 </a-layout>
168 169 </a-layout>
169 170 )
  171 + },
  172 + created () {
  173 + let workId = this.$route.query.workId
  174 + if (workId) {
  175 + // this.$store.dispatch('getWorkById', workId)
  176 + } else {
  177 + this.createWork()
  178 + }
170 179 }
171 180 }
... ...
front-end/h5/src/components/core/models/work.js 0 → 100644
  1 +class Work {
  2 + constructor (work = {}) {
  3 + this.title = work.title || '标题'
  4 + this.description = work.description || '描述'
  5 + this.pages = work.pages || []
  6 + this.type = work.type || 'h5'
  7 +
  8 + this.id = this.id
  9 + this.key = this.key
  10 + this.cover_image_url = ''
  11 + this.project_id = 1
  12 + this.is_publish = false
  13 + this.create_time = new Date()
  14 + this.update_time = new Date()
  15 + }
  16 +}
  17 +
  18 +export default Work
... ...
front-end/h5/src/components/core/styles/index.scss
... ... @@ -67,6 +67,20 @@
67 67 margin-bottom: 12px;
68 68 }
69 69 }
  70 +
  71 + .page-manager-container {
  72 + margin-top: 16px;
  73 + .page-button {
  74 + padding: 18px 8px;
  75 + border-bottom: 1px solid #ebeef5;
  76 + box-sizing: border-box;
  77 + cursor: pointer;
  78 + }
  79 + .active-page {
  80 + color: #2981fa;
  81 + }
  82 +
  83 + }
70 84 }
71 85  
72 86  
... ...
front-end/h5/src/store/index.js
... ... @@ -5,7 +5,6 @@ import editor from &#39;./modules/editor&#39;
5 5 import user from './modules/user'
6 6 import visible from './modules/visible'
7 7 import loading from './modules/loading'
8   -import element from './modules/element'
9 8  
10 9 Vue.use(Vuex)
11 10  
... ... @@ -23,8 +22,7 @@ export default new Vuex.Store({
23 22 editor,
24 23 user,
25 24 visible,
26   - loading,
27   - element
  25 + loading
28 26 },
29 27 plugins: [undoRedoPlugin]
30 28 })
... ...
front-end/h5/src/store/modules/editor.js
1 1 // initial state
2   -const state = {
  2 +import Work from '../../components/core/models/work'
  3 +import { actions as pageActions, mutations as pageMutations } from './page'
  4 +import { actions as elementActions, mutations as elementMutations } from './element'
  5 +import { actions as workActions, mutations as workMutations } from './work'
3 6  
  7 +const state = {
  8 + work: new Work(),
  9 + editingPage: { elements: [] },
  10 + editingElement: null
4 11 }
5 12  
6 13 // getters
7   -const getters = {
8   -
9   -}
  14 +const getters = {}
10 15  
11 16 // actions
12 17 const actions = {
13   -
  18 + ...elementActions,
  19 + ...pageActions,
  20 + ...workActions,
  21 + createWork ({ commit }, payload) {
  22 + commit('createWork')
  23 + commit('pageManager', { type: 'add' })
  24 + commit('setEditingPage')
  25 + }
14 26 }
15 27  
16 28 // mutations
17 29 const mutations = {
18   -
  30 + ...elementMutations,
  31 + ...pageMutations,
  32 + ...workMutations
19 33 }
20 34  
21 35 export default {
... ...
front-end/h5/src/store/modules/element.js
1   -// initial state
2 1 import Element from '../../components/core/models/element'
3 2  
4   -const state = {
5   - editingElement: null,
6   - elementsOfCurrentPage: []
7   -}
8   -
9   -// getters
10   -const getters = {
11   -
12   -}
13   -
14 3 // actions
15   -const actions = {
  4 +export const actions = {
16 5 setEditingElement ({ commit }, payload) {
17 6 commit('setEditingElement', payload)
18 7 },
... ... @@ -31,7 +20,7 @@ const actions = {
31 20 }
32 21  
33 22 // mutations
34   -const mutations = {
  23 +export const mutations = {
35 24 setEditingElement (state, payload) {
36 25 state.editingElement = payload
37 26 },
... ... @@ -45,18 +34,18 @@ const mutations = {
45 34 switch (type) {
46 35 case 'add':
47 36 const element = new Element(value)
48   - state.elementsOfCurrentPage.push(element)
  37 + state.editingPage.elements.push(element)
49 38 break
50 39 case 'copy':
51   - state.elementsOfCurrentPage.push(state.editingElement.clone())
  40 + state.editingPage.elements.push(state.editingElement.clone())
52 41 break
53 42 case 'delete':
54   - const { elementsOfCurrentPage, editingElement } = state
55   - let index = elementsOfCurrentPage.findIndex(e => e.uuid === editingElement.uuid)
  43 + const { editingPage, editingElement } = state
  44 + let index = editingPage.elements.findIndex(e => e.uuid === editingElement.uuid)
56 45 if (index !== -1) {
57   - let newElements = elementsOfCurrentPage.slice()
  46 + let newElements = editingPage.elements.slice()
58 47 newElements.splice(index, 1)
59   - state.elementsOfCurrentPage = newElements
  48 + state.editingPage.elements = newElements
60 49 }
61 50 break
62 51 default:
... ... @@ -66,11 +55,3 @@ const mutations = {
66 55  
67 56 }
68 57 }
69   -
70   -export default {
71   - namespaced: true,
72   - state,
73   - getters,
74   - actions,
75   - mutations
76   -}
... ...
front-end/h5/src/store/modules/page.js
1   -// initial state
2 1 import Page from '../../components/core/models/page'
3 2  
4   -const state = {
5   - editingPage: { pages: [] },
6   - pagesOfCurrentWork: []
7   -}
8   -
9   -// getters
10   -const getters = {
11   -
12   -}
13   -
14 3 // actions
15   -const actions = {
16   - setEditing ({ commit }, payload) {
  4 +export const actions = {
  5 + setEditingPage ({ commit }, payload) {
17 6 commit('setEditing', payload)
18 7 },
19   - manager ({ commit }, payload) {
  8 + pageManager ({ commit }, payload) {
20 9 commit('manager', payload)
21 10 }
22 11 }
23 12  
24 13 // mutations
25   -const mutations = {
26   - setEditing (state, payload) {
27   - state.editing = payload
  14 +export const mutations = {
  15 + setEditingPage (state, payload) {
  16 + payload = payload || state.work.pages[0]
  17 + state.editingPage = payload
28 18 },
29   - manager (state, { type, value }) {
  19 + pageManager (state, { type, value }) {
30 20 switch (type) {
31 21 case 'add':
32 22 const page = new Page(value)
33   - state.pagesOfCurrentWork.push(page)
  23 + state.work.pages.push(page)
34 24 break
35 25 case 'copy':
36   - state.pagesOfCurrentWork.push(state.editing.clone())
  26 + state.work.pages.push(state.editing.clone())
37 27 break
38 28 case 'delete':
39 29 const { pagesOfCurrentWork, editing } = state
... ... @@ -41,18 +31,10 @@ const mutations = {
41 31 if (index !== -1) {
42 32 let newPages = pagesOfCurrentWork.slice()
43 33 newPages.splice(index, 1)
44   - state.pagesOfCurrentWork = newPages
  34 + state.work.pages = newPages
45 35 }
46 36 break
47 37 default:
48 38 }
49 39 }
50 40 }
51   -
52   -export default {
53   - namespaced: true,
54   - state,
55   - getters,
56   - actions,
57   - mutations
58   -}
... ...
front-end/h5/src/store/modules/work.js
1   -class Work {
2   - constructor (work = {}) {
3   - this.title = work.title || '标题'
4   - this.description = work.description || '描述'
5   - this.pages = work.pages || []
6   - this.type = work.type || 'h5'
7   - this.work = work
  1 +import Work from '../../components/core/models/work'
8 2  
9   - this.id = this.id
10   - this.coverImageUrl = ''
11   - this.projectId = 1
12   - this.isPublish = false
13   - this.createTime = +new Date()
14   - this.updateTime = +new Date()
  3 +export const actions = {
  4 + previewWork ({ commit }, payload = {}) {
  5 + commit('previewWork', payload)
  6 + },
  7 + deployWork ({ commit }, payload = {}) {
  8 + commit('previewWork', payload)
  9 + },
  10 + saveWork ({ commit, state }, payload = {}) {
  11 + // save work with api
15 12 }
16 13 }
17 14  
18   -export default Work
  15 +// mutations
  16 +export const mutations = {
  17 + createWork (state) {
  18 + state.work = new Work()
  19 + },
  20 + previewWork (state, { type, value }) {},
  21 + deployWork (state, { type, value }) {}
  22 +}
... ...