Skip to content

Commit

Permalink
fix: enhance markup comparison performance
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
  • Loading branch information
aonnikov committed Jan 17, 2025
1 parent f184962 commit c617bb9
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion plugins/text-editor-resources/src/components/diff/recreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import { type Change, diffWordsWithSpace } from 'diff'
import { type Node, type Schema } from '@tiptap/pm/model'
import { ReplaceStep, type Step, Transform } from '@tiptap/pm/transform'
import { deepEqual } from 'fast-equals'
import { applyPatch, createPatch, type Operation } from 'rfc6902'
import { diffAny } from 'rfc6902/diff'
import { type Pointer } from 'rfc6902/pointer'
import { diffArraysPM } from './diff'

Expand Down Expand Up @@ -127,6 +129,13 @@ function clone (obj: any): any {
return result
}

const quickDiff = (input: any, output: any, ptr: Pointer): Operation[] => {
if (ptr.tokens.length > 4) {
return deepEqual(input, output) ? [] : [{ op: 'replace', path: ptr.toString(), value: output }]
}
return diffAny(input, output, ptr, quickDiff)
}

export class StepTransform {
schema: Schema
tr: Transform
Expand All @@ -147,7 +156,7 @@ export class StepTransform {
this.finalDoc = removeMarks(this.toDoc).toJSON()
this.ops = createPatch(this.currentDoc, this.finalDoc, (input: any, output: any, ptr: Pointer) => {
if (Array.isArray(input) && Array.isArray(output)) {
return diffArraysPM(input, output, ptr)
return diffArraysPM(input, output, ptr, quickDiff)
}
})
this.recreateChangeContentSteps()
Expand Down

0 comments on commit c617bb9

Please sign in to comment.