forked from livebook-dev/livebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.js
More file actions
437 lines (366 loc) · 11.9 KB
/
Copy pathcell.js
File metadata and controls
437 lines (366 loc) · 11.9 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import { getAttributeOrDefault, getAttributeOrThrow } from "../lib/attribute";
import Markdown from "../lib/markdown";
import { globalPubSub } from "../lib/pub_sub";
import {
md5Base64,
pushQueueEvaluationEvent,
smoothlyScrollToElement,
} from "../lib/utils";
import scrollIntoView from "scroll-into-view-if-needed";
import { isEvaluable } from "../lib/notebook";
/**
* A hook managing a single cell.
*
* Manages the collaborative editor, takes care of markdown rendering
* and focusing the editor when applicable.
*
* ## Configuration
*
* * `data-cell-id` - id of the cell being edited
*
* * `data-type` - type of the cell
*
* * `data-session-path` - root path to the current session
*
* * `data-evaluation-digest` - digest of the last evaluated cell source
*/
const Cell = {
mounted() {
this.props = this.getProps();
this.isFocused = false;
this.insertMode = false;
this.liveEditors = {};
this.updateInsertModeAvailability();
// Setup action handlers
if (["code", "smart"].includes(this.props.type)) {
const amplifyButton = this.el.querySelector(
`[data-el-amplify-outputs-button]`
);
amplifyButton.addEventListener("click", (event) => {
this.el.toggleAttribute("data-js-amplified");
});
}
if (this.props.type === "smart") {
const toggleSourceButton = this.el.querySelector(
`[data-el-toggle-source-button]`
);
toggleSourceButton.addEventListener("click", (event) => {
this.el.toggleAttribute("data-js-source-visible");
this.updateInsertModeAvailability();
this.maybeFocusCurrentEditor();
});
}
// Setup listeners
this.el.addEventListener("lb:cell:editor_created", (event) => {
const { tag, liveEditor } = event.detail;
this.handleCellEditorCreated(tag, liveEditor);
});
this.el.addEventListener("lb:cell:editor_removed", (event) => {
const { tag } = event.detail;
this.handleCellEditorRemoved(tag);
});
// We manually track hover to correctly handle absolute iframe
this.el.addEventListener("mouseenter", (event) => {
this.el.setAttribute("data-js-hover", "");
});
this.el.addEventListener("mouseleave", (event) => {
this.el.removeAttribute("data-js-hover");
});
this.unsubscribeFromNavigationEvents = globalPubSub.subscribe(
"navigation",
(event) => this.handleNavigationEvent(event)
);
this.unsubscribeFromCellsEvents = globalPubSub.subscribe("cells", (event) =>
this.handleCellsEvent(event)
);
this.unsubscribeFromCellEvents = globalPubSub.subscribe(
`cells:${this.props.cellId}`,
(event) => this.handleCellEvent(event)
);
// DOM events
this._handleViewportResize = this.handleViewportResize.bind(this);
window.visualViewport.addEventListener(
"resize",
this._handleViewportResize
);
},
disconnected() {
// Reinitialize on reconnection
this.el.removeAttribute("id");
},
destroyed() {
this.unsubscribeFromNavigationEvents();
this.unsubscribeFromCellsEvents();
this.unsubscribeFromCellEvents();
window.visualViewport.removeEventListener(
"resize",
this._handleViewportResize
);
},
updated() {
const prevProps = this.props;
this.props = this.getProps();
if (this.props.evaluationDigest !== prevProps.evaluationDigest) {
this.updateChangeIndicator();
}
},
getProps() {
return {
cellId: getAttributeOrThrow(this.el, "data-cell-id"),
type: getAttributeOrThrow(this.el, "data-type"),
sessionPath: getAttributeOrThrow(this.el, "data-session-path"),
evaluationDigest: getAttributeOrDefault(
this.el,
"data-evaluation-digest",
null
),
smartCellJSViewRef: getAttributeOrDefault(
this.el,
"data-smart-cell-js-view-ref",
null
),
allowedUriSchemes: getAttributeOrThrow(
this.el,
"data-allowed-uri-schemes"
),
};
},
handleNavigationEvent(event) {
if (event.type === "element_focused") {
this.handleElementFocused(event.focusableId, event.scroll);
} else if (event.type === "insert_mode_changed") {
this.handleInsertModeChanged(event.enabled);
} else if (event.type === "location_report") {
this.handleLocationReport(event.client, event.report);
}
},
handleCellsEvent(event) {
if (event.type === "cell_moved") {
this.handleCellMoved(event.cellId);
} else if (event.type === "cell_upload") {
this.handleCellUpload(event.cellId, event.url);
}
},
handleCellEvent(event) {
if (event.type === "dispatch_queue_evaluation") {
this.handleDispatchQueueEvaluation(event.event);
}
},
handleElementFocused(focusableId, scroll) {
if (this.props.cellId === focusableId) {
this.isFocused = true;
this.el.setAttribute("data-js-focused", "");
if (scroll) {
smoothlyScrollToElement(this.el);
}
} else if (this.isFocused) {
this.isFocused = false;
this.el.removeAttribute("data-js-focused");
}
},
handleCellEditorCreated(tag, liveEditor) {
this.liveEditors[tag] = liveEditor;
this.updateInsertModeAvailability();
if (this.props.type !== "markdown") {
// For markdown cells the editor is mounted lazily when needed,
// for other cells we mount the editor eagerly, however mounting
// is a synchronous operation and is relatively expensive, so we
// defer it to run after the current event handlers
setTimeout(() => {
if (!liveEditor.isMounted()) {
liveEditor.mount();
}
}, 0);
}
if (liveEditor === this.currentEditor()) {
// Once the editor is created, reflect the current insert mode state
this.maybeFocusCurrentEditor(true);
}
liveEditor.onBlur(() => {
// Prevent from blurring unless the state changes. For example
// when we move cell using buttons the editor should keep focus
if (this.isFocused && this.insertMode) {
this.currentEditor().focus();
}
});
liveEditor.onCursorSelectionChange((selection) => {
this.broadcastSelection(selection);
});
if (tag === "primary") {
const source = liveEditor.getSource();
this.el.toggleAttribute("data-js-empty", source === "");
liveEditor.onChange((newSource) => {
this.el.toggleAttribute("data-js-empty", newSource === "");
});
// Setup markdown rendering
if (this.props.type === "markdown") {
const markdownContainer = this.el.querySelector(
`[data-el-markdown-container]`
);
const markdown = new Markdown(markdownContainer, source, {
baseUrl: this.props.sessionPath,
emptyText: "Empty markdown cell",
allowedUriSchemes: this.props.allowedUriSchemes.split(","),
});
liveEditor.onChange((newSource) => {
markdown.setContent(newSource);
});
}
// Setup change indicator
if (isEvaluable(this.props.type)) {
this.updateChangeIndicator();
liveEditor.onChange((newSource) => {
this.updateChangeIndicator();
});
this.handleEvent(
`evaluation_finished:${this.props.cellId}`,
({ code_markers }) => {
liveEditor.setCodeMarkers(code_markers);
}
);
this.handleEvent(`start_evaluation:${this.props.cellId}`, () => {
liveEditor.clearDoctests();
});
this.handleEvent(
`doctest_report:${this.props.cellId}`,
(doctestReport) => {
liveEditor.updateDoctest(doctestReport);
}
);
this.handleEvent(`erase_outputs`, () => {
liveEditor.setCodeMarkers([]);
liveEditor.clearDoctests();
});
}
}
},
handleCellEditorRemoved(tag) {
delete this.liveEditors[tag];
},
handleViewportResize() {
if (this.isFocused) {
this.scrollActiveElementIntoView();
}
},
currentEditor() {
return this.liveEditors[this.currentEditorTag()];
},
currentEditorTag() {
if (this.props.type === "smart") {
const isSourceTab = this.el.hasAttribute("data-js-source-visible");
return isSourceTab ? "primary" : "secondary";
}
return "primary";
},
updateInsertModeAvailability() {
this.el.toggleAttribute(
"data-js-insert-mode-disabled",
!this.currentEditor()
);
},
maybeFocusCurrentEditor(scroll = false) {
if (this.isFocused && this.insertMode) {
this.currentEditor().focus();
if (scroll) {
// If the element is being scrolled to, focus interrupts it,
// so ensure the scrolling continues.
smoothlyScrollToElement(this.el);
}
this.broadcastSelection();
}
},
updateChangeIndicator() {
const cellStatus = this.el.querySelector(`[data-el-cell-status]`);
const indicator =
cellStatus && cellStatus.querySelector(`[data-el-change-indicator]`);
if (indicator && this.props.evaluationDigest) {
const source = this.liveEditors.primary.getSource();
const digest = md5Base64(source);
const changed = this.props.evaluationDigest !== digest;
this.el.toggleAttribute("data-js-changed", changed);
}
},
handleInsertModeChanged(insertMode) {
if (this.isFocused && !this.insertMode && insertMode) {
this.insertMode = insertMode;
if (this.currentEditor()) {
this.currentEditor().focus();
// The insert mode may be enabled as a result of clicking the editor,
// in which case we want to wait until editor handles the click and
// sets new cursor position. To achieve this, we simply put this task
// at the end of event loop, ensuring the editor mousedown handler is
// executed first
setTimeout(this.scrollActiveElementIntoView.bind(this), 0);
this.broadcastSelection();
}
} else if (this.insertMode && !insertMode) {
this.insertMode = insertMode;
if (this.currentEditor()) {
this.currentEditor().blur();
}
}
},
handleCellMoved(cellId) {
if (this.isFocused && cellId === this.props.cellId) {
smoothlyScrollToElement(this.el);
}
},
handleCellUpload(cellId, url) {
const liveEditor = this.liveEditors.primary;
if (!liveEditor) {
return;
}
if (this.props.cellId === cellId) {
const markdown = ``;
liveEditor.insert(markdown);
}
},
handleDispatchQueueEvaluation(event) {
if (this.props.type === "smart" && this.props.smartCellJSViewRef) {
// Ensure the smart cell UI is reflected on the server, before the evaluation
globalPubSub.broadcast(`js_views:${this.props.smartCellJSViewRef}`, {
type: "sync",
callback: event,
});
} else {
pushQueueEvaluationEvent(this, event);
}
},
handleLocationReport(client, report) {
Object.entries(this.liveEditors).forEach(([tag, liveEditor]) => {
if (
this.props.cellId === report.focusableId &&
report.selection &&
report.selection.tag === tag
) {
liveEditor.updateUserSelection(
client,
report.selection.editorSelection
);
} else {
liveEditor.removeUserSelection(client);
}
});
},
broadcastSelection(editorSelection = null) {
editorSelection =
editorSelection || this.currentEditor().editor.getSelection();
const tag = this.currentEditorTag();
// Report new selection only if this cell is in insert mode
if (this.isFocused && this.insertMode) {
globalPubSub.broadcast("session", {
type: "cursor_selection_changed",
focusableId: this.props.cellId,
selection: { tag, editorSelection },
});
}
},
scrollActiveElementIntoView() {
scrollIntoView(document.activeElement, {
scrollMode: "if-needed",
behavior: "smooth",
block: "center",
});
},
};
export default Cell;