Skip to content

Commit 28688bb

Browse files
committed
Add custom command to node for example
1 parent 27b55ab commit 28688bb

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

examples/vue-custom-node-example/src/components/DocumentEditor.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,18 @@ const hooks = {
6666
6767
// Let's insert our custom node into the document after the editor is ready
6868
editor.value?.activeEditor.commands.insertContent(`
69-
<div data-node-type='customNode' data-id='some-id-123'>Custom Node Content</div>
69+
<div data-node-type='customNode' id='some-id-123'>Custom Node Content</div>
7070
`);
7171
72+
// Now instead of using the generic insertContent, we can use our custom node command
73+
editor.value.activeEditor.commands.insertCustomNode({
74+
id: 'second-node-id',
75+
content: 'Display this text!',
76+
})
77+
78+
// Let's check our current HTML
7279
const html = editor.value?.activeEditor.getHTML();
80+
console.debug('Editor HTML:', html);
7381
},
7482
onError: (error) => {
7583
emit('editor-error', error);
@@ -162,6 +170,7 @@ onUnmounted(() => {
162170
color: white;
163171
display: inline-block;
164172
padding: 2px 8px;
173+
font-size: 12px;
165174
}
166175
.my-custom-node-default-class:hover {
167176
background-color: #0a3dff;

examples/vue-custom-node-example/src/plugins/MyCustomNodePlugin.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ export const myCustomNode = Extensions.Node.create({
3131
// Add custom node attributes here
3232
addAttributes() {
3333
return {
34-
'data-id': {
34+
id: {
3535
default: null,
3636

3737
// The DOM attribute to be used for this node attribute
38-
parseDOM: (elem) => elem.getAttribute('data-id'),
38+
parseDOM: (elem) => elem.getAttribute('id'),
3939

4040
// Tell the node how to render this attribute in the DOM
41-
renderDOM: ({ 'data-id': id }) => {
41+
renderDOM: ({ id }) => {
4242
if (!id) return {};
43-
return { 'data-id': id };
43+
return { id };
4444
},
4545
}
4646
};
@@ -57,4 +57,21 @@ export const myCustomNode = Extensions.Node.create({
5757
renderDOM({ htmlAttributes }) {
5858
return ['div', Attribute.mergeAttributes(this.options.htmlAttributes, htmlAttributes), 0];
5959
},
60+
61+
// Add custom commands here
62+
addCommands() {
63+
return {
64+
insertCustomNode: ({ content, id }) => ({ commands }) => {
65+
return commands.insertContent({
66+
type: this.name,
67+
attrs: { id },
68+
69+
// Our custom node only renders placeholder text, but in theory it can render any other editor node
70+
content: [
71+
{ type: 'text', text: content || 'Default Content' },
72+
]
73+
});
74+
},
75+
};
76+
}
6077
});

0 commit comments

Comments
 (0)