Skip to content

Commit

Permalink
use minMax fo selection commands
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Apr 27, 2021
1 parent eb7e92f commit fb3990d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/commands/setNodeSelection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NodeSelection } from 'prosemirror-state'
import minMax from '../utilities/minMax'
import { Command, RawCommands } from '../types'

declare module '@tiptap/core' {
Expand All @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/commands/setTextSelection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TextSelection } from 'prosemirror-state'
import minMax from '../utilities/minMax'
import { Command, RawCommands, Range } from '../types'

declare module '@tiptap/core' {
Expand All @@ -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)
}
Expand Down

0 comments on commit fb3990d

Please sign in to comment.