@firstpick/pi-package-webui 0.11.1 → 0.11.3
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 +151 -12
- package/README.md +34 -6
- package/TECHNICAL.md +75 -8
- package/bin/pi-webui.mjs +5390 -11423
- package/index.ts +10 -3
- 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 +4 -4
- 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 -3886
- 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 +322 -169
- package/public/subagent-launch-slot-state.mjs +2 -2
- 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/action-feedback-gutter.spec.mjs +251 -0
- 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-fast-mode-static.test.mjs +16 -11
- package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
- package/tests/codex-usage-source.test.mjs +4 -3
- 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-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 +21 -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-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,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Own one EventSource without interpreting its events. Callbacks receive the
|
|
3
|
+
* original event and captured context. Stale or disposed connects return null;
|
|
4
|
+
* successful connects return the source. Factory and callback errors propagate.
|
|
5
|
+
*/
|
|
6
|
+
export function createSessionEventStream({ createSource, isCurrent, onOpen = () => {}, onMessage = () => {}, onError = () => {} } = {}) {
|
|
7
|
+
if ([createSource, isCurrent, onOpen, onMessage, onError].some((callback) => typeof callback !== "function")) {
|
|
8
|
+
throw new TypeError("Event stream requires a source factory and context/callback functions");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let owner = null;
|
|
12
|
+
let disposed = false;
|
|
13
|
+
|
|
14
|
+
function release(source) {
|
|
15
|
+
if (!source) return;
|
|
16
|
+
source.onopen = null;
|
|
17
|
+
source.onmessage = null;
|
|
18
|
+
source.onerror = null;
|
|
19
|
+
source.close();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function close() {
|
|
23
|
+
const previous = owner;
|
|
24
|
+
owner = null;
|
|
25
|
+
release(previous?.source);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
connect(url, context) {
|
|
30
|
+
if (disposed || !isCurrent(context)) return null;
|
|
31
|
+
close();
|
|
32
|
+
const next = { source: null, context };
|
|
33
|
+
owner = next;
|
|
34
|
+
try {
|
|
35
|
+
next.source = createSource(url);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (owner === next) owner = null;
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Construction may invalidate the context or close/replace this owner.
|
|
42
|
+
if (disposed || owner !== next || !isCurrent(context)) {
|
|
43
|
+
if (owner === next) owner = null;
|
|
44
|
+
release(next.source);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const forward = (callback) => (event) => {
|
|
49
|
+
if (!disposed && owner === next && isCurrent(next.context)) callback(event, next.context);
|
|
50
|
+
};
|
|
51
|
+
next.source.onopen = forward(onOpen);
|
|
52
|
+
next.source.onmessage = forward(onMessage);
|
|
53
|
+
next.source.onerror = forward(onError);
|
|
54
|
+
return next.source;
|
|
55
|
+
},
|
|
56
|
+
close,
|
|
57
|
+
dispose() {
|
|
58
|
+
if (disposed) return;
|
|
59
|
+
disposed = true;
|
|
60
|
+
close();
|
|
61
|
+
},
|
|
62
|
+
get source() { return owner?.source ?? null; },
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/** Owns the canonical artifact content; the parent owns its host and session lifetime. */
|
|
2
|
+
export function createProjectArtifactController({
|
|
3
|
+
document, api, captureTarget, isTargetCurrent, onOpen, onClose, onReference, onError,
|
|
4
|
+
}) {
|
|
5
|
+
const element = document.createElement("div");
|
|
6
|
+
element.className = "document-artifact-content";
|
|
7
|
+
const AbortController = document.defaultView.AbortController;
|
|
8
|
+
const lifetime = new AbortController();
|
|
9
|
+
let viewListeners = new AbortController();
|
|
10
|
+
let generation = 0;
|
|
11
|
+
let owner = null;
|
|
12
|
+
let disposed = false;
|
|
13
|
+
|
|
14
|
+
function make(tag, className, text) {
|
|
15
|
+
const node = document.createElement(tag);
|
|
16
|
+
if (className) node.className = className;
|
|
17
|
+
if (text !== undefined) node.textContent = text;
|
|
18
|
+
return node;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function current(candidate) {
|
|
22
|
+
return !disposed && owner === candidate && candidate.generation === generation
|
|
23
|
+
&& !!candidate.target?.tabId && isTargetCurrent(candidate.target);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function replaceContent(...nodes) {
|
|
27
|
+
viewListeners.abort();
|
|
28
|
+
viewListeners = new AbortController();
|
|
29
|
+
element.replaceChildren(...nodes);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function listen(node, type, listener) {
|
|
33
|
+
node.addEventListener(type, listener, { signal: viewListeners.signal });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function button(label, action) {
|
|
37
|
+
const node = make("button", undefined, label);
|
|
38
|
+
node.type = "button";
|
|
39
|
+
listen(node, "click", action);
|
|
40
|
+
return node;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function downloadLink(artifact) {
|
|
44
|
+
const link = make("a", "button-like", "Download");
|
|
45
|
+
link.href = artifact.downloadUrl;
|
|
46
|
+
link.download = artifact.title || "document";
|
|
47
|
+
return link;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function close({ notify = true } = {}) {
|
|
51
|
+
if (disposed) return;
|
|
52
|
+
const wasOpen = !!owner;
|
|
53
|
+
generation += 1;
|
|
54
|
+
owner = null;
|
|
55
|
+
replaceContent();
|
|
56
|
+
if (!disposed && wasOpen && notify) onClose();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function showError(error, candidate) {
|
|
60
|
+
if (!current(candidate)) return;
|
|
61
|
+
replaceContent();
|
|
62
|
+
element.append(
|
|
63
|
+
make("div", "document-artifact-error", `Document artifact is stale or unavailable: ${error?.message || error}`),
|
|
64
|
+
button("Close", () => close()),
|
|
65
|
+
);
|
|
66
|
+
onError(error, candidate.target);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function renderManifest(manifest, candidate) {
|
|
70
|
+
const { artifact, target } = candidate;
|
|
71
|
+
const pages = Array.isArray(manifest.pages) ? manifest.pages : [];
|
|
72
|
+
let pageIndex = 0;
|
|
73
|
+
let zoom = 1;
|
|
74
|
+
let rotation = 0;
|
|
75
|
+
let sending = false;
|
|
76
|
+
replaceContent();
|
|
77
|
+
const shell = make("section", "document-artifact-shell");
|
|
78
|
+
const header = make("header", "document-artifact-header");
|
|
79
|
+
const title = make("div", "document-artifact-title");
|
|
80
|
+
const actions = make("div", "document-artifact-actions");
|
|
81
|
+
const body = make("div", "document-artifact-body");
|
|
82
|
+
const sidebar = make("aside", "document-artifact-sidebar");
|
|
83
|
+
const stage = make("main", "document-artifact-stage");
|
|
84
|
+
const image = make("img", "document-artifact-page");
|
|
85
|
+
const toolbar = make("div", "document-artifact-toolbar");
|
|
86
|
+
const meta = make("span", "document-artifact-meta");
|
|
87
|
+
const search = make("input", "document-artifact-search");
|
|
88
|
+
const semantic = make("details", "document-artifact-semantic");
|
|
89
|
+
const pageCount = artifact.pageCount || pages.length || 0;
|
|
90
|
+
title.append(make("strong", undefined, artifact.title || "Document"),
|
|
91
|
+
make("span", "muted", `${pageCount} page${Number(pageCount) === 1 ? "" : "s"} · ${manifest.renderer?.engine || "document artifact"}`));
|
|
92
|
+
if (artifact.downloadUrl) actions.append(downloadLink(artifact));
|
|
93
|
+
actions.append(button("Close", () => close()));
|
|
94
|
+
header.append(title, actions);
|
|
95
|
+
search.type = "search";
|
|
96
|
+
search.placeholder = "Search outline, comments, revisions, or metadata";
|
|
97
|
+
search.setAttribute("aria-label", search.placeholder);
|
|
98
|
+
|
|
99
|
+
function changePage(action) {
|
|
100
|
+
return () => { if (current(candidate)) { action(); renderPage(); } };
|
|
101
|
+
}
|
|
102
|
+
const previous = button("Previous", changePage(() => { pageIndex = Math.max(0, pageIndex - 1); }));
|
|
103
|
+
const next = button("Next", changePage(() => { pageIndex = Math.min(pages.length - 1, pageIndex + 1); }));
|
|
104
|
+
const zoomOut = button("−", changePage(() => { zoom = Math.max(0.25, zoom - 0.25); }));
|
|
105
|
+
const zoomIn = button("+", changePage(() => { zoom = Math.min(3, zoom + 0.25); }));
|
|
106
|
+
const fit = button("Fit", changePage(() => { zoom = 1; }));
|
|
107
|
+
const rotate = button("Rotate", changePage(() => { rotation = (rotation + 90) % 360; }));
|
|
108
|
+
const send = button("Send page to composer", async () => {
|
|
109
|
+
const page = pages[pageIndex];
|
|
110
|
+
if (!page || sending || !current(candidate)) return;
|
|
111
|
+
sending = true;
|
|
112
|
+
send.disabled = true;
|
|
113
|
+
try {
|
|
114
|
+
const result = await onReference(artifact, page.pageNum, target);
|
|
115
|
+
if (current(candidate) && result !== false) close();
|
|
116
|
+
} catch (error) {
|
|
117
|
+
showError(error, candidate);
|
|
118
|
+
} finally {
|
|
119
|
+
sending = false;
|
|
120
|
+
if (current(candidate)) send.disabled = !page;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
const thumbnails = [];
|
|
124
|
+
function renderPage() {
|
|
125
|
+
const page = pages[pageIndex];
|
|
126
|
+
image.hidden = !page;
|
|
127
|
+
if (page) {
|
|
128
|
+
image.src = page.imageUrl;
|
|
129
|
+
image.alt = `Rendered page ${page.pageNum} of ${artifact.title || "document"}`;
|
|
130
|
+
image.style.transform = `scale(${zoom}) rotate(${rotation}deg)`;
|
|
131
|
+
meta.textContent = `Page ${page.pageNum} of ${artifact.pageCount || pages.length} · ${Math.round(zoom * 100)}% · ${rotation}°`;
|
|
132
|
+
} else meta.textContent = "No rendered pages are available";
|
|
133
|
+
previous.disabled = pageIndex <= 0;
|
|
134
|
+
next.disabled = pageIndex >= pages.length - 1;
|
|
135
|
+
send.disabled = !page || sending;
|
|
136
|
+
for (const [index, thumbnail] of thumbnails.entries()) thumbnail.classList.toggle("active", index === pageIndex);
|
|
137
|
+
}
|
|
138
|
+
toolbar.append(previous, next, zoomOut, zoomIn, fit, rotate, send, meta);
|
|
139
|
+
for (const [index, page] of pages.entries()) {
|
|
140
|
+
const thumbnail = button("", changePage(() => { pageIndex = index; }));
|
|
141
|
+
thumbnail.className = "document-artifact-thumbnail";
|
|
142
|
+
const thumb = make("img");
|
|
143
|
+
thumb.src = page.imageUrl;
|
|
144
|
+
thumb.alt = "";
|
|
145
|
+
thumbnail.append(thumb, make("span", undefined, `Page ${page.pageNum}`));
|
|
146
|
+
thumbnails.push(thumbnail);
|
|
147
|
+
sidebar.append(thumbnail);
|
|
148
|
+
}
|
|
149
|
+
const semanticData = {
|
|
150
|
+
outline: manifest.outline || [], comments: manifest.comments || [],
|
|
151
|
+
revisions: manifest.revisions || [], warnings: manifest.warnings || [],
|
|
152
|
+
diff: manifest.diff || undefined, renderer: manifest.renderer || undefined,
|
|
153
|
+
};
|
|
154
|
+
semantic.append(make("summary", undefined, "Semantic structure, revisions, warnings, and diff"),
|
|
155
|
+
make("pre", undefined, JSON.stringify(semanticData, null, 2)));
|
|
156
|
+
listen(search, "input", () => {
|
|
157
|
+
if (!current(candidate)) return;
|
|
158
|
+
const query = search.value.trim().toLocaleLowerCase();
|
|
159
|
+
semantic.hidden = !!query && !JSON.stringify(semanticData).toLocaleLowerCase().includes(query);
|
|
160
|
+
search.setCustomValidity(query && semantic.hidden ? "No semantic match" : "");
|
|
161
|
+
});
|
|
162
|
+
stage.append(toolbar, search, image, semantic);
|
|
163
|
+
body.append(sidebar, stage);
|
|
164
|
+
shell.append(header, body);
|
|
165
|
+
element.append(shell);
|
|
166
|
+
renderPage();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async function open(artifact, target = captureTarget()) {
|
|
170
|
+
if (disposed || !target?.tabId || !isTargetCurrent(target)) return false;
|
|
171
|
+
const beforeReveal = generation;
|
|
172
|
+
if (onOpen({ element, artifact, target }) === false) return false;
|
|
173
|
+
// Host callbacks can synchronously close or replace this view.
|
|
174
|
+
if (disposed || generation !== beforeReveal || !isTargetCurrent(target)) return false;
|
|
175
|
+
const candidate = { artifact, target, generation: ++generation };
|
|
176
|
+
owner = candidate;
|
|
177
|
+
replaceContent(make("div", "document-artifact-loading", "Loading document artifact…"));
|
|
178
|
+
try {
|
|
179
|
+
const response = await api(artifact.manifestUrl, { tabId: target.tabId });
|
|
180
|
+
if (!current(candidate)) return false;
|
|
181
|
+
renderManifest(response.data || {}, candidate);
|
|
182
|
+
return true;
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (!current(candidate)) return false;
|
|
185
|
+
showError(error, candidate);
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function renderCard(artifact, target = captureTarget()) {
|
|
191
|
+
if (disposed || artifact?.schema !== "pi.artifact/v1" || artifact?.kind !== "document") return null;
|
|
192
|
+
const card = make("div", `document-artifact-card${artifact.unavailable ? " unavailable" : ""}`);
|
|
193
|
+
const text = make("div");
|
|
194
|
+
const actions = make("div", "document-artifact-card-actions");
|
|
195
|
+
text.append(make("strong", undefined, artifact.title || "Document artifact"), make("span", "muted",
|
|
196
|
+
artifact.unavailable ? "Preview unavailable" : `${artifact.pageCount || 0} page${Number(artifact.pageCount) === 1 ? "" : "s"}`));
|
|
197
|
+
if (!artifact.unavailable && artifact.manifestUrl) {
|
|
198
|
+
const openButton = make("button", undefined, "Open viewer");
|
|
199
|
+
openButton.type = "button";
|
|
200
|
+
openButton.addEventListener("click", () => { void open(artifact, target); }, { signal: lifetime.signal });
|
|
201
|
+
actions.append(openButton);
|
|
202
|
+
}
|
|
203
|
+
if (!artifact.unavailable && artifact.downloadUrl) actions.append(downloadLink(artifact));
|
|
204
|
+
card.append(text, actions);
|
|
205
|
+
return card;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function dispose() {
|
|
209
|
+
if (disposed) return;
|
|
210
|
+
close({ notify: false });
|
|
211
|
+
disposed = true;
|
|
212
|
+
lifetime.abort();
|
|
213
|
+
viewListeners.abort();
|
|
214
|
+
element.remove();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return { element, open, renderCard, close, dispose };
|
|
218
|
+
}
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
const GIT_FILE_DIFF_CATEGORY_BY_PANEL = Object.freeze({
|
|
2
|
+
staged: "staged",
|
|
3
|
+
changes: "unstaged",
|
|
4
|
+
conflicted: "conflicted",
|
|
5
|
+
untracked: "untracked",
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
export function gitFileDiffCategory(panelCategory = "") {
|
|
9
|
+
return GIT_FILE_DIFF_CATEGORY_BY_PANEL[String(panelCategory || "").trim()] || "";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function resolveFileViewerMode(viewer) {
|
|
13
|
+
if (!viewer) return "source";
|
|
14
|
+
const hasChanges = !!viewer.gitChanges;
|
|
15
|
+
if (viewer.mode === "changes") return hasChanges ? "changes" : viewer.kind === "image" ? "image" : "source";
|
|
16
|
+
if (viewer.kind === "image") return "image";
|
|
17
|
+
if (viewer.sourceAvailable === false) return hasChanges ? "changes" : "source";
|
|
18
|
+
return viewer.mode === "preview" && viewer.language === "markdown" ? "preview" : "source";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Owns per-tab buffers and selections. Projections are borrowed read-only objects.
|
|
23
|
+
* activate restores a cached buffer; cache accepts only the active tab and retains
|
|
24
|
+
* object identity. resetActive clears presentation without discarding that cache.
|
|
25
|
+
* Requests return {success:true,viewer} or {success:false,reason,error?}. Reasons:
|
|
26
|
+
* path, target, empty, read-only, declined, stale, error. A stale request may have
|
|
27
|
+
* reached the server. Only a diff may apply while its originating tab is background.
|
|
28
|
+
*/
|
|
29
|
+
export function createProjectFileViewerController({
|
|
30
|
+
api,
|
|
31
|
+
captureTarget,
|
|
32
|
+
isTargetCurrent,
|
|
33
|
+
isTargetAlive,
|
|
34
|
+
confirmDiscardEdits,
|
|
35
|
+
normalizePath,
|
|
36
|
+
fileApiPath,
|
|
37
|
+
fileDisplayName,
|
|
38
|
+
fileExtensionForPath,
|
|
39
|
+
fileLanguageForPath,
|
|
40
|
+
imageDataUrl,
|
|
41
|
+
remapPath: remapFilePath,
|
|
42
|
+
onChange = () => {},
|
|
43
|
+
onStatus = () => {},
|
|
44
|
+
onSelectPath = () => {},
|
|
45
|
+
onRefreshDirectory = () => {},
|
|
46
|
+
addEvent = () => {},
|
|
47
|
+
}) {
|
|
48
|
+
const tabs = new Map();
|
|
49
|
+
const saves = new WeakMap();
|
|
50
|
+
let activeTabId = null;
|
|
51
|
+
let viewer = null;
|
|
52
|
+
let selection = null;
|
|
53
|
+
let openSerial = 0;
|
|
54
|
+
let diffSerial = 0;
|
|
55
|
+
|
|
56
|
+
function ensureTab(tabId) {
|
|
57
|
+
let owner = tabs.get(tabId);
|
|
58
|
+
if (!owner) {
|
|
59
|
+
owner = { viewer: null, selection: null };
|
|
60
|
+
tabs.set(tabId, owner);
|
|
61
|
+
}
|
|
62
|
+
return owner;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function cache(tabId = activeTabId, editorContinuity) {
|
|
66
|
+
if (!tabId || tabId !== activeTabId) return;
|
|
67
|
+
if (viewer && editorContinuity !== undefined) viewer.editorContinuity = editorContinuity;
|
|
68
|
+
const owner = ensureTab(tabId);
|
|
69
|
+
owner.viewer = viewer;
|
|
70
|
+
owner.selection = selection;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function activate(tabId) {
|
|
74
|
+
openSerial += 1;
|
|
75
|
+
activeTabId = tabId || null;
|
|
76
|
+
viewer = tabs.get(activeTabId)?.viewer || null;
|
|
77
|
+
selection = tabs.get(activeTabId)?.selection || null;
|
|
78
|
+
onChange("activate");
|
|
79
|
+
if (viewer?.dirty) onStatus("Unsaved file edits.", "warn");
|
|
80
|
+
else if (viewer) onStatus("Restored for this thread.", "success");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function resetActive() {
|
|
84
|
+
openSerial += 1;
|
|
85
|
+
viewer = null;
|
|
86
|
+
selection = null;
|
|
87
|
+
onChange("reset");
|
|
88
|
+
onStatus("");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function close() {
|
|
92
|
+
if (activeTabId) tabs.delete(activeTabId);
|
|
93
|
+
openSerial += 1;
|
|
94
|
+
viewer = null;
|
|
95
|
+
selection = null;
|
|
96
|
+
onChange("close");
|
|
97
|
+
onStatus("");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function forgetTab(tabId) {
|
|
101
|
+
// Deleting the owner rejects old work even if a later tab reuses this ID.
|
|
102
|
+
tabs.delete(tabId);
|
|
103
|
+
if (tabId === activeTabId) {
|
|
104
|
+
openSerial += 1;
|
|
105
|
+
viewer = null;
|
|
106
|
+
selection = null;
|
|
107
|
+
}
|
|
108
|
+
onChange("forget");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function setDirty(dirty) {
|
|
112
|
+
if (!viewer || viewer.readOnly) return;
|
|
113
|
+
viewer.dirty = !!dirty;
|
|
114
|
+
cache();
|
|
115
|
+
onChange("content");
|
|
116
|
+
if (viewer.dirty) onStatus("Unsaved file edits.", "warn");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function setContent(content) {
|
|
120
|
+
if (!viewer || viewer.readOnly) return;
|
|
121
|
+
viewer.content = String(content ?? "");
|
|
122
|
+
viewer.size = new Blob([viewer.content]).size;
|
|
123
|
+
setDirty(true);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function setMode(mode = "source") {
|
|
127
|
+
if (!viewer) return;
|
|
128
|
+
const previousMode = viewer.mode;
|
|
129
|
+
viewer.mode = resolveFileViewerMode({ ...viewer, mode });
|
|
130
|
+
if (viewer.mode === "changes" && previousMode !== "changes") selection = null;
|
|
131
|
+
cache();
|
|
132
|
+
onChange("mode");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function setSelection(next) {
|
|
136
|
+
if (!viewer) return;
|
|
137
|
+
selection = next;
|
|
138
|
+
cache();
|
|
139
|
+
onChange("selection");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function setEditorContinuity(state) {
|
|
143
|
+
if (!viewer) return;
|
|
144
|
+
viewer.editorContinuity = state;
|
|
145
|
+
cache();
|
|
146
|
+
onChange("continuity");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function remapPath(source, destination) {
|
|
150
|
+
if (!viewer) return;
|
|
151
|
+
const path = normalizePath(viewer.path || "");
|
|
152
|
+
const nextPath = remapFilePath(path, source, destination);
|
|
153
|
+
if (!nextPath || nextPath === path) return;
|
|
154
|
+
openSerial += 1;
|
|
155
|
+
saves.delete(viewer);
|
|
156
|
+
const language = fileLanguageForPath(nextPath);
|
|
157
|
+
Object.assign(viewer, {
|
|
158
|
+
path: nextPath,
|
|
159
|
+
name: fileDisplayName(nextPath),
|
|
160
|
+
extension: fileExtensionForPath(nextPath),
|
|
161
|
+
language,
|
|
162
|
+
mode: viewer.mode === "preview" && language === "markdown" ? "preview" : "source",
|
|
163
|
+
gitChanges: null,
|
|
164
|
+
});
|
|
165
|
+
viewer.mode = resolveFileViewerMode(viewer);
|
|
166
|
+
if (selection) selection = { ...selection, path: nextPath, fileName: viewer.name };
|
|
167
|
+
cache();
|
|
168
|
+
onChange("move");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function isForeground(target) {
|
|
172
|
+
return !!target?.tabId && target.tabId === activeTabId && isTargetCurrent(target);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function beginOpen() {
|
|
176
|
+
const target = captureTarget();
|
|
177
|
+
if (!isForeground(target)) return null;
|
|
178
|
+
return {
|
|
179
|
+
target,
|
|
180
|
+
owner: ensureTab(target.tabId),
|
|
181
|
+
candidate: viewer,
|
|
182
|
+
content: viewer?.content,
|
|
183
|
+
dirty: viewer?.dirty,
|
|
184
|
+
path: viewer?.path,
|
|
185
|
+
serial: ++openSerial,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function isOpenCurrent(token) {
|
|
190
|
+
return token.serial === openSerial && tabs.get(token.target.tabId) === token.owner
|
|
191
|
+
&& isForeground(token.target) && viewer === token.candidate
|
|
192
|
+
&& viewer?.path === token.path && viewer?.content === token.content && viewer?.dirty === token.dirty;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async function mayReplace(token, path) {
|
|
196
|
+
if (token.candidate?.dirty && token.path !== path) {
|
|
197
|
+
const accepted = await confirmDiscardEdits({ what: "unsaved file edits", target: token.path });
|
|
198
|
+
if (!isOpenCurrent(token)) return { success: false, reason: "stale" };
|
|
199
|
+
if (!accepted) return { success: false, reason: "declined" };
|
|
200
|
+
}
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function publishOpen(next) {
|
|
205
|
+
viewer = next;
|
|
206
|
+
selection = null;
|
|
207
|
+
cache();
|
|
208
|
+
onChange("open");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async function loadDiff(path, category, tabId) {
|
|
212
|
+
const snapshot = { category, path, label: "", command: "", diff: "", truncated: false, capBytes: 0, error: "" };
|
|
213
|
+
try {
|
|
214
|
+
const params = new URLSearchParams({ path, category });
|
|
215
|
+
const response = await api(`/api/git-file-diff?${params.toString()}`, { tabId });
|
|
216
|
+
if (!response.ok) throw new Error(response.error || "Failed to load Git changes for this file");
|
|
217
|
+
const data = response.data || {};
|
|
218
|
+
return {
|
|
219
|
+
...snapshot, ...data, category, path: data.path || path,
|
|
220
|
+
diff: String(data.diff || ""), truncated: data.truncated === true,
|
|
221
|
+
capBytes: Number(data.capBytes) || 0, contentError: data.error ? String(data.error) : "", error: "",
|
|
222
|
+
};
|
|
223
|
+
} catch (error) {
|
|
224
|
+
return { ...snapshot, error: error.message || String(error) };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function applyDiff(request, token, buffer, placeholder) {
|
|
229
|
+
const path = buffer.path;
|
|
230
|
+
const snapshot = await request;
|
|
231
|
+
if (tabs.get(token.target.tabId) !== token.owner || token.owner.viewer !== buffer
|
|
232
|
+
|| buffer.path !== path || buffer.gitChanges !== placeholder || !isTargetAlive(token.target)) return;
|
|
233
|
+
buffer.gitChanges = snapshot;
|
|
234
|
+
// Navigation changes the foreground generation, but not this live buffer.
|
|
235
|
+
if (viewer === buffer && activeTabId === token.target.tabId) onChange("diff");
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function open(path = "", { gitCategory = "", gitPath = "" } = {}) {
|
|
239
|
+
const normalized = normalizePath(path);
|
|
240
|
+
if (!normalized) return { success: false, reason: "path" };
|
|
241
|
+
const token = beginOpen();
|
|
242
|
+
if (!token) return { success: false, reason: "target" };
|
|
243
|
+
let changesRequest;
|
|
244
|
+
let category;
|
|
245
|
+
let changesPath;
|
|
246
|
+
try {
|
|
247
|
+
const declined = await mayReplace(token, normalized);
|
|
248
|
+
if (!isOpenCurrent(token)) return { success: false, reason: "stale" };
|
|
249
|
+
if (declined) return declined;
|
|
250
|
+
onSelectPath(normalized);
|
|
251
|
+
onStatus(`Opening ${normalized}…`);
|
|
252
|
+
category = gitFileDiffCategory(gitCategory);
|
|
253
|
+
changesPath = normalizePath(gitPath) || normalized;
|
|
254
|
+
const requestSerial = category ? ++diffSerial : 0;
|
|
255
|
+
changesRequest = category ? loadDiff(changesPath, category, token.target.tabId) : null;
|
|
256
|
+
const response = await api(fileApiPath("/api/files/content", normalized), { tabId: token.target.tabId });
|
|
257
|
+
if (!isOpenCurrent(token)) return { success: false, reason: "stale" };
|
|
258
|
+
const data = response.data || {};
|
|
259
|
+
const kind = data.kind === "image" ? "image" : "text";
|
|
260
|
+
const imageUrl = kind === "image" ? imageDataUrl(data) : "";
|
|
261
|
+
if (kind === "image" && !imageUrl) throw new Error("Image preview data is invalid or unsupported");
|
|
262
|
+
const gitChanges = changesRequest ? {
|
|
263
|
+
category, path: changesPath, label: "", command: "", diff: "", truncated: false,
|
|
264
|
+
capBytes: 0, loading: true, requestSerial, error: "",
|
|
265
|
+
} : null;
|
|
266
|
+
const next = {
|
|
267
|
+
path: normalizePath(data.path || normalized), name: data.name || fileDisplayName(normalized),
|
|
268
|
+
content: kind === "text" ? String(data.content || "") : "",
|
|
269
|
+
size: Number(data.size) || 0, mtimeMs: Number(data.mtimeMs) || 0, extension: data.extension || "",
|
|
270
|
+
language: kind === "text" ? data.language || "text" : "image", kind,
|
|
271
|
+
mimeType: kind === "image" ? String(data.mimeType || "") : "", imageUrl,
|
|
272
|
+
mode: gitChanges ? "changes" : kind === "image" ? "image" : data.language === "markdown" ? "preview" : "source",
|
|
273
|
+
dirty: false, readOnly: kind === "image", sourceAvailable: kind === "text", gitChanges,
|
|
274
|
+
};
|
|
275
|
+
publishOpen(next);
|
|
276
|
+
onStatus(kind === "image" ? "Image opened in WebUI (read-only)." : "Opened in WebUI.", "success");
|
|
277
|
+
if (changesRequest) void applyDiff(changesRequest, token, next, gitChanges);
|
|
278
|
+
return { success: true, viewer: next };
|
|
279
|
+
} catch (error) {
|
|
280
|
+
if (!isOpenCurrent(token)) return { success: false, reason: "stale" };
|
|
281
|
+
// Deleted tracked files can still show a bounded diff without live source.
|
|
282
|
+
const gitChanges = changesRequest ? await changesRequest : null;
|
|
283
|
+
if (!isOpenCurrent(token)) return { success: false, reason: "stale" };
|
|
284
|
+
if (gitChanges && !gitChanges.error) {
|
|
285
|
+
const next = {
|
|
286
|
+
path: normalized, name: fileDisplayName(normalized), content: "", size: 0, mtimeMs: 0,
|
|
287
|
+
extension: fileExtensionForPath(normalized), language: fileLanguageForPath(normalized),
|
|
288
|
+
mode: "changes", dirty: false, readOnly: true, sourceAvailable: false, gitChanges,
|
|
289
|
+
};
|
|
290
|
+
publishOpen(next);
|
|
291
|
+
onStatus("File content unavailable; showing Git changes (read-only).", "warn");
|
|
292
|
+
return { success: true, viewer: next };
|
|
293
|
+
}
|
|
294
|
+
const message = error.message || String(error);
|
|
295
|
+
onStatus(message, "error");
|
|
296
|
+
addEvent(`file open failed: ${message}`, "error");
|
|
297
|
+
return { success: false, reason: "error", error };
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async function openReport(path = "", repoRoot = "") {
|
|
302
|
+
const normalized = normalizePath(path);
|
|
303
|
+
const expectedRepoRoot = String(repoRoot || "");
|
|
304
|
+
if (!normalized || !expectedRepoRoot) return { success: false, reason: "path" };
|
|
305
|
+
const token = beginOpen();
|
|
306
|
+
if (!token) return { success: false, reason: "target" };
|
|
307
|
+
try {
|
|
308
|
+
const declined = await mayReplace(token, normalized);
|
|
309
|
+
if (!isOpenCurrent(token)) return { success: false, reason: "stale" };
|
|
310
|
+
if (declined) return declined;
|
|
311
|
+
onStatus(`Opening ${normalized}…`);
|
|
312
|
+
const params = new URLSearchParams({ path: normalized, repoRoot: expectedRepoRoot });
|
|
313
|
+
const response = await api(`/api/aur-review/report-content?${params.toString()}`, { tabId: token.target.tabId });
|
|
314
|
+
if (!isOpenCurrent(token)) return { success: false, reason: "stale" };
|
|
315
|
+
const data = response.data || {};
|
|
316
|
+
const next = {
|
|
317
|
+
path: normalizePath(data.path || normalized), name: data.name || fileDisplayName(normalized),
|
|
318
|
+
content: String(data.content || ""), size: Number(data.size) || 0, mtimeMs: Number(data.mtimeMs) || 0,
|
|
319
|
+
extension: data.extension || "", language: data.language || "text",
|
|
320
|
+
mode: data.language === "markdown" ? "preview" : "source", dirty: false, readOnly: true,
|
|
321
|
+
};
|
|
322
|
+
publishOpen(next);
|
|
323
|
+
onStatus("Report opened in WebUI (read-only).", "success");
|
|
324
|
+
return { success: true, viewer: next };
|
|
325
|
+
} catch (error) {
|
|
326
|
+
if (!isOpenCurrent(token)) return { success: false, reason: "stale" };
|
|
327
|
+
const message = error.message || String(error);
|
|
328
|
+
onStatus(message, "error");
|
|
329
|
+
addEvent(`review report open failed: ${message}`, "error");
|
|
330
|
+
return { success: false, reason: "error", error };
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
async function save(content) {
|
|
335
|
+
if (!viewer) return { success: false, reason: "empty" };
|
|
336
|
+
if (viewer.readOnly) {
|
|
337
|
+
onStatus("This report is read-only.", "info");
|
|
338
|
+
return { success: false, reason: "read-only" };
|
|
339
|
+
}
|
|
340
|
+
const target = captureTarget();
|
|
341
|
+
if (!isForeground(target)) return { success: false, reason: "target" };
|
|
342
|
+
const owner = tabs.get(target.tabId);
|
|
343
|
+
const buffer = viewer;
|
|
344
|
+
const path = buffer.path;
|
|
345
|
+
const originalContent = buffer.content;
|
|
346
|
+
const submitted = String(content ?? "");
|
|
347
|
+
const token = {};
|
|
348
|
+
saves.set(buffer, token);
|
|
349
|
+
const isCurrent = () => tabs.get(target.tabId) === owner && owner?.viewer === buffer
|
|
350
|
+
&& viewer === buffer && buffer.path === path && saves.get(buffer) === token && isForeground(target);
|
|
351
|
+
onStatus(`Saving ${path}…`);
|
|
352
|
+
try {
|
|
353
|
+
const response = await api("/api/files/content", {
|
|
354
|
+
method: "POST", body: { path, content: submitted, mtimeMs: buffer.mtimeMs }, tabId: target.tabId,
|
|
355
|
+
});
|
|
356
|
+
if (!isCurrent()) return { success: false, reason: "stale" };
|
|
357
|
+
const data = response.data || {};
|
|
358
|
+
const currentContent = buffer.content === originalContent ? submitted : buffer.content;
|
|
359
|
+
Object.assign(buffer, data, {
|
|
360
|
+
path: normalizePath(data.path || path), content: currentContent, dirty: currentContent !== submitted,
|
|
361
|
+
size: currentContent === submitted ? Number(data.size) || 0 : new Blob([currentContent]).size,
|
|
362
|
+
});
|
|
363
|
+
cache();
|
|
364
|
+
onChange("save");
|
|
365
|
+
await onRefreshDirectory(buffer.path, target);
|
|
366
|
+
if (!isCurrent()) return { success: false, reason: "stale" };
|
|
367
|
+
onStatus(buffer.dirty ? "Unsaved file edits." : "Saved.", buffer.dirty ? "warn" : "success");
|
|
368
|
+
return { success: true, viewer: buffer };
|
|
369
|
+
} catch (error) {
|
|
370
|
+
if (!isCurrent()) return { success: false, reason: "stale" };
|
|
371
|
+
onStatus(error.message || String(error), "error");
|
|
372
|
+
return { success: false, reason: "error", error };
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return {
|
|
377
|
+
get viewer() { return viewer; },
|
|
378
|
+
get selection() { return selection; },
|
|
379
|
+
get tabIds() { return [...tabs].filter(([, owner]) => owner.viewer || owner.selection).map(([tabId]) => tabId); },
|
|
380
|
+
activate, cache, resetActive, close, forgetTab, setContent, setDirty, setMode,
|
|
381
|
+
setSelection, setEditorContinuity, remapPath, open, openReport, save,
|
|
382
|
+
};
|
|
383
|
+
}
|