master
ts 258 lines 6.4 KB
Raw
1 import { fs, path as p } from "@tauri-apps/api";
2 import { openInputModal, openRenameModal } from "../App.svelte";
3 import { addEditorTab, closeAllTabs } from "../lib/EditorTabList.svelte";
4 import { saveFile, openFile, openFolderDialog, openInExplorer, renameFile, createFolder, createFile } from "../lib/File";
5 import { appWindow } from "@tauri-apps/api/window";
6 import { info, warn } from "tauri-plugin-log-api";
7 import { fitTerminal } from "../lib/Terminal.svelte";
8 import { exit } from "@tauri-apps/api/process";
9
10 export const commands = {
11 "addEditorTab": {
12 "keybind": "Control+N",
13 "command": () => {
14 addEditorTab();
15 }
16 },
17 "openFile": {
18 "keybind": "Control+O",
19 "command": async () => {
20 await openFile();
21 }
22 },
23 "openFolder": {
24 "keybind": "Control+K",
25 "command": async () => {
26 await openFolderDialog();
27 }
28 },
29 "saveFile": {
30 "keybind": "Control+S",
31 "command": async () => {
32 await saveFile();
33 }
34 },
35 "saveFileAs": {
36 "keybind": "Control+Shift+S",
37 "command": async () => {
38 await saveFile(true);
39 }
40 },
41 "undo": {
42 "keybind": "Control+Z",
43 "disabled": "true",
44 "command": async () => {
45 //
46 }
47 },
48 "redo": {
49 "keybind": "Control+Shift+Z",
50 "disabled": "true",
51 "command": async () => {
52 //
53 }
54 },
55 "cut": {
56 "keybind": "Control+X",
57 "disabled": "true",
58 "command": async () => {
59 //
60 }
61 },
62 "copy": {
63 "keybind": "Control+C",
64 "disabled": "true",
65 "command": async () => {
66 //
67 }
68 },
69 "paste": {
70 "keybind": "Control+V",
71 "disabled": "true",
72 "command": async () => {
73 //
74 }
75 },
76 "pasteFromHistory": {
77 "keybind": "Control+Shift+V",
78 "command": () => {
79 //
80 }
81 },
82 "delete": {
83 "keybind": "Delete",
84 "disabled": "true",
85 "command": async () => {
86 return;
87 }
88 },
89 "find": {
90 "keybind": "Control+F",
91 "command": () => {
92 //
93 }
94 },
95 "replace": {
96 "keybind": "Control+H",
97 "command": () => {
98 //
99 }
100 },
101 "openCommandPallete": {
102 "keybind": "Control+Shift+P",
103 "command": () => {
104 //
105 }
106 },
107 "zoomIn": {
108 "keybind": "Control +",
109 "command": () => {
110 //
111 }
112 },
113 "zoomOut": {
114 "keybind": "Control -",
115 "command": () => {
116 //
117 }
118 },
119 "fullscreen": {
120 "keybind": "F11",
121 "disabled": "true",
122 "command": async () => {
123 if (await appWindow.isFullscreen()) {
124 appWindow.setFullscreen(false);
125 }
126 else {
127 appWindow.setFullscreen(true);
128 }
129 }
130 },
131 "runFile": {
132 "keybind": "F5",
133 "command": () => {
134 //
135 }
136 },
137 "debugFile": {
138 "keybind": "Control+F5",
139 "command": () => {
140 //
141 }
142 },
143 "stopFile": {
144 "keybind": "Shift+F5",
145 "command": () => {
146 //
147 }
148 },
149 "openNewWindow": {
150 "keybind": "Control+Shift+N",
151 "command": () => {
152 //
153 }
154 },
155 "minimizeWindow": {
156 "keybind": "",
157 "command": () => {
158 appWindow.minimize()
159 }
160 },
161 "maximizeWindow": {
162 "keybind": "",
163 "command": async () => {
164 if (await appWindow.isFullscreen()) {
165 return;
166 }
167 if (await appWindow.isMaximized()) {
168 appWindow.unmaximize();
169 }
170 else {
171 appWindow.maximize()
172 }
173 fitTerminal();
174 }
175 },
176 "closeWindow": {
177 "keybind": "Alt+F4",
178 "command": async () => {
179 await exit();
180 }
181 },
182 "closeTab": {
183 "keybind": "Control+F4",
184 "command": () => {}
185 },
186 "closeAllTabs": {
187 "keybind": "",
188 "command": () => {
189 closeAllTabs();
190 }
191 },
192 "renameFile": {
193 "keybind": "F2",
194 "command": async (filename, oldpath) => {
195 if (!await fs.exists(oldpath)) {
196 warn(`Unable to rename file. ${oldpath} does not exist.`, {file: "commands.ts", line: 193});
197 return;
198 }
199 openRenameModal(`Rename ${filename}`,
200 `Give a new name to ${filename}`,
201 [
202 {name: "Rename", action: async (name) => {await renameFile(name, oldpath)}},
203 {name: "Cancel", cancel: true, style: "danger", action: () => {}}
204 ],
205 oldpath
206 )
207 }
208 },
209 "createFolder": {
210 "keybind": "",
211 "command": (path) => {
212 openInputModal("Create New Folder",
213 `Create a new folder in ${path}`,
214 [
215 {name: "Create Folder", action: async (name) => { await createFolder(`${path}${p.sep}${name}`)}},
216 {name: "Cancel", cancel: true, action: () => {}}
217 ],
218 {label: "Folder Name"}, path)
219 }
220 },
221 "createFile": {
222 "keybind": "",
223 "command": (path) => {
224 openInputModal("Create New File",
225 `Create a new file in ${path}`,
226 [
227 {name: "Create File", action: (name) => {createFile(`${path}${p.sep}${name}`)}},
228 {name: "Cancel", cancel: true, action: () => {}}
229 ],
230 {label: "File Name"}, path)
231 }
232 },
233 "openInExplorer": {
234 "keybind": "",
235 "command": async (path) => {
236 if (!await fs.exists(path)) return;
237 openInExplorer(path);
238 }
239 }
240 }
241
242 export function registerCommand(name: string, keybind: string, command: () => void) {
243 if (commands[name]) {
244 info(`Command "${name}" already exists, skipping...`, {file: "commands.ts", line: 214});
245 return;
246 }
247 commands[name] = { "keybind": keybind, "command": command }
248 }
249
250 let keybinds = []
251 export function getKeybinds() {
252 for (const value of Object.values(commands)) {
253 if (!keybinds.includes(value)) {
254 keybinds = [...keybinds, value];
255 }
256 }
257 return keybinds;
258 }