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