Description
There is following blocks of code inside editor's normalizeNode method:
// Ensure that block and inline nodes have at least one text child.
if (Element.isElement(node) && node.children.length === 0) {
const child = { text: '' }; // <-- Problem here
Transforms.insertNodes(editor, child, {
at: path.concat(0),
voids: true,
});
return;
}
and
// Ensure that inline nodes are surrounded by text nodes.
if (editor.isInline(child)) {
if (prev == null || !Text.isText(prev)) {
const newChild = { text: '' }; // <-- Problem here
Transforms.insertNodes(editor, newChild, {
at: path.concat(n),
voids: true,
});
n++;
} else if (isLast) {
const newChild = { text: '' }; // <-- Problem here
Transforms.insertNodes(editor, newChild, {
at: path.concat(n + 1),
voids: true,
});
n++;
}
}
Text node structure is hardcoded with only one text field, but i want to add some properties and declare interface like this:
Here i expect bold property to contain true or false, but sometimes i receive undefined. It's okay for boolean, but i might want to create non-boolean property:
interface Text {
text: string;
bold: boolean;
fontFamily: string; // <- single source of truth for default font family
}
This is not feature request but bug, because of logical conflict between example in docs and real implementation.
Expectation
I expect ability to override text nodes creation inside Slate, for example:
function createCustomTextNode(text: string): Text {
return { text, bold: false, fontFamily: "Helvetica" }
}
function withCustomTextNode(editor: Editor): Editor {
editor.createTextNode = createCustomTextNode;
return editor;
}
And Slate methods should use this method instead of hardcoded values.
Environment
Slate Version: 0.65.3
Operating System: all
Browser: all
TypeScript Version: all
Workaround
I've found current workaround for this problem with overriding editor's apply method:
Description There is following blocks of code inside editor's
normalizeNode
method:and
Text node structure is hardcoded with only one
text
field, but i want to add some properties and declare interface like this:This is correct according to docs (https://docs.slatejs.org/concepts/02-nodes#text).
Here i expect
bold
property to containtrue
orfalse
, but sometimes i receiveundefined
. It's okay for boolean, but i might want to create non-boolean property:This is not feature request but bug, because of logical conflict between example in docs and real implementation.
Expectation I expect ability to override text nodes creation inside Slate, for example:
And Slate methods should use this method instead of hardcoded values.
Environment
Workaround I've found current workaround for this problem with overriding editor's
apply
method: