@firstpick/pi-package-webui 0.11.0 → 0.11.2
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/DEVELOPMENT.md +181 -12
- package/README.md +41 -9
- package/TECHNICAL.md +91 -8
- package/bin/pi-webui.mjs +5432 -11428
- package/index.ts +10 -3
- package/lib/codex-cli-usage.mjs +232 -0
- package/lib/codex-usage-auth.mjs +39 -3
- package/lib/codex-usage-source.mjs +36 -0
- package/lib/component-update-state.mjs +2 -2
- package/lib/git-workflow-preference-values.mjs +122 -0
- package/lib/git-workflow-preferences.mjs +35 -563
- package/lib/package-updates.mjs +260 -0
- package/lib/rpc-supervisor-client.mjs +8 -4
- package/lib/server/app-runners.mjs +1667 -0
- package/lib/server/codex-fast-mode.mjs +161 -0
- package/lib/server/conversation-mode.mjs +218 -0
- package/lib/server/conversation-voice.mjs +318 -0
- package/lib/server/extension-ui-requests.mjs +114 -0
- package/lib/server/git-generation.mjs +459 -0
- package/lib/server/git-mutations.mjs +1827 -0
- package/lib/server/git-reads.mjs +608 -0
- package/lib/server/git-worktree-sessions.mjs +408 -0
- package/lib/server/helper-requests.mjs +82 -0
- package/lib/server/no-project-workspaces.mjs +103 -0
- package/lib/server/rpc-transports.mjs +394 -0
- package/lib/server/settings-store.mjs +544 -0
- package/lib/server/sse-delivery.mjs +138 -0
- package/lib/server/workspace-files.mjs +563 -0
- package/lib/update/supervisor.mjs +4 -0
- package/lib/webui-workspaces.mjs +2 -0
- package/package.json +5 -5
- package/public/activity-agents-controller.mjs +269 -0
- package/public/activity-events-controller.mjs +236 -0
- package/public/activity-intercom-controller.mjs +627 -0
- package/public/activity-queue-controller.mjs +534 -0
- package/public/activity-todo-view.mjs +89 -0
- package/public/activity-usage-controller.mjs +589 -0
- package/public/app.js +3761 -3878
- package/public/conversation-overview.css +84 -0
- package/public/conversation-overview.mjs +193 -0
- package/public/draft-conversations.mjs +135 -0
- package/public/focused-composer-controls.css +111 -0
- package/public/focused-composer-controls.mjs +143 -0
- package/public/focused-composer-disclosure.mjs +223 -0
- package/public/focused-shell-adapter.css +264 -0
- package/public/focused-shell-adapter.mjs +257 -0
- package/public/index.html +137 -80
- package/public/model-catalog-order.mjs +63 -0
- package/public/platform-event-stream.mjs +64 -0
- package/public/project-artifact-controller.mjs +218 -0
- package/public/project-file-viewer-controller.mjs +383 -0
- package/public/project-git-tools-controller.mjs +181 -0
- package/public/project-runners-controller.mjs +220 -0
- package/public/service-worker.js +52 -1
- package/public/session-attention-scheduler.mjs +182 -0
- package/public/session-composer-store.mjs +56 -0
- package/public/session-context.mjs +51 -0
- package/public/session-extension-requests.mjs +63 -0
- package/public/session-shelf-state.mjs +151 -0
- package/public/session-shelves.css +171 -0
- package/public/session-shelves.mjs +95 -0
- package/public/settings-navigation-controller.mjs +249 -0
- package/public/settings-navigation.css +61 -0
- package/public/settings-resource-controller.mjs +232 -0
- package/public/settings-workspace.css +19 -0
- package/public/settings-workspace.mjs +175 -0
- package/public/shell-actions.mjs +87 -0
- package/public/shell-attention-inbox.css +72 -0
- package/public/shell-attention-inbox.mjs +198 -0
- package/public/shell-context-actions.css +61 -0
- package/public/shell-context-actions.mjs +168 -0
- package/public/shell-inspector-resize.mjs +124 -0
- package/public/shell-navigation-resize.mjs +107 -0
- package/public/shell-navigation.mjs +127 -0
- package/public/shell-preferences.mjs +108 -0
- package/public/shell-surfaces.mjs +99 -0
- package/public/shell-tools-launcher.css +97 -0
- package/public/shell-tools-launcher.mjs +222 -0
- package/public/shell-view-host.mjs +115 -0
- package/public/shell-workspace.css +266 -0
- package/public/shell-workspace.mjs +402 -0
- package/public/sidebar-update-menu.css +190 -0
- package/public/sidebar-update-menu.mjs +512 -0
- package/public/styles.css +320 -169
- package/public/subagent-launch-slot-state.mjs +2 -2
- package/public/theme-contract.mjs +1 -1
- package/public/themed-selects.css +156 -0
- package/public/workspace-choices.mjs +104 -0
- package/tests/activity-agents-controller.test.mjs +652 -0
- package/tests/activity-events-controller.test.mjs +511 -0
- package/tests/app-runner-terminal-static.test.mjs +3 -3
- package/tests/append-system-http.test.mjs +7 -6
- package/tests/append-system-selector-static.test.mjs +6 -6
- package/tests/aur-review-widget-static.test.mjs +11 -3
- package/tests/boot-failure-diagnostics.test.mjs +8 -3
- package/tests/browser/activity-intercom-controller.spec.mjs +457 -0
- package/tests/browser/activity-navigation-integration.spec.mjs +45 -0
- package/tests/browser/activity-owner-integration.spec.mjs +201 -0
- package/tests/browser/activity-queue-controller.spec.mjs +405 -0
- package/tests/browser/activity-section-picker.spec.mjs +294 -0
- package/tests/browser/activity-todo-view.spec.mjs +60 -0
- package/tests/browser/activity-usage-controller.spec.mjs +416 -0
- package/tests/browser/compact-layout-integration.spec.mjs +58 -0
- package/tests/browser/composer-action-grid-reorder.spec.mjs +16 -0
- package/tests/browser/composer-disclosure.spec.mjs +389 -0
- package/tests/browser/composer-toolbar-polish.spec.mjs +343 -0
- package/tests/browser/control-deck-component-updates.spec.mjs +5 -14
- package/tests/browser/control-deck-side-panels.spec.mjs +27 -12
- package/tests/browser/control-visibility.spec.mjs +5 -0
- package/tests/browser/controls-layout.spec.mjs +21 -2
- package/tests/browser/conversation-overview.spec.mjs +251 -0
- package/tests/browser/draft-conversations.spec.mjs +276 -0
- package/tests/browser/empty-startup-fixture.mjs +135 -0
- package/tests/browser/empty-startup.spec.mjs +282 -0
- package/tests/browser/events-tree-context-menu.spec.mjs +1 -0
- package/tests/browser/focused-composer-more.spec.mjs +127 -0
- package/tests/browser/focused-model-shortcuts.spec.mjs +120 -0
- package/tests/browser/focused-navigation-fixture.mjs +32 -0
- package/tests/browser/focused-project-activity.spec.mjs +111 -0
- package/tests/browser/focused-shell-integration.spec.mjs +208 -0
- package/tests/browser/folder-picker-layout-fixture.mjs +13 -0
- package/tests/browser/folder-picker-layout.spec.mjs +302 -0
- package/tests/browser/footer-model-advanced-layout.spec.mjs +27 -14
- package/tests/browser/git-setup-cancel.spec.mjs +251 -0
- package/tests/browser/guided-git-fallback.spec.mjs +40 -21
- package/tests/browser/inspector-header.spec.mjs +221 -0
- package/tests/browser/inspector-overview-integration.spec.mjs +104 -0
- package/tests/browser/inspector-resize.spec.mjs +227 -0
- package/tests/browser/inspector-topic-overflow.spec.mjs +56 -0
- package/tests/browser/interaction-action-discovery.spec.mjs +332 -0
- package/tests/browser/interaction-attention-inbox.spec.mjs +364 -0
- package/tests/browser/interaction-attention-integration.spec.mjs +53 -0
- package/tests/browser/interaction-composer-controls.spec.mjs +267 -0
- package/tests/browser/interaction-composer-integration.spec.mjs +175 -0
- package/tests/browser/interaction-continuity.spec.mjs +50 -26
- package/tests/browser/interaction-model-picker.spec.mjs +86 -0
- package/tests/browser/last-terminal-close.spec.mjs +280 -0
- package/tests/browser/mobile-foundation.spec.mjs +172 -20
- package/tests/browser/model-catalog-groups.spec.mjs +104 -0
- package/tests/browser/more-row-menus.spec.mjs +258 -0
- package/tests/browser/new-tab-hit-target.spec.mjs +228 -0
- package/tests/browser/no-project-conversation.spec.mjs +113 -0
- package/tests/browser/optimistic-prompt.spec.mjs +3 -0
- package/tests/browser/overhaul-w7-accessibility.spec.mjs +227 -0
- package/tests/browser/overhaul-w7-capabilities.spec.mjs +298 -0
- package/tests/browser/overhaul-w7-companions.spec.mjs +290 -0
- package/tests/browser/overhaul-w7-focus-repairs.spec.mjs +210 -0
- package/tests/browser/overhaul-w7-layout-repairs.spec.mjs +183 -0
- package/tests/browser/overhaul-w7-parent-repairs.spec.mjs +97 -0
- package/tests/browser/overhaul-w7-pwa.spec.mjs +247 -0
- package/tests/browser/overhaul-w7-responsive.spec.mjs +450 -0
- package/tests/browser/overhaul-w7-update.spec.mjs +215 -0
- package/tests/browser/overhaul-w7-zoom.spec.mjs +243 -0
- package/tests/browser/overview-agent-ownership.spec.mjs +45 -0
- package/tests/browser/persistent-ui-layout.spec.mjs +3 -0
- package/tests/browser/project-artifact-controller.spec.mjs +324 -0
- package/tests/browser/project-artifact-integration.spec.mjs +103 -0
- package/tests/browser/project-file-buffer-integration.spec.mjs +230 -0
- package/tests/browser/project-git-tools-integration.spec.mjs +369 -0
- package/tests/browser/session-fixture.mjs +118 -0
- package/tests/browser/session-ownership.spec.mjs +175 -0
- package/tests/browser/session-shelves-integration.spec.mjs +219 -0
- package/tests/browser/session-shelves.spec.mjs +340 -0
- package/tests/browser/session-sidebar-resize.spec.mjs +138 -0
- package/tests/browser/session-summary.spec.mjs +17 -12
- package/tests/browser/settings-complete-home.spec.mjs +463 -0
- package/tests/browser/settings-entrypoint.spec.mjs +104 -0
- package/tests/browser/settings-navigation-controller.spec.mjs +383 -0
- package/tests/browser/settings-navigation-integration.spec.mjs +348 -0
- package/tests/browser/settings-resource-lifetime.spec.mjs +349 -0
- package/tests/browser/settings-workspace-integration.spec.mjs +132 -0
- package/tests/browser/shell-ownership.spec.mjs +398 -0
- package/tests/browser/shell-rollout-fixture.mjs +259 -0
- package/tests/browser/shell-rollout.spec.mjs +357 -0
- package/tests/browser/shell-tools-launcher.spec.mjs +260 -0
- package/tests/browser/shell-workspace.spec.mjs +433 -0
- package/tests/browser/side-panel-section-reorder.spec.mjs +3 -1
- package/tests/browser/sidebar-floating-tooltips.spec.mjs +37 -0
- package/tests/browser/sidebar-update-integration.spec.mjs +277 -0
- package/tests/browser/sidebar-update-menu.spec.mjs +402 -0
- package/tests/browser/sidebar-update-progress.spec.mjs +472 -0
- package/tests/browser/stream-output-isolation.spec.mjs +6 -3
- package/tests/browser/themed-selects.spec.mjs +297 -0
- package/tests/browser/thread-start-flow.spec.mjs +481 -0
- package/tests/browser/ui-polish-activity-controls.spec.mjs +177 -0
- package/tests/browser/ui-polish-features.spec.mjs +85 -0
- package/tests/browser/ui-polish-file-header.spec.mjs +43 -0
- package/tests/browser/ui-polish-local-git.spec.mjs +183 -0
- package/tests/browser/ui-polish-mobile-recovery.spec.mjs +36 -0
- package/tests/browser/ui-polish-navigation.spec.mjs +127 -0
- package/tests/browser/ui-polish-session-actions.spec.mjs +282 -0
- package/tests/browser/user-noticeable-fixes.spec.mjs +10 -3
- package/tests/browser/windows-drive-parent.spec.mjs +4 -2
- package/tests/browser/workspace-draft-selection.spec.mjs +266 -0
- package/tests/codex-cli-usage.test.mjs +189 -0
- package/tests/codex-fast-mode-static.test.mjs +16 -11
- package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
- package/tests/codex-usage-auth.test.mjs +168 -9
- package/tests/codex-usage-http.test.mjs +124 -0
- package/tests/codex-usage-source.test.mjs +146 -0
- package/tests/component-update-api-static.test.mjs +2 -2
- package/tests/composer-action-grid-reorder-static.test.mjs +3 -3
- package/tests/control-deck-component-updates-static.test.mjs +9 -4
- package/tests/control-visibility-static.test.mjs +3 -3
- package/tests/controls-layout-static.test.mjs +5 -5
- package/tests/conversation-overview-adapter.test.mjs +100 -0
- package/tests/document-artifact-static.test.mjs +2 -1
- package/tests/draft-conversations.test.mjs +205 -0
- package/tests/empty-startup-launcher.test.mjs +197 -0
- package/tests/events-tree-context-menu-static.test.mjs +22 -18
- package/tests/fake-pi-session-ownership.test.mjs +302 -0
- package/tests/fast-mode-client-static.test.mjs +1 -1
- package/tests/feature-decision-output-popup-static.test.mjs +2 -2
- package/tests/feature-system-prompt-optional-feature-static.test.mjs +1 -1
- package/tests/file-viewer-image-static.test.mjs +15 -11
- package/tests/file-viewer-search-static.test.mjs +5 -5
- package/tests/fixtures/fake-codex-app-server.mjs +61 -0
- package/tests/fixtures/fake-pi.mjs +41 -9
- package/tests/footer-provider-usage-visual-static.test.mjs +7 -6
- package/tests/foreground-transcript-catch-up-static.test.mjs +7 -4
- package/tests/git-footer-context-menu-static.test.mjs +1 -1
- package/tests/git-panel-file-preview-static.test.mjs +41 -48
- package/tests/git-panel-live-updates-static.test.mjs +2 -1
- package/tests/git-workflow-dispatch.test.mjs +66 -0
- package/tests/git-workflow-preferences.test.mjs +9 -1
- package/tests/guided-git-activation-static.test.mjs +14 -11
- package/tests/guided-git-fallback-ui-static.test.mjs +8 -5
- package/tests/guided-git-message-reliability-static.test.mjs +10 -6
- package/tests/guided-git-publication-backend-static.test.mjs +16 -11
- package/tests/http-endpoints-harness.test.mjs +29 -9
- package/tests/interaction-state-stability-static.test.mjs +4 -3
- package/tests/intercom-conversation-viewer-static.test.mjs +29 -17
- package/tests/intercom-tag-layout-static.test.mjs +13 -12
- package/tests/intercom-tag-overflow-layout.test.mjs +11 -10
- package/tests/main-output-loading-static.test.mjs +4 -4
- package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
- package/tests/mobile-compact-layout.test.mjs +2 -2
- package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
- package/tests/mobile-foundation-static.test.mjs +10 -7
- package/tests/mobile-phone-experience-static.test.mjs +3 -2
- package/tests/mobile-static.test.mjs +173 -135
- package/tests/model-catalog-order.test.mjs +103 -0
- package/tests/native-parity.test.mjs +6 -4
- package/tests/native-session-choice-owner.test.mjs +102 -0
- package/tests/native-update-lifecycle.test.mjs +10 -5
- package/tests/no-project-http-harness.test.mjs +328 -0
- package/tests/no-project-navigation.test.mjs +54 -0
- package/tests/no-project-workspaces.test.mjs +77 -0
- package/tests/open-issue-wizard-static.test.mjs +1 -1
- package/tests/optional-feature-migration-frontend.test.mjs +4 -4
- package/tests/optional-feature-prompt-impact-static.test.mjs +3 -3
- package/tests/package-updates.test.mjs +630 -0
- package/tests/persistent-ui-layout-static.test.mjs +3 -3
- package/tests/platform-event-stream.test.mjs +253 -0
- package/tests/project-file-viewer-controller.test.mjs +652 -0
- package/tests/project-git-tools-adapter.test.mjs +103 -0
- package/tests/project-git-tools-controller.test.mjs +577 -0
- package/tests/project-runners-controller.test.mjs +453 -0
- package/tests/prompt-input-vertical-scroll-static.test.mjs +3 -3
- package/tests/prompt-routing-input-clear-static.test.mjs +2 -2
- package/tests/questionnaire-dialog.test.mjs +6 -6
- package/tests/queue-edit-reorder-static.test.mjs +48 -51
- package/tests/rpc-supervisor-host.test.mjs +4 -1
- package/tests/safety-guard-auto-review-static.test.mjs +1 -1
- package/tests/sampling-native-controls-user-flow.test.mjs +7 -1
- package/tests/server-app-runners.test.mjs +536 -0
- package/tests/server-codex-fast-mode.test.mjs +346 -0
- package/tests/server-conversation-mode.test.mjs +386 -0
- package/tests/server-conversation-voice.test.mjs +426 -0
- package/tests/server-extension-ui-requests.test.mjs +357 -0
- package/tests/server-git-generation.test.mjs +558 -0
- package/tests/server-git-mutations.test.mjs +729 -0
- package/tests/server-git-reads.test.mjs +381 -0
- package/tests/server-git-worktree-sessions.test.mjs +502 -0
- package/tests/server-helper-requests.test.mjs +364 -0
- package/tests/server-rpc-transports.test.mjs +553 -0
- package/tests/server-settings-store.test.mjs +132 -0
- package/tests/server-shell-rollout.test.mjs +478 -0
- package/tests/server-sse-delivery.test.mjs +410 -0
- package/tests/server-workspace-files.test.mjs +546 -0
- package/tests/session-attention-scheduler.test.mjs +514 -0
- package/tests/session-composer-store.test.mjs +153 -0
- package/tests/session-context.test.mjs +134 -0
- package/tests/session-extension-requests.test.mjs +193 -0
- package/tests/session-ownership-adapter.test.mjs +130 -0
- package/tests/session-sampling-ui-static.test.mjs +10 -4
- package/tests/session-send-lifetime.test.mjs +140 -0
- package/tests/session-shelf-state.test.mjs +348 -0
- package/tests/session-shelves-adapter.test.mjs +79 -0
- package/tests/session-summary-lifetime.test.mjs +54 -0
- package/tests/settings-resource-controller.test.mjs +548 -0
- package/tests/shell-action-adapter.test.mjs +169 -0
- package/tests/shell-actions.test.mjs +182 -0
- package/tests/shell-navigation.test.mjs +348 -0
- package/tests/shell-preferences.test.mjs +367 -0
- package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
- package/tests/stream-output-isolation-static.test.mjs +1 -1
- package/tests/subagent-gate-visibility.test.mjs +1 -1
- package/tests/subagent-launch-slot-state.test.mjs +2 -2
- package/tests/subagent-launch-slots-static.test.mjs +1 -1
- package/tests/subagent-observability-static.test.mjs +2 -2
- package/tests/subagent-reliability-integration.test.mjs +13 -30
- package/tests/syntax-highlighting-static.test.mjs +3 -3
- package/tests/tab-close-continuity-static.test.mjs +1 -1
- package/tests/tab-metadata-ordering.test.mjs +99 -0
- package/tests/terminal-cwd-grouping-static.test.mjs +2 -2
- package/tests/terminal-tab-prompt-tooltip-static.test.mjs +2 -0
- package/tests/terminal-tab-workspace-static.test.mjs +5 -4
- package/tests/terminal-tabs-resize-static.test.mjs +1 -1
- package/tests/theme-contract.test.mjs +10 -3
- package/tests/theme-customizer-static.test.mjs +1 -1
- package/tests/thread-display-title.test.mjs +44 -0
- package/tests/update-api-harness.test.mjs +15 -14
- package/tests/update-tab-restore-harness.test.mjs +3 -0
- package/tests/webui-settings-locking.test.mjs +2 -2
- package/tests/windows-drive-cwd-picker-static.test.mjs +7 -9
- package/tests/workspace-choices.test.mjs +114 -0
- package/tests/workspace-save-load-static.test.mjs +7 -4
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const FOCUSABLE_SELECTOR = 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
|
2
|
+
const OWNED_ATTRIBUTES = ['role', 'tabindex', 'aria-modal'];
|
|
3
|
+
|
|
4
|
+
/** Coordinates drawer focus only; native modal dialogs retain browser ownership. */
|
|
5
|
+
export function createSurfaceCoordinator({ document, getFocusableElements, onEscape }) {
|
|
6
|
+
let active = null;
|
|
7
|
+
let disposed = false;
|
|
8
|
+
let focusGeneration = 0;
|
|
9
|
+
|
|
10
|
+
function canFocus(element) {
|
|
11
|
+
if (!element?.isConnected || element.closest('[hidden], [inert]') || element.disabled) return false;
|
|
12
|
+
const style = document.defaultView.getComputedStyle(element);
|
|
13
|
+
return style.visibility !== 'hidden' && style.visibility !== 'collapse' && element.getClientRects().length > 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function handleKeydown(event) {
|
|
17
|
+
if (!active || event.defaultPrevented || document.querySelector('dialog:modal') || !canFocus(active.surface)) return;
|
|
18
|
+
if (event.key === 'Escape') {
|
|
19
|
+
const record = active;
|
|
20
|
+
event.preventDefault();
|
|
21
|
+
if (onEscape?.(record.surface, event) === true) {
|
|
22
|
+
event.stopPropagation();
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
record.close?.();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (event.key !== 'Tab') return;
|
|
29
|
+
const { surface } = active;
|
|
30
|
+
const focusable = [...(getFocusableElements ? getFocusableElements(surface) : surface.querySelectorAll(FOCUSABLE_SELECTOR))]
|
|
31
|
+
.filter(element => canFocus(element) && element.tabIndex >= 0);
|
|
32
|
+
if (!focusable.length) {
|
|
33
|
+
event.preventDefault();
|
|
34
|
+
surface.focus({ preventScroll: true });
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const first = focusable[0];
|
|
38
|
+
const last = focusable[focusable.length - 1];
|
|
39
|
+
const focused = document.activeElement;
|
|
40
|
+
if (!focusable.includes(focused)) {
|
|
41
|
+
event.preventDefault();
|
|
42
|
+
(event.shiftKey ? last : first).focus({ preventScroll: true });
|
|
43
|
+
} else if (event.shiftKey && focused === first) {
|
|
44
|
+
event.preventDefault();
|
|
45
|
+
last.focus({ preventScroll: true });
|
|
46
|
+
} else if (!event.shiftKey && focused === last) {
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
first.focus({ preventScroll: true });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function release() {
|
|
53
|
+
const record = active;
|
|
54
|
+
active = null;
|
|
55
|
+
for (const [name, value] of record.attributes) {
|
|
56
|
+
if (value === null) record.surface.removeAttribute(name);
|
|
57
|
+
else record.surface.setAttribute(name, value);
|
|
58
|
+
}
|
|
59
|
+
document.removeEventListener('keydown', handleKeydown, true);
|
|
60
|
+
return record.trigger;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function activate(surface, trigger, { close } = {}) {
|
|
64
|
+
if (disposed) return;
|
|
65
|
+
focusGeneration += 1;
|
|
66
|
+
if (active?.surface === surface) {
|
|
67
|
+
active.trigger = trigger;
|
|
68
|
+
active.close = close;
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (active) release();
|
|
72
|
+
active = { surface, trigger, close, attributes: OWNED_ATTRIBUTES.map((name) => [name, surface.getAttribute(name)]) };
|
|
73
|
+
surface.setAttribute('role', 'dialog');
|
|
74
|
+
surface.setAttribute('aria-modal', 'true');
|
|
75
|
+
surface.setAttribute('tabindex', '-1');
|
|
76
|
+
document.addEventListener('keydown', handleKeydown, true);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function deactivate(surface, { restoreFocus = true } = {}) {
|
|
80
|
+
if (active?.surface !== surface) return;
|
|
81
|
+
const trigger = release();
|
|
82
|
+
const generation = ++focusGeneration;
|
|
83
|
+
if (restoreFocus) {
|
|
84
|
+
queueMicrotask(() => {
|
|
85
|
+
if (generation !== focusGeneration || !canFocus(trigger)) return;
|
|
86
|
+
const modal = document.querySelector('dialog:modal');
|
|
87
|
+
if (!modal || modal.contains(trigger)) trigger.focus({ preventScroll: true });
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function dispose() {
|
|
93
|
+
focusGeneration += 1;
|
|
94
|
+
if (active) release();
|
|
95
|
+
disposed = true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return { activate, deactivate, dispose };
|
|
99
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
#shellToolsLauncher {
|
|
2
|
+
box-sizing: border-box;
|
|
3
|
+
width: min(700px, calc(100% - 24px));
|
|
4
|
+
max-width: none;
|
|
5
|
+
max-height: min(740px, calc(100dvh - 24px));
|
|
6
|
+
padding: 0;
|
|
7
|
+
overflow: hidden;
|
|
8
|
+
color: var(--text);
|
|
9
|
+
background: var(--bg);
|
|
10
|
+
border: 1px solid var(--border);
|
|
11
|
+
border-radius: 10px;
|
|
12
|
+
box-shadow: 0 24px 90px var(--shadow, #0008);
|
|
13
|
+
font: inherit;
|
|
14
|
+
font-size: var(--text-sm, 0.875rem);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#shellToolsLauncher[open] { display: flex; flex-direction: column; }
|
|
18
|
+
#shellToolsLauncher::backdrop { background: #080810aa; backdrop-filter: blur(3px); }
|
|
19
|
+
#shellToolsLauncher * { box-sizing: border-box; min-width: 0; }
|
|
20
|
+
#shellToolsLauncher [hidden] { display: none !important; }
|
|
21
|
+
#shellToolsLauncher button, #shellToolsLauncher input { font: inherit; color: inherit; }
|
|
22
|
+
#shellToolsLauncher button { cursor: pointer; border-radius: 5px; }
|
|
23
|
+
#shellToolsLauncher :is(.tools-launcher-close, .tools-launcher-categories button, .tools-launcher-action, .tools-launcher-pin) {
|
|
24
|
+
box-shadow: none;
|
|
25
|
+
transform: none;
|
|
26
|
+
transition: none;
|
|
27
|
+
}
|
|
28
|
+
#shellToolsLauncher button:disabled { cursor: default; opacity: 0.65; }
|
|
29
|
+
#shellToolsLauncher :is(button, input):focus-visible { outline: 2px solid var(--accent-2, var(--accent)); outline-offset: -2px; }
|
|
30
|
+
|
|
31
|
+
#shellToolsLauncher .tools-launcher-header {
|
|
32
|
+
display: flex;
|
|
33
|
+
align-items: center;
|
|
34
|
+
justify-content: space-between;
|
|
35
|
+
gap: 12px;
|
|
36
|
+
padding: 12px 16px 4px;
|
|
37
|
+
flex-shrink: 0;
|
|
38
|
+
}
|
|
39
|
+
#shellToolsLauncher h2 { margin: 0; font-size: 1rem; font-weight: 600; }
|
|
40
|
+
#shellToolsLauncher .tools-launcher-close { padding: 6px 9px; border: 1px solid var(--border); background: var(--panel); }
|
|
41
|
+
#shellToolsLauncher .tools-launcher-context { margin: 0; padding: 0 16px 8px; color: var(--muted); overflow-wrap: anywhere; flex-shrink: 0; }
|
|
42
|
+
#shellToolsLauncher .tools-launcher-search {
|
|
43
|
+
flex-shrink: 0;
|
|
44
|
+
width: calc(100% - 32px);
|
|
45
|
+
margin: 8px 16px 12px;
|
|
46
|
+
padding: 10px;
|
|
47
|
+
border: 1px solid var(--border);
|
|
48
|
+
border-radius: 6px;
|
|
49
|
+
background: var(--panel);
|
|
50
|
+
}
|
|
51
|
+
#shellToolsLauncher .tools-launcher-search::placeholder { color: var(--muted); }
|
|
52
|
+
#shellToolsLauncher .tools-launcher-run-controls { flex-shrink: 0; max-height: 25dvh; overflow: auto; overflow-wrap: anywhere; }
|
|
53
|
+
#shellToolsLauncher .tools-launcher-run-controls:not(:empty) { padding: 8px 16px; border-top: 1px solid var(--border); }
|
|
54
|
+
#shellToolsLauncher .tools-launcher-body { display: flex; min-height: 0; height: 440px; border-top: 1px solid var(--border); }
|
|
55
|
+
#shellToolsLauncher .tools-launcher-categories { width: 145px; flex-shrink: 0; display: flex; flex-direction: column; gap: 3px; padding: 10px 8px; overflow: auto; border-right: 1px solid var(--border); }
|
|
56
|
+
#shellToolsLauncher .tools-launcher-categories button { flex-shrink: 0; text-align: left; overflow-wrap: anywhere; border: 0; background: transparent; padding: 9px; }
|
|
57
|
+
#shellToolsLauncher .tools-launcher-categories button[aria-pressed="true"] { background: var(--panel-2); color: var(--accent); }
|
|
58
|
+
#shellToolsLauncher .tools-launcher-results { flex: 1; overflow: auto; padding: 10px 8px; overscroll-behavior: contain; }
|
|
59
|
+
#shellToolsLauncher .tools-launcher-status { margin: 0 8px 8px; color: var(--muted); font-size: var(--text-xs, 0.75rem); }
|
|
60
|
+
#shellToolsLauncher .tools-launcher-list { display: grid; gap: 3px; margin: 0; padding: 0; list-style: none; }
|
|
61
|
+
#shellToolsLauncher .tools-launcher-row { display: flex; align-items: center; border-radius: 5px; }
|
|
62
|
+
#shellToolsLauncher .tools-launcher-row:is(:hover, :focus-within) { background: var(--panel-2); }
|
|
63
|
+
#shellToolsLauncher .tools-launcher-action-content { flex: 1; }
|
|
64
|
+
#shellToolsLauncher .tools-launcher-action { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 9px 8px; overflow-wrap: anywhere; }
|
|
65
|
+
#shellToolsLauncher .tools-launcher-label { display: block; font-weight: 550; }
|
|
66
|
+
#shellToolsLauncher .tools-launcher-description { display: block; margin-top: 2px; color: var(--muted); font-size: var(--text-xs, 0.75rem); }
|
|
67
|
+
#shellToolsLauncher .tools-launcher-category { display: block; color: var(--accent-2, var(--accent)); font-size: var(--text-xs, 0.75rem); margin-top: 3px; }
|
|
68
|
+
#shellToolsLauncher .tools-launcher-reason { color: var(--warning); margin: 0 8px 9px; font-size: var(--text-xs, 0.75rem); overflow-wrap: anywhere; }
|
|
69
|
+
#shellToolsLauncher .tools-launcher-pin { flex-shrink: 0; align-self: flex-start; min-width: 36px; min-height: 40px; padding: 8px; background: transparent; border: 0; font-size: 1.125rem; }
|
|
70
|
+
#shellToolsLauncher .tools-launcher-pin[aria-pressed="true"] { color: var(--accent-2, var(--accent)); }
|
|
71
|
+
#shellToolsLauncher .tools-launcher-empty { color: var(--muted); padding: 8px; overflow-wrap: anywhere; }
|
|
72
|
+
#shellToolsLauncher .tools-launcher-footer { flex-shrink: 0; padding: 10px 16px; border-top: 1px solid var(--border); color: var(--muted); font-size: var(--text-xs, 0.75rem); }
|
|
73
|
+
|
|
74
|
+
@media (max-width: 600px) {
|
|
75
|
+
#shellToolsLauncher { width: calc(100% - 16px); max-height: calc(100dvh - 16px); }
|
|
76
|
+
#shellToolsLauncher .tools-launcher-categories { width: 98px; padding: 8px 4px; }
|
|
77
|
+
#shellToolsLauncher .tools-launcher-categories button { padding: 10px 5px; }
|
|
78
|
+
#shellToolsLauncher .tools-launcher-results { padding: 10px 3px; }
|
|
79
|
+
#shellToolsLauncher .tools-launcher-header { padding: 10px 12px 4px; }
|
|
80
|
+
#shellToolsLauncher .tools-launcher-context { padding-inline: 12px; }
|
|
81
|
+
#shellToolsLauncher .tools-launcher-footer { padding: 8px 12px; }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@media (forced-colors: active) {
|
|
85
|
+
#shellToolsLauncher {
|
|
86
|
+
--bg: Canvas;
|
|
87
|
+
--panel: Canvas;
|
|
88
|
+
--panel-2: Canvas;
|
|
89
|
+
--text: CanvasText;
|
|
90
|
+
--muted: CanvasText;
|
|
91
|
+
--accent: LinkText;
|
|
92
|
+
--accent-2: LinkText;
|
|
93
|
+
--warning: CanvasText;
|
|
94
|
+
--border: CanvasText;
|
|
95
|
+
}
|
|
96
|
+
#shellToolsLauncher [aria-pressed="true"] { outline: 2px solid Highlight; outline-offset: -2px; }
|
|
97
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { filterActionItems } from './shell-actions.mjs';
|
|
2
|
+
|
|
3
|
+
const PIN_LIMIT = 20;
|
|
4
|
+
|
|
5
|
+
/** Present caller-owned actions; invocation validation and persistence belong to the parent. */
|
|
6
|
+
export function createToolsLauncher({ document, onInvoke, onPinsChange, onClose }) {
|
|
7
|
+
const node = (tag, className, text) => {
|
|
8
|
+
const element = document.createElement(tag);
|
|
9
|
+
if (className) element.className = className;
|
|
10
|
+
if (text !== undefined) element.textContent = text;
|
|
11
|
+
return element;
|
|
12
|
+
};
|
|
13
|
+
const button = (className, text) => {
|
|
14
|
+
const element = node('button', className, text);
|
|
15
|
+
element.type = 'button';
|
|
16
|
+
return element;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const element = node('dialog');
|
|
20
|
+
element.id = 'shellToolsLauncher';
|
|
21
|
+
element.setAttribute('aria-labelledby', 'shellToolsLauncherTitle');
|
|
22
|
+
const header = node('header', 'tools-launcher-header');
|
|
23
|
+
const heading = node('h2', '', 'All tools');
|
|
24
|
+
heading.id = 'shellToolsLauncherTitle';
|
|
25
|
+
const closeButton = button('tools-launcher-close', 'Close');
|
|
26
|
+
closeButton.setAttribute('aria-label', 'Close All tools');
|
|
27
|
+
header.append(heading, closeButton);
|
|
28
|
+
const context = node('p', 'tools-launcher-context');
|
|
29
|
+
const search = node('input', 'tools-launcher-search');
|
|
30
|
+
search.type = 'search';
|
|
31
|
+
search.placeholder = 'Search tools by name or purpose';
|
|
32
|
+
search.setAttribute('aria-label', 'Search all tools');
|
|
33
|
+
search.setAttribute('aria-controls', 'shellToolsLauncherResults');
|
|
34
|
+
search.autofocus = true;
|
|
35
|
+
const runControls = node('div', 'tools-launcher-run-controls');
|
|
36
|
+
runControls.setAttribute('role', 'group');
|
|
37
|
+
const body = node('div', 'tools-launcher-body');
|
|
38
|
+
const categories = node('nav', 'tools-launcher-categories');
|
|
39
|
+
categories.setAttribute('aria-label', 'Tool categories');
|
|
40
|
+
const results = node('section', 'tools-launcher-results');
|
|
41
|
+
results.setAttribute('aria-label', 'Tools');
|
|
42
|
+
const status = node('p', 'tools-launcher-status');
|
|
43
|
+
status.setAttribute('role', 'status');
|
|
44
|
+
const list = node('ul', 'tools-launcher-list');
|
|
45
|
+
list.id = 'shellToolsLauncherResults';
|
|
46
|
+
const empty = node('p', 'tools-launcher-empty');
|
|
47
|
+
results.append(status, list, empty);
|
|
48
|
+
body.append(categories, results);
|
|
49
|
+
const footer = node('footer', 'tools-launcher-footer', '↑ ↓ Home End to browse · Enter to open · Esc to close');
|
|
50
|
+
element.append(header, context, search, runControls, body, footer);
|
|
51
|
+
|
|
52
|
+
let actions = [];
|
|
53
|
+
let pinnedIds = [];
|
|
54
|
+
let selectedCategory = 'all';
|
|
55
|
+
let disposed = false;
|
|
56
|
+
let closePending = false;
|
|
57
|
+
let rows = [];
|
|
58
|
+
const categoryButtons = new Map();
|
|
59
|
+
|
|
60
|
+
function finishClose() {
|
|
61
|
+
if (!closePending) return;
|
|
62
|
+
closePending = false;
|
|
63
|
+
onClose?.();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function close() {
|
|
67
|
+
if (disposed) return;
|
|
68
|
+
if (element.open) element.close();
|
|
69
|
+
finishClose();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function handleNativeClose() {
|
|
73
|
+
if (!element.open) finishClose();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function render() {
|
|
77
|
+
const active = document.activeElement;
|
|
78
|
+
const focusedRow = rows.find((row) => row.invoke === active || row.pin === active);
|
|
79
|
+
const focusedKind = focusedRow?.pin === active ? 'pin' : 'invoke';
|
|
80
|
+
const choices = [['all', 'All'], ['pinned', 'Pinned'],
|
|
81
|
+
...[...new Set(actions.map((action) => action.category).filter(Boolean))]
|
|
82
|
+
.map((category) => [`category:${category}`, category])];
|
|
83
|
+
if (!choices.some(([key]) => key === selectedCategory)) selectedCategory = 'all';
|
|
84
|
+
for (const [key, control] of categoryButtons) {
|
|
85
|
+
if (!choices.some(([choice]) => choice === key)) {
|
|
86
|
+
control.remove();
|
|
87
|
+
categoryButtons.delete(key);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (const [index, [key, label]] of choices.entries()) {
|
|
91
|
+
let control = categoryButtons.get(key);
|
|
92
|
+
if (!control) {
|
|
93
|
+
control = button('', label);
|
|
94
|
+
control.addEventListener('click', () => {
|
|
95
|
+
if (disposed) return;
|
|
96
|
+
selectedCategory = key;
|
|
97
|
+
search.value = '';
|
|
98
|
+
render();
|
|
99
|
+
});
|
|
100
|
+
categoryButtons.set(key, control);
|
|
101
|
+
}
|
|
102
|
+
control.setAttribute('aria-pressed', String(key === selectedCategory));
|
|
103
|
+
if (categories.children[index] !== control) categories.insertBefore(control, categories.children[index] ?? null);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const query = search.value.trim();
|
|
107
|
+
// A search reaches the whole supplied catalog, including records past the palette's default cap.
|
|
108
|
+
const candidates = query ? actions : actions.filter((action) => selectedCategory === 'all'
|
|
109
|
+
|| (selectedCategory === 'pinned' ? pinnedIds.includes(action.id) : `category:${action.category}` === selectedCategory));
|
|
110
|
+
const matches = filterActionItems(candidates, query, { limit: actions.length });
|
|
111
|
+
status.textContent = query ? `${matches.length} results across all tools`
|
|
112
|
+
: `${choices.find(([key]) => key === selectedCategory)[1]} · ${matches.length} tools`;
|
|
113
|
+
empty.hidden = matches.length > 0;
|
|
114
|
+
empty.textContent = query ? 'No matching tools. Try another name or purpose.'
|
|
115
|
+
: selectedCategory === 'pinned' ? 'No pinned tools. Pin a tool from All to keep it here.' : 'No tools available in this category.';
|
|
116
|
+
rows = matches.map((action, index) => {
|
|
117
|
+
const row = node('li', 'tools-launcher-row');
|
|
118
|
+
const invoke = button('tools-launcher-action');
|
|
119
|
+
invoke.disabled = action.available === false;
|
|
120
|
+
const label = node('span', 'tools-launcher-label', action.label);
|
|
121
|
+
const description = node('span', 'tools-launcher-description', action.description);
|
|
122
|
+
const category = node('span', 'tools-launcher-category', action.category);
|
|
123
|
+
invoke.append(label, description, category);
|
|
124
|
+
invoke.setAttribute('aria-label', action.label);
|
|
125
|
+
const reason = node('p', 'tools-launcher-reason', action.unavailableReason || 'This action is currently unavailable.');
|
|
126
|
+
reason.id = `shellToolsLauncherReason${index}`;
|
|
127
|
+
reason.hidden = !invoke.disabled;
|
|
128
|
+
if (invoke.disabled) invoke.setAttribute('aria-describedby', reason.id);
|
|
129
|
+
invoke.addEventListener('click', () => {
|
|
130
|
+
if (!disposed && element.open && action.available !== false) onInvoke?.(action);
|
|
131
|
+
});
|
|
132
|
+
const pinned = pinnedIds.includes(action.id);
|
|
133
|
+
const pin = button('tools-launcher-pin', pinned ? '★' : '☆');
|
|
134
|
+
pin.setAttribute('aria-label', `${pinned ? 'Unpin' : 'Pin'} ${action.label}`);
|
|
135
|
+
pin.setAttribute('aria-pressed', String(pinned));
|
|
136
|
+
pin.disabled = !pinned && pinnedIds.length >= PIN_LIMIT;
|
|
137
|
+
pin.title = pin.disabled ? 'You can pin up to 20 tools. Unpin a tool first.' : `${pinned ? 'Unpin' : 'Pin'} tool`;
|
|
138
|
+
pin.addEventListener('click', () => {
|
|
139
|
+
if (disposed || !element.open || pin.disabled) return;
|
|
140
|
+
pinnedIds = pinned ? pinnedIds.filter((id) => id !== action.id) : [...pinnedIds, action.id];
|
|
141
|
+
render();
|
|
142
|
+
onPinsChange?.([...pinnedIds]);
|
|
143
|
+
});
|
|
144
|
+
const text = node('div', 'tools-launcher-action-content');
|
|
145
|
+
text.append(invoke, reason);
|
|
146
|
+
row.append(text, pin);
|
|
147
|
+
return { action, row, invoke, pin };
|
|
148
|
+
});
|
|
149
|
+
list.replaceChildren(...rows.map((row) => row.row));
|
|
150
|
+
if (focusedRow) {
|
|
151
|
+
const replacement = rows.find((row) => row.action.id === focusedRow.action.id)?.[focusedKind];
|
|
152
|
+
if (replacement && !replacement.disabled) replacement.focus({ preventScroll: true });
|
|
153
|
+
else search.focus({ preventScroll: true });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function update(snapshot = {}) {
|
|
158
|
+
if (disposed) return;
|
|
159
|
+
if (snapshot.actions !== undefined) actions = snapshot.actions;
|
|
160
|
+
if (snapshot.pinnedIds !== undefined) {
|
|
161
|
+
pinnedIds = [...new Set(snapshot.pinnedIds.filter((id) => typeof id === 'string' && id.length > 0))].slice(0, PIN_LIMIT);
|
|
162
|
+
}
|
|
163
|
+
if (snapshot.contextLabel !== undefined) context.textContent = snapshot.contextLabel;
|
|
164
|
+
context.hidden = !context.textContent;
|
|
165
|
+
render();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function open(snapshot = {}) {
|
|
169
|
+
if (disposed) return;
|
|
170
|
+
if (!element.open) {
|
|
171
|
+
search.value = typeof snapshot.initialQuery === 'string' ? snapshot.initialQuery : '';
|
|
172
|
+
selectedCategory = snapshot.initialCategory ? `category:${snapshot.initialCategory}` : 'all';
|
|
173
|
+
}
|
|
174
|
+
update(snapshot);
|
|
175
|
+
if (!element.open) element.showModal();
|
|
176
|
+
closePending = true;
|
|
177
|
+
search.focus({ preventScroll: true });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function handleKeydown(event) {
|
|
181
|
+
if (disposed || event.defaultPrevented || event.isComposing || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
|
|
182
|
+
const current = rows.findIndex((row) => row.invoke === event.target);
|
|
183
|
+
if (event.target !== search && current < 0) return;
|
|
184
|
+
const available = rows.filter((row) => !row.invoke.disabled).map((row) => row.invoke);
|
|
185
|
+
if (event.key === 'Enter' && event.target === search) {
|
|
186
|
+
event.preventDefault();
|
|
187
|
+
available[0]?.click();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return;
|
|
191
|
+
event.preventDefault();
|
|
192
|
+
if (!available.length) return;
|
|
193
|
+
const index = available.indexOf(event.target);
|
|
194
|
+
let next;
|
|
195
|
+
if (event.key === 'Home') next = 0;
|
|
196
|
+
else if (event.key === 'End') next = available.length - 1;
|
|
197
|
+
else if (event.key === 'ArrowDown') next = (index + 1) % available.length;
|
|
198
|
+
else next = index < 0 ? available.length - 1 : (index - 1 + available.length) % available.length;
|
|
199
|
+
available[next].focus();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function dispose() {
|
|
203
|
+
if (disposed) return;
|
|
204
|
+
close();
|
|
205
|
+
disposed = true;
|
|
206
|
+
search.removeEventListener('input', render);
|
|
207
|
+
closeButton.removeEventListener('click', close);
|
|
208
|
+
element.removeEventListener('close', handleNativeClose);
|
|
209
|
+
element.removeEventListener('keydown', handleKeydown);
|
|
210
|
+
element.remove();
|
|
211
|
+
actions = [];
|
|
212
|
+
rows = [];
|
|
213
|
+
categoryButtons.clear();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
search.addEventListener('input', render);
|
|
217
|
+
closeButton.addEventListener('click', close);
|
|
218
|
+
element.addEventListener('close', handleNativeClose);
|
|
219
|
+
element.addEventListener('keydown', handleKeydown);
|
|
220
|
+
update();
|
|
221
|
+
return { element, slots: { runControls }, open, update, close, dispose };
|
|
222
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
function isVisible(node, document) {
|
|
2
|
+
const element = node?.nodeType === 1 ? node : node?.parentElement;
|
|
3
|
+
if (!element?.isConnected || element.closest('[hidden], [inert]')) return false;
|
|
4
|
+
const style = document.defaultView.getComputedStyle(element);
|
|
5
|
+
return style.visibility !== 'hidden' && style.visibility !== 'collapse' && element.getClientRects().length > 0;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function captureContinuity(node, document) {
|
|
9
|
+
const active = node.contains(document.activeElement) ? document.activeElement : null;
|
|
10
|
+
const inputSelection = active && typeof active.selectionStart === 'number'
|
|
11
|
+
? [active.selectionStart, active.selectionEnd, active.selectionDirection]
|
|
12
|
+
: null;
|
|
13
|
+
const selection = document.getSelection();
|
|
14
|
+
const ranges = [];
|
|
15
|
+
// Chromium exposes a document selection for a focused text control too.
|
|
16
|
+
// Reapplying that range would overwrite the control's own selection.
|
|
17
|
+
if (selection && !inputSelection) {
|
|
18
|
+
for (let index = 0; index < selection.rangeCount; index += 1) {
|
|
19
|
+
const range = selection.getRangeAt(index);
|
|
20
|
+
if (range.intersectsNode(node)) {
|
|
21
|
+
ranges.push([range.startContainer, range.startOffset, range.endContainer, range.endOffset]);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const direction = ranges.length && selection.anchorNode
|
|
26
|
+
? [selection.anchorNode, selection.anchorOffset, selection.focusNode, selection.focusOffset]
|
|
27
|
+
: null;
|
|
28
|
+
const scroll = new Map();
|
|
29
|
+
// Reparenting and restoring focus can scroll both the view and its old/new ancestors.
|
|
30
|
+
for (const element of [node, ...node.querySelectorAll('*')]) {
|
|
31
|
+
if (element.scrollTop || element.scrollLeft) scroll.set(element, [element.scrollLeft, element.scrollTop]);
|
|
32
|
+
}
|
|
33
|
+
for (let ancestor = node.parentElement; ancestor; ancestor = ancestor.parentElement) {
|
|
34
|
+
scroll.set(ancestor, [ancestor.scrollLeft, ancestor.scrollTop]);
|
|
35
|
+
}
|
|
36
|
+
const windowScroll = [document.defaultView.scrollX, document.defaultView.scrollY];
|
|
37
|
+
|
|
38
|
+
return (host) => {
|
|
39
|
+
if (active && isVisible(active, document)) {
|
|
40
|
+
active.focus({ preventScroll: true });
|
|
41
|
+
if (inputSelection) active.setSelectionRange(...inputSelection);
|
|
42
|
+
}
|
|
43
|
+
if (ranges.length && ranges.every(([start, , end]) => isVisible(start, document) && isVisible(end, document))) {
|
|
44
|
+
selection.removeAllRanges();
|
|
45
|
+
for (const [start, startOffset, end, endOffset] of ranges) {
|
|
46
|
+
const range = document.createRange();
|
|
47
|
+
range.setStart(start, startOffset);
|
|
48
|
+
range.setEnd(end, endOffset);
|
|
49
|
+
selection.addRange(range);
|
|
50
|
+
}
|
|
51
|
+
if (ranges.length === 1 && direction) selection.setBaseAndExtent(...direction);
|
|
52
|
+
}
|
|
53
|
+
for (const [element, [left, top]] of scroll) element.scrollTo(left, top);
|
|
54
|
+
// Preserve the destination's scroll as well when it was not an old ancestor.
|
|
55
|
+
for (const [element, left, top] of host) element.scrollTo(left, top);
|
|
56
|
+
document.defaultView.scrollTo(...windowScroll);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Owns canonical DOM placement, without owning view or session resources. */
|
|
61
|
+
export function createViewHost({ document }) {
|
|
62
|
+
const records = new Map();
|
|
63
|
+
const parentOrders = new Map();
|
|
64
|
+
let disposed = false;
|
|
65
|
+
|
|
66
|
+
function place(node, host, sibling = null) {
|
|
67
|
+
const restoreContinuity = captureContinuity(node, document);
|
|
68
|
+
const destinationScroll = [];
|
|
69
|
+
for (let ancestor = host; ancestor?.nodeType === 1; ancestor = ancestor.parentElement) {
|
|
70
|
+
destinationScroll.push([ancestor, ancestor.scrollLeft, ancestor.scrollTop]);
|
|
71
|
+
}
|
|
72
|
+
host.insertBefore(node, sibling);
|
|
73
|
+
restoreContinuity(destinationScroll);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function move(node, host) {
|
|
77
|
+
if (disposed) return;
|
|
78
|
+
if (!records.has(node)) {
|
|
79
|
+
const parent = node.parentNode;
|
|
80
|
+
if (!parent) throw new TypeError('A canonical view must have an original parent.');
|
|
81
|
+
if (!parentOrders.has(parent)) parentOrders.set(parent, [...parent.childNodes]);
|
|
82
|
+
const order = parentOrders.get(parent);
|
|
83
|
+
const index = order.indexOf(node);
|
|
84
|
+
records.set(node, { parent, nextSibling: index < 0 ? node.nextSibling : order[index + 1] ?? null });
|
|
85
|
+
}
|
|
86
|
+
if (node.parentNode !== host) place(node, host);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function restore(node) {
|
|
90
|
+
const record = records.get(node);
|
|
91
|
+
if (!record) return;
|
|
92
|
+
let sibling = record.nextSibling;
|
|
93
|
+
// A neighboring canonical view may still be mounted elsewhere. Follow its
|
|
94
|
+
// saved position so individual restores also retain the original ordering.
|
|
95
|
+
const visited = new Set();
|
|
96
|
+
while (sibling && sibling.parentNode !== record.parent && records.has(sibling) && !visited.has(sibling)) {
|
|
97
|
+
visited.add(sibling);
|
|
98
|
+
sibling = records.get(sibling).nextSibling;
|
|
99
|
+
}
|
|
100
|
+
place(node, record.parent, sibling?.parentNode === record.parent ? sibling : null);
|
|
101
|
+
records.delete(node);
|
|
102
|
+
if (![...records.values()].some(({ parent }) => parent === record.parent)) parentOrders.delete(record.parent);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function restoreAll() {
|
|
106
|
+
for (const node of records.keys()) restore(node);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function dispose() {
|
|
110
|
+
restoreAll();
|
|
111
|
+
disposed = true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return { move, restore, restoreAll, dispose };
|
|
115
|
+
}
|