-
-
Notifications
You must be signed in to change notification settings - Fork 6
Add browse-view hotkeys for new entry and search focus #2481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <script module lang="ts"> | ||
| /** Keyboard modifier for app shortcuts. Extend this union as more modifiers are supported. */ | ||
| export type Modifier = 'primary'; | ||
| </script> | ||
|
|
||
| <script lang="ts"> | ||
| import {getOpenDialogCount} from '$lib/components/ui/dialog-shared/dialog-shared-root.svelte'; | ||
| import {hasPrimaryModifier} from '$lib/utils/platform'; | ||
|
|
||
| type Props = { | ||
| key: string; | ||
| modifier?: Modifier; | ||
| disabled?: boolean; | ||
| allowWhenDialogOpen?: boolean; | ||
| onHotkey: () => void; | ||
| }; | ||
|
|
||
| let {key, modifier = 'primary', disabled = false, allowWhenDialogOpen = false, onHotkey}: Props = $props(); | ||
|
|
||
| function matchesModifier(e: KeyboardEvent, mod: Modifier): boolean { | ||
| switch (mod) { | ||
| case 'primary': | ||
| return hasPrimaryModifier(e); | ||
| } | ||
| } | ||
|
|
||
| function handleKeydown(e: KeyboardEvent) { | ||
| if (e.key.toLowerCase() !== key.toLowerCase() || !matchesModifier(e, modifier)) return; | ||
| if (disabled) return; | ||
| if (!allowWhenDialogOpen && getOpenDialogCount() > 0) return; | ||
| e.preventDefault(); | ||
| onHotkey(); | ||
| } | ||
| </script> | ||
|
|
||
| <svelte:window onkeydown={handleKeydown} /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /** True on macOS / iOS (for Cmd vs Ctrl shortcut labels and matching). */ | ||
| export function isApplePlatform(): boolean { | ||
| if (typeof navigator === 'undefined') return false; | ||
| const uaDataPlatform = (navigator as Navigator & {userAgentData?: {platform?: string}}).userAgentData | ||
| ?.platform; | ||
| if (uaDataPlatform) return /mac|iphone|ipad|ipod/i.test(uaDataPlatform); | ||
| // Fallback for browsers without userAgentData (Firefox, older Safari). | ||
| return /Mac|iPhone|iPad|iPod/i.test(navigator.platform); | ||
| } | ||
|
|
||
| /** Primary modifier for app shortcuts: Cmd on Apple, Ctrl elsewhere. */ | ||
| export function hasPrimaryModifier(e: KeyboardEvent): boolean { | ||
| if (e.altKey || e.shiftKey) return false; | ||
| return isApplePlatform() ? e.metaKey : e.ctrlKey; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import {expect, test, type Locator, type Page} from '@playwright/test'; | ||
|
|
||
| import {DemoProjectPage} from './demo-project.page'; | ||
|
|
||
| /** Demo project uses dictionary terminology ("New Word"); entry view would say "New Entry". */ | ||
| function newEntryDialog(page: Page): Locator { | ||
| return page.getByRole('dialog').filter({has: page.getByRole('heading', {name: /New (Entry|Word)/})}); | ||
| } | ||
|
|
||
| test.describe('Browse hotkeys', () => { | ||
| let projectPage: DemoProjectPage; | ||
|
|
||
| test.beforeEach(async ({page}) => { | ||
| projectPage = new DemoProjectPage(page); | ||
| await projectPage.goto(); | ||
| }); | ||
|
|
||
| test.describe('New entry (Ctrl/Cmd+E)', () => { | ||
| test('Ctrl/Cmd+E opens the new entry dialog', async ({page}) => { | ||
| await page.keyboard.press('ControlOrMeta+e'); | ||
| await expect(newEntryDialog(page)).toBeVisible(); | ||
| }); | ||
|
|
||
| test('Ctrl/Cmd+E works while the search input is focused', async ({page}) => { | ||
| await projectPage.entriesList.searchInput.click(); | ||
| await expect(projectPage.entriesList.searchInput).toBeFocused(); | ||
|
|
||
| await page.keyboard.press('ControlOrMeta+e'); | ||
| await expect(newEntryDialog(page)).toBeVisible(); | ||
| }); | ||
|
|
||
| test('plain E does not open the new entry dialog', async ({page}) => { | ||
| await page.keyboard.press('e'); | ||
| await expect(page.getByRole('dialog')).toHaveCount(0); | ||
| }); | ||
|
|
||
| test('Ctrl/Cmd+E does not reset an already-open new entry dialog', async ({page}) => { | ||
| await page.keyboard.press('ControlOrMeta+e'); | ||
|
|
||
| const dialog = newEntryDialog(page); | ||
| await expect(dialog).toBeVisible(); | ||
|
|
||
| const lexemeInput = dialog.locator('[style*="grid-area: lexemeForm"] input').first(); | ||
| await expect(lexemeInput).toBeVisible(); | ||
| await lexemeInput.fill('hotkey-preserve'); | ||
| await expect(lexemeInput).toHaveValue('hotkey-preserve'); | ||
|
|
||
| await page.keyboard.press('ControlOrMeta+e'); | ||
|
|
||
| await expect(page.getByRole('dialog')).toHaveCount(1); | ||
| await expect(lexemeInput).toHaveValue('hotkey-preserve'); | ||
| }); | ||
|
|
||
| test('Ctrl/Cmd+E does nothing when the project is read-only', async ({page}) => { | ||
| await page.evaluate(async () => { | ||
| await window.__PLAYWRIGHT_UTILS__.setWrite(false); | ||
| }); | ||
| await expect(page.getByRole('button', {name: /New (Entry|Word)/})).toHaveCount(0); | ||
|
|
||
| await page.keyboard.press('ControlOrMeta+e'); | ||
| await expect(page.getByRole('dialog')).toHaveCount(0); | ||
| }); | ||
| }); | ||
|
|
||
| test.describe('Search focus (Ctrl/Cmd+F)', () => { | ||
| test('Ctrl/Cmd+F focuses the Filter search input and selects existing text', async ({page}) => { | ||
| const filter = 'hotkey-select'; | ||
| await projectPage.entriesList.searchInput.fill(filter); | ||
| await projectPage.entriesList.searchInput.blur(); | ||
| await expect(projectPage.entriesList.searchInput).not.toBeFocused(); | ||
|
|
||
| await page.keyboard.press('ControlOrMeta+f'); | ||
|
|
||
| const searchInput = projectPage.entriesList.searchInput; | ||
| await expect(searchInput).toBeFocused(); | ||
| await expect(searchInput).toHaveValue(filter); | ||
| await expect.poll(async () => searchInput.evaluate((el: HTMLInputElement) => ({ | ||
| start: el.selectionStart, | ||
| end: el.selectionEnd, | ||
| length: el.value.length, | ||
| }))).toEqual({start: 0, end: filter.length, length: filter.length}); | ||
| }); | ||
|
|
||
| test('Ctrl/Cmd+F focuses search after selecting an entry', async ({page}) => { | ||
| await projectPage.entriesList.selectEntryByIndex(0); | ||
| await expect(projectPage.entriesList.searchInput).not.toBeFocused(); | ||
|
|
||
| await page.keyboard.press('ControlOrMeta+f'); | ||
| await expect(projectPage.entriesList.searchInput).toBeFocused(); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.