通过哪种方式可以防止用户从slate.js编辑器中删除特定的块(或者通知用户将删除块),我的目标是保护信息不被意外删除。提前谢谢。
发布于 2022-08-25 09:51:41
我在找类似的东西。
我发现的一种方法是--在节点内部防止默认的deleteForward和deleteBackward调用。
例如,我有一个front-matter节点,它包含关于文档的元信息,我希望用户编辑它,但从不从树中删除。
text节点是可编辑的,这是模式
{
type: 'front-matter',
children: [{ text: 'Welcome to the document' }],
}这就是插件
export const withFrontmatter = (editor: Editor) => {
const { deleteForward } = editor;
editor.deleteForward = (unit: TextUnit) => {
const [frontmatter] = Editor.nodes(editor, {
match: (node) =>
!Editor.isEditor(node) && Element.isElement(node) &&
node.type === 'front-matter'
});
if (
!!frontmatter &&
Element.isElement(frontmatter[0]) &&
Editor.isEmpty(editor, frontmatter[0])
) {
return;
}
deleteForward(unit);
};
return editor;
};相同的逻辑用于deleteBackward,而您有一个不可删除的节点。
Editor.isEmpty(editor, frontmatter[0])检查内部text片段是否为空。
https://stackoverflow.com/questions/69486318
复制相似问题