国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

VueJS 集成 Medium Editor的示例代碼 (自定義編輯器按鈕)

2024-05-06 15:12:25
字體:
來源:轉載
供稿:網友

0x00 前言

VueJS 社區里面關于富文本編輯器的集成也不少了,但是之前小調研了一下,基本上就是 quill,medium-editor,因為之前用 AngularJS 用過 medium-editor,并且需要自定義某些按鈕,而且最好還是選中彈出式的,所以就決定用 medium-editor。

社區里面 star 較多的就是這個了:vue-medium-editor,但是打開它官網,看了看文檔,越看越別扭,來看看它用法:

<!-- index.html --><medium-editor :text='myText' :options='options' custom-tag='h2' v-on:edit='applyTextEdit'>

gosh,傳這么多參數,我只想要一個簡單的 editor 啊

打開源碼一看,就 62 行,所以決定自己動手來一個簡單點的

0x01 最簡版

最簡版,其實就在 vue 組件中實例化一下 medium-editor 就可以了

<template> <div class="textEditor" @input="handleInput"> </div></template><script>/* eslint-disable no-new */import MediumEditor from 'medium-editor'export default { props: {  value: String,  options: {   type: Object,   default: () => ({})  } }, data () {  return {   editor: null // 用來存放 editor   } }, watch: {  // refer: https://github.com/FranzSkuffka/vue-medium-editor/blob/master/index.js  value (newVal, oldVal) {   if (newVal !== this.$el.innerHTML) { // 用 $el.innerHTML 來解決 v-html 的光標跳到行首的問題    this.$el.innerHTML = newVal || ''   }  } }, methods: {  handleInput (e) {   this.$emit('input', e.target.innerHTML)  } }, mounted () {  // 處理初始值的情況  this.$el.innerHTML = this.value  // 這里當然可以自定義 options 啦  this.editor = new MediumEditor(this.$el, Object.assign({}, this.options))  // medium-editor 的 api,監聽內容改變化  this.editor.subscribe('editableInput', this.handleInput) }, beforeDestroy () {  this.editor.unsubscribe('editableInput', this.handleInput)  this.editor.destroy() }}</script>

完成~,是不是很簡單~~哈哈,最簡版是一個 v-html 控制的,但是會有自動跳轉到首行的問題,所以這里是最終版,細節問題看注釋啦

0x02 用法

咋用呢?很簡單,在其他組件中這樣:

<text-editor v-model="vm.richText"></text-editor>

當然 你首先得安裝 medium-editor的 js 和 css了

0x03 自定義 button

下面是我項目中用到的自定義 button 的相關代碼,是一個 buttonBuilder:

import MediumEditor from 'medium-editor'import rangy from 'rangy/lib/rangy-core.js'import 'rangy/lib/rangy-classapplier'import 'rangy/lib/rangy-highlighter'import 'rangy/lib/rangy-selectionsaverestore'import 'rangy/lib/rangy-textrange'import 'rangy/lib/rangy-serializer'const pHash = { p1: { name: 'p1', class: 'fs-36' }, p2: { name: 'p2', class: 'fs-30' }, p3: { name: 'p3', class: 'fs-24' }, p4: { name: 'p4', class: 'fs-18' }, p5: { name: 'p5', class: 'fs-14' }, p6: { name: 'p6', class: 'fs-12' }}function pButtonCreator (p) { return MediumEditor.Extension.extend({  name: p.name,  init: function () {   this.classApplier = rangy.createClassApplier(p.class, {    elementTagName: 'span',    normalize: false   })   this.button = this.document.createElement('button')   this.button.classList.add('medium-editor-action')   this.button.innerHTML = p.name   this.button.title = p.class   this.on(this.button, 'click', this.handleClick.bind(this))  },  getButton: function () {   return this.button  },  clearFontSize: function () {   MediumEditor.selection.getSelectedElements(this.document).forEach(function (el) {    if (el.nodeName.toLowerCase() === 'span' && el.hasAttribute('class')) {     el.removeAttribute('class')    }   })  },  handleClick: function (event) {   this.clearFontSize()   this.classApplier.toggleSelection()   // Ensure the editor knows about an html change so watchers are notified   // ie: <textarea> elements depend on the editableInput event to stay synchronized   this.base.checkContentChanged()  } })}export default { P1: pButtonCreator(pHash['p1']), P2: pButtonCreator(pHash['p2']), P3: pButtonCreator(pHash['p3']), P4: pButtonCreator(pHash['p4']), P5: pButtonCreator(pHash['p5']), P6: pButtonCreator(pHash['p6'])}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 五寨县| 鄢陵县| 环江| 西贡区| 乌什县| 罗江县| 浑源县| 大冶市| 沾益县| 永仁县| 抚顺市| 长丰县| 周口市| 蒙城县| 光泽县| 应用必备| 新龙县| 巴林右旗| 淮南市| 安溪县| 卓尼县| 左权县| 仁布县| 固阳县| 利辛县| 渑池县| 三门县| 体育| 定远县| 拜泉县| 界首市| 邵武市| 宜川县| 老河口市| 兴宁市| 汤原县| 遵义市| 禹城市| 政和县| 沙洋县| 新丰县|