tui-editor编辑器自定义按钮

tui-editor编辑器自定义按钮

上传图片 按钮及事件

<input ref="image" style="display:none;" type="file" accept="image/*" @change="uploadImage">
initEditor(){
  this.editor = new Editor({
    el: document.getElementById(this.id),
    ...this.editorOptions
  })
  if(this.value){
    this.editor.setValue(this.value)
  }
  this.editor.on('change', ()=>{
    this.$emit('input', this.editor.getValue())	
  })
  //获取菜单栏
  const toolbar = this.editor.getUI().getToolbar()
  
  //图片上传按钮及事件
  const imageDom = this.$refs.image
  this.editor.eventManager.addEventType('uploadEvent')
  this.editor.eventManager.listen('uploadEvent', ()=>{
    imageDom.click()
  })
  toolbar.insertItem(15, {
    type: 'button',
    options: {
	  className: 'el-icon-picture',
	  event: 'uploadEvent',
	  tooltip: '插入图片',
	  text:'',
	  style: 'font-size:14px;color:#000;background:none'
    }
  })
}
//图片上传
uploadImage(e){
  const target = e.target
  const file = target.files[0]
  const formdata = new FormData()
  formdata.append('file', file)
  uploadImageFile(formdata).then((res)=>{  //调接口
    if(res.success){
      const imgUrl = res.url
      this.addImgToMd(imgUrl, filename)
    }
  })
  target.value = ''  //清除数据
},
//添加图片到编辑器中
addImgToMd(data, imgname){
  const editor = this.editor.getCodeMirror()
  const editorHtml = this.editor.getCurrentModeEditor()
  const isMarkdownMode = this.editor.isMarkdownMode()
  if(isMarkdownMode){
    editor.replaceSelection(`![${imgname}](${data})`)
  } else {
    const range = editorHtml.getRange()
    const img = document.createElement('img')
    img.src = `${data}`
    img.alt = `${imgname}`
    range.insertNode(img)
  }
}

导入文件按钮及事件

<input ref="file" style="display:none;" type="file" accept=".md" @change="uploadFile">
  //导入文件按钮及事件
  const fileDom = this.$refs.file
  this.editor.eventManager.addEventType('uploadFileEvent')
  this.editor.eventManager.listen('uploadFileEvent', ()=>{
    fileDom.click()
  })
  toolbar.insertItem(23, {
    type: 'button',
    options: {
	  className: 'el-icon-upload',
	  event: 'uploadFileEvent',
	  tooltip: '导入md文件',
	  text:'',
	  style: 'font-size:14px;color:#000;background:none'
    }
  })
uploadFile(e){
  const target = e.target
  const file = target.files[0]
  const reader = new FileReader()
  const that = this
  reader.onload = function(evt) {
    that.setValue(evt.target.result)  //向编辑器传值
  }
  reader.readAsText(file)
}

导出文件按钮及事件

  //导出文件按钮及事件
  this.editor.eventManager.addEventType('exportFileEvent')
  this.editor.eventManager.listen('exportFileEvent', ()=>{
    const md = this.editor.getValue()
    //获取当前日期
    const date = new Date()
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const today = year + '-' + month + '-' + day
    this.exportFile(md, today + '.md')  //不加后缀,默认为txt文件
  })
  toolbar.insertItem(24, {
    type: 'button',
    options: {
	  className: 'el-icon-download',
	  event: 'exportFileEvent',
	  tooltip: '导出md文件',
	  text:'',
	  style: 'font-size:14px;color:#000;background:none'
    }
  })
//导出文件
exportFile(content, filename){
  const linkNode = document.createElement('a')
  linkNode.download = filename
  linkNode.style.display = 'none'
  // 利用Blob对象将字符内容转变成二进制数据
  var blob = new Blob([content])
  linkNode.href = URL.createObjectURL(blob)
  // 触发点击
  document.body.appendChild(linkNode)
  linkNode.click()
  // 移除
  document.body.removeChild(linkNode)
}

完整代码

<template>
  <div>
    <div :id="id" />
    <input ref="image" style="display:none;" type="file" accept="image/*" @change="uploadImage">
    <input ref="file" style="display:none;" type="file" accept=".md" @change="uploadFile">
  </div>
</template>

<script>
// deps for editor
import 'codemirror/lib/codemirror.css' // codemirror
import 'tui-editor/dist/tui-editor.css' // editor ui
import 'tui-editor/dist/tui-editor-contents.css' // editor content

import Editor from 'tui-editor'
import defaultOptions from '@/components/MarkdownEditor/default-options'

// 自定义图片上传地址
import { uploadDescriptionFile } from '@/api/document'

export default {
  name: 'MarddownEditor',
  props: {
    value: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      required: false,
      default() {
        return 'markdown-editor-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
      }
    },
    options: {
      type: Object,
      default() {
        return defaultOptions
      }
    },
    mode: {
      type: String,
      default: 'markdown'
    },
    height: {
      type: String,
      required: false,
      default: '300px'
    },
    language: {
      type: String,
      required: false,
      default: 'zh_CN' // https://github.com/nhnent/tui.editor/tree/master/src/js/langs
    }
  },
  data() {
    return {
      editor: null
    }
  },
  computed: {
    editorOptions() {
      const options = Object.assign({}, defaultOptions, this.options)
      options.initialEditType = this.mode
      options.height = this.height
      options.language = this.language
      return options
    }
  },
  watch: {
    value(newValue, preValue) {
      if (newValue !== preValue && newValue !== this.editor.getValue()) {
        this.editor.setValue(newValue)
      }
    },
    language(val) {
      this.destroyEditor()
      this.initEditor()
    },
    height(newValue) {
      this.editor.height(newValue)
    },
    mode(newValue) {
      this.editor.changeMode(newValue)
    }
  },
  mounted() {
    this.initEditor()
  },
  destroyed() {
    this.destroyEditor()
  },
  methods: {
    initEditor() {
      this.editor = new Editor({
        el: document.getElementById(this.id),
        ...this.editorOptions
      })
      if (this.value) {
        this.editor.setValue(this.value)
      }
      this.editor.on('change', () => {
        this.$emit('input', this.editor.getValue())
      })
      /*
      * 添加自定义按钮
      * */
      // 获取编辑器上的功能条,即菜单栏
      const toolbar = this.editor.getUI().getToolbar()
      const imageDom = this.$refs.image
      // 添加图片上传事件
      this.editor.eventManager.addEventType('uploadEvent')
      this.editor.eventManager.listen('uploadEvent', () => {
        imageDom.click()
      })
      // 添加自定义按钮
      toolbar.insertItem(15, {
        type: 'button',
        options: {
          className: 'el-icon-picture',
          event: 'uploadEvent',
          tooltip: '插入图片',
          text: '',
          style: 'font-size:14px;color:#000;background:none;'
        }
      })
      // 导入文件按钮
      const fileDom = this.$refs.file
      this.editor.eventManager.addEventType('importfileEvent')
      this.editor.eventManager.listen('importfileEvent', () => {
        fileDom.click()
      })
      toolbar.insertItem(23, {
        type: 'button',
        options: {
          className: 'el-icon-upload2',
          event: 'importfileEvent',
          tooltip: '导入md文件',
          text: '',
          style: 'font-size:14px;color:#000;background:none;'
        }
      })
      // 添加保存文档事件
      // this.editor.eventManager.addEventType('saveEvent')
      // this.editor.eventManager.listen('saveEvent', () => {
      //   this.$emit('save')
      // })
      // toolbar.insertItem(22, {
      //   type: 'button',
      //   options: {
      //     className: 'el-icon-s-claim',
      //     event: 'saveEvent',
      //     tooltip: '保存文档',
      //     text: '',
      //     style: 'font-size:14px;color:#000;background:none;'
      //   }
      // })
      // 导出md文件按钮
      this.editor.eventManager.addEventType('exportEvent')
      this.editor.eventManager.listen('exportEvent', () => {
        const md = this.editor.getValue()
        // 获取当前日期
        const date = new Date()
        const year = date.getFullYear()
        const month = date.getMonth() + 1
        const day = date.getDate()
        const today = year + '-' + month + '-' + day
        this.downloadF(md, today + '.md') // 不加后缀,默认是txt格式
      })
      toolbar.insertItem(24, {
        type: 'button',
        options: {
          className: 'el-icon-download',
          event: 'exportEvent',
          tooltip: '导出md文件',
          text: '',
          style: 'font-size:14px;color:#000;background:none;'
        }
      })
    },
    downloadF(content, filename) {
      const linkNode = document.createElement('a')
      linkNode.download = filename
      linkNode.style.display = 'none'
      // 利用Blob对象将字符内容转变成二进制数据
      const blob = new Blob([content])
      linkNode.href = URL.createObjectURL(blob)
      // 点击
      document.body.appendChild(linkNode)
      linkNode.click()
      // 移除
      document.body.removeChild(linkNode)
    },
    destroyEditor() {
      if (!this.editor) return
      this.editor.off('change')
      this.editor.remove()
    },
    setValue(value) {
      this.editor.setValue(value)
    },
    getValue() {
      return this.editor.getValue()
    },
    setHtml(value) {
      this.editor.setHtml(value)
    },
    getHtml() {
      return this.editor.getHtml()
    },
    /*
    * 自定义图片上传
    * */
    uploadImage(e) {
      const target = e.target
      const file = target.files[0]
      const formdata = new FormData()
      formdata.append('file', file)
      uploadDescriptionFile(formdata).then((res) => {
        if (res.success) {
          const imgUrl = res.url
          this.addImgToMd(imgUrl, file.name)
        }
      }).catch(error => {
        this.$message.error(error.msg)
      })
      target.value = '' // 清除数据
    },
    //  添加图片到markdown
    addImgToMd(data, imgname) {
      const editor = this.editor.getCodeMirror()
      const editorHtml = this.editor.getCurrentModeEditor()
      const isMarkdownMode = this.editor.isMarkdownMode()
      if (isMarkdownMode) {
        editor.replaceSelection(`![${imgname}](${data})`)
      } else {
        const range = editorHtml.getRange()
        const img = document.createElement('img')
        img.src = `${data}`
        img.alt = `${imgname}`
        range.insertNode(img)
      }
    },
    // 导入md文件
    uploadFile(e) {
      const target = e.target
      const file = target.files[0]
      var reader = new FileReader()
      const that = this
      reader.onload = function(evt) {
        that.setValue(evt.target.result)// 向编辑器传值
      }
      reader.readAsText(file)
    }
  }
}
</script>
<style lang="scss">
.tui-tooltip{
  z-index:9999
}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pixie Image Editor 在线图片编辑器 中文版 功能 集成 - 轻松将pixie集成到任何现有项目或应用程序中。 可扩展 - Pixie接口和API可以使用新功能进行扩展。 移动 - Pixie拥有完整的移动支持,并可自动调整其界面以适应任何设备的大小。 可自定义的UI - 通过显示,隐藏或添加新菜单项,更改工具栏位置或使用不同的主题来自定义UI。 可翻译 - Pixie的界面可通过配置完全翻译。 水印 - 保存的照片可以使用指定的文本轻松加水印。 模式 - 在叠加(模态),内联或全屏模式之间进行选择。 工具API - 通过API使用所有精灵工具(调整大小,裁剪,框架等),而无需打开精灵界面。 可自定义的工具 - 所有工具都可完全自定义,您可以删除或修改和添加自定义贴纸,形状,字体,框架等。 状态 - 以json格式保存当前编辑器状态,允许使用预构建模板等功能。 照片处理 - 通过界面或API调整大小,裁剪,转换等。 滤镜 - Pixie配有许多内置滤镜,如灰度,模糊,黑白,复古等。可以通过API添加更多过滤器.. 框架 - 为任何大小的照片添加内置响应帧或添加自己的帧。 裁剪 - 将照片裁剪为指定宽高比之一,或让用户通过UI选择自定义裁剪区域。 绘图 - 功能强大的免费绘图工具支持鼠标和触摸,具有多种画笔类型,颜色等。 文本 - 完全支持向图像添加文本。可以使用数百种谷歌字体或仅使用自定义添加的字体。 形状 - 只需指定svg图像路径,即可轻松添加自定义形状。 贴纸 - 可以添加或删除自定义贴纸。任何类型的图像都可以用作贴纸。 角落 - 只需单击一下或API调用即可对图像角进行四舍五入。 空画布 - Pixie不必编辑现有照片,也可以从头开始轻松创建自定义图像。 历史记录 - 所有编辑器操作都是非破坏性的,可以通过历史记录工具轻松撤消和重做。 对象 - 所有对象(如贴纸,形状和文本)都在自己的图层上,可以通过更改颜色,添加阴影,背景等来轻松移动,调整大小,删除和修改。 图案和渐变 - 所有对象都可以使用许多内置或自定义图案和渐变填充。 保存 - 修改后的图像可以通过API或接口轻松保存在本地设备或服务器上。 缩放和平移 - 可以使用鼠标,鼠标滚轮或移动设备上的触摸和捏合手势来缩放和平移画布。 HTML5 - Pixie使用原生HTML5,这意味着它可以在每个设备上运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值