Skip to content

Commit

Permalink
feat: add Tracker class
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Jun 4, 2021
1 parent 8e29b5f commit a757716
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/core/src/Tracker.ts
@@ -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,
}
}

}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Expand Up @@ -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'
Expand Down

0 comments on commit a757716

Please sign in to comment.