use foreach command for imageToFigure

This commit is contained in:
Philipp Kühn
2021-06-04 22:26:18 +02:00
parent 783ce4e3ac
commit ab5a81e3a8
2 changed files with 23 additions and 12 deletions

View File

@@ -122,31 +122,36 @@ export const Figure = Node.create<FigureOptions>({
.run()
},
imageToFigure: () => ({ tr }) => {
imageToFigure: () => ({ tr, commands }) => {
const { doc, selection } = tr
const { from, to } = selection
const range = { from, to }
const nodes = findChildrenInRange(doc, range, node => node.type.name === 'image')
const nodes = findChildrenInRange(doc, { from, to }, node => node.type.name === 'image')
if (!nodes.length) {
return false
}
nodes.forEach(({ node, pos }, index) => {
return commands.forEach(nodes, ({ node, pos }, { index }) => {
const mappedPos = tr.steps
.slice(tr.steps.length - index)
.reduce((newPos, step) => step.getMap().map(newPos), pos)
tr.replaceRangeWith(mappedPos, mappedPos + node.nodeSize, this.type.create({
src: node.attrs.src,
}))
})
const range = {
from: mappedPos,
to: mappedPos + node.nodeSize,
}
return true
return commands.insertContentAt(range, {
type: this.name,
attrs: {
src: node.attrs.src,
},
})
})
},
figureToImage: () => () => {
return true
return false
},
}
},

View File

@@ -3,10 +3,16 @@
<button @click="addFigure">
figure
</button>
<button @click="editor.chain().focus().imageToFigure().run()">
<button
@click="editor.chain().focus().imageToFigure().run()"
:disabled="!editor.can().imageToFigure()"
>
image to figure
</button>
<button @click="editor.chain().focus().figureToImage().run()">
<button
@click="editor.chain().focus().figureToImage().run()"
:disabled="!editor.can().figureToImage()"
>
figure to image
</button>
<editor-content :editor="editor" />