vue2使用wangeditor實(shí)現(xiàn)手寫(xiě)輸入功能
1.效果

2.實(shí)現(xiàn)
2.1:先看我上一篇,這篇就是在上一篇的基礎(chǔ)上添加一個(gè)手寫(xiě)功能,導(dǎo)入注冊(cè)就行了
vue2使用wangeditor實(shí)現(xiàn)數(shù)學(xué)公式+富文本編輯器
在components中添加myscriptMath.js
svg也就是個(gè)顯示的圖標(biāo),可以替換為其他
import $ from "jquery";
import { mathIcon } from "../assets/icons/svg-icon.ts";
class MyScriptMathMenu {
constructor() {
this.title = "手寫(xiě)公式";
this.iconSvg = mathIcon;
this.tag = "button";
this.showModal = true;
this.modalWidth = 900;
this.modalHeight = 500;
}
// 菜單是否需要激活(如選中加粗文本,“加粗”菜單會(huì)激活),用不到則返回 false
isActive(editor) {
return false;
}
// 獲取菜單執(zhí)行時(shí)的 value ,用不到則返回空 字符串或 false
getValue(editor) {
return "";
}
// 菜單是否需要禁用(如選中 H1 ,“引用”菜單被禁用),用不到則返回 false
isDisabled(editor) {
return false;
}
// 點(diǎn)擊菜單時(shí)觸發(fā)的函數(shù)
exec(editor, value) {
// Modal menu ,這個(gè)函數(shù)不用寫(xiě),空著即可
}
// 彈出框 modal 的定位:1. 返回某一個(gè) SlateNode; 2. 返回 null (根據(jù)當(dāng)前選區(qū)自動(dòng)定位)
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 緩存下來(lái),這樣不用每次重復(fù)創(chuàng)建、重復(fù)綁定事件,優(yōu)化性能
}
}
const menuConf = {
key: "myscriptMath", // menu key ,唯一。注冊(cè)之后,需通過(guò) toolbarKeys 配置到工具欄
factory() {
return new MyScriptMathMenu();
},
};
export default menuConf;2.2導(dǎo)入注冊(cè)實(shí)現(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: "請(qǐng)輸入內(nèi)容...",
// autoFocus: false,
// 所有的菜單配置,都要在 MENU_CONF 屬性下
MENU_CONF: {},
},
};
},
methods: {
onCreated(editor) {
console.log("created", editor);
this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否則會(huì)報(bào)錯(cuò)
},
onChange(editor) {
console.log("onChange", editor.getHtml()); // onChange 時(shí)獲取編輯器最新內(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 請(qǐng)求,異步渲染編輯器
setTimeout(() => {
this.html = "<p>Ajax 異步設(shè)置內(nèi)容 HTML</p>";
}, 1500);
},
beforeDestroy() {
const editor = this.editor;
if (editor == null) return;
editor.destroy(); // 組件銷毀時(shí),及時(shí)銷毀 editor ,重要?。?!
},
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>到此這篇關(guān)于vue2使用wangeditor實(shí)現(xiàn)手寫(xiě)輸入功能的文章就介紹到這了,更多相關(guān)vue2 wangeditor手寫(xiě)輸入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vite中使用Ant?Design?Vue3.x框架教程示例
這篇文章主要為大家介紹了Vite中使用Ant?Design?Vue3.x框架教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Vue 實(shí)現(xiàn)前端權(quán)限控制的示例代碼
這篇文章主要介紹了Vue 實(shí)現(xiàn)前端權(quán)限控制的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
在?Vue?項(xiàng)目中如何引用?assets?文件夾中的圖片(方式匯總)
Vue中引用assets文件夾中的圖片有多種方式,在.vue文件的模板部分,使用相對(duì)路徑或通過(guò)@別名引用圖片,在CSS中,通過(guò)相對(duì)路徑或@別名引用圖片作為背景,在JavaScript中,通過(guò)import語(yǔ)句導(dǎo)入圖片資源,并使用v-bind在模板中綁定顯示,這些方法均可有效管理和引用項(xiàng)目中的圖片資源2024-09-09
Vue中實(shí)現(xiàn)v-for循環(huán)遍歷圖片的方法
這篇文章主要介紹了Vue中實(shí)現(xiàn)v-for循環(huán)遍歷圖片的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue項(xiàng)目中使用better-scroll實(shí)現(xiàn)一個(gè)輪播圖自動(dòng)播放功能
better-scroll是一個(gè)非常非常強(qiáng)大的第三方庫(kù) 在移動(dòng)端利用這個(gè)庫(kù) 不僅可以實(shí)現(xiàn)一個(gè)非常類似原生ScrollView的效果 也可以實(shí)現(xiàn)一個(gè)輪播圖的效果。這篇文章主要介紹了Vue項(xiàng)目中使用better-scroll實(shí)現(xiàn)一個(gè)輪播圖,需要的朋友可以參考下2018-12-12
Vue3監(jiān)聽(tīng)store中數(shù)據(jù)變化的三種方式
這篇文章給大家介紹了Vue3監(jiān)聽(tīng)store中數(shù)據(jù)變化的三種方法,使用watch和storeToRefs函數(shù),使用計(jì)算屬性computed和使用watchEffect函數(shù)這三種方法,文中通過(guò)代碼講解非常詳細(xì),需要的朋友可以參考下2024-01-01

