-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathStepEditor.tsx
More file actions
1063 lines (980 loc) · 45.8 KB
/
Copy pathStepEditor.tsx
File metadata and controls
1063 lines (980 loc) · 45.8 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import React, { useEffect, useState } from 'react';
import { CATEGORIZED_ACTIONS, THEMES } from '../../types/demo';
import { getFieldsForAction, getRequiredFields } from '../../utils/actionHelpers';
import { validateStep } from '../../utils/validation';
import { SearchableDropdown } from '../ui/SearchableDropdown';
import { ComboBox } from '../ui/ComboBox';
import { PathInput } from '../ui/PathInput';
import { messageHandler } from '@estruyf/vscode/dist/client';
import { Switch } from '../ui/Switch';
import { SnippetArguments } from './SnippetArguments';
import { DemoIdPicker } from '../ui/DemoIdPicker';
import { Action, COMMAND, EngageTimeMessageType, Step, WebViewMessages } from '@demotime/common';
import { PollIdPicker } from './PollIdPicker';
import { useDemoConfigContext } from '../../hooks';
import { VSCodeCommandPicker } from './VSCodeCommandPicker';
import { InsertTypingModePicker } from '../ui/InsertTypingModePicker';
import { SettingField } from './SettingField';
import { StateField } from './StateField';
import { NoteBox } from '../ui/NoteBox';
import { KeybindingPicker } from './KeybindingPicker';
interface StepEditorProps {
step: Step;
onChange: (step: Step) => void;
}
export const StepEditor: React.FC<StepEditorProps> = ({ step, onChange }) => {
// Local state for id field (ExecuteScript only)
const [localId, setLocalId] = React.useState(step.id || '');
React.useEffect(() => {
setLocalId(step.id || '');
}, [step.id]);
const commitId = () => {
if (localId !== step.id) {
handleChange('id', localId || undefined);
}
};
const availableFields = getFieldsForAction(step?.action);
const requiredFields = getRequiredFields(step?.action);
const stepValidation = validateStep(step);
const { config } = useDemoConfigContext();
// State for fetched themes
const [fetchedThemes, setFetchedThemes] = useState<string[]>([]);
const [loadingThemes, setLoadingThemes] = useState(false);
const [themeError, setThemeError] = useState<string | null>(null);
// State for available snippet files (used in contentPath suggestions)
const [snippetSuggestions, setSnippetSuggestions] = useState<{ label: string; path: string; description?: string }[]>([]);
const [loadingSnippetSuggestions, setLoadingSnippetSuggestions] = useState(false);
useEffect(() => {
setLoadingSnippetSuggestions(true);
messageHandler
.request<{ label: string; path: string; description?: string }[]>(
WebViewMessages.toVscode.configEditor.listSnippets,
)
.then((data) => {
setSnippetSuggestions(Array.isArray(data) ? data : []);
})
.catch(() => setSnippetSuggestions([]))
.finally(() => setLoadingSnippetSuggestions(false));
}, []);
useEffect(() => {
const fetchThemes = async () => {
setLoadingThemes(true);
setThemeError(null);
try {
messageHandler.request<string[]>(WebViewMessages.toVscode.configEditor.getThemes).then((data) => {
setFetchedThemes(Array.isArray(data) ? data : THEMES);
});
} catch (err) {
console.log('Error fetching themes:', (err as Error).message);
setFetchedThemes(THEMES);
} finally {
setLoadingThemes(false);
}
};
fetchThemes();
}, []);
// Parse position into separate fields
const parsePosition = (position?: string | number) => {
if (!position) { return { startLine: '', startChar: '', endLine: '', endChar: '' }; }
const pos = position.toString().trim();
// Handle special cases
if (pos === 'start' || pos === 'end') {
return { startLine: pos, startChar: '', endLine: '', endChar: '' };
}
// Handle full range: "10,5:20,10"
const fullRangeMatch = pos.match(/^(\d+),(\d+):(\d+),(\d+)$/);
if (fullRangeMatch) {
return {
startLine: fullRangeMatch[1],
startChar: fullRangeMatch[2],
endLine: fullRangeMatch[3],
endChar: fullRangeMatch[4]
};
}
// Handle line range: "10:20"
const lineRangeMatch = pos.match(/^(\d+):(\d+)$/);
if (lineRangeMatch) {
return {
startLine: lineRangeMatch[1],
startChar: '',
endLine: lineRangeMatch[2],
endChar: ''
};
}
// Handle line,character: "10,5"
const lineCharMatch = pos.match(/^(\d+),(\d+)$/);
if (lineCharMatch) {
return {
startLine: lineCharMatch[1],
startChar: lineCharMatch[2],
endLine: '',
endChar: ''
};
}
// Handle single line: "10"
if (/^\d+$/.test(pos)) {
return {
startLine: pos,
startChar: '',
endLine: '',
endChar: ''
};
}
return { startLine: pos, startChar: '', endLine: '', endChar: '' };
};
// Combine position fields into position string
const combinePosition = (startLine: string, startChar: string, endLine: string, endChar: string): string | undefined => {
if (!startLine.trim()) { return undefined; }
const start = startLine.trim();
const startC = startChar.trim();
const end = endLine.trim();
const endC = endChar.trim();
// Handle special cases
if (start === 'start' || start === 'end') {
return start;
}
// Build position string based on what's filled
if (end && endC) {
// Full range: "10,5:20,10"
return `${start},${startC || '0'}:${end},${endC}`;
} else if (end) {
// Line range: "10:20"
return `${start}:${end}`;
} else if (startC) {
// Line,character: "10,5"
return `${start},${startC}`;
} else {
// Single line: "10"
return start;
}
};
// Validate position ranges
const validatePositionRange = (startLine: string, startChar: string, endLine: string, endChar: string): string | null => {
if (!startLine.trim()) { return null; }
const start = startLine.trim();
const startC = startChar.trim();
const end = endLine.trim();
const endC = endChar.trim();
// Skip validation for special cases
if (start === 'start' || start === 'end') { return null; }
// Validate that all values are numbers when provided
if (!/^\d+$/.test(start)) { return 'Start line must be a number'; }
if (startC && !/^\d+$/.test(startC)) { return 'Start character must be a number'; }
if (end && !/^\d+$/.test(end)) { return 'End line must be a number'; }
if (endC && !/^\d+$/.test(endC)) { return 'End character must be a number'; }
if (end) {
const startLineNum = parseInt(start);
const endLineNum = parseInt(end);
if (endLineNum < startLineNum) {
return 'End line cannot be lower than start line';
}
if (startC && endC && startLineNum === endLineNum) {
const startCharNum = parseInt(startC);
const endCharNum = parseInt(endC);
if (endCharNum < startCharNum) {
return 'End character cannot be lower than start character on the same line';
}
}
}
return null;
};
const handleChange = <K extends keyof Step>(field: K, value: Step[K]) => {
const newStep = { ...step };
if (value === '' || value === undefined) {
delete newStep[field];
} else {
newStep[field] = value;
}
// Special logic for highlightWholeLine based on position
if (field === 'position' && typeof value === 'string') {
// Full range: "10,5:20,10"
const fullRangeMatch = value.match(/^(\d+),(\d+):(\d+),(\d+)$/);
if (fullRangeMatch) {
newStep.highlightWholeLine = false;
} else if (typeof newStep.highlightWholeLine === 'undefined') {
// If not a full range and highlightWholeLine is undefined, default to true
newStep.highlightWholeLine = true;
}
}
onChange(newStep);
};
const handleSettingChange = (key: string, value: string | number | boolean | object | null | undefined) => {
handleChange('setting', { key, value: value === undefined ? null : value });
};
const handleStateChange = (key: string, value?: string) => {
const k = (key || '').trim();
const v = value === undefined ? undefined : value;
// If both key and value are empty/undefined, remove the state entirely
if (!k && v === undefined) {
handleChange('state', undefined);
return;
}
// Otherwise ensure we set the state object; default missing value to empty string to preserve previous behavior
handleChange('state', { key: k, value: v ?? '' });
};
// Keep EngageTime poll toggles mutually exclusive in a single update
const handleExclusivePollToggle = (field: 'startOnOpen' | 'closeOnOpen', checked: boolean) => {
const opposite = field === 'startOnOpen' ? 'closeOnOpen' : 'startOnOpen';
const updatedStep: Step = { ...step, [field]: checked } as Step;
if (checked) {
updatedStep[opposite] = false;
}
onChange(updatedStep);
};
// Handle position field changes
const handlePositionChange = (field: 'startLine' | 'startChar' | 'endLine' | 'endChar', value: string) => {
const currentPos = parsePosition(step.position);
const newPos = { ...currentPos, [field]: value };
const combinedPosition = combinePosition(newPos.startLine, newPos.startChar, newPos.endLine, newPos.endChar);
handleChange('position', combinedPosition);
};
const getQrFieldsForLayout = (layout: Step['qrLayout']): string[] => {
switch (layout) {
case 'minimal': return ['qrLayout', 'url'];
case 'stacked':
case 'text-left':
case 'text-right': return ['qrLayout', 'url', 'topText', 'title', 'description'];
default: return ['qrLayout', 'url', 'topText', 'title', 'description', 'logo'];
}
};
const renderField = (field: string) => {
if (step.action === Action.ShowQR && field !== 'action') {
const allowedQrFields = getQrFieldsForLayout(step.qrLayout);
if (!allowedQrFields.includes(field)) {
return null;
}
}
const isRequired = requiredFields.includes(field);
const fieldErrors = stepValidation.errors.filter(error => error.field === field || error[`field:${field}`] !== undefined);
const hasError = fieldErrors.length > 0;
let label = field.charAt(0).toUpperCase() + field.slice(1).replace(/([A-Z])/g, ' $1');
if (field === "slide") {
label = "Slide Number (1-based index)";
} else if (field === 'timeout') {
label = "Timeout (ms)";
} else if (field === 'zoom') {
label = "Zoom Level (times to use VS Code zoom)";
} else if (field === 'insertTypingSpeed') {
label = "Insert Typing Speed (ms)";
} else if (field === 'highlightBlur') {
label = "Highlight Blur (0-10px)";
} else if (field === 'highlightOpacity') {
label = "Highlight Opacity (0-1)";
} else if (step.action === Action.RunDemoById) {
label = "Scene ID";
} else if (step.action === Action.ExecuteScript && field === 'id') {
label = "Script ID";
} else if (field === 'openInVSCode') {
label = 'Open in VS Code';
} else if (field === 'startOnOpen') {
label = 'Start the poll when opening it';
} else if (field === 'closeOnOpen') {
label = 'Close the poll when opening it';
} else if (field === 'pollDarkTheme') {
label = 'Use dark theme';
} else if (field === 'pollControls') {
label = 'Show poll controls';
}
switch (field) {
case 'action':
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
Action {isRequired && <span className="text-red-500">*</span>}
</label>
<SearchableDropdown
value={step.action}
options={CATEGORIZED_ACTIONS}
onChange={(value) => handleChange('action', value as Action)}
placeholder="Select action..."
autoFocus={!step.action}
/>
{
step.action && step.action.toLowerCase().includes('engagetime') && (
<>
<NoteBox variant="warning" className="mt-2 space-y-1">
<>
<strong>Note:</strong> Make sure to set the EngageTime Session ID in the demo configuration.
<button
type="button"
className="bg-transparent p-0 m-0 border-none underline decoration-dotted hover:decoration-solid text-blue-700 dark:text-blue-300 cursor-pointer"
style={{ font: 'inherit' }}
onClick={(e) => {
e.preventDefault();
window.dispatchEvent(new CustomEvent('engagetime:open-config', {}));
}}
>
Open EngageTime Config
</button>
</>
</NoteBox>
{config.engageTime?.sessionId ? null : (
<NoteBox variant="error" className="mt-2">
<strong>Error:</strong> No EngageTime Session ID is set in the demo configuration.
</NoteBox>
)}
</>
)
}
</div>
);
case 'theme':
return (
<div key={field}>
<ComboBox
label="Theme"
required={isRequired}
value={step.theme || ''}
onChange={(value) => handleChange('theme', value || undefined)}
options={loadingThemes ? THEMES : (fetchedThemes.length > 0 ? fetchedThemes : THEMES)}
placeholder={loadingThemes ? 'Loading themes...' : 'Select or enter custom theme'}
error={themeError || (fieldErrors.length > 0 ? fieldErrors[0].message : undefined)}
/>
{themeError && (
<p className="text-sm text-red-600 dark:text-red-400 mt-1">{themeError}</p>
)}
</div>
);
case 'pollId':
return (
<div key={field}>
<PollIdPicker
label="Poll ID"
required={isRequired}
sessionId={config.engageTime?.sessionId || ''}
value={step.pollId || ''}
onChange={(value) => handleChange('pollId', value || undefined)}
error={fieldErrors.length > 0 ? fieldErrors[0].message : undefined}
/>
</div>
);
case 'type':
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
{label} {isRequired && <span className="text-red-500">*</span>}
</label>
<SearchableDropdown
value={step[field] || ''}
options={["demo", "slide", "custom"] as EngageTimeMessageType[]}
onChange={(value) => handleChange(field, value as typeof step.type)}
placeholder="Select type..."
/>
</div>
);
case 'qrLayout':
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
QR Layout {isRequired && <span className="text-red-500">*</span>}
</label>
<SearchableDropdown
value={step.qrLayout || 'default'}
options={['default', 'reversed', 'minimal', 'stacked', 'text-left', 'text-right']}
onChange={(value) => handleChange('qrLayout', value as Step['qrLayout'])}
placeholder="Select QR layout..."
/>
</div>
);
case 'insertTypingMode':
return (
<InsertTypingModePicker
key={field}
value={step.insertTypingMode}
action={step.action}
required={isRequired}
fieldErrors={fieldErrors}
onChange={(value) => handleChange('insertTypingMode', value)}
/>
);
case 'highlightWholeLine': {
// Disable the field if position is a full range (start line/char:end line/char)
const pos = step.position?.toString() || '';
const isFullRange = /^\d+,\d+:\d+,\d+$/.test(pos);
return (
<div key={field}>
<label className="h-full flex items-center space-x-2">
<input
type="checkbox"
checked={typeof step[field] === 'undefined' ? true : step[field]}
onChange={(e) => handleChange(field, e.target.checked)}
className="rounded-sm border-gray-300 text-blue-600 focus:ring-blue-500"
disabled={isFullRange}
/>
<span className="text-sm font-medium text-gray-600 dark:text-gray-300">
{label} {isRequired && <span className="text-red-500">*</span>}
{isFullRange && (
<span className="ml-2 text-xs text-gray-400">(Disabled for full range position)</span>
)}
</span>
</label>
</div>
);
}
case 'focusTop':
case 'overwrite':
case 'openInVSCode':
case 'autoExecute':
case 'showProgress':
return (
<div key={field}>
<label className="h-full flex items-center space-x-2">
<input
type="checkbox"
checked={typeof step[field] === 'undefined' ? true : step[field]}
onChange={(e) => handleChange(field, e.target.checked)}
className="rounded-sm border-gray-300 text-blue-600 focus:ring-blue-500"
/>
<span className="text-sm font-medium text-gray-600 dark:text-gray-300">
{label} {isRequired && <span className="text-red-500">*</span>}
</span>
</label>
</div>
);
case 'startOnOpen':
return (
<div key={field}>
<label className="h-full flex items-center space-x-2">
<input
type="checkbox"
checked={typeof step[field] === 'undefined' ? false : step[field]}
onChange={(e) => handleExclusivePollToggle('startOnOpen', e.target.checked)}
className="rounded-sm border-gray-300 text-blue-600 focus:ring-blue-500"
/>
<span className="text-sm font-medium text-gray-600 dark:text-gray-300">
{label} {isRequired && <span className="text-red-500">*</span>}
</span>
</label>
</div>
);
case 'closeOnOpen':
return (
<div key={field}>
<label className="h-full flex items-center space-x-2">
<input
type="checkbox"
checked={typeof step[field] === 'undefined' ? false : step[field]}
onChange={(e) => handleExclusivePollToggle('closeOnOpen', e.target.checked)}
className="rounded-sm border-gray-300 text-blue-600 focus:ring-blue-500"
/>
<span className="text-sm font-medium text-gray-600 dark:text-gray-300">
{label} {isRequired && <span className="text-red-500">*</span>}
</span>
</label>
</div>
);
case 'pollDarkTheme':
return (
<div key={field}>
<label className="h-full flex items-center space-x-2">
<input
type="checkbox"
checked={typeof step.pollDarkTheme === 'undefined' ? false : step.pollDarkTheme}
onChange={(e) => handleChange('pollDarkTheme', e.target.checked)}
className="rounded-sm border-gray-300 text-blue-600 focus:ring-blue-500"
/>
<span className="text-sm font-medium text-gray-600 dark:text-gray-300">
{label} {isRequired && <span className="text-red-500">*</span>}
</span>
</label>
</div>
);
case 'pollControls':
return (
<div key={field}>
<label className="h-full flex items-center space-x-2">
<input
type="checkbox"
checked={typeof step.pollControls === 'undefined' ? true : step.pollControls}
onChange={(e) => handleChange('pollControls', e.target.checked)}
className="rounded-sm border-gray-300 text-blue-600 focus:ring-blue-500"
/>
<span className="text-sm font-medium text-gray-600 dark:text-gray-300">
{label} {isRequired && <span className="text-red-500">*</span>}
</span>
</label>
</div>
);
case 'timeout':
case 'zoom':
case 'insertTypingSpeed':
case 'slide':
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
{label} {isRequired && <span className="text-red-500">*</span>}
</label>
<input
type="number"
value={typeof step[field] !== 'undefined' ? step[field] : ''}
onChange={(e) => handleChange(field, e.target.value ? parseInt(e.target.value) : undefined)}
className={`w-full px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${hasError ? 'border-red-300 bg-red-50 dark:border-red-400 dark:bg-red-900/20' : 'border-gray-300 dark:border-gray-600'
}`}
placeholder={`Enter ${label.toLowerCase()}`}
min={0}
/>
{fieldErrors.map((error, index) => (
<p key={index} className="text-sm text-red-600 dark:text-red-400 mt-1">{error.message}</p>
))}
</div>
);
case 'highlightBlur':
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
{label} {isRequired && <span className="text-red-500">*</span>}
</label>
<input
type="number"
value={typeof step[field] !== 'undefined' ? step[field] : ''}
onChange={(e) => handleChange(field, e.target.value ? parseFloat(e.target.value) : undefined)}
className={`w-full px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${hasError ? 'border-red-300 bg-red-50 dark:border-red-400 dark:bg-red-900/20' : 'border-gray-300 dark:border-gray-600'
}`}
placeholder="Enter blur value (0-10)"
min={0}
max={10}
step={1}
/>
{fieldErrors.map((error, index) => (
<p key={index} className="text-sm text-red-600 dark:text-red-400 mt-1">{error.message}</p>
))}
</div>
);
case 'highlightOpacity':
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
{label} {isRequired && <span className="text-red-500">*</span>}
</label>
<input
type="number"
value={typeof step[field] !== 'undefined' ? step[field] : ''}
onChange={(e) => handleChange(field, e.target.value ? parseFloat(e.target.value) : undefined)}
className={`w-full px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${hasError ? 'border-red-300 bg-red-50 dark:border-red-400 dark:bg-red-900/20' : 'border-gray-300 dark:border-gray-600'
}`}
placeholder="Enter opacity value (0-1)"
min={0}
max={1}
step={0.05}
/>
{fieldErrors.map((error, index) => (
<p key={index} className="text-sm text-red-600 dark:text-red-400 mt-1">{error.message}</p>
))}
</div>
);
case 'content':
case 'message':
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
{label} {isRequired && <span className="text-red-500">*</span>}
</label>
<textarea
value={step[field] || ''}
onChange={(e) => handleChange(field, e.target.value || undefined)}
rows={3}
className={`w-full px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${hasError ? 'border-red-300 bg-red-50 dark:border-red-400 dark:bg-red-900/20' : 'border-gray-300 dark:border-gray-600'
}`}
placeholder={`Enter ${label.toLowerCase()}`}
/>
{fieldErrors.map((error, index) => (
<p key={index} className="text-sm text-red-600 dark:text-red-400 mt-1">{error.message}</p>
))}
</div>
);
case 'setting':
return (
<div key={field}>
<SettingField
setting={step.setting}
onChange={handleSettingChange}
fieldErrors={fieldErrors}
isRequired={isRequired}
/>
</div>
);
case 'state':
return (
<div key={field}>
<StateField
state={step.state}
onChange={handleStateChange}
fieldErrors={fieldErrors}
isRequired={isRequired}
/>
</div>
);
case 'keybinding':
return (
<div key={field}>
<KeybindingPicker
value={step.keybinding || ''}
onChange={(value) => handleChange('keybinding', value || undefined)}
required={isRequired}
error={fieldErrors.length > 0 ? fieldErrors[0].message : undefined}
/>
</div>
);
case 'args':
return (
<div key={field} className='col-span-1 md:col-span-2'>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
Arguments {isRequired && <span className="text-red-500">*</span>}
{step.action !== 'snippet' && (
<span className="text-xs text-gray-600 dark:text-gray-400 block mt-1">
For VS Code commands: JSON object/array. For scripts: positional args array
</span>
)}
</label>
{step.action === 'snippet' && step.contentPath ? (
<SnippetArguments
path={step.contentPath}
args={typeof step.args === 'object' && step.args !== null ? step.args : undefined}
onChange={(newArgs) => handleChange('args', newArgs)}
/>
) : (
<textarea
value={typeof step.args === 'string' ? step.args : JSON.stringify(step.args, null, 2)}
onChange={(e) => {
try {
const parsed = JSON.parse(e.target.value);
handleChange('args', parsed);
} catch {
handleChange('args', e.target.value);
}
}}
rows={3}
className={`w-full px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${hasError ? 'border-red-300 bg-red-50 dark:border-red-400 dark:bg-red-900/20' : 'border-gray-300 dark:border-gray-600'
}`}
placeholder="Enter arguments (JSON or string)"
/>
)}
{fieldErrors.map((error, index) => (
<p key={index} className="text-sm text-red-600 dark:text-red-400 mt-1">{error.message}</p>
))}
</div>
);
case 'waitForMessage':
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
Wait For Message
<span className="text-xs text-gray-600 dark:text-gray-400 block mt-1">
Optional: wait until stdout contains this string before advancing
</span>
</label>
<input
type="text"
value={step.waitForMessage || ''}
onChange={(e) => handleChange('waitForMessage', e.target.value || undefined)}
className={`w-full px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${hasError ? 'border-red-300 bg-red-50 dark:border-red-400 dark:bg-red-900/20' : 'border-gray-300 dark:border-gray-600'}`}
placeholder="e.g. done"
/>
{fieldErrors.map((error, index) => (
<p key={index} className="text-sm text-red-600 dark:text-red-400 mt-1">{error.message}</p>
))}
</div>
);
case 'command': {
// Different placeholders based on action type
const commandPlaceholder = step.action === 'executeTerminalCommand'
? 'Enter terminal command (e.g., npm install)'
: step.action === 'executeScript'
? 'Enter script command (e.g., node, bash, python)'
: step.action === 'executeVSCodeCommand'
? 'Enter VS Code command ID (e.g., workbench.action.files.save)'
: 'Enter command';
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
Command {isRequired && <span className="text-red-500">*</span>}
</label>
{
step.action === 'executeVSCodeCommand' ? (
<VSCodeCommandPicker
value={step.command || ''}
onChange={(value) => handleChange('command', value || undefined)}
placeholder={commandPlaceholder}
error={fieldErrors.length > 0 ? fieldErrors[0].message : undefined}
/>
) : (
<>
<input
type="text"
value={step[field] || ''}
onChange={(e) => handleChange(field, e.target.value || undefined)}
className={`w-full px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${hasError ? 'border-red-300 bg-red-50 dark:border-red-400 dark:bg-red-900/20' : 'border-gray-300 dark:border-gray-600'
}`}
placeholder={commandPlaceholder}
/>
{fieldErrors.map((error, index) => (
<p key={index} className="text-sm text-red-600 dark:text-red-400 mt-1">{error.message}</p>
))}
</>
)
}
</div>
);
}
case 'position': {
const positionFields = parsePosition(step.position);
const positionError = validatePositionRange(positionFields.startLine, positionFields.startChar, positionFields.endLine, positionFields.endChar);
return (
<div key={field} className="space-y-2">
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
Position {isRequired && <span className="text-red-500">*</span>}
</label>
<div className="bg-gray-50 dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg p-4 flex flex-col gap-4">
<div className="grid grid-cols-2 gap-4">
<div className="flex flex-col gap-1">
<label className="text-xs font-medium text-gray-600 dark:text-gray-200 mb-1" htmlFor="position-start-line">Start Line</label>
<input
id="position-start-line"
type="text"
value={positionFields.startLine}
onChange={(e) => handlePositionChange('startLine', e.target.value)}
className={`px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${positionError ? 'border-red-300 dark:border-red-400' : 'border-gray-300 dark:border-gray-600'}`}
placeholder="10 or start"
/>
</div>
<div className="flex flex-col gap-1">
<label className="text-xs font-medium text-gray-600 dark:text-gray-200 mb-1" htmlFor="position-start-char">Start Character</label>
<input
id="position-start-char"
type="text"
value={positionFields.startChar}
onChange={(e) => handlePositionChange('startChar', e.target.value)}
className={`px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${positionError ? 'border-red-300 dark:border-red-400' : 'border-gray-300 dark:border-gray-600'}`}
placeholder="5 (optional)"
/>
</div>
<div className="flex flex-col gap-1">
<label className="text-xs font-medium text-gray-600 mb-1" htmlFor="position-end-line">End Line</label>
<input
id="position-end-line"
type="text"
value={positionFields.endLine}
onChange={(e) => handlePositionChange('endLine', e.target.value)}
className={`px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${positionError ? 'border-red-300 dark:border-red-400' : 'border-gray-300 dark:border-gray-600'}`}
placeholder="20 (optional)"
/>
</div>
<div className="flex flex-col gap-1">
<label className="text-xs font-medium text-gray-600 mb-1" htmlFor="position-end-char">End Character</label>
<input
id="position-end-char"
type="text"
value={positionFields.endChar}
onChange={(e) => handlePositionChange('endChar', e.target.value)}
className={`px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${positionError ? 'border-red-300 dark:border-red-400' : 'border-gray-300 dark:border-gray-600'}`}
placeholder="10 (optional)"
/>
</div>
</div>
{positionError && (
<p className="text-sm text-red-600 dark:text-red-400">{positionError}</p>
)}
<div className="bg-blue-50 dark:bg-gray-800 border border-blue-100 dark:border-blue-900 rounded-lg p-3">
<p className="text-sm text-blue-800 dark:text-blue-200 mb-2">
<strong>Position Examples:</strong>
</p>
<div className="text-xs text-blue-700 dark:text-blue-200 space-y-1">
<p><code className="px-1 rounded-xs">10</code> - Line 10 (Start Line only)</p>
<p><code className="px-1 rounded-xs">10:20</code> - Lines 10 to 20 (Start + End Line)</p>
<p><code className="px-1 rounded-xs">10,5</code> - Line 10, character 5 (Start Line + Character)</p>
<p><code className="px-1 rounded-xs">10,5:20,10</code> - From line 10, char 5 to line 20, char 10 (All fields)</p>
<p><code className="px-1 rounded-xs">start</code> or <code className="px-1 rounded-xs">end</code> - Special keywords (Start Line only)</p>
</div>
</div>
</div>
</div>
);
}
case 'startPlaceholder': {
// Handle both startPlaceholder and endPlaceholder together
if (availableFields.includes('endPlaceholder')) {
const startError = stepValidation.errors.find(error => error.field === 'startPlaceholder');
const endError = stepValidation.errors.find(error => error.field === 'endPlaceholder');
return (
<div key="placeholders" className="space-y-2">
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
Placeholders {(requiredFields.includes('startPlaceholder') || requiredFields.includes('endPlaceholder')) && <span className="text-red-500">*</span>}
</label>
<div className="bg-gray-50 dark:bg-gray-900 border border-gray-200 dark:border-gray-700 rounded-lg p-4 flex flex-col gap-4">
<div className="grid grid-cols-1 gap-4">
<div className="flex flex-col gap-1">
<label className="text-xs font-medium text-gray-600 dark:text-gray-200 mb-1" htmlFor="start-placeholder">Start Placeholder</label>
<input
id="start-placeholder"
type="text"
value={step.startPlaceholder || ''}
onChange={(e) => handleChange('startPlaceholder', e.target.value || undefined)}
className={`px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${startError ? 'border-red-300 dark:border-red-400' : 'border-gray-300 dark:border-gray-600'}`}
placeholder="<!-- START -->"
/>
{startError && (
<p className="text-sm text-red-600 dark:text-red-400">{startError.message}</p>
)}
</div>
<div className="flex flex-col gap-1">
<label className="text-xs font-medium text-gray-600 dark:text-gray-200 mb-1" htmlFor="end-placeholder">End Placeholder</label>
<input
id="end-placeholder"
type="text"
value={step.endPlaceholder || ''}
onChange={(e) => handleChange('endPlaceholder', e.target.value || undefined)}
className={`px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${endError ? 'border-red-300 dark:border-red-400' : 'border-gray-300 dark:border-gray-600'}`}
placeholder="<!-- END -->"
/>
{endError && (
<p className="text-sm text-red-600 dark:text-red-400">{endError.message}</p>
)}
</div>
</div>
</div>
</div>
);
}
// Fall through to default case if endPlaceholder is not available
return null;
}
case 'id': {
if (step.action === Action.ExecuteScript) {
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
{label} {isRequired && <span className="text-red-500">*</span>}
</label>
<input
type="text"
value={localId}
onChange={e => setLocalId(e.target.value)}
onBlur={commitId}
onKeyDown={e => { if (e.key === 'Enter') { commitId(); } }}
className={`w-full px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${hasError ? 'border-red-300 bg-red-50 dark:border-red-400 dark:bg-red-900/20' : 'border-gray-300 dark:border-gray-600'}`}
placeholder={`Enter script ID`}
/>
{fieldErrors.map((error, index) => (
<p key={index} className="text-sm text-red-600 dark:text-red-400 mt-1">{error.message}</p>
))}
</div>
);
}
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
{label} {isRequired && <span className="text-red-500">*</span>}
</label>
<DemoIdPicker
value={step[field as keyof Step] || ''}
onDemoSelect={(demo) => handleChange(field as keyof Step, demo.id)}
placeholder="Select a demo..."
/>
{fieldErrors.map((error, index) => (
<p key={index} className="text-sm text-red-600 dark:text-red-400 mt-1">{error.message}</p>
))}
</div>
);
}
default:
// Handle path fields with file picker
if (field === 'path' || field === 'contentPath' || field === 'dest') {
const fileTypes = step.action === 'openSlide' ? ['md'] : undefined;
const isSnippetContentPath = field === 'contentPath' && step.action === 'snippet';
return (
<div key={field}>
<PathInput
label={label === "Dest" ? "Destination Path" : label}
required={isRequired}
value={step[field] || ''}
onChange={(value) => handleChange(field, value || undefined)}
placeholder={`Enter ${label.toLowerCase()}`}
error={fieldErrors.length > 0 ? fieldErrors[0].message : undefined}
type={field === 'contentPath' ? 'file' : 'file'}
fileTypes={fileTypes}
suggestions={isSnippetContentPath ? snippetSuggestions : undefined}
loadingSuggestions={isSnippetContentPath ? loadingSnippetSuggestions : false}
/>
{isSnippetContentPath && (
<div className="mt-2 flex justify-end">
<button
type="button"
onClick={() => messageHandler.send(WebViewMessages.toVscode.runCommand, { command: COMMAND.showGallery })}
className="text-xs text-demo-time-accent hover:underline"
>
Open Snippet Gallery
</button>
</div>
)}
</div>
);
}
return (
<div key={field}>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
{label} {isRequired && <span className="text-red-500">*</span>}
</label>
<input
type="text"
value={step[field as keyof Step] || ''}
onChange={(e) => handleChange(field as keyof Step, e.target.value || undefined)}
className={`w-full px-3 py-2 border rounded-md focus:outline-hidden focus:ring-2 focus:ring-demo-time-accent focus:border-demo-time-accent bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${hasError ? 'border-red-300 bg-red-50 dark:border-red-400 dark:bg-red-900/20' : 'border-gray-300 dark:border-gray-600'
}`}
placeholder={`Enter ${label.toLowerCase()}`}