From cb0cb0b6fd831ad9e0ff2727814a25e38ca9e25f Mon Sep 17 00:00:00 2001 From: Bauke Date: Tue, 12 Dec 2023 13:42:14 +0100 Subject: [PATCH] Add the ProcessedSnippetShortcut functionality. --- source/storage/markdown-toolbar.ts | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/source/storage/markdown-toolbar.ts b/source/storage/markdown-toolbar.ts index 8df83e2..1d231a9 100644 --- a/source/storage/markdown-toolbar.ts +++ b/source/storage/markdown-toolbar.ts @@ -217,3 +217,42 @@ export function sortSnippets( return position; }); } + +/** A processed {@linkcode MarkdownSnippet} shortcut string. */ +export type ProcessedSnippetShortcut = { + /** Whether the ALT key is required for this shortcut. */ + alt: boolean; + + /** Whether the CTRL key is required for this shortcut. */ + ctrl: boolean; + + /** Whether the SHIFT key is required for this shortcut. */ + shift: boolean; + + /** The main key to trigger the shortcut. */ + key: string; +}; + +/** + * Process a {@linkcode MarkdownSnippet} shortcut string. + * + * The expected structure of a shortcut string is modifier keys in any order + * separated by a `+` and the main key on the end. For example `ALT+CTRL+B` is + * valid but `ALT+B+CTRL` is not. The casing of the letters is not relevant. + * + * This function performs no validation on the string and simply returns any + * input as-is. In the `ALT+B+CTRL` example, `CTRL` is what would be returned as + * the main key. + */ +export function processSnippetShortcut( + shortcut: string, +): ProcessedSnippetShortcut { + const components = shortcut.toLowerCase().split("+"); + + return { + alt: components.includes("alt"), + ctrl: components.includes("ctrl"), + shift: components.includes("shift"), + key: components[components.length - 1].toLowerCase(), + } satisfies ProcessedSnippetShortcut; +}