-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathBlockPropertiesDialog.svelte
More file actions
484 lines (433 loc) · 13.5 KB
/
Copy pathBlockPropertiesDialog.svelte
File metadata and controls
484 lines (433 loc) · 13.5 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<script lang="ts">
import { onDestroy } from 'svelte';
import { graphStore } from '$lib/stores/graph';
import { historyStore } from '$lib/stores/history';
import { nodeDialogStore, closeNodeDialog } from '$lib/stores/nodeDialog';
import { nodeRegistry, type NodeInstance } from '$lib/nodes';
import { generateBlockCode } from '$lib/pyodide/pathsimRunner';
import { codeContextStore } from '$lib/stores/codeContext';
import { generateBlockCodeHeader } from '$lib/utils/codePreviewHeader';
import { getDocstring } from '$lib/nodes/docstrings';
import { themeStore, type Theme } from '$lib/stores/theme';
import { tooltip } from '$lib/components/Tooltip.svelte';
import { paramInput } from '$lib/actions/paramInput';
import { loadCodeMirrorModules, createEditorExtensions, type CodeMirrorModules } from '$lib/utils/codemirror';
import ColorPicker from './shared/ColorPicker.svelte';
import DialogShell from './shared/DialogShell.svelte';
import DocumentationSection from './shared/DocumentationSection.svelte';
import Icon from '$lib/components/icons/Icon.svelte';
import { DEFAULT_NODE_COLOR } from '$lib/utils/colors';
import { exportComponent } from '$lib/schema/componentOps';
import { NODE_TYPES } from '$lib/constants/nodeTypes';
import { exportRecordingData } from '$lib/utils/csvExport';
import { createRecordingDataState } from '$lib/stores/recordingData.svelte';
// Code preview state (declared early — referenced by subscription below)
let showCode = $state(false);
let previewCode = $state('');
let editorContainer = $state<HTMLDivElement | undefined>(undefined);
let editorView: import('@codemirror/view').EditorView | null = null;
let cmModules: CodeMirrorModules | null = null;
let editorLoading = $state(true);
let copied = $state(false);
let currentTheme = $state<Theme>('dark');
function destroyEditor() {
if (editorView) {
editorView.destroy();
editorView = null;
}
}
// Get the node from store
let nodeId = $state<string | null>(null);
let node = $state<NodeInstance | null>(null);
const unsubscribeDialog = nodeDialogStore.subscribe((id) => {
nodeId = id;
if (id) {
node = graphStore.getNode(id) || null;
// Reset to properties view when opening a new node
showCode = false;
destroyEditor();
} else {
node = null;
showCode = false;
destroyEditor();
}
});
// Keep node updated when params change
const unsubscribeNodes = graphStore.nodesArray.subscribe((nodes) => {
if (nodeId) {
node = nodes.find(n => n.id === nodeId) || null;
}
});
const unsubscribeTheme = themeStore.subscribe((theme) => {
currentTheme = theme;
if (editorView && cmModules && editorContainer) {
recreateEditor();
}
});
onDestroy(() => {
unsubscribeDialog();
unsubscribeNodes();
unsubscribeTheme();
recordingData.destroy();
destroyEditor();
});
// Get type definition
const typeDef = $derived(node ? nodeRegistry.get(node.type) : null);
// Get docstring for current node type
const docstringHtml = $derived(typeDef?.blockClass ? getDocstring(typeDef.blockClass) : undefined);
// Get current color for display
const currentColor = $derived(node?.color || DEFAULT_NODE_COLOR);
// Handle color selection
function handleColorSelect(color: string | undefined) {
if (!node) return;
const nodeId = node.id;
historyStore.mutate(() => graphStore.updateNodeColor(nodeId, color));
}
function getExtensions() {
if (!cmModules) return [];
return createEditorExtensions(cmModules, currentTheme === 'dark', {
readOnly: true
});
}
function recreateEditor() {
if (!editorView || !editorContainer || !cmModules) return;
const currentCode = editorView.state.doc.toString();
editorView.destroy();
editorView = new cmModules.EditorView({
doc: currentCode,
extensions: getExtensions(),
parent: editorContainer
});
}
async function initEditor() {
if (!editorContainer) return;
destroyEditor();
editorLoading = true;
cmModules = await loadCodeMirrorModules();
editorView = new cmModules.EditorView({
doc: previewCode,
extensions: getExtensions(),
parent: editorContainer
});
editorLoading = false;
}
// Toggle code view
function toggleCodeView() {
if (showCode) {
showCode = false;
destroyEditor();
} else {
if (!node) return;
const allNodes = graphStore.getAllNodes();
const allConnections = graphStore.getAllConnections().map(c => c.connection);
const codeContext = codeContextStore.getCode();
const header = generateBlockCodeHeader(node, codeContext);
const blockCode = generateBlockCode(node, allNodes, allConnections);
previewCode = header + blockCode;
copied = false;
showCode = true;
setTimeout(() => initEditor(), 0);
}
}
function copyToClipboard() {
navigator.clipboard.writeText(previewCode);
copied = true;
setTimeout(() => (copied = false), 2000);
}
// Export block/subsystem
function handleExport() {
if (!node) return;
const type = node.type === NODE_TYPES.SUBSYSTEM ? 'subsystem' : 'block';
exportComponent(type, node.id);
}
// Check if node can be exported (not Interface blocks)
const canExport = $derived(node?.type !== NODE_TYPES.INTERFACE);
// Check if node is a recording node (Scope or Spectrum)
const isRecordingNode = $derived(node?.type === 'Scope' || node?.type === 'Spectrum');
// Shared recording data state (for CSV export button)
const recordingData = createRecordingDataState();
// Reactively check if exportable data exists
const hasData = $derived(
nodeId && node && isRecordingNode
? recordingData.hasData(nodeId, node.type)
: false
);
// Export recording data to CSV
function handleExportCsv() {
if (!node || !nodeId) return;
exportRecordingData(nodeId, node.name, node.type);
}
// Handle parameter change
// All values are stored as raw Python expressions (strings)
// No parsing or type coercion - user writes valid Python syntax
function handleParamChange(paramName: string, value: string) {
if (!node) return;
const nodeId = node.id;
historyStore.mutate(() => graphStore.updateNodeParams(nodeId, { [paramName]: value }));
}
// Check if a parameter is pinned to the node
function isParamPinned(paramName: string): boolean {
return node?.pinnedParams?.includes(paramName) ?? false;
}
// Toggle pin state for a parameter
function togglePinParam(paramName: string) {
if (!node) return;
const nodeId = node.id;
const currentPinned = node.pinnedParams ?? [];
const newPinned = currentPinned.includes(paramName)
? currentPinned.filter(p => p !== paramName)
: [...currentPinned, paramName];
historyStore.mutate(() => graphStore.updateNode(nodeId, { pinnedParams: newPinned }));
}
// Handle name change
function handleNameChange(name: string) {
if (!node) return;
const nodeId = node.id;
historyStore.mutate(() => graphStore.updateNodeName(nodeId, name));
}
// Format value for display
function formatValue(value: unknown): string {
if (value === null || value === undefined) return '';
if (typeof value === 'object') return JSON.stringify(value);
return String(value);
}
// Format default value for placeholder (Python style)
function formatDefault(value: unknown): string {
if (value === null || value === undefined) return 'None';
if (typeof value === 'object') return JSON.stringify(value);
return String(value);
}
</script>
<DialogShell
open={!!(nodeId && node && typeDef)}
onClose={closeNodeDialog}
ariaLabelledby="dialog-title"
dataTour="dialog-properties"
dialogClass="properties-dialog glass-panel"
dialogStyle="--node-color: {currentColor};"
>
{#if nodeId && node && typeDef}
<div class="dialog-header">
{#if showCode}
<span id="dialog-title">Python Code</span>
{:else}
<div class="node-info">
<input
id="dialog-title"
class="node-name-input"
data-tour="block-name-input"
type="text"
value={node.name}
oninput={(e) => handleNameChange(e.currentTarget.value)}
use:paramInput
/>
<span class="node-type">{typeDef.name}</span>
</div>
{/if}
<div class="header-actions">
{#if showCode}
<!-- Copy button in code view -->
<button
class="icon-btn"
class:success={copied}
onclick={copyToClipboard}
use:tooltip={copied ? "Copied!" : "Copy to Clipboard"}
aria-label="Copy to Clipboard"
>
{#if copied}
<Icon name="check" size={16} />
{:else}
<Icon name="copy" size={16} />
{/if}
</button>
{:else}
<!-- Color picker -->
<ColorPicker
currentColor={currentColor}
defaultColor={DEFAULT_NODE_COLOR}
onSelect={handleColorSelect}
/>
<!-- CSV Export button for recording nodes -->
{#if isRecordingNode}
<button
class="icon-btn"
onclick={handleExportCsv}
disabled={!hasData}
use:tooltip={hasData ? "Export CSV" : "Run simulation to export data"}
aria-label="Export CSV"
>
<Icon name="table" size={16} />
</button>
{/if}
<!-- Export button -->
{#if canExport}
<button
class="icon-btn"
onclick={handleExport}
use:tooltip={"Export"}
aria-label="Export"
>
<Icon name="upload" size={16} />
</button>
{/if}
{/if}
<!-- Toggle code view button -->
<button
class="icon-btn"
onclick={toggleCodeView}
use:tooltip={showCode ? "View Properties" : "View Python Code"}
aria-label={showCode ? "View Properties" : "View Python Code"}
>
<Icon name={showCode ? "settings" : "braces"} size={16} />
</button>
<button class="icon-btn" onclick={closeNodeDialog} aria-label="Close">
<Icon name="x" size={16} />
</button>
</div>
</div>
<div class="dialog-body" class:flush={showCode}>
{#if showCode}
<!-- Code view -->
<div class="code-preview" bind:this={editorContainer}>
{#if editorLoading}
<div class="loading">Loading...</div>
{/if}
</div>
{:else}
<!-- Parameters -->
{#if typeDef.params.length > 0}
<div class="section">
<div class="section-title">Parameters</div>
<div class="params-grid">
{#each typeDef.params as param}
{@const value = node.params[param.name]}
{@const pinned = isParamPinned(param.name)}
<div class="param-item">
<label for="param-{param.name}">
<span>{param.name}</span>
{#if param.description}
<span class="help-icon" use:tooltip={param.description}>?</span>
{/if}
</label>
<div class="param-input-row">
{#if param.options && param.options.length > 0}
<select
id="param-{param.name}"
value={formatValue(value)}
onchange={(e) => handleParamChange(param.name, e.currentTarget.value)}
>
{#each param.options as option}
<option value={option}>{option}</option>
{/each}
</select>
{:else}
<input
id="param-{param.name}"
type="text"
value={formatValue(value)}
placeholder={formatDefault(param.default)}
oninput={(e) => handleParamChange(param.name, e.currentTarget.value)}
use:paramInput
/>
{/if}
<!-- Pin toggle button -->
<button
class="pin-btn"
class:pinned
data-tour="block-pin-btn"
onclick={() => togglePinParam(param.name)}
use:tooltip={pinned ? "Unpin from node" : "Pin to node"}
aria-label={pinned ? "Unpin from node" : "Pin to node"}
>
<Icon name={pinned ? 'pin-filled' : 'pin'} size={14} />
</button>
</div>
</div>
{/each}
</div>
</div>
{:else}
<div class="no-params">No configurable parameters</div>
{/if}
<!-- Documentation section (lazy loaded) -->
<DocumentationSection docstringHtml={docstringHtml} />
{/if}
</div>
{#if !showCode}
<div class="dialog-footer">
<span class="hint">R rotate · X flip horizontal · Y flip vertical</span>
</div>
{/if}
{/if}
</DialogShell>
<style>
/* Uses global .properties-dialog styles from app.css */
/* Block-specific: param input row with pin button */
.param-input-row {
display: flex;
align-items: center;
gap: var(--space-xs);
}
.param-input-row > :first-child {
flex: 1;
min-width: 0;
}
/* Pin button */
.pin-btn {
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
border: none;
border-radius: var(--radius-sm);
background: transparent;
color: var(--text-muted);
cursor: pointer;
transition: all var(--transition-fast);
}
.pin-btn :global(svg) {
display: block;
flex-shrink: 0;
}
.pin-btn:hover {
background: var(--surface-hover);
color: var(--text-muted);
}
.pin-btn.pinned {
color: var(--node-color);
}
.pin-btn.pinned:hover {
color: var(--node-color);
filter: brightness(1.2);
}
/* Remove padding when showing code editor */
.dialog-body.flush {
padding: 0;
border-radius: 0 0 var(--radius-lg) var(--radius-lg);
overflow: hidden;
}
/* Code preview */
.code-preview {
overflow: hidden;
max-height: 600px;
}
.code-preview :global(.cm-editor) {
max-height: 600px;
}
.code-preview :global(.cm-scroller) {
overflow: auto;
}
.code-preview :global(.cm-content) {
padding: 0;
}
.loading {
display: flex;
align-items: center;
justify-content: center;
height: 100px;
color: var(--text-muted);
font-size: 12px;
}
</style>