|
6 | 6 | let chosenRecentFile: string = "";
|
7 | 7 | let targetFilePath: string = "";
|
8 | 8 | let recentFiles: string[] = [];
|
| 9 | + let contextMenuVisible: boolean = false; |
| 10 | + let contextMenuX: number = 0; |
| 11 | + let contextMenuY: number = 0; |
| 12 | + let contextMenuFile: string = ""; |
9 | 13 |
|
10 | 14 | async function setTargetFilePath(filePath: string) {
|
11 | 15 | try {
|
|
84 | 88 | }
|
85 | 89 | }
|
86 | 90 |
|
| 91 | + async function removeRecentFile(filePath: string) { |
| 92 | + try { |
| 93 | + await invoke("remove_config_recent_file", { filePath: filePath }); |
| 94 | + await invoke("save_config"); |
| 95 | + await getRecentFiles(); |
| 96 | + contextMenuVisible = false; |
| 97 | + } catch (error) { |
| 98 | + console.error("Error removing recent file:", error); |
| 99 | + } |
| 100 | + } |
| 101 | +
|
| 102 | + function showContextMenu(event: MouseEvent, filePath: string) { |
| 103 | + event.preventDefault(); |
| 104 | + contextMenuFile = filePath; |
| 105 | + contextMenuX = event.clientX; |
| 106 | + contextMenuY = event.clientY; |
| 107 | + contextMenuVisible = true; |
| 108 | + } |
| 109 | +
|
| 110 | + function hideContextMenu() { |
| 111 | + contextMenuVisible = false; |
| 112 | + } |
| 113 | +
|
| 114 | + function handleGlobalClick(event: MouseEvent) { |
| 115 | + if (contextMenuVisible && !(event.target as Element)?.closest('.context-menu')) { |
| 116 | + hideContextMenu(); |
| 117 | + } |
| 118 | + } |
| 119 | +
|
87 | 120 | onMount(async () => {
|
88 | 121 | await invoke("init_config", {});
|
89 | 122 | await getRecentFiles();
|
| 123 | + document.addEventListener('click', handleGlobalClick); |
| 124 | + |
| 125 | + return () => { |
| 126 | + document.removeEventListener('click', handleGlobalClick); |
| 127 | + }; |
90 | 128 | });
|
91 | 129 | </script>
|
92 | 130 |
|
|
133 | 171 | onchange={onChangeListBoxItemHandler}
|
134 | 172 | >
|
135 | 173 | {#each recentFiles as recentFile}
|
136 |
| - <option value={recentFile}>{recentFile}</option> |
| 174 | + <option |
| 175 | + value={recentFile} |
| 176 | + oncontextmenu={(e) => showContextMenu(e, recentFile)} |
| 177 | + > |
| 178 | + {recentFile} |
| 179 | + </option> |
137 | 180 | {/each}
|
138 | 181 | </select>
|
139 | 182 | </form>
|
140 | 183 | </div>
|
| 184 | + |
| 185 | + {#if contextMenuVisible} |
| 186 | + <div |
| 187 | + class="context-menu fixed z-50 bg-surface-800 border border-surface-600 rounded-lg shadow-lg py-1 min-w-48" |
| 188 | + style="left: {contextMenuX}px; top: {contextMenuY}px;" |
| 189 | + > |
| 190 | + <button |
| 191 | + class="w-full text-left px-4 py-2 hover:bg-surface-700 transition-colors text-surface-100" |
| 192 | + onclick={() => removeRecentFile(contextMenuFile)} |
| 193 | + > |
| 194 | + Remove from list |
| 195 | + </button> |
| 196 | + </div> |
| 197 | + {/if} |
141 | 198 | </main>
|
0 commit comments