Skip to content

Commit

Permalink
fix: improve calculation for min/max positions in selections, fix #1588
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Aug 11, 2021
1 parent f57466c commit 9425e72
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
8 changes: 5 additions & 3 deletions packages/core/src/commands/focus.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/commands/setNodeSelection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeSelection } from 'prosemirror-state'
import { Selection, NodeSelection } from 'prosemirror-state'
import minMax from '../utilities/minMax'
import { RawCommands } from '../types'

Expand All @@ -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)
}
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/commands/setTextSelection.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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)
}
Expand Down

0 comments on commit 9425e72

Please sign in to comment.