chore(client/ts): port services/keyboard_actions

This commit is contained in:
Elian Doran 2024-12-21 17:55:22 +02:00
parent 476ce0545a
commit 03b6ac450d
No known key found for this signature in database
2 changed files with 26 additions and 7 deletions

View File

@ -37,7 +37,7 @@ export type TriggerData = {
messages?: unknown[]; messages?: unknown[];
} | { } | {
ntxId: string; ntxId: string;
notePath: string; notePath?: string;
} | { } | {
text: string; text: string;
} | { } | {

View File

@ -1,10 +1,18 @@
import server from "./server.js"; import server from "./server.js";
import appContext from "../components/app_context.js"; import appContext from "../components/app_context.js";
import shortcutService from "./shortcuts.js"; import shortcutService from "./shortcuts.js";
import Component from "../components/component.js";
const keyboardActionRepo = {}; const keyboardActionRepo: Record<string, Action> = {};
const keyboardActionsLoaded = server.get('keyboard-actions').then(actions => { // TODO: Deduplicate with server.
interface Action {
actionName: string;
effectiveShortcuts: string[];
scope: string;
}
const keyboardActionsLoaded = server.get<Action[]>('keyboard-actions').then(actions => {
actions = actions.filter(a => !!a.actionName); // filter out separators actions = actions.filter(a => !!a.actionName); // filter out separators
for (const action of actions) { for (const action of actions) {
@ -20,13 +28,13 @@ async function getActions() {
return await keyboardActionsLoaded; return await keyboardActionsLoaded;
} }
async function getActionsForScope(scope) { async function getActionsForScope(scope: string) {
const actions = await keyboardActionsLoaded; const actions = await keyboardActionsLoaded;
return actions.filter(action => action.scope === scope); return actions.filter(action => action.scope === scope);
} }
async function setupActionsForElement(scope, $el, component) { async function setupActionsForElement(scope: string, $el: JQuery<HTMLElement>, component: Component) {
const actions = await getActionsForScope(scope); const actions = await getActionsForScope(scope);
for (const action of actions) { for (const action of actions) {
@ -44,7 +52,7 @@ getActionsForScope("window").then(actions => {
} }
}); });
async function getAction(actionName, silent = false) { async function getAction(actionName: string, silent = false) {
await keyboardActionsLoaded; await keyboardActionsLoaded;
const action = keyboardActionRepo[actionName]; const action = keyboardActionRepo[actionName];
@ -61,9 +69,15 @@ async function getAction(actionName, silent = false) {
return action; return action;
} }
function updateDisplayedShortcuts($container) { function updateDisplayedShortcuts($container: JQuery<HTMLElement>) {
//@ts-ignore
//TODO: each() does not support async callbacks.
$container.find('kbd[data-command]').each(async (i, el) => { $container.find('kbd[data-command]').each(async (i, el) => {
const actionName = $(el).attr('data-command'); const actionName = $(el).attr('data-command');
if (!actionName) {
return;
}
const action = await getAction(actionName, true); const action = await getAction(actionName, true);
if (action) { if (action) {
@ -75,8 +89,13 @@ function updateDisplayedShortcuts($container) {
} }
}); });
//@ts-ignore
//TODO: each() does not support async callbacks.
$container.find('[data-trigger-command]').each(async (i, el) => { $container.find('[data-trigger-command]').each(async (i, el) => {
const actionName = $(el).attr('data-trigger-command'); const actionName = $(el).attr('data-trigger-command');
if (!actionName) {
return;
}
const action = await getAction(actionName, true); const action = await getAction(actionName, true);
if (action) { if (action) {