From fb3990d333381758f46e261caf881866ffe17da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Tue, 27 Apr 2021 11:52:50 +0200 Subject: [PATCH] use minMax fo selection commands --- packages/core/src/commands/setNodeSelection.ts | 5 ++++- packages/core/src/commands/setTextSelection.ts | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/core/src/commands/setNodeSelection.ts b/packages/core/src/commands/setNodeSelection.ts index 6427d27ba..2ec8722fa 100644 --- a/packages/core/src/commands/setNodeSelection.ts +++ b/packages/core/src/commands/setNodeSelection.ts @@ -1,4 +1,5 @@ import { NodeSelection } from 'prosemirror-state' +import minMax from '../utilities/minMax' import { Command, RawCommands } from '../types' declare module '@tiptap/core' { @@ -14,7 +15,9 @@ declare module '@tiptap/core' { export const setNodeSelection: RawCommands['setNodeSelection'] = position => ({ tr, dispatch }) => { if (dispatch) { - const selection = NodeSelection.create(tr.doc, position) + const { doc } = tr + const from = minMax(position, 0, doc.content.size) + const selection = NodeSelection.create(doc, from) tr.setSelection(selection) } diff --git a/packages/core/src/commands/setTextSelection.ts b/packages/core/src/commands/setTextSelection.ts index e25cfd981..068ad1054 100644 --- a/packages/core/src/commands/setTextSelection.ts +++ b/packages/core/src/commands/setTextSelection.ts @@ -1,4 +1,5 @@ import { TextSelection } from 'prosemirror-state' +import minMax from '../utilities/minMax' import { Command, RawCommands, Range } from '../types' declare module '@tiptap/core' { @@ -14,7 +15,10 @@ declare module '@tiptap/core' { export const setTextSelection: RawCommands['setTextSelection'] = range => ({ tr, dispatch }) => { if (dispatch) { - const selection = TextSelection.create(tr.doc, range.from, range.to) + const { doc } = tr + const from = minMax(range.from, 0, doc.content.size) + const to = minMax(range.to, 0, doc.content.size) + const selection = TextSelection.create(doc, from, to) tr.setSelection(selection) }