-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
1378 lines (1159 loc) · 43.8 KB
/
Copy pathpopup.js
File metadata and controls
1378 lines (1159 loc) · 43.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
let files = {}; // { filename: content }
let fileList = [];
let editingConstraintName = null; // The name of the constraint currently being edited(nullIndicates new)
let currentIndex = -1;
const dropZone = document.getElementById("drop-zone");
const promptBox = document.getElementById("prompt");
const dependBox = document.getElementById("depend");
const suggestionList = document.getElementById("suggestion-list");
const createFileModal = document.getElementById("create-file-modal");
const defaultSettings = {
maxTotalStorageKB: 2000,
autoCleanupEnabled: true,
maxFileAgeDays: 30,
maxFileSizeKB: 500,
maxFilesCount: 20,
maxConstraintWords: 1000,
maxConstraintsCount: 10,
autoCleanupPromptEnabled: true,
maxPromptAgeDays: 7, // prompt Cache time is usually shorter than file
maxPromptKB: 200
};
const maxPromptAge = getSetting('maxPromptAgeDays') * 24 * 60 * 60 * 1000;
// 🔹 Restore on page load
window.addEventListener("DOMContentLoaded", () => {
const savedFiles = localStorage.getItem("ai_paste_files");
const savedFileList = localStorage.getItem("ai_paste_fileList");
if (savedFiles) files = JSON.parse(savedFiles);
if (savedFileList) fileList = JSON.parse(savedFileList);
initPromptBox()
initDependBox()
// Initialize file timestamps and automatic cleanup
initFileTimestamps();
if (getSetting('autoCleanupEnabled')) {
const cleanedCount = autoCleanupFiles();
if (cleanedCount > 0) {
console.log(`Auto cleaned ${cleanedCount} expired files`);
}
resetStatusToDefault();
}
updateFileList();
});
function initPromptBox() {
// examine prompt Is it expired?
const savedPrompt = localStorage.getItem("ai_paste_prompt");
const savedPromptTimestamp = localStorage.getItem("ai_paste_promptTimestamp");
const promptAge = savedPromptTimestamp ? Date.now() - parseInt(savedPromptTimestamp) : Infinity;
if (savedPrompt && promptAge <= maxPromptAge) {
promptBox.innerHTML = savedPrompt;
} else if (savedPromptTimestamp && promptAge > maxPromptAge) {
// prompt Expired,clean up
localStorage.removeItem("ai_paste_prompt");
localStorage.removeItem("ai_paste_promptTimestamp");
promptBox.innerHTML = "";
console.log("Cleaned expired prompt");
} else {
promptBox.innerHTML = "";
}
if (!promptBox.innerHTML.trim()) {
promptBox.innerHTML = `<div style="color: #666; font-style: italic;">
Task objective goes here. You can reference files using @filename tokens.
</div>`;
// Clear sample text on click
promptBox.addEventListener('focus', function clearExample() {
if (this.innerHTML.includes('Task objective goes here')) {
this.innerHTML = '';
}
this.removeEventListener('focus', clearExample);
});
}
}
function initDependBox() {
// New:initializationDependPanel content
const savedDepend = localStorage.getItem("ai_paste_depend");
const savedDependTimestamp = localStorage.getItem("ai_paste_dependTimestamp");
if (savedDepend) {
const dependAge = savedDependTimestamp ? Date.now() - parseInt(savedDependTimestamp) : Infinity;
const maxDependAge = getSetting('maxPromptAgeDays') * 24 * 60 * 60 * 1000;
if (dependAge <= maxDependAge) {
document.getElementById("depend").innerHTML = savedDepend;
} else {
localStorage.removeItem("ai_paste_depend");
localStorage.removeItem("ai_paste_dependTimestamp");
document.getElementById("depend").innerHTML = "";
}
}
if (!dependBox.innerHTML.trim()) {
dependBox.innerHTML = `<div style="color: #666; font-style: italic;">
Example format:
- main.py (target)
- depends on utils.py
- should follow structure of main-bk.py
- utils.py (reference)
- contains helper functions
</div>`;
// Clear sample text on click
dependBox.addEventListener('focus', function clearExample() {
if (this.innerHTML.includes('Example format')) {
this.innerHTML = '';
}
this.removeEventListener('focus', clearExample);
});
}
}
// Create File Button click event
document.getElementById("create-file-btn").addEventListener("click", () => {
document.getElementById("new-filename").value = "";
document.getElementById("new-filecontent").value = "";
createFileModal.classList.remove("hidden");
});
// Cancel button
document.getElementById("cancel-new-file").addEventListener("click", () => {
createFileModal.classList.add("hidden");
});
// Save button
document.getElementById("save-new-file").addEventListener("click", async () => {
const filename = document.getElementById("new-filename").value.trim();
const desc = document.getElementById("new-filedesc").value.trim();
const content = document.getElementById("new-filecontent").value;
if (!filename) {
showStatus("File name can not be empty", false);
return;
}
const checkResult = checkStorageLimits(content, filename, false);
if (!checkResult.isValid) {
showStatus(`Saving failed: ${checkResult.errors.join('; ')}`, false);
return;
}
if (files[filename]) {
const overwrite = await showConfirmModal(`File "${filename}" already exists. Replace it?`);
if (!overwrite) {
return; // User cancels → Do not replace
}
}
// Show warning
if (checkResult.warnings.length > 0) {
showStatus(`Warning: ${checkResult.warnings.join('; ')}`, 'warning');
}
files[filename] = {
content: content,
desc: desc || "", // Default empty
time: Date.now()
};
if (!fileList.includes(filename)) {
fileList.push(filename);
}
updateFileList();
createFileModal.classList.add("hidden");
showStatus(`File ${filename} created`, true);
});
document.getElementById("copy-btn").addEventListener("click", async () => {
// 1. Extract using new functiontask(Contains only filename references)
const mainTask = extractFileReferences(promptBox);
// 2. Extract using new functiondependencies(Contains only filename references)
const dependencies = extractFileReferences(dependBox);
// 3. collect alltokenReferenced file content
const fileContents = collectAllFileContents(promptBox, dependBox);
// 4. Collection constraints(keep it the same way)
const constraints = collectConstraints();
// 5. Assemble according to template
const finalText = buildTemplate({
task: mainTask,
dependencies: dependencies,
constraints: constraints,
files: fileContents
});
// 5. copy to clipboard(original logic)
try {
await navigator.clipboard.writeText(finalText);
showStatus("Prompt copied with template!", true);
saveCurrentPrompt();
saveCurrentDepend();
} catch (err) {
console.error("Copy failed:", err);
showStatus("Copy failed, please copy manually.", false);
}
});
// ----------------- Drag and drop files(Same as before) -----------------
dropZone.addEventListener("dragover", e => {
e.preventDefault();
dropZone.style.borderColor = "#333";
});
dropZone.addEventListener("dragleave", () => dropZone.style.borderColor = "#888");
dropZone.addEventListener("drop", e => {
e.preventDefault();
e.stopPropagation();
dropZone.style.borderColor = "#888";
const droppedFiles = e.dataTransfer.files;
for (let f of droppedFiles) {
const reader = new FileReader();
reader.onload = () => {
const content = reader.result;
const checkResult = checkStorageLimits(content, f.name, false);
if (!checkResult.isValid) {
showStatus(`Failed to add ${checkResult.errors.join('; ')}`, false);
return;
}
if (files[f.name]) {
const overwrite = confirm(`File "${filename}" already exists. Replace it?`);
if (!overwrite) {
return; // User cancels → Do not replace
}
}
// Show warning
if (checkResult.warnings.length > 0) {
showStatus(`Warning: ${checkResult.warnings.join('; ')}`, 'warning');
}
files[f.name] = {
content: content,
desc: "", // Default empty
deps: [],
time: Date.now()
};
if (!fileList.includes(f.name)) fileList.push(f.name);
updateFileTimestamp(f.name); // For drag and drop files
updateFileList();
showStatus(`Added ${f.name}`, true);
};
reader.readAsText(f);
}
});
promptBox.addEventListener("mouseup", saveCurrentPrompt);
promptBox.addEventListener("keyup", function(e) {
handleEditorKeyup(e, promptBox);
});
promptBox.addEventListener("keydown", function(e) {
handleEditorKeydown(e, promptBox);
});
promptBox.addEventListener('blur', () => {
// Save immediately when out of focus
saveCurrentDepend();
saveCurrentPrompt();
});
dependBox.addEventListener("mouseup", saveCurrentDepend);
dependBox.addEventListener("keyup", function(e) {
handleEditorKeyup(e, dependBox);
});
dependBox.addEventListener("keydown", function(e) {
handleEditorKeydown(e, dependBox);
});
dependBox.addEventListener('blur', () => {
// Save immediately when out of focus
saveCurrentDepend();
saveCurrentPrompt();
});
// Initialization file timestamp
function initFileTimestamps() {
if (!localStorage.getItem("ai_paste_fileTimestamps")) {
const timestamps = {};
fileList.forEach(filename => {
timestamps[filename] = Date.now();
});
localStorage.setItem("ai_paste_fileTimestamps", JSON.stringify(timestamps));
}
}
// Update file timestamp(Adding/Called when a file is modified)
function updateFileTimestamp(filename) {
const timestamps = JSON.parse(localStorage.getItem("ai_paste_fileTimestamps") || "{}");
timestamps[filename] = Date.now();
localStorage.setItem("ai_paste_fileTimestamps", JSON.stringify(timestamps));
}
// Storage check utility function
function checkStorageLimits(newFileContent = '', newFileName = '', isCommand = false) {
const settings = {...defaultSettings, ...loadSettings()};
const errors = [];
const warnings = [];
if (!isCommand) {
// File size check
if (newFileContent.length > settings.maxFileSizeKB * 1024) {
errors.push(`File size exceeds limit: ${(newFileContent.length/1024).toFixed(1)}KB > ${settings.maxFileSizeKB}KB`);
}
// File quantity check
const willExceedCount = !files[newFileName] && fileList.length >= settings.maxFilesCount;
if (willExceedCount) {
errors.push(`The number of files reached the upper limit: ${settings.maxFilesCount}`);
}
} else {
// Command length check (by number of words)
const wordCount = newFileContent.trim().split(/\s+/).length;
if (wordCount > settings.maxConstraintWords) {
errors.push(`Command length exceeds limit: ${wordCount} words > ${settings.maxConstraintWords} words`);
}
// Command number check
const commandCount = Object.keys(customCmds).length;
const willExceedCommands = !customCmds[newFileName] && commandCount >= settings.maxConstraintsCount;
if (willExceedCommands) {
errors.push(`The number of commands reaches the upper limit: ${settings.maxConstraintsCount}`);
}
}
// Total storage space check
const currentTotal = Object.values(files).reduce((sum, content) => sum + content.length, 0);
const currentSize = files[newFileName] ? files[newFileName].length : 0;
const newTotal = currentTotal + newFileContent.length - currentSize;
if (newTotal > settings.maxTotalStorageKB * 1024) {
errors.push(`Insufficient total storage space: ${(newTotal/1024).toFixed(1)}KB > ${settings.maxTotalStorageKB}KB`);
}
// warning check (Exceed80%)
const usagePercent = (newTotal / (settings.maxTotalStorageKB * 1024)) * 100;
if (usagePercent > 80) {
warnings.push(`Storage space usage: ${usagePercent.toFixed(1)}%`);
}
return { isValid: errors.length === 0, errors, warnings };
}
function handleEditorKeyup(e, editor) {
saveSelectionRange(editor);
if (e.key === "@") {
showSuggestionListFor(editor, true);
} else if (
e.key.length === 1 ||
e.key === "Backspace" ||
e.key === "Delete"
) {
const sel = window.getSelection();
if (sel && sel.rangeCount) {
const r = sel.getRangeAt(0);
const prefix = getTextBeforeCaret(r, 30, editor);
const match = prefix.match(/@([^\s]*)$/);
if (match) {
showSuggestionListFor(editor, false, match[1]);
} else {
hideSuggestionListFor(editor);
}
}
}
}
function handleEditorKeydown(e, editor) {
const suggestionList = editor === promptBox ?
document.getElementById("suggestion-list") :
document.getElementById("depend-suggestion-list");
if (suggestionList.classList.contains("hidden")) return;
const items = suggestionList.querySelectorAll("li");
if (items.length === 0) return;
if (e.key === "ArrowDown") {
e.preventDefault();
currentIndex = (currentIndex + 1) % items.length;
highlightSuggestionItem(suggestionList);
} else if (e.key === "ArrowUp") {
e.preventDefault();
currentIndex = (currentIndex - 1 + items.length) % items.length;
highlightSuggestionItem(suggestionList);
} else if (e.key === "Enter") {
if (currentIndex >= 0 && currentIndex < items.length) {
e.preventDefault();
if (editor === promptBox) {
restorePromptSelection()
} else {
restoreDependSelection()
}
const li = items[currentIndex];
if (li.dataset.type === "file") {
insertFileToken(li.dataset.filename, editor);
} else if (li.dataset.type === "cmd") {
insertCmdToken(li.dataset.filename, editor);
}
hideSuggestionListFor(editor);
}
}
}
function getTextBeforeCaret(range, maxChars, container) {
try {
const r = range.cloneRange();
r.collapse(true);
r.setStart(container, 0);
let txt = r.toString();
if (txt.length > maxChars) txt = txt.slice(-maxChars);
return txt;
} catch (err) {
return "";
}
}
// ----------------- insert token -----------------
function insertCmdToken(cmdKey, editor) {
if (editor === promptBox) {
restorePromptSelection()
} else {
restoreDependSelection()
}
removePrecedingAtChar(editor);
const token = document.createElement("span");
token.className = "cmd-token";
token.setAttribute("data-cmd", cmdKey);
token.contentEditable = "false";
token.innerHTML = `<svg viewBox="0 0 16 16" width="12" height="12" style="vertical-align:middle;margin-right:4px;">
<circle cx="8" cy="8" r="8" fill="#8bc34a"></circle>
</svg>${cmdKey}`;
const sel = window.getSelection();
let range;
if (sel && sel.rangeCount > 0) range = sel.getRangeAt(0);
else range = document.createRange();
range.deleteContents();
range.insertNode(token);
const afterSpace = document.createTextNode("\u00A0");
token.parentNode.insertBefore(afterSpace, token.nextSibling);
const newRange = document.createRange();
newRange.setStartAfter(afterSpace);
newRange.collapse(true);
sel.removeAllRanges();
sel.addRange(newRange);
saveSelectionRange(editor);
}
// ----------------- insert token -----------------
function insertFileToken(filename, editor) {
if (editor === promptBox) {
restorePromptSelection()
} else {
restoreDependSelection()
}
removePrecedingAtChar(editor);
const token = document.createElement("span");
token.className = "file-token";
token.setAttribute("data-filename", filename);
token.contentEditable = "false";
token.innerHTML = `<svg viewBox="0 0 16 16" width="12" height="12" style="vertical-align:middle;margin-right:4px;">
<rect width="16" height="16" fill="#bbb"></rect>
</svg>${filename}`;
const sel = window.getSelection();
let range;
if (sel && sel.rangeCount > 0) range = sel.getRangeAt(0);
else range = document.createRange();
range.deleteContents();
range.insertNode(token);
const afterSpace = document.createTextNode("\u00A0");
token.parentNode.insertBefore(afterSpace, token.nextSibling);
const newRange = document.createRange();
newRange.setStartAfter(afterSpace);
newRange.collapse(true);
sel.removeAllRanges();
sel.addRange(newRange);
saveSelectionRange(editor);
}
function removePrecedingAtChar(editor) {
const sel = window.getSelection();
if (!sel || sel.rangeCount === 0) return;
const range = sel.getRangeAt(0);
const sc = range.startContainer;
const offset = range.startOffset;
// Make sure we are operating on the contents of the current editor
if (sc.nodeType === Node.TEXT_NODE &&
editor.contains(sc) &&
offset > 0 &&
sc.data[offset-1] === "@") {
sc.deleteData(offset-1, 1);
const newRange = document.createRange();
newRange.setStart(sc, offset-1);
newRange.collapse(true);
sel.removeAllRanges();
sel.addRange(newRange);
}
}
// ----------------- Copy button -----------------
function showStatus(msg, status = 'default') {
const statusEl = document.getElementById("copy-status");
// clear timer
clearTimeout(statusEl._timer);
if (msg) {
let icon, background, border, textColor;
switch(status) {
case true: // success
icon = '✓';
background = '#e8f5e9';
border = '1px solid #c8e6c9';
textColor = '#388e3c';
break;
case false: // mistake
icon = '✗';
background = '#ffebee';
border = '1px solid #ffcdd2';
textColor = '#d32f2f';
break;
case 'warning': // warn
icon = '⚠';
background = '#fff3e0';
border = '1px solid #ffcc80';
textColor = '#ff9800';
break;
default: // default
icon = '🔍';
background = '#ebf8ff';
border = '1px solid #bee3f8';
textColor = '#222';
}
statusEl.innerHTML = `<span style="color:${textColor};font-weight:bold;">${icon}</span> <span style="color:#222;">${msg}</span>`;
statusEl.style.background = background;
statusEl.style.border = border;
statusEl.style.borderRadius = "6px";
statusEl.style.padding = "4px 12px";
// Set timer
const timeout = status === 'warning' ? 4000 : 2000;
statusEl._timer = setTimeout(() => {
resetStatusToDefault();
}, timeout);
} else {
resetStatusToDefault();
}
}
// New:Function to reset to default state
function resetStatusToDefault() {
const status = document.getElementById("copy-status");
status.innerHTML = `
<span class="status-default">
<span class="status-icon">🔍</span>
<span class="status-text">What are you up to?
</span>
`;
status.style.background = '#ebf8ff';
status.style.border = '1px solid #bee3f8';
status.style.borderRadius = "6px";
status.style.padding = "4px 12px";
}
function updateFileList() {
const fileListDiv = document.getElementById("file-list");
if (fileList.length === 0) {
fileListDiv.textContent = "(No files in the list)";
} else {
fileListDiv.innerHTML = fileList.map(name => `
<div class="file-item" draggable="true" data-filename="${name}">
<a href="#" class="file-link" data-fname="${name}">
${shortenFileName(name)}
</a>
${files[name].desc ? "" : `
<span class="missing-desc"
style="color:red;cursor:help;margin-left:4px;"
title="The file is missingdescription,Click on the file name to adddescription">!
</span>`}
<span>${formatFileSize(files[name].content)}</span>
<span class="delete-file" style="cursor:pointer;color:red;margin-left:5px;">x</span>
</div>
`).join("");
}
// 🔹 save to localStorage
localStorage.setItem("ai_paste_files", JSON.stringify(files));
localStorage.setItem("ai_paste_fileList", JSON.stringify(fileList));
document.querySelectorAll(".file-link").forEach(a => {
a.addEventListener("click", (e) => {
e.preventDefault();
const fname = a.dataset.fname;
openEditFileModal(fname);
});
});
}
function openEditFileModal(fname) {
const modal = document.getElementById("create-file-modal");
modal.classList.remove("hidden");
document.getElementById("new-filename").value = fname;
//document.getElementById("new-filename").disabled = true; // Name change not allowed
document.getElementById("new-filedesc").value = files[fname].desc || "";
document.getElementById("new-filecontent").value = files[fname].content || "";
// Modify title
modal.querySelector("h3").innerText = "Edit File";
// Change the save button to“renew”
document.getElementById("save-new-file").onclick = function() {
files[fname].desc = document.getElementById("new-filedesc").value.trim();
files[fname].content = document.getElementById("new-filecontent").value;
modal.classList.add("hidden");
updateFileList();
};
}
// --- Quick command management ---
let customCmds = JSON.parse(localStorage.getItem("ai_paste_cmds") || "{}");
// Open pop-up window
document.getElementById("customize-btn").onclick = function() {
renderCmdTable();
document.getElementById("customize-modal").classList.remove("hidden");
};
// Close pop-up window
document.getElementById("close-cmds").onclick = function() {
document.getElementById("customize-modal").classList.add("hidden");
};
// Delete row
document.querySelector("#cmd-table tbody").onclick = function(e) {
if (e.target.classList.contains("del-cmd")) {
e.target.closest("tr").remove();
}
};
// Open the settings modal box
document.getElementById("settings-btn").addEventListener("click", () => {
renderSettingsForm();
document.getElementById("settings-modal").classList.remove("hidden");
});
// Close settings
document.getElementById("close-settings").addEventListener("click", () => {
document.getElementById("settings-modal").classList.add("hidden");
});
// Save settings
document.getElementById("save-settings").addEventListener("click", () => {
saveCurrentSettings();
});
// Reset settings
document.getElementById("reset-settings").addEventListener("click", () => {
if (confirm("Reset to default settings?")) {
saveSettings({});
document.getElementById("settings-modal").classList.add("hidden");
}
});
// Automatically clean up checkbox change events
document.getElementById("auto-cleanup-enabled").addEventListener("change", (e) => {
document.getElementById("max-file-age-days").disabled = !e.target.checked;
});
// Clean files manually
document.getElementById("purge-files").addEventListener("click", purgeAllFiles);
// prompt Automatically clean up checkbox change events
document.getElementById("auto-cleanup-prompt-enabled").addEventListener("change", (e) => {
document.getElementById("max-prompt-age-days").disabled = !e.target.checked;
});
// New constraint button
document.getElementById("add-new-constraint").addEventListener("click", () => {
openConstraintEditModal(); // If no parameters are passed, it means creating a new one.
});
// Constraint edit modal event
document.getElementById("cancel-constraint-edit").addEventListener("click", () => {
document.getElementById("constraint-edit-modal").classList.add("hidden");
});
document.getElementById("save-constraint").addEventListener("click", saveConstraint);
// Using event delegation to handle editing and deletion of constraint lists
document.querySelector("#cmd-table tbody").addEventListener("click", (e) => {
const constraintName = e.target.dataset.name;
if (e.target.classList.contains("edit-constraint")) {
// Edit constraints
openConstraintEditModal(constraintName, customCmds[constraintName]);
} else if (e.target.classList.contains("remove-constraint")) {
// Delete constraints
if (confirm(`Remove constraint "${constraintName}"?`)) {
delete customCmds[constraintName];
localStorage.setItem("ai_paste_cmds", JSON.stringify(customCmds));
renderCmdTable();
showStatus(`Constraint "${constraintName}" removed`, 'warning');
}
}
});
function renderCmdTable() {
const tbody = document.querySelector("#cmd-table tbody");
tbody.innerHTML = "";
Object.entries(customCmds).forEach(([name, content]) => {
const tr = document.createElement("tr");
tr.innerHTML = `
<td style="width: 60%; min-width: 40px; text-align: left;font-weight: bold">@${name}</td>
<td style="width: 40%; min-width: 120px; text-align: right;white-space: nowrap;">
<button class="edit-constraint modal-btn small" data-name="${name}" style="width: 60px;">Edit</button>
<button class="remove-constraint modal-btn small danger" data-name="${name}" style="width: 60px;">Remove</button>
</td>
`;
tbody.appendChild(tr);
});
}
// --- exist@Support commands when completing ---
function showSuggestionListFor(editor, resetIndex = true, keyword = "") {
const suggestionList = editor === promptBox ?
document.getElementById("suggestion-list") :
document.getElementById("depend-suggestion-list");
// Hide another panel's suggestion list first
if (editor === promptBox) {
document.getElementById("depend-suggestion-list").classList.add("hidden");
} else {
document.getElementById("suggestion-list").classList.add("hidden");
}
suggestionList.innerHTML = "";
// Merge files and commands
const allList = [
...fileList.map(name => ({type:"file", name})),
...Object.keys(customCmds).map(name => ({type:"cmd", name}))
];
const filteredList = keyword
? allList.filter(item => item.name.toLowerCase().includes(keyword.toLowerCase()))
: allList;
if (filteredList.length === 0) {
const li = document.createElement("li");
li.textContent = "(List is empty)";
suggestionList.appendChild(li);
} else {
filteredList.forEach((item, idx) => {
const li = document.createElement("li");
li.textContent = item.name + (item.type === "cmd" ? " (cmd)" : "");
li.dataset.filename = item.name;
li.dataset.type = item.type;
li.addEventListener("pointerdown", (evt) => {
evt.preventDefault();
if (editor === promptBox) {
restorePromptSelection()
} else {
restoreDependSelection()
}
if (item.type === "file") {
insertFileToken(item.name, editor);
} else {
insertCmdToken(item.name, editor);
}
hideSuggestionListFor(editor);
});
suggestionList.appendChild(li);
});
}
// ...Subsequent positioning and highlighting logic remains unchanged...
// Calculate cursor position,Let the list hover under the cursor
let top = 0, left = 0;
let alignLeft = true; // Defaults to the right of the cursor
const sel = window.getSelection();
if (sel && sel.rangeCount > 0) {
const range = sel.getRangeAt(0).cloneRange();
saveSelectionRange();
const tempSpan = document.createElement("span");
tempSpan.textContent = "\u200b";
range.insertNode(tempSpan);
const rect = tempSpan.getBoundingClientRect();
const promptRect = promptBox.getBoundingClientRect();
const viewportWidth = window.innerWidth;
// base position(Cursor lower right corner)
top = rect.bottom + window.scrollY;
left = rect.left + window.scrollX;
// suggestion Estimated size of list
const suggestionWidth = 200; // andCSSset inwidthconsistent
const suggestionHeight = Math.min(suggestionList.scrollHeight, 150); // andCSSmiddlemaxHeightconsistent
// Check if there is enough space on the right
const spaceOnRight = viewportWidth - rect.right;
const spaceOnLeft = rect.left;
// If there is not enough space on the right,But there is enough space on the left side,is displayed on the left
if (spaceOnRight < suggestionWidth && spaceOnLeft >= suggestionWidth) {
alignLeft = false;
left = rect.left + window.scrollX - suggestionWidth;
}
// If there is not enough space on both sides,Choose the side with more space
else if (spaceOnRight < suggestionWidth && spaceOnLeft < suggestionWidth) {
alignLeft = spaceOnRight >= spaceOnLeft;
if (!alignLeft) {
left = rect.left + window.scrollX - suggestionWidth;
}
}
// Vertical adjustment:Make sure not to exceed the visible area
const spaceBelow = window.innerHeight - rect.bottom;
const spaceAbove = rect.top;
// If there is not enough space below,But there is enough space above,is displayed above
if (spaceBelow < suggestionHeight && spaceAbove >= suggestionHeight) {
top = rect.top + window.scrollY - suggestionHeight;
}
// If there is not enough space up and down,Choose the side with more space
else if (spaceBelow < suggestionHeight && spaceAbove < suggestionHeight) {
if (spaceAbove >= spaceBelow) {
top = rect.top + window.scrollY - suggestionHeight;
}
}
// Make sure not to exceed prompt box bounds
if (top < promptRect.top + window.scrollY) {
top = promptRect.top + window.scrollY;
}
if (top + suggestionHeight > promptRect.bottom + window.scrollY) {
top = promptRect.bottom + window.scrollY - suggestionHeight;
}
// Make sure you don't exceed the viewport boundaries
if (left < 0) left = 5;
if (left + suggestionWidth > viewportWidth) {
left = viewportWidth - suggestionWidth - 5;
}
tempSpan.remove();
if (editor === promptBox) {
restorePromptSelection()
} else {
restoreDependSelection()
}
} else {
// When the cursor is not available,Default in prompt center bottom
const rect = promptBox.getBoundingClientRect();
top = rect.bottom + window.scrollY;
left = rect.left + window.scrollX + 10;
}
// Position after applying calculation
suggestionList.style.position = "absolute";
suggestionList.style.top = `${top}px`;
suggestionList.style.left = `${left}px`;
suggestionList.style.width = "200px";
suggestionList.style.maxHeight = "150px";
suggestionList.style.overflowY = "auto";
suggestionList.style.zIndex = 10000;
suggestionList.classList.remove("hidden");
if (resetIndex) {
currentIndex = 0;
}
highlightSuggestionItem(suggestionList);
}
function hideSuggestionListFor(editor) {
const suggestionList = editor === promptBox ?
document.getElementById("suggestion-list") :
document.getElementById("depend-suggestion-list");
suggestionList.classList.add("hidden");
currentIndex = -1;
}
function highlightSuggestionItem(suggestionList) {
const items = suggestionList.querySelectorAll("li");
items.forEach((el, idx) => {
el.classList.toggle("highlight", idx === currentIndex);
if (idx === currentIndex) {
el.scrollIntoView({ block: "nearest" });
}
});
}
function hideSuggestionListFor(editor) {
const suggestionList = editor === promptBox ?
document.getElementById("suggestion-list") :
document.getElementById("depend-suggestion-list");
suggestionList.classList.add("hidden");
currentIndex = -1;
}
function highlightSuggestionItem(suggestionList) {
const items = suggestionList.querySelectorAll("li");
items.forEach((el, idx) => {
el.classList.toggle("highlight", idx === currentIndex);
if (idx === currentIndex) {
el.scrollIntoView({ block: "nearest" });
}
});
}
function loadSettings() {
return JSON.parse(localStorage.getItem("ai_paste_settings") || "{}");
}
function saveSettings(settings) {
localStorage.setItem("ai_paste_settings", JSON.stringify(settings));
}
function getSetting(key) {
const settings = loadSettings();
return settings[key] !== undefined ? settings[key] : defaultSettings[key];
}
function renderSettingsForm() {
const settings = {...defaultSettings, ...loadSettings()};
// Numeric settings
document.getElementById("max-total-storage-kb").value = settings.maxTotalStorageKB;
document.getElementById("max-file-size-kb").value = settings.maxFileSizeKB;
document.getElementById("max-files-count").value = settings.maxFilesCount;
document.getElementById("max-file-age-days").value = settings.maxFileAgeDays;
document.getElementById("max-constraint-words").value = settings.maxConstraintWords;
document.getElementById("max-constraints-count").value = settings.maxConstraintsCount;
document.getElementById("max-prompt-kb").value = settings.maxPromptKB;
document.getElementById("max-prompt-age-days").value = settings.maxPromptAgeDays;
// Switch type setting
document.getElementById("auto-cleanup-enabled").checked = settings.autoCleanupEnabled;
document.getElementById("auto-cleanup-prompt-enabled").checked = settings.autoCleanupPromptEnabled;
// Set the disabled state of the relevant input according to the switch state
document.getElementById("max-file-age-days").disabled = !settings.autoCleanupEnabled;
document.getElementById("max-prompt-age-days").disabled = !settings.autoCleanupPromptEnabled;
}
function saveCurrentSettings() {
const settings = {
maxTotalStorageKB: parseInt(document.getElementById("max-total-storage-kb").value),
maxFileSizeKB: parseInt(document.getElementById("max-file-size-kb").value),
maxFilesCount: parseInt(document.getElementById("max-files-count").value),
maxFileAgeDays: parseInt(document.getElementById("max-file-age-days").value),
maxConstraintWords: parseInt(document.getElementById("max-constraint-words").value),
maxConstraintsCount: parseInt(document.getElementById("max-constraints-count").value),
maxPromptKB: parseInt(document.getElementById("max-prompt-kb").value),
maxPromptAgeDays: parseInt(document.getElementById("max-prompt-age-days").value),
autoCleanupEnabled: document.getElementById("auto-cleanup-enabled").checked,
autoCleanupPromptEnabled: document.getElementById("auto-cleanup-prompt-enabled").checked