diff --git a/simple-mind-map/src/plugins/RichText.js b/simple-mind-map/src/plugins/RichText.js index 750be858..7bf4ec91 100644 --- a/simple-mind-map/src/plugins/RichText.js +++ b/simple-mind-map/src/plugins/RichText.js @@ -7,7 +7,8 @@ import { isWhite, getVisibleColorFromTheme, isUndef, - checkSmmFormatData + checkSmmFormatData, + removeHtmlNodeByClass } from '../utils' import { CONSTANTS } from '../constants/constant' @@ -309,6 +310,8 @@ class RichText { // 获取当前正在编辑的内容 getEditText() { let html = this.quill.container.firstChild.innerHTML + // 去除ql-cursor节点 + html = removeHtmlNodeByClass(html, '.ql-cursor') // 去除最后的空行 return html.replace(/


<\/p>$/, '') } @@ -488,7 +491,7 @@ class RichText { // 格式化当前选中的文本 formatText(config = {}, clear = false, pure = false) { if (!this.range && !this.lastRange) return - if(!pure) this.syncFormatToNodeConfig(config, clear) + if (!pure) this.syncFormatToNodeConfig(config, clear) let rangeLost = !this.range let range = rangeLost ? this.lastRange : this.range clear @@ -636,36 +639,6 @@ class RichText { } } - // 处理导出为图片 - async handleExportPng(node) { - let el = document.createElement('div') - el.style.position = 'absolute' - el.style.left = '-9999999px' - el.appendChild(node) - this.mindMap.el.appendChild(el) - // 遍历所有节点,将它们的margin和padding设为0 - let walk = root => { - root.style.margin = 0 - root.style.padding = 0 - if (root.hasChildNodes()) { - Array.from(root.children).forEach(item => { - walk(item) - }) - } - } - walk(node) - - // 如果使用html2canvas - // let canvas = await html2canvas(el, { - // backgroundColor: null - // }) - // return canvas.toDataURL() - - const res = await domtoimage.toPng(el) - this.mindMap.el.removeChild(el) - return res - } - // 将所有节点转换成非富文本节点 transformAllNodesToNormalNode() { walk( diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index 6bd64d58..25458a54 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -535,6 +535,20 @@ export const replaceHtmlText = (html, searchText, replaceText) => { return replaceHtmlTextEl.innerHTML } +// 去除html字符串中指定选择器的节点,然后返回html字符串 +let removeHtmlNodeByClassEl = null +export const removeHtmlNodeByClass = (html, selector) => { + if (!removeHtmlNodeByClassEl) { + removeHtmlNodeByClassEl = document.createElement('div') + } + removeHtmlNodeByClassEl.innerHTML = html + const node = removeHtmlNodeByClassEl.querySelector(selector) + if (node) { + node.parentNode.removeChild(node) + } + return removeHtmlNodeByClassEl.innerHTML +} + // 判断一个颜色是否是白色 export const isWhite = color => { color = String(color).replaceAll(/\s+/g, '')