From 9425e7249a15e0fb4e8c714545c09f367d216536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 11 Aug 2021 15:06:31 +0200 Subject: [PATCH] fix: improve calculation for min/max positions in selections, fix #1588 --- packages/core/src/commands/focus.ts | 8 +++++--- packages/core/src/commands/setNodeSelection.ts | 8 +++++--- packages/core/src/commands/setTextSelection.ts | 10 ++++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/core/src/commands/focus.ts b/packages/core/src/commands/focus.ts index dd3aee21..4fb3d13c 100644 --- a/packages/core/src/commands/focus.ts +++ b/packages/core/src/commands/focus.ts @@ -1,4 +1,4 @@ -import { EditorState, TextSelection } from 'prosemirror-state' +import { EditorState, Selection, TextSelection } from 'prosemirror-state' import { RawCommands, FocusPosition } from '../types' import minMax from '../utilities/minMax' import isTextSelection from '../helpers/isTextSelection' @@ -59,8 +59,10 @@ export const focus: RawCommands['focus'] = (position = null) => ({ const { from, to } = resolveSelection(editor.state, position) || editor.state.selection const { doc, storedMarks } = tr - const resolvedFrom = minMax(from, 0, doc.content.size) - const resolvedEnd = minMax(to, 0, doc.content.size) + const minPos = Selection.atStart(doc).from + const maxPos = Selection.atEnd(doc).to + const resolvedFrom = minMax(from, minPos, maxPos) + const resolvedEnd = minMax(to, minPos, maxPos) const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd) const isSameSelection = editor.state.selection.eq(selection) diff --git a/packages/core/src/commands/setNodeSelection.ts b/packages/core/src/commands/setNodeSelection.ts index 3fd07590..e40ef3a6 100644 --- a/packages/core/src/commands/setNodeSelection.ts +++ b/packages/core/src/commands/setNodeSelection.ts @@ -1,4 +1,4 @@ -import { NodeSelection } from 'prosemirror-state' +import { Selection, NodeSelection } from 'prosemirror-state' import minMax from '../utilities/minMax' import { RawCommands } from '../types' @@ -16,8 +16,10 @@ declare module '@tiptap/core' { export const setNodeSelection: RawCommands['setNodeSelection'] = position => ({ tr, dispatch }) => { if (dispatch) { const { doc } = tr - const from = minMax(position, 0, doc.content.size) - const selection = NodeSelection.create(doc, from) + const minPos = Selection.atStart(doc).from + const maxPos = Selection.atEnd(doc).to + const resolvedPos = minMax(position, minPos, maxPos) + const selection = NodeSelection.create(doc, resolvedPos) tr.setSelection(selection) } diff --git a/packages/core/src/commands/setTextSelection.ts b/packages/core/src/commands/setTextSelection.ts index 123249f4..ce85ba2a 100644 --- a/packages/core/src/commands/setTextSelection.ts +++ b/packages/core/src/commands/setTextSelection.ts @@ -1,4 +1,4 @@ -import { TextSelection } from 'prosemirror-state' +import { Selection, TextSelection } from 'prosemirror-state' import minMax from '../utilities/minMax' import { RawCommands, Range } from '../types' @@ -19,9 +19,11 @@ export const setTextSelection: RawCommands['setTextSelection'] = position => ({ const { from, to } = typeof position === 'number' ? { from: position, to: position } : position - const boundedFrom = minMax(from, 0, doc.content.size) - const boundedTo = minMax(to, 0, doc.content.size) - const selection = TextSelection.create(doc, boundedFrom, boundedTo) + const minPos = Selection.atStart(doc).from + const maxPos = Selection.atEnd(doc).to + const resolvedFrom = minMax(from, minPos, maxPos) + const resolvedEnd = minMax(to, minPos, maxPos) + const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd) tr.setSelection(selection) }