use insertContent in some extensions

This commit is contained in:
Philipp Kühn
2021-05-05 14:50:43 +02:00
parent 12b6f0e4f7
commit fda4c780d3
2 changed files with 25 additions and 26 deletions

View File

@@ -42,28 +42,31 @@ export const HorizontalRule = Node.create<HorizontalRuleOptions>({
addCommands() { addCommands() {
return { return {
setHorizontalRule: () => ({ tr, dispatch }) => { setHorizontalRule: () => ({ chain }) => {
if (dispatch) { return chain()
tr.replaceSelectionWith(this.type.create()) .insertContent({ type: this.name })
.command(({ tr, dispatch }) => {
if (dispatch) {
const { parent, pos } = tr.selection.$from
const posAfter = pos + 1
const nodeAfter = tr.doc.nodeAt(posAfter)
const { parent, pos } = tr.selection.$from // end of document
const posAfter = pos + 1 if (!nodeAfter) {
const nodeAfter = tr.doc.nodeAt(posAfter) const node = parent.type.contentMatch.defaultType?.create()
// end of document if (node) {
if (!nodeAfter) { tr.insert(posAfter, node)
const node = parent.type.contentMatch.defaultType?.create() tr.setSelection(TextSelection.create(tr.doc, posAfter))
}
}
if (node) { tr.scrollIntoView()
tr.insert(posAfter, node)
tr.setSelection(TextSelection.create(tr.doc, posAfter))
} }
}
tr.scrollIntoView() return true
} })
.run()
return true
}, },
} }
}, },

View File

@@ -69,15 +69,11 @@ export const Image = Node.create<ImageOptions>({
addCommands() { addCommands() {
return { return {
setImage: options => ({ tr, dispatch }) => { setImage: options => ({ commands }) => {
const { selection } = tr return commands.insertContent({
const node = this.type.create(options) type: this.name,
attrs: options,
if (dispatch) { })
tr.replaceRangeWith(selection.from, selection.to, node)
}
return true
}, },
} }
}, },