move selectionToInsertionEnd

This commit is contained in:
Philipp Kühn
2021-02-17 23:30:40 +01:00
parent 51772590cc
commit 501d540c2d
2 changed files with 29 additions and 15 deletions

View File

@@ -1,22 +1,8 @@
import { DOMParser } from 'prosemirror-model'
import { Selection, Transaction } from 'prosemirror-state'
import { ReplaceStep, ReplaceAroundStep } from 'prosemirror-transform'
import elementFromString from '../utilities/elementFromString'
import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd'
import { Command, RawCommands } from '../types'
// TODO: move to utils
// https://github.com/ProseMirror/prosemirror-state/blob/master/src/selection.js#L466
function selectionToInsertionEnd(tr: Transaction, startLen: number, bias: number) {
const last = tr.steps.length - 1
if (last < startLen) return
const step = tr.steps[last]
if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep)) return
const map = tr.mapping.maps[last]
let end = 0
map.forEach((_from, _to, _newFrom, newTo) => { if (end === 0) end = newTo })
tr.setSelection(Selection.near(tr.doc.resolve(end as unknown as number), bias))
}
declare module '@tiptap/core' {
interface Commands {
insertHTML: {

View File

@@ -0,0 +1,28 @@
import { Selection, Transaction } from 'prosemirror-state'
import { ReplaceStep, ReplaceAroundStep } from 'prosemirror-transform'
// source: https://github.com/ProseMirror/prosemirror-state/blob/master/src/selection.js#L466
export default function selectionToInsertionEnd(tr: Transaction, startLen: number, bias: number) {
const last = tr.steps.length - 1
if (last < startLen) {
return
}
const step = tr.steps[last]
if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep)) {
return
}
const map = tr.mapping.maps[last]
let end = 0
map.forEach((_from, _to, _newFrom, newTo) => {
if (end === 0) {
end = newTo
}
})
tr.setSelection(Selection.near(tr.doc.resolve(end), bias))
}