use foreach command for imageToFigure
This commit is contained in:
@@ -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
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -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" />
|
||||||
|
|||||||
Reference in New Issue
Block a user