Relevant code blocks:
<Slate editor={editor} value={value} onChange={value => {
setValue(value);
const { selection } = editor;
// if nothing is currently selected under the cursor
if (selection && Range.isCollapsed(selection)) {
const [start] = Range.edges(selection);
// if the two characters beforce the cursor are {{, select them and replace with a template block
const before = Editor.before(editor, start, {distance: 2})
const beforeRange = before && Editor.range(editor, before, start)
const beforeText = beforeRange && Editor.string(editor, beforeRange)
const beforeMatch = beforeText && beforeText.match(/{{/);
if (beforeMatch) {
Transforms.select(editor, beforeRange as Location);
insertTemplateBlock(editor, {name: "test"})
}
}
}}>
...
</Slate>
export const insertTemplateBlock = (editor: Editor, {name, opts, defaultValue}: TemplateBlockProps) => {
const templateBlock = { type: "template-block", name, opts, defaultValue, children: [{text: ''}] }
Transforms.insertNodes(editor, templateBlock);
Transforms.move(editor);
}
Specifically, it always breaks at the 2nd line within the insertTemplateBlock
function, giving an error along the lines of:
Cannot find a descendant at path [0,2] in node: {"children":[{"type":"paragraph","children":[{"text":"Thing is working {{"}]}],"operations":[{"type":"insert_text","path":[0,0],"offset":18,"text":"{"},{"type":"set_selection","properties":{"anchor":{"path":[0,0],"offset":19}},"newProperties":{"anchor":{"path":[0,0],"offset":17}}},{"type":"remove_text","path":[0,0],"offset":17,"text":"{{"},{"type":"insert_node","path":[0,1],"node":{"type":"template-block","name":"test","children":[{"text":""}]}},{"type":"set_selection","properties":{"anchor":{"path":[0,0],"offset":17},"focus":{"path":[0,0],"offset":17}},"newProperties":{"anchor":{"path":[0,1,0],"offset":0},"focus":{"path":[0,1,0],"offset":0}}},{"type":"insert_node","path":[0,2],"node":{"text":""}},{"type":"set_selection","properties":{"anchor":{"path":[0,1,0],"offset":0},"focus":{"path":[0,1,0],"offset":0}},"newProperties":{"anchor":{"path":[0,2],"offset":0},"focus":{"path":[0,2],"offset":0}}}],"selection":{"anchor":{"path":[0,2],"offset":0},"focus":{"path":[0,2],"offset":0}},"marks":null,"history":{"undos":[[{"type":"set_selection","properties":null,"newProperties":{"anchor":{"path":[0,0],"offset":0},"focus":{"path":[0,0],"offset":0}}}],[{"type":"insert_text","path":[0,0],"offset":0,"text":"T"},{"type":"insert_text","path":[0,0],"offset":1,"text":"h"},{"type":"insert_text","path":[0,0],"offset":2,"text":"i"},{"type":"insert_text","path":[0,0],"offset":3,"text":"n"},{"type":"insert_text","path":[0,0],"offset":4,"text":"g"},{"type":"insert_text","path":[0,0],"offset":5,"text":" "},{"type":"insert_text","path":[0,0],"offset":6,"text":"i"},{"type":"insert_text","path":[0,0],"offset":7,"text":"s"},{"type":"insert_text","path":[0,0],"offset":8,"text":" "},{"type":"insert_text","path":[0,0],"offset":9,"text":"w"},{"type":"insert_text","path":[0,0],"offset":10,"text":"o"},{"type":"insert_text","path":[0,0],"offset":11,"text":"r"},{"type":"insert_text","path":[0,0],"offset":12,"text":"k"},{"type":"insert_text","path":[0,0],"offset":13,"text":"i"},{"type":"insert_text","path":[0,0],"offset":14,"text":"n"},{"type":"insert_text","path":[0,0],"offset":15,"text":"g"},{"type":"insert_text","path":[0,0],"offset":16,"text":" "},{"type":"set_selection","properties":null,"newProperties":{"anchor":{"path":[0,0],"offset":17},"focus":{"path":[0,0],"offset":17}}}],[{"type":"insert_text","path":[0,0],"offset":17,"text":"{"},{"type":"insert_text","path":[0,0],"offset":18,"text":"{"},{"type":"set_selection","properties":{"anchor":{"path":[0,0],"offset":19}},"newProperties":{"anchor":{"path":[0,0],"offset":17}}},{"type":"remove_text","path":[0,0],"offset":17,"text":"{{"},{"type":"insert_node","path":[0,1],"node":{"type":"template-block","name":"test","children":[{"text":""}]}},{"type":"set_selection","properties":{"anchor":{"path":[0,0],"offset":17},"focus":{"path":[0,0],"offset":17}},"newProperties":{"anchor":{"path":[0,1,0],"offset":0},"focus":{"path":[0,1,0],"offset":0}}},{"type":"insert_node","path":[0,2],"node":{"text":""}},{"type":"set_selection","properties":{"anchor":{"path":[0,1,0],"offset":0},"focus":{"path":[0,1,0],"offset":0}},"newProperties":{"anchor":{"path":[0,2],"offset":0},"focus":{"path":[0,2],"offset":0}}}]],"redos":[]}}
This does not happen in their mentions example, which uses a similar logic. Any help would be appreciated!
Codesandbox: https://codesandbox.io/s/elastic-turing-dv9bm?file=/src/App.tsx
UPDATE: I have since found out that this problem can be avoided by moving the insertion logic (currently in the if statement) to a useEffect
hook depending on a target
value containing the selection, similar to how Slate's mentions example works. However, I am still curious to know why Slate breaks in the way that it does when I write it in the way of the question statement.
question from:
https://stackoverflow.com/questions/65852411/slate-js-throws-an-error-when-inserting-a-new-node-at-selected-region