forked from Emanuele-web04/synara
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffPanel.logic.ts
More file actions
68 lines (62 loc) · 2.73 KB
/
Copy pathDiffPanel.logic.ts
File metadata and controls
68 lines (62 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// FILE: DiffPanel.logic.ts
// Purpose: Resolve the thread context the diff panel should use across server-backed and local draft chats.
// Exports: resolveDiffPanelThread, resolveDiffSelectAllArmed
// Depends on: ChatView.logic draft-thread normalization.
import { DEFAULT_MODEL_BY_PROVIDER, type ModelSelection, type ThreadId } from "@t3tools/contracts";
import type { DraftThreadState } from "../composerDraftStore";
import type { Thread } from "../types";
import { buildLocalDraftThread } from "./ChatView.logic";
// Reuse the chat-view draft fallback so diff surfaces keep working before the first server turn exists.
export function resolveDiffPanelThread(input: {
threadId: ThreadId | null | undefined;
serverThread: Thread | undefined;
draftThread: DraftThreadState | null | undefined;
fallbackModelSelection: ModelSelection | null | undefined;
}): Thread | undefined {
if (input.serverThread) {
return input.serverThread;
}
if (!input.threadId || !input.draftThread) {
return undefined;
}
return buildLocalDraftThread(
input.threadId,
input.draftThread,
input.fallbackModelSelection ?? {
provider: "codex",
model: DEFAULT_MODEL_BY_PROVIDER.codex,
},
null,
);
}
// Track whether the diff viewport is in a "select all then copy" gesture so the copy
// handler can substitute the full serialized diff instead of the few mounted rows the
// virtualizer left in the DOM. Pure so it can be unit tested without a real DOM.
//
// The diff surface renders into shadow DOM, so a native Cmd/Ctrl+A actually selects the
// surrounding light-DOM page and the resulting `copy` event never travels through the
// viewport element. We instead listen on `document`: the keydown still passes through the
// viewport (so we can tell the select-all happened there), and this state machine decides
// whether the very next copy should be hijacked.
export function resolveDiffSelectAllArmed(
previous: boolean,
event: Pick<KeyboardEvent, "key" | "metaKey" | "ctrlKey">,
isWithinDiffViewport: boolean,
): boolean {
const key = event.key.toLowerCase();
const hasShortcutModifier = event.metaKey || event.ctrlKey;
// Cmd/Ctrl+A arms the gesture, but only when it happens inside the diff viewport.
if (hasShortcutModifier && key === "a") {
return isWithinDiffViewport;
}
// Cmd/Ctrl+C is the copy half of the gesture — preserve whatever state we were in.
if (hasShortcutModifier && key === "c") {
return previous;
}
// Bare modifier keydowns precede the real shortcut keys; never disarm on them.
if (key === "meta" || key === "control" || key === "shift" || key === "alt") {
return previous;
}
// Any other key starts a fresh selection intent, so drop back to native copy behavior.
return false;
}