From 405f4d959b24460d4b4397718f0d02211f02449a Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Sat, 21 Dec 2024 20:40:48 +0800 Subject: [PATCH] fix: fix parse init html error if the content is text only --- src/main.ts | 6 ++---- src/util/htmlUtil.ts | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main.ts b/src/main.ts index c1dbbda..a6ac5be 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,10 +12,8 @@ window.aiEditor = new AiEditor({ // pasteAsText: true, // draggable:false, // editable:false, - content: '# AiEditor 是一个 **面向 AI 的** 下一代富文本编辑器。 \n **提示:** \n- 1、输入 空格 + "/" 可以快速弹出 AI 菜单 \n- 2、输入 空格 + "@" 可以提及某人 ' + - '\n\n haha ![图片alt](https://aieditor.com.cn/logo.png)' + - '\n ', - contentIsMarkdown: true, + content: 'AiEditor 是一个面向 AI 的下一代富文本编辑器。', + // contentIsMarkdown: true, textSelectionBubbleMenu: { // enable:false //[AI, Bold, Italic, Underline, Strike, Code] diff --git a/src/util/htmlUtil.ts b/src/util/htmlUtil.ts index 7078e87..7613430 100644 --- a/src/util/htmlUtil.ts +++ b/src/util/htmlUtil.ts @@ -173,20 +173,23 @@ export const organizeHTMLContent = (originalHtml: string) => { } let html = ''; - for (let i = 0; i < doc.body.children.length; i++) { - const element = doc.body.children[i]; - if (i == 0 && element.tagName === "P") { - html += element.innerHTML; - } else { - // https://gitee.com/aieditor-team/aieditor/pulls/10 - if (element.querySelector("img") && element.tagName !== "A") { - //return image element + doc.body.childNodes.forEach(node => { + if (node.nodeType === Node.TEXT_NODE) { + html += node.textContent; + } else if (node.nodeType === Node.ELEMENT_NODE) { + const element = node as HTMLElement; + if (element === doc.body.firstChild && element.tagName === "P") { html += element.innerHTML; } else { - html += element.outerHTML; + // https://gitee.com/aieditor-team/aieditor/pulls/10 + if (element.querySelector("img") && element.tagName !== "A") { + //return image element + html += element.innerHTML; + } else { + html += element.outerHTML; + } } } - } - // debugger + }) return html; }