Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

embed-script.js 44 KB

You have to be logged in to leave a comment. Sign In
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
  1. class EmbedDesigner {
  2. constructor() {
  3. this.params = this.parseURLParams();
  4. this.config = this.getInitialConfig();
  5. this.initDesignerMode();
  6. }
  7. parseURLParams() {
  8. const urlParams = new URLSearchParams(window.location.search);
  9. const params = {};
  10. for (const [key, value] of urlParams.entries()) {
  11. params[key] = decodeURIComponent(value);
  12. }
  13. return params;
  14. }
  15. getInitialConfig() {
  16. // Check if we have URL parameters
  17. const hasUrlParams = Object.keys(this.params).length > 0;
  18. // If URL params exist, prioritize them over saved config
  19. if (hasUrlParams) {
  20. // Start with defaults, then apply saved config, then override with URL params
  21. const savedConfig = this.loadFromLocalStorage() || {};
  22. return {
  23. prompt: this.params.prompt || savedConfig.prompt || '',
  24. context: this.params.context ? this.params.context.split(',').map(c => c.trim()) : (savedConfig.context || []),
  25. model: this.params.model || savedConfig.model || 'gpt-4o',
  26. mode: this.params.agentMode || savedConfig.mode || 'chat',
  27. thinking: this.params.thinking === 'true' ? true : (this.params.thinking === 'false' ? false : (savedConfig.thinking || false)),
  28. max: this.params.max === 'true' ? true : (this.params.max === 'false' ? false : (savedConfig.max || false)),
  29. lightColor: this.params.lightColor || savedConfig.lightColor || '#3b82f6',
  30. darkColor: this.params.darkColor || savedConfig.darkColor || '#60a5fa',
  31. height: this.params.height || savedConfig.height || '400',
  32. themeMode: this.params.themeMode || savedConfig.themeMode || 'auto',
  33. filetree: this.params.filetree ? decodeURIComponent(this.params.filetree).split('\n').filter(f => f.trim()) : (savedConfig.filetree || []),
  34. showFiletree: savedConfig.showFiletree !== undefined ? savedConfig.showFiletree : true,
  35. showDiff: this.params.showDiff === 'true' || savedConfig.showDiff || false,
  36. diffFilename: this.params.diffFilename || savedConfig.diffFilename || '',
  37. diffOldText: this.params.diffOldText ? decodeURIComponent(this.params.diffOldText) : (savedConfig.diffOldText || ''),
  38. diffNewText: this.params.diffNewText ? decodeURIComponent(this.params.diffNewText) : (savedConfig.diffNewText || ''),
  39. flashButton: this.params.flashButton || savedConfig.flashButton || 'none'
  40. };
  41. }
  42. // Otherwise, try to load from localStorage
  43. const savedConfig = this.loadFromLocalStorage();
  44. if (savedConfig) {
  45. return savedConfig;
  46. }
  47. // Fall back to defaults
  48. return {
  49. prompt: '',
  50. context: [],
  51. model: 'gpt-4o',
  52. mode: 'chat',
  53. thinking: false,
  54. max: false,
  55. lightColor: '#3b82f6',
  56. darkColor: '#60a5fa',
  57. height: '400',
  58. themeMode: 'auto',
  59. filetree: [],
  60. showFiletree: true,
  61. showDiff: false,
  62. diffFilename: '',
  63. diffOldText: '',
  64. diffNewText: '',
  65. flashButton: 'none'
  66. };
  67. }
  68. loadFromLocalStorage() {
  69. try {
  70. const saved = localStorage.getItem('embedDesignerConfig');
  71. if (saved) {
  72. const config = JSON.parse(saved);
  73. // Validate the loaded config has all required fields
  74. if (config && typeof config === 'object') {
  75. // Ensure all properties have defaults
  76. return {
  77. prompt: config.prompt || '',
  78. context: config.context || [],
  79. model: config.model || 'gpt-4o',
  80. mode: config.mode || 'chat',
  81. thinking: config.thinking || false,
  82. max: config.max || false,
  83. lightColor: config.lightColor || '#3b82f6',
  84. darkColor: config.darkColor || '#60a5fa',
  85. height: config.height || '400',
  86. themeMode: config.themeMode || 'auto',
  87. filetree: config.filetree || [],
  88. showFiletree: config.showFiletree !== undefined ? config.showFiletree : true,
  89. showDiff: config.showDiff || false,
  90. diffFilename: config.diffFilename || '',
  91. diffOldText: config.diffOldText || '',
  92. diffNewText: config.diffNewText || '',
  93. flashButton: config.flashButton || 'none'
  94. };
  95. }
  96. }
  97. } catch (e) {
  98. console.error('Error loading from localStorage:', e);
  99. }
  100. return null;
  101. }
  102. saveToLocalStorage() {
  103. try {
  104. localStorage.setItem('embedDesignerConfig', JSON.stringify(this.config));
  105. // Auto-save is silent - no notification
  106. } catch (e) {
  107. console.error('Error saving to localStorage:', e);
  108. }
  109. }
  110. clearLocalStorage() {
  111. try {
  112. localStorage.removeItem('embedDesignerConfig');
  113. this.showNotification('Settings cleared!');
  114. } catch (e) {
  115. console.error('Error clearing localStorage:', e);
  116. }
  117. }
  118. initDesignerMode() {
  119. this.setupDesignerElements();
  120. this.setupDesignerEvents();
  121. this.setupDesignerColors();
  122. this.updatePreview();
  123. this.updateIframeSnippet();
  124. }
  125. setupDesignerElements() {
  126. // Populate form with current config
  127. document.getElementById('designer-prompt').value = this.config.prompt;
  128. document.getElementById('designer-context').value = this.config.context.join(', ');
  129. document.getElementById('designer-filetree').value = this.config.filetree.join('\n');
  130. // Handle model selection
  131. const modelSelect = document.getElementById('designer-model');
  132. const customModelInput = document.getElementById('designer-custom-model');
  133. // Check if model is one of the predefined options
  134. const isPredefinedModel = Array.from(modelSelect.options).some(opt => opt.value === this.config.model);
  135. if (isPredefinedModel) {
  136. modelSelect.value = this.config.model;
  137. customModelInput.classList.add('hidden');
  138. } else {
  139. // It's a custom model
  140. modelSelect.value = 'custom';
  141. customModelInput.value = this.config.model;
  142. customModelInput.classList.remove('hidden');
  143. }
  144. document.getElementById('designer-mode-select').value = this.config.mode;
  145. document.getElementById('designer-thinking').checked = this.config.thinking;
  146. document.getElementById('designer-max').checked = this.config.max;
  147. // Set show filetree checkbox (default to true if not set)
  148. const showFiletreeCheckbox = document.getElementById('designer-show-filetree');
  149. if (showFiletreeCheckbox) {
  150. showFiletreeCheckbox.checked = this.config.showFiletree !== false;
  151. }
  152. // Set height slider
  153. const heightSlider = document.getElementById('designer-height');
  154. const heightValue = document.getElementById('height-value');
  155. if (heightSlider) {
  156. heightSlider.value = this.config.height;
  157. if (heightValue) {
  158. heightValue.textContent = this.config.height;
  159. }
  160. }
  161. // Set theme mode buttons
  162. this.updateThemeModeButtons();
  163. // Set color values
  164. const lightColorPicker = document.getElementById('designer-light-color');
  165. const lightColorText = document.getElementById('designer-light-color-text');
  166. const darkColorPicker = document.getElementById('designer-dark-color');
  167. const darkColorText = document.getElementById('designer-dark-color-text');
  168. if (lightColorPicker) {
  169. lightColorPicker.value = this.config.lightColor;
  170. lightColorText.value = this.config.lightColor;
  171. }
  172. if (darkColorPicker) {
  173. darkColorPicker.value = this.config.darkColor;
  174. darkColorText.value = this.config.darkColor;
  175. }
  176. // Set up diff view
  177. const showDiffCheckbox = document.getElementById('designer-show-diff');
  178. const diffFields = document.getElementById('diff-fields');
  179. if (showDiffCheckbox) {
  180. showDiffCheckbox.checked = this.config.showDiff || false;
  181. if (diffFields) {
  182. diffFields.classList.toggle('hidden', !this.config.showDiff);
  183. }
  184. }
  185. // Set diff field values
  186. const diffFilename = document.getElementById('designer-diff-filename');
  187. const diffOldText = document.getElementById('designer-diff-old');
  188. const diffNewText = document.getElementById('designer-diff-new');
  189. const flashButton = document.getElementById('designer-flash-button');
  190. if (diffFilename) diffFilename.value = this.config.diffFilename || '';
  191. if (diffOldText) diffOldText.value = this.config.diffOldText || '';
  192. if (diffNewText) diffNewText.value = this.config.diffNewText || '';
  193. if (flashButton) flashButton.value = this.config.flashButton || 'none';
  194. }
  195. updateThemeModeButtons() {
  196. // Update theme mode button states
  197. document.querySelectorAll('.theme-mode-btn').forEach(btn => {
  198. btn.classList.remove('bg-dynamic-primary', 'text-white');
  199. btn.classList.add('bg-dynamic-background', 'text-dynamic-foreground');
  200. });
  201. const activeButton = document.getElementById(`theme-${this.config.themeMode}`);
  202. if (activeButton) {
  203. activeButton.classList.remove('bg-dynamic-background', 'text-dynamic-foreground');
  204. activeButton.classList.add('bg-dynamic-primary', 'text-white');
  205. }
  206. }
  207. setupDesignerEvents() {
  208. // Form changes update preview
  209. ['designer-prompt', 'designer-context', 'designer-mode-select', 'designer-thinking', 'designer-max', 'designer-filetree', 'designer-show-filetree', 'designer-diff-filename', 'designer-diff-old', 'designer-diff-new', 'designer-flash-button'].forEach(id => {
  210. const element = document.getElementById(id);
  211. if (element) {
  212. element.addEventListener('input', () => this.updateConfigFromForm());
  213. element.addEventListener('change', () => this.updateConfigFromForm());
  214. }
  215. });
  216. // Diff view toggle
  217. const showDiffCheckbox = document.getElementById('designer-show-diff');
  218. const diffFields = document.getElementById('diff-fields');
  219. if (showDiffCheckbox) {
  220. showDiffCheckbox.addEventListener('change', (e) => {
  221. if (diffFields) {
  222. diffFields.classList.toggle('hidden', !e.target.checked);
  223. }
  224. this.updateConfigFromForm();
  225. });
  226. }
  227. // Theme mode buttons
  228. document.querySelectorAll('.theme-mode-btn').forEach(btn => {
  229. btn.addEventListener('click', (e) => {
  230. const mode = e.target.id.replace('theme-', '');
  231. this.config.themeMode = mode;
  232. this.updateThemeModeButtons();
  233. this.updatePreview();
  234. this.saveToLocalStorage();
  235. });
  236. });
  237. // Color preset buttons
  238. document.querySelectorAll('.color-preset').forEach(btn => {
  239. btn.addEventListener('click', (e) => {
  240. const lightColor = e.currentTarget.getAttribute('data-light');
  241. const darkColor = e.currentTarget.getAttribute('data-dark');
  242. // Update color pickers and text inputs
  243. const lightColorPicker = document.getElementById('designer-light-color');
  244. const lightColorText = document.getElementById('designer-light-color-text');
  245. const darkColorPicker = document.getElementById('designer-dark-color');
  246. const darkColorText = document.getElementById('designer-dark-color-text');
  247. if (lightColorPicker && lightColor) {
  248. lightColorPicker.value = lightColor;
  249. lightColorText.value = lightColor;
  250. }
  251. if (darkColorPicker && darkColor) {
  252. darkColorPicker.value = darkColor;
  253. darkColorText.value = darkColor;
  254. }
  255. this.updateConfigFromForm();
  256. });
  257. });
  258. // Model dropdown special handling
  259. const modelSelect = document.getElementById('designer-model');
  260. const customModelInput = document.getElementById('designer-custom-model');
  261. modelSelect.addEventListener('change', () => {
  262. if (modelSelect.value === 'custom') {
  263. customModelInput.classList.remove('hidden');
  264. customModelInput.focus();
  265. } else {
  266. customModelInput.classList.add('hidden');
  267. }
  268. this.updateConfigFromForm();
  269. });
  270. // Custom model input
  271. customModelInput.addEventListener('input', () => this.updateConfigFromForm());
  272. // Height slider
  273. const heightSlider = document.getElementById('designer-height');
  274. const heightValue = document.getElementById('height-value');
  275. if (heightSlider) {
  276. heightSlider.addEventListener('input', (e) => {
  277. if (heightValue) {
  278. heightValue.textContent = e.target.value;
  279. }
  280. this.updateConfigFromForm();
  281. });
  282. }
  283. // Color pickers
  284. const lightColorPicker = document.getElementById('designer-light-color');
  285. const lightColorText = document.getElementById('designer-light-color-text');
  286. const darkColorPicker = document.getElementById('designer-dark-color');
  287. const darkColorText = document.getElementById('designer-dark-color-text');
  288. if (lightColorPicker) {
  289. lightColorPicker.addEventListener('input', (e) => {
  290. lightColorText.value = e.target.value;
  291. this.updateConfigFromForm();
  292. });
  293. }
  294. if (lightColorText) {
  295. lightColorText.addEventListener('input', (e) => {
  296. if (/^#[0-9A-Fa-f]{6}$/.test(e.target.value)) {
  297. lightColorPicker.value = e.target.value;
  298. this.updateConfigFromForm();
  299. }
  300. });
  301. }
  302. if (darkColorPicker) {
  303. darkColorPicker.addEventListener('input', (e) => {
  304. darkColorText.value = e.target.value;
  305. this.updateConfigFromForm();
  306. });
  307. }
  308. if (darkColorText) {
  309. darkColorText.addEventListener('input', (e) => {
  310. if (/^#[0-9A-Fa-f]{6}$/.test(e.target.value)) {
  311. darkColorPicker.value = e.target.value;
  312. this.updateConfigFromForm();
  313. }
  314. });
  315. }
  316. // Generate embed button
  317. document.getElementById('generate-embed').addEventListener('click', () => this.showEmbedModal());
  318. // Iframe snippet click to copy
  319. document.getElementById('iframe-snippet').addEventListener('click', () => this.copyIframeCode());
  320. // Reset settings button
  321. document.getElementById('reset-settings').addEventListener('click', () => {
  322. if (confirm('Are you sure you want to reset all settings to defaults?')) {
  323. this.clearLocalStorage();
  324. // Reset to default config
  325. this.config = {
  326. prompt: '',
  327. context: [],
  328. model: 'gpt-4o',
  329. mode: 'chat',
  330. thinking: false,
  331. max: false,
  332. lightColor: '#3b82f6',
  333. darkColor: '#60a5fa',
  334. height: '400',
  335. themeMode: 'auto',
  336. filetree: [],
  337. showFiletree: true,
  338. showDiff: false,
  339. diffFilename: '',
  340. diffOldText: '',
  341. diffNewText: ''
  342. };
  343. // Update UI to reflect defaults
  344. this.setupDesignerElements();
  345. this.updatePreview();
  346. this.updateIframeSnippet();
  347. }
  348. });
  349. // Example select
  350. const exampleSelect = document.getElementById('example-select');
  351. if (exampleSelect) {
  352. exampleSelect.addEventListener('change', (e) => {
  353. const example = e.target.value;
  354. if (example) {
  355. switch (example) {
  356. case 'vibe-coding':
  357. this.loadVibeCodingExample();
  358. break;
  359. case 'vibe-coding-diff':
  360. this.loadVibeCodingDiffExample();
  361. break;
  362. case 'chatgpt':
  363. this.loadChatGPTExample();
  364. break;
  365. case 'claude':
  366. this.loadClaudeExample();
  367. break;
  368. case 'image-analysis':
  369. this.loadImageAnalysisExample();
  370. break;
  371. case 'api-design':
  372. this.loadAPIDesignExample();
  373. break;
  374. }
  375. // Reset select to placeholder after loading
  376. exampleSelect.value = '';
  377. }
  378. });
  379. }
  380. // Modal events
  381. document.getElementById('close-modal').addEventListener('click', () => this.hideEmbedModal());
  382. document.getElementById('copy-embed-code').addEventListener('click', () => this.copyEmbedCode());
  383. document.getElementById('copy-share-url').addEventListener('click', () => this.copyShareURL());
  384. // Close modal on backdrop click
  385. document.getElementById('embed-modal').addEventListener('click', (e) => {
  386. if (e.target.id === 'embed-modal') this.hideEmbedModal();
  387. });
  388. }
  389. updateConfigFromForm() {
  390. const heightSlider = document.getElementById('designer-height');
  391. const modelSelect = document.getElementById('designer-model');
  392. const customModelInput = document.getElementById('designer-custom-model');
  393. const lightColorText = document.getElementById('designer-light-color-text');
  394. const darkColorText = document.getElementById('designer-dark-color-text');
  395. // Get model value
  396. let modelValue = modelSelect.value;
  397. if (modelValue === 'custom') {
  398. modelValue = customModelInput.value || 'Custom Model';
  399. }
  400. this.config = {
  401. prompt: document.getElementById('designer-prompt').value,
  402. context: document.getElementById('designer-context').value.split(',').map(c => c.trim()).filter(c => c),
  403. model: modelValue,
  404. mode: document.getElementById('designer-mode-select').value,
  405. thinking: document.getElementById('designer-thinking').checked,
  406. max: document.getElementById('designer-max').checked,
  407. lightColor: lightColorText ? lightColorText.value : '#3b82f6',
  408. darkColor: darkColorText ? darkColorText.value : '#60a5fa',
  409. height: heightSlider ? heightSlider.value : '400',
  410. themeMode: this.config.themeMode || 'auto',
  411. filetree: document.getElementById('designer-filetree').value.split('\n').map(f => f.trim()).filter(f => f),
  412. showFiletree: document.getElementById('designer-show-filetree').checked,
  413. showDiff: document.getElementById('designer-show-diff').checked,
  414. diffFilename: document.getElementById('designer-diff-filename').value,
  415. diffOldText: document.getElementById('designer-diff-old').value,
  416. diffNewText: document.getElementById('designer-diff-new').value,
  417. flashButton: document.getElementById('designer-flash-button').value
  418. };
  419. this.updatePreview();
  420. this.saveToLocalStorage();
  421. this.updateIframeSnippet();
  422. }
  423. updateIframeSnippet() {
  424. const snippet = document.getElementById('iframe-snippet');
  425. if (!snippet) return;
  426. const code = this.generateEmbedCode();
  427. // Show a shortened version in the snippet
  428. const shortCode = code.replace(/\n/g, ' ').replace(/\s+/g, ' ');
  429. snippet.textContent = shortCode;
  430. }
  431. setupDesignerColors() {
  432. // Set up fixed designer colors - always use default blue theme in light mode
  433. const root = document.documentElement;
  434. // Always use default blue colors for designer
  435. root.style.setProperty('--primary', '59 130 246'); // Blue-500
  436. root.style.setProperty('--background', '255 255 255');
  437. root.style.setProperty('--foreground', '15 23 42');
  438. root.style.setProperty('--muted', '248 250 252');
  439. root.style.setProperty('--muted-foreground', '100 116 139');
  440. root.style.setProperty('--border', '226 232 240');
  441. root.style.setProperty('--accent', '16 185 129');
  442. }
  443. updateDesignerColors() {
  444. // Deprecated - no longer update designer colors
  445. // Designer maintains fixed color scheme
  446. }
  447. updatePreview() {
  448. const previewContainer = document.getElementById('preview-container');
  449. const previewWrapper = document.getElementById('preview-wrapper');
  450. if (!previewContainer) return;
  451. // Update preview wrapper height
  452. if (previewWrapper) {
  453. previewWrapper.style.height = `${this.config.height}px`;
  454. }
  455. // Generate preview URL with parameters
  456. const previewUrl = this.generatePreviewURL();
  457. // Update iframe src
  458. let iframe = previewContainer.querySelector('iframe');
  459. if (!iframe) {
  460. iframe = document.createElement('iframe');
  461. iframe.className = 'w-full h-full border-0 rounded-lg';
  462. previewContainer.innerHTML = '';
  463. previewContainer.appendChild(iframe);
  464. }
  465. iframe.src = previewUrl;
  466. }
  467. generatePreviewURL() {
  468. const params = new URLSearchParams();
  469. if (this.config.prompt) params.set('prompt', this.config.prompt);
  470. if (this.config.context.length > 0) params.set('context', this.config.context.join(','));
  471. if (this.config.model !== 'gpt-4o') params.set('model', this.config.model);
  472. if (this.config.mode !== 'chat') params.set('agentMode', this.config.mode);
  473. if (this.config.thinking) params.set('thinking', 'true');
  474. if (this.config.max) params.set('max', 'true');
  475. if (this.config.lightColor !== '#3b82f6') params.set('lightColor', this.config.lightColor);
  476. if (this.config.darkColor !== '#60a5fa') params.set('darkColor', this.config.darkColor);
  477. if (this.config.themeMode !== 'auto') params.set('themeMode', this.config.themeMode);
  478. if (this.config.showFiletree && this.config.filetree && this.config.filetree.length > 0) params.set('filetree', encodeURIComponent(this.config.filetree.join('\n')));
  479. if (this.config.showDiff) {
  480. params.set('showDiff', 'true');
  481. if (this.config.diffFilename) params.set('diffFilename', this.config.diffFilename);
  482. if (this.config.flashButton && this.config.flashButton !== 'none') params.set('flashButton', this.config.flashButton);
  483. // Truncate diff text if too long to prevent URL length issues
  484. if (this.config.diffOldText) {
  485. const truncated = this.config.diffOldText.substring(0, 100);
  486. params.set('diffOldText', encodeURIComponent(truncated)+'...');
  487. }
  488. if (this.config.diffNewText) {
  489. const truncated = this.config.diffNewText.substring(0, 100);
  490. params.set('diffNewText', encodeURIComponent(truncated)+'...');
  491. }
  492. }
  493. params.set('preview', 'true');
  494. return `/embed-preview/?${params.toString()}`;
  495. }
  496. generateShareURL() {
  497. const params = new URLSearchParams();
  498. if (this.config.prompt) params.set('prompt', this.config.prompt);
  499. if (this.config.context.length > 0) params.set('context', this.config.context.join(','));
  500. if (this.config.model !== 'gpt-4o') params.set('model', this.config.model);
  501. if (this.config.mode !== 'chat') params.set('agentMode', this.config.mode);
  502. if (this.config.thinking) params.set('thinking', 'true');
  503. if (this.config.max) params.set('max', 'true');
  504. if (this.config.lightColor !== '#3b82f6') params.set('lightColor', this.config.lightColor);
  505. if (this.config.darkColor !== '#60a5fa') params.set('darkColor', this.config.darkColor);
  506. if (this.config.themeMode !== 'auto') params.set('themeMode', this.config.themeMode);
  507. if (this.config.showFiletree && this.config.filetree && this.config.filetree.length > 0) params.set('filetree', encodeURIComponent(this.config.filetree.join('\n')));
  508. if (this.config.showDiff) {
  509. params.set('showDiff', 'true');
  510. if (this.config.diffFilename) params.set('diffFilename', this.config.diffFilename);
  511. if (this.config.flashButton && this.config.flashButton !== 'none') params.set('flashButton', this.config.flashButton);
  512. // Truncate diff text if too long to prevent URL length issues
  513. if (this.config.diffOldText) {
  514. const truncated = this.config.diffOldText.substring(0, 150);
  515. params.set('diffOldText', encodeURIComponent(truncated));
  516. }
  517. if (this.config.diffNewText) {
  518. const truncated = this.config.diffNewText.substring(0, 150);
  519. params.set('diffNewText', encodeURIComponent(truncated));
  520. }
  521. }
  522. return `${window.location.origin}/embed-preview/?${params.toString()}`;
  523. }
  524. generateEmbedCode() {
  525. const url = this.generateShareURL();
  526. return `<iframe
  527. src="${url}"
  528. width="100%"
  529. height="${this.config.height}"
  530. frameborder="0"
  531. style="border-radius: 12px; border: 1px solid #e5e7eb;">
  532. </iframe>`;
  533. }
  534. showEmbedModal() {
  535. const modal = document.getElementById('embed-modal');
  536. const embedCode = document.getElementById('embed-code');
  537. const shareUrl = document.getElementById('share-url');
  538. embedCode.value = this.generateEmbedCode();
  539. shareUrl.value = this.generateShareURL();
  540. modal.classList.remove('hidden');
  541. }
  542. hideEmbedModal() {
  543. document.getElementById('embed-modal').classList.add('hidden');
  544. }
  545. async copyEmbedCode() {
  546. const embedCode = document.getElementById('embed-code').value;
  547. await this.copyToClipboard(embedCode);
  548. this.showNotification('Embed code copied to clipboard!');
  549. }
  550. async copyShareURL() {
  551. const shareUrl = this.generateShareURL();
  552. await this.copyToClipboard(shareUrl);
  553. this.showNotification('Share URL copied to clipboard!');
  554. }
  555. async copyIframeCode() {
  556. const embedCode = this.generateEmbedCode();
  557. await this.copyToClipboard(embedCode);
  558. this.showNotification('Iframe code copied to clipboard!');
  559. }
  560. async copyToClipboard(text) {
  561. if (navigator.clipboard && window.isSecureContext) {
  562. await navigator.clipboard.writeText(text);
  563. } else {
  564. // Fallback
  565. const textArea = document.createElement('textarea');
  566. textArea.value = text;
  567. textArea.style.position = 'fixed';
  568. textArea.style.left = '-999999px';
  569. textArea.style.top = '-999999px';
  570. document.body.appendChild(textArea);
  571. textArea.focus();
  572. textArea.select();
  573. document.execCommand('copy');
  574. textArea.remove();
  575. }
  576. }
  577. showNotification(message, duration = 2000) {
  578. const notification = document.getElementById('notification');
  579. if (!notification) return;
  580. notification.textContent = message;
  581. notification.classList.remove('opacity-0');
  582. notification.classList.add('opacity-100');
  583. setTimeout(() => {
  584. notification.classList.remove('opacity-100');
  585. notification.classList.add('opacity-0');
  586. }, duration);
  587. }
  588. loadVibeCodingExample() {
  589. // Vibe coding example WITHOUT diff
  590. // Set vibe coding example values
  591. document.getElementById('designer-prompt').value =
  592. `Refactor my React ProductList component:
  593. - Extract filtering logic to custom hook
  594. - Split into smaller components
  595. - Add TypeScript types
  596. - Implement virtualization
  597. Currently handles display, filtering, sorting, and pagination in one file.
  598. @web Check React Query docs for data fetching patterns.`;
  599. document.getElementById('designer-context').value = '@codebase, ProductList.tsx';
  600. document.getElementById('designer-filetree').value =
  601. `src/components/ProductList.tsx*
  602. src/components/ProductCard.tsx
  603. src/hooks/useProducts.ts
  604. src/types/product.ts`;
  605. // Set vibe coding settings
  606. document.getElementById('designer-model').value = 'Claude 4 Opus';
  607. document.getElementById('designer-mode-select').value = 'agent';
  608. document.getElementById('designer-thinking').checked = true;
  609. document.getElementById('designer-max').checked = true;
  610. document.getElementById('designer-show-filetree').checked = true;
  611. // Set height and colors for coding example
  612. const heightSlider = document.getElementById('designer-height');
  613. const heightValue = document.getElementById('height-value');
  614. if (heightSlider) {
  615. heightSlider.value = '500';
  616. if (heightValue) {
  617. heightValue.textContent = '500';
  618. }
  619. }
  620. // Set developer-friendly color scheme (purple/violet)
  621. document.getElementById('designer-light-color').value = '#8b5cf6';
  622. document.getElementById('designer-light-color-text').value = '#8b5cf6';
  623. document.getElementById('designer-dark-color').value = '#a78bfa';
  624. document.getElementById('designer-dark-color-text').value = '#a78bfa';
  625. // Set dark theme mode for coding
  626. this.config.themeMode = 'dark';
  627. this.updateThemeModeButtons();
  628. // No diff for this example
  629. document.getElementById('designer-show-diff').checked = false;
  630. document.getElementById('diff-fields').classList.add('hidden');
  631. document.getElementById('designer-flash-button').value = 'none';
  632. // Update config from form
  633. this.updateConfigFromForm();
  634. this.showNotification('Vibe coding example loaded!');
  635. }
  636. loadVibeCodingDiffExample() {
  637. // Vibe coding WITH diff - user giving feedback on suggested changes
  638. document.getElementById('designer-prompt').value =
  639. `Actually, don't use Error | null for the error state. Instead:
  640. - Create a custom ApiError type with code, message, and retry()
  641. - Add an AbortController for request cancellation
  642. - Include error boundary integration
  643. - Show me how to handle network vs API errors differently
  644. Also add a refetch function that the UI can call.`;
  645. document.getElementById('designer-context').value = '@codebase, useProducts.ts';
  646. document.getElementById('designer-filetree').value =
  647. `src/hooks/useProducts.ts*
  648. src/types/errors.ts
  649. src/utils/api.ts`;
  650. // Set same settings as regular vibe coding
  651. document.getElementById('designer-model').value = 'Claude 4 Opus';
  652. document.getElementById('designer-mode-select').value = 'agent';
  653. document.getElementById('designer-thinking').checked = true;
  654. document.getElementById('designer-max').checked = true;
  655. document.getElementById('designer-show-filetree').checked = true;
  656. // Set height and colors
  657. const heightSlider = document.getElementById('designer-height');
  658. const heightValue = document.getElementById('height-value');
  659. if (heightSlider) {
  660. heightSlider.value = '500';
  661. if (heightValue) {
  662. heightValue.textContent = '500';
  663. }
  664. }
  665. document.getElementById('designer-light-color').value = '#8b5cf6';
  666. document.getElementById('designer-light-color-text').value = '#8b5cf6';
  667. document.getElementById('designer-dark-color').value = '#a78bfa';
  668. document.getElementById('designer-dark-color-text').value = '#a78bfa';
  669. this.config.themeMode = 'dark';
  670. this.updateThemeModeButtons();
  671. // Add diff view showing the previous suggestion
  672. document.getElementById('designer-show-diff').checked = true;
  673. document.getElementById('diff-fields').classList.remove('hidden');
  674. document.getElementById('designer-diff-filename').value = 'useProducts.ts';
  675. document.getElementById('designer-diff-old').value =
  676. `const [products, setProducts] = useState([]);
  677. const [loading, setLoading] = useState(true);`;
  678. document.getElementById('designer-diff-new').value =
  679. `const [products, setProducts] = useState<Product[]>([]);
  680. const [loading, setLoading] = useState(true);
  681. const [error, setError] = useState<Error | null>(null);`;
  682. // Flash accept button for this example with diff
  683. document.getElementById('designer-flash-button').value = 'accept';
  684. // Update config from form
  685. this.updateConfigFromForm();
  686. this.showNotification('Vibe coding with diff loaded!');
  687. }
  688. loadChatGPTExample() {
  689. // ChatGPT example with green colors
  690. document.getElementById('designer-prompt').value =
  691. `I'm planning a dinner party for 8 people this weekend. One person is vegetarian, another is gluten-free, and I want to make something impressive but not too complicated.
  692. Can you suggest a menu with appetizers, main course, and dessert that would work for everyone? I have about 4 hours to prepare everything and a moderate cooking skill level.`;
  693. document.getElementById('designer-context').value = '#Kitchen Layout.png, #Pantry Inventory.jpg';
  694. document.getElementById('designer-filetree').value = '';
  695. // ChatGPT settings
  696. document.getElementById('designer-model').value = 'GPT 4o';
  697. document.getElementById('designer-mode-select').value = 'chat';
  698. document.getElementById('designer-thinking').checked = false;
  699. document.getElementById('designer-max').checked = false;
  700. document.getElementById('designer-show-filetree').checked = false;
  701. // Set height and colors for ChatGPT
  702. const heightSlider = document.getElementById('designer-height');
  703. const heightValue = document.getElementById('height-value');
  704. if (heightSlider) {
  705. heightSlider.value = '350';
  706. if (heightValue) {
  707. heightValue.textContent = '350';
  708. }
  709. }
  710. // ChatGPT green color scheme
  711. document.getElementById('designer-light-color').value = '#10b981';
  712. document.getElementById('designer-light-color-text').value = '#10b981';
  713. document.getElementById('designer-dark-color').value = '#34d399';
  714. document.getElementById('designer-dark-color-text').value = '#34d399';
  715. // Light theme for ChatGPT
  716. this.config.themeMode = 'light';
  717. this.updateThemeModeButtons();
  718. // Clear diff view
  719. document.getElementById('designer-show-diff').checked = false;
  720. document.getElementById('diff-fields').classList.add('hidden');
  721. document.getElementById('designer-flash-button').value = 'none';
  722. // Update config from form
  723. this.updateConfigFromForm();
  724. this.showNotification('ChatGPT example loaded!');
  725. }
  726. loadClaudeExample() {
  727. // Claude example with orange colors - daily usage
  728. document.getElementById('designer-prompt').value =
  729. `Help me write a professional email to decline a job offer while keeping the door open for future opportunities.
  730. Context:
  731. - Received offer from TechCorp as Senior Engineer
  732. - Great team and compensation, but role doesn't align with my career goals
  733. - Want to maintain good relationship with the hiring manager Sarah
  734. - Interested in their upcoming ML team expansion next year
  735. Keep it warm but professional, around 150-200 words.`;
  736. document.getElementById('designer-context').value = '';
  737. document.getElementById('designer-filetree').value = '';
  738. // Claude settings
  739. document.getElementById('designer-model').value = 'Claude 3.7 Sonnet';
  740. document.getElementById('designer-mode-select').value = 'chat';
  741. document.getElementById('designer-thinking').checked = false;
  742. document.getElementById('designer-max').checked = false;
  743. document.getElementById('designer-show-filetree').checked = false;
  744. // Set height and colors for Claude
  745. const heightSlider = document.getElementById('designer-height');
  746. const heightValue = document.getElementById('height-value');
  747. if (heightSlider) {
  748. heightSlider.value = '350';
  749. if (heightValue) {
  750. heightValue.textContent = '350';
  751. }
  752. }
  753. // Claude orange color scheme
  754. document.getElementById('designer-light-color').value = '#f97316';
  755. document.getElementById('designer-light-color-text').value = '#f97316';
  756. document.getElementById('designer-dark-color').value = '#fb923c';
  757. document.getElementById('designer-dark-color-text').value = '#fb923c';
  758. // Auto theme for Claude
  759. this.config.themeMode = 'auto';
  760. this.updateThemeModeButtons();
  761. // Clear diff view
  762. document.getElementById('designer-show-diff').checked = false;
  763. document.getElementById('diff-fields').classList.add('hidden');
  764. document.getElementById('designer-flash-button').value = 'none';
  765. // Update config from form
  766. this.updateConfigFromForm();
  767. this.showNotification('Claude example loaded!');
  768. }
  769. loadImageAnalysisExample() {
  770. // Image analysis example
  771. document.getElementById('designer-prompt').value =
  772. `Analyze these UI screenshots and provide detailed feedback on:
  773. - Visual hierarchy and layout
  774. - Color scheme and contrast
  775. - Typography choices
  776. - Accessibility concerns
  777. - Mobile responsiveness issues`;
  778. document.getElementById('designer-context').value = '#Homepage Desktop.png, #Homepage Mobile.png, #Dashboard View.jpg';
  779. document.getElementById('designer-filetree').value = '';
  780. // Image analysis settings
  781. document.getElementById('designer-model').value = 'GPT 4.1';
  782. document.getElementById('designer-mode-select').value = 'chat';
  783. document.getElementById('designer-thinking').checked = false;
  784. document.getElementById('designer-max').checked = false;
  785. document.getElementById('designer-show-filetree').checked = false;
  786. // Set height and colors
  787. const heightSlider = document.getElementById('designer-height');
  788. const heightValue = document.getElementById('height-value');
  789. if (heightSlider) {
  790. heightSlider.value = '400';
  791. if (heightValue) {
  792. heightValue.textContent = '400';
  793. }
  794. }
  795. // Pink/purple color scheme for design
  796. document.getElementById('designer-light-color').value = '#ec4899';
  797. document.getElementById('designer-light-color-text').value = '#ec4899';
  798. document.getElementById('designer-dark-color').value = '#f472b6';
  799. document.getElementById('designer-dark-color-text').value = '#f472b6';
  800. // Light theme
  801. this.config.themeMode = 'light';
  802. this.updateThemeModeButtons();
  803. // Clear diff view
  804. document.getElementById('designer-show-diff').checked = false;
  805. document.getElementById('diff-fields').classList.add('hidden');
  806. document.getElementById('designer-flash-button').value = 'none';
  807. // Update config from form
  808. this.updateConfigFromForm();
  809. this.showNotification('Image analysis example loaded!');
  810. }
  811. loadAPIDesignExample() {
  812. // API design example
  813. document.getElementById('designer-prompt').value =
  814. `Design a RESTful API for a task management system with:
  815. - User authentication
  816. - Project and task CRUD operations
  817. - Team collaboration features
  818. - Real-time notifications
  819. Include endpoint definitions, request/response schemas, and error handling patterns.`;
  820. document.getElementById('designer-context').value = '@Web, openapi.yaml';
  821. document.getElementById('designer-filetree').value =
  822. `api/
  823. api/v1/
  824. api/v1/users/
  825. api/v1/projects/
  826. api/v1/tasks/
  827. docs/openapi.yaml*`;
  828. // API design settings
  829. document.getElementById('designer-model').value = 'Claude 4 Opus';
  830. document.getElementById('designer-mode-select').value = 'manual';
  831. document.getElementById('designer-thinking').checked = true;
  832. document.getElementById('designer-max').checked = false;
  833. document.getElementById('designer-show-filetree').checked = true;
  834. // Set height and colors
  835. const heightSlider = document.getElementById('designer-height');
  836. const heightValue = document.getElementById('height-value');
  837. if (heightSlider) {
  838. heightSlider.value = '500';
  839. if (heightValue) {
  840. heightValue.textContent = '500';
  841. }
  842. }
  843. // Blue color scheme for API
  844. document.getElementById('designer-light-color').value = '#3b82f6';
  845. document.getElementById('designer-light-color-text').value = '#3b82f6';
  846. document.getElementById('designer-dark-color').value = '#60a5fa';
  847. document.getElementById('designer-dark-color-text').value = '#60a5fa';
  848. // Dark theme for technical
  849. this.config.themeMode = 'dark';
  850. this.updateThemeModeButtons();
  851. // Clear diff view
  852. document.getElementById('designer-show-diff').checked = false;
  853. document.getElementById('diff-fields').classList.add('hidden');
  854. document.getElementById('designer-flash-button').value = 'none';
  855. // Update config from form
  856. this.updateConfigFromForm();
  857. this.showNotification('API design example loaded!');
  858. }
  859. }
  860. // Dark mode toggle function
  861. function toggleDarkMode() {
  862. const body = document.body;
  863. const isDark = body.classList.contains('dark-mode');
  864. if (isDark) {
  865. body.classList.remove('dark-mode');
  866. localStorage.setItem('darkMode', 'false');
  867. document.querySelector('.dark-mode-toggle .sun-icon').style.display = 'block';
  868. document.querySelector('.dark-mode-toggle .moon-icon').style.display = 'none';
  869. } else {
  870. body.classList.add('dark-mode');
  871. localStorage.setItem('darkMode', 'true');
  872. document.querySelector('.dark-mode-toggle .sun-icon').style.display = 'none';
  873. document.querySelector('.dark-mode-toggle .moon-icon').style.display = 'block';
  874. }
  875. }
  876. // Initialize when DOM is ready
  877. document.addEventListener('DOMContentLoaded', () => {
  878. window.embedDesigner = new EmbedDesigner();
  879. // Initialize dark mode from localStorage
  880. const darkMode = localStorage.getItem('darkMode');
  881. if (darkMode === 'true') {
  882. document.body.classList.add('dark-mode');
  883. document.querySelector('.dark-mode-toggle .sun-icon').style.display = 'none';
  884. document.querySelector('.dark-mode-toggle .moon-icon').style.display = 'block';
  885. }
  886. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...