mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-24 17:47:50 +08:00
43 lines
798 B
TypeScript
43 lines
798 B
TypeScript
|
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,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|