From 27885aabe741962595f2b0aa8f179bae4e70c6d3 Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Sat, 5 Aug 2023 12:04:43 +0800 Subject: [PATCH] =?UTF-8?q?Demo:=E5=A4=A7=E7=BA=B2=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=8A=82=E7=82=B9=E9=BB=98=E8=AE=A4=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/utils/index.js | 55 ++++++++++++++ web/src/pages/Edit/components/Outline.vue | 90 +++++++++++++++++------ 2 files changed, 123 insertions(+), 22 deletions(-) diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index 2be4cc22..9c1745d0 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -539,4 +539,59 @@ export const getVisibleColorFromTheme = (themeConfig) => { return color } } +} + +// 将

形式的节点富文本内容转换成
换行的文本 +let nodeRichTextToTextWithWrapEl = null +export const nodeRichTextToTextWithWrap = (html) => { + if (!nodeRichTextToTextWithWrapEl) { + nodeRichTextToTextWithWrapEl = document.createElement('div') + } + nodeRichTextToTextWithWrapEl.innerHTML = html + const childNodes = nodeRichTextToTextWithWrapEl.childNodes + let res = '' + for(let i = 0; i < childNodes.length; i++) { + const node = childNodes[i] + if (node.nodeType === 1) {// 元素节点 + if (node.tagName.toLowerCase() === 'p') { + res += node.textContent + '\n' + } else { + res += node.textContent + } + } else if (node.nodeType === 3) {// 文本节点 + res += node.nodeValue + } + } + return res.replace(/\n$/, '') +} + +// 将
换行的文本转换成

形式的节点富文本内容 +let textToNodeRichTextWithWrapEl = null +export const textToNodeRichTextWithWrap = (html) => { + if (!textToNodeRichTextWithWrapEl) { + textToNodeRichTextWithWrapEl = document.createElement('div') + } + textToNodeRichTextWithWrapEl.innerHTML = html + const childNodes = textToNodeRichTextWithWrapEl.childNodes + let list = [] + let str = '' + for(let i = 0; i < childNodes.length; i++) { + const node = childNodes[i] + if (node.nodeType === 1) {// 元素节点 + if (node.tagName.toLowerCase() === 'br') { + list.push(str) + str = '' + } else { + str += node.textContent + } + } else if (node.nodeType === 3) {// 文本节点 + str += node.nodeValue + } + } + if (str) { + list.push(str) + } + return list.map((item) => { + return `

${item}

` + }).join('') } \ No newline at end of file diff --git a/web/src/pages/Edit/components/Outline.vue b/web/src/pages/Edit/components/Outline.vue index 173f4095..c106e08e 100644 --- a/web/src/pages/Edit/components/Outline.vue +++ b/web/src/pages/Edit/components/Outline.vue @@ -8,7 +8,11 @@ :expand-on-click-node="false" default-expand-all > - + @@ -26,6 +31,11 @@ @@ -129,7 +165,8 @@ export default {