feat: add deleteNode method to node views
This commit is contained in:
@@ -106,6 +106,7 @@ Here is the full list of what props you can expect:
|
||||
| `extension` | Access to the node extension, for example to get options |
|
||||
| `getPos` | Get the document position of the current node |
|
||||
| `updateAttributes` | Update attributes of the current node |
|
||||
| `deleteNode` | Delete the current node |
|
||||
|
||||
## Dragging
|
||||
To make your node views draggable, set `draggable: true` in the extension and add `data-drag-handle` to the DOM element that should function as the drag handle.
|
||||
|
||||
@@ -167,6 +167,11 @@ export default {
|
||||
updateAttributes: {
|
||||
type: Function,
|
||||
},
|
||||
|
||||
// delete the current node
|
||||
deleteNode: {
|
||||
type: Function,
|
||||
},
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -238,4 +238,10 @@ export class NodeView<Component, Editor extends CoreEditor = CoreEditor> impleme
|
||||
this.editor.view.dispatch(transaction)
|
||||
}
|
||||
|
||||
deleteNode(): void {
|
||||
const from = this.getPos()
|
||||
const to = from + this.node.nodeSize
|
||||
|
||||
this.editor.commands.deleteRange({ from, to })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,6 +142,7 @@ export type NodeViewProps = {
|
||||
extension: Node,
|
||||
getPos: () => number,
|
||||
updateAttributes: (attributes: Record<string, any>) => void,
|
||||
deleteNode: () => void,
|
||||
}
|
||||
|
||||
export type NodeViewRendererProps = {
|
||||
|
||||
@@ -31,6 +31,7 @@ class ReactNodeView extends NodeView<React.FunctionComponent, Editor> {
|
||||
extension: this.extension,
|
||||
getPos: () => this.getPos(),
|
||||
updateAttributes: (attributes = {}) => this.updateAttributes(attributes),
|
||||
deleteNode: () => this.deleteNode(),
|
||||
}
|
||||
|
||||
if (!(this.component as any).displayName) {
|
||||
|
||||
@@ -40,6 +40,10 @@ export const nodeViewProps = {
|
||||
type: Function as PropType<NodeViewProps['updateAttributes']>,
|
||||
required: true,
|
||||
},
|
||||
deleteNode: {
|
||||
type: Function as PropType<NodeViewProps['deleteNode']>,
|
||||
required: true,
|
||||
},
|
||||
}
|
||||
|
||||
export interface VueNodeViewRendererOptions {
|
||||
@@ -64,6 +68,7 @@ class VueNodeView extends NodeView<(Vue | VueConstructor), Editor> {
|
||||
extension: this.extension,
|
||||
getPos: () => this.getPos(),
|
||||
updateAttributes: (attributes = {}) => this.updateAttributes(attributes),
|
||||
deleteNode: () => this.deleteNode(),
|
||||
}
|
||||
|
||||
const onDragStart = this.onDragStart.bind(this)
|
||||
|
||||
@@ -46,6 +46,10 @@ export const nodeViewProps = {
|
||||
type: Function as PropType<NodeViewProps['updateAttributes']>,
|
||||
required: true,
|
||||
},
|
||||
deleteNode: {
|
||||
type: Function as PropType<NodeViewProps['deleteNode']>,
|
||||
required: true,
|
||||
},
|
||||
}
|
||||
|
||||
interface VueNodeViewRendererOptions {
|
||||
@@ -68,6 +72,7 @@ class VueNodeView extends NodeView<Component, Editor> {
|
||||
extension: this.extension,
|
||||
getPos: () => this.getPos(),
|
||||
updateAttributes: (attributes = {}) => this.updateAttributes(attributes),
|
||||
deleteNode: () => this.deleteNode(),
|
||||
}
|
||||
|
||||
const onDragStart = this.onDragStart.bind(this)
|
||||
|
||||
Reference in New Issue
Block a user