|
| 1 | +import { python } from '@codemirror/lang-python'; |
| 2 | +import { MergeView } from '@codemirror/merge'; |
| 3 | +import { EditorView } from '@codemirror/view'; |
| 4 | +import { ICellModel } from '@jupyterlab/cells'; |
| 5 | +import { jupyterTheme } from '@jupyterlab/codemirror'; |
| 6 | +import { Message } from '@lumino/messaging'; |
| 7 | +import { Widget } from '@lumino/widgets'; |
| 8 | +import { basicSetup } from 'codemirror'; |
| 9 | +import { ICellFooterTracker } from 'jupyterlab-cell-input-footer'; |
| 10 | +import { IDiffStrategy, IDiffWidgetOptions } from '../interfaces'; |
| 11 | +import { BaseDiffWidget } from '../widget'; |
| 12 | + |
| 13 | +/** |
| 14 | + * CodeMirror-based diff strategy that performs client-side diffing |
| 15 | + */ |
| 16 | +export class CodeMirrorStrategy implements IDiffStrategy { |
| 17 | + readonly id = 'codemirror'; |
| 18 | + readonly name = 'CodeMirror (Client-side)'; |
| 19 | + |
| 20 | + /** |
| 21 | + * Create a diff widget using CodeMirror merge view |
| 22 | + */ |
| 23 | + async createDiffWidget(options: IDiffWidgetOptions): Promise<Widget> { |
| 24 | + const { |
| 25 | + cell, |
| 26 | + cellFooterTracker, |
| 27 | + originalSource, |
| 28 | + newSource, |
| 29 | + showActionButtons = true, |
| 30 | + openDiff = true |
| 31 | + } = options; |
| 32 | + |
| 33 | + const modifiedCode = newSource || cell.sharedModel.getSource(); |
| 34 | + |
| 35 | + const diffWidget = new CodeMirrorDiffWidget({ |
| 36 | + originalCode: originalSource, |
| 37 | + modifiedCode, |
| 38 | + cell, |
| 39 | + cellFooterTracker, |
| 40 | + showActionButtons, |
| 41 | + openDiff |
| 42 | + }); |
| 43 | + |
| 44 | + diffWidget.addClass('jupyterlab-cell-diff'); |
| 45 | + diffWidget.addToFooter(); |
| 46 | + |
| 47 | + return diffWidget; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * A Lumino widget that contains a CodeMirror diff view |
| 53 | + */ |
| 54 | +class CodeMirrorDiffWidget extends BaseDiffWidget { |
| 55 | + private _originalCode: string; |
| 56 | + private _modifiedCode: string; |
| 57 | + private _mergeView: MergeView | null = null; |
| 58 | + |
| 59 | + /** |
| 60 | + * Construct a new CodeMirrorDiffWidget. |
| 61 | + */ |
| 62 | + constructor(options: CodeMirrorDiffWidget.IOptions) { |
| 63 | + super({ |
| 64 | + cell: options.cell, |
| 65 | + cellFooterTracker: options.cellFooterTracker, |
| 66 | + originalSource: options.originalCode, |
| 67 | + newSource: options.modifiedCode, |
| 68 | + showActionButtons: options.showActionButtons, |
| 69 | + openDiff: options.openDiff |
| 70 | + }); |
| 71 | + this._originalCode = options.originalCode; |
| 72 | + this._modifiedCode = options.modifiedCode; |
| 73 | + this.addClass('jp-DiffView'); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Handle after-attach messages for the widget. |
| 78 | + */ |
| 79 | + protected onAfterAttach(msg: Message): void { |
| 80 | + super.onAfterAttach(msg); |
| 81 | + this._createMergeView(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Handle before-detach messages for the widget. |
| 86 | + */ |
| 87 | + protected onBeforeDetach(msg: Message): void { |
| 88 | + this._destroyMergeView(); |
| 89 | + super.onBeforeDetach(msg); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Create the merge view with CodeMirror diff functionality. |
| 94 | + */ |
| 95 | + private _createMergeView(): void { |
| 96 | + if (this._mergeView) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + this._mergeView = new MergeView({ |
| 101 | + a: { |
| 102 | + doc: this._originalCode, |
| 103 | + extensions: [ |
| 104 | + basicSetup, |
| 105 | + python(), |
| 106 | + EditorView.editable.of(false), |
| 107 | + jupyterTheme |
| 108 | + ] |
| 109 | + }, |
| 110 | + b: { |
| 111 | + doc: this._modifiedCode, |
| 112 | + extensions: [ |
| 113 | + basicSetup, |
| 114 | + python(), |
| 115 | + EditorView.editable.of(false), |
| 116 | + jupyterTheme |
| 117 | + ] |
| 118 | + }, |
| 119 | + parent: this.node |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Destroy the merge view and clean up resources. |
| 125 | + */ |
| 126 | + private _destroyMergeView(): void { |
| 127 | + if (this._mergeView) { |
| 128 | + this._mergeView.destroy(); |
| 129 | + this._mergeView = null; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * A namespace for `CodeMirrorDiffWidget` statics. |
| 136 | + */ |
| 137 | +namespace CodeMirrorDiffWidget { |
| 138 | + /** |
| 139 | + * The options used to construct a `CodeMirrorDiffWidget`. |
| 140 | + */ |
| 141 | + export interface IOptions { |
| 142 | + originalCode: string; |
| 143 | + modifiedCode: string; |
| 144 | + cell: ICellModel; |
| 145 | + cellFooterTracker: ICellFooterTracker; |
| 146 | + showActionButtons?: boolean; |
| 147 | + openDiff?: boolean; |
| 148 | + } |
| 149 | +} |
0 commit comments