How can I manually destroy a node view? #1317
-
Hello, I am looking for a way to delete the currently selected node using a button (and not the backspace key) inside the node. I can replace the current node with the below code but I need the node to disappear not be replaced.
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
You have to calculate the range of your node view. After that you can use the // get some node view props
// depends on your setup (vue, react)
const { editor, getPos, node } = this
const from = getPos()
const to = from + node.nodeSize
editor.commands.deleteRange({ from, to }) |
Beta Was this translation helpful? Give feedback.
-
If anyone needs to do so with a react custom node, here's a simple example: export const RemovableNode = ({ destroyNode, myLink }) => {
return (
<NodeViewWrapper className="signature">
<div>
<span>
sent with{' '}
<a
href={myLink}
target="_blank"
referrerPolicy="no-referrer"
rel="noreferrer"
>
folk
</a>
</span>
<Button
type="button"
onClick={() => destroyNode()}
>
<MdClose />
</Button>
</div>
</NodeViewWrapper>
);
}; |
Beta Was this translation helpful? Give feedback.
-
The previous solutions didn't work for me so here's how I got it to work. I wanted to delete a specific node ( import {
findChildren,
} from '@tiptap/react'
const photoNodes = findChildren(
editor.state.doc,
(node) => node.type.name === 'photoNode' && node.attrs['photoId'] === photoId
)
if (photoNodes.length) {
const photoNode = photoNodes.at(0)!
editor
.chain()
.focus()
.deleteRange({ from: photoNode.pos, to: photoNode.pos + photoNode.node.nodeSize })
.run()
} |
Beta Was this translation helpful? Give feedback.
You have to calculate the range of your node view. After that you can use the
deleteRange
command.