vue2使用wangeditor實現(xiàn)數(shù)學(xué)公式和富文本編輯器
需求:
做一個帶有數(shù)學(xué)公式的富文本編輯器,在網(wǎng)上看了很多,這個最合適,借鑒了wangEditor富文本編輯器
這里面寫的是v3的整合富文本編輯器,我照著上面改成了v2的,本文章主要是實現(xiàn)步驟和錯誤解決,源碼我放在最后了~
1.效果
2. 需要插件
npm install @wangeditor/editor @wangeditor/editor-for-vue @wangeditor/plugin-formula -S
jquery看自己項目里有沒有,沒有就下一個
npm install jquery
3.實現(xiàn)
wangEditor官網(wǎng):用于 Vue React | wangEditor
下載完插件后在所需頁面添加如下代碼即可實現(xiàn)(不包含數(shù)學(xué)公式)
<template> <div> <div> <button @click="printEditorHtml">print html</button> <button @click="getEditorText">print text</button> </div> <div style="border: 1px solid #ccc; margin-top: 10px"> <!-- 工具欄 --> <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" /> <!-- 編輯器 --> <Editor style="height: 400px; overflow-y: hidden" :defaultConfig="editorConfig" v-model="html" @onChange="onChange" @onCreated="onCreated" /> </div> <div style="margin-top: 10px"> <textarea v-model="html" readonly style="width: 100%; height: 200px; outline: none" ></textarea> </div> </div> </template> <script> import { Editor, Toolbar } from "@wangeditor/editor-for-vue"; export default { name: "MyEditor", components: { Editor, Toolbar }, data() { return { editor: null, html: "<p>hello world</p>", toolbarConfig: { // toolbarKeys: [ /* 顯示哪些菜單,如何排序、分組 */ ], // excludeKeys: [ /* 隱藏哪些菜單 */ ], }, editorConfig: { placeholder: "請輸入內(nèi)容...", // autoFocus: false, // 所有的菜單配置,都要在 MENU_CONF 屬性下 MENU_CONF: {}, }, }; }, methods: { onCreated(editor) { this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否則會報錯 }, onChange(editor) { console.log("onChange", editor.getHtml()); // onChange 時獲取編輯器最新內(nèi)容 }, getEditorText() { const editor = this.editor; if (editor == null) return; console.log(editor.getText()); // 執(zhí)行 editor API }, printEditorHtml() { const editor = this.editor; if (editor == null) return; console.log(editor.getHtml()); // 執(zhí)行 editor API }, }, mounted() { // 模擬 ajax 請求,異步渲染編輯器 setTimeout(() => { this.html = "<p>Ajax 異步設(shè)置內(nèi)容 HTML</p>"; }, 1500); }, beforeDestroy() { const editor = this.editor; if (editor == null) return; editor.destroy(); // 組件銷毀時,及時銷毀 editor ,重要?。。? }, }; </script> <style src="@wangeditor/editor/dist/css/style.css"></style>
3.1加入數(shù)學(xué)公式
在components文件下添加kityformula.js,內(nèi)容如下:
注意?。D標地址別寫錯了,不然圖標顯示不出來
constructor里面就是一些基本的配置
import $ from "jquery"; import { formulaIcon } from "../assets/icons/svg-icon.ts"; class MyKityFormulaMenu { constructor() { this.title = "編輯公式"; this.iconSvg = formulaIcon; this.tag = "button"; this.showModal = true; this.modalWidth = 900; this.modalHeight = 400; } // 菜單是否需要激活(如選中加粗文本,“加粗”菜單會激活),用不到則返回 false isActive(editor) { return false; } // 獲取菜單執(zhí)行時的 value ,用不到則返回空 字符串或 false getValue(editor) { return ""; } // 菜單是否需要禁用(如選中 H1 ,“引用”菜單被禁用),用不到則返回 false isDisabled(editor) { return false; } // 點擊菜單時觸發(fā)的函數(shù) exec(editor, value) { // Modal menu ,這個函數(shù)不用寫,空著即可 } // 彈出框 modal 的定位:1. 返回某一個 SlateNode; 2. 返回 null (根據(jù)當前選區(qū)自動定位) getModalPositionNode(editor) { return null; // modal 依據(jù)選區(qū)定位 } // 定義 modal 內(nèi)部的 DOM Element getModalContentElem(editor) { // panel 中需要用到的id const inputIFrameId = "kityformula_" + Math.ceil(Math.random() * 10); const btnOkId = "kityformula-btn" + Math.ceil(Math.random() * 10); const $content = $(` <div> <iframe id="${inputIFrameId}" class="iframe" height="400px" width="100%" frameborder="0" scrolling="no" src="/kityformula/index.html"></iframe> </div>`); const $button = $( `<button id="${btnOkId}" class="right" style='margin: 5px 0'> 確認插入 </button>` ); $content.append($button); $button.on("click", () => { // 執(zhí)行插入公式 const node = document.getElementById(inputIFrameId); const kfe = node.contentWindow.kfe; kfe.execCommand("get.image.data", function (data) { // 獲取base64 // console.log(data.img); }); let latex = kfe.execCommand("get.source"); latex = latex.replace(/\s/g, ""); // 去掉空格 const formulaNode = { type: "paragraph", children: [ { type: "formula", value: latex, children: [ { text: "", }, ], }, ], }; console.log(editor, '編輯器'); editor.insertNode(formulaNode); editor.hidePanelOrModal(); }); return $content[0]; // 返回 DOM Element 類型 // PS:也可以把 $content 緩存下來,這樣不用每次重復(fù)創(chuàng)建、重復(fù)綁定事件,優(yōu)化性能 } } const menuConf = { key: "kityFormula", // menu key ,唯一。注冊之后,需通過 toolbarKeys 配置到工具欄 factory() { return new MyKityFormulaMenu(); }, }; export default menuConf;
代碼講解:content就是數(shù)學(xué)公式彈窗,點擊確認插入之后會添加節(jié)點在編輯器內(nèi)
const $content = $(` <div> <iframe id="${inputIFrameId}" class="iframe" height="400px" width="100%" frameborder="0" scrolling="no" src="/kityformula/index.html"></iframe> </div>`); const $button = $( `<button id="${btnOkId}" class="right" style='margin: 5px 0'> 確認插入 </button>` );
3.2在pulic文件下添加數(shù)學(xué)公式相關(guān)代碼
3.3主要頁面代碼講解
這個代碼意思就是插入注冊,添加到編輯器當中
import kityformula from "@/components/kityformula"; import { Boot } from "@wangeditor/editor"; toolbarConfig: { // 插入編輯公式菜單 insertKeys: { index: 0, keys: [ "kityFormula", // “編輯公式”菜單 ], }, // excludeKeys: [ /* 隱藏哪些菜單 */ ], }, created() { Boot.registerMenu(kityformula); },
3.4解決編輯完成數(shù)學(xué)公式后內(nèi)容沒插入編輯器bug
意思就是寫完了數(shù)學(xué)公式后沒反應(yīng),內(nèi)容沒添加進編輯器當中
解決如下:
import formulaModule from "@wangeditor/plugin-formula"; import { Boot } from "@wangeditor/editor"; created() { Boot.registerModule(formulaModule); },
此時再次運行會報錯:
Module not found: Error: Can't resolve 'katex' in xxx
解決:
下載這個,默認下載最新版5.1.0,下載完成后就不會報錯
npm install myscript-math-web
3.5完整頁面代碼
<template> <div class="content-box"> <div class="container" style="width: 1000px; margin: 0 auto"> <div> <button @click="printEditorHtml">獲取編輯器html</button> <button @click="getEditorText">獲取編輯器文本</button> </div> <div style="border: 1px solid #ccc; margin-top: 10px; text-align: left"> <!-- 工具欄 --> <Toolbar style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" /> <!-- 編輯器 --> <Editor style="height: 500px; overflow-y: hidden" :defaultConfig="editorConfig" v-model="html" @onChange="onChange" @onCreated="onCreated" @onFocus="handleFocus" /> </div> <div style="margin-top: 10px"> <textarea v-model="html" readonly style="width: 100%; height: 200px; outline: none" ></textarea> </div> </div> </div> </template> <script> import { Editor, Toolbar } from "@wangeditor/editor-for-vue"; import kityformula from "@/components/kityformula"; import { Boot } from "@wangeditor/editor"; import formulaModule from "@wangeditor/plugin-formula"; export default { name: "MyEditor", components: { Editor, Toolbar }, data() { return { editor: null, html: "<p>hello world</p>", toolbarConfig: { // 插入編輯公式菜單 insertKeys: { index: 0, keys: [ "kityFormula", // “編輯公式”菜單 ], }, // excludeKeys: [ /* 隱藏哪些菜單 */ ], }, editorConfig: { placeholder: "請輸入內(nèi)容...", // autoFocus: false, // 所有的菜單配置,都要在 MENU_CONF 屬性下 MENU_CONF: {}, }, }; }, methods: { onCreated(editor) { console.log("created", editor); this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否則會報錯 }, onChange(editor) { console.log("onChange", editor.getHtml()); // onChange 時獲取編輯器最新內(nèi)容 }, handleFocus(editor) { console.log("focus", editor); }, getEditorText() { const editor = this.editor; if (editor == null) return; console.log(editor.getText()); // 執(zhí)行 editor API }, printEditorHtml() { const editor = this.editor; if (editor == null) return; console.log(editor.getHtml()); // 執(zhí)行 editor API }, }, created() { Boot.registerMenu(kityformula); Boot.registerModule(formulaModule); }, mounted() { // 模擬 ajax 請求,異步渲染編輯器 setTimeout(() => { this.html = "<p>Ajax 異步設(shè)置內(nèi)容 HTML</p>"; }, 1500); }, beforeDestroy() { const editor = this.editor; if (editor == null) return; editor.destroy(); // 組件銷毀時,及時銷毀 editor ,重要?。。? }, }; </script> <style src="@wangeditor/editor/dist/css/style.css"></style>
以上就是vue2使用wangeditor實現(xiàn)數(shù)學(xué)公式和富文本編輯器的詳細內(nèi)容,更多關(guān)于vue2 wangeditor的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3 開始時間與結(jié)束時間比較驗證(結(jié)束時間需要大于開始時間)
本文通過示例代碼介紹了vue3 開始時間與結(jié)束時間比較驗證(結(jié)束時間需要大于開始時間)的相關(guān)操作,代碼簡單易懂,感興趣的朋友跟隨小編一起看看吧2024-07-07vue2+elementui上傳照片方式(el-upload超簡單)
這篇文章主要介紹了vue2+elementui上傳照片方式(el-upload超簡單),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03使用自動導(dǎo)入后eslint報錯eslint9的問題及解決方法
本文介紹了使用`pnpm create vue@latest`創(chuàng)建Vue應(yīng)用時,如何配置ESLint和Prettier,包括解決兩者沖突以及自動導(dǎo)入后Eslint報錯的問題,感興趣的朋友一起看看吧2025-03-03vue中使用 pinia 全局狀態(tài)管理的實現(xiàn)
本文主要介紹了vue中使用 pinia 全局狀態(tài)管理的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2022-07-07Vue 組件(component)教程之實現(xiàn)精美的日歷方法示例
組件是我們學(xué)習vue必須會的一部分,下面這篇文章主要給大家介紹了關(guān)于Vue 組件(component)教程之實現(xiàn)精美的日歷的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧。2018-01-01