@kabmo-labs/kabmo 0.9.0 → 0.10.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 (180) hide show
  1. package/README.md +45 -0
  2. package/dist/client/index.html +1 -1
  3. package/dist/client/index.js +333 -333
  4. package/dist/client/static/js/index-18xe7655.css +1 -0
  5. package/dist/client/static/js/{index-bpptfkz2.js → index-nxpe7835.js} +768 -763
  6. package/package.json +3 -1
  7. package/src/client/components/common/code-surface/bracket-guide.test.ts +249 -0
  8. package/src/client/components/common/code-surface/bracket-guide.ts +131 -0
  9. package/src/client/components/common/code-surface/documents.test.ts +5 -3
  10. package/src/client/components/common/code-surface/documents.ts +85 -0
  11. package/src/client/components/common/code-surface/extensions.test.ts +5 -0
  12. package/src/client/components/common/code-surface/extensions.ts +140 -7
  13. package/src/client/components/common/code-surface/fold-stack.test.ts +218 -0
  14. package/src/client/components/common/code-surface/fold.test.ts +148 -0
  15. package/src/client/components/common/code-surface/fold.ts +118 -0
  16. package/src/client/components/common/code-surface/indent-guide.ts +104 -0
  17. package/src/client/components/common/code-surface/indent.test.ts +239 -0
  18. package/src/client/components/common/code-surface/indent.ts +124 -0
  19. package/src/client/components/common/code-surface/index.tsx +76 -46
  20. package/src/client/components/common/code-surface/keymap.ts +12 -23
  21. package/src/client/components/common/code-surface/line-commands.test.ts +90 -0
  22. package/src/client/components/common/code-surface/line-commands.ts +103 -0
  23. package/src/client/components/common/code-surface/props.ts +87 -0
  24. package/src/client/components/common/code-surface/stream-language.test.ts +118 -0
  25. package/src/client/components/common/code-surface/stream-language.ts +123 -0
  26. package/src/client/components/common/code-surface/theme.test.ts +40 -5
  27. package/src/client/components/common/code-surface/theme.ts +113 -0
  28. package/src/client/components/common/code-surface/typing.test.ts +97 -0
  29. package/src/client/components/common/code-surface/typing.ts +111 -0
  30. package/src/client/components/common/toolbar-row/index.test.tsx +53 -4
  31. package/src/client/components/common/toolbar-row/index.tsx +101 -30
  32. package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +17 -0
  33. package/src/client/components/layout/desktop-layout/index.tsx +18 -0
  34. package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +35 -33
  35. package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +20 -1
  36. package/src/client/components/mobile/mobile-right-sidebar/editor-props.ts +25 -0
  37. package/src/client/components/settings/categories/AppearanceSettings.tsx +6 -0
  38. package/src/client/components/settings/categories/appearance-settings/EditorIndentSection.tsx +87 -0
  39. package/src/client/components/settings/categories/appearance-settings/EditorTypingSection.tsx +99 -0
  40. package/src/client/components/workspace/chat-timeline/chat-input/expansion.ts +9 -4
  41. package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +15 -11
  42. package/src/client/components/workspace/chat-timeline/chat-input/stretch-send.test.tsx +165 -0
  43. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.test.ts +17 -3
  44. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/DiffView.tsx +10 -6
  45. package/src/client/components/workspace/editor/Body.tsx +24 -0
  46. package/src/client/components/workspace/editor/CommandPalette.tsx +4 -4
  47. package/src/client/components/workspace/editor/ConflictStrip.tsx +77 -0
  48. package/src/client/components/workspace/editor/Dialogs.tsx +53 -0
  49. package/src/client/components/workspace/editor/EmptyEditor.tsx +23 -0
  50. package/src/client/components/workspace/editor/ExternalChangeStrip.tsx +61 -0
  51. package/src/client/components/workspace/editor/FileDocument.tsx +89 -1
  52. package/src/client/components/workspace/editor/FilePane.tsx +43 -0
  53. package/src/client/components/workspace/editor/FindWidget.test.ts +9 -5
  54. package/src/client/components/workspace/editor/FindWidget.tsx +29 -0
  55. package/src/client/components/workspace/editor/GoToLineModal.tsx +123 -0
  56. package/src/client/components/workspace/editor/Notices.tsx +71 -0
  57. package/src/client/components/workspace/editor/Pane.tsx +118 -0
  58. package/src/client/components/workspace/editor/PluginColumn.tsx +32 -0
  59. package/src/client/components/workspace/editor/QuickOpenModal.tsx +146 -0
  60. package/src/client/components/workspace/editor/SaveAsModal.tsx +175 -0
  61. package/src/client/components/workspace/editor/SplitColumn.tsx +98 -0
  62. package/src/client/components/workspace/editor/SplitPane.tsx +57 -0
  63. package/src/client/components/workspace/editor/StatusBar.tsx +106 -0
  64. package/src/client/components/workspace/editor/TabContextMenu.tsx +75 -0
  65. package/src/client/components/workspace/editor/Tabs.tsx +195 -18
  66. package/src/client/components/workspace/editor/Toolbar.tsx +54 -181
  67. package/src/client/components/workspace/editor/index.tsx +199 -189
  68. package/src/client/components/workspace/editor/props.ts +64 -0
  69. package/src/client/components/workspace/editor/toolbar-clusters.tsx +300 -0
  70. package/src/client/components/workspace/file-explorer/Modals.tsx +14 -1
  71. package/src/client/components/workspace/file-explorer/TreeItem.tsx +2 -1
  72. package/src/client/components/workspace/file-explorer/clipboard.test.ts +3 -1
  73. package/src/client/components/workspace/file-explorer/index.test.ts +61 -0
  74. package/src/client/components/workspace/file-explorer/index.tsx +10 -48
  75. package/src/client/components/workspace/file-explorer/local-edit.test.ts +8 -3
  76. package/src/client/components/workspace/search-panel/Body.tsx +6 -1
  77. package/src/client/hooks/chat/composer/file-drop.ts +22 -37
  78. package/src/client/hooks/chat/composer/pipeline.test.ts +76 -0
  79. package/src/client/hooks/chat/composer/pipeline.ts +12 -1
  80. package/src/client/hooks/editor/caret-position.test.ts +58 -0
  81. package/src/client/hooks/editor/caret-position.ts +48 -0
  82. package/src/client/hooks/editor/file-editor-types.ts +200 -0
  83. package/src/client/hooks/editor/find-history.test.ts +132 -0
  84. package/src/client/hooks/editor/find-history.ts +46 -0
  85. package/src/client/hooks/editor/find-selection-range.test.ts +100 -0
  86. package/src/client/hooks/editor/find-selection-range.ts +49 -0
  87. package/src/client/hooks/editor/find-state.ts +13 -0
  88. package/src/client/hooks/editor/font.test.ts +0 -93
  89. package/src/client/hooks/editor/indent-settings.test.ts +104 -0
  90. package/src/client/hooks/editor/indent-settings.ts +88 -0
  91. package/src/client/hooks/editor/quick-open.test.ts +141 -0
  92. package/src/client/hooks/editor/quick-open.ts +127 -0
  93. package/src/client/hooks/editor/save-as.test.ts +156 -0
  94. package/src/client/hooks/editor/save-as.ts +122 -0
  95. package/src/client/hooks/editor/save-file.test.ts +179 -0
  96. package/src/client/hooks/editor/save-file.ts +73 -28
  97. package/src/client/hooks/editor/typing-settings.test.ts +73 -0
  98. package/src/client/hooks/editor/typing-settings.ts +65 -0
  99. package/src/client/hooks/editor/use-close-guard.ts +98 -0
  100. package/src/client/hooks/editor/use-editor-drop-overlay.test.ts +159 -0
  101. package/src/client/hooks/editor/use-editor-drop.shell.test.ts +224 -0
  102. package/src/client/hooks/editor/use-editor-drop.ts +58 -68
  103. package/src/client/hooks/editor/use-editor-find.test.ts +2 -2
  104. package/src/client/hooks/editor/use-editor-find.ts +38 -3
  105. package/src/client/hooks/editor/use-editor-panel-keys.ts +152 -0
  106. package/src/client/hooks/editor/use-editor-tab-state.ts +71 -0
  107. package/src/client/hooks/editor/use-external-change-notice.ts +71 -0
  108. package/src/client/hooks/editor/use-external-change.test.ts +88 -0
  109. package/src/client/hooks/editor/use-external-change.ts +146 -0
  110. package/src/client/hooks/editor/use-file-editor.ts +83 -116
  111. package/src/client/hooks/editor/use-file-read.ts +16 -8
  112. package/src/client/hooks/editor/use-file-writes.ts +28 -3
  113. package/src/client/hooks/editor/use-mobile-editor-find.ts +69 -0
  114. package/src/client/hooks/editor/use-mobile-reveal.ts +53 -0
  115. package/src/client/hooks/editor/use-search-reveal-jump.ts +57 -0
  116. package/src/client/hooks/editor/use-split-editor.test.tsx +169 -0
  117. package/src/client/hooks/editor/use-split-editor.ts +91 -0
  118. package/src/client/hooks/editor/use-unload-guard.tsx +117 -0
  119. package/src/client/hooks/editor/use-write-conflict.test.ts +164 -0
  120. package/src/client/hooks/editor/use-write-conflict.ts +152 -0
  121. package/src/client/hooks/editor/wheel-zoom.test.ts +141 -0
  122. package/src/client/hooks/editor/wheel-zoom.ts +96 -0
  123. package/src/client/hooks/ui/drag-presence.test.ts +193 -0
  124. package/src/client/hooks/ui/drag-presence.ts +108 -0
  125. package/src/client/hooks/ui/settings-modal.test.ts +115 -0
  126. package/src/client/hooks/workspace/file-listing.ts +36 -24
  127. package/src/client/hooks/workspace/file-tabs.test.ts +90 -0
  128. package/src/client/hooks/workspace/file-tabs.ts +46 -0
  129. package/src/client/hooks/workspace/file-tree-edits.ts +103 -0
  130. package/src/client/lib/desktop/dropped-paths.ts +83 -0
  131. package/src/client/lib/editor/tab-menu.test.ts +127 -0
  132. package/src/client/lib/editor/tab-menu.ts +127 -0
  133. package/src/client/lib/editor-dirty-tabs.test.ts +102 -0
  134. package/src/client/lib/editor-dirty-tabs.ts +103 -0
  135. package/src/client/lib/native/resume.test.ts +66 -0
  136. package/src/client/lib/native/resume.ts +42 -0
  137. package/src/client/main.tsx +8 -0
  138. package/src/client/tailwind.css +38 -0
  139. package/src/server/lib/bundler/css.test.ts +31 -0
  140. package/src/server/lib/fs/listing.test.ts +81 -16
  141. package/src/server/lib/fs/listing.ts +72 -5
  142. package/src/server/lib/fs/write-conflict.test.ts +68 -0
  143. package/src/server/lib/fs/write-conflict.ts +84 -0
  144. package/src/server/routes/fs/action-save-as.test.ts +82 -0
  145. package/src/server/routes/fs/action.test.ts +82 -0
  146. package/src/server/routes/fs/action.ts +54 -1
  147. package/src/server/routes/fs/exists.ts +11 -1
  148. package/src/server/routes/fs/read.ts +6 -1
  149. package/src/shared/lib/code/editor/find-replace.test.ts +1 -1
  150. package/src/shared/lib/code/editor/find-scan.test.ts +1 -1
  151. package/src/shared/lib/code/editor/find-scan.ts +22 -1
  152. package/src/shared/lib/code/editor/find-selection.test.ts +56 -0
  153. package/src/shared/lib/code/editor/indent-guide-rule.test.ts +65 -0
  154. package/src/shared/lib/code/editor/indent-guide-rule.ts +47 -0
  155. package/src/shared/lib/code/editor/indent-rules.test.ts +219 -0
  156. package/src/shared/lib/code/editor/indent-rules.ts +198 -0
  157. package/src/shared/lib/code/editor/keymap.test.ts +23 -0
  158. package/src/shared/lib/code/editor/keymap.ts +40 -3
  159. package/src/shared/lib/code/editor/palette.test.ts +25 -2
  160. package/src/shared/lib/code/editor/palette.ts +10 -0
  161. package/src/shared/lib/code/editor/stream-modes.ts +225 -0
  162. package/src/shared/lib/code/syntax-highlight.test.ts +1 -1
  163. package/src/shared/lib/fs/file-tree.test.ts +41 -0
  164. package/src/shared/lib/fs/file-tree.ts +126 -34
  165. package/src/shared/lib/fs/folder-chain.test.ts +87 -0
  166. package/src/shared/lib/fs/folder-chain.ts +91 -0
  167. package/src/shared/lib/fs/fs-helpers.test.ts +4 -50
  168. package/src/shared/lib/realtime/connection.test.ts +5 -1
  169. package/src/shared/lib/realtime/connection.ts +25 -119
  170. package/src/shared/lib/realtime/frozen-recovery.test.ts +93 -0
  171. package/src/shared/lib/realtime/resume.ts +223 -0
  172. package/src/shared/lib/realtime/state.ts +22 -0
  173. package/src/shared/lib/realtime/watchdog-wiring.test.ts +224 -0
  174. package/src/shared/lib/realtime/watchdog.test.ts +99 -0
  175. package/src/shared/lib/realtime/watchdog.ts +72 -0
  176. package/src/shared/lib/workspace/split-editor.test.ts +104 -0
  177. package/src/shared/lib/workspace/split-editor.ts +129 -0
  178. package/src/test-support/file-explorer-disk.test-util.ts +30 -3
  179. package/src/test-support/pristine-globals.ts +7 -0
  180. package/dist/client/static/js/index-k315p1e8.css +0 -1
@@ -0,0 +1,141 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The wheel-zoom arithmetic and its one exclusion.
8
+ *
9
+ * Both are silent failures if wrong: a threshold-based step makes a trackpad pinch
10
+ * zoom several sizes per gesture, and a missing nested-scroller check resizes the
11
+ * code while the user is scrolling a list of search hits.
12
+ */
13
+
14
+ import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
15
+ import { Window } from 'happy-dom';
16
+
17
+ import { clampZoom, isInsideNestedScroller, zoomStepsFromWheel } from '@/client/hooks/editor/wheel-zoom';
18
+ import { EDITOR_MAX_FONT_SIZE, EDITOR_MIN_FONT_SIZE } from '@/shared/lib/code/editor/typography';
19
+
20
+ describe('zoomStepsFromWheel', () => {
21
+ test('a wheel up zooms in and a wheel down zooms out', () => {
22
+ expect(zoomStepsFromWheel(-100)).toBe(1);
23
+ expect(zoomStepsFromWheel(100)).toBe(-1);
24
+ });
25
+
26
+ test('a trackpad pinch of ±1 is still exactly one step', () => {
27
+ // A magnitude threshold would treat these as noise and zoom nothing, which is the
28
+ // reported "pinch does nothing" failure.
29
+ expect(zoomStepsFromWheel(-1)).toBe(1);
30
+ expect(zoomStepsFromWheel(1)).toBe(-1);
31
+ });
32
+
33
+ test('a fast fling is capped at one step, so one gesture cannot jump several sizes', () => {
34
+ expect(zoomStepsFromWheel(-1000)).toBe(1);
35
+ expect(zoomStepsFromWheel(1000)).toBe(-1);
36
+ });
37
+
38
+ test('no delta is no step', () => {
39
+ expect(zoomStepsFromWheel(0)).toBe(0);
40
+ });
41
+ });
42
+
43
+ describe('clampZoom', () => {
44
+ test('clamps to the SHARED bounds the toolbar uses', () => {
45
+ expect(clampZoom(EDITOR_MAX_FONT_SIZE, 1)).toBe(EDITOR_MAX_FONT_SIZE);
46
+ expect(clampZoom(EDITOR_MIN_FONT_SIZE, -1)).toBe(EDITOR_MIN_FONT_SIZE);
47
+ });
48
+
49
+ test('a step inside the bounds moves by exactly one', () => {
50
+ expect(clampZoom(12, 1)).toBe(13);
51
+ expect(clampZoom(12, -1)).toBe(11);
52
+ });
53
+
54
+ test('a caller may pass its own bounds', () => {
55
+ expect(clampZoom(5, 1, 0, 6)).toBe(6);
56
+ expect(clampZoom(5, 1, 0, 5)).toBe(5);
57
+ });
58
+ });
59
+
60
+ describe('isInsideNestedScroller', () => {
61
+ // The helper reads `HTMLElement` and `getComputedStyle` from the global scope, which
62
+ // is how it runs in the browser — so the DOM globals are installed for this block.
63
+ const win = new Window({ url: 'http://localhost' });
64
+ const GLOBALS = ['window', 'document', 'navigator', 'HTMLElement', 'Element', 'Node', 'getComputedStyle'] as const;
65
+ const native: Partial<Record<(typeof GLOBALS)[number], unknown>> = {};
66
+
67
+
68
+ beforeAll(() => {
69
+ const target = globalThis as unknown as Record<string, unknown>;
70
+ const source = win as unknown as Record<string, unknown>;
71
+ for (const key of GLOBALS) {
72
+ native[key] = target[key];
73
+ target[key] = source[key];
74
+ }
75
+ });
76
+
77
+ afterAll(() => {
78
+ const target = globalThis as unknown as Record<string, unknown>;
79
+ for (const key of GLOBALS) target[key] = native[key];
80
+ });
81
+
82
+ /**
83
+ * Attach the tree before asking.
84
+ *
85
+ * `getComputedStyle` resolves the cascade, which needs the node in a document —
86
+ * measured in happy-dom: a detached `overflow-y: auto` element computes to an empty
87
+ * string. A real editor's nodes are always attached, so this is the fixture being
88
+ * honest rather than a workaround for the helper.
89
+ */
90
+ function attach(host: HTMLElement): void {
91
+ // happy-dom's own `Node`/`HTMLElement` classes are structurally different from the
92
+ // DOM lib's, so the handover is a cast at the fixture boundary — the runtime objects
93
+ // are the ones the helper will see.
94
+ win.document.body.appendChild(host as unknown as Parameters<typeof win.document.body.appendChild>[0]);
95
+ }
96
+
97
+ test('a scrolling ancestor between the target and the host owns the wheel', () => {
98
+ const host = win.document.createElement('div');
99
+ const scroller = win.document.createElement('div');
100
+ const inner = win.document.createElement('span');
101
+ host.appendChild(scroller);
102
+ scroller.appendChild(inner);
103
+ Object.defineProperty(scroller, 'scrollHeight', { configurable: true, value: 400 });
104
+ Object.defineProperty(scroller, 'clientHeight', { configurable: true, value: 100 });
105
+ scroller.style.overflowY = 'auto';
106
+ attach(host as unknown as HTMLElement);
107
+ expect(isInsideNestedScroller(inner as unknown as EventTarget, host as unknown as HTMLElement)).toBe(true);
108
+ });
109
+
110
+ test('a plain ancestor does not', () => {
111
+ const host = win.document.createElement('div');
112
+ const plain = win.document.createElement('div');
113
+ const inner = win.document.createElement('span');
114
+ host.appendChild(plain);
115
+ plain.appendChild(inner);
116
+ attach(host as unknown as HTMLElement);
117
+ expect(isInsideNestedScroller(inner as unknown as EventTarget, host as unknown as HTMLElement)).toBe(false);
118
+ });
119
+
120
+ test('a scroller that cannot actually scroll does not claim the wheel', () => {
121
+ // `overflow-y: auto` on a box whose content fits is not a scroll container in any
122
+ // sense the user can feel, so it must not swallow the zoom.
123
+ const host = win.document.createElement('div');
124
+ const box = win.document.createElement('div');
125
+ const inner = win.document.createElement('span');
126
+ host.appendChild(box);
127
+ box.appendChild(inner);
128
+ box.style.overflowY = 'auto';
129
+ Object.defineProperty(box, 'scrollHeight', { configurable: true, value: 100 });
130
+ Object.defineProperty(box, 'clientHeight', { configurable: true, value: 100 });
131
+ attach(host as unknown as HTMLElement);
132
+ expect(isInsideNestedScroller(inner as unknown as EventTarget, host as unknown as HTMLElement)).toBe(false);
133
+ });
134
+
135
+ test('the host itself is not nested, and a null target is not either', () => {
136
+ const host = win.document.createElement('div');
137
+ attach(host as unknown as HTMLElement);
138
+ expect(isInsideNestedScroller(host as unknown as EventTarget, host as unknown as HTMLElement)).toBe(false);
139
+ expect(isInsideNestedScroller(null, host as unknown as HTMLElement)).toBe(false);
140
+ });
141
+ });
@@ -0,0 +1,96 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Ctrl/⌘ + wheel zoom, wired to the editor's own scroller.
8
+ *
9
+ * `ctrlKey` rather than a bare wheel: a bare wheel over the editor must SCROLL, and a
10
+ * zoom that took it would make the document unscrollable. macOS reports a trackpad
11
+ * pinch as `ctrlKey + wheel` (the platform's own convention), so this also gives
12
+ * pinch-to-zoom for free.
13
+ *
14
+ * ## It must not fire over a nested scroller
15
+ *
16
+ * The find bar and a composer popup are real scroll containers inside the panel. A
17
+ * wheel there belongs to them, so the handler walks up from the event target and
18
+ * stands down for any ancestor that scrolls — otherwise scrolling a long list of
19
+ * search hits would resize the code underneath it.
20
+ *
21
+ * ## The bounds are the SHARED ones
22
+ *
23
+ * `EDITOR_MIN_FONT_SIZE` / `EDITOR_MAX_FONT_SIZE` are what the toolbar's buttons and
24
+ * the phone's slider clamp to. A wheel that used its own range would be a second
25
+ * answer to "how small can the text get".
26
+ */
27
+
28
+ import { useEffect } from 'preact/hooks';
29
+
30
+ import { EDITOR_MAX_FONT_SIZE, EDITOR_MIN_FONT_SIZE } from '@/shared/lib/code/editor/typography';
31
+
32
+ /** Wheel delta → one zoom step. A trackpad sends many small deltas; a mouse sends ±100. */
33
+ export function zoomStepsFromWheel(deltaY: number): number {
34
+ if (deltaY === 0) return 0;
35
+ // `sign` rather than a magnitude threshold: a pinch sends ±1…±10 and a mouse ±100,
36
+ // and both should be one step per gesture tick. Capped so a fast fling cannot jump
37
+ // several sizes at once.
38
+ return Math.max(-1, Math.min(1, -Math.sign(deltaY)));
39
+ }
40
+
41
+ /** The next font size, clamped to the shared bounds. */
42
+ export function clampZoom(current: number, steps: number, min = EDITOR_MIN_FONT_SIZE, max = EDITOR_MAX_FONT_SIZE): number {
43
+ return Math.max(min, Math.min(max, current + steps));
44
+ }
45
+
46
+ /**
47
+ * Whether the event target sits inside a scroller of its own, between it and `host`.
48
+ *
49
+ * A node that scrolls its own content owns the wheel: resizing the code underneath a
50
+ * scrolling list is the failure this prevents.
51
+ */
52
+ export function isInsideNestedScroller(target: EventTarget | null, host: HTMLElement): boolean {
53
+ let node = target instanceof HTMLElement ? target : null;
54
+ while (node && node !== host) {
55
+ const style = getComputedStyle(node);
56
+ if (/(auto|scroll)/.test(style.overflowY) && node.scrollHeight > node.clientHeight) return true;
57
+ node = node.parentElement;
58
+ }
59
+ return false;
60
+ }
61
+
62
+ /**
63
+ * Attach the zoom listener to a scroller.
64
+ *
65
+ * `element` is CodeMirror's own `scrollDOM` (reported through the surface's
66
+ * `onScroller`), which appears only once the view mounts — so this re-arms on the
67
+ * render it arrives in rather than capturing a ref that was null at mount.
68
+ */
69
+ export function useWheelZoom(
70
+ element: HTMLElement | null,
71
+ setZoomLevel: (update: (current: number) => number) => void,
72
+ ): void {
73
+ useEffect(() => {
74
+ if (!element) return;
75
+ // The HOST is the surface, not the scroller: a nested scroller is an ancestor of
76
+ // the event target and a descendant of the host, so the walk needs both ends.
77
+ const host = element.closest('[data-code-surface]');
78
+ const stop = host instanceof HTMLElement ? host : element;
79
+
80
+ const onWheel = (event: WheelEvent) => {
81
+ if (!event.ctrlKey && !event.metaKey) return;
82
+ // The browser's own page zoom is what this gesture means by default, and it
83
+ // would scale the whole console instead of the document.
84
+ event.preventDefault();
85
+ if (isInsideNestedScroller(event.target, stop)) return;
86
+ const steps = zoomStepsFromWheel(event.deltaY);
87
+ if (steps === 0) return;
88
+ setZoomLevel((current) => clampZoom(current, steps));
89
+ };
90
+
91
+ // `passive: false` is required: a passive listener cannot `preventDefault`, and
92
+ // the browser would zoom the page instead.
93
+ element.addEventListener('wheel', onWheel, { passive: false });
94
+ return () => element.removeEventListener('wheel', onWheel);
95
+ }, [element, setZoomLevel]);
96
+ }
@@ -0,0 +1,193 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Drag presence.
8
+ *
9
+ * The rule this replaced was a counter, and the counter is why a drop target
10
+ * could stay lit with the pointer nowhere near it. `dragenter` fires for every
11
+ * nested child the pointer crosses, and the editor's handler incremented on each
12
+ * one while ignoring the matching `dragleave` (a move between children). Measured
13
+ * in Chromium against the real app: five child crossings inside the chat raised
14
+ * the count to six, one exit lowered it to five, and the "Drop a file to open it"
15
+ * overlay stayed on the chat after the gesture ended.
16
+ *
17
+ * These cases pin the symmetric answer — `relatedTarget` — and the backstops for
18
+ * a gesture that never reports its own end to the element.
19
+ */
20
+
21
+ import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
22
+ import { Window } from 'happy-dom';
23
+ import { h, render } from 'preact';
24
+ import { act } from 'preact/test-utils';
25
+ import { useDragPresence, type DragPresence } from '@/client/hooks/ui/drag-presence';
26
+
27
+ const DOM_GLOBALS = ['window', 'document', 'navigator', 'Node', 'Element', 'HTMLElement', 'Event'] as const;
28
+ const native: Partial<Record<(typeof DOM_GLOBALS)[number], unknown>> = {};
29
+
30
+ beforeAll(() => {
31
+ const win = new Window({ url: 'http://localhost' });
32
+ const target = globalThis as unknown as Record<string, unknown>;
33
+ for (const key of DOM_GLOBALS) {
34
+ native[key] = target[key];
35
+ target[key] = (win as unknown as Record<string, unknown>)[key];
36
+ }
37
+ });
38
+
39
+ afterAll(() => {
40
+ const target = globalThis as unknown as Record<string, unknown>;
41
+ for (const key of DOM_GLOBALS) {
42
+ if (native[key] === undefined) delete target[key];
43
+ else target[key] = native[key];
44
+ }
45
+ });
46
+
47
+ interface Harness {
48
+ api: () => DragPresence;
49
+ /** The bound target, with a child inside it and a sibling outside it. */
50
+ target: HTMLElement;
51
+ child: HTMLElement;
52
+ outside: HTMLElement;
53
+ }
54
+
55
+ let container: HTMLElement;
56
+
57
+ beforeEach(() => {
58
+ container?.remove();
59
+ container = document.body.appendChild(document.createElement('div'));
60
+ });
61
+
62
+ async function mount(): Promise<Harness> {
63
+ let api: DragPresence | null = null;
64
+ const target = document.createElement('div');
65
+ const child = document.createElement('div');
66
+ target.appendChild(child);
67
+ const outside = document.createElement('div');
68
+ container.append(target, outside);
69
+
70
+ const Probe = () => {
71
+ api = useDragPresence();
72
+ return null;
73
+ };
74
+ await act(async () => {
75
+ render(h(Probe, {}), container);
76
+ });
77
+
78
+ return { api: () => api!, target, child, outside };
79
+ }
80
+
81
+ /** A `dragleave` as the browser delivers one, with the element being entered. */
82
+ function leaveEvent(currentTarget: HTMLElement, relatedTarget: Node | null) {
83
+ return { currentTarget, relatedTarget } as never;
84
+ }
85
+
86
+ describe('useDragPresence', () => {
87
+ test('an enter lights the target and a leave to another element clears it', async () => {
88
+ const harness = await mount();
89
+ expect(harness.api().active).toBe(false);
90
+
91
+ await act(async () => { harness.api().enter(); });
92
+ expect(harness.api().active).toBe(true);
93
+
94
+ await act(async () => { harness.api().leave(leaveEvent(harness.target, harness.outside)); });
95
+ expect(harness.api().active).toBe(false);
96
+ });
97
+
98
+ test('a move between children keeps it lit, however many children are crossed', async () => {
99
+ const harness = await mount();
100
+ await act(async () => { harness.api().enter(); });
101
+
102
+ // The shape that broke the counter: enter/leave pairs as the pointer walks
103
+ // across nested children, every leave naming a node still inside.
104
+ for (let pass = 0; pass < 6; pass += 1) {
105
+ await act(async () => { harness.api().enter(); });
106
+ await act(async () => { harness.api().leave(leaveEvent(harness.target, harness.child)); });
107
+ }
108
+
109
+ expect(harness.api().active).toBe(true);
110
+
111
+ // And one real exit still clears it — the count cannot have drifted.
112
+ await act(async () => { harness.api().leave(leaveEvent(harness.target, harness.outside)); });
113
+ expect(harness.api().active).toBe(false);
114
+ });
115
+
116
+ test('a leave with no relatedTarget is an exit, which is how a pointer leaving the window arrives', async () => {
117
+ const harness = await mount();
118
+ await act(async () => { harness.api().enter(); });
119
+ await act(async () => { harness.api().leave(leaveEvent(harness.target, null)); });
120
+ expect(harness.api().active).toBe(false);
121
+ });
122
+
123
+ test('a drop anywhere clears it, so a gesture claimed elsewhere leaves nothing lit', async () => {
124
+ const harness = await mount();
125
+ await act(async () => { harness.api().enter(); });
126
+ await act(async () => { window.dispatchEvent(new window.Event('drop')); });
127
+ expect(harness.api().active).toBe(false);
128
+ });
129
+
130
+ test('a dragend clears it, which is what a cancelled in-app drag fires', async () => {
131
+ const harness = await mount();
132
+ await act(async () => { harness.api().enter(); });
133
+ await act(async () => { window.dispatchEvent(new window.Event('dragend')); });
134
+ expect(harness.api().active).toBe(false);
135
+ });
136
+
137
+ test('a pointer move with no button held clears it, which is the only signal a cancelled OS drag leaves', async () => {
138
+ // Measured in Chromium: an Escape during a real file drag delivers no
139
+ // `dragleave`, no `dragend` and no `keydown` to the page — but the drag
140
+ // suppressed pointer movement for its whole duration, so the first move
141
+ // afterwards is proof the gesture is over.
142
+ const harness = await mount();
143
+ await act(async () => { harness.api().enter(); });
144
+
145
+ const move = (buttons: number) => {
146
+ const event = new window.PointerEvent('pointermove', { bubbles: true });
147
+ Object.defineProperty(event, 'buttons', { value: buttons });
148
+ window.dispatchEvent(event);
149
+ };
150
+
151
+ // A held button is a drag still in progress (or any other gesture), so the
152
+ // overlay stays.
153
+ await act(async () => { move(1); });
154
+ expect(harness.api().active).toBe(true);
155
+
156
+ await act(async () => { move(0); });
157
+ expect(harness.api().active).toBe(false);
158
+ });
159
+
160
+ test('an idle page ignores pointer moves entirely', async () => {
161
+ const harness = await mount();
162
+ const event = new window.PointerEvent('pointermove', { bubbles: true });
163
+ Object.defineProperty(event, 'buttons', { value: 0 });
164
+ await act(async () => { window.dispatchEvent(event); });
165
+ expect(harness.api().active).toBe(false);
166
+ });
167
+
168
+ test('two mounted targets keep their own state, and one unmounting leaves the other lit', async () => {
169
+ // The chat's drop target and the editor's are mounted at the same time, so a
170
+ // backstop registered once for the module would clear both — and one of them
171
+ // unmounting would remove the other's listener.
172
+ const containerB = document.body.appendChild(document.createElement('div'));
173
+ let first: DragPresence | null = null;
174
+ let second: DragPresence | null = null;
175
+ const A = () => { first = useDragPresence(); return null; };
176
+ const B = () => { second = useDragPresence(); return null; };
177
+ await act(async () => {
178
+ render(h(A, {}), container);
179
+ render(h(B, {}), containerB);
180
+ });
181
+
182
+ await act(async () => { first!.enter(); });
183
+ await act(async () => { second!.enter(); });
184
+ expect([first!.active, second!.active]).toEqual([true, true]);
185
+
186
+ await act(async () => { render(null, container); });
187
+ expect(second!.active).toBe(true);
188
+
189
+ await act(async () => { window.dispatchEvent(new window.Event('dragend')); });
190
+ expect(second!.active).toBe(false);
191
+ containerB.remove();
192
+ });
193
+ });
@@ -0,0 +1,108 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * Whether a file drag is currently over a drop target — the one rule every
8
+ * overlay in the app asks.
9
+ *
10
+ * The counter this replaces was the bug. `dragenter`/`dragleave` fire for every
11
+ * nested child the pointer crosses, so both hooks counted them, and the counts
12
+ * did NOT cancel: `dragleave` was ignored when its `relatedTarget` was still
13
+ * inside the element (a move between children), while `dragenter` incremented
14
+ * unconditionally. Every child crossing therefore raised the counter by one and
15
+ * only a full exit lowered it by one — so after sweeping across a few message
16
+ * rows the counter was already 4, and leaving the panel brought it to 3. The
17
+ * overlay stayed on with the pointer nowhere near the target (measured in
18
+ * Chromium: five child crossings, one exit, overlay still drawn).
19
+ *
20
+ * `relatedTarget` answers the question directly and symmetrically, so there is
21
+ * no count to drift:
22
+ *
23
+ * - A `dragleave` whose `relatedTarget` is still inside the element is a move
24
+ * between children — the pointer never left, so the overlay stays.
25
+ * - Every other `dragleave` is a real exit, and `relatedTarget` is null when the
26
+ * pointer left the window, which is exactly the case a counter could not
27
+ * distinguish from a missed event.
28
+ *
29
+ * The window listeners are the backstop for a gesture that ends without ever
30
+ * reaching the element again: an in-app drag released over another surface
31
+ * bubbles a `drop`, and an in-app drag cancelled with Escape fires `dragend` on
32
+ * its source. An OS file drag has neither (its source is outside the browser),
33
+ * which is why the element's own `dragleave` is the primary signal and these
34
+ * only clear what it could not.
35
+ *
36
+ * **A cancelled OS drag therefore sends nothing at all**, and that is the case
37
+ * that left the overlay on screen after the user pressed Escape: measured in
38
+ * Chromium, an Escape during a real file drag delivered no `dragleave`, no
39
+ * `dragend` and no `keydown` to the page. The recovery signal is the POINTER:
40
+ * a native drag swallows `pointermove`/`mousemove` for its whole duration
41
+ * (measured — three dispatched moves during an active drag produced none), and
42
+ * they resume the instant it ends, so a move carrying no held button means no
43
+ * drag can be in progress. That also covers a drag released outside the window.
44
+ */
45
+
46
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks';
47
+ import type { TargetedDragEvent } from 'preact';
48
+
49
+ export interface DragPresence {
50
+ /** True while a file drag hovers the element. Drives the overlay. */
51
+ active: boolean;
52
+ /** The pointer is over the element: entered it, or moved back onto it. */
53
+ enter: () => void;
54
+ /** A `dragleave` — clears only when the pointer actually left. */
55
+ leave: (event: TargetedDragEvent<HTMLElement>) => void;
56
+ /** The gesture is over here, or a descendant claimed it. */
57
+ clear: () => void;
58
+ }
59
+
60
+ export function useDragPresence(): DragPresence {
61
+ const [active, setActive] = useState(false);
62
+ // Read by the window backstops, which are bound once and must not be
63
+ // re-attached on every hover.
64
+ const activeRef = useRef(false);
65
+ const set = useCallback((next: boolean) => {
66
+ activeRef.current = next;
67
+ setActive(next);
68
+ }, []);
69
+
70
+ const enter = useCallback(() => set(true), [set]);
71
+ const clear = useCallback(() => set(false), [set]);
72
+
73
+ const leave = useCallback((event: TargetedDragEvent<HTMLElement>) => {
74
+ // `currentTarget` is the element the handler is bound to, which is the
75
+ // question: is the pointer still inside THIS target's subtree?
76
+ const target = event.currentTarget;
77
+ const related = event.relatedTarget;
78
+ if (target && related instanceof Node && target.contains(related)) return;
79
+ set(false);
80
+ }, [set]);
81
+
82
+ useEffect(() => {
83
+ window.addEventListener('drop', clear);
84
+ window.addEventListener('dragend', clear);
85
+ return () => {
86
+ window.removeEventListener('drop', clear);
87
+ window.removeEventListener('dragend', clear);
88
+ };
89
+ }, [clear]);
90
+
91
+ // The cancelled-OS-drag recovery. A drag is performed with a button held, and
92
+ // a native drag suppresses pointer movement entirely, so a move with NO
93
+ // buttons held can only happen after the gesture is over. Guarded on `active`
94
+ // so an idle page pays one boolean read per move and no re-render.
95
+ useEffect(() => {
96
+ const onMove = (event: PointerEvent) => {
97
+ if (!activeRef.current) return;
98
+ if (event.buttons !== 0) return;
99
+ set(false);
100
+ };
101
+ window.addEventListener('pointermove', onMove);
102
+ return () => window.removeEventListener('pointermove', onMove);
103
+ }, [set]);
104
+
105
+ // Memoised so a caller can depend on the object without rebuilding its own
106
+ // handlers every render — both callers spread these into props.
107
+ return useMemo(() => ({ active, enter, leave, clear }), [active, enter, leave, clear]);
108
+ }
@@ -0,0 +1,115 @@
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ /**
7
+ * The console's half of the shell's Settings › row.
8
+ *
9
+ * `useSettingsModal` is what a desktop layout gives the modal, and it is driven
10
+ * from two places: the sidebar footer's button and the `omp:open-settings`
11
+ * window event. The event is the ONLY path a native shell has — the macOS menu
12
+ * row dispatches it (`PageEvents.swift`), and the Linux one sends the same
13
+ * string — so the hook must open the modal from it, adopt the category the
14
+ * caller asked for, and carry the provider dialog request.
15
+ *
16
+ * The failure this pins is silent in both directions: a hook that never
17
+ * subscribed leaves the menu row doing nothing on screen, and a hook that
18
+ * opened the modal WITHOUT the requested category lands the user on Appearance
19
+ * when they asked for Providers.
20
+ */
21
+
22
+ import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:test';
23
+ import { Window } from 'happy-dom';
24
+ import { h, render } from 'preact';
25
+ import { act } from 'preact/test-utils';
26
+
27
+ import { useSettingsModal } from '@/client/hooks/ui/settings-modal';
28
+ import type { SettingsModalState } from '@/client/hooks/ui/settings-modal';
29
+
30
+ const DOM_GLOBALS = ['window', 'document', 'Event', 'CustomEvent', 'HTMLElement', 'Node', 'Element'] as const;
31
+ const nativeGlobals: Partial<Record<(typeof DOM_GLOBALS)[number], unknown>> = {};
32
+
33
+ let container: HTMLElement | null = null;
34
+
35
+ beforeAll(() => {
36
+ const win = new Window({ url: 'http://localhost' });
37
+ const target = globalThis as unknown as Record<string, unknown>;
38
+ for (const key of DOM_GLOBALS) {
39
+ if (!(key in nativeGlobals)) nativeGlobals[key] = target[key];
40
+ target[key] = (win as unknown as Record<string, unknown>)[key];
41
+ }
42
+ });
43
+
44
+ afterAll(() => {
45
+ const target = globalThis as unknown as Record<string, unknown>;
46
+ for (const key of DOM_GLOBALS) target[key] = nativeGlobals[key];
47
+ });
48
+
49
+ afterEach(() => {
50
+ if (container) render(null, container);
51
+ container?.remove();
52
+ container = null;
53
+ });
54
+
55
+ async function mount(): Promise<{ current: SettingsModalState }> {
56
+ const box: { current: SettingsModalState } = { current: null as never };
57
+ function Probe() {
58
+ box.current = useSettingsModal();
59
+ return null;
60
+ }
61
+ container ??= document.body.appendChild(document.createElement('div'));
62
+ await act(async () => {
63
+ render(h(Probe, null), container as HTMLElement);
64
+ });
65
+ return box;
66
+ }
67
+
68
+ /** Dispatch the way a shell does: a `CustomEvent` carrying a detail record. */
69
+ async function shellOpensSettings(detail: Record<string, unknown>): Promise<void> {
70
+ await act(async () => {
71
+ window.dispatchEvent(new CustomEvent('omp:open-settings', { detail }));
72
+ });
73
+ }
74
+
75
+ describe('useSettingsModal', () => {
76
+ test('a closed modal is the starting state', async () => {
77
+ const box = await mount();
78
+ expect(box.current.isOpen).toBe(false);
79
+ });
80
+
81
+ test("the shell's event opens it", async () => {
82
+ const box = await mount();
83
+ await shellOpensSettings({});
84
+ expect(box.current.isOpen).toBe(true);
85
+ });
86
+
87
+ test('the requested category is adopted, not replaced by the default', async () => {
88
+ const box = await mount();
89
+ await shellOpensSettings({ category: 'providers' });
90
+ expect(box.current.isOpen).toBe(true);
91
+ expect(box.current.category).toBe('providers');
92
+ });
93
+
94
+ test('the provider dialog request rides along', async () => {
95
+ const box = await mount();
96
+ await shellOpensSettings({ category: 'providers', autoOpenAdd: true });
97
+ expect(box.current.autoOpenAddProvider).toBe(true);
98
+ });
99
+
100
+ test('closing clears the provider request, so the next open is a plain one', async () => {
101
+ const box = await mount();
102
+ await shellOpensSettings({ autoOpenAdd: true });
103
+ await act(async () => box.current.close());
104
+ expect(box.current.isOpen).toBe(false);
105
+ expect(box.current.autoOpenAddProvider).toBe(false);
106
+ });
107
+
108
+ test('unmounting drops the listener', async () => {
109
+ const box = await mount();
110
+ render(null, container as HTMLElement);
111
+ container = null;
112
+ await shellOpensSettings({});
113
+ expect(box.current.isOpen).toBe(false);
114
+ });
115
+ });