Commit 98ffdd6a356bb715a22d978b76421b33bfda1031

Authored by ly525
1 parent 7afbd71d

feat: add preview mode and register plugin globally for adding script later

front-end/h5/src/views/Editor.vue
1 1 <script>
  2 +import Vue from 'vue'
  3 +
2 4 // LuBanPlugin -> Lbp
3 5 const LbpButton = {
4 6 render () {
... ... @@ -344,11 +346,15 @@ const Editor = {
344 346 data: () => ({
345 347 pages: [],
346 348 elements: [],
347   - editingElement: null
  349 + editingElement: null,
  350 + isPreviewMode: false,
348 351 }),
349 352 methods: {
350 353 getEditorConfig (pluginName) {
351   - return this.$options.components[pluginName].editorConfig
  354 + // const pluginCtor = Vue.options[pluginName]
  355 + // const pluginCtor = this.$options.components[pluginName]
  356 + const pluginCtor = Vue.component(pluginName)
  357 + return new pluginCtor().$options.editorConfig
352 358 },
353 359 /**
354 360 * !#zh 点击插件,copy 其基础数据到组件树(中间画布)
... ... @@ -396,6 +402,22 @@ const Editor = {
396 402 </div>
397 403 )
398 404 },
  405 + renderPreview (h, elements) {
  406 + return (
  407 + <div style={{ height: '100%' }}>
  408 + {elements.map((element, index) => {
  409 + return (() => {
  410 + const data = {
  411 + style: element.getStyle(),
  412 + props: element.pluginProps, // #6 #3
  413 + nativeOn: {}
  414 + }
  415 + return h(element.name, data)
  416 + })()
  417 + })}
  418 + </div>
  419 + )
  420 + },
399 421 renderPluginListPanel () {
400 422 return (
401 423 <el-tabs tab-position="left" class="lb-tabs">
... ... @@ -465,8 +487,14 @@ const Editor = {
465 487 { this.renderPluginListPanel() }
466 488 </div>
467 489 <div class='el-col-13'>
  490 + <div style="text-align: center;">
  491 + <el-radio-group value={this.isPreviewMode} onInput={(value) => this.isPreviewMode = value} size="mini">
  492 + <el-radio-button label={false}>Edit</el-radio-button>
  493 + <el-radio-button label={true}>Preview</el-radio-button>
  494 + </el-radio-group>
  495 + </div>
468 496 <div class='canvas-wrapper'>
469   - { this.renderCanvas(h, this.elements) }
  497 + { this.isPreviewMode ? this.renderPreview(h, this.elements) : this.renderCanvas(h, this.elements) }
470 498 </div>
471 499 </div>
472 500 <div class='el-col-6' style="border-left: 1px solid #eee;">
... ... @@ -488,7 +516,8 @@ export default {
488 516 methods: {
489 517 mixinPlugins2Editor () {
490 518 PluginList.forEach(plugin => {
491   - this.$options.components[plugin.name] = plugin.component
  519 + // 全局注册组件,便于以后扩展自定义脚本,注释原来的局部注册:this.$options.components[plugin.name] = plugin.component
  520 + Vue.component(plugin.name, plugin.component)
492 521 })
493 522 }
494 523 },
... ...