Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 解决反序列化导致节点丢失块信息出现任意键入后回删文本和图片的问题 #45

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading