Which way I can prevent user to remove specific block from slate.js editor (or inform user that block will be deleted) my goal is protect information from accidental removal. Thanks in advance.
Slate.js prevent block deletion
968 Views Asked by gingray At
3
There are 3 best solutions below
0
On
Add the property: contentEditable={false} to the element renderer, this will prevent this element (node) from being changed in the editor
0
On
In slate, all the delete / remove node seems to be happening the endpoint of
editor.apply('remove-node', options);
Therefore, adding plugins worked for me as shown below:
const withPreventBlockDeletion = editor => {
const { apply } = editor
editor.apply = operation => {
if (operation.type === 'remove_node') {
const { node } = operation;
if (node.type === 'your block type') {
// Prevent certain element/node to be deleted
return;
}
}
// Other cases
apply(operation)
}
return editor
}
I was looking for something similar.
One way that I found was - by preventing default
deleteForwardanddeleteBackwardinvocation when inside that node.For example I have a
front-matternode that contains meta-information about the document and I'd like the user to edit it but never remove from the tree.textnode is editable and this is the schemaAnd this is the plugin
Identical logic for
deleteBackwardand you have an un-deletable node.Editor.isEmpty(editor, frontmatter[0])checks if the innertextfragment is empty.