@setoelkahfi / svara / commits / 97a9eb8

feat!: added working shortcuts

mellbacon committed Jul 8, 2023 at 14:53 UTC 97a9eb8da811390634b61c45253258cf83697b03
5 files changed +234 -87
package.json
+1
@@ -14,6 +14,7 @@
14 "@codemirror/language-data": "^6.3.1",
15 "@tauri-apps/api": "^1.2.0",
16 "codemirror": "^6.0.1",
17 + "mousetrap": "^1.6.5",
18 "sass": "^1.62.0",
19 "sortablejs": "^1.15.0",
20 "svelte-splitpanes": "^0.7.13",
src/config/commands.ts new
+162
@@ -0,0 +1,162 @@
1 +import { addEditorTab } from "../lib/EditorTabList.svelte";
2 +import { saveFile, openFile, openFolder } from "../lib/File";
3 +import { appWindow } from "@tauri-apps/api/window";
4 +
5 +export const commands = {
6 + "addEditorTab": {
7 + "keybind": "Control+N",
8 + "command": () => {
9 + addEditorTab();
10 + }
11 + },
12 + "openFile": {
13 + "keybind": "Control+O",
14 + "command": async () => {
15 + await openFile();
16 + }
17 + },
18 + "openFolder": {
19 + "keybind": "Control+K",
20 + "command": async () => {
21 + await openFolder();
22 + }
23 + },
24 + "saveFile": {
25 + "keybind": "Control+S",
26 + "command": async () => {
27 + await saveFile();
28 + }
29 + },
30 + "saveFileAs": {
31 + "keybind": "Control+Shift+S",
32 + "command": async () => {
33 + await saveFile(true);
34 + }
35 + },
36 + "undo": {
37 + "keybind": "Control+Z",
38 + "command": () => {
39 + console.warn("Feature not implemented yet.")
40 + }
41 + },
42 + "redo": {
43 + "keybind": "Control+Shift+Z",
44 + "command": () => {
45 + console.warn("Feature not implemented yet.")
46 + }
47 + },
48 + "cut": {
49 + "keybind": "Control+X",
50 + "command": () => {
51 + console.warn("Feature not implemented yet.")
52 + }
53 + },
54 + "copy": {
55 + "keybind": "Control+C",
56 + "command": () => {
57 + console.warn("Feature not implemented yet.")
58 + }
59 + },
60 + "paste": {
61 + "keybind": "Control+V",
62 + "command": () => {
63 + console.warn("Feature not implemented yet.")
64 + }
65 + },
66 + "pasteFromHistory": {
67 + "keybind": "Control+Shift+V",
68 + "command": () => {
69 + console.warn("Feature not implemented yet.")
70 + }
71 + },
72 + "delete": {
73 + "keybind": "Delete",
74 + "command": () => {
75 + console.warn("Feature not implemented yet.")
76 + }
77 + },
78 + "find": {
79 + "keybind": "Control+F",
80 + "command": () => {
81 + console.warn("Feature not implemented yet.")
82 + }
83 + },
84 + "replace": {
85 + "keybind": "Control+H",
86 + "command": () => {
87 + console.warn("Feature not implemented yet.")
88 + }
89 + },
90 + "openCommandPallete": {
91 + "keybind": "Control+Shift+P",
92 + "command": () => {
93 + console.warn("Feature not implemented yet.")
94 + }
95 + },
96 + "zoomIn": {
97 + "keybind": "Control +",
98 + "command": () => {
99 + console.warn("Feature not implemented yet.")
100 + }
101 + },
102 + "zoomOut": {
103 + "keybind": "Control -",
104 + "command": () => {
105 + console.warn("Feature not implemented yet.")
106 + }
107 + },
108 + "fullscreen": {
109 + "keybind": "F11",
110 + "command": () => {
111 + console.warn("Feature not implemented yet.")
112 + }
113 + },
114 + "runFile": {
115 + "keybind": "F5",
116 + "command": () => {
117 + console.warn("Feature not implemented yet.")
118 + }
119 + },
120 + "debugFile": {
121 + "keybind": "Control+F5",
122 + "command": () => {
123 + console.warn("Feature not implemented yet.")
124 + }
125 + },
126 + "stopFile": {
127 + "keybind": "Shift+F5",
128 + "command": () => {
129 + console.warn("Feature not implemented yet.")
130 + }
131 + },
132 + "openNewWindow": {
133 + "keybind": "Control+Shift+N",
134 + "command": () => {
135 + console.warn("Feature not implemented yet.")
136 + }
137 + },
138 + "closeWindow": {
139 + "keybind": "Alt+F4",
140 + "command": async () => {
141 + await appWindow.close();
142 + }
143 + }
144 +}
145 +
146 +export function registerCommand(name: string, keybind: string, command: () => void) {
147 + if (commands[name]) {
148 + console.warn(`Command "${name}" already exists, skipping.`);
149 + return;
150 + }
151 + commands[name] = { "keybind": keybind, "command": command }
152 +}
153 +
154 +let keybinds = []
155 +export function getKeybinds() {
156 + for (const value of Object.values(commands)) {
157 + if (!keybinds.includes(value)) {
158 + keybinds = [...keybinds, value];
159 + }
160 + }
161 + return keybinds;
162 +}
src/config/config.ts
+39 -58
@@ -1,4 +1,6 @@
1 import { writable } from "svelte/store";
2 +import Mousetrap from "mousetrap";
3 +import { getKeybinds } from "./commands";
4
5 export const systemfonts = writable([]);
6 export const editorfont = writable("");
@@ -6,69 +8,48 @@ export const windowtheme = writable("");
8 export let font = "";
9 export const autosave = writable(false);
10
9 -export function getShortcut({ primaryKey, secondaryKey, modifier }) {
10 - if (modifier) {
11 - modifier = `${modifier} + `;
11 +// mousetrap is outdated and i hate the lowercase keymaps but cba to go into the code and fix everything so this will do
12 +function parseKeybind(keybind: string) {
13 + const keys = keybind.split("+");
14 + const keymap = {
15 + "Shift": "shift",
16 + "Control": "ctrl",
17 + "Alt": "alt",
18 + "Meta": "meta",
19 + "Backspace": "backspace",
20 + "Tab": "tab",
21 + "Enter": "enter",
22 + "Capslock": "capslock",
23 + "Escape": "escape",
24 + "Space": "space",
25 + "Pageup": "pageup",
26 + "Pagedown": "pagedown",
27 + "Home": "home",
28 + "Delete": "del",
29 + "End": "end",
30 + "+": "up",
31 + "-": "down"
32 }
13 - if (secondaryKey) {
14 - secondaryKey = `${secondaryKey} + `;
33 + for (const key of keys) {
34 + if (keymap[key]) {
35 + keybind = keybind.replace(key, keymap[key]);
36 + }
37 + else {
38 + keybind = keybind.replace(key, key.toLowerCase());
39 + }
40 }
16 - return `${modifier}${secondaryKey}${primaryKey}`;
41 + return keybind;
42 }
43
19 -export function executeEditorShortcut(shortcut) {
20 - switch (shortcut) {
21 - case "save-file-shortcut":
22 - console.log(shortcut);
23 - break;
24 - case "save-file-as-shortcut":
25 - console.log(shortcut);
26 - break;
27 - case "find-shortcut":
28 - console.log(shortcut);
29 - break;
30 - case "select-all-text-shortcut":
31 - break;
32 - }
33 -}
34 -export async function executeWindowShortcut(shortcut) {
35 - switch (shortcut) {
36 - case "new-file-shortcut":
37 - console.log(shortcut);
38 - break;
39 - case "open-file-shortcut":
40 - console.log(shortcut);
41 - break;
42 - case "open-folder-shortcut":
43 - console.log(shortcut);
44 - break;
45 - case "build-shortcut":
46 - console.log(shortcut);
47 - break;
48 - case "run-shortcut":
49 - console.log(shortcut);
50 - break;
51 - case "run-no-debug-shortcut":
52 - console.log(shortcut);
53 - break;
44 +export async function getShortcuts() {
45 + const shortcuts = getKeybinds();
46 + for (const shortcut of shortcuts) {
47 + const keybind = parseKeybind(shortcut.keybind);
48 + Mousetrap.bind(keybind, async (e) => {e.preventDefault(); await fireAction(shortcut.command)});
49 }
50 }
51
57 -export function getTime() {
58 - let time = new Intl.DateTimeFormat('en-US', {dateStyle: "short", timeStyle: "long"}).format();
59 - return time;
60 -}
61 -
62 -export function getInstalledFonts(fonts: []) {
63 - let fontlist = [];
64 - let id = 0;
65 - for (const f of fonts) {
66 - let font = {id: id, name: f};
67 - fontlist = [...fontlist, font];
68 - id++;
69 - }
70 - systemfonts.set(fontlist);
52 +async function fireAction(callback: () => Promise<void>, args = []) {
53 + await callback();
54 + return false;
55 }
72 -export function setFont(efont) {
73 -
74 -}
\ No newline at end of file
src/global.d.ts
+2 -1
@@ -1,2 +1,3 @@
1 /// <reference types="svelte" />
2 -/// <reference types="vite/client" />
\ No newline at end of file
2 +/// <reference types="vite/client" />
3 +declare module 'mousetrap';
\ No newline at end of file
src/lib/Header.svelte
+30 -28
@@ -1,48 +1,50 @@
1 <script lang="ts">
2 import { appWindow } from "@tauri-apps/api/window";
3 import Dropdown from "./utility/Dropdown.svelte";
4 - import Settings from "carbon-icons-svelte/lib/Settings.svelte";
4 + import settings from "./Settings.svelte";
5 + import { Settings } from "carbon-icons-svelte";
6 import { addTab, addEditorTab, tabs, closeActiveTab } from "../lib/EditorTabList.svelte";
7 import { saveFile, openFile, openFolder, workspaceName } from "./File";
8 + import { commands } from "../config/commands";
9
10 const items = [
11 {menuname: "File", children: [
10 - {name: "New File", shortcut: "Ctrl + N", action: () => {addEditorTab()}},
11 - {name: "Open File...", shortcut: "Ctrl + O", action: async () => {await openFile()}},
12 - {name: "Open Folder...", shortcut: "Ctrl + K", action: async () => {await openFolder()}},
13 - {name: "Save File", shortcut: "Ctrl + S", action: async () => {await saveFile()}},
14 - {name: "Save File As...", shortcut: "Ctrl + Shift + S", action: async () => {await saveFile(true)}},
12 + {name: "New File", shortcut: commands.addEditorTab.keybind, action: commands.addEditorTab.command},
13 + {name: "Open File...", shortcut: commands.openFile.keybind, action: commands.openFile.command},
14 + {name: "Open Folder...", shortcut: commands.openFolder.keybind, action: commands.openFolder.command},
15 + {name: "Save File", shortcut: commands.saveFile.keybind, action: commands.saveFile.command},
16 + {name: "Save File As...", shortcut: commands.saveFileAs.keybind, action: commands.saveFileAs.command},
17 {name: "Close Tab", disabled: $tabs.length === 0, shortcut: "", action: async () => {await closeActiveTab()}},
18 ]},
19 {menuname: "Edit", children: [
18 - {name: "Undo", shortcut: "Ctrl + Z", action: () => {console.warn("Feature not implemented yet.")}},
19 - {name: "Redo", shortcut: "Ctrl + Shift + Z", action: () => {console.warn("Feature not implemented yet.")}},
20 - {name: "Cut", shortcut: "Ctrl + X", action: () => {console.warn("Feature not implemented yet.")}},
21 - {name: "Copy", shortcut: "Ctrl + C", action: () => {console.warn("Feature not implemented yet.")}},
22 - {name: "Paste", shortcut: "Ctrl + V", action: () => {console.warn("Feature not implemented yet.")}},
23 - {name: "Paste From History...", disabled: true, shortcut: "Ctrl + Shift + V", action: () => {console.warn("Feature not implemented yet.")}},
24 - {name: "Delete", shortcut: "Delete", action: () => {console.warn("Feature not implemented yet.")}},
25 - {name: "Find", disabled: true, shortcut: "Ctrl + F", action: () => {console.warn("Feature not implemented yet.")}},
26 - {name: "Replace", disabled: true, shortcut: "Ctrl + H", action: () => {console.warn("Feature not implemented yet.")}},
20 + {name: "Undo", shortcut: commands.undo.keybind, action: commands.undo.command},
21 + {name: "Redo", shortcut: commands.redo.keybind, action: commands.redo.command},
22 + {name: "Cut", shortcut: commands.cut.keybind, action: commands.cut.command},
23 + {name: "Copy", shortcut: commands.copy.keybind, action: commands.copy.command},
24 + {name: "Paste", shortcut: commands.paste.keybind, action: commands.paste.command},
25 + {name: "Paste From History...", disabled: true, shortcut: commands.pasteFromHistory.keybind, action: commands.pasteFromHistory.command},
26 + {name: "Delete", shortcut: commands.delete.keybind, action: commands.delete.command},
27 + {name: "Find", disabled: true, shortcut: commands.find.keybind, action: () => commands.find.command},
28 + {name: "Replace", disabled: true, shortcut: commands.replace.keybind, action: commands.replace.command},
29 ]},
30 {menuname: "View", children: [
29 - {name: "Command Pallete", disabled: true, shortcut: "Ctrl + Shift + P", action: () => {console.warn("Feature not implemented yet.")}},
30 - {name: "Zoom In", shortcut: "Ctrl +", action: () => {console.warn("Feature not implemented yet.")}},
31 - {name: "Zoom Out", shortcut: "Ctrl -", action: () => {console.warn("Feature not implemented yet.")}},
31 + {name: "Command Pallete", disabled: true, shortcut: commands.openCommandPallete.keybind, action: commands.openCommandPallete.command},
32 + {name: "Zoom In", shortcut: commands.zoomIn.keybind, action: commands.zoomIn.command},
33 + {name: "Zoom Out", shortcut: commands.zoomOut.keybind, action: commands.zoomOut.command},
34 {name: "Reset Zoom", shortcut: "", action: () => {console.warn("Feature not implemented yet.")}},
33 - {name: "Fullscreen", shortcut: "F11", action: () => {console.warn("Feature not implemented yet.")}},
35 + {name: "Fullscreen", shortcut: commands.fullscreen.keybind, action: commands.fullscreen.command},
36 ]},
37 {menuname: "Run", children: [
36 - {name: "Run File", disabled: true, shortcut: "F5", action: () => {console.warn("Feature not implemented yet.")}},
37 - {name: "Debug File", disabled: true, shortcut: "Ctrl + F5", action: () => {console.warn("Feature not implemented yet.")}},
38 - {name: "Stop File", disabled: true, shortcut: "Shift + F5", action: () => {console.warn("Feature not implemented yet.")}},
38 + {name: "Run File", disabled: true, shortcut: commands.runFile.keybind, action: commands.runFile.command},
39 + {name: "Debug File", disabled: true, shortcut: commands.debugFile.keybind, action: commands.debugFile.command},
40 + {name: "Stop File", disabled: true, shortcut: commands.stopFile.keybind, action: commands.stopFile.command},
41 {name: "Open Configurations", disabled: true, shortcut: "", action: () => {console.warn("Feature not implemented yet.")}},
42 ]},
43 {menuname: "Window", children: [
42 - {name: "New Window", disabled: true, shortcut: "Ctrl + Shift + N", action: () => {console.warn("Feature not implemented yet.")}},
44 + {name: "New Window", disabled: true, shortcut: commands.openNewWindow.keybind, action: commands.openNewWindow.command},
45 {name: "Minimize Window", shortcut: "", action: async () => {await appWindow.minimize()}},
46 {name: "Maximize Window", shortcut: "", action: async () => {await appWindow.maximize()}},
45 - {name: "Close Window", shortcut: "Alt + F4", action: async () => {await appWindow.close()}},
47 + {name: "Close Window", shortcut: commands.closeWindow.keybind, action: commands.closeWindow.command},
48 ]},
49 {menuname: "Help", children: [
50 {name: "Send Feedback", disabled: true, shortcut: "", action: () => {console.warn("Feature not implemented yet.")}},
@@ -69,9 +71,9 @@
71 <div id="handle" data-tauri-drag-region></div>
72 <div class="tools">
73
72 - <div class="settings">
74 + <div class="settings-button">
75 <Dropdown right menu={{icon: Settings, children: [
74 - {name: "Settings", disabled: true, shortcut: "", action: () => {addTab("Settings", "Settings")}},
76 + {name: "Settings", shortcut: "", action: () => {addTab("Settings", "Settings", new settings({target: document.getElementById("tabview")}))}},
77 {name: "Keymap", disabled: true, shortcut: "", action: () => {console.warn("Feature not implemented yet.")}}
78 ]
79 }} />
@@ -94,7 +96,7 @@
96 <div
97 class="window-button"
98 id="close"
97 - on:click={async () => await appWindow.close()}
99 + on:click={commands.closeWindow.command}
100 />
101 </div>
102 </div>
@@ -145,7 +147,7 @@
147 z-index: 9;
148 }
149
148 - .settings {
150 + .settings-button {
151 height: 100%;
152 display: flex;
153 justify-content: center;