move selectionToInsertionEnd
This commit is contained in:
@@ -1,22 +1,8 @@
|
|||||||
import { DOMParser } from 'prosemirror-model'
|
import { DOMParser } from 'prosemirror-model'
|
||||||
import { Selection, Transaction } from 'prosemirror-state'
|
|
||||||
import { ReplaceStep, ReplaceAroundStep } from 'prosemirror-transform'
|
|
||||||
import elementFromString from '../utilities/elementFromString'
|
import elementFromString from '../utilities/elementFromString'
|
||||||
|
import selectionToInsertionEnd from '../helpers/selectionToInsertionEnd'
|
||||||
import { Command, RawCommands } from '../types'
|
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' {
|
declare module '@tiptap/core' {
|
||||||
interface Commands {
|
interface Commands {
|
||||||
insertHTML: {
|
insertHTML: {
|
||||||
|
|||||||
28
packages/core/src/helpers/selectionToInsertionEnd.ts
Normal file
28
packages/core/src/helpers/selectionToInsertionEnd.ts
Normal 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))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user