ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

百度编辑器ueditor自定义增加一个菜单按钮,并实现与父级组件通信

2022-01-03 10:05:39  阅读:218  来源: 互联网

标签:menuRow ueditor false 自定义 父级 content newval ue console


项目中,已实现封装百度编辑器组件。这里,是为了增加支持自定义插入商品等服务功能。

新增自定义按钮,实现与父级组件通信

initEditor () {
	let that = this
	  window.UE.registerUI('goodsmenu', function (editor, uiName) {
		return new window.UE.ui.Button({
		  name: uiName,
		  title: '服务',  // 这里是设置当鼠标指向这个按扭时显示的文字
		  cssRules: "background-position: -550px 44px;",
		  onclick: function () {
			// todo 这里使用that.emit传递,父组件无法接收到抛出事件??
			// todo 或者是that.parent不起作用 建议都试一下
			// that.parent.ueditorMenuOpen()
			that.$emit('ueditorMenuOpen')
			// alert('我是新增按扭在被点击时执行的方法,在这里可以编写你想要实现的功能哦!');
		  }
		});
	  });
	}
}


父级组件实现选择商品等服务,并插入到编辑器中

<template>
  <div>
    <!-- 百度编辑器vue组件 -->
    <VueUEditor :ueditorPath="ueditorPath" style="height: 520px" :ueditorConfig="ueditorConfig"  @ready="ueditorReady"></VueUEditor>
    <!-- 商品等服务组件 -->
    <url-picker :formData="menuRow" field="url" visibleField="urlVisible" @onSelectSuccess="onSelectSuccess"/>
  </div>
</template>
<script>
import VueUEditor from 'vue-ueditor';
import UrlPicker from '@/components/UeditorUrlPicker/'

export default {
  name: "UEditor",
  data() {
    return { 
      ueditorPath : 'http://localhost:8082/ueditor/',
      ueditorConfig: {
        'UEDITOR_HOME_URL': 'https://localhost:8082/ueditor/',
        'serverUrl': "https://localhost:8082/ueditor-upload-pic",
        'toolbars': []
      },
      ue: false,
      enter: false,
//      serverUrl: "http://duoyunzhushou.me/core/ueditor/index?i=1"

      //单个服务
      menuRow: {
        url: {},
        urlVisible: false
      },
    }
  },
  props: {
    ueditorContent: String,
    content: String,
    visible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
  },
  watch: {
    ueditorContent: function (newval, oldval) {
      // console.log(newval);
      if(!this.enter && this.ue) {
        this.ue.setContent(newval);
      }
    },
    visible: function (newval, oldval) {
      // 解决多次连续打开百度编辑器,编辑器中内容不进行替换的问题
      console.log('visible', newval,oldval)
      if (newval) {
        // console.log(this.ueditorContent, newval,oldval)
        // this.ue.setContent(this.content);
        // this.enter = false
      }

      this.enter = false
    }, 
  },
  methods: {
    ueditorReady(ue) {
      const _this = this;
      this.ue = ue;
      // console.log('ueditorReady', ue)
      ue.setContent(this.ueditorContent)
      ue.addListener('contentChange', () => {
        _this.enter = true;
        _this.$emit('update:ueditorContent', ue.getContent())
      })
    },

    ueditorContentSet(content) {
      const _this = this;
      _this.ue.execCommand('insertHtml', content);
      // this.menuRow = {
      //   url: {},
      //   urlVisible: false
      // }
    },

    onSelectSuccess() { 
      // console.log('onSelectSuccess', this.menuRow)
      let data = this.menuRow.url

      // 将服务内容 插入到编辑器中
      let content = '<p></p>'
      this.ueditorContentSet(content)
      // this.menuRow.urlVisible = false
    },

    // 打开服务菜单
    ueditorMenuOpen() {
      console.log('ueditorMenuOpen', this.menuRow)
      this.menuRow.urlVisible = true
    },
  },
  components: {
    VueUEditor, UrlPicker
  }
}
</script>
<style lang="scss">
.edui-default .edui-toolbar {
  line-height: initial;
}
</style>

标签:menuRow,ueditor,false,自定义,父级,content,newval,ue,console
来源: https://blog.csdn.net/qq_41039151/article/details/122284183

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有