Skip to content

Commit

Permalink
fix: 解决反序列化导致节点丢失块信息出现任意键入后回删文本和图片的问题 (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
vagusX committed Jul 25, 2023
1 parent 4eaf418 commit 895bd97
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/components/editor/deserialize.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { jsx } from 'slate-hyperscript';

type JSXElement = ReturnType<typeof jsx>;
const deserialize = (el: Node): JSXElement | string | null => {
type SlateJSXElement = ReturnType<typeof jsx>;
const deserialize = (el: Node): SlateJSXElement | string | null => {
// 递归到 children 时直接返回 textContent string 格式
if (el.nodeType === 3) {
return (el as Text).textContent;
} else if (el.nodeType !== 1) {
return null;
}

const htmlElement = el as HTMLElement;
let children: (JSXElement | string)[] = Array.from(
let children: (SlateJSXElement | string)[] = Array.from(
htmlElement.childNodes,
).map(deserialize);

Expand Down Expand Up @@ -46,7 +47,7 @@ const deserialize = (el: Node): JSXElement | string | null => {
children,
);
default:
return htmlElement.textContent || '';
return jsx('element', { type: 'paragraph' }, [ htmlElement.textContent || '' ]);
}
};

Expand Down

0 comments on commit 895bd97

Please sign in to comment.