Skip to content

Commit

Permalink
feat: add deleteNode method to node views
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed May 18, 2021
1 parent 0f299d2 commit fcee5f8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/docPages/guide/node-views/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions docs/src/docPages/guide/node-views/vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ export default {
updateAttributes: {
type: Function,
},
// delete the current node
deleteNode: {
type: Function,
},
},
}
</script>
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/NodeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
}
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export type NodeViewProps = {
extension: Node,
getPos: () => number,
updateAttributes: (attributes: Record<string, any>) => void,
deleteNode: () => void,
}

export type NodeViewRendererProps = {
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/ReactNodeViewRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions packages/vue-2/src/VueNodeViewRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions packages/vue-3/src/VueNodeViewRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit fcee5f8

Please sign in to comment.