@kabmo-labs/kabmo 0.17.0 → 0.18.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.
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +538 -457
- package/dist/client/static/js/index-hp75vsv9.css +1 -0
- package/dist/client/static/js/{index-rerjq5th.js → index-tc09dtca.js} +823 -814
- package/package.json +1 -1
- package/src/client/components/common/MarkdownRenderer.tsx +136 -89
- package/src/client/components/common/anchored-menu/index.test.tsx +141 -0
- package/src/client/components/common/anchored-menu/index.tsx +143 -0
- package/src/client/components/common/markdown-review/ReviewPopover.tsx +144 -0
- package/src/client/components/common/markdown-review/ReviewSummary.tsx +108 -0
- package/src/client/components/common/markdown-review/index.tsx +129 -0
- package/src/client/components/common/server-control/Panel.tsx +33 -23
- package/src/client/components/common/server-control/index.test.tsx +11 -5
- package/src/client/components/common/server-control/index.tsx +10 -8
- package/src/client/components/layout/session-sidebar/CategoryHeader.tsx +42 -21
- package/src/client/components/layout/session-sidebar/CategoryHeader.worktree.test.tsx +36 -17
- package/src/client/components/layout/session-sidebar/CategoryItem.tsx +8 -7
- package/src/client/components/layout/session-sidebar/about-modal/NotesDialog.tsx +94 -0
- package/src/client/components/layout/session-sidebar/about-modal/VersionLine.test.tsx +68 -0
- package/src/client/components/layout/session-sidebar/about-modal/VersionLine.tsx +36 -0
- package/src/client/components/layout/session-sidebar/about-modal/index.tsx +57 -34
- package/src/client/components/layout/session-sidebar/modern/WorkspaceHeader.tsx +28 -10
- package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +7 -6
- package/src/client/components/layout/session-sidebar/modern/workspace-header.test.tsx +1 -1
- package/src/client/components/layout/update-popup/ReleaseNotes.tsx +8 -3
- package/src/client/components/layout/update-popup/index.tsx +5 -1
- package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +13 -15
- package/src/client/components/mobile/mobile-right-sidebar/Header.tsx +23 -1
- package/src/client/components/mobile/mobile-right-sidebar/MissingFileBanner.tsx +30 -0
- package/src/client/components/mobile/mobile-right-sidebar/PreviewScroller.tsx +18 -2
- package/src/client/components/mobile/mobile-session-sidebar/Item.tsx +22 -67
- package/src/client/components/mobile/mobile-session-sidebar/ItemMenu.tsx +110 -0
- package/src/client/components/settings/SettingsSidebarList.tsx +20 -0
- package/src/client/components/settings/categories/AgentSettings.tsx +23 -50
- package/src/client/components/settings/categories/CommandSettings.tsx +2 -2
- package/src/client/components/settings/categories/McpSettings.tsx +4 -21
- package/src/client/components/settings/categories/OmpSettings.tsx +147 -194
- package/src/client/components/settings/categories/SkillSettings.tsx +2 -2
- package/src/client/components/settings/categories/TokenUsageSettings.tsx +211 -229
- package/src/client/components/settings/categories/agent-settings/DetailPane.tsx +229 -237
- package/src/client/components/settings/categories/agent-settings/ModelPicker.tsx +71 -0
- package/src/client/components/settings/categories/agent-settings/SidebarList.tsx +9 -2
- package/src/client/components/settings/categories/agent-settings/levels.ts +38 -0
- package/src/client/components/settings/categories/behavior-settings/Editor.tsx +27 -7
- package/src/client/components/settings/categories/chat-settings/ToolRenderSection.tsx +23 -5
- package/src/client/components/settings/categories/command-settings/DetailPane.tsx +75 -33
- package/src/client/components/settings/categories/command-settings/SidebarList.tsx +7 -0
- package/src/client/components/settings/categories/mcp-settings/DetailPane.tsx +91 -76
- package/src/client/components/settings/categories/mcp-settings/EnvVarsSection.tsx +83 -0
- package/src/client/components/settings/categories/mcp-settings/ImportModal.tsx +26 -13
- package/src/client/components/settings/categories/mcp-settings/SidebarList.tsx +20 -0
- package/src/client/components/settings/categories/omp-settings/ConfigTabs.tsx +97 -36
- package/src/client/components/settings/categories/omp-settings/EngineHeader.tsx +151 -0
- package/src/client/components/settings/categories/omp-settings/ExtensionsPanel.tsx +68 -32
- package/src/client/components/settings/categories/omp-settings/OrderedChips.tsx +119 -0
- package/src/client/components/settings/categories/omp-settings/PresetBar.tsx +14 -7
- package/src/client/components/settings/categories/omp-settings/RoleEditor.tsx +15 -6
- package/src/client/components/settings/categories/omp-settings/RowEditor.tsx +165 -226
- package/src/client/components/settings/categories/omp-settings/types.ts +28 -0
- package/src/client/components/settings/categories/skill-settings/DetailHeader.tsx +35 -7
- package/src/client/components/settings/categories/skill-settings/DetailPane.tsx +34 -14
- package/src/client/components/settings/categories/skill-settings/SidebarList.tsx +21 -0
- package/src/client/components/settings/categories/skill-settings/pane.test.tsx +15 -6
- package/src/client/components/settings/categories/token-usage-settings/Breakdown.tsx +242 -102
- package/src/client/components/settings/categories/token-usage-settings/Chart.tsx +130 -56
- package/src/client/components/settings/categories/token-usage-settings/Controls.tsx +135 -0
- package/src/client/components/settings/categories/token-usage-settings/MetricsGrid.tsx +55 -33
- package/src/client/components/settings/categories/token-usage-settings/Provenance.tsx +59 -0
- package/src/client/components/settings/categories/token-usage-settings/panel.test.tsx +239 -0
- package/src/client/components/workspace/browser-panel/Screencast.test.ts +1 -0
- package/src/client/components/workspace/browser-panel/Screencast.tsx +17 -22
- package/src/client/components/workspace/browser-panel/TabStrip.test.tsx +158 -0
- package/src/client/components/workspace/browser-panel/TabStrip.tsx +90 -0
- package/src/client/components/workspace/browser-panel/index.tsx +50 -4
- package/src/client/components/workspace/browser-toolbar/index.tsx +41 -1
- package/src/client/components/workspace/chat-timeline/ThinkingSection.tsx +3 -1
- package/src/client/components/workspace/chat-timeline/ToolCallingSection.tsx +2 -0
- package/src/client/components/workspace/chat-timeline/block-chrome.test.tsx +13 -2
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.test.ts +88 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Bash.tsx +24 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.test.ts +34 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Read.tsx +5 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ArtifactChip.tsx +7 -6
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Tui.test.tsx +237 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Tui.tsx +191 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/styles/styles.test.tsx +1 -1
- package/src/client/components/workspace/diff-panel/MergeDiffView.tsx +21 -7
- package/src/client/components/workspace/diff-panel/changes.test.ts +51 -0
- package/src/client/components/workspace/diff-panel/changes.ts +28 -12
- package/src/client/components/workspace/diff-panel/merge.test.ts +28 -0
- package/src/client/components/workspace/diff-panel/merge.ts +21 -1
- package/src/client/components/workspace/editor/Body.tsx +7 -0
- package/src/client/components/workspace/editor/FileDocument.tsx +15 -21
- package/src/client/components/workspace/editor/FilePane.tsx +12 -0
- package/src/client/components/workspace/editor/PreviewPane.tsx +70 -0
- package/src/client/components/workspace/editor/Toolbar.tsx +17 -1
- package/src/client/components/workspace/editor/index.tsx +25 -22
- package/src/client/components/workspace/editor/toolbar-clusters.test.ts +14 -0
- package/src/client/components/workspace/editor/toolbar-clusters.tsx +12 -86
- package/src/client/components/workspace/editor/toolbar-toggle.tsx +53 -0
- package/src/client/components/workspace/editor/toolbar-write-clusters.tsx +112 -0
- package/src/client/components/workspace/git-panel/commit-modal.test.ts +5 -3
- package/src/client/components/workspace/user-browser-panel/AddressBar.tsx +5 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateBar.test.tsx +201 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateBar.tsx +112 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateLayer.test.tsx +225 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateLayer.tsx +217 -0
- package/src/client/components/workspace/user-browser-panel/AnnotateTarget.tsx +99 -0
- package/src/client/components/workspace/user-browser-panel/Frame.tsx +30 -1
- package/src/client/components/workspace/user-browser-panel/index.tsx +81 -2
- package/src/client/data/agent-data/builtin.ts +0 -10
- package/src/client/data/agent-data/orchestrator.ts +0 -45
- package/src/client/data/agent-data/specialist.ts +0 -100
- package/src/client/data/mock/token-usage.ts +170 -120
- package/src/client/data/settings/omp-schema.json +33 -1
- package/src/client/hooks/browser/use-annotate.test.ts +143 -0
- package/src/client/hooks/browser/use-annotate.ts +98 -0
- package/src/client/hooks/browser/use-close-tab.test.ts +148 -0
- package/src/client/hooks/browser/use-close-tab.ts +67 -0
- package/src/client/hooks/browser/use-screencast.ts +13 -1
- package/src/client/hooks/chat/composer/browser-context.test.ts +117 -0
- package/src/client/hooks/chat/composer/browser-context.ts +40 -16
- package/src/client/hooks/chat/composer/items.test.ts +1 -1
- package/src/client/hooks/chat/composer/review-context.ts +38 -0
- package/src/client/hooks/chat/composer/use-draft.test.ts +132 -0
- package/src/client/hooks/chat/composer/use-draft.ts +31 -2
- package/src/client/hooks/chat/timeline/row-window.test.ts +15 -2
- package/src/client/hooks/editor/use-document-annotations.ts +124 -0
- package/src/client/hooks/editor/use-editor-panel-ui.ts +52 -0
- package/src/client/hooks/editor/use-file-review-mode.ts +35 -0
- package/src/client/hooks/editor/use-preview-review.ts +148 -0
- package/src/client/hooks/settings/agents.ts +149 -0
- package/src/client/hooks/settings/crud-list.test.ts +28 -6
- package/src/client/hooks/settings/crud-list.ts +96 -57
- package/src/client/hooks/settings/omp-config.ts +164 -0
- package/src/client/hooks/settings/useTokenUsage.ts +125 -0
- package/src/client/hooks/settings/write-feedback.test.ts +104 -0
- package/src/client/hooks/settings/write-feedback.ts +63 -0
- package/src/client/hooks/ui/markdown-hydration.ts +125 -0
- package/src/client/hooks/ui/updates/index.test.ts +100 -1
- package/src/client/hooks/ui/updates/index.ts +50 -0
- package/src/client/hooks/ui/updates/notes.test.ts +138 -0
- package/src/client/hooks/ui/updates/notes.ts +57 -0
- package/src/client/lib/browser/element-at.test.ts +229 -0
- package/src/client/lib/browser/element-at.ts +139 -0
- package/src/client/lib/review/client.ts +73 -0
- package/src/client/lib/router/search-params.ts +42 -20
- package/src/client/tailwind.css +65 -0
- package/src/server/lib/btw/lifecycle.server.ts +2 -2
- package/src/server/lib/db/schema.ts +7 -0
- package/src/server/lib/db/seed-terminal.test.ts +4 -0
- package/src/server/lib/omp/config/agent-definitions.test.ts +273 -0
- package/src/server/lib/omp/config/agents.ts +231 -61
- package/src/server/lib/omp/config/definitions.test.ts +9 -105
- package/src/server/lib/omp/config/extensions.ts +16 -7
- package/src/server/lib/omp/config/omp-config-files.test.ts +27 -0
- package/src/server/lib/omp/rpc/constants.test.ts +11 -0
- package/src/server/lib/omp/rpc/constants.ts +15 -0
- package/src/server/lib/omp/rpc/process.ts +11 -14
- package/src/server/lib/omp/rpc/session-commands.ts +6 -3
- package/src/server/lib/omp/rpc/utility-agent-dir.test.ts +134 -0
- package/src/server/lib/omp/rpc/utility-agent-dir.ts +138 -0
- package/src/server/lib/omp/rpc/utility.ts +12 -0
- package/src/server/lib/review/schema.server.ts +45 -0
- package/src/server/lib/review/store.server.ts +138 -0
- package/src/server/lib/terminal/attach-replay.test.ts +132 -0
- package/src/server/lib/terminal/output.test.ts +166 -0
- package/src/server/lib/terminal/output.ts +104 -0
- package/src/server/lib/terminal/runtime.server.ts +5 -0
- package/src/server/lib/terminal/send-result.test.ts +33 -0
- package/src/server/lib/terminal/send-result.ts +32 -0
- package/src/server/lib/terminal/session.server.ts +45 -11
- package/src/server/lib/terminal/store-liveness.test.ts +4 -0
- package/src/server/lib/updates/active-notes.test.ts +149 -0
- package/src/server/lib/updates/active-notes.ts +75 -0
- package/src/server/lib/updates/changelog-file.ts +42 -11
- package/src/server/lib/updates/changelog.test.ts +15 -1
- package/src/server/lib/updates/omp.ts +42 -15
- package/src/server/lib/updates/release-url.ts +27 -1
- package/src/server/lib/usage/index-parse.test.ts +200 -0
- package/src/server/lib/usage/index-parse.ts +178 -0
- package/src/server/lib/usage/index-query.server.ts +248 -0
- package/src/server/lib/usage/index-scan.server.ts +343 -0
- package/src/server/lib/usage/index-schema.server.ts +82 -0
- package/src/server/lib/usage/index.test.ts +234 -0
- package/src/server/lib/usage/providers.server.ts +4 -53
- package/src/server/routes/browser/index.ts +3 -0
- package/src/server/routes/browser/stream.ts +31 -2
- package/src/server/routes/browser/tabs.test.ts +94 -0
- package/src/server/routes/browser/tabs.ts +85 -0
- package/src/server/routes/index.ts +2 -0
- package/src/server/routes/review/bindings.ts +11 -0
- package/src/server/routes/review/index.ts +127 -0
- package/src/server/routes/review/routes.test.ts +191 -0
- package/src/server/routes/settings/agents.test.ts +180 -0
- package/src/server/routes/settings/agents.ts +95 -102
- package/src/server/routes/settings/mcp.test.ts +135 -0
- package/src/server/routes/settings/mcp.ts +14 -6
- package/src/server/routes/settings/settings-store-routes.test.ts +6 -113
- package/src/server/routes/telemetry/tokens.test.ts +145 -0
- package/src/server/routes/telemetry/tokens.ts +70 -149
- package/src/server/routes/terminal/ws.ts +6 -5
- package/src/server/routes/updates/index.ts +2 -0
- package/src/server/routes/updates/notes.ts +34 -0
- package/src/server/routes/updates/routes.test.ts +49 -0
- package/src/shared/lib/browser/annotate.test.ts +283 -0
- package/src/shared/lib/browser/annotate.ts +231 -0
- package/src/shared/lib/browser/browser-viewer.test.ts +26 -4
- package/src/shared/lib/browser/frame-gate.test.ts +91 -0
- package/src/shared/lib/browser/frame-gate.ts +69 -0
- package/src/shared/lib/browser/frame-params.test.ts +43 -0
- package/src/shared/lib/browser/frame-params.ts +60 -0
- package/src/shared/lib/browser/frame-pipeline.test.ts +132 -0
- package/src/shared/lib/browser/frame-pipeline.ts +80 -0
- package/src/shared/lib/browser/page-context.ts +60 -4
- package/src/shared/lib/browser/snapshot.test.ts +115 -0
- package/src/shared/lib/browser/snapshot.ts +112 -0
- package/src/shared/lib/browser/tab-close.test.ts +74 -0
- package/src/shared/lib/browser/tab-close.ts +77 -0
- package/src/shared/lib/browser/viewer.ts +45 -26
- package/src/shared/lib/chat/timeline/block-chrome.ts +19 -0
- package/src/shared/lib/chat/timeline/tool-render-mode.ts +17 -4
- package/src/shared/lib/chat/tool/artifact.ts +55 -20
- package/src/shared/lib/chat/tool/bash-facts.ts +96 -0
- package/src/shared/lib/chat/tool/minimized.test.ts +98 -0
- package/src/shared/lib/chat/tool/minimized.ts +64 -0
- package/src/shared/lib/chat/tool/summary.test.ts +28 -8
- package/src/shared/lib/chat/tool/summary.ts +3 -32
- package/src/shared/lib/markdown/blocks/anchor.ts +63 -0
- package/src/shared/lib/markdown/blocks/blocks.test.ts +141 -0
- package/src/shared/lib/markdown/blocks/renderer.ts +120 -0
- package/src/shared/lib/markdown/blocks/scan.ts +100 -0
- package/src/shared/lib/markdown/blocks/types.ts +62 -0
- package/src/shared/lib/markdown/marked.ts +58 -147
- package/src/shared/lib/markdown/math-extensions.ts +168 -0
- package/src/shared/lib/markdown/review/prompt.ts +77 -0
- package/src/shared/lib/markdown/review/reconcile.ts +55 -0
- package/src/shared/lib/markdown/review/review.test.ts +96 -0
- package/src/shared/lib/markdown/xml-sections.test.ts +140 -0
- package/src/shared/lib/markdown/xml-sections.ts +139 -0
- package/src/shared/lib/models/provider/label.ts +57 -0
- package/src/shared/lib/omp/config/agent-file.test.ts +54 -0
- package/src/shared/lib/omp/config/agent-file.ts +43 -0
- package/src/shared/lib/omp/config/row-value.test.ts +95 -0
- package/src/shared/lib/omp/config/row-value.ts +97 -0
- package/src/shared/lib/omp/config/schema.test.ts +31 -0
- package/src/shared/lib/omp/config/schema.ts +6 -0
- package/src/shared/lib/omp/rpc/frame.test.ts +48 -47
- package/src/shared/lib/omp/rpc/frame.ts +49 -40
- package/src/shared/lib/ui/menu-anchor.test.ts +69 -0
- package/src/shared/lib/ui/menu-anchor.ts +89 -0
- package/src/shared/lib/usage/breakdown.test.ts +197 -0
- package/src/shared/lib/usage/breakdown.ts +241 -0
- package/src/shared/lib/usage/range.test.ts +204 -0
- package/src/shared/lib/usage/range.ts +240 -0
- package/src/shared/lib/usage/view.test.ts +85 -0
- package/src/shared/lib/usage/view.ts +74 -0
- package/src/shared/types/browser.ts +78 -0
- package/src/shared/types/index.ts +1 -0
- package/src/shared/types/settings/agent.ts +28 -6
- package/src/shared/types/settings/state.ts +4 -3
- package/src/shared/types/settings/write.ts +6 -0
- package/src/shared/types/tokenUsage.ts +128 -21
- package/src/shared/types/updates.ts +20 -0
- package/dist/client/static/js/index-3ve31rzx.css +0 -1
- package/src/server/lib/omp/session/usage/aggregate-shaping.test.ts +0 -149
- package/src/server/lib/omp/session/usage/aggregate.test.ts +0 -243
- package/src/server/lib/omp/session/usage/aggregate.ts +0 -330
- package/src/server/lib/omp/session/usage/shaping.ts +0 -130
package/package.json
CHANGED
|
@@ -12,6 +12,12 @@
|
|
|
12
12
|
* delegated click handler manages the per-block "Copy" buttons. Mermaid
|
|
13
13
|
* fences hydrate async into sanitized SVGs after mount.
|
|
14
14
|
*
|
|
15
|
+
* ONE more mode: a `review` bridge switches the parse to the MAPPED render, so
|
|
16
|
+
* every top-level block carries `data-md-block` and a click names the block it
|
|
17
|
+
* landed on instead of following its link. The handler consults that bridge
|
|
18
|
+
* FIRST — a second click listener on the same container would have to agree with
|
|
19
|
+
* this one about precedence, and the two would eventually disagree.
|
|
20
|
+
*
|
|
15
21
|
* Three source kinds, one pipeline: agent output (default), a workspace file's
|
|
16
22
|
* preview (`document`), and a wiki page (`wikiScope`). The last two differ only
|
|
17
23
|
* in what they let through — a document's own HTML is markup, and a wiki page
|
|
@@ -21,15 +27,16 @@
|
|
|
21
27
|
|
|
22
28
|
import type { TargetedMouseEvent } from 'preact';
|
|
23
29
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'preact/hooks';
|
|
24
|
-
import { renderMarkdown } from '@/shared/lib/markdown/marked';
|
|
30
|
+
import { renderMarkdown, renderMappedMarkdown } from '@/shared/lib/markdown/marked';
|
|
31
|
+
import type { BlockVerdict, MdBlock } from '@/shared/lib/markdown/blocks/types';
|
|
25
32
|
import { rewriteDocumentReferences, type DocumentScope } from '@/shared/lib/markdown/document-urls';
|
|
26
33
|
import { rewriteWikiReferences, type WikiRenderScope } from '@/shared/lib/wiki/document-urls';
|
|
27
34
|
import { expandGollumLinks } from '@/shared/lib/wiki/pages';
|
|
28
|
-
import {
|
|
35
|
+
import { MATH_PENDING_CLASS, preloadKatex } from '@/shared/lib/markdown/katex';
|
|
29
36
|
import { sanitizeHtml } from '@/shared/lib/markdown/sanitize';
|
|
30
|
-
import { hydrateMermaidBlocks } from '@/shared/lib/markdown/mermaid';
|
|
31
37
|
import { DIRECTIVE_BLOCK_CLASS } from '@/shared/lib/markdown/directives';
|
|
32
38
|
import { hydrateDirectiveBlocks, unmountDirectiveBlocks } from '@/client/lib/plugins/directives';
|
|
39
|
+
import { useMathHydration, useMermaidHydration } from '@/client/hooks/ui/markdown-hydration';
|
|
33
40
|
import { useDirectiveContext, useDirectiveRenderer } from '@/client/hooks/workspace/directives';
|
|
34
41
|
import { copyToClipboard } from '@/client/hooks/ui/clipboard';
|
|
35
42
|
import { openFileInEditor } from '@/client/hooks/ui/open-file';
|
|
@@ -38,6 +45,7 @@ import { DiagramViewer } from '@/client/components/common/diagram-viewer';
|
|
|
38
45
|
import { readDiagramBlock, type DiagramSnapshot } from '@/client/components/common/diagram-viewer/read-block';
|
|
39
46
|
|
|
40
47
|
|
|
48
|
+
/** `useLayoutEffect` warns under SSR; the document renderer runs in both. */
|
|
41
49
|
const useIsomorphicLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
|
42
50
|
|
|
43
51
|
interface MarkdownRendererProps {
|
|
@@ -59,9 +67,42 @@ interface MarkdownRendererProps {
|
|
|
59
67
|
wikiScope?: WikiRenderScope;
|
|
60
68
|
/** Follow a wiki link to another page of the same wiki. */
|
|
61
69
|
onWikiNavigate?: (path: string) => void;
|
|
70
|
+
/**
|
|
71
|
+
* The review layer, when it is ON.
|
|
72
|
+
*
|
|
73
|
+
* Absent is the ordinary renderer: no `data-md-*` attributes are emitted and
|
|
74
|
+
* every click keeps its usual meaning. Present switches the pipeline to the
|
|
75
|
+
* MAPPED render (each block carries its identity) and routes block clicks to
|
|
76
|
+
* `onPick` BEFORE the copy/link/diagram branches — one handler, one
|
|
77
|
+
* precedence rule, because two handlers on one container would have to agree
|
|
78
|
+
* about which wins.
|
|
79
|
+
*/
|
|
80
|
+
review?: MarkdownReviewBridge;
|
|
81
|
+
/** The blocks the render produced, for a caller that stores judgements. */
|
|
82
|
+
onBlocks?: (blocks: MdBlock[]) => void;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** What the renderer needs of the review surface — nothing about storage. */
|
|
86
|
+
export interface MarkdownReviewBridge {
|
|
87
|
+
active: boolean;
|
|
88
|
+
/** Verdicts by block key, painted as `data-md-verdict`. */
|
|
89
|
+
marks: ReadonlyMap<string, BlockVerdict>;
|
|
90
|
+
/** The block the popover is open on, painted as `data-md-selected`. */
|
|
91
|
+
selected: string | null;
|
|
92
|
+
/** A block was clicked while review was active; `rect` is where it was. */
|
|
93
|
+
onPick: (block: MdBlock, rect: DOMRect) => void;
|
|
62
94
|
}
|
|
63
95
|
|
|
64
|
-
export function MarkdownRenderer({
|
|
96
|
+
export function MarkdownRenderer({
|
|
97
|
+
content,
|
|
98
|
+
className = '',
|
|
99
|
+
document: isDocument = false,
|
|
100
|
+
scope,
|
|
101
|
+
wikiScope,
|
|
102
|
+
onWikiNavigate,
|
|
103
|
+
review,
|
|
104
|
+
onBlocks,
|
|
105
|
+
}: MarkdownRendererProps) {
|
|
65
106
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
66
107
|
/** Block the open viewer was read from, so a theme re-render can refresh it. */
|
|
67
108
|
const diagramBlockRef = useRef<HTMLElement | null>(null);
|
|
@@ -81,25 +122,70 @@ export function MarkdownRenderer({ content, className = '', document: isDocument
|
|
|
81
122
|
/** Presence, not identity: this drives the click handler, never the parse. */
|
|
82
123
|
const isWiki = wikiScope !== undefined;
|
|
83
124
|
|
|
84
|
-
|
|
85
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Review mode is a MODE, not a flag on the parse: it changes which pipeline
|
|
127
|
+
* entry point runs, because only the mapped one emits `data-md-block`. Kept
|
|
128
|
+
* to a boolean so a caller passing a fresh bridge object per render does not
|
|
129
|
+
* re-parse the whole document.
|
|
130
|
+
*/
|
|
131
|
+
const reviewActive = review?.active === true;
|
|
132
|
+
|
|
133
|
+
const rendered = useMemo(() => {
|
|
134
|
+
if (!content) return { html: '', blocks: [] as MdBlock[] };
|
|
86
135
|
// Gollum's `[[Page]]` is invisible to a markdown parser, so it is expanded
|
|
87
136
|
// before parsing rather than patched afterwards.
|
|
88
137
|
const source = wikiPath ? expandGollumLinks(content) : content;
|
|
89
|
-
const
|
|
138
|
+
const allowHtml = isDocument || isWiki;
|
|
139
|
+
const mapped = reviewActive ? renderMappedMarkdown(source, { allowHtml }) : null;
|
|
140
|
+
const markdown = mapped ? mapped.html : renderMarkdown(source, { allowHtml });
|
|
90
141
|
const scoped = wikiPath && wikiEntries
|
|
91
|
-
? rewriteWikiReferences(
|
|
142
|
+
? rewriteWikiReferences(markdown, { path: wikiPath, entries: wikiEntries, root: wikiRoot, repo: wikiRepo })
|
|
92
143
|
: isDocument && scopePath
|
|
93
|
-
? rewriteDocumentReferences(
|
|
94
|
-
:
|
|
144
|
+
? rewriteDocumentReferences(markdown, { path: scopePath, root: scopeRoot, repo: scopeRepo })
|
|
145
|
+
: markdown;
|
|
95
146
|
// If the message contains math, start fetching KaTeX during parse rather
|
|
96
147
|
// than waiting for the post-mount hydration effect — the request overlaps
|
|
97
148
|
// sanitization and the rest of the render instead of following it.
|
|
98
149
|
if (scoped.includes(MATH_PENDING_CLASS)) preloadKatex();
|
|
99
|
-
return sanitizeHtml(scoped);
|
|
150
|
+
return { html: sanitizeHtml(scoped), blocks: mapped?.blocks ?? [] };
|
|
100
151
|
// Primitives, never the `wikiScope` object: the panel memoizes it, but a
|
|
101
152
|
// call site that builds one inline must not re-parse the page per render.
|
|
102
|
-
}, [content, syntaxReady, isDocument, scopePath, scopeRoot, scopeRepo, isWiki, wikiPath, wikiEntries, wikiRoot, wikiRepo]);
|
|
153
|
+
}, [content, syntaxReady, isDocument, scopePath, scopeRoot, scopeRepo, isWiki, wikiPath, wikiEntries, wikiRoot, wikiRepo, reviewActive]);
|
|
154
|
+
|
|
155
|
+
const { html, blocks } = rendered;
|
|
156
|
+
|
|
157
|
+
// The caller stores judgements against these, so it has to know what the
|
|
158
|
+
// render actually produced — never what the file contains (a healed or
|
|
159
|
+
// truncated document renders fewer blocks than it has lines).
|
|
160
|
+
useEffect(() => {
|
|
161
|
+
if (onBlocks) onBlocks(blocks);
|
|
162
|
+
}, [blocks, onBlocks]);
|
|
163
|
+
|
|
164
|
+
const byKey = useMemo(() => new Map(blocks.map((block) => [block.key, block])), [blocks]);
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Paint the verdicts onto the rendered DOM.
|
|
168
|
+
*
|
|
169
|
+
* An imperative pass rather than markup: the annotations arrive from the
|
|
170
|
+
* server AFTER the HTML string was built (and the string is memoized on
|
|
171
|
+
* content), so there is nothing to interpolate. The attributes are what the
|
|
172
|
+
* CSS hangs off, and what the click handler reads.
|
|
173
|
+
*/
|
|
174
|
+
const marks = review?.marks;
|
|
175
|
+
const selected = review?.selected ?? null;
|
|
176
|
+
useIsomorphicLayoutEffect(() => {
|
|
177
|
+
const container = containerRef.current;
|
|
178
|
+
if (!container || !reviewActive) return;
|
|
179
|
+
for (const element of container.querySelectorAll<HTMLElement>('[data-md-block]')) {
|
|
180
|
+
const key = element.dataset.mdBlock;
|
|
181
|
+
if (!key) continue;
|
|
182
|
+
const verdict = marks?.get(key);
|
|
183
|
+
if (verdict) element.dataset.mdVerdict = verdict;
|
|
184
|
+
else delete element.dataset.mdVerdict;
|
|
185
|
+
if (key === selected) element.dataset.mdSelected = 'true';
|
|
186
|
+
else delete element.dataset.mdSelected;
|
|
187
|
+
}
|
|
188
|
+
}, [html, marks, selected, reviewActive]);
|
|
103
189
|
|
|
104
190
|
const hasMermaid = html.includes('mermaid-block');
|
|
105
191
|
const hasMath = html.includes(MATH_PENDING_CLASS);
|
|
@@ -125,87 +211,48 @@ export function MarkdownRenderer({ content, className = '', document: isDocument
|
|
|
125
211
|
}, [hasDirective, html, directive, slotContext]);
|
|
126
212
|
|
|
127
213
|
// KaTeX is ~522 kB of JS fetched only when a message contains math; the
|
|
128
|
-
// placeholders emitted by `marked.ts` are swapped for real markup
|
|
129
|
-
|
|
130
|
-
if (!hasMath || typeof window === 'undefined') return;
|
|
131
|
-
const container = containerRef.current;
|
|
132
|
-
if (!container) return;
|
|
133
|
-
|
|
134
|
-
let cancelled = false;
|
|
135
|
-
const hydrate = async () => {
|
|
136
|
-
if (!container.isConnected) return;
|
|
137
|
-
await hydrateMathBlocks(container);
|
|
138
|
-
if (cancelled) return;
|
|
139
|
-
};
|
|
140
|
-
void hydrate();
|
|
141
|
-
// Same reason as the mermaid observer: a timeline re-render can replace the
|
|
142
|
-
// container's innerHTML with an identical string without this effect
|
|
143
|
-
// re-running, stranding new placeholders. hydrate() only writes inside the
|
|
144
|
-
// placeholder elements, so it never re-triggers this observer.
|
|
145
|
-
const observer = new MutationObserver(() => {
|
|
146
|
-
const current = containerRef.current;
|
|
147
|
-
if (current?.isConnected) void hydrateMathBlocks(current);
|
|
148
|
-
});
|
|
149
|
-
observer.observe(container, { childList: true });
|
|
150
|
-
|
|
151
|
-
return () => {
|
|
152
|
-
cancelled = true;
|
|
153
|
-
observer.disconnect();
|
|
154
|
-
};
|
|
155
|
-
}, [hasMath, html]);
|
|
156
|
-
|
|
157
|
-
useIsomorphicLayoutEffect(() => {
|
|
158
|
-
if (!hasMermaid || typeof window === 'undefined') return;
|
|
159
|
-
const container = containerRef.current;
|
|
160
|
-
if (!container) return;
|
|
214
|
+
// placeholders emitted by `marked.ts` are swapped for real markup by the hook.
|
|
215
|
+
useMathHydration(containerRef, html, hasMath);
|
|
161
216
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const current = containerRef.current;
|
|
177
|
-
if (current?.isConnected) void hydrateMermaidBlocks(current, { cachedOnly: true });
|
|
178
|
-
window.clearTimeout(timer);
|
|
179
|
-
timer = window.setTimeout(() => void hydrate(), 120);
|
|
180
|
-
});
|
|
181
|
-
observer.observe(container, { childList: true });
|
|
182
|
-
|
|
183
|
-
// Diagrams bake the palette into their SVG, so every theme write must
|
|
184
|
-
// re-render them. The trigger is the `data-theme` attribute itself rather
|
|
185
|
-
// than the `omp:theme-changed` event: the navbar toggle and the settings
|
|
186
|
-
// modal are both writers, and observing the attribute catches all of them
|
|
187
|
-
// the moment the document changes. Vacuously cheap for a same-palette write
|
|
188
|
-
// (blocks already matching the new theme key are skipped).
|
|
189
|
-
const rehydrateForTheme = async () => {
|
|
190
|
-
await hydrate();
|
|
191
|
-
const block = diagramBlockRef.current;
|
|
192
|
-
// A timeline re-render can replace the block under an open viewer; keep
|
|
193
|
-
// the last snapshot then rather than closing the viewer on the user.
|
|
194
|
-
if (!block?.isConnected) return;
|
|
195
|
-
const snapshot = readDiagramBlock(block);
|
|
196
|
-
if (snapshot) setDiagram(snapshot);
|
|
197
|
-
};
|
|
198
|
-
const themeObserver = new MutationObserver(() => void rehydrateForTheme());
|
|
199
|
-
themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
|
|
217
|
+
// Diagrams bake the palette into their SVG, so a theme write re-renders them —
|
|
218
|
+
// through the block the open viewer was read from, which the hook hands back
|
|
219
|
+
// when that block survived the re-render.
|
|
220
|
+
const onDiagramBlock = useCallback((block: HTMLElement) => {
|
|
221
|
+
const snapshot = readDiagramBlock(block);
|
|
222
|
+
if (snapshot) setDiagram(snapshot);
|
|
223
|
+
}, []);
|
|
224
|
+
useMermaidHydration({
|
|
225
|
+
container: containerRef,
|
|
226
|
+
html,
|
|
227
|
+
active: hasMermaid,
|
|
228
|
+
diagramBlock: diagramBlockRef,
|
|
229
|
+
onDiagramBlock,
|
|
230
|
+
});
|
|
200
231
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
232
|
+
// Read through a ref so the single delegated handler never has to be rebuilt
|
|
233
|
+
// for a new bridge object — and so it always sees the CURRENT mode.
|
|
234
|
+
const reviewRef = useRef(review);
|
|
235
|
+
reviewRef.current = review;
|
|
236
|
+
const byKeyRef = useRef(byKey);
|
|
237
|
+
byKeyRef.current = byKey;
|
|
207
238
|
|
|
208
239
|
const handleContainerClick = useCallback((e: TargetedMouseEvent<HTMLDivElement>) => {
|
|
240
|
+
const bridge = reviewRef.current;
|
|
241
|
+
if (bridge?.active) {
|
|
242
|
+
// In review mode a click names a BLOCK. Consumed before the copy button,
|
|
243
|
+
// the diagram and the document links, because otherwise the reader could
|
|
244
|
+
// not select a block that happens to contain one of them.
|
|
245
|
+
const block = (e.target as HTMLElement).closest<HTMLElement>('[data-md-block]');
|
|
246
|
+
const key = block?.dataset.mdBlock;
|
|
247
|
+
const picked = key ? byKeyRef.current.get(key) : undefined;
|
|
248
|
+
if (picked) {
|
|
249
|
+
e.preventDefault();
|
|
250
|
+
e.stopPropagation();
|
|
251
|
+
bridge.onPick(picked, block!.getBoundingClientRect());
|
|
252
|
+
}
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
|
|
209
256
|
const target = (e.target as HTMLElement).closest<HTMLButtonElement>('button.code-copy-float');
|
|
210
257
|
if (target) {
|
|
211
258
|
const text = target.dataset.copy ?? '';
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The portalled menu panel.
|
|
8
|
+
*
|
|
9
|
+
* The regression this pins is the reported one: a `z-50` panel rendered INSIDE
|
|
10
|
+
* its trigger's subtree is still confined to that ancestor's stacking context,
|
|
11
|
+
* so a `sticky z-10` workspace header covered its own `⋯` menu with the NEXT
|
|
12
|
+
* header's surface, and a `z-50` panel inside the navbar's `z-20` header lost to
|
|
13
|
+
* anything at `z-20`+ in the chat column. The fix is structural — the panel is a
|
|
14
|
+
* child of `document.body` — so the test asserts the STRUCTURE, because a test
|
|
15
|
+
* that only asserted the menu's rows would stay green with the bug restored.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
19
|
+
import { Window } from 'happy-dom';
|
|
20
|
+
import { h, render } from 'preact';
|
|
21
|
+
import { act } from 'preact/test-utils';
|
|
22
|
+
import { AnchoredMenu } from '@/client/components/common/anchored-menu';
|
|
23
|
+
import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
|
|
24
|
+
|
|
25
|
+
let win: Window;
|
|
26
|
+
let host: HTMLElement;
|
|
27
|
+
let closed: number;
|
|
28
|
+
|
|
29
|
+
const ANCHOR = { top: 100, right: 300, bottom: 122, left: 278 };
|
|
30
|
+
|
|
31
|
+
function mount(anchor = ANCHOR) {
|
|
32
|
+
closed = 0;
|
|
33
|
+
win.document.body.innerHTML = '';
|
|
34
|
+
host = win.document.createElement('div') as unknown as HTMLElement;
|
|
35
|
+
// The host stands in for the sidebar's `sticky z-10` header: it is a stacking
|
|
36
|
+
// context, so a panel rendered inside it can never paint over a sibling.
|
|
37
|
+
host.style.position = 'relative';
|
|
38
|
+
host.style.zIndex = '10';
|
|
39
|
+
win.document.body.appendChild(host as never);
|
|
40
|
+
act(() => {
|
|
41
|
+
render(
|
|
42
|
+
h(AnchoredMenu, {
|
|
43
|
+
anchor,
|
|
44
|
+
ariaLabel: 'Options for kabmo',
|
|
45
|
+
className: 'w-44 rounded-md bg-paper',
|
|
46
|
+
onClose: () => {
|
|
47
|
+
closed += 1;
|
|
48
|
+
},
|
|
49
|
+
children: h('button', { type: 'button' }, 'Pin Workspace'),
|
|
50
|
+
}),
|
|
51
|
+
host as never,
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The panel is labelled, not roled: see `AnchoredMenuProps.role`. */
|
|
57
|
+
const panel = () => win.document.querySelector('[aria-label="Options for kabmo"]') as HTMLElement | null;
|
|
58
|
+
|
|
59
|
+
beforeEach(() => {
|
|
60
|
+
win = new Window({ url: 'http://127.0.0.1:3001/' });
|
|
61
|
+
installDomGlobals(win);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
afterEach(() => {
|
|
65
|
+
if (host) act(() => render(null, host as never));
|
|
66
|
+
restoreDomGlobals();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('AnchoredMenu', () => {
|
|
70
|
+
test('the panel is a child of the document, never of its stacking ancestor', () => {
|
|
71
|
+
mount();
|
|
72
|
+
const menu = panel();
|
|
73
|
+
expect(menu).not.toBeNull();
|
|
74
|
+
// The bug, stated as an assertion: inside `host` it would be clipped by the
|
|
75
|
+
// NEXT sticky header and by the sidebar's own scroller.
|
|
76
|
+
expect(host.contains(menu as never)).toBe(false);
|
|
77
|
+
expect(win.document.body.contains(menu as never)).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('it is placed from the anchor, right edges aligned', () => {
|
|
81
|
+
mount();
|
|
82
|
+
const menu = panel();
|
|
83
|
+
// The panel is `fixed` by class (happy-dom computes no layout, so the box is
|
|
84
|
+
// 0×0 and the arithmetic is covered by `menu-anchor.test.ts`); what this
|
|
85
|
+
// asserts is that the placement is applied HERE rather than by the caller's
|
|
86
|
+
// CSS, which is what lets it escape the ancestor's clipping.
|
|
87
|
+
expect(menu?.className).toContain('fixed');
|
|
88
|
+
expect(menu?.style.top).not.toBe('');
|
|
89
|
+
expect(menu?.style.left).not.toBe('');
|
|
90
|
+
expect(menu?.style.maxHeight).not.toBe('');
|
|
91
|
+
// Not the pre-measure placeholder, which is hidden until the layout effect
|
|
92
|
+
// has run.
|
|
93
|
+
expect(menu?.style.visibility).toBe('');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('Escape closes it', () => {
|
|
97
|
+
mount();
|
|
98
|
+
act(() => {
|
|
99
|
+
win.document.dispatchEvent(new win.KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
|
|
100
|
+
});
|
|
101
|
+
expect(closed).toBe(1);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('a click on the backdrop closes it, a click on a row does not', () => {
|
|
105
|
+
mount();
|
|
106
|
+
const menu = panel() as HTMLElement;
|
|
107
|
+
const row = menu.querySelector('button') as HTMLButtonElement;
|
|
108
|
+
|
|
109
|
+
act(() => {
|
|
110
|
+
row.dispatchEvent(new win.MouseEvent('click', { bubbles: true }) as unknown as Event);
|
|
111
|
+
});
|
|
112
|
+
expect(closed).toBe(0);
|
|
113
|
+
|
|
114
|
+
act(() => {
|
|
115
|
+
(menu.parentElement as HTMLElement).dispatchEvent(new win.MouseEvent('click', { bubbles: true }) as unknown as Event);
|
|
116
|
+
});
|
|
117
|
+
expect(closed).toBe(1);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test('no anchor and no viewport style renders nothing', () => {
|
|
121
|
+
closed = 0;
|
|
122
|
+
win.document.body.innerHTML = '';
|
|
123
|
+
const bare = win.document.createElement('div');
|
|
124
|
+
win.document.body.appendChild(bare as never);
|
|
125
|
+
act(() => {
|
|
126
|
+
render(
|
|
127
|
+
h(AnchoredMenu, {
|
|
128
|
+
anchor: null,
|
|
129
|
+
ariaLabel: 'closed',
|
|
130
|
+
className: 'w-44',
|
|
131
|
+
onClose: () => {
|
|
132
|
+
closed += 1;
|
|
133
|
+
},
|
|
134
|
+
children: null,
|
|
135
|
+
}),
|
|
136
|
+
bare as never,
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
expect(panel()).toBeNull();
|
|
140
|
+
});
|
|
141
|
+
});
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A menu that hangs off a trigger, portalled to `document.body`.
|
|
8
|
+
*
|
|
9
|
+
* Two defects make this its own module rather than a class on each call site:
|
|
10
|
+
*
|
|
11
|
+
* - **A dropdown inside a `z-index`ed ancestor cannot paint over its siblings.**
|
|
12
|
+
* A positioned or flex item with a `z-index` establishes a stacking context,
|
|
13
|
+
* so a `z-50` menu inside the sidebar's `sticky z-10` workspace header was
|
|
14
|
+
* still BELOW the next workspace's `sticky z-10` header — the reported "menu
|
|
15
|
+
* appears, then disappears under the folder below it". Measured before this
|
|
16
|
+
* module: `elementFromPoint` at the menu's own centre answered the next
|
|
17
|
+
* header's button. The desktop navbar had the same shape one level up: the
|
|
18
|
+
* `header` is a flex item at `z-20`, so the server panel's `z-50` lost to
|
|
19
|
+
* anything at `z-20`+ in the chat column.
|
|
20
|
+
* - **`overflow` clips a menu, `fixed` or not.** A menu drawn inside the
|
|
21
|
+
* sidebar's scroller is cut at the scroller's edge and drifts with the rows
|
|
22
|
+
* when it scrolls (`toolbar-row/OverflowMenu` documents the same bug from the
|
|
23
|
+
* other direction).
|
|
24
|
+
*
|
|
25
|
+
* So the panel is a `fixed` node in `document.body` at `z-[70]` — above the
|
|
26
|
+
* navbar (`z-20`), the panels (`z-50`) and the modals (`z-[60]`), below toasts
|
|
27
|
+
* (`z-[200]`) — placed from the trigger's measured rectangle (`placeMenu`) and
|
|
28
|
+
* measured again whenever its own content changes height.
|
|
29
|
+
*
|
|
30
|
+
* The dismissal surface is a full-viewport layer at the same layer, which is
|
|
31
|
+
* also what makes the grid behind it inert: a click outside closes the menu,
|
|
32
|
+
* and the wheel cannot scroll the list out from under it.
|
|
33
|
+
*
|
|
34
|
+
* The phone's server sheet passes `viewportStyle` instead of `anchor`: it is
|
|
35
|
+
* pinned below the title bar rather than to a trigger sitting near the edge
|
|
36
|
+
* (`shared/lib/ui/mobile-overlay` owns those insets).
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
import { useEffect, useLayoutEffect, useRef, useState } from 'preact/hooks';
|
|
40
|
+
import type { ComponentChildren } from 'preact';
|
|
41
|
+
import type { CSSProperties } from 'preact/compat';
|
|
42
|
+
import { createPortal } from 'preact/compat';
|
|
43
|
+
import { placeMenu, type AnchorRect, type MenuAlign, type MenuPlacement } from '@/shared/lib/ui/menu-anchor';
|
|
44
|
+
import type { MobileOverlayStyle } from '@/shared/lib/ui/mobile-overlay';
|
|
45
|
+
|
|
46
|
+
export interface AnchoredMenuProps {
|
|
47
|
+
/** The trigger's viewport rectangle, or null while the menu is closed. */
|
|
48
|
+
anchor: AnchorRect | null;
|
|
49
|
+
align?: MenuAlign;
|
|
50
|
+
/** Pin to the viewport (a phone sheet) instead of hanging off the anchor. */
|
|
51
|
+
viewportStyle?: MobileOverlayStyle;
|
|
52
|
+
/**
|
|
53
|
+
* `dialog` for the server sheet — a panel with headings and an Add form.
|
|
54
|
+
* Omitted for the folder menus: their rows are plain buttons (as they were
|
|
55
|
+
* before the portal), and claiming `menu` would promise arrow-key roving that
|
|
56
|
+
* no one implements.
|
|
57
|
+
*/
|
|
58
|
+
role?: 'dialog';
|
|
59
|
+
/** Names the panel; every caller labels it by the trigger it opened from. */
|
|
60
|
+
ariaLabel?: string;
|
|
61
|
+
/** The panel's own chrome: width, padding, border, typography. */
|
|
62
|
+
className: string;
|
|
63
|
+
/** Close on Escape, on a click outside, and on a context menu. */
|
|
64
|
+
onClose: () => void;
|
|
65
|
+
children: ComponentChildren;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function AnchoredMenu({
|
|
69
|
+
anchor,
|
|
70
|
+
align = 'end',
|
|
71
|
+
viewportStyle,
|
|
72
|
+
role,
|
|
73
|
+
ariaLabel,
|
|
74
|
+
className,
|
|
75
|
+
onClose,
|
|
76
|
+
children,
|
|
77
|
+
}: AnchoredMenuProps) {
|
|
78
|
+
const panelRef = useRef<HTMLDivElement>(null);
|
|
79
|
+
const [placement, setPlacement] = useState<MenuPlacement | null>(null);
|
|
80
|
+
|
|
81
|
+
// Measured before paint, and re-measured on every render: the folder menu
|
|
82
|
+
// swaps its rows for the delete confirmation, which changes its height, and a
|
|
83
|
+
// placement computed for the shorter menu put the longer one past the window
|
|
84
|
+
// edge. The state is only written when a number actually moved, so this
|
|
85
|
+
// settles in one extra render instead of looping.
|
|
86
|
+
useLayoutEffect(() => {
|
|
87
|
+
if (viewportStyle) return;
|
|
88
|
+
if (!anchor || !panelRef.current) return;
|
|
89
|
+
const box = panelRef.current.getBoundingClientRect();
|
|
90
|
+
const next = placeMenu(
|
|
91
|
+
anchor,
|
|
92
|
+
{ width: box.width, height: box.height },
|
|
93
|
+
{ width: window.innerWidth, height: window.innerHeight },
|
|
94
|
+
align,
|
|
95
|
+
);
|
|
96
|
+
setPlacement((prev) =>
|
|
97
|
+
prev && prev.top === next.top && prev.left === next.left && prev.maxHeight === next.maxHeight ? prev : next,
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
const onKeyDown = (event: globalThis.KeyboardEvent) => {
|
|
103
|
+
if (event.key !== 'Escape') return;
|
|
104
|
+
event.preventDefault();
|
|
105
|
+
onClose();
|
|
106
|
+
};
|
|
107
|
+
document.addEventListener('keydown', onKeyDown);
|
|
108
|
+
return () => document.removeEventListener('keydown', onKeyDown);
|
|
109
|
+
}, [onClose]);
|
|
110
|
+
|
|
111
|
+
if (!anchor && !viewportStyle) return null;
|
|
112
|
+
|
|
113
|
+
const style: CSSProperties = viewportStyle
|
|
114
|
+
? { ...viewportStyle }
|
|
115
|
+
: placement
|
|
116
|
+
? { top: `${placement.top}px`, left: `${placement.left}px`, maxHeight: `${placement.maxHeight}px` }
|
|
117
|
+
: // The single pre-measure render. Hidden rather than drawn at 0,0, so a
|
|
118
|
+
// menu never flashes in the corner before the layout effect settles it.
|
|
119
|
+
{ top: '0px', left: '0px', visibility: 'hidden' };
|
|
120
|
+
|
|
121
|
+
return createPortal(
|
|
122
|
+
<div
|
|
123
|
+
className="fixed inset-0 z-[70]"
|
|
124
|
+
onClick={onClose}
|
|
125
|
+
onContextMenu={(event) => {
|
|
126
|
+
event.preventDefault();
|
|
127
|
+
onClose();
|
|
128
|
+
}}
|
|
129
|
+
>
|
|
130
|
+
<div
|
|
131
|
+
ref={panelRef}
|
|
132
|
+
role={role}
|
|
133
|
+
aria-label={ariaLabel}
|
|
134
|
+
className={`fixed z-[70] overflow-y-auto overscroll-contain ${className}`}
|
|
135
|
+
style={style}
|
|
136
|
+
onClick={(event) => event.stopPropagation()}
|
|
137
|
+
>
|
|
138
|
+
{children}
|
|
139
|
+
</div>
|
|
140
|
+
</div>,
|
|
141
|
+
document.body,
|
|
142
|
+
);
|
|
143
|
+
}
|