@peaske7/readit 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +0 -3
  2. package/biome.json +1 -1
  3. package/bun.lock +43 -185
  4. package/docs/perf-baseline.md +75 -0
  5. package/docs/superpowers/plans/2026-03-26-surgical-pruning.md +1176 -0
  6. package/e2e/perf/add-comment.spec.ts +118 -0
  7. package/e2e/perf/fixtures/generate.ts +331 -0
  8. package/e2e/perf/initial-load.spec.ts +49 -0
  9. package/e2e/perf/perf.setup.ts +23 -0
  10. package/e2e/perf/perf.teardown.ts +9 -0
  11. package/e2e/perf/scroll.spec.ts +39 -0
  12. package/e2e/perf/tab-switch.spec.ts +69 -0
  13. package/e2e/perf/text-selection.spec.ts +119 -0
  14. package/e2e/perf/utils/metrics.ts +286 -0
  15. package/e2e/perf/utils/perf-cli.ts +86 -0
  16. package/package.json +9 -18
  17. package/playwright.config.ts +12 -0
  18. package/src/App.tsx +124 -172
  19. package/src/{cli/index.ts → cli.ts} +37 -53
  20. package/src/components/ActionsMenu.tsx +6 -27
  21. package/src/components/DocumentViewer/DocumentViewer.tsx +77 -106
  22. package/src/components/DocumentViewer/MermaidDiagram.tsx +6 -7
  23. package/src/components/Header.tsx +9 -20
  24. package/src/components/InlineEditor.tsx +5 -5
  25. package/src/components/MarginNote.tsx +71 -93
  26. package/src/components/MarginNotes.tsx +7 -34
  27. package/src/components/RawModal.tsx +9 -8
  28. package/src/components/ReanchorConfirm.tsx +2 -2
  29. package/src/components/SettingsModal.tsx +11 -89
  30. package/src/components/TabBar.tsx +4 -4
  31. package/src/components/TableOfContents.tsx +5 -5
  32. package/src/components/comments/CommentInput.tsx +7 -35
  33. package/src/components/comments/CommentListItem.tsx +9 -11
  34. package/src/components/comments/CommentManager.tsx +53 -37
  35. package/src/components/comments/CommentNav.tsx +14 -14
  36. package/src/components/ui/ActionLink.tsx +14 -18
  37. package/src/components/ui/Button.tsx +42 -43
  38. package/src/components/ui/Dialog.tsx +73 -113
  39. package/src/components/ui/DropdownMenu.tsx +113 -69
  40. package/src/components/ui/Text.tsx +30 -37
  41. package/src/contexts/CommentContext.tsx +75 -106
  42. package/src/contexts/LocaleContext.tsx +45 -4
  43. package/src/contexts/PositionsContext.tsx +16 -0
  44. package/src/contexts/SettingsContext.tsx +133 -0
  45. package/src/hooks/useClickOutside.ts +0 -4
  46. package/src/hooks/useCommentNavigation.ts +6 -29
  47. package/src/hooks/useComments.ts +6 -18
  48. package/src/hooks/useDocument.ts +35 -34
  49. package/src/hooks/useHeadings.test.ts +8 -50
  50. package/src/hooks/useHeadings.ts +5 -88
  51. package/src/hooks/useScrollSpy.ts +10 -14
  52. package/src/hooks/useTextSelection.ts +1 -38
  53. package/src/lib/__fixtures__/bench-data.ts +1 -41
  54. package/src/lib/anchor.bench.ts +57 -67
  55. package/src/lib/anchor.test.ts +5 -1
  56. package/src/lib/anchor.ts +13 -93
  57. package/src/lib/comment-storage.test.ts +4 -4
  58. package/src/lib/comment-storage.ts +2 -46
  59. package/src/lib/export.ts +7 -13
  60. package/src/lib/highlight/core.test.ts +1 -1
  61. package/src/lib/highlight/dom.ts +5 -68
  62. package/src/lib/highlight/highlighter.ts +102 -262
  63. package/src/lib/highlight/resolver.ts +112 -0
  64. package/src/lib/highlight/types.ts +0 -35
  65. package/src/lib/highlight/worker.ts +45 -0
  66. package/src/lib/i18n/en.ts +1 -50
  67. package/src/lib/i18n/ja.ts +1 -50
  68. package/src/lib/i18n/types.ts +1 -49
  69. package/src/lib/margin-layout.ts +5 -27
  70. package/src/lib/positions.ts +150 -0
  71. package/src/lib/utils.ts +2 -19
  72. package/src/schema.ts +81 -0
  73. package/src/{server/index.ts → server.ts} +74 -74
  74. package/src/{store/index.ts → store.ts} +14 -46
  75. package/vite.config.ts +8 -0
  76. package/src/components/DocumentViewer/IframeContainer.tsx +0 -251
  77. package/src/components/DocumentViewer/InlineCode.tsx +0 -60
  78. package/src/components/DocumentViewer/index.ts +0 -1
  79. package/src/components/FloatingTOC.tsx +0 -61
  80. package/src/components/ShortcutCapture.tsx +0 -48
  81. package/src/components/ShortcutList.tsx +0 -198
  82. package/src/components/comments/CommentMinimap.tsx +0 -62
  83. package/src/components/ui/ActionBar.tsx +0 -16
  84. package/src/components/ui/SeparatorDot.tsx +0 -9
  85. package/src/contexts/LayoutContext.tsx +0 -88
  86. package/src/hooks/useClipboard.ts +0 -82
  87. package/src/hooks/useEditorScheme.ts +0 -51
  88. package/src/hooks/useFontPreference.ts +0 -59
  89. package/src/hooks/useKeybindings.ts +0 -108
  90. package/src/hooks/useKeyboardShortcuts.ts +0 -63
  91. package/src/hooks/useLayoutMode.ts +0 -44
  92. package/src/hooks/useLocalePreference.ts +0 -42
  93. package/src/hooks/useReanchorMode.ts +0 -33
  94. package/src/hooks/useScrollMetrics.ts +0 -56
  95. package/src/hooks/useThemePreference.ts +0 -66
  96. package/src/lib/comment-storage.bench.ts +0 -63
  97. package/src/lib/context.bench.ts +0 -41
  98. package/src/lib/context.test.ts +0 -224
  99. package/src/lib/context.ts +0 -193
  100. package/src/lib/editor-links.ts +0 -59
  101. package/src/lib/export.bench.ts +0 -35
  102. package/src/lib/highlight/colors.ts +0 -37
  103. package/src/lib/highlight/core.ts +0 -54
  104. package/src/lib/highlight/index.ts +0 -23
  105. package/src/lib/highlight/script-builder.ts +0 -485
  106. package/src/lib/html-processor.test.tsx +0 -170
  107. package/src/lib/html-processor.tsx +0 -95
  108. package/src/lib/i18n/completeness.test.ts +0 -51
  109. package/src/lib/i18n/translations.test.ts +0 -39
  110. package/src/lib/layout-constants.ts +0 -12
  111. package/src/lib/margin-layout.bench.ts +0 -28
  112. package/src/lib/scroll.test.ts +0 -118
  113. package/src/lib/scroll.ts +0 -47
  114. package/src/lib/shortcut-registry.test.ts +0 -173
  115. package/src/lib/shortcut-registry.ts +0 -209
  116. package/src/lib/utils.test.ts +0 -110
  117. package/src/store/index.test.ts +0 -242
  118. package/src/types/index.ts +0 -127
@@ -1,95 +0,0 @@
1
- import type { Element, Root } from "hast";
2
- import { TriangleAlert } from "lucide-react";
3
- import type { ReactNode } from "react";
4
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
- import rehypeParse from "rehype-parse";
6
- import rehypeReact from "rehype-react";
7
- import { unified } from "unified";
8
- import { visit } from "unist-util-visit";
9
-
10
- /**
11
- * Placeholder component for stripped dangerous elements
12
- */
13
- function StrippedElement({ tagName }: { tagName: string }) {
14
- return (
15
- <span className="inline-flex items-center gap-1 px-3 py-1.5 text-sm bg-red-50 text-red-700 border border-red-200 rounded-md font-mono">
16
- <TriangleAlert className="w-4 h-4" />
17
- &lt;{tagName}&gt; removed
18
- </span>
19
- );
20
- }
21
-
22
- /**
23
- * Component mappings for dangerous elements - renders placeholders instead
24
- */
25
- const dangerousElementComponents = {
26
- script: () => <StrippedElement tagName="script" />,
27
- // Style tags targeting body/html/* can leak outside Shadow DOM, so strip them
28
- // Our base styles in ShadowContainer provide typography instead
29
- style: () => null,
30
- link: () => null, // External stylesheets break app styles
31
- iframe: () => <StrippedElement tagName="iframe" />,
32
- object: () => <StrippedElement tagName="object" />,
33
- embed: () => <StrippedElement tagName="embed" />,
34
- frame: () => <StrippedElement tagName="frame" />,
35
- frameset: () => <StrippedElement tagName="frameset" />,
36
- };
37
-
38
- /**
39
- * Rehype plugin to strip event handler attributes (onclick, onerror, etc.)
40
- * and dangerous href/src values (javascript:, data:, vbscript:)
41
- */
42
- function rehypeStripDangerousAttributes() {
43
- const dangerousSchemes = /^(javascript|vbscript|data):/i;
44
-
45
- return (tree: Root) => {
46
- visit(tree, "element", (node: Element) => {
47
- const props = node.properties;
48
- if (!props) return;
49
-
50
- for (const key of Object.keys(props)) {
51
- // Strip all event handlers (on*)
52
- if (key.startsWith("on") || key.startsWith("On")) {
53
- delete props[key];
54
- continue;
55
- }
56
-
57
- // Neutralize dangerous href/src schemes
58
- if (key === "href" || key === "src") {
59
- const value = props[key];
60
- if (
61
- typeof value === "string" &&
62
- dangerousSchemes.test(value.trim())
63
- ) {
64
- props[key] = "#";
65
- }
66
- }
67
- }
68
- });
69
- };
70
- }
71
-
72
- /**
73
- * Create the unified processor for HTML -> React conversion
74
- */
75
- const processor = unified()
76
- .use(rehypeParse, { fragment: true })
77
- .use(rehypeStripDangerousAttributes)
78
- .use(rehypeReact, {
79
- jsx,
80
- jsxs,
81
- Fragment,
82
- components: dangerousElementComponents,
83
- });
84
-
85
- /**
86
- * Process HTML content and return safe React elements
87
- *
88
- * - Dangerous tags (script, iframe, etc.) become visible placeholders
89
- * - Event handlers (onclick, onerror, etc.) are stripped
90
- * - Dangerous URLs (javascript:, data:, etc.) are neutralized
91
- */
92
- export function processHtml(content: string): ReactNode {
93
- const result = processor.processSync(content);
94
- return result.result as ReactNode;
95
- }
@@ -1,51 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { en } from "./en";
3
- import { ja } from "./ja";
4
-
5
- describe("translation completeness", () => {
6
- const enKeys = Object.keys(en).sort();
7
- const jaKeys = Object.keys(ja).sort();
8
-
9
- it("en and ja have the same keys", () => {
10
- expect(enKeys).toEqual(jaKeys);
11
- });
12
-
13
- // Prefix/suffix keys may be intentionally empty in some locales
14
- // (e.g., Japanese has no prefix before the command)
15
- const ALLOW_EMPTY = new Set(["app.noDocumentsHintPrefix"]);
16
-
17
- it("no empty string values in en", () => {
18
- for (const [key, value] of Object.entries(en)) {
19
- if (ALLOW_EMPTY.has(key)) continue;
20
- expect(value, `en.${key} is empty`).not.toBe("");
21
- }
22
- });
23
-
24
- it("no empty string values in ja", () => {
25
- for (const [key, value] of Object.entries(ja)) {
26
- if (ALLOW_EMPTY.has(key)) continue;
27
- expect(value, `ja.${key} is empty`).not.toBe("");
28
- }
29
- });
30
-
31
- it("interpolation placeholders match between locales", () => {
32
- const placeholderPattern = /\{\{(\w+)\}\}/g;
33
-
34
- for (const key of enKeys) {
35
- const enValue = en[key as keyof typeof en];
36
- const jaValue = ja[key as keyof typeof ja];
37
-
38
- const enPlaceholders = [...enValue.matchAll(placeholderPattern)]
39
- .map((m) => m[1])
40
- .sort();
41
- const jaPlaceholders = [...jaValue.matchAll(placeholderPattern)]
42
- .map((m) => m[1])
43
- .sort();
44
-
45
- expect(
46
- enPlaceholders,
47
- `Placeholder mismatch for key "${key}": en has ${JSON.stringify(enPlaceholders)}, ja has ${JSON.stringify(jaPlaceholders)}`,
48
- ).toEqual(jaPlaceholders);
49
- }
50
- });
51
- });
@@ -1,39 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { createT } from "./translations";
3
- import { Locales } from "./types";
4
-
5
- describe("createT", () => {
6
- it("returns English strings for en locale", () => {
7
- const t = createT(Locales.EN);
8
- expect(t("app.loading")).toBe("Loading...");
9
- expect(t("settings.title")).toBe("Settings");
10
- expect(t("comment.placeholder")).toBe("Add your comment...");
11
- });
12
-
13
- it("returns Japanese strings for ja locale", () => {
14
- const t = createT(Locales.JA);
15
- expect(t("app.loading")).toBe("読み込み中...");
16
- expect(t("settings.title")).toBe("設定");
17
- expect(t("comment.placeholder")).toBe("コメントを入力...");
18
- });
19
-
20
- it("interpolates {{placeholder}} params", () => {
21
- const tEn = createT(Locales.EN);
22
- expect(tEn("commentNav.of", { current: 1, total: 5 })).toBe("1 of 5");
23
-
24
- const tJa = createT(Locales.JA);
25
- expect(tJa("commentNav.of", { current: 1, total: 5 })).toBe("1 / 5");
26
- });
27
-
28
- it("interpolates multiple params", () => {
29
- const t = createT(Locales.EN);
30
- expect(t("commentManager.deleteAllConfirm", { count: 3 })).toBe(
31
- "Delete all 3 comments?",
32
- );
33
- });
34
-
35
- it("returns string unchanged when no params provided", () => {
36
- const t = createT(Locales.EN);
37
- expect(t("app.footer")).toBe("Made with ❤️ by Jay and Claude");
38
- });
39
- });
@@ -1,12 +0,0 @@
1
- // Layout constants for margin notes and document viewer
2
-
3
- export const HEADER_HEIGHT_PX = 48;
4
-
5
- // Minimum gap between margin notes (accounts for quote + comment + actions + padding)
6
- export const MARGIN_NOTE_MIN_GAP_PX = 150;
7
-
8
- // Height reserved for comment input form when selecting text
9
- export const COMMENT_INPUT_HEIGHT_PX = 160;
10
-
11
- // Minimap starts below the header
12
- export const MINIMAP_HEADER_OFFSET_PX = HEADER_HEIGHT_PX;
@@ -1,28 +0,0 @@
1
- import { bench, describe } from "vitest";
2
- import { makeHighlightPositions } from "./__fixtures__/bench-data";
3
- import { resolveMarginNotePositions } from "./margin-layout";
4
-
5
- // Pre-compute fixture data outside bench loops
6
- const ids5 = Array.from({ length: 5 }, (_, i) => `c${i}`);
7
- const pos5 = makeHighlightPositions(5);
8
-
9
- const ids50 = Array.from({ length: 50 }, (_, i) => `c${i}`);
10
- const pos50 = makeHighlightPositions(50);
11
-
12
- describe("resolveMarginNotePositions", () => {
13
- bench("5 notes, no input zone", () => {
14
- resolveMarginNotePositions(ids5, pos5, undefined);
15
- });
16
-
17
- bench("5 notes, with input zone collision", () => {
18
- resolveMarginNotePositions(ids5, pos5, 400);
19
- });
20
-
21
- bench("50 notes, no input zone", () => {
22
- resolveMarginNotePositions(ids50, pos50, undefined);
23
- });
24
-
25
- bench("50 notes, with input zone collision", () => {
26
- resolveMarginNotePositions(ids50, pos50, 3000);
27
- });
28
- });
@@ -1,118 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { calculateScrollTarget, getElementTopInDocument } from "./scroll";
3
-
4
- describe("calculateScrollTarget", () => {
5
- it("positions element at 25% from top by default", () => {
6
- // Element at 1000px, viewport 800px
7
- // Target offset = 800 * 0.25 = 200px
8
- // Scroll target = 1000 - 200 = 800px
9
- expect(
10
- calculateScrollTarget({ elementTop: 1000, viewportHeight: 800 }),
11
- ).toBe(800);
12
- });
13
-
14
- it("respects custom offset percent", () => {
15
- // Element at 1000px, viewport 800px, offset 50%
16
- // Target offset = 800 * 0.5 = 400px
17
- // Scroll target = 1000 - 400 = 600px
18
- expect(
19
- calculateScrollTarget({
20
- elementTop: 1000,
21
- viewportHeight: 800,
22
- offsetPercent: 0.5,
23
- }),
24
- ).toBe(600);
25
- });
26
-
27
- it("returns 0 when element is near top", () => {
28
- // Element at 100px, viewport 800px
29
- // Target offset = 200px
30
- // Scroll target = max(0, 100 - 200) = 0
31
- expect(
32
- calculateScrollTarget({ elementTop: 100, viewportHeight: 800 }),
33
- ).toBe(0);
34
- });
35
-
36
- it("returns 0 for element at position 0", () => {
37
- expect(calculateScrollTarget({ elementTop: 0, viewportHeight: 800 })).toBe(
38
- 0,
39
- );
40
- });
41
-
42
- it("handles small viewport", () => {
43
- // Element at 500px, viewport 400px
44
- // Target offset = 100px
45
- // Scroll target = 500 - 100 = 400px
46
- expect(
47
- calculateScrollTarget({ elementTop: 500, viewportHeight: 400 }),
48
- ).toBe(400);
49
- });
50
-
51
- it("handles zero offset percent", () => {
52
- // Element at 1000px, no offset
53
- // Scroll target = 1000px (element at very top of viewport)
54
- expect(
55
- calculateScrollTarget({
56
- elementTop: 1000,
57
- viewportHeight: 800,
58
- offsetPercent: 0,
59
- }),
60
- ).toBe(1000);
61
- });
62
- });
63
-
64
- describe("getElementTopInDocument", () => {
65
- it("calculates position for element in main document (not scrolled)", () => {
66
- // Element at 500px from viewport top, no scroll
67
- const elementRect = { top: 500 };
68
- expect(getElementTopInDocument({ elementRect, scrollY: 0 })).toBe(500);
69
- });
70
-
71
- it("calculates position for element in main document (scrolled)", () => {
72
- // Element at 200px from viewport top, scrolled 300px
73
- // Absolute position = 300 + 200 = 500px
74
- const elementRect = { top: 200 };
75
- expect(getElementTopInDocument({ elementRect, scrollY: 300 })).toBe(500);
76
- });
77
-
78
- it("calculates position for element inside iframe", () => {
79
- // Iframe at 100px from viewport top
80
- // Element at 150px from iframe top (inside iframe)
81
- // Scrolled 50px
82
- // Absolute position = 50 + 100 + 150 = 300px
83
- const elementRect = { top: 150 };
84
- expect(
85
- getElementTopInDocument({
86
- elementRect,
87
- scrollY: 50,
88
- iframeTopOffset: 100,
89
- }),
90
- ).toBe(300);
91
- });
92
-
93
- it("handles element at viewport top with scroll", () => {
94
- // Element at 0px from viewport top, scrolled 1000px
95
- const elementRect = { top: 0 };
96
- expect(getElementTopInDocument({ elementRect, scrollY: 1000 })).toBe(1000);
97
- });
98
-
99
- it("handles negative element position (above viewport)", () => {
100
- // Element scrolled past viewport top
101
- const elementRect = { top: -100 };
102
- expect(getElementTopInDocument({ elementRect, scrollY: 500 })).toBe(400);
103
- });
104
-
105
- it("handles iframe with element above iframe viewport", () => {
106
- // Iframe at 200px, element at -50px within iframe (scrolled past)
107
- // scrollY = 100
108
- // Absolute position = 100 + 200 + (-50) = 250px
109
- const elementRect = { top: -50 };
110
- expect(
111
- getElementTopInDocument({
112
- elementRect,
113
- scrollY: 100,
114
- iframeTopOffset: 200,
115
- }),
116
- ).toBe(250);
117
- });
118
- });
package/src/lib/scroll.ts DELETED
@@ -1,47 +0,0 @@
1
- /**
2
- * Scroll calculation utilities for TOC navigation.
3
- * These pure functions enable unit testing of scroll position calculations.
4
- */
5
-
6
- /**
7
- * Parameters for scroll target calculation.
8
- * Using object destructuring per style guide §3.5 for clarity.
9
- */
10
- export interface CalculateScrollTargetParams {
11
- elementTop: number;
12
- viewportHeight: number;
13
- offsetPercent?: number;
14
- }
15
-
16
- export interface GetElementTopParams {
17
- elementRect: { top: number };
18
- scrollY: number;
19
- iframeTopOffset?: number;
20
- }
21
-
22
- /**
23
- * Calculate the scroll target position to place an element at a comfortable
24
- * reading position (default: 25% from top of viewport).
25
- */
26
- export function calculateScrollTarget({
27
- elementTop,
28
- viewportHeight,
29
- offsetPercent = 0.25,
30
- }: CalculateScrollTargetParams): number {
31
- const targetOffset = viewportHeight * offsetPercent;
32
- return Math.max(0, elementTop - targetOffset);
33
- }
34
-
35
- /**
36
- * Get an element's absolute position in the main document.
37
- *
38
- * For elements directly in the document: pass scrollY and the element.
39
- * For elements inside an iframe: also pass the iframe's top offset.
40
- */
41
- export function getElementTopInDocument({
42
- elementRect,
43
- scrollY,
44
- iframeTopOffset,
45
- }: GetElementTopParams): number {
46
- return scrollY + (iframeTopOffset ?? 0) + elementRect.top;
47
- }
@@ -1,173 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- DEFAULT_SHORTCUTS,
4
- formatBinding,
5
- isReservedBinding,
6
- type KeybindingOverride,
7
- matchesBinding,
8
- resolveShortcuts,
9
- ShortcutActions,
10
- type ShortcutBinding,
11
- } from "./shortcut-registry";
12
-
13
- describe("DEFAULT_SHORTCUTS", () => {
14
- it("defines 7 shortcuts", () => {
15
- expect(DEFAULT_SHORTCUTS).toHaveLength(7);
16
- });
17
-
18
- it("has unique IDs", () => {
19
- const ids = DEFAULT_SHORTCUTS.map((s) => s.id);
20
- expect(new Set(ids).size).toBe(ids.length);
21
- });
22
-
23
- it("all enabled by default", () => {
24
- for (const shortcut of DEFAULT_SHORTCUTS) {
25
- expect(shortcut.enabled).toBe(true);
26
- }
27
- });
28
- });
29
-
30
- describe("matchesBinding", () => {
31
- it("matches exact key + modifiers", () => {
32
- const binding: ShortcutBinding = { key: "c", alt: true };
33
- const event = new KeyboardEvent("keydown", { key: "c", altKey: true });
34
- expect(matchesBinding(event, binding)).toBe(true);
35
- });
36
-
37
- it("rejects wrong key", () => {
38
- const binding: ShortcutBinding = { key: "c", alt: true };
39
- const event = new KeyboardEvent("keydown", { key: "v", altKey: true });
40
- expect(matchesBinding(event, binding)).toBe(false);
41
- });
42
-
43
- it("rejects extra modifiers", () => {
44
- const binding: ShortcutBinding = { key: "c", alt: true };
45
- const event = new KeyboardEvent("keydown", {
46
- key: "c",
47
- altKey: true,
48
- shiftKey: true,
49
- });
50
- expect(matchesBinding(event, binding)).toBe(false);
51
- });
52
-
53
- it("matches binding with shift", () => {
54
- const binding: ShortcutBinding = { key: "c", alt: true, shift: true };
55
- const event = new KeyboardEvent("keydown", {
56
- key: "c",
57
- altKey: true,
58
- shiftKey: true,
59
- });
60
- expect(matchesBinding(event, binding)).toBe(true);
61
- });
62
-
63
- it("matches meta key", () => {
64
- const binding: ShortcutBinding = { key: "c", meta: true };
65
- const event = new KeyboardEvent("keydown", { key: "c", metaKey: true });
66
- expect(matchesBinding(event, binding)).toBe(true);
67
- });
68
-
69
- it("matches shifted letter key (browser reports uppercase)", () => {
70
- const binding: ShortcutBinding = { key: "c", alt: true, shift: true };
71
- const event = new KeyboardEvent("keydown", {
72
- key: "C",
73
- altKey: true,
74
- shiftKey: true,
75
- });
76
- expect(matchesBinding(event, binding)).toBe(true);
77
- });
78
-
79
- it("matches meta+shift letter key (browser reports uppercase)", () => {
80
- const binding: ShortcutBinding = { key: "c", meta: true, shift: true };
81
- const event = new KeyboardEvent("keydown", {
82
- key: "C",
83
- metaKey: true,
84
- shiftKey: true,
85
- });
86
- expect(matchesBinding(event, binding)).toBe(true);
87
- });
88
-
89
- it("matches key-only binding (no modifiers)", () => {
90
- const binding: ShortcutBinding = { key: "Escape" };
91
- const event = new KeyboardEvent("keydown", { key: "Escape" });
92
- expect(matchesBinding(event, binding)).toBe(true);
93
- });
94
- });
95
-
96
- describe("formatBinding", () => {
97
- it("formats Alt+C", () => {
98
- const binding: ShortcutBinding = { key: "c", alt: true };
99
- expect(formatBinding(binding, true)).toBe("Alt+C");
100
- });
101
-
102
- it("formats meta key as ⌘ on Mac", () => {
103
- const binding: ShortcutBinding = { key: "c", meta: true };
104
- expect(formatBinding(binding, true)).toBe("⌘+C");
105
- });
106
-
107
- it("formats meta key as Ctrl on non-Mac", () => {
108
- const binding: ShortcutBinding = { key: "c", meta: true };
109
- expect(formatBinding(binding, false)).toBe("Ctrl+C");
110
- });
111
-
112
- it("formats shift modifier", () => {
113
- const binding: ShortcutBinding = { key: "c", meta: true, shift: true };
114
- expect(formatBinding(binding, true)).toBe("⌘+Shift+C");
115
- });
116
-
117
- it("formats arrow keys with symbols", () => {
118
- const binding: ShortcutBinding = { key: "ArrowUp", alt: true };
119
- expect(formatBinding(binding, true)).toBe("Alt+↑");
120
- });
121
-
122
- it("formats Escape", () => {
123
- const binding: ShortcutBinding = { key: "Escape" };
124
- expect(formatBinding(binding, true)).toBe("Esc");
125
- });
126
- });
127
-
128
- describe("resolveShortcuts", () => {
129
- it("returns defaults when no overrides", () => {
130
- const resolved = resolveShortcuts([]);
131
- expect(resolved).toEqual(DEFAULT_SHORTCUTS);
132
- });
133
-
134
- it("applies enabled override", () => {
135
- const overrides: KeybindingOverride[] = [
136
- { id: ShortcutActions.COPY_ALL, enabled: false },
137
- ];
138
- const resolved = resolveShortcuts(overrides);
139
- const copyAll = resolved.find((s) => s.id === ShortcutActions.COPY_ALL);
140
- expect(copyAll?.enabled).toBe(false);
141
- });
142
-
143
- it("applies binding override", () => {
144
- const overrides: KeybindingOverride[] = [
145
- {
146
- id: ShortcutActions.COPY_ALL,
147
- enabled: true,
148
- binding: { key: "a", meta: true },
149
- },
150
- ];
151
- const resolved = resolveShortcuts(overrides);
152
- const copyAll = resolved.find((s) => s.id === ShortcutActions.COPY_ALL);
153
- expect(copyAll?.binding).toEqual({ key: "a", meta: true });
154
- });
155
-
156
- it("ignores unknown override IDs", () => {
157
- const overrides: KeybindingOverride[] = [
158
- { id: "unknown_action", enabled: false },
159
- ];
160
- const resolved = resolveShortcuts(overrides);
161
- expect(resolved).toEqual(DEFAULT_SHORTCUTS);
162
- });
163
- });
164
-
165
- describe("isReservedBinding", () => {
166
- it("detects ⌘+W as reserved", () => {
167
- expect(isReservedBinding({ key: "w", meta: true })).toBe(true);
168
- });
169
-
170
- it("allows Alt+C", () => {
171
- expect(isReservedBinding({ key: "c", alt: true })).toBe(false);
172
- });
173
- });