Repository navigation
Ensuring the last node in the editor is a paragraph #5713
Replies: 1 comment
|
Install the normalizer before initialization, then call the static import { createEditor, Editor, Element, Transforms } from 'slate';
function withTrailingParagraph(editor) {
const { normalizeNode } = editor;
editor.normalizeNode = (entry, options) => {
const [, path] = entry;
if (path.length === 0) {
const last = editor.children.at(-1);
if (!Element.isElement(last) || last.type !== 'paragraph') {
Transforms.insertNodes(editor,
{ type: 'paragraph', children: [{ text: '' }] },
{ at: [editor.children.length] });
return;
}
}
normalizeNode(entry, options);
};
return editor;
}
const editor = withTrailingParagraph(createEditor());
editor.children = [{ type: 'image', children: [{ text: '' }] }];
Editor.normalize(editor, { force: true });
// editor.children ends with a paragraph.Both Tested with Slate 0.133.7: an initial image, empty initial document, already-ending paragraph, and later insertion of a final non-paragraph all preserve one trailing paragraph. Custom normalization, Editor normalization API. |
Uh oh!
There was an error while loading. Please reload this page.
I'm having trouble with that seems to be a very simple thing to implement.
In my editor I have voids, these voids are not editable, so it's quite difficult to add a new node after them. Ideally I'd like if there was always a paragraph as the last node in my editor.
My initial thought was to use normalization for this.
The above code does work if I've just added a node to the editor, but it doesn't run on the editor's first render.
I even tried adding
But the normalize function just won't run.
What's my best option here?
All reactions