Delete multiple nodes from document by ID #2252
-
|
Hi everybody, I have a custom block node with uuid and I want to delete several of them in document. What would be the best way to do it? Is it possible to do it in one pass? I am using Is it possible to use I came up with this which looks terrible (simplified version), but it seems it works... but there has to be a better way let ch = this.editor.chain();
ids.forEach((id) => {
ch = ch.command(({ tr, commands }) => {
tr.doc.descendants((node, pos) => {
if (node.type.name == 'custom_item') {
if (id === node.attrs.uuid) {
commands.deleteRange({
from: pos,
to: pos + node.nodeSize,
});
}
return false;
}
});
return true;
});
});
ch.run(); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
There is a import { findChildren } from '@tiptap/core'
editor.commands.forEach(ids, (id, { tr, commands }) => {
const item = findChildren(tr.doc, node => {
return node.type.name === 'custom_item' && id === node.attrs.uuid
})?.[0]
if (!item) {
return true
}
return commands.deleteRange({
from: item.pos,
to: item.pos + item.node.nodeSize,
})
}) |
Beta Was this translation helpful? Give feedback.
-
|
cheers, I will try this out |
Beta Was this translation helpful? Give feedback.
There is a
forEachcommand and afindChildrenhelper which could make this more readable. This code should create one single transaction: