From 87059c9b462348aaa70226b59061dcdf8f72a7a3 Mon Sep 17 00:00:00 2001 From: Hans Pagel Date: Thu, 11 Mar 2021 00:47:58 +0100 Subject: [PATCH 1/3] add anchors to table of contents (wip) --- .../NodeViews/TableOfContents/Component.vue | 40 +++++++++++++++---- .../TableOfContents/TableOfContents.js | 15 +++++++ .../Guide/NodeViews/TableOfContents/index.vue | 2 +- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/docs/src/demos/Guide/NodeViews/TableOfContents/Component.vue b/docs/src/demos/Guide/NodeViews/TableOfContents/Component.vue index 00dcfb3f..22ea1f62 100644 --- a/docs/src/demos/Guide/NodeViews/TableOfContents/Component.vue +++ b/docs/src/demos/Guide/NodeViews/TableOfContents/Component.vue @@ -7,7 +7,9 @@ v-for="(heading, index) in headings" :key="index" > - {{ heading.text }} + + {{ heading.text }} + @@ -38,17 +40,41 @@ export default { handleUpdate() { const headings = [] - this.editor.state.doc.descendants(node => { + this.editor.state.doc.descendants((node, pos) => { if (node.type.name === 'heading') { + const id = `heading-${headings.length + 1}` + + if (node.attrs.id !== id) { + this.updateNodeAttributes(node, pos, { + id, + }) + } + headings.push({ level: node.attrs.level, text: node.textContent, + id, }) } }) this.headings = headings }, + updateNodeAttributes(node, pos, attributes) { + const { state } = this.editor.view + const transaction = state.tr.setNodeMarkup(pos, undefined, { + ...node.attrs, + ...attributes, + }) + + console.log(pos, undefined, { + ...node.attrs, + ...attributes, + }) + + // TODO: Why is that not needed? 🤔 + this.editor.view.dispatch(transaction) + }, }, mounted() { @@ -58,10 +84,6 @@ export default { } - -