diff.ts (1795B)
1 import {Fragment} from "./fragment" 2 3 export function findDiffStart(a: Fragment, b: Fragment, pos: number): number | null { 4 for (let i = 0;; i++) { 5 if (i == a.childCount || i == b.childCount) 6 return a.childCount == b.childCount ? null : pos 7 8 let childA = a.child(i), childB = b.child(i) 9 if (childA == childB) { pos += childA.nodeSize; continue } 10 11 if (!childA.sameMarkup(childB)) return pos 12 13 if (childA.isText && childA.text != childB.text) { 14 for (let j = 0; childA.text![j] == childB.text![j]; j++) 15 pos++ 16 return pos 17 } 18 if (childA.content.size || childB.content.size) { 19 let inner = findDiffStart(childA.content, childB.content, pos + 1) 20 if (inner != null) return inner 21 } 22 pos += childA.nodeSize 23 } 24 } 25 26 export function findDiffEnd(a: Fragment, b: Fragment, posA: number, posB: number): {a: number, b: number} | null { 27 for (let iA = a.childCount, iB = b.childCount;;) { 28 if (iA == 0 || iB == 0) 29 return iA == iB ? null : {a: posA, b: posB} 30 31 let childA = a.child(--iA), childB = b.child(--iB), size = childA.nodeSize 32 if (childA == childB) { 33 posA -= size; posB -= size 34 continue 35 } 36 37 if (!childA.sameMarkup(childB)) return {a: posA, b: posB} 38 39 if (childA.isText && childA.text != childB.text) { 40 let same = 0, minSize = Math.min(childA.text!.length, childB.text!.length) 41 while (same < minSize && childA.text![childA.text!.length - same - 1] == childB.text![childB.text!.length - same - 1]) { 42 same++; posA--; posB-- 43 } 44 return {a: posA, b: posB} 45 } 46 if (childA.content.size || childB.content.size) { 47 let inner = findDiffEnd(childA.content, childB.content, posA - 1, posB - 1) 48 if (inner) return inner 49 } 50 posA -= size; posB -= size 51 } 52 }