BitPhinix / slate-yjs

Yjs binding for Slate
https://docs.slate-yjs.dev
MIT License
514 stars 73 forks source link

slate-yjs demo withNormalize plugin Constant insertion of ‘’ characters problem #424

Open ZhangMingZhao1 opened 5 months ago

ZhangMingZhao1 commented 5 months ago
import { Editor, Transforms } from 'slate';

export function withNormalize(editor: Editor) {
    const { normalizeNode } = editor;

    // Ensure editor always has at least one child.
    editor.normalizeNode = (entry) => {
        const [node] = entry;
        console.log('node.children', node.children);

        if (!Editor.isEditor(node) || node.children.length > 0) {
            return normalizeNode(entry);
            // return;
        }
        // if()
        Transforms.insertNodes(
            editor,
            {
                type: 'paragraph',
                children: [{ text: '' }]
            },
            { at: [0] }
        );
    };

    return editor;
}

this project demo use withNormalize plugin,because normalizeNode can execute multiple times, (!Editor.isEditor(node) || node.children.length > 0) 。This code snippet is likely to be executed every time the document is initialized