| 38 | } |
| 39 | |
| 40 | export function applyFastDiff(ytext: Y.Text, currentText: string, newText: string): void { |
| 41 | if (currentText === newText) return; |
| 42 | if ( |
| 43 | currentText.length > APPLY_FAST_DIFF_MAX_BYTES || |
| 44 | newText.length > APPLY_FAST_DIFF_MAX_BYTES |
| 45 | ) { |
| 46 | applyByPrefixSuffixMiddleReplace(ytext, currentText, newText); |
| 47 | return; |
| 48 | } |
| 49 | const diffs = dmpDiff.diff_main(currentText, newText); |
| 50 | dmpDiff.diff_cleanupSemantic(diffs); |
| 51 | let offset = 0; |
| 52 | for (const [type, text] of diffs) { |
| 53 | if (type === 0) { |
| 54 | offset += text.length; |
| 55 | } else if (type === -1) { |
| 56 | ytext.delete(offset, text.length); |
| 57 | } else if (type === 1) { |
| 58 | ytext.insert(offset, text); |
| 59 | offset += text.length; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | function applyByPrefixSuffixMiddleReplace( |
| 65 | ytext: Y.Text, |