From af752ea7618749e80c2f0a51a2c798b84cb695b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Thu, 10 Oct 2024 09:28:28 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9A=E6=96=B0=E5=A2=9E=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=92=8C=E5=88=A0=E9=99=A4=E5=BF=85=E8=A6=81=E7=9A=84?= =?UTF-8?q?css=E6=A0=B7=E5=BC=8F=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=9BFix?= =?UTF-8?q?=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=AF=8C=E6=96=87=E6=9C=AC=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=8B=E8=8A=82=E7=82=B9=E6=96=87=E6=9C=AC=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E8=BF=9E=E7=BB=AD=E7=9A=84=E6=95=B0=E5=AD=97=E6=88=96?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E6=97=B6=E5=AF=BC=E5=87=BA=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=8D=A2=E8=A1=8C=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/index.js | 46 ++++++++++++++++++++++--- simple-mind-map/src/plugins/RichText.js | 22 +++++++----- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/simple-mind-map/index.js b/simple-mind-map/index.js index 90c66547..4e918905 100644 --- a/simple-mind-map/index.js +++ b/simple-mind-map/index.js @@ -52,9 +52,9 @@ class MindMap { this.initWidth = this.width this.initHeight = this.height - // 添加css + // 必要的css样式 this.cssEl = null - this.addCss() + this.cssTextMap = {} // 该样式在实例化时会动态添加到页面,同时导出为svg时也会添加到svg源码中 // 画布 this.initContainer() @@ -98,6 +98,9 @@ class MindMap { this.initPlugin(plugin) }) + // 添加必要的css样式 + this.addCss() + // 初始渲染 this.render(this.opt.fit ? () => this.view.fit() : () => {}) setTimeout(() => { @@ -170,11 +173,41 @@ class MindMap { this.otherDraw.clear() } + // 追加必要的css样式 + // 该样式在实例化时会动态添加到页面,同时导出为svg时也会添加到svg源码中 + appendCss(key, str) { + this.cssTextMap[key] = str + this.removeCss() + this.addCss() + } + + // 移除追加的css样式 + removeAppendCss(key) { + if (this.cssTextMap[key]) { + delete this.cssTextMap[key] + this.removeCss() + this.addCss() + } + } + + // 拼接必要的css样式 + joinCss() { + return ( + cssContent + + Object.keys(this.cssTextMap) + .map(key => { + return this.cssTextMap[key] + }) + .join('\n') + ) + } + // 添加必要的css样式到页面 addCss() { this.cssEl = document.createElement('style') this.cssEl.type = 'text/css' - this.cssEl.innerHTML = cssContent + this.cssEl.innerHTML = this.joinCss() + document.head.appendChild(this.cssEl) } @@ -254,7 +287,10 @@ class MindMap { // 设置主题 initTheme() { // 合并主题配置 - this.themeConfig = mergeTheme(theme[this.opt.theme] || theme.default, this.opt.themeConfig) + this.themeConfig = mergeTheme( + theme[this.opt.theme] || theme.default, + this.opt.themeConfig + ) // 设置背景样式 Style.setBackgroundStyle(this.el, this.themeConfig) } @@ -511,7 +547,7 @@ class MindMap { this.watermark.isInExport = false } // 添加必要的样式 - ;[cssContent, ...cssTextList].forEach(s => { + ;[this.joinCss(), ...cssTextList].forEach(s => { clone.add(SVG(``)) }) // 附加内容 diff --git a/simple-mind-map/src/plugins/RichText.js b/simple-mind-map/src/plugins/RichText.js index 6a46023d..f75e5993 100644 --- a/simple-mind-map/src/plugins/RichText.js +++ b/simple-mind-map/src/plugins/RichText.js @@ -89,6 +89,18 @@ class RichText { // 插入样式 appendCss() { + this.mindMap.appendCss( + 'richText', + ` + .smm-richtext-node-wrap { + word-break: break-all; + } + + .smm-richtext-node-wrap p { + font-family: auto; + } + ` + ) let cssText = ` .ql-editor { overflow: hidden; @@ -107,15 +119,6 @@ class RichText { border: none; } - .smm-richtext-node-wrap { - word-break: break-all; - } - - .smm-richtext-node-wrap p { - font-family: auto; - - } - .smm-richtext-node-edit-wrap p { font-family: auto; } @@ -864,6 +867,7 @@ class RichText { this.transformAllNodesToNormalNode() document.head.removeChild(this.styleEl) this.unbindEvent() + this.mindMap.removeAppendCss('richText') } // 插件被卸载前做的事情