From bdab760cdbf89da7a56dcc1a183010974d2e1290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20K=C3=BChn?= Date: Mon, 7 Feb 2022 10:19:46 +0100 Subject: [PATCH] fix: revert changes to selectAll command, fix #2491 --- .../core/src/commands/setTextSelection.ts | 2 +- .../core/src/helpers/resolveFocusPosition.ts | 28 ++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/core/src/commands/setTextSelection.ts b/packages/core/src/commands/setTextSelection.ts index 75b0e02c7fd..79a45fa36c7 100644 --- a/packages/core/src/commands/setTextSelection.ts +++ b/packages/core/src/commands/setTextSelection.ts @@ -20,7 +20,7 @@ export const setTextSelection: RawCommands['setTextSelection'] = position => ({ ? { from: position, to: position } : position const minPos = TextSelection.atStart(doc).from - const maxPos = doc.content.size + const maxPos = TextSelection.atEnd(doc).to const resolvedFrom = minMax(from, minPos, maxPos) const resolvedEnd = minMax(to, minPos, maxPos) const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd) diff --git a/packages/core/src/helpers/resolveFocusPosition.ts b/packages/core/src/helpers/resolveFocusPosition.ts index a5e6ac49ae1..af7c9dc16ea 100644 --- a/packages/core/src/helpers/resolveFocusPosition.ts +++ b/packages/core/src/helpers/resolveFocusPosition.ts @@ -12,23 +12,31 @@ export function resolveFocusPosition( return null } + const selectionAtStart = Selection.atStart(doc) + const selectionAtEnd = Selection.atEnd(doc) + if (position === 'start' || position === true) { - return Selection.atStart(doc) + return selectionAtStart } if (position === 'end') { - return Selection.atEnd(doc) + return selectionAtEnd } + const minPos = selectionAtStart.from + const maxPos = selectionAtEnd.to + if (position === 'all') { - return TextSelection.create(doc, 0, doc.content.size) + return TextSelection.create( + doc, + minMax(0, minPos, maxPos), + minMax(doc.content.size, minPos, maxPos), + ) } - // Check if `position` is in bounds of the doc if `position` is a number. - const minPos = Selection.atStart(doc).from - const maxPos = Selection.atEnd(doc).to - const resolvedFrom = minMax(position, minPos, maxPos) - const resolvedEnd = minMax(position, minPos, maxPos) - - return TextSelection.create(doc, resolvedFrom, resolvedEnd) + return TextSelection.create( + doc, + minMax(position, minPos, maxPos), + minMax(position, minPos, maxPos), + ) }