|
| 1 | +import { |
| 2 | + ViewPlugin, |
| 3 | + Decoration, |
| 4 | + DecorationSet, |
| 5 | + ViewUpdate, |
| 6 | + WidgetType, |
| 7 | + EditorView |
| 8 | +} from '@codemirror/view'; |
| 9 | +import * as Diff from 'diff'; |
| 10 | +class HighlightDiff { |
| 11 | + constructor(view: EditorView) { |
| 12 | + this._view = view; |
| 13 | + this.decorations = Decoration.none; |
| 14 | + this._originalText = this._view.state.doc.toString(); |
| 15 | + this.updateDecorations(); |
| 16 | + } |
| 17 | + |
| 18 | + update(update: ViewUpdate) { |
| 19 | + if (update.docChanged || update.selectionSet) { |
| 20 | + this.updateDecorations(); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + updateDecorations() { |
| 25 | + const currentText = this._view.state.doc.toString(); |
| 26 | + // Compare words with spaces |
| 27 | + const diffs = Diff.diffWordsWithSpace(this._originalText, currentText); |
| 28 | + |
| 29 | + const decorations = []; |
| 30 | + let pos = 0; |
| 31 | + |
| 32 | + for (const diff of diffs) { |
| 33 | + const length = diff.value.length; |
| 34 | + |
| 35 | + if (diff.added) { |
| 36 | + // Highlight added text |
| 37 | + decorations.push( |
| 38 | + Decoration.mark({ |
| 39 | + class: 'cm-diff-added' |
| 40 | + }).range(pos, pos + length) |
| 41 | + ); |
| 42 | + pos += length; // Move the position for added text |
| 43 | + } else if (diff.removed) { |
| 44 | + // Keep removed text and display it inline as strikethrough |
| 45 | + decorations.push( |
| 46 | + Decoration.widget({ |
| 47 | + widget: new RemovedTextWidget(diff.value), |
| 48 | + side: -1 |
| 49 | + }).range(pos, pos) // Insert before current position |
| 50 | + ); |
| 51 | + } else { |
| 52 | + pos += length; // Unchanged text |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + this.decorations = Decoration.set(decorations); |
| 57 | + } |
| 58 | + |
| 59 | + destroy() { |
| 60 | + // TODO |
| 61 | + } |
| 62 | + |
| 63 | + decorations: DecorationSet; |
| 64 | + private _originalText: string; |
| 65 | + private _view: EditorView; |
| 66 | +} |
| 67 | +/** |
| 68 | + * Widget to show removed text |
| 69 | + * |
| 70 | + * @class RemovedTextWidget |
| 71 | + * @extends {WidgetType} |
| 72 | + */ |
| 73 | +class RemovedTextWidget extends WidgetType { |
| 74 | + constructor(text: string) { |
| 75 | + super(); |
| 76 | + this._text = text; |
| 77 | + } |
| 78 | + |
| 79 | + get text(): string { |
| 80 | + return this._text; |
| 81 | + } |
| 82 | + |
| 83 | + toDOM() { |
| 84 | + const span = document.createElement('span'); |
| 85 | + span.textContent = this._text; |
| 86 | + span.className = 'cm-diff-removed'; |
| 87 | + return span; |
| 88 | + } |
| 89 | + |
| 90 | + eq(other: WidgetType) { |
| 91 | + return other instanceof RemovedTextWidget && other.text === this.text; |
| 92 | + } |
| 93 | + |
| 94 | + updateDOM() { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + private _text: string; |
| 99 | +} |
| 100 | + |
| 101 | +export const highlightTextExtension = [ |
| 102 | + ViewPlugin.fromClass(HighlightDiff, { |
| 103 | + decorations: (v: HighlightDiff) => v.decorations |
| 104 | + }) |
| 105 | +]; |
0 commit comments