From 22a409b9a8d5d57181b0dbc176ad3fd826a751eb Mon Sep 17 00:00:00 2001 From: Abderrahmane Smimite Date: Sun, 15 Dec 2024 14:06:44 +0100 Subject: [PATCH] Be able to create new items on a modeltable view by pressing c --- .../components/CommandPalette/paletteData.ts | 4 ++ .../(internal)/[model=urlmodel]/+page.svelte | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/frontend/src/lib/components/CommandPalette/paletteData.ts b/frontend/src/lib/components/CommandPalette/paletteData.ts index 88b2c8ddd..acd388755 100644 --- a/frontend/src/lib/components/CommandPalette/paletteData.ts +++ b/frontend/src/lib/components/CommandPalette/paletteData.ts @@ -30,6 +30,10 @@ export const navigationLinks: NavigationLink[] = [ label: 'riskAssessments', href: '/risk-assessments' }, + { + label: 'riskScenarios', + href: '/risk-scenarios' + }, { label: 'actionPlan', href: '/applied-controls' diff --git a/frontend/src/routes/(app)/(internal)/[model=urlmodel]/+page.svelte b/frontend/src/routes/(app)/(internal)/[model=urlmodel]/+page.svelte index 73237965f..a07461fea 100644 --- a/frontend/src/routes/(app)/(internal)/[model=urlmodel]/+page.svelte +++ b/frontend/src/routes/(app)/(internal)/[model=urlmodel]/+page.svelte @@ -11,6 +11,8 @@ import { getSecureRedirect } from '$lib/utils/helpers'; import { goto } from '$app/navigation'; + import { onMount } from 'svelte'; + export let data: PageData; export let form: ActionData; $: URLModel = data.URLModel; @@ -46,6 +48,43 @@ modalStore.trigger(modal); } + function handleKeyDown(event: KeyboardEvent) { + if (event.metaKey || event.ctrlKey) return; + + // Check if 'c' is pressed and no input fields are currently focused + if ( + event.key.toLowerCase() === 'c' && + document.activeElement?.tagName !== 'INPUT' && + document.activeElement?.tagName !== 'TEXTAREA' + ) { + // Prevent default 'c' key behavior + event.preventDefault(); + + // Check if the add button exists and is not in a disabled list + if ( + ![ + 'risk-matrices', + 'frameworks', + 'requirement-mapping-sets', + 'user-groups', + 'role-assignments' + ].includes(URLModel) + ) { + modalCreateForm(); + } + } + } + + onMount(() => { + // Add event listener when component mounts + window.addEventListener('keydown', handleKeyDown); + + // Cleanup event listener when component is destroyed + return () => { + window.removeEventListener('keydown', handleKeyDown); + }; + }); + $: if (form && form.redirect) { goto(getSecureRedirect(form.redirect)); }