@mcp-b/interactive-components 0.2.0
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.
- package/LICENSE +21 -0
- package/README.md +99 -0
- package/dist/code-editor-B94tewLC.js +425 -0
- package/dist/code-preview-gKk0nBKP.js +269 -0
- package/dist/components/code-editor/code-editor.d.ts +65 -0
- package/dist/components/code-editor/code-editor.js +2 -0
- package/dist/components/code-preview/code-preview.d.ts +58 -0
- package/dist/components/code-preview/code-preview.js +3 -0
- package/dist/components/interactive-editor/interactive-editor.d.ts +77 -0
- package/dist/components/interactive-editor/interactive-editor.js +3 -0
- package/dist/components/r-editor/r-editor.d.ts +68 -0
- package/dist/components/r-editor/r-editor.js +2 -0
- package/dist/components/sql-editor/sql-editor.d.ts +78 -0
- package/dist/components/sql-editor/sql-editor.js +2 -0
- package/dist/decorate-DcF3lt7P.js +9 -0
- package/dist/docs/custom-elements.json +2807 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/interactive-editor-WfTWxJGF.js +1454 -0
- package/dist/lib/runner-channel.d.ts +25 -0
- package/dist/lib/runner-channel.js +55 -0
- package/dist/lib/runner-url.d.ts +15 -0
- package/dist/lib/runner-url.js +19 -0
- package/dist/lib/transform.d.ts +2 -0
- package/dist/lib/transform.js +210 -0
- package/dist/r-editor-DwSyDo2i.js +743 -0
- package/dist/runners/ephemeral-indexeddb.js +114 -0
- package/dist/runners/pglite-runner.js +175 -0
- package/dist/runners/pyscript-runner.html +189 -0
- package/dist/runners/webr-runner.html +282 -0
- package/dist/sql-editor-CsNrjJ2_.js +968 -0
- package/dist/themes/interactive.css +79 -0
- package/dist/transform-DWhHlnkw.d.ts +53 -0
- package/dist/vite.d.ts +14 -0
- package/dist/vite.js +62 -0
- package/package.json +96 -0
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
import "./code-editor-B94tewLC.js";
|
|
2
|
+
import { t as __decorate } from "./decorate-DcF3lt7P.js";
|
|
3
|
+
import { bindRunnerUrl, connectRunner, createRunnerChannelId, isRecord, parseRunnerConsoleMessage } from "./lib/runner-channel.js";
|
|
4
|
+
import { getRunnerUrl } from "./lib/runner-url.js";
|
|
5
|
+
import { css, html } from "lit";
|
|
6
|
+
import { property, state } from "lit/decorators.js";
|
|
7
|
+
import { SigveloElement } from "@mcp-b/wc-support/base/sigvelo-element";
|
|
8
|
+
import hostStyles from "@mcp-b/wc-support/styles/host.styles";
|
|
9
|
+
import "@mcp-b/web-components/components/spinner/spinner.js";
|
|
10
|
+
import { ifDefined } from "lit/directives/if-defined.js";
|
|
11
|
+
import "@mcp-b/web-components/components/splitter/splitter.js";
|
|
12
|
+
import "@mcp-b/web-components/components/button/button.js";
|
|
13
|
+
import "@mcp-b/web-components/components/button-group/button-group.js";
|
|
14
|
+
import "@mcp-b/web-components/components/tooltip/tooltip.js";
|
|
15
|
+
//#region src/components/r-editor/r-editor.styles.ts
|
|
16
|
+
var r_editor_styles_default = css`
|
|
17
|
+
/* ═══════════════════════════════════════════
|
|
18
|
+
HOST
|
|
19
|
+
═══════════════════════════════════════════ */
|
|
20
|
+
|
|
21
|
+
:host {
|
|
22
|
+
display: flex;
|
|
23
|
+
flex-direction: column;
|
|
24
|
+
height: var(--r-editor-height, 560px);
|
|
25
|
+
border: 1px solid var(--sigvelo-color-neutral-border-muted, #d1d5db);
|
|
26
|
+
border-radius: 8px;
|
|
27
|
+
overflow: hidden;
|
|
28
|
+
background: var(--sigvelo-color-surface, #fff);
|
|
29
|
+
font-family:
|
|
30
|
+
system-ui,
|
|
31
|
+
-apple-system,
|
|
32
|
+
sans-serif;
|
|
33
|
+
font-size: 13px;
|
|
34
|
+
color: var(--sigvelo-color-text, #1f2937);
|
|
35
|
+
position: relative;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/* ═══════════════════════════════════════════
|
|
39
|
+
CONTENT (desktop layout)
|
|
40
|
+
═══════════════════════════════════════════ */
|
|
41
|
+
|
|
42
|
+
.content {
|
|
43
|
+
display: flex;
|
|
44
|
+
flex: 1;
|
|
45
|
+
min-height: 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.content sigvelo-splitter {
|
|
49
|
+
flex: 1;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* ═══════════════════════════════════════════
|
|
53
|
+
EDITOR PANE (left side)
|
|
54
|
+
═══════════════════════════════════════════ */
|
|
55
|
+
|
|
56
|
+
.editor-pane {
|
|
57
|
+
display: flex;
|
|
58
|
+
flex-direction: column;
|
|
59
|
+
min-width: 0;
|
|
60
|
+
height: 100%;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.editor-pane__toolbar {
|
|
64
|
+
display: flex;
|
|
65
|
+
align-items: center;
|
|
66
|
+
gap: 6px;
|
|
67
|
+
padding: 4px 8px;
|
|
68
|
+
border-bottom: 1px solid var(--sigvelo-color-neutral-border-subtle, #e5e7eb);
|
|
69
|
+
background: var(--sigvelo-color-neutral-bg-subtle, #f9fafb);
|
|
70
|
+
min-height: 32px;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.editor-pane__toolbar-title {
|
|
74
|
+
font-weight: 600;
|
|
75
|
+
font-size: 12px;
|
|
76
|
+
color: var(--sigvelo-color-neutral-text, #6b7280);
|
|
77
|
+
margin-right: auto;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.editor-pane__code {
|
|
81
|
+
flex: 1;
|
|
82
|
+
min-height: 0;
|
|
83
|
+
overflow: hidden;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.editor-pane__code sigvelo-code-editor {
|
|
87
|
+
display: block;
|
|
88
|
+
height: 100%;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/* ═══════════════════════════════════════════
|
|
92
|
+
RIGHT PANE (plots + console)
|
|
93
|
+
═══════════════════════════════════════════ */
|
|
94
|
+
|
|
95
|
+
.right-pane {
|
|
96
|
+
display: flex;
|
|
97
|
+
flex-direction: column;
|
|
98
|
+
min-width: 0;
|
|
99
|
+
height: 100%;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.right-pane__header {
|
|
103
|
+
display: flex;
|
|
104
|
+
align-items: center;
|
|
105
|
+
gap: 6px;
|
|
106
|
+
padding: 4px 8px;
|
|
107
|
+
border-bottom: 1px solid var(--sigvelo-color-neutral-border-subtle, #e5e7eb);
|
|
108
|
+
background: var(--sigvelo-color-neutral-bg-subtle, #f9fafb);
|
|
109
|
+
min-height: 32px;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
.right-pane__header sigvelo-button-group {
|
|
113
|
+
margin-right: auto;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
.right-pane__content {
|
|
117
|
+
flex: 1;
|
|
118
|
+
min-height: 0;
|
|
119
|
+
overflow: auto;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* ═══════════════════════════════════════════
|
|
123
|
+
PLOT VIEWER
|
|
124
|
+
═══════════════════════════════════════════ */
|
|
125
|
+
|
|
126
|
+
.plot-viewer {
|
|
127
|
+
display: flex;
|
|
128
|
+
flex-direction: column;
|
|
129
|
+
height: 100%;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
.plot-viewer__canvas {
|
|
133
|
+
flex: 1;
|
|
134
|
+
display: flex;
|
|
135
|
+
align-items: center;
|
|
136
|
+
justify-content: center;
|
|
137
|
+
min-height: 0;
|
|
138
|
+
overflow: auto;
|
|
139
|
+
background: #fff;
|
|
140
|
+
padding: 8px;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
.plot-viewer__canvas img {
|
|
144
|
+
max-width: 100%;
|
|
145
|
+
max-height: 100%;
|
|
146
|
+
object-fit: contain;
|
|
147
|
+
border-radius: 4px;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.plot-viewer__nav {
|
|
151
|
+
display: flex;
|
|
152
|
+
align-items: center;
|
|
153
|
+
justify-content: center;
|
|
154
|
+
gap: 8px;
|
|
155
|
+
padding: 6px 8px;
|
|
156
|
+
border-top: 1px solid var(--sigvelo-color-neutral-border-subtle, #e5e7eb);
|
|
157
|
+
background: var(--sigvelo-color-neutral-bg-subtle, #f9fafb);
|
|
158
|
+
font-size: 12px;
|
|
159
|
+
color: var(--sigvelo-color-neutral-text, #6b7280);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.plot-viewer__counter {
|
|
163
|
+
min-width: 80px;
|
|
164
|
+
text-align: center;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
.plot-viewer__empty {
|
|
168
|
+
display: flex;
|
|
169
|
+
align-items: center;
|
|
170
|
+
justify-content: center;
|
|
171
|
+
height: 100%;
|
|
172
|
+
color: var(--sigvelo-color-neutral-text, #9ca3af);
|
|
173
|
+
font-size: 13px;
|
|
174
|
+
padding: 24px;
|
|
175
|
+
text-align: center;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/* ═══════════════════════════════════════════
|
|
179
|
+
CONSOLE VIEW
|
|
180
|
+
═══════════════════════════════════════════ */
|
|
181
|
+
|
|
182
|
+
.console-view {
|
|
183
|
+
flex: 1;
|
|
184
|
+
overflow: auto;
|
|
185
|
+
padding: 8px 12px;
|
|
186
|
+
font-family: "SF Mono", "Cascadia Code", "Fira Code", "Consolas", monospace;
|
|
187
|
+
font-size: 12px;
|
|
188
|
+
line-height: 1.5;
|
|
189
|
+
background: var(--sigvelo-color-neutral-bg-subtle, #f9fafb);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
.console-empty {
|
|
193
|
+
color: var(--sigvelo-color-neutral-text, #9ca3af);
|
|
194
|
+
font-style: italic;
|
|
195
|
+
padding: 16px 0;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
.console-entry {
|
|
199
|
+
display: flex;
|
|
200
|
+
gap: 8px;
|
|
201
|
+
padding: 1px 0;
|
|
202
|
+
white-space: pre-wrap;
|
|
203
|
+
word-break: break-word;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
.console-entry--error {
|
|
207
|
+
color: var(--sigvelo-color-danger-text, #dc2626);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
.console-entry--warn {
|
|
211
|
+
color: var(--sigvelo-color-warning-text, #d97706);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
.console-entry__level {
|
|
215
|
+
flex-shrink: 0;
|
|
216
|
+
width: 16px;
|
|
217
|
+
text-align: center;
|
|
218
|
+
opacity: 0.5;
|
|
219
|
+
font-size: 10px;
|
|
220
|
+
line-height: 18px;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
.console-entry__text {
|
|
224
|
+
flex: 1;
|
|
225
|
+
min-width: 0;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/* ═══════════════════════════════════════════
|
|
229
|
+
CONSOLE BADGE
|
|
230
|
+
═══════════════════════════════════════════ */
|
|
231
|
+
|
|
232
|
+
.console-badge {
|
|
233
|
+
display: inline-flex;
|
|
234
|
+
align-items: center;
|
|
235
|
+
justify-content: center;
|
|
236
|
+
min-width: 16px;
|
|
237
|
+
height: 16px;
|
|
238
|
+
padding: 0 4px;
|
|
239
|
+
border-radius: 8px;
|
|
240
|
+
background: var(--sigvelo-color-danger-bg, #ef4444);
|
|
241
|
+
color: #fff;
|
|
242
|
+
font-size: 10px;
|
|
243
|
+
font-weight: 600;
|
|
244
|
+
margin-left: 4px;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/* ═══════════════════════════════════════════
|
|
248
|
+
LOADING OVERLAY
|
|
249
|
+
═══════════════════════════════════════════ */
|
|
250
|
+
|
|
251
|
+
.loading-overlay {
|
|
252
|
+
position: absolute;
|
|
253
|
+
inset: 0;
|
|
254
|
+
display: flex;
|
|
255
|
+
flex-direction: column;
|
|
256
|
+
align-items: center;
|
|
257
|
+
justify-content: center;
|
|
258
|
+
gap: 12px;
|
|
259
|
+
background: color-mix(in srgb, var(--sigvelo-color-surface, #fff) 85%, transparent);
|
|
260
|
+
z-index: 10;
|
|
261
|
+
font-size: 13px;
|
|
262
|
+
color: var(--sigvelo-color-neutral-text, #6b7280);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
.loading-overlay__message {
|
|
266
|
+
max-width: 280px;
|
|
267
|
+
text-align: center;
|
|
268
|
+
line-height: 1.4;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/* ═══════════════════════════════════════════
|
|
272
|
+
MOBILE / COMPACT LAYOUT
|
|
273
|
+
═══════════════════════════════════════════ */
|
|
274
|
+
|
|
275
|
+
.mobile-nav {
|
|
276
|
+
display: flex;
|
|
277
|
+
align-items: center;
|
|
278
|
+
gap: 6px;
|
|
279
|
+
padding: 4px 8px;
|
|
280
|
+
border-bottom: 1px solid var(--sigvelo-color-neutral-border-subtle, #e5e7eb);
|
|
281
|
+
background: var(--sigvelo-color-neutral-bg-subtle, #f9fafb);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
.mobile-nav sigvelo-button-group {
|
|
285
|
+
margin-right: auto;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
.single-pane {
|
|
289
|
+
flex: 1;
|
|
290
|
+
min-height: 0;
|
|
291
|
+
display: flex;
|
|
292
|
+
flex-direction: column;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
.single-pane .editor-pane {
|
|
296
|
+
flex: 1;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
.single-pane .console-view {
|
|
300
|
+
flex: 1;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
.single-pane .plot-viewer {
|
|
304
|
+
flex: 1;
|
|
305
|
+
}
|
|
306
|
+
`;
|
|
307
|
+
//#endregion
|
|
308
|
+
//#region src/components/r-editor/r-editor.ts
|
|
309
|
+
const ICON_PLAY = html`
|
|
310
|
+
<svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor" style="display: block">
|
|
311
|
+
<path d="M5 3l14 9-14 9V3z" />
|
|
312
|
+
</svg>
|
|
313
|
+
`;
|
|
314
|
+
const ICON_TRASH = html`
|
|
315
|
+
<svg
|
|
316
|
+
width="12"
|
|
317
|
+
height="12"
|
|
318
|
+
viewBox="0 0 24 24"
|
|
319
|
+
fill="none"
|
|
320
|
+
stroke="currentColor"
|
|
321
|
+
stroke-width="2"
|
|
322
|
+
stroke-linecap="round"
|
|
323
|
+
stroke-linejoin="round"
|
|
324
|
+
style="display: block"
|
|
325
|
+
>
|
|
326
|
+
<path
|
|
327
|
+
d="M3 6h18M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"
|
|
328
|
+
/>
|
|
329
|
+
</svg>
|
|
330
|
+
`;
|
|
331
|
+
const ICON_CHEVRON_LEFT = html`
|
|
332
|
+
<svg
|
|
333
|
+
width="10"
|
|
334
|
+
height="10"
|
|
335
|
+
viewBox="0 0 24 24"
|
|
336
|
+
fill="none"
|
|
337
|
+
stroke="currentColor"
|
|
338
|
+
stroke-width="2.5"
|
|
339
|
+
stroke-linecap="round"
|
|
340
|
+
stroke-linejoin="round"
|
|
341
|
+
style="display: block"
|
|
342
|
+
>
|
|
343
|
+
<path d="M15 18l-6-6 6-6" />
|
|
344
|
+
</svg>
|
|
345
|
+
`;
|
|
346
|
+
const ICON_CHEVRON_RIGHT = html`
|
|
347
|
+
<svg
|
|
348
|
+
width="10"
|
|
349
|
+
height="10"
|
|
350
|
+
viewBox="0 0 24 24"
|
|
351
|
+
fill="none"
|
|
352
|
+
stroke="currentColor"
|
|
353
|
+
stroke-width="2.5"
|
|
354
|
+
stroke-linecap="round"
|
|
355
|
+
stroke-linejoin="round"
|
|
356
|
+
style="display: block"
|
|
357
|
+
>
|
|
358
|
+
<path d="M9 18l6-6-6-6" />
|
|
359
|
+
</svg>
|
|
360
|
+
`;
|
|
361
|
+
const ICON_DOWNLOAD = html`
|
|
362
|
+
<svg
|
|
363
|
+
width="12"
|
|
364
|
+
height="12"
|
|
365
|
+
viewBox="0 0 24 24"
|
|
366
|
+
fill="none"
|
|
367
|
+
stroke="currentColor"
|
|
368
|
+
stroke-width="2"
|
|
369
|
+
stroke-linecap="round"
|
|
370
|
+
stroke-linejoin="round"
|
|
371
|
+
style="display: block"
|
|
372
|
+
>
|
|
373
|
+
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
|
|
374
|
+
<polyline points="7 10 12 15 17 10" />
|
|
375
|
+
<line x1="12" y1="15" x2="12" y2="3" />
|
|
376
|
+
</svg>
|
|
377
|
+
`;
|
|
378
|
+
const COMPACT_BREAKPOINT = 560;
|
|
379
|
+
/**
|
|
380
|
+
* `<sigvelo-r-editor>`
|
|
381
|
+
*
|
|
382
|
+
* @summary A WebR-powered R code editor with integrated console output and plot
|
|
383
|
+
* viewer. Runs R code in the browser via WebAssembly. The editor pane uses
|
|
384
|
+
* CodeMirror with R syntax highlighting, while the right pane shows captured
|
|
385
|
+
* plots and console output in separate tabs.
|
|
386
|
+
*
|
|
387
|
+
* @tag sigvelo-r-editor
|
|
388
|
+
* @status experimental
|
|
389
|
+
* @since 0.1
|
|
390
|
+
*/
|
|
391
|
+
var SigveloREditor = class extends SigveloElement {
|
|
392
|
+
constructor(..._args) {
|
|
393
|
+
super(..._args);
|
|
394
|
+
this.code = "";
|
|
395
|
+
this.packages = [];
|
|
396
|
+
this.editorLabel = "R code editor";
|
|
397
|
+
this._editedCode = "";
|
|
398
|
+
this._consoleLogs = [];
|
|
399
|
+
this._plots = [];
|
|
400
|
+
this._currentPlotIndex = 0;
|
|
401
|
+
this._webRReady = false;
|
|
402
|
+
this._webRLoading = true;
|
|
403
|
+
this._loadingMessage = "Initializing WebR...";
|
|
404
|
+
this._running = false;
|
|
405
|
+
this._compact = false;
|
|
406
|
+
this._compactView = "code";
|
|
407
|
+
this._rightPane = "console";
|
|
408
|
+
this._unreadConsoleCount = 0;
|
|
409
|
+
this._resizeObserver = null;
|
|
410
|
+
this._runnerIframe = null;
|
|
411
|
+
this._runnerPort = null;
|
|
412
|
+
this._runnerChannelId = createRunnerChannelId();
|
|
413
|
+
}
|
|
414
|
+
static {
|
|
415
|
+
this.styles = [hostStyles, r_editor_styles_default];
|
|
416
|
+
}
|
|
417
|
+
connectedCallback() {
|
|
418
|
+
super.connectedCallback();
|
|
419
|
+
this._editedCode = this.code;
|
|
420
|
+
this._resizeObserver = new ResizeObserver((entries) => {
|
|
421
|
+
for (const entry of entries) {
|
|
422
|
+
const compact = entry.contentRect.width < COMPACT_BREAKPOINT;
|
|
423
|
+
if (compact !== this._compact) this._compact = compact;
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
this._resizeObserver.observe(this);
|
|
427
|
+
}
|
|
428
|
+
firstUpdated() {
|
|
429
|
+
this._initRunner();
|
|
430
|
+
}
|
|
431
|
+
disconnectedCallback() {
|
|
432
|
+
super.disconnectedCallback();
|
|
433
|
+
this._resizeObserver?.disconnect();
|
|
434
|
+
this._runnerPort?.close();
|
|
435
|
+
this._runnerPort = null;
|
|
436
|
+
this._runnerIframe?.remove();
|
|
437
|
+
this._runnerIframe = null;
|
|
438
|
+
}
|
|
439
|
+
_initRunner() {
|
|
440
|
+
const iframe = document.createElement("iframe");
|
|
441
|
+
iframe.style.display = "none";
|
|
442
|
+
iframe.sandbox.add("allow-scripts");
|
|
443
|
+
iframe.addEventListener("load", () => {
|
|
444
|
+
this._runnerPort?.close();
|
|
445
|
+
this._runnerPort = connectRunner(iframe, this._runnerChannelId, (data) => this._onMessage(data));
|
|
446
|
+
}, { once: true });
|
|
447
|
+
const runnerUrl = bindRunnerUrl(getRunnerUrl("webr"), this._runnerChannelId);
|
|
448
|
+
runnerUrl.searchParams.set("v", String(Date.now()));
|
|
449
|
+
iframe.src = runnerUrl.href;
|
|
450
|
+
this._runnerIframe = iframe;
|
|
451
|
+
document.body.appendChild(iframe);
|
|
452
|
+
}
|
|
453
|
+
_onMessage(data) {
|
|
454
|
+
if (!isRecord(data)) return;
|
|
455
|
+
if (data.__sigvelo_webr_ready === true) {
|
|
456
|
+
this._webRReady = true;
|
|
457
|
+
this._webRLoading = false;
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (data.__sigvelo_webr_loading === true) {
|
|
461
|
+
this._loadingMessage = typeof data.message === "string" ? data.message : "Loading...";
|
|
462
|
+
this._webRLoading = true;
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const consoleMessage = parseRunnerConsoleMessage(data);
|
|
466
|
+
if (consoleMessage) {
|
|
467
|
+
const entry = consoleMessage;
|
|
468
|
+
this._consoleLogs = [...this._consoleLogs, entry];
|
|
469
|
+
if (!this._compact && this._rightPane !== "console") this._unreadConsoleCount++;
|
|
470
|
+
if (this._compact && this._compactView !== "console") this._unreadConsoleCount++;
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
if (data.__sigvelo_plot === true && typeof data.dataUrl === "string" && typeof data.index === "number" && Number.isInteger(data.index)) {
|
|
474
|
+
const plot = {
|
|
475
|
+
dataUrl: data.dataUrl,
|
|
476
|
+
index: data.index
|
|
477
|
+
};
|
|
478
|
+
this._plots = [...this._plots, plot];
|
|
479
|
+
this._currentPlotIndex = this._plots.length - 1;
|
|
480
|
+
if (!this._compact) this._rightPane = "plots";
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
if (data.__sigvelo_webr_done === true) {
|
|
484
|
+
this._running = false;
|
|
485
|
+
this._webRLoading = false;
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
_runCode() {
|
|
490
|
+
if (!this._webRReady || !this._runnerPort) return;
|
|
491
|
+
this._running = true;
|
|
492
|
+
this._consoleLogs = [];
|
|
493
|
+
this._plots = [];
|
|
494
|
+
this._currentPlotIndex = 0;
|
|
495
|
+
this._unreadConsoleCount = 0;
|
|
496
|
+
this._runnerPort.postMessage({
|
|
497
|
+
type: "webr-run",
|
|
498
|
+
code: this._editedCode,
|
|
499
|
+
packages: this.packages
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
_onEditorChange(e) {
|
|
503
|
+
this._editedCode = e.target.value;
|
|
504
|
+
}
|
|
505
|
+
_prevPlot() {
|
|
506
|
+
if (this._currentPlotIndex > 0) this._currentPlotIndex--;
|
|
507
|
+
}
|
|
508
|
+
_nextPlot() {
|
|
509
|
+
if (this._currentPlotIndex < this._plots.length - 1) this._currentPlotIndex++;
|
|
510
|
+
}
|
|
511
|
+
_savePlot() {
|
|
512
|
+
const plot = this._plots[this._currentPlotIndex];
|
|
513
|
+
if (!plot) return;
|
|
514
|
+
const a = document.createElement("a");
|
|
515
|
+
a.href = plot.dataUrl;
|
|
516
|
+
a.download = `plot-${this._currentPlotIndex + 1}.png`;
|
|
517
|
+
a.click();
|
|
518
|
+
}
|
|
519
|
+
_clearPlots() {
|
|
520
|
+
this._plots = [];
|
|
521
|
+
this._currentPlotIndex = 0;
|
|
522
|
+
}
|
|
523
|
+
_clearConsole() {
|
|
524
|
+
this._consoleLogs = [];
|
|
525
|
+
this._unreadConsoleCount = 0;
|
|
526
|
+
}
|
|
527
|
+
_switchRightPane(pane) {
|
|
528
|
+
this._rightPane = pane;
|
|
529
|
+
if (pane === "console") this._unreadConsoleCount = 0;
|
|
530
|
+
}
|
|
531
|
+
_switchView(view) {
|
|
532
|
+
this._compactView = view;
|
|
533
|
+
if (view === "console") this._unreadConsoleCount = 0;
|
|
534
|
+
}
|
|
535
|
+
_renderEditorPane(slot) {
|
|
536
|
+
return html`
|
|
537
|
+
<div class="editor-pane" slot=${ifDefined(slot)}>
|
|
538
|
+
<div class="editor-pane__toolbar">
|
|
539
|
+
<span class="editor-pane__toolbar-title">R Script</span>
|
|
540
|
+
<sigvelo-button
|
|
541
|
+
size="xs"
|
|
542
|
+
color="primary"
|
|
543
|
+
?disabled=${!this._webRReady || this._running}
|
|
544
|
+
@click=${() => this._runCode()}
|
|
545
|
+
>
|
|
546
|
+
${ICON_PLAY} Run
|
|
547
|
+
</sigvelo-button>
|
|
548
|
+
</div>
|
|
549
|
+
<div class="editor-pane__code">
|
|
550
|
+
<sigvelo-code-editor
|
|
551
|
+
.value=${this._editedCode}
|
|
552
|
+
language="r"
|
|
553
|
+
editor-label=${this.editorLabel}
|
|
554
|
+
@sigvelo-change=${this._onEditorChange}
|
|
555
|
+
></sigvelo-code-editor>
|
|
556
|
+
</div>
|
|
557
|
+
</div>
|
|
558
|
+
`;
|
|
559
|
+
}
|
|
560
|
+
_renderPlotViewer() {
|
|
561
|
+
if (this._plots.length === 0) return html`
|
|
562
|
+
<div class="plot-viewer">
|
|
563
|
+
<div class="plot-viewer__empty">
|
|
564
|
+
No plots yet. Run R code that creates a plot to see it here.
|
|
565
|
+
</div>
|
|
566
|
+
</div>
|
|
567
|
+
`;
|
|
568
|
+
return html`
|
|
569
|
+
<div class="plot-viewer">
|
|
570
|
+
<div class="plot-viewer__canvas">
|
|
571
|
+
<img src=${this._plots[this._currentPlotIndex]?.dataUrl ?? ""} alt="R plot ${this._currentPlotIndex + 1}" />
|
|
572
|
+
</div>
|
|
573
|
+
<div class="plot-viewer__nav">
|
|
574
|
+
<sigvelo-button
|
|
575
|
+
size="xs"
|
|
576
|
+
variant="ghost"
|
|
577
|
+
?disabled=${this._currentPlotIndex <= 0}
|
|
578
|
+
icon-label="Previous plot"
|
|
579
|
+
@click=${() => this._prevPlot()}
|
|
580
|
+
>
|
|
581
|
+
${ICON_CHEVRON_LEFT}
|
|
582
|
+
</sigvelo-button>
|
|
583
|
+
<span class="plot-viewer__counter">
|
|
584
|
+
${this._currentPlotIndex + 1} / ${this._plots.length}
|
|
585
|
+
</span>
|
|
586
|
+
<sigvelo-button
|
|
587
|
+
size="xs"
|
|
588
|
+
variant="ghost"
|
|
589
|
+
?disabled=${this._currentPlotIndex >= this._plots.length - 1}
|
|
590
|
+
icon-label="Next plot"
|
|
591
|
+
@click=${() => this._nextPlot()}
|
|
592
|
+
>
|
|
593
|
+
${ICON_CHEVRON_RIGHT}
|
|
594
|
+
</sigvelo-button>
|
|
595
|
+
<sigvelo-button
|
|
596
|
+
size="xs"
|
|
597
|
+
variant="ghost"
|
|
598
|
+
icon-label="Save plot"
|
|
599
|
+
@click=${() => this._savePlot()}
|
|
600
|
+
>
|
|
601
|
+
${ICON_DOWNLOAD}
|
|
602
|
+
</sigvelo-button>
|
|
603
|
+
<sigvelo-button
|
|
604
|
+
size="xs"
|
|
605
|
+
variant="ghost"
|
|
606
|
+
icon-label="Clear plots"
|
|
607
|
+
@click=${() => this._clearPlots()}
|
|
608
|
+
>
|
|
609
|
+
${ICON_TRASH}
|
|
610
|
+
</sigvelo-button>
|
|
611
|
+
</div>
|
|
612
|
+
</div>
|
|
613
|
+
`;
|
|
614
|
+
}
|
|
615
|
+
_renderConsoleView() {
|
|
616
|
+
return html`
|
|
617
|
+
<div class="console-view">
|
|
618
|
+
${this._consoleLogs.length === 0 ? html`<div class="console-empty">No console output yet.</div>` : this._consoleLogs.map((entry) => html`
|
|
619
|
+
<div class="console-entry console-entry--${entry.level}">
|
|
620
|
+
<span class="console-entry__level"
|
|
621
|
+
>${entry.level === "error" ? "E" : entry.level === "warn" ? "W" : ">"}</span
|
|
622
|
+
>
|
|
623
|
+
<span class="console-entry__text">${entry.args.join(" ")}</span>
|
|
624
|
+
</div>
|
|
625
|
+
`)}
|
|
626
|
+
</div>
|
|
627
|
+
`;
|
|
628
|
+
}
|
|
629
|
+
_renderRightPane(slot) {
|
|
630
|
+
const content = html`
|
|
631
|
+
<div class="right-pane__header">
|
|
632
|
+
<sigvelo-button-group label="Output toggle">
|
|
633
|
+
<sigvelo-button
|
|
634
|
+
size="xs"
|
|
635
|
+
variant=${this._rightPane === "plots" ? "normal" : "ghost"}
|
|
636
|
+
color=${this._rightPane === "plots" ? "primary" : "neutral"}
|
|
637
|
+
@click=${() => this._switchRightPane("plots")}
|
|
638
|
+
>
|
|
639
|
+
Plots${this._plots.length > 0 ? html` (${this._plots.length})` : ""}
|
|
640
|
+
</sigvelo-button>
|
|
641
|
+
<sigvelo-button
|
|
642
|
+
size="xs"
|
|
643
|
+
variant=${this._rightPane === "console" ? "normal" : "ghost"}
|
|
644
|
+
color=${this._rightPane === "console" ? "primary" : "neutral"}
|
|
645
|
+
@click=${() => this._switchRightPane("console")}
|
|
646
|
+
>
|
|
647
|
+
Console${this._unreadConsoleCount > 0 ? html`<span class="console-badge">${this._unreadConsoleCount}</span>` : ""}
|
|
648
|
+
</sigvelo-button>
|
|
649
|
+
</sigvelo-button-group>
|
|
650
|
+
${this._rightPane === "console" ? html`
|
|
651
|
+
<sigvelo-button
|
|
652
|
+
size="xs"
|
|
653
|
+
variant="ghost"
|
|
654
|
+
icon-label="Clear console"
|
|
655
|
+
@click=${() => this._clearConsole()}
|
|
656
|
+
>
|
|
657
|
+
${ICON_TRASH}
|
|
658
|
+
</sigvelo-button>
|
|
659
|
+
` : ""}
|
|
660
|
+
</div>
|
|
661
|
+
<div class="right-pane__content">
|
|
662
|
+
${this._rightPane === "plots" ? this._renderPlotViewer() : this._renderConsoleView()}
|
|
663
|
+
</div>
|
|
664
|
+
`;
|
|
665
|
+
return slot ? html`<div class="right-pane" slot=${slot}>${content}</div>` : html`<div class="right-pane">${content}</div>`;
|
|
666
|
+
}
|
|
667
|
+
_renderLoadingOverlay() {
|
|
668
|
+
if (!this._webRLoading) return "";
|
|
669
|
+
return html`
|
|
670
|
+
<div class="loading-overlay">
|
|
671
|
+
<sigvelo-spinner label="Loading WebR"></sigvelo-spinner>
|
|
672
|
+
<div class="loading-overlay__message">${this._loadingMessage}</div>
|
|
673
|
+
</div>
|
|
674
|
+
`;
|
|
675
|
+
}
|
|
676
|
+
_renderMobileNav() {
|
|
677
|
+
const btn = (view, label, badge) => html`
|
|
678
|
+
<sigvelo-button
|
|
679
|
+
size="xs"
|
|
680
|
+
variant=${this._compactView === view ? "normal" : "ghost"}
|
|
681
|
+
color=${this._compactView === view ? "primary" : "neutral"}
|
|
682
|
+
@click=${() => this._switchView(view)}
|
|
683
|
+
>
|
|
684
|
+
${label}${badge && badge > 0 ? html`<span class="console-badge">${badge}</span>` : ""}
|
|
685
|
+
</sigvelo-button>
|
|
686
|
+
`;
|
|
687
|
+
return html`
|
|
688
|
+
<div class="mobile-nav">
|
|
689
|
+
<sigvelo-button-group label="View toggle">
|
|
690
|
+
${btn("code", "Code")} ${btn("plots", "Plots")}
|
|
691
|
+
${btn("console", "Console", this._unreadConsoleCount)}
|
|
692
|
+
</sigvelo-button-group>
|
|
693
|
+
<sigvelo-button
|
|
694
|
+
size="xs"
|
|
695
|
+
color="primary"
|
|
696
|
+
icon-label="Run"
|
|
697
|
+
?disabled=${!this._webRReady || this._running}
|
|
698
|
+
@click=${() => this._runCode()}
|
|
699
|
+
>
|
|
700
|
+
${ICON_PLAY}
|
|
701
|
+
</sigvelo-button>
|
|
702
|
+
</div>
|
|
703
|
+
`;
|
|
704
|
+
}
|
|
705
|
+
render() {
|
|
706
|
+
if (this._compact) return html`
|
|
707
|
+
${this._renderLoadingOverlay()} ${this._renderMobileNav()}
|
|
708
|
+
<div class="single-pane">
|
|
709
|
+
${this._compactView === "code" ? this._renderEditorPane() : this._compactView === "plots" ? this._renderPlotViewer() : this._renderConsoleView()}
|
|
710
|
+
</div>
|
|
711
|
+
`;
|
|
712
|
+
return html`
|
|
713
|
+
${this._renderLoadingOverlay()}
|
|
714
|
+
<div class="content">
|
|
715
|
+
<sigvelo-splitter
|
|
716
|
+
orientation="vertical"
|
|
717
|
+
position="0.5"
|
|
718
|
+
style="--divider-min-position: 20%; --divider-max-position: 80%;"
|
|
719
|
+
>
|
|
720
|
+
${this._renderEditorPane("start")} ${this._renderRightPane("end")}
|
|
721
|
+
</sigvelo-splitter>
|
|
722
|
+
</div>
|
|
723
|
+
`;
|
|
724
|
+
}
|
|
725
|
+
};
|
|
726
|
+
__decorate([property()], SigveloREditor.prototype, "code", void 0);
|
|
727
|
+
__decorate([property({ type: Array })], SigveloREditor.prototype, "packages", void 0);
|
|
728
|
+
__decorate([property({ attribute: "editor-label" })], SigveloREditor.prototype, "editorLabel", void 0);
|
|
729
|
+
__decorate([state()], SigveloREditor.prototype, "_editedCode", void 0);
|
|
730
|
+
__decorate([state()], SigveloREditor.prototype, "_consoleLogs", void 0);
|
|
731
|
+
__decorate([state()], SigveloREditor.prototype, "_plots", void 0);
|
|
732
|
+
__decorate([state()], SigveloREditor.prototype, "_currentPlotIndex", void 0);
|
|
733
|
+
__decorate([state()], SigveloREditor.prototype, "_webRReady", void 0);
|
|
734
|
+
__decorate([state()], SigveloREditor.prototype, "_webRLoading", void 0);
|
|
735
|
+
__decorate([state()], SigveloREditor.prototype, "_loadingMessage", void 0);
|
|
736
|
+
__decorate([state()], SigveloREditor.prototype, "_running", void 0);
|
|
737
|
+
__decorate([state()], SigveloREditor.prototype, "_compact", void 0);
|
|
738
|
+
__decorate([state()], SigveloREditor.prototype, "_compactView", void 0);
|
|
739
|
+
__decorate([state()], SigveloREditor.prototype, "_rightPane", void 0);
|
|
740
|
+
__decorate([state()], SigveloREditor.prototype, "_unreadConsoleCount", void 0);
|
|
741
|
+
customElements.define("sigvelo-r-editor", SigveloREditor);
|
|
742
|
+
//#endregion
|
|
743
|
+
export { SigveloREditor as t };
|