feat: add Tracker class

This commit is contained in:
Philipp Kühn 2021-06-04 23:57:41 +02:00
parent 8e29b5f854
commit a757716f68
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,42 @@
import { Transaction } from 'prosemirror-state'
export interface TrackerResult {
position: number,
deleted: boolean,
}
export class Tracker {
transaction: Transaction
currentStep: number
constructor(transaction: Transaction) {
this.transaction = transaction
this.currentStep = this.transaction.steps.length
}
map(position: number): TrackerResult {
let deleted = false
const mappedPosition = this.transaction.steps
.slice(this.currentStep)
.reduce((newPosition, step) => {
const mapResult = step
.getMap()
.mapResult(newPosition)
if (mapResult.deleted) {
deleted = true
}
return mapResult.pos
}, position)
return {
position: mappedPosition,
deleted,
}
}
}

View File

@ -3,6 +3,7 @@ export * from './Extension'
export * from './Node'
export * from './Mark'
export * from './NodeView'
export * from './Tracker'
export * from './types'
export { default as nodeInputRule } from './inputRules/nodeInputRule'