mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-12 04:33:34 +08:00
feat: add Tracker class
This commit is contained in:
parent
8e29b5f854
commit
a757716f68
42
packages/core/src/Tracker.ts
Normal file
42
packages/core/src/Tracker.ts
Normal 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,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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'
|
||||
|
Loading…
Reference in New Issue
Block a user