first pass of slash commands

This commit is contained in:
MrStashley 2024-03-12 18:40:37 -07:00
parent d1baf504ba
commit dd24323296
3 changed files with 89 additions and 43 deletions

View File

@ -41,7 +41,8 @@
},
{
"command": "app:openConnectionsView",
"keys": []
"keys": [],
"commandStr": "/remote:show"
},
{
"command": "app:openSettingsView",
@ -125,39 +126,48 @@
},
{
"command": "app:selectWorkspace-1",
"keys": ["Cmd:Ctrl:1"]
"keys": ["Cmd:Ctrl:1"],
"commandStr": "/session 1"
},
{
"command": "app:selectWorkspace-2",
"keys": ["Cmd:Ctrl:2"]
"keys": ["Cmd:Ctrl:2"],
"commandStr": "/session 2"
},
{
"command": "app:selectWorkspace-3",
"keys": ["Cmd:Ctrl:3"]
"keys": ["Cmd:Ctrl:3"],
"commandStr": "/session 3"
},
{
"command": "app:selectWorkspace-4",
"keys": ["Cmd:Ctrl:4"]
"keys": ["Cmd:Ctrl:4"],
"commandStr": "/session 4"
},
{
"command": "app:selectWorkspace-5",
"keys": ["Cmd:Ctrl:5"]
"keys": ["Cmd:Ctrl:5"],
"commandStr": "/session 5"
},
{
"command": "app:selectWorkspace-6",
"keys": ["Cmd:Ctrl:6"]
"keys": ["Cmd:Ctrl:6"],
"commandStr": "/session 6"
},
{
"command": "app:selectWorkspace-7",
"keys": ["Cmd:Ctrl:7"]
"keys": ["Cmd:Ctrl:7"],
"commandStr": "/session 7"
},
{
"command": "app:selectWorkspace-8",
"keys": ["Cmd:Ctrl:8"]
"keys": ["Cmd:Ctrl:8"],
"commandStr": "/session 8"
},
{
"command": "app:selectWorkspace-9",
"keys": ["Cmd:Ctrl:9"]
"keys": ["Cmd:Ctrl:9"],
"commandStr": "/session 9"
},
{
"command": "app:toggleSidebar",
@ -169,7 +179,8 @@
},
{
"command": "app:bookmarkActiveLine",
"keys": ["Cmd:b"]
"keys": ["Cmd:b"],
"commandStr": "/bookmarks:show"
},
{
"command": "bookmarks:edit",
@ -213,7 +224,8 @@
},
{
"command": "cmdinput:openHistory",
"keys": ["Ctrl:r"]
"keys": ["Ctrl:r"],
"commandStr": "/history"
},
{
"command": "cmdinput:openAIChat",

View File

@ -143,7 +143,7 @@ class Model {
this.runUpdate(message, interactive);
});
this.ws.reconnect();
this.keybindManager = new KeybindManager();
this.keybindManager = new KeybindManager(this);
this.readConfigKeybindings();
this.initSystemKeybindings();
this.initAppKeybindings();
@ -226,38 +226,20 @@ class Model {
initAppKeybindings() {
for (let index = 1; index <= 9; index++) {
this.keybindManager.registerKeybinding("app", "model", "app:selectWorkspace-" + index, (waveEvent) => {
this.onSwitchSessionCmd(index);
return true;
});
this.keybindManager.registerKeybinding("app", "model", "app:selectWorkspace-" + index, null);
}
this.keybindManager.registerKeybinding("app", "model", "app:focusCmdInput", (waveEvent) => {
console.log("focus cmd input callback");
this.onFocusCmdInputPressed();
return true;
});
this.keybindManager.registerKeybinding("app", "model", "app:bookmarkActiveLine", (waveEvent) => {
this.onBookmarkViewPressed();
return true;
});
this.keybindManager.registerKeybinding("app", "model", "app:openHistory", (waveEvent) => {
this.onOpenHistoryPressed();
return true;
});
this.keybindManager.registerKeybinding("app", "model", "app:bookmarkActiveLine", null);
this.keybindManager.registerKeybinding("app", "model", "app:openHistory", null);
this.keybindManager.registerKeybinding("app", "model", "app:openTabSearchModal", (waveEvent) => {
this.onOpenTabSearchModalPressed();
return true;
});
this.keybindManager.registerKeybinding("app", "model", "app:openConnectionsView", (waveEvent) => {
this.onOpenConnectionsViewPressed();
return true;
});
this.keybindManager.registerKeybinding("app", "model", "app:openConnectionsView", null);
this.keybindManager.registerKeybinding("app", "model", "app:openSettingsView", (waveEvent) => {
this.onOpenSettingsViewPressed();
return true;

View File

@ -4,7 +4,7 @@ import * as electron from "electron";
import { parse } from "node:path";
import { v4 as uuidv4 } from "uuid";
import defaultKeybindingsFile from "../../assets/default-keybindings.json";
const defaultKeybindings: KeybindConfig = defaultKeybindingsFile;
const defaultKeybindings: KeybindConfigArray = defaultKeybindingsFile;
type KeyPressDecl = {
mods: {
@ -24,12 +24,18 @@ const KeyTypeKey = "key";
const KeyTypeCode = "code";
type KeybindCallback = (event: WaveKeyboardEvent) => boolean;
type KeybindConfig = Array<{ command: string; keys: Array<string> }>;
type KeybindConfigArray = Array<KeybindConfig>;
type KeybindConfig = { command: string; keys: Array<string>; commandStr?: string };
const Callback = "callback";
const Command = "command";
type Keybind = {
domain: string;
keybinding: string;
action: string;
callback: KeybindCallback;
commandStr: string;
};
const KeybindLevels = ["system", "modal", "app", "pane", "plugin"];
@ -38,11 +44,12 @@ class KeybindManager {
domainCallbacks: Map<string, KeybindCallback>;
levelMap: Map<string, Array<Keybind>>;
levelArray: Array<string>;
keyDescriptionsMap: Map<string, Array<string>>;
userKeybindings: KeybindConfig;
keyDescriptionsMap: Map<string, KeybindConfig>;
userKeybindings: KeybindConfigArray;
userKeybindingError: OV<string>;
globalModel: any;
constructor() {
constructor(GlobalModel: any) {
this.levelMap = new Map();
this.domainCallbacks = new Map();
this.levelArray = KeybindLevels;
@ -53,6 +60,7 @@ class KeybindManager {
this.userKeybindingError = mobx.observable.box(null, {
name: "keyutil-userKeybindingError",
});
this.globalModel = GlobalModel;
this.initKeyDescriptionsMap();
}
@ -63,7 +71,7 @@ class KeybindManager {
let newKeyDescriptions = new Map();
for (let index = 0; index < defaultKeybindings.length; index++) {
let curKeybind = defaultKeybindings[index];
newKeyDescriptions.set(curKeybind.command, curKeybind.keys);
newKeyDescriptions.set(curKeybind.command, curKeybind);
}
let curUserCommand = "";
if (this.userKeybindings != null && this.userKeybindings instanceof Array) {
@ -85,7 +93,15 @@ class KeybindManager {
throw new Error("invalid keybind key");
}
}
newKeyDescriptions.set(curKeybind.command, curKeybind.keys);
let defaultCmd = this.keyDescriptionsMap.get(curKeybind.command);
if (
defaultCmd != null &&
defaultCmd.commandStr != null &&
(curKeybind.commandStr == null || curKeybind.commandStr == "")
) {
curKeybind.commandStr = this.keyDescriptionsMap.get(curKeybind.command).commandStr;
}
newKeyDescriptions.set(curKeybind.command, curKeybind);
}
} catch (e) {
let userError = `${curUserCommand} is invalid: error: ${e}`;
@ -98,16 +114,49 @@ class KeybindManager {
this.keyDescriptionsMap = newKeyDescriptions;
}
runSlashCommand(curKeybind: Keybind): boolean {
let curConfigKeybind = this.keyDescriptionsMap.get(curKeybind.keybinding);
console.log("slash command?", curKeybind, curConfigKeybind);
if (curConfigKeybind == null || curConfigKeybind.commandStr == null || curKeybind.commandStr == "") {
console.log("curKeybind: ", curKeybind);
return false;
}
let commandsList = curConfigKeybind.commandStr.split("\n");
this.runIndividualSlashCommand(commandsList);
return true;
}
runIndividualSlashCommand(commandsList: Array<string>): boolean {
if (commandsList.length == 0) {
return true;
}
let curCommand = commandsList.shift();
let prtn = this.globalModel.submitRawCommand(curCommand, false, false);
prtn.then((rtn) => {
if (!rtn.success) {
console.log("error running command ", curCommand);
return false;
}
return this.runIndividualSlashCommand(commandsList);
}).catch((error) => {
console.log("caught error running command ", curCommand, ": ", error);
return false;
});
}
processLevel(nativeEvent: any, event: WaveKeyboardEvent, keybindsArray: Array<Keybind>): boolean {
// iterate through keybinds in backwards order
for (let index = keybindsArray.length - 1; index >= 0; index--) {
let curKeybind = keybindsArray[index];
if (this.checkKeyPressed(event, curKeybind.keybinding)) {
let shouldReturn = false;
let shouldRunCommand = true;
if (curKeybind.callback != null) {
shouldReturn = curKeybind.callback(event);
shouldRunCommand = false;
}
if (!shouldReturn && this.domainCallbacks.has(curKeybind.domain)) {
shouldRunCommand = false;
let curDomainCallback = this.domainCallbacks.get(curKeybind.domain);
if (curDomainCallback != null) {
shouldReturn = curDomainCallback(event);
@ -115,6 +164,9 @@ class KeybindManager {
console.log("domain callback for ", curKeybind.domain, " is null. This should never happen");
}
}
if (shouldRunCommand) {
shouldReturn = this.runSlashCommand(curKeybind);
}
if (shouldReturn) {
nativeEvent.preventDefault();
nativeEvent.stopPropagation();
@ -269,7 +321,7 @@ class KeybindManager {
if (!this.keyDescriptionsMap.has(keyDescription)) {
return false;
}
let keyPressArray = this.keyDescriptionsMap.get(keyDescription);
let keyPressArray = this.keyDescriptionsMap.get(keyDescription).keys;
for (let index = 0; index < keyPressArray.length; index++) {
let curKeyPress = keyPressArray[index];
let pressed = checkKeyPressed(event, curKeyPress);