Fix:优化公式插件,适配创建多个实例的情况

This commit is contained in:
街角小林 2024-09-24 18:21:48 +08:00
parent 19fa0af6c0
commit 0344599411
2 changed files with 18 additions and 1 deletions

View File

@ -35,6 +35,7 @@ class MindMap {
* @param {defaultOpt} opt * @param {defaultOpt} opt
*/ */
constructor(opt = {}) { constructor(opt = {}) {
MindMap.instanceCount++
// 合并选项 // 合并选项
this.opt = this.handleOpt(merge(defaultOpt, opt)) this.opt = this.handleOpt(merge(defaultOpt, opt))
// 预处理节点数据 // 预处理节点数据
@ -618,6 +619,7 @@ class MindMap {
this.el.innerHTML = '' this.el.innerHTML = ''
this.el = null this.el = null
this.removeCss() this.removeCss()
MindMap.instanceCount--
} }
} }
@ -634,6 +636,7 @@ MindMap.hasPlugin = plugin => {
return item === plugin return item === plugin
}) })
} }
MindMap.instanceCount = 0
// 定义新主题 // 定义新主题
MindMap.defineTheme = (name, config = {}) => { MindMap.defineTheme = (name, config = {}) => {

View File

@ -4,6 +4,7 @@ import { getChromeVersion, htmlEscape } from '../utils/index'
import { getBaseStyleText, getFontStyleText } from './FormulaStyle' import { getBaseStyleText, getFontStyleText } from './FormulaStyle'
let extended = false let extended = false
const QuillFormula = Quill.import('formats/formula')
// 数学公式支持插件 // 数学公式支持插件
// 该插件在富文本模式下可用 // 该插件在富文本模式下可用
@ -18,6 +19,18 @@ class Formula {
this.cssEl = null this.cssEl = null
this.addStyle() this.addStyle()
this.extendQuill() this.extendQuill()
this.onDestroy = this.onDestroy.bind(this)
this.mindMap.on('beforeDestroy', this.onDestroy)
}
onDestroy() {
const instanceCount = Object.getPrototypeOf(this.mindMap).constructor
.instanceCount
// 如果思维导图实例数量变成0了那么就恢复成默认的
if (instanceCount <= 1) {
extended = false
Quill.register('formats/formula', QuillFormula, true)
}
} }
init() { init() {
@ -55,7 +68,6 @@ class Formula {
if (extended) return if (extended) return
extended = true extended = true
const QuillFormula = Quill.import('formats/formula')
const self = this const self = this
class CustomFormulaBlot extends QuillFormula { class CustomFormulaBlot extends QuillFormula {
@ -173,11 +185,13 @@ class Formula {
// 插件被移除前做的事情 // 插件被移除前做的事情
beforePluginRemove() { beforePluginRemove() {
this.removeStyle() this.removeStyle()
this.mindMap.off('beforeDestroy', this.onDestroy)
} }
// 插件被卸载前做的事情 // 插件被卸载前做的事情
beforePluginDestroy() { beforePluginDestroy() {
this.removeStyle() this.removeStyle()
this.mindMap.off('beforeDestroy', this.onDestroy)
} }
} }