vue2使用wangeditor實現(xiàn)手寫輸入功能
1.效果
2.實現(xiàn)
2.1:先看我上一篇,這篇就是在上一篇的基礎(chǔ)上添加一個手寫功能,導(dǎo)入注冊就行了
vue2使用wangeditor實現(xiàn)數(shù)學(xué)公式+富文本編輯器
在components中添加myscriptMath.js
svg也就是個顯示的圖標(biāo),可以替換為其他
import $ from "jquery"; import { mathIcon } from "../assets/icons/svg-icon.ts"; class MyScriptMathMenu { constructor() { this.title = "手寫公式"; this.iconSvg = mathIcon; this.tag = "button"; this.showModal = true; this.modalWidth = 900; this.modalHeight = 500; } // 菜單是否需要激活(如選中加粗文本,“加粗”菜單會激活),用不到則返回 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ù)當(dāng)前選區(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="/myscriptMath/index.html"></iframe> </div>`); const $button = $( `<button id="${btnOkId}" class="right" style='margin: 5px 0'> 確認(rèn)插入 </button>` ); $content.append($button); $button.on("click", () => { // 執(zhí)行插入公式 const node = document.getElementById(inputIFrameId); const latex = node.contentWindow.latex; const formulaNode = { type: "paragraph", children: [ { type: "formula", value: latex, children: [ { text: "", }, ], }, ], }; editor.insertNode(formulaNode); editor.hidePanelOrModal(); }); return $content[0]; // 返回 DOM Element 類型 // PS:也可以把 $content 緩存下來,這樣不用每次重復(fù)創(chuàng)建、重復(fù)綁定事件,優(yōu)化性能 } } const menuConf = { key: "myscriptMath", // menu key ,唯一。注冊之后,需通過 toolbarKeys 配置到工具欄 factory() { return new MyScriptMathMenu(); }, }; export default menuConf;
2.2導(dǎo)入注冊實現(xiàn)
import myscriptMath from "@/components/myscriptMath"; import { Boot } from "@wangeditor/editor"; toolbarConfig: { // 插入編輯公式菜單 insertKeys: { index: 0, keys: [ "kityFormula", // “編輯公式”菜單 "myscriptMath", ], }, // excludeKeys: [ /* 隱藏哪些菜單 */ ], }, created() { Boot.registerMenu(myscriptMath); },
3.完整代碼
<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 myscriptMath from "@/components/myscriptMath"; 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", // “編輯公式”菜單 "myscriptMath", ], }, // 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.registerMenu(myscriptMath); 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>
到此這篇關(guān)于vue2使用wangeditor實現(xiàn)手寫輸入功能的文章就介紹到這了,更多相關(guān)vue2 wangeditor手寫輸入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vite中使用Ant?Design?Vue3.x框架教程示例
這篇文章主要為大家介紹了Vite中使用Ant?Design?Vue3.x框架教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06在?Vue?項目中如何引用?assets?文件夾中的圖片(方式匯總)
Vue中引用assets文件夾中的圖片有多種方式,在.vue文件的模板部分,使用相對路徑或通過@別名引用圖片,在CSS中,通過相對路徑或@別名引用圖片作為背景,在JavaScript中,通過import語句導(dǎo)入圖片資源,并使用v-bind在模板中綁定顯示,這些方法均可有效管理和引用項目中的圖片資源2024-09-09Vue中實現(xiàn)v-for循環(huán)遍歷圖片的方法
這篇文章主要介紹了Vue中實現(xiàn)v-for循環(huán)遍歷圖片的方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue項目中使用better-scroll實現(xiàn)一個輪播圖自動播放功能
better-scroll是一個非常非常強大的第三方庫 在移動端利用這個庫 不僅可以實現(xiàn)一個非常類似原生ScrollView的效果 也可以實現(xiàn)一個輪播圖的效果。這篇文章主要介紹了Vue項目中使用better-scroll實現(xiàn)一個輪播圖,需要的朋友可以參考下2018-12-12Vue3監(jiān)聽store中數(shù)據(jù)變化的三種方式
這篇文章給大家介紹了Vue3監(jiān)聽store中數(shù)據(jù)變化的三種方法,使用watch和storeToRefs函數(shù),使用計算屬性computed和使用watchEffect函數(shù)這三種方法,文中通過代碼講解非常詳細(xì),需要的朋友可以參考下2024-01-01