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: { methods: {
handleUpdate() { handleUpdate() {
const headings = [] const headings = []
const transaction = this.editor.state.tr
this.editor.state.doc.descendants((node, pos) => { this.editor.state.doc.descendants((node, pos) => {
if (node.type.name === 'heading') { if (node.type.name === 'heading') {
const id = `heading-${headings.length + 1}` const id = `heading-${headings.length + 1}`
if (node.attrs.id !== id) { if (node.attrs.id !== id) {
this.updateNodeAttributes(node, pos, { transaction.setNodeMarkup(pos, undefined, {
...node.attrs,
id, id,
}) })
} }
@@ -58,28 +60,17 @@ export default {
} }
}) })
this.headings = headings transaction.setMeta('preventUpdate', true)
},
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) this.editor.view.dispatch(transaction)
this.headings = headings
}, },
}, },
mounted() { mounted() {
this.editor.on('update', this.handleUpdate) this.editor.on('update', this.handleUpdate)
this.handleUpdate() this.$nextTick(this.handleUpdate)
}, },
} }
</script> </script>

View File

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