-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSchemaManager.html
1048 lines (955 loc) · 40.6 KB
/
SchemaManager.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Schema Manager</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.32.2/ace.js"></script>
<style>
/* --- Base Styles --- */
:root {
--primary: #2563eb;
--primary-dark: #1d4ed8;
--primary-light: #60a5fa;
--success: #22c55e;
--danger: #ef4444;
--warning: #f59e0b;
--gray-50: #f9fafb;
--gray-100: #f3f4f6;
--gray-200: #e5e7eb;
--gray-300: #d1d5db;
--gray-400: #9ca3af;
--gray-500: #6b7280;
--gray-600: #4b5563;
--gray-700: #374151;
--gray-800: #1f2937;
--gray-900: #111827;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
line-height: 1.6;
padding: 2rem;
max-width: 1200px;
margin: 0 auto;
color: var(--gray-800);
background-color: var(--gray-50);
}
/* --- Typography --- */
h1 {
margin-bottom: 2rem;
color: var(--gray-900);
font-size: 2rem;
font-weight: 600;
}
h2 {
color: var(--gray-800);
font-size: 1.5rem;
margin-bottom: 1rem;
}
h3 {
color: var(--gray-700);
font-size: 1.25rem;
margin-bottom: 0.5rem;
}
/* --- Layout Components --- */
.container {
background: white;
border-radius: 0.5rem;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
padding: 2rem;
margin-bottom: 2rem;
}
.folder-section {
margin-bottom: 2rem;
padding: 1rem;
background: white;
border-radius: 0.5rem;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
display: flex;
align-items: center;
gap: 1rem;
}
.folder-path {
flex: 1;
font-family: monospace;
padding: 0.5rem;
background: var(--gray-50);
border: 1px solid var(--gray-300);
border-radius: 0.25rem;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* --- Tabs --- */
.tabs {
display: flex;
gap: 1rem;
margin-bottom: 2rem;
border-bottom: 2px solid var(--gray-200);
padding-bottom: 0.5rem;
}
.tab {
padding: 0.5rem 1rem;
border: none;
background: none;
cursor: pointer;
font-size: 1rem;
color: var(--gray-600);
border-bottom: 2px solid transparent;
margin-bottom: -0.5rem;
transition: all 0.2s ease;
}
.tab:hover {
color: var(--primary);
}
.tab.active {
color: var(--primary);
border-bottom: 2px solid var(--primary);
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
/* --- Forms --- */
.form-group {
margin-bottom: 1.5rem;
}
label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
color: var(--gray-700);
}
input[type="text"],
select,
textarea {
width: 100%;
padding: 0.5rem;
border: 1px solid var(--gray-300);
border-radius: 0.25rem;
font-size: 1rem;
transition: border-color 0.2s ease;
}
input[type="text"]:focus,
select:focus,
textarea:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}
/* --- Properties Section --- */
.properties-container {
border: 1px solid var(--gray-300);
padding: 1rem;
border-radius: 0.25rem;
margin-bottom: 1rem;
background: white;
}
.property {
border: 1px solid var(--gray-200);
padding: 1rem;
margin-bottom: 1rem;
border-radius: 0.25rem;
background: var(--gray-50);
display: flex;
flex-direction: column;
gap: 1rem;
}
.property-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1rem;
}
/* --- Buttons --- */
.btn {
padding: 0.5rem 1rem;
border: none;
border-radius: 0.25rem;
cursor: pointer;
font-size: 1rem;
background: var(--primary);
color: white;
transition: all 0.2s ease;
}
.btn:hover {
background: var(--primary-dark);
}
.btn:focus {
outline: none;
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.3);
}
.btn-danger {
background: var(--danger);
}
.btn-danger:hover {
background: #dc2626;
}
/* --- Editor --- */
.editor-container {
height: 500px;
border: 1px solid var(--gray-300);
border-radius: 0.25rem;
margin-bottom: 1rem;
overflow: hidden;
}
#jsonEditor {
height: 100%;
font-size: 14px;
}
/* --- Checkboxes --- */
.checkbox-group {
display: flex;
align-items: center;
gap: 0.5rem;
}
.checkbox-label {
display: flex;
align-items: center;
gap: 0.5rem;
font-weight: normal;
cursor: pointer;
}
/* --- Toast Notifications --- */
#toastContainer {
position: fixed;
top: 1rem;
right: 1rem;
display: flex;
flex-direction: column;
gap: 0.5rem;
z-index: 1000;
}
.toast {
min-width: 250px;
padding: 1rem 1.5rem;
border-radius: 0.25rem;
color: white;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
opacity: 0;
transform: translateX(100%);
animation: slideIn 0.5s forwards, fadeOut 0.5s forwards 2.5s;
}
.toast-success {
background-color: var(--success);
}
.toast-error {
background-color: var(--danger);
}
@keyframes slideIn {
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes fadeOut {
to {
opacity: 0;
transform: translateX(100%);
}
}
/* --- Schema Select --- */
#loadSchemaSelect {
margin-bottom: 1rem;
width: 100%;
padding: 0.5rem;
border: 1px solid var(--gray-300);
border-radius: 0.25rem;
font-size: 1rem;
}
/* --- Responsive Design --- */
@media (max-width: 768px) {
body {
padding: 1rem;
}
.folder-section {
flex-direction: column;
align-items: stretch;
}
.property-header {
flex-direction: column;
gap: 0.5rem;
}
.property {
grid-template-columns: 1fr;
}
}
/* --- New Styles --- */
/* Add new styles for locked fields */
.locked-field {
background-color: var(--gray-100);
cursor: not-allowed;
}
.locked-field:disabled {
color: var(--gray-700);
}
/* Add style for the additional properties checkbox */
.global-settings {
margin-bottom: 1.5rem;
padding: 1rem;
background: var(--gray-100);
border-radius: 0.25rem;
}
/* --- Enum Fields --- */
.enum-fields {
display: none;
}
/* --- Updated Property Layout --- */
.property-row {
display: flex;
gap: 1rem;
align-items: center;
}
.property-row.large {
flex-wrap: wrap;
}
.property-row.large .form-group {
flex: 2;
}
.property-row.small {
flex: 1;
}
.property-row .form-group {
flex: 1;
}
.conditional-input {
flex: 2;
}
/* Adjust Description field to be twice as wide */
.description-input {
flex: 2;
}
</style>
</head>
<body>
<h1>Schema Manager</h1>
<div class="folder-section">
<button class="btn" id="selectDirectoryBtn">Select Schemas Folder</button>
<div class="folder-path" id="folderPath">No folder selected</div>
</div>
<!-- Toast Notifications Container -->
<div id="toastContainer"></div>
<div class="tabs">
<button class="tab active" data-tab="create">Create Schema</button>
<button class="tab" data-tab="viewRaw">View/Edit Schema (raw)</button>
</div>
<div id="createSchema" class="tab-content active">
<div class="form-group">
<label for="schemaName">Schema Name:</label>
<input type="text" id="schemaName" placeholder="Identifier for the schema and node type within the memory">
</div>
<div class="form-group">
<label for="schemaDescription">Schema Description:</label>
<input type="text" id="schemaDescription"
placeholder="Description for the add_<name> tool, providing context for the AI.">
</div>
<div class="global-settings">
<div class="checkbox-group">
<label class="checkbox-label">
<input type="checkbox" id="allowAdditionalProps" checked>
Allow Additional Properties
</label>
</div>
</div>
<div class="form-group">
<label>Properties:</label>
<div id="propertiesContainer" class="properties-container">
<!-- Properties will be added here -->
</div>
<button class="btn" id="addPropertyBtn">Add Property</button>
</div>
<div style="display: flex; gap: 1rem;">
<button class="btn" id="viewRawSchemaBtn">View in Raw Format</button>
<button class="btn btn-danger" id="resetFormBtn">Reset</button>
</div>
</div>
<div id="viewRawSchema" class="tab-content">
<select id="loadSchemaSelect">
<option value="">Select a schema to load...</option>
</select>
<div class="editor-container">
<div id="jsonEditor"></div>
</div>
<div class="button-group" style="display: flex; gap: 1rem;">
<button class="btn" id="saveSchemaBtn">Save Schema</button>
<button class="btn" id="editSchemaBtn">Edit</button>
<button class="btn btn-danger" id="deleteSchemaBtn">Delete Schema</button>
</div>
</div>
<script>
// --- Constants and State ---
const DB_NAME = 'SchemaManagerDB';
const STORE_NAME = 'directory';
let db = null;
let dirHandle = null;
let currentSchemaHandle = null;
// --- Ace Editor Initialization ---
const editor = ace.edit("jsonEditor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/json");
editor.setOptions({
fontSize: "14px",
showPrintMargin: false,
showGutter: true,
highlightActiveLine: true,
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
enableSnippets: true
});
// --- Utility Functions ---
function showToast(message, type = 'success', duration = 3000) {
const toastContainer = document.getElementById('toastContainer');
const toast = document.createElement('div');
toast.classList.add('toast', type === 'success' ? 'toast-success' : 'toast-error');
toast.textContent = message;
toastContainer.appendChild(toast);
setTimeout(() => toast.remove(), duration + 1000);
}
// --- IndexedDB Operations ---
const idb = {
initDB: async () => {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, 1);
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(STORE_NAME)) {
db.createObjectStore(STORE_NAME);
}
};
request.onsuccess = (event) => {
db = event.target.result;
resolve();
};
request.onerror = (event) => reject(event.target.error);
});
},
saveDirectoryHandle: async (handle) => {
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], 'readwrite');
const store = transaction.objectStore(STORE_NAME);
const request = store.put(handle, 'schemasDirectory');
request.onsuccess = () => resolve();
request.onerror = (event) => reject(event.target.error);
});
},
getDirectoryHandleFromDB: async () => {
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], 'readonly');
const store = transaction.objectStore(STORE_NAME);
const request = store.get('schemasDirectory');
request.onsuccess = async (event) => {
const handle = event.target.result;
if (handle) {
const permission = await handle.queryPermission({mode: 'readwrite'});
if (permission === 'granted') {
resolve(handle);
return;
}
}
resolve(null);
};
request.onerror = (event) => reject(event.target.error);
});
},
removeDirectoryHandleFromDB: async () => {
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], 'readwrite');
const store = transaction.objectStore(STORE_NAME);
const request = store.delete('schemasDirectory');
request.onsuccess = () => resolve();
request.onerror = (event) => reject(event.target.error);
});
}
};
// --- File System Operations ---
const fileSystem = {
selectDirectory: async () => {
try {
let options = {id: 'schemas-directory', mode: 'readwrite'};
const storedHandle = await idb.getDirectoryHandleFromDB();
options.startIn = storedHandle || 'documents';
dirHandle = await window.showDirectoryPicker(options);
if (storedHandle && dirHandle.name !== storedHandle.name) {
await idb.removeDirectoryHandleFromDB();
}
document.getElementById('folderPath').textContent = dirHandle.name;
await ui.loadSchemas();
showToast('Folder selected successfully!', 'success');
await idb.saveDirectoryHandle(dirHandle);
} catch (error) {
if (error.name !== 'AbortError') {
showToast('Error selecting folder: ' + error.message, 'error');
}
}
},
loadSchemaFile: async (filename) => {
try {
const fileHandle = await dirHandle.getFileHandle(filename);
currentSchemaHandle = fileHandle;
const file = await fileHandle.getFile();
const content = await file.text();
const formattedContent = JSON.stringify(JSON.parse(content), null, 2);
editor.setValue(formattedContent);
editor.clearSelection();
showToast('Schema loaded successfully!', 'success');
} catch (error) {
showToast('Error loading schema: ' + error.message, 'error');
}
},
saveSchema: async () => {
if (!dirHandle) {
showToast('Please select a schemas folder first', 'error');
return;
}
try {
const schemaContent = editor.getValue();
const schema = JSON.parse(schemaContent);
if (!schema.name) throw new Error('Schema must have a name property');
if (!schema.properties) throw new Error('Schema must have properties defined');
// Check for schema name uniqueness before saving
const isSchemaNameUnique = await schemaValidator.isSchemaNameUnique(schema.name);
if (!isSchemaNameUnique) {
showToast(`A schema with the name "${schema.name}" already exists. Please choose a unique name.`, 'error');
return;
}
const fileName = `${schema.name.replace('add_', '')}.schema.json`;
let fileHandle;
try {
fileHandle = await dirHandle.getFileHandle(fileName);
if (!confirm(`Schema ${fileName} already exists. Do you want to overwrite it?`)) return;
} catch {
fileHandle = await dirHandle.getFileHandle(fileName, {create: true});
}
const writable = await fileHandle.createWritable();
await writable.write(JSON.stringify(schema, null, 2));
await writable.close();
currentSchemaHandle = fileHandle;
await ui.loadSchemas();
showToast('Schema saved successfully!', 'success');
} catch (error) {
showToast('Error saving schema: ' + error.message, 'error');
}
},
deleteSchema: async () => {
if (!currentSchemaHandle) {
showToast('Please select a schema to delete', 'error');
return;
}
if (!confirm(`Are you sure you want to delete ${currentSchemaHandle.name}? This action cannot be undone.`)) return;
try {
await dirHandle.removeEntry(currentSchemaHandle.name);
editor.setValue('');
currentSchemaHandle = null;
await ui.loadSchemas();
showToast('Schema deleted successfully!', 'success');
} catch (error) {
showToast('Error deleting schema: ' + error.message, 'error');
}
}
};
// --- Schema Generation and Validation ---
const schemaGenerator = {
generateSchema: () => {
const name = document.getElementById('schemaName').value;
const description = document.getElementById('schemaDescription').value;
const allowAdditionalProps = document.getElementById('allowAdditionalProps').checked;
if (!name) {
showToast('Schema name is required', 'error');
return;
}
// Real-time validation for schema name uniqueness
const isSchemaNameUnique = schemaValidator.isSchemaNameUnique(name, dirHandle);
if (!isSchemaNameUnique) {
showToast('Schema name must be unique', 'error');
return;
}
const properties = {};
document.querySelectorAll('.property').forEach(prop => {
if (prop.id === 'property-name-property') return;
const propName = prop.querySelector('.property-name').value;
if (!propName) return;
const propDesc = prop.querySelector('.property-description').value;
const propType = prop.querySelector('.property-type').value;
const required = prop.querySelector('.property-required').checked;
const isEnum = prop.querySelector('.property-enum').checked;
const enumValues = isEnum ? prop.querySelector('.enum-values-input')?.value
.split(',')
.map(v => v.trim())
.filter(v => v) : null;
const isRelationship = prop.querySelector('.property-relationship').checked;
const edgeType = isRelationship ? prop.querySelector('.edge-type').value : null;
const property = {
type: propType,
description: propDesc,
required: required
};
if (isEnum && enumValues) {
property.enum = enumValues;
}
if (isRelationship) {
property.relationship = {
edgeType: edgeType,
description: propDesc
};
}
properties[propName] = property;
});
const nameProp = document.querySelector('#property-name-property');
if (nameProp) {
const propName = nameProp.querySelector('.property-name').value;
const propDesc = nameProp.querySelector('.property-description').value;
const propType = nameProp.querySelector('.property-type').value;
const required = nameProp.querySelector('.property-required').checked;
properties[propName] = {
type: propType,
description: propDesc,
required: required
};
}
const schema = {
name: `add_${name}`,
description: description,
properties: properties,
additionalProperties: allowAdditionalProps
};
editor.setValue(JSON.stringify(schema, null, 2));
ui.switchTab('viewRaw');
}
};
const schemaValidator = {
isSchemaNameUnique: async (schemaName) => {
if (!dirHandle) return true;
const fullSchemaName = `add_${schemaName}.schema.json`;
const simplifiedSchemaName = `${schemaName}.schema.json`
try {
for await (const entry of dirHandle.values()) {
if (entry.kind === 'file' && (entry.name === fullSchemaName || entry.name === simplifiedSchemaName)) {
return false;
}
}
return true; // No matching file found
} catch (error) {
console.error('Error checking for schema name uniqueness:', error);
return false; // Assume not unique in case of error
}
}
};
// --- UI Management ---
const ui = {
switchTab: (tabName) => {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
document.querySelector(`.tab[data-tab="${tabName}"]`).classList.add('active');
document.getElementById(`${tabName}Schema`).classList.add('active');
},
loadSchemas: async () => {
if (!dirHandle) return;
const select = document.getElementById('loadSchemaSelect');
select.innerHTML = '<option value="">Select a schema to load...</option>';
try {
for await (const entry of dirHandle.values()) {
if (entry.kind === 'file' && entry.name.endsWith('.schema.json')) {
const option = document.createElement('option');
option.value = entry.name;
option.textContent = entry.name;
select.appendChild(option);
}
}
} catch (error) {
showToast('Error loading schemas: ' + error.message, 'error');
}
},
resetForm: () => {
if (!confirm('Are you sure you want to reset the form? All unsaved changes will be lost.')) return;
document.getElementById('schemaName').value = '';
document.getElementById('schemaDescription').value = '';
document.getElementById('allowAdditionalProps').checked = true;
const propertiesContainer = document.getElementById('propertiesContainer');
propertiesContainer.innerHTML = '';
propertiesContainer.appendChild(ui.createNamePropertyElement());
ui.addProperty();
showToast('Form has been reset to default state.', 'success');
},
addProperty: () => {
const container = document.getElementById('propertiesContainer');
const newProperty = ui.createPropertyElement();
container.appendChild(newProperty);
},
removeProperty: (button) => {
const property = button.closest('.property');
property.remove();
},
toggleEnumFields: (checkbox) => {
const property = checkbox.closest('.property');
const enumInput = property.querySelector('.enum-values-input');
enumInput.style.display = checkbox.checked ? 'block' : 'none';
},
toggleRelationshipFields: (checkbox) => {
const property = checkbox.closest('.property');
const relationshipInput = property.querySelector('.edge-type');
relationshipInput.style.display = checkbox.checked ? 'block' : 'none';
},
createPropertyElement: () => {
const propertyTemplate = `
<div class="property">
<div class="property-header">
<h3>Property</h3>
<button class="btn btn-danger" onclick="ui.removeProperty(this)">Remove</button>
</div>
<div class="property-row large">
<div class="form-group">
<label>Name:</label>
<input type="text" class="property-name" placeholder="Property" oninput="schemaValidator.validatePropertyName(this)">
<div class="validation-message"></div>
</div>
<div class="form-group description-input">
<label>Description:</label>
<input type="text" class="property-description" placeholder="Property description">
</div>
<div class="form-group">
<label>Type:</label>
<select class="property-type" onchange="ui.handlePropertyTypeChange(this)">
<option value="string">String</option>
<option value="array">Array</option>
</select>
</div>
<div class="checkbox-group">
<label class="checkbox-label">
<input type="checkbox" class="property-required">
Required
</label>
</div>
</div>
<div class="property-row small">
<div class="checkbox-group">
<label class="checkbox-label">
<input type="checkbox" class="property-enum" onchange="ui.toggleEnumFields(this)">
Is Enum
</label>
<input type="text" class="enum-values-input conditional-input" placeholder="e.g., active, inactive, pending" style="display: none;">
</div>
</div>
<div class="property-row small">
<div class="checkbox-group">
<label class="checkbox-label">
<input type="checkbox" class="property-relationship" onchange="ui.toggleRelationshipFields(this)">
Is Relationship
</label>
<input type="text" class="edge-type conditional-input" placeholder="e.g., owns, belongs_to" style="display: none;">
</div>
</div>
</div>
`;
const template = document.createElement('template');
template.innerHTML = propertyTemplate.trim();
return template.content.firstChild;
},
createNamePropertyElement: () => {
const namePropertyTemplate = `
<div class="property" id="property-name-property">
<div class="property-header">
<h3>Name Property</h3>
</div>
<div class="property-row large">
<div class="form-group">
<label>Name:</label>
<input type="text" class="property-name" value="name" disabled>
</div>
<div class="form-group description-input">
<label>Description:</label>
<input type="text" class="property-description" placeholder="Property description">
</div>
<div class="form-group">
<label>Type:</label>
<select class="property-type" disabled>
<option value="string" selected>String</option>
</select>
</div>
<div class="checkbox-group">
<label class="checkbox-label">
<input type="checkbox" class="property-required" checked disabled>
Required
</label>
</div>
</div>
<div class="property-row small">
<div class="checkbox-group">
<label class="checkbox-label">
<input type="checkbox" class="property-enum" disabled>
Is Enum
</label>
<input type="text" class="enum-values-input conditional-input" placeholder="e.g., active, inactive, pending" style="display: none;" disabled>
</div>
</div>
<div class="property-row small">
<div class="checkbox-group">
<label class="checkbox-label">
<input type="checkbox" class="property-relationship" disabled>
Is Relationship
</label>
<input type="text" class="edge-type conditional-input" placeholder="e.g., owns, belongs_to" style="display: none;" disabled>
</div>
</div>
</div>
`;
const template = document.createElement('template');
template.innerHTML = namePropertyTemplate.trim();
return template.content.firstChild;
},
handlePropertyTypeChange: (select) => {
// No change here
},
loadSchemaIntoCreateTab: async (filename) => {
try {
const fileHandle = await dirHandle.getFileHandle(filename);
const file = await fileHandle.getFile();
const content = await file.text();
const schema = JSON.parse(content);
// Check for parsing errors
if (typeof schema !== 'object' || schema === null) {
throw new Error('Invalid schema format. The schema should be a JSON object.');
}
// Populate schemaName and schemaDescription
document.getElementById('schemaName').value = schema.name.replace(/^add_/, '') || '';
document.getElementById('schemaDescription').value = schema.description || '';
// Clear existing properties except the Name Property
const propertiesContainer = document.getElementById('propertiesContainer');
propertiesContainer.innerHTML = '';
propertiesContainer.appendChild(ui.createNamePropertyElement());
// Populate the Description of the Name Property
const namePropertyDescriptionInput = document.querySelector('#property-name-property .property-description');
if (namePropertyDescriptionInput && schema.properties && schema.properties.name) {
namePropertyDescriptionInput.value = schema.properties.name.description || '';
}
// Add properties from schema
if (schema.properties) {
for (const [propName, propDetails] of Object.entries(schema.properties)) {
if (propName === 'name') continue; // Skip the Name Property
const propertyElement = ui.generatePropertyHTML(propName, propDetails);
propertiesContainer.appendChild(propertyElement);
}
}
// Switch to the Create Schema tab
ui.switchTab('create');
showToast('Schema loaded into Create Schema tab for editing.', 'success');
} catch (error) {
showToast('Error editing schema: ' + error.message, 'error');
}
},
generatePropertyHTML: (propName, propDetails) => {
const propertyElement = ui.createPropertyElement();
// Set values for the dynamically created property
const nameInput = propertyElement.querySelector('.property-name');
const descriptionInput = propertyElement.querySelector('.property-description');
const typeSelect = propertyElement.querySelector('.property-type');
const requiredCheckbox = propertyElement.querySelector('.property-required');
const enumCheckbox = propertyElement.querySelector('.property-enum');
const enumInput = propertyElement.querySelector('.enum-values-input');
const relationshipCheckbox = propertyElement.querySelector('.property-relationship');
const edgeTypeInput = propertyElement.querySelector('.edge-type');
nameInput.value = propName;
descriptionInput.value = propDetails.description || '';
typeSelect.value = propDetails.type || 'string';
requiredCheckbox.checked = propDetails.required || false;
if (propDetails.enum && Array.isArray(propDetails.enum)) {
enumCheckbox.checked = true;
enumInput.value = propDetails.enum.join(', ');
enumInput.style.display = 'block';
}
if (propDetails.relationship && propDetails.relationship.edgeType) {
relationshipCheckbox.checked = true;
edgeTypeInput.value = propDetails.relationship.edgeType;
edgeTypeInput.style.display = 'block';
}
return propertyElement;
},
editSchema: () => {
const select = document.getElementById('loadSchemaSelect');
const selectedFile = select.value;
if (!selectedFile) {
showToast('Please select a schema to edit.', 'error');
return;
}
ui.loadSchemaIntoCreateTab(selectedFile);
}
};
// --- Event Handlers ---
function setupEventListeners() {
document.getElementById('selectDirectoryBtn').addEventListener('click', fileSystem.selectDirectory);
document.getElementById('addPropertyBtn').addEventListener('click', ui.addProperty);
document.getElementById('viewRawSchemaBtn').addEventListener('click', schemaGenerator.generateSchema);
document.getElementById('resetFormBtn').addEventListener('click', ui.resetForm);
document.getElementById('saveSchemaBtn').addEventListener('click', fileSystem.saveSchema);
document.getElementById('deleteSchemaBtn').addEventListener('click', fileSystem.deleteSchema);
document.getElementById('editSchemaBtn').addEventListener('click', ui.editSchema);
document.getElementById('schemaName').addEventListener('input', async (event) => {
const schemaName = event.target.value;
const isUnique = await schemaValidator.isSchemaNameUnique(schemaName);
if (!isUnique) {
showToast('Schema name must be unique', 'error');
}
});
document.querySelectorAll('.tab').forEach(tab => {
tab.addEventListener('click', () => ui.switchTab(tab.dataset.tab));
});
document.getElementById('loadSchemaSelect').addEventListener('change', (e) => {
if (e.target.value) {
fileSystem.loadSchemaFile(e.target.value);
}
});
}
// --- Initialization ---
async function initialize() {
try {
await idb.initDB();