Commit d995005405f49715a57dfd4b6370571e94382c94

Authored by ly525
1 parent 8bd78f37

chore: move Editor.vue to core/editor.js

front-end/h5/src/components/core/editor.js 0 → 100644
  1 +import Vue from 'vue'
  2 +
  3 +export default {
  4 + name: 'Editor',
  5 + components: {
  6 + },
  7 + data: () => ({
  8 + pages: [],
  9 + elements: [],
  10 + editingElement: null,
  11 + isPreviewMode: false
  12 + }),
  13 + methods: {
  14 + getEditorConfig (pluginName) {
  15 + // const pluginCtor = Vue.options[pluginName]
  16 + // const pluginCtor = this.$options.components[pluginName]
  17 + const PluginCtor = Vue.component(pluginName)
  18 + return new PluginCtor().$options.editorConfig
  19 + },
  20 + /**
  21 + * !#zh 点击插件,copy 其基础数据到组件树(中间画布)
  22 + * pluginInfo {Object}: 插件列表中的基础数据, {name}=pluginInfo
  23 + */
  24 + clone ({ name }) {
  25 + const zindex = this.elements.length + 1
  26 + // const defaultPropsValue = this.getPropsDefaultValue(name)
  27 + const editorConfig = this.getEditorConfig(name)
  28 + this.elements.push(new Element({ name, zindex, editorConfig }))
  29 + },
  30 + mixinPluginCustomComponents2Editor () {
  31 + const { components } = this.editingElement.editorConfig
  32 + for (const key in components) {
  33 + if (this.$options.components[key]) return
  34 + this.$options.components[key] = components[key]
  35 + }
  36 + },
  37 + setCurrentEditingElement (element) {
  38 + this.editingElement = element
  39 + this.mixinPluginCustomComponents2Editor()
  40 + },
  41 + /**
  42 + * #!zh: renderCanvas 渲染中间画布
  43 + * elements
  44 + * @param {*} h
  45 + * @param {*} elements
  46 + * @returns
  47 + */
  48 + renderCanvas (h, elements) {
  49 + return (
  50 + <div style={{ height: '100%' }}>
  51 + {elements.map((element, index) => {
  52 + return (() => {
  53 + const data = {
  54 + style: element.getStyle(),
  55 + props: element.pluginProps, // #6 #3
  56 + nativeOn: {
  57 + click: this.setCurrentEditingElement.bind(this, element)
  58 + }
  59 + }
  60 + return h(element.name, data)
  61 + })()
  62 + })}
  63 + </div>
  64 + )
  65 + },
  66 + renderPreview (h, elements) {
  67 + return (
  68 + <div style={{ height: '100%' }}>
  69 + {elements.map((element, index) => {
  70 + return (() => {
  71 + const data = {
  72 + style: element.getStyle(),
  73 + props: element.pluginProps, // #6 #3
  74 + nativeOn: {}
  75 + }
  76 + return h(element.name, data)
  77 + })()
  78 + })}
  79 + </div>
  80 + )
  81 + },
  82 + renderPluginListPanel () {
  83 + return (
  84 + <el-tabs tab-position="left" class="lb-tabs">
  85 + <el-tab-pane label='组件列表'>
  86 + <div class="full-height">
  87 + {this.visiblePluginList.sort().map(item => {
  88 + return (
  89 + <el-button
  90 + class='plugin-item ma-0 no-border-radius'
  91 + onClick={this.clone.bind(this, item)}
  92 + >
  93 + <i
  94 + class={['fa', `fa-${item.icon}`]}
  95 + aria-hidden='true'
  96 + style='display: block;font-size: 16px;margin-bottom: 10px;'
  97 + />
  98 + <span>{ item.title }</span>
  99 + </el-button>
  100 + )
  101 + })}
  102 + </div>
  103 + </el-tab-pane>
  104 + <el-tab-pane label='页面管理'>
  105 + <span>页面管理</span>
  106 + </el-tab-pane>
  107 + </el-tabs>
  108 + )
  109 + },
  110 + renderPropsEditorPanel (h) {
  111 + if (!this.editingElement) return (<span>请先选择一个元素</span>)
  112 + const editingElement = this.editingElement
  113 + const propsConfig = editingElement.editorConfig.propsConfig
  114 + return (
  115 + <el-form ref="form" label-width="100px" size="mini">
  116 + {
  117 + Object.keys(propsConfig).map(propKey => {
  118 + const item = propsConfig[propKey]
  119 + // https://vuejs.org/v2/guide/render-function.html
  120 + const data = {
  121 + props: {
  122 + ...item.prop,
  123 + // https://vuejs.org/v2/guide/render-function.html#v-model
  124 + ...{ value: editingElement[propKey] || item.defaultPropValue }
  125 + },
  126 + on: {
  127 + // https://vuejs.org/v2/guide/render-function.html#v-model
  128 + input (value) {
  129 + editingElement[propKey] = value
  130 + }
  131 + }
  132 + }
  133 + return (
  134 + <el-form-item label={item.label}>
  135 + { h(item.type, data) }
  136 + </el-form-item>
  137 + )
  138 + })
  139 + }
  140 + </el-form>
  141 + )
  142 + }
  143 + },
  144 + render (h) {
  145 + return (
  146 + <div id='designer-page'>
  147 + <div class='el-col-5'>
  148 + { this.renderPluginListPanel() }
  149 + </div>
  150 + <div class='el-col-13'>
  151 + <div style="text-align: center;">
  152 + <el-radio-group value={this.isPreviewMode} onInput={(value) => {
  153 + this.isPreviewMode = value
  154 + }} size="mini">
  155 + <el-radio-button label={false}>Edit</el-radio-button>
  156 + <el-radio-button label={true}>Preview</el-radio-button>
  157 + </el-radio-group>
  158 + </div>
  159 + <div class='canvas-wrapper'>
  160 + { this.isPreviewMode ? this.renderPreview(h, this.elements) : this.renderCanvas(h, this.elements) }
  161 + </div>
  162 + </div>
  163 + <div class='el-col-6' style="border-left: 1px solid #eee;">
  164 + { this.renderPropsEditorPanel(h) }
  165 + </div>
  166 + </div>
  167 + )
  168 + }
  169 +}
front-end/h5/src/views/Editor.vue
1 <script> 1 <script>
2 import Vue from 'vue' 2 import Vue from 'vue'
  3 +import CoreEditor from '../components/core/editor'
3 4
4 import LbpButton from '../components/plugins/lbp-button' 5 import LbpButton from '../components/plugins/lbp-button'
5 6
@@ -15,174 +16,8 @@ const PluginList = [ @@ -15,174 +16,8 @@ const PluginList = [
15 } 16 }
16 ] 17 ]
17 18
18 -const Editor = {  
19 - name: 'Editor',  
20 - components: {  
21 - },  
22 - data: () => ({  
23 - pages: [],  
24 - elements: [],  
25 - editingElement: null,  
26 - isPreviewMode: false,  
27 - }),  
28 - methods: {  
29 - getEditorConfig (pluginName) {  
30 - // const pluginCtor = Vue.options[pluginName]  
31 - // const pluginCtor = this.$options.components[pluginName]  
32 - const pluginCtor = Vue.component(pluginName)  
33 - return new pluginCtor().$options.editorConfig  
34 - },  
35 - /**  
36 - * !#zh 点击插件,copy 其基础数据到组件树(中间画布)  
37 - * pluginInfo {Object}: 插件列表中的基础数据, {name}=pluginInfo  
38 - */  
39 - clone ({ name }) {  
40 - const zindex = this.elements.length + 1  
41 - // const defaultPropsValue = this.getPropsDefaultValue(name)  
42 - const editorConfig = this.getEditorConfig(name)  
43 - this.elements.push(new Element({ name, zindex, editorConfig }))  
44 - },  
45 - mixinPluginCustomComponents2Editor () {  
46 - const { components } = this.editingElement.editorConfig  
47 - for (const key in components) {  
48 - if (this.$options.components[key]) return  
49 - this.$options.components[key] = components[key]  
50 - }  
51 - },  
52 - setCurrentEditingElement (element) {  
53 - this.editingElement = element  
54 - this.mixinPluginCustomComponents2Editor()  
55 - },  
56 - /**  
57 - * #!zh: renderCanvas 渲染中间画布  
58 - * elements  
59 - * @param {*} h  
60 - * @param {*} elements  
61 - * @returns  
62 - */  
63 - renderCanvas (h, elements) {  
64 - return (  
65 - <div style={{ height: '100%' }}>  
66 - {elements.map((element, index) => {  
67 - return (() => {  
68 - const data = {  
69 - style: element.getStyle(),  
70 - props: element.pluginProps, // #6 #3  
71 - nativeOn: {  
72 - click: this.setCurrentEditingElement.bind(this, element)  
73 - }  
74 - }  
75 - return h(element.name, data)  
76 - })()  
77 - })}  
78 - </div>  
79 - )  
80 - },  
81 - renderPreview (h, elements) {  
82 - return (  
83 - <div style={{ height: '100%' }}>  
84 - {elements.map((element, index) => {  
85 - return (() => {  
86 - const data = {  
87 - style: element.getStyle(),  
88 - props: element.pluginProps, // #6 #3  
89 - nativeOn: {}  
90 - }  
91 - return h(element.name, data)  
92 - })()  
93 - })}  
94 - </div>  
95 - )  
96 - },  
97 - renderPluginListPanel () {  
98 - return (  
99 - <el-tabs tab-position="left" class="lb-tabs">  
100 - <el-tab-pane label='组件列表'>  
101 - <div class="full-height">  
102 - {this.visiblePluginList.sort().map(item => {  
103 - return (  
104 - <el-button  
105 - class='plugin-item ma-0 no-border-radius'  
106 - onClick={this.clone.bind(this, item)}  
107 - >  
108 - <i  
109 - class={['fa', `fa-${item.icon}`]}  
110 - aria-hidden='true'  
111 - style='display: block;font-size: 16px;margin-bottom: 10px;'  
112 - />  
113 - <span>{ item.title }</span>  
114 - </el-button>  
115 - )  
116 - })}  
117 - </div>  
118 - </el-tab-pane>  
119 - <el-tab-pane label='页面管理'>  
120 - <span>页面管理</span>  
121 - </el-tab-pane>  
122 - </el-tabs>  
123 - )  
124 - },  
125 - renderPropsEditorPanel (h) {  
126 - if (!this.editingElement) return (<span>请先选择一个元素</span>)  
127 - const editingElement = this.editingElement  
128 - const propsConfig = editingElement.editorConfig.propsConfig  
129 - return (  
130 - <el-form ref="form" label-width="100px" size="mini">  
131 - {  
132 - Object.keys(propsConfig).map(propKey => {  
133 - const item = propsConfig[propKey]  
134 - // https://vuejs.org/v2/guide/render-function.html  
135 - const data = {  
136 - props: {  
137 - ...item.prop,  
138 - // https://vuejs.org/v2/guide/render-function.html#v-model  
139 - ...{ value: editingElement[propKey] || item.defaultPropValue }  
140 - },  
141 - on: {  
142 - // https://vuejs.org/v2/guide/render-function.html#v-model  
143 - input (value) {  
144 - editingElement[propKey] = value  
145 - }  
146 - }  
147 - }  
148 - return (  
149 - <el-form-item label={item.label}>  
150 - { h(item.type, data) }  
151 - </el-form-item>  
152 - )  
153 - })  
154 - }  
155 - </el-form>  
156 - )  
157 - }  
158 - },  
159 - render (h) {  
160 - return (  
161 - <div id='designer-page'>  
162 - <div class='el-col-5'>  
163 - { this.renderPluginListPanel() }  
164 - </div>  
165 - <div class='el-col-13'>  
166 - <div style="text-align: center;">  
167 - <el-radio-group value={this.isPreviewMode} onInput={(value) => this.isPreviewMode = value} size="mini">  
168 - <el-radio-button label={false}>Edit</el-radio-button>  
169 - <el-radio-button label={true}>Preview</el-radio-button>  
170 - </el-radio-group>  
171 - </div>  
172 - <div class='canvas-wrapper'>  
173 - { this.isPreviewMode ? this.renderPreview(h, this.elements) : this.renderCanvas(h, this.elements) }  
174 - </div>  
175 - </div>  
176 - <div class='el-col-6' style="border-left: 1px solid #eee;">  
177 - { this.renderPropsEditorPanel(h) }  
178 - </div>  
179 - </div>  
180 - )  
181 - }  
182 -}  
183 -  
184 export default { 19 export default {
185 - extends: Editor, 20 + extends: CoreEditor,
186 computed: { 21 computed: {
187 // !#zh 显示在侧边栏或顶部的 可用组件列表 22 // !#zh 显示在侧边栏或顶部的 可用组件列表
188 visiblePluginList () { 23 visiblePluginList () {