How to get the current most outer parent node type in focus? #3787
-
I'm desparately trying to get the current node type in a function triggered by a menu button (where the user can toggle headlines, bold etc.) I made a custom paragraph with different variants; I did the same with bulletList and orderedList. All of these extensions also got a I need to get
Here's what I'd like to have toggleSize(size) {
const currentTypeName = getCurrentNode();
const currentSize = editor.getAttributes(currentTypeName).size;
// toggle back to default size
if (currentSize && currentSize === size) { ... }
// set desired size
else { ... }
} Here's what I have: toggleSize(size) {
const { editor } = this;
if (editor.isActive('bulletList')) {
const currentSize = editor.getAttributes('bulletList').size;
const newSize = currentSize === size
? 'medium' // default size
: size
;
editor.chain().focus().updateAttributes('bulletList', { size: newSize }).run();
}
if (editor.isActive('orderedList')) {
editor.chain().focus().updateAttributes('orderedList', { size }).run();
}
},
// do the same for a few more node types EDIT: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I found the way to get the parent node from current selection: const state = editor.view.state;
const from = state.selection.from;
const pos = state.doc.resolve(from);
const parent = pos.node(pos.depth - 1); |
Beta Was this translation helpful? Give feedback.
I found the way to get the parent node from current selection: