From 3a743f4284d85b92b5dcada106a9b2af7d3a326f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 8 Nov 2021 13:42:10 +0100 Subject: [PATCH 1/4] check if we can replace the wrapping node by the newly inserted content / --- packages/core/src/commands/insertContentAt.ts | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/core/src/commands/insertContentAt.ts b/packages/core/src/commands/insertContentAt.ts index ea875a89c..28b0650aa 100644 --- a/packages/core/src/commands/insertContentAt.ts +++ b/packages/core/src/commands/insertContentAt.ts @@ -45,10 +45,35 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value, return true } - const { from, to } = typeof position === 'number' + let { from, to } = typeof position === 'number' ? { from: position, to: position } : position + let isOnlyBlockContent = true + + content.forEach(node => { + isOnlyBlockContent = isOnlyBlockContent + ? node.isBlock + : false + }) + + // check if we can replace the wrapping node by + // the newly inserted content + // example: + // replace an empty paragraph by an inserted image + // instead of inserting the image below the paragraph + if (from === to && isOnlyBlockContent) { + const $from = tr.doc.resolve(from) + const isEmptyTextBlock = $from.parent.isTextblock + && !$from.parent.type.spec.code + && !$from.parent.textContent + + if (isEmptyTextBlock) { + from -= 1 + to += 1 + } + } + tr.replaceWith(from, to, content) // set cursor at end of inserted content From 50f14b538638f45921572e43c6d603720a247f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 8 Nov 2021 13:42:37 +0100 Subject: [PATCH 2/4] we dont need this anymore --- .../src/horizontal-rule.ts | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/packages/extension-horizontal-rule/src/horizontal-rule.ts b/packages/extension-horizontal-rule/src/horizontal-rule.ts index c82ab053b..64751f99e 100644 --- a/packages/extension-horizontal-rule/src/horizontal-rule.ts +++ b/packages/extension-horizontal-rule/src/horizontal-rule.ts @@ -45,26 +45,6 @@ export const HorizontalRule = Node.create({ return { setHorizontalRule: () => ({ chain }) => { return chain() - // remove node before hr if it’s an empty text block - .command(({ tr, dispatch }) => { - const { selection } = tr - const { empty, $anchor } = selection - const isEmptyTextBlock = $anchor.parent.isTextblock - && !$anchor.parent.type.spec.code - && !$anchor.parent.textContent - - if (!empty || !isEmptyTextBlock || !dispatch) { - return true - } - - const from = $anchor.before() - const to = $anchor.start() - - tr.deleteRange(from, to) - tr.setSelection(TextSelection.create(tr.doc, from)) - - return true - }) .insertContent({ type: this.name }) // add node after hr if it’s the end of the document .command(({ tr, dispatch }) => { From 6f86f6732f64f2c91fa436716a97b5e50e4da5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 8 Nov 2021 14:15:14 +0100 Subject: [PATCH 3/4] set selection to inserted content instead of below --- packages/core/src/commands/insertContentAt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/commands/insertContentAt.ts b/packages/core/src/commands/insertContentAt.ts index 28b0650aa..10f1f69f1 100644 --- a/packages/core/src/commands/insertContentAt.ts +++ b/packages/core/src/commands/insertContentAt.ts @@ -78,7 +78,7 @@ export const insertContentAt: RawCommands['insertContentAt'] = (position, value, // set cursor at end of inserted content if (options.updateSelection) { - selectionToInsertionEnd(tr, tr.steps.length - 1, 1) + selectionToInsertionEnd(tr, tr.steps.length - 1, -1) } } From e96dc162ba4d5f724f37b048733742ce6b52dbd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 8 Nov 2021 14:15:34 +0100 Subject: [PATCH 4/4] improve hr cursor behavior --- packages/extension-horizontal-rule/src/horizontal-rule.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/extension-horizontal-rule/src/horizontal-rule.ts b/packages/extension-horizontal-rule/src/horizontal-rule.ts index 64751f99e..6f583e1fd 100644 --- a/packages/extension-horizontal-rule/src/horizontal-rule.ts +++ b/packages/extension-horizontal-rule/src/horizontal-rule.ts @@ -46,14 +46,17 @@ export const HorizontalRule = Node.create({ setHorizontalRule: () => ({ chain }) => { return chain() .insertContent({ type: this.name }) - // add node after hr if it’s the end of the document + // set cursor after horizontal rule .command(({ tr, dispatch }) => { if (dispatch) { const { parent, pos } = tr.selection.$from const posAfter = pos + 1 const nodeAfter = tr.doc.nodeAt(posAfter) - if (!nodeAfter) { + if (nodeAfter) { + tr.setSelection(TextSelection.create(tr.doc, posAfter)) + } else { + // add node after horizontal rule if it’s the end of the document const node = parent.type.contentMatch.defaultType?.create() if (node) {