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

View File

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