-
Notifications
You must be signed in to change notification settings - Fork 156
/
diff.ts
269 lines (234 loc) · 6.46 KB
/
diff.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
'use strict';
import type * as nbformat from '@jupyterlab/nbformat';
import { Panel, Widget } from '@lumino/widgets';
import { RenderMimeRegistry } from '@jupyterlab/rendermime';
import { Sanitizer } from '@jupyterlab/apputils';
import { MathJaxTypesetter } from '@jupyterlab/mathjax-extension';
import { createEditorFactory } from 'nbdime/lib/common/editor';
import type { IDiffEntry } from 'nbdime/lib/diff/diffentries';
import { NotebookDiffModel } from 'nbdime/lib/diff/model';
import { NotebookDiffWidget } from 'nbdime/lib/diff/widget';
import { requestDiff } from 'nbdime/lib/request';
import {
getBaseUrl,
getConfigOption,
toggleSpinner,
toggleShowUnchanged,
markUnchangedRanges,
} from './common';
import { exportDiff } from './staticdiff';
import { rendererFactories } from './rendermime';
let diffWidget: NotebookDiffWidget | null = null;
const prefixes = ['git:', 'checkpoint:'];
function hasPrefix(candidate: string): boolean {
for (let p of prefixes) {
if (candidate.slice(0, p.length) === p) {
return true;
}
}
return false;
}
function stripPrefix(s: string): string {
for (let p of prefixes) {
if (s.slice(0, p.length) === p) {
return s.slice(p.length);
}
}
return s;
}
/**
* Show the diff as represented by the base notebook and a list of diff entries
*/
function showDiff(data: {
base: nbformat.INotebookContent;
diff: IDiffEntry[];
}): Promise<void> {
let rendermime = new RenderMimeRegistry({
initialFactories: rendererFactories,
sanitizer: new Sanitizer(),
latexTypesetter: new MathJaxTypesetter(),
});
const collapseIdentical = getConfigOption('collapseIdentical', 2);
let nbdModel = new NotebookDiffModel(data.base, data.diff);
let nbdWidget = new NotebookDiffWidget({
model: nbdModel,
rendermime,
editorFactory: createEditorFactory(),
collapseIdentical,
});
let root = document.getElementById('nbdime-root');
if (!root) {
throw new Error('Missing root element "nbidme-root"');
}
root.innerHTML = '';
// Hide unchanged cells by default:
toggleShowUnchanged(!getConfigOption('hideUnchanged', true));
let panel = new Panel();
panel.id = 'main';
Widget.attach(panel, root);
panel.addWidget(nbdWidget);
let work = nbdWidget.init();
work.then(() => {
window.onresize = () => {
panel.update();
};
});
diffWidget = nbdWidget;
return work;
}
/**
* Diff form submission callback. Sends a request for a diff to the server based
* on the content of the form.
*/
function onDiff(e: Event) {
e.preventDefault();
let b = (document.getElementById('diff-base') as HTMLInputElement).value;
let r = (document.getElementById('diff-remote') as HTMLInputElement).value;
compare(b, r, true);
return false;
}
function compare(
base: string,
remote: string | undefined,
pushHistory: boolean | 'replace',
) {
toggleSpinner(true);
getDiff(base, remote);
if (pushHistory) {
let uri = window.location.pathname;
base = stripPrefix(base);
uri = '?base=' + encodeURIComponent(base);
if (remote) {
uri += '&remote=' + encodeURIComponent(remote);
}
editHistory(
pushHistory,
{ base, remote },
'Diff: "' + base + '" vs "' + remote + '"',
uri,
);
}
}
function editHistory(
pushHistory: boolean | 'replace',
statedata: any,
title: string,
url?: string,
): void {
if (pushHistory === true) {
history.pushState(statedata, title, url);
} else if (pushHistory === 'replace') {
history.replaceState(statedata, title, url);
}
}
/**
* Calls `requestDiff` with our response handlers
*/
export function getDiff(base: string, remote: string | undefined) {
let baseUrl = getBaseUrl();
requestDiff(
base,
remote,
baseUrl,
onDiffRequestCompleted,
onDiffRequestFailed,
);
}
/**
* Callback for a successfull diff request
*/
function onDiffRequestCompleted(data: any) {
let layoutWork = showDiff(data);
layoutWork.then(() => {
let exportBtn = document.getElementById(
'nbdime-export',
) as HTMLButtonElement;
exportBtn.style.display = 'initial';
toggleSpinner(false);
markUnchangedRanges();
});
}
/**
* Callback for a failed diff request
*/
function onDiffRequestFailed(response: string) {
console.log('Diff request failed.');
const root = document.getElementById('nbdime-root');
if (!root) {
throw new Error('Missing root element "nbidme-root"');
}
const pre = document.createElement('pre');
pre.innerText = response;
root.innerHTML = '';
root.appendChild(pre);
diffWidget = null;
toggleSpinner(false);
}
/**
* Called when a 'back' is requested
*/
function onPopState(e: PopStateEvent) {
if (e.state) {
let eb = document.getElementById('diff-base') as HTMLInputElement;
let er = document.getElementById('diff-remote') as HTMLInputElement;
eb.value = e.state.base;
er.value = e.state.remote;
compare(e.state.base, e.state.remote, false);
}
}
/**
* Trust all outputs in diff
*/
function trustOutputs() {
let trust = true;
if (!diffWidget) {
return;
}
let model = diffWidget.model;
for (let cell of model.cells) {
if (!cell.outputs) {
continue;
}
for (let output of cell.outputs) {
output.trusted = trust;
}
}
}
/**
* Wire up callbacks.
*/
function attachToForm() {
let frm = document.getElementById('nbdime-diff-form') as HTMLFormElement;
if (frm) {
frm.onsubmit = onDiff;
// It only makes sense to listen to pop state events when the form is
// availalbe (i.e. when we are not a diff/mergetool):
window.onpopstate = onPopState;
}
}
/**
*
*/
export function initializeDiff() {
attachToForm();
// If arguments supplied in config, run diff directly:
let base = getConfigOption('base');
let remote = getConfigOption('remote');
if (base && (remote || hasPrefix(base))) {
compare(base, remote, 'replace');
}
let exportBtn = document.getElementById('nbdime-export') as HTMLButtonElement;
exportBtn.onclick = exportDiff;
let hideUnchangedChk = document.getElementById(
'nbdime-hide-unchanged',
) as HTMLInputElement;
hideUnchangedChk.checked = getConfigOption('hideUnchanged', true);
hideUnchangedChk.onchange = () => {
toggleShowUnchanged(!hideUnchangedChk.checked, diffWidget);
};
let trustBtn = document.getElementById('nbdime-trust') as HTMLButtonElement;
trustBtn.onclick = trustOutputs;
trustBtn.style.display = 'initial';
}