8000 [pull] main from CPAtoCybersecurity:main by pull[bot] · Pull Request #27 · ericrihm/csf_profile · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
]
},
"devDependencies": {
"@tauri-apps/cli": "^2.9.6"
"@tauri-apps/cli": "^2.9.6",
"postcss": "^8.4.31"
}
}
50 changes: 45 additions & 5 deletions src/components/KeyboardShortcutsOverlay.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { useCallback, useEffect, useRef, useState } from 'react';
import {
PLATFORM_MAC,
PLATFORM_WINDOWS,
resolvePlatform,
storePlatform,
} from '../utils/platform';

/**
* KeyboardShortcutsOverlay
Expand All @@ -15,11 +21,19 @@ import React, { useCallback, useEffect, useRef, useState } from 'react';

// A key token renders as a <kbd> chip.
const K = (label) => ({ kbd: label });
// The primary chord modifier, rendered per the selected platform. The hook
// accepts ctrlKey || metaKey everywhere, so either label is truthful.
const MOD = { mod: true };
// A separator between alternative key options ("or").
const OR = { or: true };
// A "+" combiner rendered between chord keys.
const PLUS = { plus: true };

const MODIFIER_LABEL = {
[PLATFORM_WINDOWS]: 'Ctrl',
[PLATFORM_MAC]: '⌘',
};

const SHORTCUT_GROUPS = [
{
title: 'Navigation',
Expand All @@ -35,7 +49,7 @@ const SHORTCUT_GROUPS = [
shortcuts: [
{ desc: 'Toggle selection of current item', keys: [K('Space')] },
{ desc: 'Extend selection while moving', keys: [K('Shift'), PLUS, K('↑'), OR, K('Shift'), PLUS, K('↓')] },
{ desc: 'Select all items', keys: [K('Ctrl'), OR, K('⌘'), PLUS, K('A')] },
{ desc: 'Select all items', keys: [MOD, PLUS, K('A')] },
{ desc: 'Exit edit / clear selection / deselect', keys: [K('Esc')] },
],
},
Expand All @@ -44,8 +58,8 @@ const SHORTCUT_GROUPS = [
shortcuts: [
{ desc: 'Edit current item', keys: [K('Enter')] },
{ desc: 'Create new item', keys: [K('N')] },
{ desc: 'Undo', keys: [K('Ctrl'), OR, K('⌘'), PLUS, K('Z')] },
{ desc: 'Redo', keys: [K('Ctrl'), OR, K('⌘'), PLUS, K('Shift'), PLUS, K('Z'), OR, K('Ctrl'), OR, K('⌘'), PLUS, K('Y')] },
{ desc: 'Undo', keys: [MOD, PLUS, K('Z')] },
{ desc: 'Redo', keys: [MOD, PLUS, K('Shift'), PLUS, K('Z'), OR, MOD, PLUS, K('Y')] },
],
},
{
Expand All @@ -57,17 +71,29 @@ const SHORTCUT_GROUPS = [
},
];

const KeyToken = ({ token }) => {
const KeyToken = ({ token, platform }) => {
if (token.or) return <span className="kbd-overlay-or">or</span>;
if (token.plus) return <span className="kbd-overlay-plus">+</span>;
if (token.mod) return <kbd className="terminal-kbd">{MODIFIER_LABEL[platform]}</kbd>;
return <kbd className="terminal-kbd">{token.kbd}</kbd>;
};

const PLATFORM_OPTIONS = [
{ id: PLATFORM_WINDOWS, label: 'Windows' },
{ id: PLATFORM_MAC, label: 'Mac' },
];

const KeyboardShortcutsOverlay = () => {
const [isOpen, setIsOpen] = useState(false);
const [platform, setPlatform] = useState(resolvePlatform);
const closeButtonRef = useRef(null);
const previouslyFocused = useRef(null);

const choosePlatform = useCallback((next) => {
setPlatform(next);
storePlatform(next);
}, []);

const close = useCallback(() => {
setIsOpen(false);
// Restore focus to whatever held it before the overlay opened.
Expand Down Expand Up @@ -124,6 +150,19 @@ const KeyboardShortcutsOverlay = () => {
>
<div className="kbd-overlay-header">
<span className="kbd-overlay-title">Keyboard Shortcuts</span>
<div className="kbd-overlay-platform" role="group" aria-label="Modifier key style">
{PLATFORM_OPTIONS.map((option) => (
<button
key={option.id}
type="button"
className={`kbd-overlay-platform-btn${platform === option.id ? ' is-active' : ''}`}
aria-pressed={platform === option.id}
=> choosePlatform(option.id)}
>
{option.label}
</button>
))}
</div>
<button
type="button"
ref={closeButtonRef}
Expand All @@ -145,7 +184,7 @@ const KeyboardShortcutsOverlay = () => {
<span className="kbd-overlay-row-keys">
{shortcut.keys.map((token, i) => (
// eslint-disable-next-line react/no-array-index-key
<KeyToken key={i} token={token} />
<KeyToken key={i} token={token} platform={platform} />
))}
</span>
</div>
Expand All @@ -155,6 +194,7 @@ const KeyboardShortcutsOverlay = () => {

<div className="kbd-overlay-footer">
Shortcuts are disabled while typing in an input, textarea, or select.
{' '}Ctrl and {'⌘'} both work regardless of the setting above.
</div>
</div>
</div>
Expand Down
147 changes: 147 additions & 0 deletions src/components/KeyboardShortcutsOverlay.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import React from 'react';
import { render, screen, fireEvent, act, within } from '@testing-library/react';
import KeyboardShortcutsOverlay from './KeyboardShortcutsOverlay';
import { PLATFORM_STORAGE_KEY } from '../utils/platform';

const openOverlay = () => {
act(() => {
window.dispatchEvent(new CustomEvent('keyboard-show-help'));
});
};

const setUserAgent = (ua) => {
Object.defineProperty(window.navigator, 'userAgent', {
value: ua,
configurable: true,
});
};

const ORIGINAL_UA = window.navigator.userAgent;

const modifierChips = () =>
screen
.getAllByText((_content, node) => node.tagName === 'KBD')
.map((node) => node.textContent);

beforeEach(() => {
window.localStorage.clear();
setUserAgent(ORIGINAL_UA);
});

afterAll(() => setUserAgent(ORIGINAL_UA));

describe('KeyboardShortcutsOverlay — platform toggle', () => {
it('renders a Windows/Mac toggle in the header when open', () => {
setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
render(<KeyboardShortcutsOverlay />);
openOverlay();

expect(screen.getByRole('group', { name: /modifier key style/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Windows' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Mac' })).toBeInTheDocument();
});

it('exposes the active side via aria-pressed', () => {
setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
render(<KeyboardShortcutsOverlay />);
openOverlay();

expect(screen.getByRole('button', { name: 'Windows' })).toHaveAttribute('aria-pressed', 'true');
expect(screen.getByRole('button', { name: 'Mac' })).toHaveAttribute('aria-pressed', 'false');
});

it('auto-detects Mac from the user agent', () => {
setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)');
render(<KeyboardShortcutsOverlay />);
openOverlay();

expect(screen.getByRole('button', { name: 'Mac' })).toHaveAttribute('aria-pressed', 'true');
expect(modifierChips()).toContain('⌘');
expect(modifierChips()).not.toContain('Ctrl');
});

it('defaults to Windows when the platform is unrecognised', () => {
setUserAgent('Mozilla/5.0 (X11; Linux x86_64)');
render(<KeyboardShortcutsOverlay />);
openOverlay();

expect(screen.getByRole('button', { name: 'Windows' })).toHaveAttribute('aria-pressed', 'true');
expect(modifierChips()).toContain('Ctrl');
expect(modifierChips()).not.toContain('⌘');
});

it('swaps every modifier chip when the other side is clicked', () => {
setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
render(<KeyboardShortcutsOverlay />);
openOverlay();

const ctrlCount = modifierChips().filter((label) => label === 'Ctrl').length;
expect(ctrlCount).toBe(4); // select-all, undo, redo x2

fireEvent.click(screen.getByRole('button', { name: 'Mac' }));

expect(modifierChips().filter((label) => label === '⌘')).toHaveLength(ctrlCount);
expect(modifierChips()).not.toContain('Ctrl');
});

it('renders both redo alternatives for the selected platform', () => {
setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)');
render(<KeyboardShortcutsOverlay />);
openOverlay();

// Scoping to the row needs one DOM step up from its description. The
// alternative — giving every keys span a role and a name purely so a query
// can reach it — makes screen readers announce each shortcut twice.
// eslint-disable-next-line testing-library/no-node-access
const redoRow = screen.getByText('Redo').closest('.kbd-overlay-row');
const labels = within(redoRow)
.getAllByText((_content, node) => node.tagName === 'KBD')
.map((node) => node.textContent);
expect(labels).toEqual(['⌘', 'Shift', 'Z', '⌘', 'Y']);
expect(within(redoRow).getByText('or')).toBeInTheDocument();
});

it('persists the choice and prefers it over detection on remount', () => {
setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
const view = render(<KeyboardShortcutsOverlay />);
openOverlay();
fireEvent.click(screen.getByRole('button', { name: 'Mac' }));
expect(window.localStorage.getItem(PLATFORM_STORAGE_KEY)).toBe('mac');
view.unmount();

render(<KeyboardShortcutsOverlay />);
openOverlay();
expect(screen.getByRole('button', { name: 'Mac' })).toHaveAttribute('aria-pressed', 'true');
expect(modifierChips()).toContain('⌘');
});
});

describe('KeyboardShortcutsOverlay — existing behavior is unchanged', () => {
it('stays closed until the help event fires, then closes on Escape', () => {
render(<KeyboardShortcutsOverlay />);
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();

openOverlay();
expect(screen.getByRole('dialog', { name: /keyboard shortcuts/i })).toBeInTheDocument();

fireEvent.keyDown(window, { key: 'Escape' });
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});

it('closes on a backdrop click but not on a panel click', () => {
render(<KeyboardShortcutsOverlay />);
openOverlay();

fireEvent.click(screen.getByRole('dialog'));
expect(screen.getByRole('dialog')).toBeInTheDocument();

fireEvent.click(screen.getByRole('presentation'));
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});

it('says both modifiers work, so the toggle cannot read as a rebind', () => {
render(<KeyboardShortcutsOverlay />);
openOverlay();
expect(screen.getByText(/both work regardless of the setting/i)).toBeInTheDocument();
});
});
11 changes: 8 additions & 3 deletions src/hooks/useKeyboardNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ export function useKeyboardNavigation() {
return;
}

// event.key carries the SHIFTED character, so Shift+Z arrives as 'Z' (and
// Caps Lock alone flips plain Z the same way). Compare case-insensitively
// or the Shift+Z redo chord the shortcuts overlay advertises never fires.
const letter = typeof event.key === 'string' ? event.key.toLowerCase() : '';

// Undo: Ctrl/Cmd + Z
if ((event.ctrlKey || event.metaKey) && event.key === 'z' && !event.shiftKey) {
if ((event.ctrlKey || event.metaKey) && letter === 'z' && !event.shiftKey) {
event.preventDefault();
if (canUndo()) {
undo();
Expand All @@ -76,8 +81,8 @@ export function useKeyboardNavigation() {

// Redo: Ctrl/Cmd + Shift + Z or Ctrl/Cmd + Y
if ((event.ctrlKey || event.metaKey) && (
(event.key === 'z' && event.shiftKey) ||
event.key === 'y'
(letter === 'z' && event.shiftKey) ||
letter === 'y'
)) {
event.preventDefault();
if (canRedo()) {
Expand Down
Loading
Loading
0