@eclipse-lyra/extension-notebook 0.7.56 → 0.7.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1043 @@
1
+ import { t as TARGET_NOTEBOOK_KERNELS } from "./notebook-kernel-api-c9l3_84P.js";
2
+ import { LyraPart, TOPIC_CONTRIBUTEIONS_CHANGED, contributionRegistry, createLogger, subscribe, unsubscribe } from "@eclipse-lyra/core";
3
+ import { css, html } from "lit";
4
+ import { customElement, property, state } from "lit/decorators.js";
5
+ import { createRef, ref } from "lit/directives/ref.js";
6
+ import { repeat } from "lit/directives/repeat.js";
7
+ import { styleMap } from "lit/directives/style-map.js";
8
+ import { unsafeHTML } from "lit/directives/unsafe-html.js";
9
+ import { marked } from "marked";
10
+ import _decorate from "@oxc-project/runtime/helpers/decorate";
11
+ //#region src/notebook-runtime.ts
12
+ var logger = createLogger("NotebookRuntime");
13
+ var LyraNotebookEditor = class LyraNotebookEditor extends LyraPart {
14
+ constructor(..._args) {
15
+ super(..._args);
16
+ this.cellOutputs = /* @__PURE__ */ new Map();
17
+ this.executingCells = /* @__PURE__ */ new Set();
18
+ this.availableKernels = [];
19
+ this.selectedKernelId = null;
20
+ this.kernel = null;
21
+ this.kernelConnected = false;
22
+ this.kernelConnecting = false;
23
+ this.kernelVersion = void 0;
24
+ this.editingMarkdownCells = /* @__PURE__ */ new Set();
25
+ this.executionCounter = 0;
26
+ this.isRunningAll = false;
27
+ this.highlightedCellIndex = -1;
28
+ this.focusedCellIndex = -1;
29
+ this.cancelRunAll = false;
30
+ this.cellWidgetRefs = /* @__PURE__ */ new Map();
31
+ this.cellHeights = /* @__PURE__ */ new Map();
32
+ }
33
+ doClose() {
34
+ this.input = void 0;
35
+ this.notebook = void 0;
36
+ this.cellOutputs.clear();
37
+ this.executingCells.clear();
38
+ this.cellWidgetRefs.clear();
39
+ this.cellHeights.clear();
40
+ this.focusedCellIndex = -1;
41
+ if (this.unsubscribeContributionsToken) {
42
+ unsubscribe(this.unsubscribeContributionsToken);
43
+ this.unsubscribeContributionsToken = void 0;
44
+ }
45
+ if (this.kernel?.close) Promise.resolve(this.kernel.close());
46
+ this.kernel = null;
47
+ this.kernelConnected = false;
48
+ this.kernelVersion = void 0;
49
+ }
50
+ async save() {
51
+ if (!this.notebook || !this.input) return;
52
+ try {
53
+ this.saveEditorContents();
54
+ this.notebook.cells.forEach((cell, index) => {
55
+ if (cell.cell_type === "code") {
56
+ const output = this.cellOutputs.get(index);
57
+ cell.outputs = output ? this.convertOutputToJupyter(output, cell.execution_count) : [];
58
+ }
59
+ });
60
+ const notebookJson = JSON.stringify(this.notebook, null, 2);
61
+ await this.input.data.saveContents(notebookJson);
62
+ this.markDirty(false);
63
+ } catch (error) {
64
+ console.error("Failed to save notebook:", error);
65
+ throw error;
66
+ }
67
+ }
68
+ doBeforeUI() {
69
+ this.loadNotebook();
70
+ }
71
+ async onKernelDropdownSelect(e) {
72
+ const nextId = (e.detail?.item?.value ?? "") || null;
73
+ if (this.selectedKernelId === nextId) return;
74
+ if (this.kernel?.close) Promise.resolve(this.kernel.close());
75
+ this.kernel = null;
76
+ this.kernelConnected = false;
77
+ this.kernelVersion = void 0;
78
+ this.selectedKernelId = nextId;
79
+ if (this.notebook) {
80
+ if (!this.notebook.metadata) this.notebook.metadata = {};
81
+ this.notebook.metadata.kernel = nextId ?? void 0;
82
+ }
83
+ this.cellOutputs.clear();
84
+ this.executionCounter = 0;
85
+ this.notebook?.cells?.forEach((cell) => {
86
+ if (cell.cell_type === "code") {
87
+ cell.execution_count = null;
88
+ cell.outputs = [];
89
+ }
90
+ });
91
+ this.resetCellState();
92
+ await this.ensureKernelLoaded();
93
+ this.requestUpdate();
94
+ }
95
+ renderToolbar() {
96
+ const kernels = this.availableKernels;
97
+ kernels.length;
98
+ const currentLabel = this.selectedKernelId ? kernels.find((c) => c.id === this.selectedKernelId)?.label ?? this.selectedKernelId : "Select kernel";
99
+ const connectionTitle = this.kernelConnecting ? "Connecting..." : this.kernelConnected ? "Kernel connected" : "Kernel disconnected";
100
+ const connectionText = this.kernelConnecting ? "Connecting..." : this.kernelConnected ? this.kernelVersion ?? "Connected" : "Not connected";
101
+ const iconColor = this.kernelConnected ? "var(--wa-color-green-40)" : this.kernelConnecting ? "var(--wa-color-warning-500)" : "var(--wa-color-red-40)";
102
+ const runAllButton = this.isRunningAll ? html`
103
+ <wa-button size="small" appearance="plain" @click=${() => this.cancelAllCells()} title="Cancel running all cells">
104
+ <wa-icon name="stop" label="Stop"></wa-icon>
105
+ Cancel All
106
+ </wa-button>
107
+ ` : html`
108
+ <wa-button size="small" appearance="plain" @click=${() => this.runAllCells()} title="Run all code cells sequentially">
109
+ <wa-icon name="play" label="Run"></wa-icon>
110
+ Run All
111
+ </wa-button>
112
+ `;
113
+ return html`
114
+ <wa-dropdown
115
+ class="kernel-select"
116
+ placement="bottom-start"
117
+ distance="4"
118
+ size="small"
119
+ @wa-select=${(e) => void this.onKernelDropdownSelect(e)}
120
+ >
121
+ <wa-button
122
+ slot="trigger"
123
+ appearance="plain"
124
+ size="small"
125
+ with-caret
126
+ title="Notebook kernel"
127
+ >
128
+ ${currentLabel}
129
+ </wa-button>
130
+ ${kernels.map((c) => html`
131
+ <wa-dropdown-item
132
+ value=${c.id}
133
+ type="checkbox"
134
+ ?checked=${c.id === this.selectedKernelId}
135
+ >
136
+ ${c.label}
137
+ </wa-dropdown-item>
138
+ `)}
139
+ </wa-dropdown>
140
+ ${runAllButton}
141
+ <wa-button
142
+ size="small"
143
+ appearance="plain"
144
+ @click=${() => this.clearAllOutputs()}
145
+ title="Clear all outputs and reset execution counter"
146
+ >
147
+ <wa-icon name="eraser" label="Clear"></wa-icon>
148
+ Clear Outputs
149
+ </wa-button>
150
+ ${this.kernel?.restart ? html`
151
+ <wa-button
152
+ size="small"
153
+ appearance="plain"
154
+ @click=${() => void this.restartKernel()}
155
+ title="Restart kernel"
156
+ ?disabled=${!this.kernelConnected || this.kernelConnecting}
157
+ >
158
+ <wa-icon name="arrows-rotate" label="Restart"></wa-icon>
159
+ Restart Kernel
160
+ </wa-button>
161
+ ` : ""}
162
+ ${this.kernel?.openPackageManager ? html`
163
+ <wa-button
164
+ size="small"
165
+ appearance="plain"
166
+ @click=${() => this.openPackageManager()}
167
+ title="Manage packages"
168
+ >
169
+ <wa-icon name="box" label="Packages"></wa-icon>
170
+ Packages
171
+ </wa-button>
172
+ ` : ""}
173
+ ${this.kernel ? this.kernel.connect ? html`
174
+ <wa-button
175
+ appearance="plain"
176
+ size="small"
177
+ style="display: flex; align-items: center; gap: 0.5rem;"
178
+ ?disabled=${this.kernelConnecting}
179
+ @click=${() => void this.connectKernel()}
180
+ title=${connectionTitle}
181
+ >
182
+ <wa-icon name="circle" label="Kernel status" style=${styleMap({ color: iconColor })}></wa-icon>
183
+ ${connectionText}
184
+ </wa-button>
185
+ ` : html`
186
+ <span style="display: flex; align-items: center; gap: 0.5rem;" title=${connectionTitle}>
187
+ <wa-icon name="circle" label="Kernel status" style=${styleMap({ color: iconColor })}></wa-icon>
188
+ ${connectionText}
189
+ </span>
190
+ ` : ""}
191
+ `;
192
+ }
193
+ async connectKernel() {
194
+ if (this.kernelConnecting || !this.kernel?.connect) return;
195
+ try {
196
+ this.kernelConnecting = true;
197
+ this.requestUpdate();
198
+ await this.kernel.connect({ requiredPackages: this.notebook?.metadata?.required_packages });
199
+ this.kernelConnected = true;
200
+ if (this.kernel.getVersion) this.kernelVersion = await this.kernel.getVersion();
201
+ } catch (err) {
202
+ logger.error("Failed to connect kernel", err);
203
+ } finally {
204
+ this.kernelConnecting = false;
205
+ this.requestUpdate();
206
+ }
207
+ }
208
+ async doInitUI() {
209
+ this.unsubscribeContributionsToken = subscribe(TOPIC_CONTRIBUTEIONS_CHANGED, (event) => {
210
+ if (event?.target === "system.notebookkernels") this.refreshKernels();
211
+ });
212
+ await this.refreshKernels();
213
+ }
214
+ resolveDefaultKernelId(contributions) {
215
+ if (!contributions.length) return null;
216
+ const fromMeta = this.notebook?.metadata?.kernel;
217
+ if (fromMeta && contributions.some((c) => c.id === fromMeta)) return fromMeta;
218
+ const python = contributions.find((c) => c.id === "python");
219
+ if (python) return python.id;
220
+ const js = contributions.find((c) => c.id === "javascript");
221
+ if (js) return js.id;
222
+ return contributions[0].id;
223
+ }
224
+ async refreshKernels() {
225
+ const contributions = contributionRegistry.getContributions(TARGET_NOTEBOOK_KERNELS);
226
+ this.availableKernels = contributions;
227
+ if (!this.selectedKernelId && contributions.length) {
228
+ this.selectedKernelId = this.resolveDefaultKernelId(contributions);
229
+ if (this.notebook && this.selectedKernelId) {
230
+ if (!this.notebook.metadata) this.notebook.metadata = {};
231
+ this.notebook.metadata.kernel = this.selectedKernelId;
232
+ }
233
+ }
234
+ if (this.selectedKernelId && !contributions.some((c) => c.id === this.selectedKernelId)) this.selectedKernelId = contributions.length ? contributions[0].id : null;
235
+ this.requestUpdate();
236
+ await this.ensureKernelLoaded();
237
+ }
238
+ async ensureKernelLoaded() {
239
+ const id = this.selectedKernelId;
240
+ if (!id || this.kernel?.id === id) return;
241
+ if (this.kernel?.close) Promise.resolve(this.kernel.close());
242
+ this.kernel = null;
243
+ this.kernelConnected = false;
244
+ this.kernelVersion = void 0;
245
+ const contribution = this.availableKernels.find((c) => c.id === id);
246
+ if (!contribution) return;
247
+ try {
248
+ this.kernelConnecting = true;
249
+ this.requestUpdate();
250
+ const k = await contribution.loadKernel();
251
+ if (this.selectedKernelId !== id) return;
252
+ this.kernel = k;
253
+ if (k.connect) await k.connect({ requiredPackages: this.notebook?.metadata?.required_packages });
254
+ this.kernelConnected = true;
255
+ if (k.getVersion) this.kernelVersion = await k.getVersion();
256
+ } catch (err) {
257
+ logger.error("Failed to load kernel", id, err);
258
+ } finally {
259
+ this.kernelConnecting = false;
260
+ this.requestUpdate();
261
+ }
262
+ }
263
+ async loadNotebook() {
264
+ const contents = await this.input.data.getContents();
265
+ try {
266
+ this.notebook = JSON.parse(contents);
267
+ } catch (e) {
268
+ console.error("Failed to parse notebook:", e);
269
+ this.notebook = { cells: [{
270
+ cell_type: "markdown",
271
+ source: ["# Error\nFailed to parse notebook file."]
272
+ }] };
273
+ }
274
+ if (this.notebook?.metadata?.kernel) this.selectedKernelId = this.notebook.metadata.kernel;
275
+ if (this.notebook?.cells) {
276
+ this.executionCounter = this.notebook.cells.filter((cell) => cell.cell_type === "code").map((cell) => cell.execution_count ?? 0).reduce((max, count) => Math.max(max, count), 0);
277
+ this.notebook.cells.forEach((cell, index) => {
278
+ if (cell.cell_type === "code" && cell.outputs && cell.outputs.length > 0) {
279
+ const output = this.convertOutputFromJupyter(cell.outputs[0]);
280
+ if (output) this.cellOutputs.set(index, output);
281
+ }
282
+ });
283
+ }
284
+ this.refreshKernels();
285
+ }
286
+ getCellSource(cell) {
287
+ return Array.isArray(cell.source) ? cell.source.join("") : cell.source;
288
+ }
289
+ convertOutputToJupyter(output, executionCount) {
290
+ if (output.type === "execute_result") {
291
+ const data = {};
292
+ if (output.imageData) data["image/png"] = output.imageData;
293
+ if (output.data) data["text/plain"] = output.data;
294
+ return [{
295
+ output_type: "execute_result",
296
+ data,
297
+ execution_count: executionCount,
298
+ metadata: {}
299
+ }];
300
+ } else if (output.type === "error") return [{
301
+ output_type: "error",
302
+ ename: "Error",
303
+ evalue: output.data,
304
+ traceback: [output.data]
305
+ }];
306
+ return [];
307
+ }
308
+ convertOutputFromJupyter(jupyterOutput) {
309
+ if (jupyterOutput.output_type === "execute_result" && jupyterOutput.data) return {
310
+ type: "execute_result",
311
+ data: jupyterOutput.data["text/plain"] || "",
312
+ imageData: jupyterOutput.data["image/png"] || void 0
313
+ };
314
+ else if (jupyterOutput.output_type === "error") return {
315
+ type: "error",
316
+ data: jupyterOutput.evalue || jupyterOutput.traceback?.join("\n") || "Unknown error"
317
+ };
318
+ return null;
319
+ }
320
+ renderHeaderActions(index, additionalButton) {
321
+ return html`
322
+ <div class="cell-header-actions">
323
+ ${additionalButton || ""}
324
+ ${additionalButton ? html`<span class="divider"></span>` : ""}
325
+ <wa-button size="small" appearance="plain" @click=${() => this.addCell(index, "code")} title="Add code cell before">
326
+ <wa-icon name="plus"></wa-icon>
327
+ <wa-icon name="code" label="Code"></wa-icon>
328
+ </wa-button>
329
+ <wa-button size="small" appearance="plain" @click=${() => this.addCell(index, "markdown")} title="Add markdown cell before">
330
+ <wa-icon name="plus"></wa-icon>
331
+ <wa-icon name="font" label="Markdown"></wa-icon>
332
+ </wa-button>
333
+ <span class="divider"></span>
334
+ <wa-button size="small" appearance="plain" @click=${() => this.deleteCell(index)} title="Delete cell" ?disabled=${this.notebook.cells.length <= 1}>
335
+ <wa-icon name="trash" label="Delete cell"></wa-icon>
336
+ </wa-button>
337
+ </div>
338
+ `;
339
+ }
340
+ renderFooterActions(index) {
341
+ return html`
342
+ <div class="cell-footer">
343
+ <wa-button size="small" appearance="plain" @click=${() => this.addCell(index + 1, "code")} title="Add code cell after">
344
+ <wa-icon name="code" label="Code"></wa-icon>
345
+ <wa-icon name="plus"></wa-icon>
346
+ </wa-button>
347
+ <wa-button size="small" appearance="plain" @click=${() => this.addCell(index + 1, "markdown")} title="Add markdown cell after">
348
+ <wa-icon name="font" label="Markdown"></wa-icon>
349
+ <wa-icon name="plus"></wa-icon>
350
+ </wa-button>
351
+ </div>
352
+ `;
353
+ }
354
+ stringToSourceArray(content) {
355
+ if (!content) return [""];
356
+ const lines = content.split("\n").map((line) => line + "\n");
357
+ if (lines.length > 0) lines[lines.length - 1] = lines[lines.length - 1].replace(/\n$/, "");
358
+ return lines;
359
+ }
360
+ createCell(cellType) {
361
+ const newCell = {
362
+ cell_type: cellType,
363
+ source: [""],
364
+ metadata: {}
365
+ };
366
+ if (cellType === "code") {
367
+ newCell.execution_count = null;
368
+ newCell.outputs = [];
369
+ }
370
+ return newCell;
371
+ }
372
+ async executeCell(cellIndex) {
373
+ const cell = this.notebook.cells[cellIndex];
374
+ if (cell.cell_type !== "code") return;
375
+ this.executingCells.add(cellIndex);
376
+ this.requestUpdate();
377
+ try {
378
+ await this.ensureKernelLoaded();
379
+ const k = this.kernel;
380
+ if (!k) {
381
+ if (this.executingCells.has(cellIndex)) this.cellOutputs.set(cellIndex, {
382
+ type: "error",
383
+ data: "No kernel selected"
384
+ });
385
+ return;
386
+ }
387
+ const widget = this.getCellWidgetRef(cellIndex).value;
388
+ const code = widget ? widget.getContent() : this.getCellSource(cell);
389
+ if (code === null || code === void 0) return;
390
+ const result = await k.execute(code);
391
+ if (result.error) this.cellOutputs.set(cellIndex, {
392
+ type: "error",
393
+ data: result.error
394
+ });
395
+ else this.cellOutputs.set(cellIndex, {
396
+ type: "execute_result",
397
+ data: result.data,
398
+ imageData: result.imageData
399
+ });
400
+ this.executionCounter++;
401
+ cell.execution_count = this.executionCounter;
402
+ this.markDirty(true);
403
+ } catch (error) {
404
+ if (this.executingCells.has(cellIndex)) this.cellOutputs.set(cellIndex, {
405
+ type: "error",
406
+ data: error instanceof Error ? error.message : String(error)
407
+ });
408
+ } finally {
409
+ this.executingCells.delete(cellIndex);
410
+ this.requestUpdate();
411
+ }
412
+ }
413
+ cancelExecution(cellIndex) {
414
+ if (this.kernel?.interrupt) this.kernel.interrupt();
415
+ else {
416
+ this.cellOutputs.set(cellIndex, {
417
+ type: "error",
418
+ data: "Cancellation not supported for this kernel"
419
+ });
420
+ this.executingCells.delete(cellIndex);
421
+ this.requestUpdate();
422
+ }
423
+ }
424
+ clearAllOutputs() {
425
+ this.cellOutputs.clear();
426
+ this.executionCounter = 0;
427
+ if (this.notebook?.cells) this.notebook.cells.forEach((cell) => {
428
+ if (cell.cell_type === "code") {
429
+ cell.execution_count = null;
430
+ cell.outputs = [];
431
+ }
432
+ });
433
+ this.markDirty(true);
434
+ this.requestUpdate();
435
+ }
436
+ async restartKernel() {
437
+ if (!this.kernel?.restart || this.kernelConnecting) return;
438
+ try {
439
+ this.kernelConnecting = true;
440
+ this.requestUpdate();
441
+ await this.kernel.restart();
442
+ this.kernelConnected = true;
443
+ if (this.kernel.getVersion) this.kernelVersion = await this.kernel.getVersion();
444
+ } catch (error) {
445
+ logger.error("Failed to restart kernel", error);
446
+ } finally {
447
+ this.kernelConnecting = false;
448
+ this.requestUpdate();
449
+ }
450
+ }
451
+ async runAllCells() {
452
+ if (!this.notebook?.cells) return;
453
+ this.isRunningAll = true;
454
+ this.cancelRunAll = false;
455
+ this.requestUpdate();
456
+ try {
457
+ for (let i = 0; i < this.notebook.cells.length; i++) {
458
+ if (this.cancelRunAll) break;
459
+ if (this.notebook.cells[i].cell_type === "code") await this.executeCell(i);
460
+ }
461
+ } finally {
462
+ this.isRunningAll = false;
463
+ this.cancelRunAll = false;
464
+ this.requestUpdate();
465
+ }
466
+ }
467
+ cancelAllCells() {
468
+ this.cancelRunAll = true;
469
+ this.kernel?.interrupt?.();
470
+ }
471
+ toggleMarkdownEdit(index) {
472
+ if (this.editingMarkdownCells.has(index)) this.editingMarkdownCells.delete(index);
473
+ else this.editingMarkdownCells.add(index);
474
+ this.requestUpdate();
475
+ }
476
+ saveMarkdownEdit(index, event) {
477
+ const newContent = event.target.value;
478
+ if (this.notebook && this.notebook.cells[index]) {
479
+ const cell = this.notebook.cells[index];
480
+ const oldContent = this.getCellSource(cell);
481
+ cell.source = this.stringToSourceArray(newContent);
482
+ if (oldContent !== newContent) this.markDirty(true);
483
+ }
484
+ this.editingMarkdownCells.delete(index);
485
+ this.requestUpdate();
486
+ }
487
+ renderMarkdownCell(cell, index) {
488
+ const source = this.getCellSource(cell);
489
+ const isEmpty = !source || source.trim() === "";
490
+ if (this.editingMarkdownCells.has(index)) {
491
+ const editButtons = html`
492
+ <wa-button
493
+ size="small"
494
+ appearance="plain"
495
+ @click=${(e) => {
496
+ const textarea = e.target.closest(".markdown-cell")?.querySelector("textarea");
497
+ if (textarea) this.saveMarkdownEdit(index, { target: textarea });
498
+ }}
499
+ title="Save changes">
500
+ <wa-icon name="check" label="Save"></wa-icon>
501
+ </wa-button>
502
+ <wa-button
503
+ size="small"
504
+ appearance="plain"
505
+ @click=${() => this.toggleMarkdownEdit(index)}
506
+ title="Cancel editing">
507
+ <wa-icon name="xmark" label="Cancel"></wa-icon>
508
+ </wa-button>
509
+ `;
510
+ return html`
511
+ <div class="cell-wrapper">
512
+ <wa-animation
513
+ name="bounce"
514
+ duration="1000"
515
+ iterations="1"
516
+ ?play=${this.highlightedCellIndex === index}
517
+ @wa-finish=${() => this.highlightedCellIndex = -1}>
518
+ <div class="cell markdown-cell editing">
519
+ <div class="cell-header">
520
+ ${this.renderHeaderActions(index, editButtons)}
521
+ <span class="cell-label">Markdown</span>
522
+ </div>
523
+ <textarea
524
+ class="markdown-editor"
525
+ .value=${source}
526
+ @blur=${(e) => this.saveMarkdownEdit(index, e)}
527
+ placeholder="Enter markdown content here... (# for headings, ** for bold, etc.)"></textarea>
528
+ ${this.renderFooterActions(index)}
529
+ </div>
530
+ </wa-animation>
531
+ </div>
532
+ `;
533
+ }
534
+ const rendered = marked.parse(source);
535
+ const editButton = html`
536
+ <wa-button
537
+ size="small"
538
+ appearance="plain"
539
+ @click=${() => this.toggleMarkdownEdit(index)}
540
+ title="Edit markdown">
541
+ <wa-icon name="pencil" label="Edit"></wa-icon>
542
+ </wa-button>
543
+ `;
544
+ return html`
545
+ <div class="cell-wrapper">
546
+ <wa-animation
547
+ name="bounce"
548
+ duration="1000"
549
+ iterations="1"
550
+ ?play=${this.highlightedCellIndex === index}
551
+ @wa-finish=${() => this.highlightedCellIndex = -1}>
552
+ <div class="cell markdown-cell ${isEmpty ? "empty" : ""}" @dblclick=${() => this.toggleMarkdownEdit(index)}>
553
+ <div class="cell-header">
554
+ ${this.renderHeaderActions(index, editButton)}
555
+ <span class="cell-label"></span>
556
+ </div>
557
+ <div class="cell-content">
558
+ ${isEmpty ? html`
559
+ <div class="markdown-placeholder">
560
+ <wa-icon name="font" label="Markdown"></wa-icon>
561
+ <span>Double-click or click the pencil icon to edit markdown</span>
562
+ </div>
563
+ ` : unsafeHTML(rendered)}
564
+ </div>
565
+ ${this.renderFooterActions(index)}
566
+ </div>
567
+ </wa-animation>
568
+ </div>
569
+ `;
570
+ }
571
+ renderCodeCell(cell, index) {
572
+ const output = this.cellOutputs.get(index);
573
+ const isExecuting = this.executingCells.has(index);
574
+ const source = this.getCellSource(cell);
575
+ const language = this.kernel?.language ?? "javascript";
576
+ const cellUri = `${(this.input?.data)?.getWorkspacePath?.() ?? "notebook"}-cell-${index}`;
577
+ const cellHeight = this.cellHeights.get(index) ?? Math.max(100, source.split("\n").length * 19 + 10);
578
+ const runButton = isExecuting ? html`
579
+ <wa-button
580
+ size="small"
581
+ appearance="plain"
582
+ @click=${() => this.cancelExecution(index)}
583
+ title="Stop execution">
584
+ <wa-icon name="stop" label="Stop" style="color: var(--wa-color-danger-500);"></wa-icon>
585
+ </wa-button>
586
+ ` : html`
587
+ <lyra-command
588
+ cmd="notebook.runCell"
589
+ icon="play"
590
+ title="Run cell"
591
+ size="small"
592
+ appearance="plain"
593
+ .params=${{ cellIndex: index }}>
594
+ </lyra-command>
595
+ `;
596
+ return html`
597
+ <div class="cell-wrapper">
598
+ <wa-animation
599
+ name="bounce"
600
+ duration="1000"
601
+ iterations="1"
602
+ ?play=${this.highlightedCellIndex === index}
603
+ @wa-finish=${() => this.highlightedCellIndex = -1}>
604
+ <div class="cell code-cell ${isExecuting ? "executing" : ""}">
605
+ <div class="cell-header">
606
+ <span class="cell-label">
607
+ ${isExecuting ? html`
608
+ In [<wa-animation name="pulse" duration="1000" iterations="Infinity" ?play=${isExecuting}>
609
+ <span class="executing-indicator">*</span>
610
+ </wa-animation>]
611
+ ` : html`
612
+ In [${cell.execution_count ?? " "}]
613
+ `}
614
+ </span>
615
+ ${this.renderHeaderActions(index, runButton)}
616
+ </div>
617
+ <div
618
+ class="cell-input monaco-container"
619
+ style=${styleMap({ height: `${cellHeight}px` })}
620
+ @wheel=${(e) => this.onCellWheel(index, e)}
621
+ >
622
+ <lyra-monaco-widget
623
+ .value=${source}
624
+ .language=${language}
625
+ .uri=${cellUri}
626
+ ?autoLayout=${true}
627
+ @content-change=${() => this.markDirty(true)}
628
+ @editor-focus=${() => {
629
+ this.focusedCellIndex = index;
630
+ }}
631
+ @editor-blur=${() => {
632
+ if (this.focusedCellIndex === index) this.focusedCellIndex = -1;
633
+ }}
634
+ @content-height-changed=${(e) => this.onCellHeightChange(index, e.detail.height)}
635
+ ${ref(this.getCellWidgetRef(index))}
636
+ ></lyra-monaco-widget>
637
+ </div>
638
+ ${output ? html`
639
+ <div class="cell-output ${output.type === "error" ? "output-error" : ""}">
640
+ <div class="output-label">Out [${index + 1}]:</div>
641
+ ${output.imageData ? html`
642
+ <img src="data:image/png;base64,${output.imageData}" alt="Output image" class="output-image" />
643
+ ` : ""}
644
+ ${output.data ? html`<pre><code>${output.data}</code></pre>` : ""}
645
+ </div>
646
+ ` : ""}
647
+ ${this.renderFooterActions(index)}
648
+ </div>
649
+ </wa-animation>
650
+ </div>
651
+ `;
652
+ }
653
+ renderCell(cell, index) {
654
+ if (cell.cell_type === "markdown") return this.renderMarkdownCell(cell, index);
655
+ else if (cell.cell_type === "code") return this.renderCodeCell(cell, index);
656
+ else return html`
657
+ <div class="cell raw-cell">
658
+ <pre><code>${this.getCellSource(cell)}</code></pre>
659
+ </div>
660
+ `;
661
+ }
662
+ addCell(index, cellType = "code") {
663
+ if (!this.notebook) return;
664
+ this.saveEditorContents();
665
+ this.shiftIndices(index, "up");
666
+ this.notebook.cells.splice(index, 0, this.createCell(cellType));
667
+ if (cellType === "markdown") this.editingMarkdownCells.add(index);
668
+ this.resetCellState();
669
+ this.highlightedCellIndex = index;
670
+ this.updateComplete.then(() => {
671
+ this.scrollToCell(index);
672
+ });
673
+ }
674
+ scrollToCell(index) {
675
+ const cellWrapper = this.shadowRoot?.querySelectorAll(".cell-wrapper")[index];
676
+ if (!cellWrapper) return;
677
+ const scroller = this.closest("wa-scroller");
678
+ if (!scroller) {
679
+ cellWrapper.scrollIntoView({
680
+ behavior: "smooth",
681
+ block: "center",
682
+ inline: "nearest"
683
+ });
684
+ return;
685
+ }
686
+ const scrollerRect = scroller.getBoundingClientRect();
687
+ const cellRect = cellWrapper.getBoundingClientRect();
688
+ const targetScroll = scroller.scrollTop + (cellRect.top - scrollerRect.top) - scrollerRect.height / 2 + cellRect.height / 2;
689
+ scroller.scrollTo({
690
+ top: targetScroll,
691
+ behavior: "smooth"
692
+ });
693
+ }
694
+ saveEditorContents() {
695
+ this.notebook?.cells.forEach((cell, index) => {
696
+ if (cell.cell_type !== "code") return;
697
+ const value = this.getCellWidgetRef(index).value?.getContent();
698
+ if (value !== void 0 && value !== null) cell.source = this.stringToSourceArray(value);
699
+ });
700
+ }
701
+ resetCellState() {
702
+ this.markDirty(true);
703
+ }
704
+ deleteCell(index) {
705
+ if (!this.notebook || this.notebook.cells.length <= 1) return;
706
+ this.saveEditorContents();
707
+ this.cellOutputs.delete(index);
708
+ this.executingCells.delete(index);
709
+ this.editingMarkdownCells.delete(index);
710
+ this.notebook.cells.splice(index, 1);
711
+ this.shiftIndices(index, "down");
712
+ this.resetCellState();
713
+ }
714
+ shiftIndices(startIndex, direction) {
715
+ const shift = direction === "up" ? 1 : -1;
716
+ const filterFn = direction === "up" ? (idx) => idx >= startIndex : (idx) => idx > startIndex;
717
+ const sortFn = direction === "up" ? (a, b) => b - a : (a, b) => a - b;
718
+ const shiftMap = (map) => {
719
+ Array.from(map.keys()).filter(filterFn).sort(sortFn).forEach((oldIndex) => {
720
+ const value = map.get(oldIndex);
721
+ map.delete(oldIndex);
722
+ map.set(oldIndex + shift, value);
723
+ });
724
+ };
725
+ const shiftSet = (set) => {
726
+ Array.from(set).filter(filterFn).sort(sortFn).forEach((oldIndex) => {
727
+ set.delete(oldIndex);
728
+ set.add(oldIndex + shift);
729
+ });
730
+ };
731
+ shiftMap(this.cellOutputs);
732
+ shiftSet(this.executingCells);
733
+ shiftSet(this.editingMarkdownCells);
734
+ shiftMap(this.cellWidgetRefs);
735
+ shiftMap(this.cellHeights);
736
+ }
737
+ getCellWidgetRef(index) {
738
+ if (!this.cellWidgetRefs.has(index)) this.cellWidgetRefs.set(index, createRef());
739
+ return this.cellWidgetRefs.get(index);
740
+ }
741
+ onCellHeightChange(index, height) {
742
+ const newHeight = Math.max(100, height + 10);
743
+ if (this.cellHeights.get(index) === newHeight) return;
744
+ this.cellHeights = new Map(this.cellHeights);
745
+ this.cellHeights.set(index, newHeight);
746
+ this.requestUpdate();
747
+ this.updateComplete.then(() => this.getCellWidgetRef(index).value?.layout?.());
748
+ }
749
+ onCellWheel(index, e) {
750
+ const editor = this.getCellWidgetRef(index).value?.getEditor();
751
+ if (!editor) return;
752
+ const scrollTop = editor.getScrollTop();
753
+ const scrollHeight = editor.getScrollHeight();
754
+ const contentHeight = editor.getContentHeight();
755
+ const canScroll = scrollHeight > contentHeight;
756
+ const atBoundary = e.deltaY < 0 && scrollTop <= 0 || e.deltaY > 0 && scrollTop + contentHeight >= scrollHeight;
757
+ if (!canScroll || atBoundary) e.stopImmediatePropagation();
758
+ }
759
+ openPackageManager() {
760
+ if (!this.kernel?.openPackageManager) return;
761
+ const packages = this.notebook?.metadata?.required_packages ?? [];
762
+ this.kernel.openPackageManager({
763
+ requiredPackages: packages,
764
+ onPackageAdded: (packageName) => {
765
+ if (!this.notebook) return;
766
+ if (!this.notebook.metadata) this.notebook.metadata = {};
767
+ if (!this.notebook.metadata.required_packages) this.notebook.metadata.required_packages = [];
768
+ if (!this.notebook.metadata.required_packages.includes(packageName)) {
769
+ this.notebook.metadata.required_packages.push(packageName);
770
+ this.markDirty(true);
771
+ }
772
+ },
773
+ onPackageRemoved: (packageName) => {
774
+ if (!this.notebook?.metadata?.required_packages) return;
775
+ const index = this.notebook.metadata.required_packages.indexOf(packageName);
776
+ if (index > -1) {
777
+ this.notebook.metadata.required_packages.splice(index, 1);
778
+ this.markDirty(true);
779
+ }
780
+ }
781
+ });
782
+ }
783
+ updated(changedProperties) {
784
+ super.updated(changedProperties);
785
+ if (changedProperties.has("kernelConnected") || changedProperties.has("kernelConnecting") || changedProperties.has("kernelVersion") || changedProperties.has("isRunningAll") || changedProperties.has("availableKernels") || changedProperties.has("selectedKernelId")) {}
786
+ }
787
+ renderContent() {
788
+ if (!this.notebook) return html`<div class="loading">Loading notebook...</div>`;
789
+ return html`
790
+ <div class="noteboocells">
791
+ ${repeat(this.notebook.cells, (_cell, index) => index, (cell, index) => this.renderCell(cell, index))}
792
+ </div>
793
+ `;
794
+ }
795
+ static {
796
+ this.styles = css`
797
+ :host {
798
+ display: block;
799
+ width: 100%;
800
+ }
801
+
802
+ .kernel-select {
803
+ max-width: 10rem;
804
+ }
805
+
806
+ .noteboocells {
807
+ display: flex;
808
+ flex-direction: column;
809
+ gap: 3rem;
810
+ max-width: 1200px;
811
+ margin: 0 auto;
812
+ padding: 2rem 1rem;
813
+ width: 100%;
814
+ box-sizing: border-box;
815
+ }
816
+
817
+ .cell-wrapper {
818
+ position: relative;
819
+ }
820
+
821
+ .cell {
822
+ border-radius: 4px;
823
+ overflow: hidden;
824
+ opacity: 0.9;
825
+ position: relative;
826
+ }
827
+
828
+ .cell-header-actions {
829
+ display: flex;
830
+ gap: 0.25rem;
831
+ align-items: center;
832
+ opacity: 0.5;
833
+ transition: opacity 0.2s;
834
+ }
835
+
836
+ .cell-header-actions .divider {
837
+ width: 1px;
838
+ height: 1rem;
839
+ background: var(--wa-color-outline);
840
+ margin: 0 0.25rem;
841
+ opacity: 0.5;
842
+ }
843
+
844
+ .cell-header:hover .cell-header-actions {
845
+ opacity: 1;
846
+ }
847
+
848
+ .cell-footer {
849
+ display: flex;
850
+ gap: 0.5rem;
851
+ align-items: center;
852
+ justify-content: flex-start;
853
+ padding: 0.5rem;
854
+ border-top: 1px solid var(--wa-color-outline);
855
+ opacity: 0.5;
856
+ transition: opacity 0.2s;
857
+ }
858
+
859
+ .cell-footer:hover {
860
+ opacity: 1;
861
+ }
862
+
863
+ .markdown-cell {
864
+ cursor: pointer;
865
+ transition: opacity 0.2s;
866
+ }
867
+
868
+ .markdown-cell:hover:not(.editing) {
869
+ opacity: 0.9;
870
+ }
871
+
872
+ .markdown-cell .cell-content {
873
+ padding: 1rem;
874
+ }
875
+
876
+ .markdown-cell.editing {
877
+ cursor: default;
878
+ padding: 0;
879
+ }
880
+
881
+ .markdown-cell.editing .cell-actions {
882
+ display: none;
883
+ }
884
+
885
+ .markdown-editor {
886
+ width: 100%;
887
+ min-height: 200px;
888
+ padding: 1rem;
889
+ font-family: monospace;
890
+ font-size: 0.95rem;
891
+ line-height: 1.6;
892
+ border: none;
893
+ outline: none;
894
+ resize: vertical;
895
+ background: transparent;
896
+ color: inherit;
897
+ }
898
+
899
+ .code-cell {
900
+ border-left: 3px solid var(--wa-color-primary-500);
901
+ transition: all 0.3s ease;
902
+ }
903
+
904
+ .code-cell.executing {
905
+ border-left: 4px solid var(--wa-color-primary-500);
906
+ box-shadow: 0 0 0 2px var(--wa-color-primary-500, rgba(59, 130, 246, 0.3));
907
+ animation: pulse-cell 2s ease-in-out infinite;
908
+ }
909
+
910
+ @keyframes pulse-cell {
911
+ 0%, 100% {
912
+ box-shadow: 0 0 0 2px var(--wa-color-primary-500, rgba(59, 130, 246, 0.3));
913
+ opacity: 1;
914
+ }
915
+ 50% {
916
+ box-shadow: 0 0 0 4px var(--wa-color-primary-500, rgba(59, 130, 246, 0.5));
917
+ opacity: 0.95;
918
+ }
919
+ }
920
+
921
+ .cell-header {
922
+ display: flex;
923
+ align-items: center;
924
+ justify-content: flex-start;
925
+ gap: 0.5rem;
926
+ padding: 0.5rem 1rem;
927
+ flex-wrap: nowrap;
928
+ }
929
+
930
+ .cell-label {
931
+ font-family: monospace;
932
+ font-weight: bold;
933
+ flex-shrink: 0;
934
+ }
935
+
936
+ .executing-indicator {
937
+ display: inline-block;
938
+ color: var(--wa-color-primary-500);
939
+ font-weight: bold;
940
+ font-size: 1.2em;
941
+ }
942
+
943
+ .cell-input {
944
+ margin: 0;
945
+ }
946
+
947
+ .monaco-container {
948
+ min-height: 100px;
949
+ height: auto;
950
+ width: 100%;
951
+ }
952
+
953
+ .cell-output {
954
+ padding: 1rem;
955
+ }
956
+
957
+ .output-label {
958
+ font-family: monospace;
959
+ font-weight: bold;
960
+ margin-bottom: 0.5rem;
961
+ opacity: 0.7;
962
+ }
963
+
964
+ .cell-output pre {
965
+ margin: 0;
966
+ overflow-x: auto;
967
+ }
968
+
969
+ .cell-output code {
970
+ font-family: 'Courier New', monospace;
971
+ font-size: 0.9rem;
972
+ line-height: 1.5;
973
+ }
974
+
975
+ .output-image {
976
+ max-width: 100%;
977
+ height: auto;
978
+ display: block;
979
+ margin: 0.5rem 0;
980
+ border-radius: 4px;
981
+ }
982
+
983
+ .output-error {
984
+ border-left: 3px solid var(--wa-color-danger-500);
985
+ }
986
+
987
+ .raw-cell {
988
+ padding: 1rem;
989
+ }
990
+
991
+ .raw-cell pre {
992
+ margin: 0;
993
+ }
994
+
995
+ .loading {
996
+ display: flex;
997
+ align-items: center;
998
+ justify-content: center;
999
+ height: 100%;
1000
+ font-size: 1.2rem;
1001
+ }
1002
+
1003
+ .markdown-placeholder {
1004
+ display: flex;
1005
+ align-items: center;
1006
+ justify-content: center;
1007
+ gap: 0.75rem;
1008
+ padding: 3rem 1rem;
1009
+ opacity: 0.5;
1010
+ font-style: italic;
1011
+ transition: opacity 0.2s;
1012
+ }
1013
+
1014
+ .markdown-cell.empty:hover .markdown-placeholder {
1015
+ opacity: 0.8;
1016
+ }
1017
+
1018
+ .markdown-placeholder wa-icon {
1019
+ font-size: 1.5rem;
1020
+ }
1021
+ `;
1022
+ }
1023
+ };
1024
+ _decorate([property({ attribute: false })], LyraNotebookEditor.prototype, "input", void 0);
1025
+ _decorate([state()], LyraNotebookEditor.prototype, "notebook", void 0);
1026
+ _decorate([state()], LyraNotebookEditor.prototype, "cellOutputs", void 0);
1027
+ _decorate([state()], LyraNotebookEditor.prototype, "executingCells", void 0);
1028
+ _decorate([state()], LyraNotebookEditor.prototype, "availableKernels", void 0);
1029
+ _decorate([state()], LyraNotebookEditor.prototype, "selectedKernelId", void 0);
1030
+ _decorate([state()], LyraNotebookEditor.prototype, "kernel", void 0);
1031
+ _decorate([state()], LyraNotebookEditor.prototype, "kernelConnected", void 0);
1032
+ _decorate([state()], LyraNotebookEditor.prototype, "kernelConnecting", void 0);
1033
+ _decorate([state()], LyraNotebookEditor.prototype, "kernelVersion", void 0);
1034
+ _decorate([state()], LyraNotebookEditor.prototype, "editingMarkdownCells", void 0);
1035
+ _decorate([state()], LyraNotebookEditor.prototype, "executionCounter", void 0);
1036
+ _decorate([state()], LyraNotebookEditor.prototype, "isRunningAll", void 0);
1037
+ _decorate([state()], LyraNotebookEditor.prototype, "highlightedCellIndex", void 0);
1038
+ _decorate([state()], LyraNotebookEditor.prototype, "cellHeights", void 0);
1039
+ LyraNotebookEditor = _decorate([customElement("lyra-notebook-editor")], LyraNotebookEditor);
1040
+ //#endregion
1041
+ export { LyraNotebookEditor };
1042
+
1043
+ //# sourceMappingURL=notebook-runtime-CWTQxD9j.js.map