This commit is contained in:
Philipp Kühn
2021-03-12 23:52:54 +01:00
parent 87059c9b46
commit 5f54a86da7
2 changed files with 8 additions and 17 deletions

View File

@@ -39,13 +39,15 @@ export default {
methods: {
handleUpdate() {
const headings = []
const transaction = this.editor.state.tr
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, {
transaction.setNodeMarkup(pos, undefined, {
...node.attrs,
id,
})
}
@@ -58,28 +60,17 @@ export default {
}
})
this.headings = headings
},
updateNodeAttributes(node, pos, attributes) {
const { state } = this.editor.view
const transaction = state.tr.setNodeMarkup(pos, undefined, {
...node.attrs,
...attributes,
})
transaction.setMeta('preventUpdate', true)
console.log(pos, undefined, {
...node.attrs,
...attributes,
})
// TODO: Why is that not needed? 🤔
this.editor.view.dispatch(transaction)
this.headings = headings
},
},
mounted() {
this.editor.on('update', this.handleUpdate)
this.handleUpdate()
this.$nextTick(this.handleUpdate)
},
}
</script>

View File

@@ -27,6 +27,7 @@ export default {
TableOfContents,
],
content: `
<toc></toc>
<h2>1 heading</h2>
<p>paragraph</p>
<h3>1.1 heading</h3>
@@ -37,7 +38,6 @@ export default {
<p>paragraph</p>
<h3>2.1 heading</h3>
<p>paragraph</p>
<toc></toc>
`,
})
},