vc-extras 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (249) hide show
  1. package/README.md +128 -71
  2. package/agent-install.mjs +159 -0
  3. package/agents/tester.md +23 -23
  4. package/install-extras.mjs +41 -109
  5. package/install-menu.mjs +22 -43
  6. package/install-skills.mjs +112 -112
  7. package/package.json +72 -28
  8. package/plugin-registry.mjs +73 -0
  9. package/plugins/config-ui/index.ts +159 -159
  10. package/plugins/config-ui/lib/arguments.mjs +24 -24
  11. package/plugins/config-ui/lib/documents.mjs +67 -67
  12. package/plugins/config-ui/lib/frontmatter.mjs +91 -91
  13. package/plugins/config-ui/lib/open.mjs +16 -16
  14. package/plugins/config-ui/lib/ownership.mjs +99 -99
  15. package/plugins/config-ui/lib/page.mjs +11 -11
  16. package/plugins/config-ui/lib/patch.mjs +35 -35
  17. package/plugins/config-ui/lib/paths.mjs +14 -14
  18. package/plugins/config-ui/lib/plugin-fields.mjs +86 -86
  19. package/plugins/config-ui/lib/plugin-validation.mjs +28 -28
  20. package/plugins/config-ui/lib/redaction.mjs +8 -8
  21. package/plugins/config-ui/lib/registry.mjs +282 -282
  22. package/plugins/config-ui/lib/secrets.mjs +44 -44
  23. package/plugins/config-ui/lib/server.mjs +250 -250
  24. package/plugins/config-ui/lib/sources.mjs +96 -96
  25. package/plugins/config-ui/lib/state.mjs +156 -156
  26. package/plugins/config-ui/lib/themes.mjs +113 -113
  27. package/plugins/config-ui/lib/writer.mjs +690 -690
  28. package/plugins/config-ui/project-local-owner.ts +106 -106
  29. package/plugins/config-ui/vc-extras.plugin.json +13 -0
  30. package/plugins/config-ui/web/app.mjs +997 -997
  31. package/plugins/config-ui/web/index.html +342 -342
  32. package/plugins/pi-blackhole/vc-extras.plugin.json +12 -0
  33. package/plugins/pi-btw/vc-extras.plugin.json +12 -0
  34. package/plugins/pi-claude-code/vc-extras.plugin.json +12 -0
  35. package/plugins/pi-fff/vc-extras.plugin.json +12 -0
  36. package/plugins/pi-jev-auto-mode/vc-extras.plugin.json +12 -0
  37. package/plugins/pi-mcp-adapter/vc-extras.plugin.json +12 -0
  38. package/plugins/pi-web-access/vc-extras.plugin.json +12 -0
  39. package/plugins/vc-ask/claude-hook.mjs +37 -0
  40. package/plugins/vc-ask/codex-native.mjs +28 -0
  41. package/plugins/vc-ask/host-channel.mjs +75 -0
  42. package/plugins/vc-ask/index.ts +57 -0
  43. package/plugins/vc-ask/native-questions.mjs +33 -0
  44. package/plugins/vc-ask/package.json +10 -0
  45. package/plugins/vc-ask/payload.ts +170 -0
  46. package/plugins/vc-ask/questionnaire/LICENSE.ask-user-question +21 -0
  47. package/plugins/vc-ask/questionnaire/UPSTREAM.md +104 -0
  48. package/plugins/vc-ask/questionnaire/answer-channel.ts +135 -0
  49. package/plugins/vc-ask/questionnaire/config.ts +180 -0
  50. package/plugins/vc-ask/questionnaire/events.ts +154 -0
  51. package/plugins/vc-ask/questionnaire/execute.ts +688 -0
  52. package/plugins/vc-ask/questionnaire/i18n.ts +73 -0
  53. package/plugins/vc-ask/questionnaire/inline-entry.ts +312 -0
  54. package/plugins/vc-ask/questionnaire/locales/de.json +43 -0
  55. package/plugins/vc-ask/questionnaire/locales/en.json +42 -0
  56. package/plugins/vc-ask/questionnaire/locales/es.json +43 -0
  57. package/plugins/vc-ask/questionnaire/locales/fr.json +43 -0
  58. package/plugins/vc-ask/questionnaire/locales/pt-BR.json +43 -0
  59. package/plugins/vc-ask/questionnaire/locales/pt.json +43 -0
  60. package/plugins/vc-ask/questionnaire/locales/ru.json +43 -0
  61. package/plugins/vc-ask/questionnaire/locales/uk.json +43 -0
  62. package/plugins/vc-ask/questionnaire/locales/zh.json +46 -0
  63. package/plugins/vc-ask/questionnaire/presentation.ts +11 -0
  64. package/plugins/vc-ask/questionnaire/question-preview.ts +6 -0
  65. package/plugins/vc-ask/questionnaire/rpc.ts +221 -0
  66. package/plugins/vc-ask/questionnaire/session-timeout.ts +67 -0
  67. package/plugins/vc-ask/questionnaire/state/build-questionnaire.ts +144 -0
  68. package/plugins/vc-ask/questionnaire/state/external-editor.ts +62 -0
  69. package/plugins/vc-ask/questionnaire/state/geometry.ts +131 -0
  70. package/plugins/vc-ask/questionnaire/state/key-router.ts +382 -0
  71. package/plugins/vc-ask/questionnaire/state/panel-router.ts +32 -0
  72. package/plugins/vc-ask/questionnaire/state/questionnaire-session.ts +642 -0
  73. package/plugins/vc-ask/questionnaire/state/row-intent.ts +143 -0
  74. package/plugins/vc-ask/questionnaire/state/selectors/contract.ts +24 -0
  75. package/plugins/vc-ask/questionnaire/state/selectors/derivations.ts +40 -0
  76. package/plugins/vc-ask/questionnaire/state/selectors/focus.ts +17 -0
  77. package/plugins/vc-ask/questionnaire/state/selectors/projections.ts +99 -0
  78. package/plugins/vc-ask/questionnaire/state/state-reducer.ts +455 -0
  79. package/plugins/vc-ask/questionnaire/state/state.ts +90 -0
  80. package/plugins/vc-ask/questionnaire/state/vertical.ts +158 -0
  81. package/plugins/vc-ask/questionnaire/timeout.ts +59 -0
  82. package/plugins/vc-ask/questionnaire/tool/external-answer.ts +103 -0
  83. package/plugins/vc-ask/questionnaire/tool/format-answer.ts +24 -0
  84. package/plugins/vc-ask/questionnaire/tool/normalize-params.ts +37 -0
  85. package/plugins/vc-ask/questionnaire/tool/response-envelope.ts +75 -0
  86. package/plugins/vc-ask/questionnaire/tool/types.ts +74 -0
  87. package/plugins/vc-ask/questionnaire/tool/validate-questionnaire.ts +75 -0
  88. package/plugins/vc-ask/questionnaire/view/card-theme.ts +245 -0
  89. package/plugins/vc-ask/questionnaire/view/components/inline-input.ts +66 -0
  90. package/plugins/vc-ask/questionnaire/view/components/multi-select-view.ts +177 -0
  91. package/plugins/vc-ask/questionnaire/view/components/option-list-view.ts +65 -0
  92. package/plugins/vc-ask/questionnaire/view/components/preview/markdown-content-cache.ts +101 -0
  93. package/plugins/vc-ask/questionnaire/view/components/preview/preview-block-renderer.ts +130 -0
  94. package/plugins/vc-ask/questionnaire/view/components/preview/preview-box-renderer.ts +86 -0
  95. package/plugins/vc-ask/questionnaire/view/components/preview/preview-layout-decider.ts +204 -0
  96. package/plugins/vc-ask/questionnaire/view/components/preview/preview-pane.ts +257 -0
  97. package/plugins/vc-ask/questionnaire/view/components/submit-picker.ts +65 -0
  98. package/plugins/vc-ask/questionnaire/view/components/tab-bar.ts +57 -0
  99. package/plugins/vc-ask/questionnaire/view/components/wrapping-select.ts +269 -0
  100. package/plugins/vc-ask/questionnaire/view/dialog-builder.ts +319 -0
  101. package/plugins/vc-ask/questionnaire/view/document-theme.ts +53 -0
  102. package/plugins/vc-ask/questionnaire/view/document-view.ts +306 -0
  103. package/plugins/vc-ask/questionnaire/view/panel-chrome.ts +133 -0
  104. package/plugins/vc-ask/questionnaire/view/props-adapter.ts +77 -0
  105. package/plugins/vc-ask/questionnaire/view/stateful-view.ts +24 -0
  106. package/plugins/vc-ask/questionnaire/view/tab-components.ts +16 -0
  107. package/plugins/vc-ask/questionnaire/view/tab-content-strategy.ts +280 -0
  108. package/plugins/vc-ask/questionnaire/view/vertical-view.ts +218 -0
  109. package/plugins/vc-ask/user-input-events.ts +34 -0
  110. package/plugins/vc-ask/vc-extras.plugin.json +18 -0
  111. package/plugins/vc-footer/agents/claude-code/adapter.mjs +29 -0
  112. package/plugins/vc-footer/agents/claude-code/cli.mjs +23 -0
  113. package/plugins/vc-footer/agents/claude-code/renderer.mjs +1 -0
  114. package/plugins/vc-footer/agents/claude-code/types.d.ts +38 -0
  115. package/plugins/vc-footer/agents/codex/adapter.mjs +13 -0
  116. package/plugins/vc-footer/agents/codex/mcp.mjs +45 -0
  117. package/plugins/vc-footer/agents/codex/native.mjs +19 -0
  118. package/plugins/vc-footer/agents/codex/renderer.mjs +2 -0
  119. package/plugins/vc-footer/agents/pi/adapter.mjs +23 -0
  120. package/plugins/vc-footer/agents/pi/index.js +84 -0
  121. package/plugins/vc-footer/agents/pi/project-local-owner.mjs +97 -0
  122. package/plugins/vc-footer/agents/pi/renderer.mjs +5 -0
  123. package/plugins/vc-footer/core/data.mjs +218 -0
  124. package/plugins/vc-footer/core/render.mjs +291 -0
  125. package/plugins/vc-footer/core/types.d.ts +29 -0
  126. package/plugins/vc-footer/package.json +22 -0
  127. package/plugins/vc-footer/vc-extras.plugin.json +32 -0
  128. package/skills/animate/LICENSE +21 -21
  129. package/skills/animate/RECIPES.md +324 -324
  130. package/skills/animate/SKILL.md +208 -208
  131. package/skills/animate/UPSTREAM.md +19 -19
  132. package/skills/animation-performance/LICENSE +21 -21
  133. package/skills/animation-performance/SKILL.md +223 -223
  134. package/skills/animation-performance/UPSTREAM.md +19 -19
  135. package/skills/answer-me-with-html/SKILL.md +174 -174
  136. package/skills/answer-me-with-html/scripts/am.mjs +12896 -12896
  137. package/skills/apple-design/LICENSE +21 -21
  138. package/skills/apple-design/SKILL.md +291 -291
  139. package/skills/apple-design/UPSTREAM.md +19 -19
  140. package/skills/bad-smell/SKILL.md +44 -44
  141. package/skills/commit/SKILL.md +90 -90
  142. package/skills/emil-design-eng/LICENSE +21 -21
  143. package/skills/emil-design-eng/SKILL.md +677 -677
  144. package/skills/emil-design-eng/UPSTREAM.md +19 -19
  145. package/skills/find-the-truth/SKILL.md +38 -38
  146. package/skills/find-the-truth/references/trace-method.md +48 -48
  147. package/skills/find-the-truth/references/truth-method.md +39 -39
  148. package/skills/gestures/LICENSE +21 -21
  149. package/skills/gestures/SKILL.md +107 -107
  150. package/skills/gestures/UPSTREAM.md +19 -19
  151. package/skills/humanizer-zh/LICENSE +21 -21
  152. package/skills/humanizer-zh/SKILL.md +409 -409
  153. package/skills/humanizer-zh/UPSTREAM.md +7 -7
  154. package/skills/impeccable/LICENSE +191 -191
  155. package/skills/impeccable/NOTICE.md +11 -11
  156. package/skills/impeccable/SKILL.md +89 -89
  157. package/skills/impeccable/licenses/modern-screenshot-MIT.txt +8 -8
  158. package/skills/impeccable/licenses/platform-design-skills-MIT.txt +21 -21
  159. package/skills/impeccable/reference/adapt.md +318 -318
  160. package/skills/impeccable/reference/adapt.native.md +58 -58
  161. package/skills/impeccable/reference/android.md +46 -46
  162. package/skills/impeccable/reference/animate.md +89 -89
  163. package/skills/impeccable/reference/audit.md +139 -139
  164. package/skills/impeccable/reference/audit.native.md +139 -139
  165. package/skills/impeccable/reference/bolder.md +33 -33
  166. package/skills/impeccable/reference/clarify.md +94 -94
  167. package/skills/impeccable/reference/colorize.md +86 -86
  168. package/skills/impeccable/reference/component-review.md +63 -63
  169. package/skills/impeccable/reference/craft-floor.md +44 -44
  170. package/skills/impeccable/reference/craft.md +5 -5
  171. package/skills/impeccable/reference/critique.md +819 -819
  172. package/skills/impeccable/reference/degraded/asset-producer.md +41 -41
  173. package/skills/impeccable/reference/degraded/documenter.md +23 -23
  174. package/skills/impeccable/reference/degraded/finish-reviewer.md +37 -37
  175. package/skills/impeccable/reference/degraded/manual-edit-applier.md +92 -92
  176. package/skills/impeccable/reference/delight.md +70 -70
  177. package/skills/impeccable/reference/distill.md +111 -111
  178. package/skills/impeccable/reference/doctor.md +54 -54
  179. package/skills/impeccable/reference/document.md +416 -416
  180. package/skills/impeccable/reference/extract.md +69 -69
  181. package/skills/impeccable/reference/generate.md +101 -101
  182. package/skills/impeccable/reference/harden.md +362 -362
  183. package/skills/impeccable/reference/hooks.md +114 -114
  184. package/skills/impeccable/reference/init.md +131 -131
  185. package/skills/impeccable/reference/ios.md +51 -51
  186. package/skills/impeccable/reference/layout.md +84 -84
  187. package/skills/impeccable/reference/live-setup.md +104 -104
  188. package/skills/impeccable/reference/live.md +327 -327
  189. package/skills/impeccable/reference/mode-operate.md +21 -21
  190. package/skills/impeccable/reference/mode-persuade.md +19 -19
  191. package/skills/impeccable/reference/mode-read.md +21 -21
  192. package/skills/impeccable/reference/new-work.md +152 -152
  193. package/skills/impeccable/reference/onboard.md +234 -234
  194. package/skills/impeccable/reference/operate.md +61 -61
  195. package/skills/impeccable/reference/optimize.md +257 -257
  196. package/skills/impeccable/reference/overdrive.md +127 -127
  197. package/skills/impeccable/reference/polish.md +105 -105
  198. package/skills/impeccable/reference/quieter.md +99 -99
  199. package/skills/impeccable/reference/region-map.md +26 -26
  200. package/skills/impeccable/reference/routing.md +24 -24
  201. package/skills/impeccable/reference/shape.md +59 -59
  202. package/skills/impeccable/reference/typeset.md +90 -90
  203. package/skills/impeccable/reference/visualize.md +47 -47
  204. package/skills/impeccable/scripts/VERSION +1 -1
  205. package/skills/impeccable/scripts/command-metadata.json +98 -98
  206. package/skills/impeccable/scripts/data/font-index-failures.json +120 -120
  207. package/skills/impeccable/scripts/engine-checksums.txt +9 -9
  208. package/skills/impeccable/scripts/impeccable +249 -249
  209. package/skills/impeccable/scripts/impeccable.cmd +278 -278
  210. package/skills/impeccable/scripts/live-browser-dom.js +320 -320
  211. package/skills/impeccable/scripts/live-browser-ignores.js +246 -246
  212. package/skills/impeccable/scripts/live-browser-session.js +183 -183
  213. package/skills/impeccable/scripts/live-browser.js +13862 -13862
  214. package/skills/impeccable/scripts/modern-screenshot.umd.js +14 -14
  215. package/skills/improve-animations/AUDIT.md +115 -115
  216. package/skills/improve-animations/LICENSE +21 -21
  217. package/skills/improve-animations/PLAN-TEMPLATE.md +73 -73
  218. package/skills/improve-animations/SKILL.md +110 -110
  219. package/skills/improve-animations/UPSTREAM.md +19 -19
  220. package/skills/pick-ui-library/LICENSE +21 -21
  221. package/skills/pick-ui-library/SKILL.md +85 -85
  222. package/skills/pick-ui-library/UPSTREAM.md +19 -19
  223. package/skills/review-animations/LICENSE +21 -21
  224. package/skills/review-animations/SKILL.md +120 -120
  225. package/skills/review-animations/STANDARDS.md +187 -187
  226. package/skills/review-animations/UPSTREAM.md +19 -19
  227. package/skills/test/SKILL.md +29 -29
  228. package/skills/test/references/test-method.md +60 -60
  229. package/skills/write-doc/SKILL.md +115 -115
  230. package/skills/write-doc/examples/result-report.md +53 -53
  231. package/skills/write-doc/examples/task-summary.md +45 -45
  232. package/skills/write-doc/references/terminology.md +147 -147
  233. package/specs/ask-host-protocol-v1.md +58 -0
  234. package/specs/design-demo.md +1102 -1102
  235. package/specs/frontend-immersive.md +28 -28
  236. package/specs/plugin-install-v2.md +123 -0
  237. package/specs/plugin-install.md +38 -0
  238. package/specs/web-modes/explore.md +31 -31
  239. package/specs/web-modes/risk.md +15 -15
  240. package/specs/web-modes/verify.md +16 -16
  241. package/vc-extras.mjs +46 -62
  242. package/plugins/essrj/context.ts +0 -47
  243. package/plugins/essrj/data.ts +0 -190
  244. package/plugins/essrj/index.ts +0 -139
  245. package/plugins/essrj/package.json +0 -12
  246. package/plugins/essrj/project-local-owner.ts +0 -106
  247. package/plugins/essrj/render.ts +0 -217
  248. package/plugins/essrj/types.ts +0 -40
  249. package/plugins.json +0 -38
@@ -0,0 +1,642 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+ import * as Tui from "@earendil-works/pi-tui";
3
+ import { CURSOR_MARKER, isKeyRelease, isKeyRepeat, matchesKey, truncateToWidth, type Editor, type TUI } from "@earendil-works/pi-tui";
4
+ import type { QuestionData, QuestionnaireResult, QuestionParams } from "../tool/types.js";
5
+ import type { WrappingSelectItem } from "../view/components/wrapping-select.js";
6
+ import { COLLAPSED_HINT } from "../view/dialog-builder.js";
7
+ import type { DocumentRect, DocumentView, PanelDragKind } from "../view/document-view.js";
8
+ import { clampDockRows, clampFloat, dockCapacity, FLOAT_MIN_COLS, FLOAT_MIN_ROWS, floatPanel, isDocked, type PanelGeometry, type TerminalSize } from "./geometry.js";
9
+ import { buildQuestionnaire } from "./build-questionnaire.js";
10
+ import { floatDefault } from "./geometry.js";
11
+
12
+ import type { VerticalView } from "../view/vertical-view.js";
13
+ import { randomAskPalette } from "../view/panel-chrome.js";
14
+ import { t } from "../i18n.js";
15
+ import { previewScrollAction, type QuestionnaireAction, routeKey } from "./key-router.js";
16
+ import type { QuestionnaireRuntime, QuestionnaireState } from "./state.js";
17
+ import { type ApplyContext, type Effect, reduce } from "./state-reducer.js";
18
+
19
+ export interface QuestionnaireSessionConfig {
20
+ tui: TUI;
21
+ theme: Theme;
22
+ params: QuestionParams;
23
+ itemsByTab: WrappingSelectItem[][];
24
+ done: (result: QuestionnaireResult) => void;
25
+ keybindings: QuestionnaireRuntime["keybindings"];
26
+ /** Opens Pi's configured external editor. Resolve `undefined` on a reported launch failure. */
27
+ editInput: (value: string) => Promise<string | undefined>;
28
+ /** Key spec for the collapse/expand shortcut, e.g. `"ctrl+]"` or `"alt+o"`. */
29
+ collapseKey: string;
30
+ /** Content-frame label for question-level previews; defaults to the locale's neutral summary label. */
31
+ previewLabel?: string;
32
+ document?: boolean;
33
+ vertical?: boolean;
34
+ /** Defaults to true; action-only gates disable notes without disabling free text. */
35
+ allowNotes?: boolean;
36
+ /** Presentation form chosen when opened; Alt+L promotes/maximises a dock. */
37
+ panelForm?: "dock" | "float";
38
+ /** Floating geometry from the user's questionnaire config or this session's last placement. */
39
+ panel?: PanelGeometry;
40
+ panelAutoSize?: boolean;
41
+ /** Dock height to restore, in rows; `null` fits the content within the dock budget. */
42
+ dockRows?: number | null;
43
+ /** Publishes the panel geometry so the host can remember it for this Pi session. */
44
+ onGeometry?: (state: QuestionnaireState) => void;
45
+ /** Reports that a resize stopped at the minimum panel size, for a one-off notice. */
46
+ onLimit?: () => void;
47
+ }
48
+
49
+ export interface DocumentMouseEvent { type: string; button: string; x: number; y: number; wheelDelta?: number; shift?: boolean; alt?: boolean; ctrl?: boolean }
50
+
51
+ /** Actions that change panel geometry; the host persists them for the Pi session. */
52
+ const GEOMETRY_ACTIONS = new Set<QuestionnaireAction["kind"]>([
53
+ "panel_move", "panel_resize", "panel_maximize", "panel_reset", "panel_promote", "dock_resize",
54
+ ]);
55
+
56
+ export interface QuestionnaireSessionComponent {
57
+ focused?: boolean;
58
+ dispose?(): void;
59
+ handleMouse?(event: DocumentMouseEvent): { handled: boolean; capture?: boolean } | undefined;
60
+ render(width: number): string[];
61
+ invalidate(): void;
62
+ handleInput(data: string): void;
63
+ }
64
+
65
+ export function clampRenderedLines(lines: readonly string[], width: number): string[] {
66
+ const maxWidth = Math.max(0, width);
67
+ return lines.map((line) => truncateToWidth(line, maxWidth, ""));
68
+ }
69
+
70
+ function initialState(): QuestionnaireState {
71
+ return {
72
+ currentTab: 0,
73
+ optionIndex: 0,
74
+ inputMode: false,
75
+ notesVisible: false,
76
+ answers: new Map(),
77
+ multiSelectChecked: new Set(),
78
+ customDraftsByTab: new Map(),
79
+ notesByTab: new Map(),
80
+ submitChoiceIndex: 0,
81
+ notesDraft: "",
82
+ previewScrollOffset: 0,
83
+ collapsed: false,
84
+ };
85
+ }
86
+
87
+ /**
88
+ * Slim runtime: owns the canonical state cell, the headless editor cells, the
89
+ * notes-draft mirror, and the effect runner. State
90
+ * transitions go through the pure `reduce` reducer; UI fan-out goes through
91
+ * the `QuestionnairePropsAdapter` produced by `buildQuestionnaire`.
92
+ */
93
+ export class QuestionnaireSession {
94
+ private state: QuestionnaireState = initialState();
95
+ readonly palette = randomAskPalette();
96
+
97
+ private readonly questions: readonly QuestionData[];
98
+ private readonly isMulti: boolean;
99
+ private readonly itemsByTab: WrappingSelectItem[][];
100
+
101
+ private readonly notesInput: Editor;
102
+ private readonly inlineInput: Editor;
103
+ private readonly viewAdapter: ReturnType<typeof buildQuestionnaire>["adapter"];
104
+ private readonly document: DocumentView | undefined;
105
+ private readonly vertical: VerticalView | undefined;
106
+ private readonly allowNotes: boolean;
107
+ private closed = false;
108
+ private epoch = 0;
109
+ private pasting = false;
110
+ private editorWidth = 1;
111
+ private renderCache: { state: QuestionnaireState; key: string; lines: string[] } | undefined;
112
+ private readonly getViewport: ReturnType<typeof buildQuestionnaire>["getViewport"];
113
+ private readonly keybindings: QuestionnaireRuntime["keybindings"];
114
+ private readonly editInput: QuestionnaireSessionConfig["editInput"];
115
+ private readonly collapseKey: string;
116
+ private readonly onGeometry: QuestionnaireSessionConfig["onGeometry"];
117
+ private readonly onLimit: QuestionnaireSessionConfig["onLimit"];
118
+ private inputEditorOpen = false;
119
+
120
+ /** Publishes reducer-driven collapse state to the inline entry renderer. */
121
+ private collapseSink: ((collapsed: boolean) => void) | undefined;
122
+
123
+ private readonly tui: QuestionnaireSessionConfig["tui"];
124
+ private readonly done: QuestionnaireSessionConfig["done"];
125
+ readonly component: QuestionnaireSessionComponent;
126
+
127
+ constructor(config: QuestionnaireSessionConfig) {
128
+ this.tui = config.tui;
129
+ this.done = config.done;
130
+ this.allowNotes = config.allowNotes !== false;
131
+ this.questions = config.params.questions;
132
+ this.isMulti = this.questions.length > 1;
133
+ this.itemsByTab = config.itemsByTab;
134
+ this.keybindings = config.keybindings;
135
+ this.editInput = config.editInput;
136
+ this.collapseKey = config.collapseKey;
137
+ this.onGeometry = config.onGeometry;
138
+ this.onLimit = config.onLimit;
139
+
140
+ // Recommendations are hints, not answers; start every question unanswered.
141
+ this.state = {
142
+ ...this.state,
143
+ panelForm: config.panelForm ?? (config.document ? "float" : "dock"),
144
+ ...(config.vertical ? { verticalScreen: "list" as const, deferredQuestions: new Set<number>() } : {}),
145
+ panelAutoSize: config.panelAutoSize ?? true,
146
+ panel: config.panel ?? floatDefault({ columns: this.tui.terminal.columns, rows: this.tui.terminal.rows }),
147
+ dockRows: config.dockRows ?? null,
148
+ };
149
+ const built = buildQuestionnaire({
150
+ tui: this.tui,
151
+ theme: config.theme,
152
+ palette: this.palette,
153
+ questions: this.questions,
154
+ itemsByTab: this.itemsByTab,
155
+ isMulti: this.isMulti,
156
+ initialState: this.state,
157
+ getCurrentTab: () => this.state.currentTab,
158
+ document: config.document,
159
+ vertical: config.vertical,
160
+ onDockOverflow: () => this.commit({ kind: "panel_promote" }),
161
+ ...(config.previewLabel ? { previewLabel: config.previewLabel } : {}),
162
+ });
163
+
164
+ this.document = built.document;
165
+ this.vertical = built.vertical;
166
+ this.notesInput = built.notesInput;
167
+ this.inlineInput = built.inlineInput;
168
+ this.viewAdapter = built.adapter;
169
+ this.getViewport = built.getViewport;
170
+
171
+ const theme = config.theme;
172
+ // The inline renderer uses this compact row while the session is collapsed.
173
+ const collapsedRender = (_width: number): string[] => [
174
+ theme.fg("dim", ` ${t("hint.expand_line", COLLAPSED_HINT)} `),
175
+ ];
176
+
177
+ let focused = false;
178
+ const owner = this;
179
+ this.component = {
180
+ get focused() { return focused; },
181
+ set focused(value: boolean) { if (focused !== value) owner.epoch++; focused = value; },
182
+ render: (width) => {
183
+ if (!isDocked(this.state) && (this.document || this.vertical)) width = Math.min(width, floatPanel(this.state, this.terminalSize()).w);
184
+ this.editorWidth = width;
185
+ const viewVersion = this.document?.getRenderVersion() ?? this.vertical?.getRenderVersion() ?? "";
186
+ const key = [width, this.tui.terminal.columns, this.tui.terminal.rows, config.theme.appearance, this.component.focused, viewVersion].join(":");
187
+ if (this.renderCache?.state === this.state && this.renderCache.key === key) return this.renderCache.lines;
188
+ if (this.vertical) this.vertical.focused = this.component.focused === true;
189
+ if (this.document) this.document.focused = this.component.focused === true;
190
+ const lines = clampRenderedLines(this.document ? built.render(width) : this.state.collapsed ? collapsedRender(width) : built.render(width), width);
191
+ this.renderCache = { state: this.state, key, lines };
192
+ return lines;
193
+ },
194
+ invalidate: () => { this.renderCache = undefined; built.invalidate(); },
195
+ handleInput: (data) => this.dispatch(data),
196
+ handleMouse: event => this.handleMouse(event),
197
+ dispose: () => this.dispose(),
198
+ };
199
+
200
+ this.viewAdapter.apply(this.state);
201
+ }
202
+
203
+ dispatch(data: string): boolean {
204
+ if (this.closed || this.inputEditorOpen) return false;
205
+ if (this.vertical) {
206
+ if (isKeyRelease(data) || isKeyRepeat(data)) return true;
207
+ const confirmingCancel = this.state.verticalScreen === "review" && this.state.verticalReviewIndex === this.questions.length + 1 && this.keybindings.matches(data, "tui.select.confirm");
208
+ if (!confirmingCancel && !this.keybindings.matches(data, "tui.select.cancel")) this.disarmCancel();
209
+ this.component.render(this.editorWidth);
210
+ if (this.drag && matchesKey(data, "escape")) { this.cancelDrag(); return true; }
211
+ if (this.geometryKey(data)) return true;
212
+ if (matchesKey(data, "pageUp") || matchesKey(data, "pageDown")) {
213
+ this.vertical.scrollList((matchesKey(data, "pageUp") ? -1 : 1) * 5);
214
+ this.tui.requestRender(); return true;
215
+ }
216
+ if (matchesKey(data, "ctrl+pageUp") || matchesKey(data, "ctrl+pageDown")) return this.scrollPreviewPage(matchesKey(data, "ctrl+pageUp") ? -1 : 1);
217
+ if (!this.vertical.actionable && !matchesKey(data, "escape") && !matchesKey(data, "ctrl+c")) return true;
218
+ }
219
+ if (this.document) {
220
+ if (isKeyRelease(data) || isKeyRepeat(data)) return true;
221
+ if (data.includes("\x1b[200~")) this.epoch++;
222
+ this.component.render(this.editorWidth);
223
+ if (this.drag && matchesKey(data, "escape")) { this.cancelDrag(); return true; }
224
+ if (this.geometryKey(data)) return true;
225
+ if (matchesKey(data, "ctrl+r")) { this.document.toggleSource(); this.commit({ kind: "preview_scroll", offset: 0 }); return true; }
226
+ if (matchesKey(data, "ctrl+c")) { this.commit({ kind: "cancel" }); return true; }
227
+ const collapseRequested = this.collapseKey !== "off" && matchesKey(data, this.collapseKey as Parameters<typeof matchesKey>[1]);
228
+ if (!this.document.actionable && !this.state.collapsed && !matchesKey(data, "escape") && !collapseRequested) return true;
229
+ if (this.keybindings.matches(data, "tui.select.confirm") && !this.document.selectionVisible) {
230
+ this.document.revealSelection();
231
+ this.tui.requestRender();
232
+ return true;
233
+ }
234
+ if (matchesKey(data, "alt+pageUp") || matchesKey(data, "alt+pageDown")) {
235
+ this.document.scrollActions(matchesKey(data, "alt+pageUp") ? -1 : 1);
236
+ this.tui.requestRender();
237
+ return true;
238
+ }
239
+ if (matchesKey(data, "ctrl+pageUp") || matchesKey(data, "ctrl+pageDown")) { this.scrollPreviewPage(matchesKey(data, "ctrl+pageUp") ? -1 : 1); return true; }
240
+ }
241
+ const action = routeKey(data, this.state, this.runtime());
242
+ if (action.kind === "notes_enter" && !this.allowNotes) return true;
243
+ if (action.kind === "ignore") {
244
+ // In inputMode the card owns the keystroke (inline text editing); outside it,
245
+ // `ignore` means the card does not want the key, so the caller should let it
246
+ // fall through to the host (e.g. typing in pi's editor).
247
+ const handled = this.handleIgnoreInline(data);
248
+ return handled;
249
+ }
250
+ this.commit(action);
251
+ if (action.kind === "list_edit" && action.input !== undefined) {
252
+ this.handleIgnoreInline(action.input);
253
+ }
254
+ return true;
255
+ }
256
+
257
+ private disarmCancel(): void {
258
+ if (!this.vertical || !this.state.cancelArmed) return;
259
+ this.state = { ...this.state, cancelArmed: false, verticalMessage: "" };
260
+ this.viewAdapter.apply(this.state);
261
+ }
262
+
263
+ private commit(action: QuestionnaireAction): void {
264
+ if (this.state.notesVisible && ["list_focus", "review_open", "submit", "confirm", "nav", "list_defer"].includes(action.kind)) {
265
+ const saved = reduce(this.mirrorNotesDraft(this.state), { kind: "notes_exit" }, this.applyContext());
266
+ this.state = saved.state;
267
+ for (const effect of saved.effects) this.runEffect(effect);
268
+ }
269
+ const reusePosition = action.kind === "panel_move" && this.renderCache?.state === this.state;
270
+ if (GEOMETRY_ACTIONS.has(action.kind) && !isDocked(this.state)) this.state = { ...this.state, panel: this.panelGeometry() };
271
+ const result = reduce(this.state, action, this.applyContext());
272
+ this.state = result.state;
273
+
274
+ if (action.kind === "nav" || action.kind === "notes_enter" || action.kind === "notes_exit" || action.kind === "toggle_collapsed" || action.kind === "cancel") this.epoch++;
275
+ for (const effect of result.effects) this.runEffect(effect);
276
+ this.state = this.mirrorNotesDraft(this.state);
277
+ if (reusePosition && this.renderCache && this.state.panel) {
278
+ const panel = this.state.panel;
279
+ const drawn = this.document?.panelRect() ?? this.vertical?.panelRect();
280
+ if (drawn?.w === panel.w && drawn.h === panel.h) {
281
+ this.document?.reposition(panel); this.vertical?.reposition(panel);
282
+ this.renderCache.state = this.state;
283
+ }
284
+ }
285
+ this.viewAdapter.apply(this.state);
286
+ if (GEOMETRY_ACTIONS.has(action.kind)) this.onGeometry?.(this.state);
287
+ }
288
+
289
+ private editorText(editor: Editor): string {
290
+ return this.document || this.vertical ? editor.getExpandedText() : editor.getText();
291
+ }
292
+
293
+ private mirrorNotesDraft(s: QuestionnaireState): QuestionnaireState {
294
+ const draft = this.editorText(this.notesInput);
295
+ return s.notesDraft === draft ? s : { ...s, notesDraft: draft };
296
+ }
297
+
298
+ private runEffect(effect: Effect): void {
299
+ switch (effect.kind) {
300
+ case "set_input_buffer":
301
+ // Preserve the live paste registry and cursor when returning to the same draft.
302
+ if ((!this.document && !this.vertical) || this.editorText(this.inlineInput) !== effect.value) this.inlineInput.setText(effect.value);
303
+ return;
304
+ case "clear_input_buffer":
305
+ this.inlineInput.setText("");
306
+ return;
307
+ case "open_input_editor":
308
+ if (this.inputEditorOpen) return;
309
+ this.inputEditorOpen = true;
310
+ this.epoch++;
311
+ void this.applyExternalEdit(effect.value);
312
+ return;
313
+ case "set_notes_value":
314
+ if ((!this.document && !this.vertical) || this.editorText(this.notesInput) !== effect.value) this.notesInput.setText(effect.value);
315
+ return;
316
+ case "set_notes_focused":
317
+ this.notesInput.focused = effect.focused;
318
+ return;
319
+ case "forward_notes_keystroke":
320
+ this.renderCache = undefined;
321
+ this.notesInput.handleInput(effect.data);
322
+ return;
323
+ case "set_collapsed":
324
+ this.collapseSink?.(effect.collapsed);
325
+ return;
326
+ case "done":
327
+ this.dispose();
328
+ this.done(effect.result);
329
+ return;
330
+ }
331
+ }
332
+
333
+ /**
334
+ * Per-keystroke `ignore` fast path: delegates text editing to Pi's headless
335
+ * multiline `Editor`, including paste, undo, cursor movement, and configured
336
+ * `tui.input.newLine` handling. `viewAdapter.apply` then projects its public
337
+ * text/cursor state without a reducer round-trip.
338
+ */
339
+ private async applyExternalEdit(value: string): Promise<void> {
340
+ try {
341
+ const edited = await this.editInput(value);
342
+ if (!this.closed && edited !== undefined) this.commit({ kind: "input_replace", value: edited });
343
+ } catch {
344
+ // The host callback reports launch errors; retain the draft and keep the TUI alive.
345
+ } finally {
346
+ this.inputEditorOpen = false;
347
+ }
348
+ }
349
+
350
+ private handleIgnoreInline(data: string): boolean {
351
+ if (this.state.collapsed || !this.state.inputMode) return false;
352
+ this.renderCache = undefined;
353
+ const previousText = this.editorText(this.inlineInput);
354
+ this.inlineInput.handleInput(data);
355
+ if (this.vertical) {
356
+ const value = this.editorText(this.inlineInput);
357
+ const saved = reduce(this.state, {kind: "list_save", value, edited: value !== previousText}, this.applyContext());
358
+ this.state = {...saved.state, inputMode: true, cancelArmed: false, verticalMessage: ""};
359
+ }
360
+ this.viewAdapter.apply(this.state);
361
+ return true;
362
+ }
363
+
364
+ private runtime(): QuestionnaireRuntime {
365
+ const cursor = this.inlineInput.getCursor();
366
+ const lastLine = this.inlineInput.getLines().length - 1;
367
+ return {
368
+ keybindings: this.keybindings,
369
+ inputBuffer: this.editorText(this.inlineInput),
370
+ document: Boolean(this.document),
371
+ vertical: Boolean(this.vertical),
372
+ canMoveInputUp: this.document || this.vertical ? this.canMoveInput(-1) : cursor.line > 0,
373
+ canMoveInputDown: this.document || this.vertical ? this.canMoveInput(1) : cursor.line < lastLine,
374
+ questions: this.questions,
375
+ isMulti: this.isMulti,
376
+ currentItem: this.currentItem(),
377
+ items: this.itemsByTab[this.state.currentTab] ?? [],
378
+ viewport: this.getViewport(),
379
+ collapseKey: this.collapseKey,
380
+ };
381
+ }
382
+
383
+ private applyContext(): ApplyContext {
384
+ return {
385
+ document: Boolean(this.document),
386
+ vertical: Boolean(this.vertical),
387
+ questions: this.questions,
388
+ itemsByTab: this.itemsByTab,
389
+ size: this.terminalSize(),
390
+ };
391
+ }
392
+
393
+ private terminalSize(): TerminalSize {
394
+ return { columns: this.tui.terminal.columns, rows: this.tui.terminal.rows };
395
+ }
396
+
397
+ private currentItem(): WrappingSelectItem | undefined {
398
+ const arr = this.itemsByTab[this.state.currentTab] ?? [];
399
+ return this.state.optionIndex < arr.length ? arr[this.state.optionIndex] : undefined;
400
+ }
401
+
402
+ private canMoveInput(direction: -1 | 1): boolean {
403
+ // Use Editor’s own visual rows, including atomic paste markers. Its public
404
+ // borders indicate rows beyond the visible window; the cursor marker locates
405
+ // the current row without duplicating Editor’s wrapping or paste registry.
406
+ const lines = this.inlineInput.render(this.vertical?.editorWidth ?? this.document?.editorWidth ?? 1);
407
+ const cursorRow = lines.findIndex(line => line.includes(CURSOR_MARKER));
408
+ if (cursorRow < 0) return false;
409
+ return direction < 0 ? cursorRow > 1 || lines[0]!.includes("↑") :
410
+ cursorRow < lines.length - 2 || lines[lines.length - 1]!.includes("↓");
411
+ }
412
+
413
+ dispose(): void { this.closed = true; this.epoch++; this.document?.dispose(); this.vertical?.dispose(); }
414
+
415
+ private handleMouse(event: DocumentMouseEvent): { handled: boolean; capture?: boolean } | undefined {
416
+ if (this.closed || this.inputEditorOpen || !this.component.focused) return;
417
+ if (!this.document && !this.vertical) return undefined;
418
+ if (this.state.collapsed) return undefined;
419
+ const cancelTarget = this.vertical?.targetAt(event.x, event.y);
420
+ if (!(["press", "release", "click"].includes(event.type) && event.button === "left" && cancelTarget?.kind === "cancel")) this.disarmCancel();
421
+ const doc = this.document;
422
+ const hits = (rect: DocumentRect) => event.x >= rect.x && event.x < rect.x + rect.width && event.y >= rect.y && event.y < rect.y + rect.height;
423
+ if (event.type === "wheel") {
424
+ // Wheel isolation: inside the panel it scrolls the questionnaire, outside it falls
425
+ // through to pi's transcript scroll.
426
+ const p = doc?.hitRect() ?? this.vertical?.hitRect();
427
+ if ((this.tui as TUI & { hasActiveSelection?: () => boolean }).hasActiveSelection?.()) return { handled: true };
428
+ if (!p || event.x < p.col || event.x >= p.col + p.w || event.y < p.row || event.y >= p.row + p.h) return undefined;
429
+ const moved = this.vertical && !this.vertical.isPreviewAt(event.x, event.y)
430
+ ? this.vertical.scrollAt(event.x, event.wheelDelta ?? 0) : this.scrollPreviewBy(event.wheelDelta ?? 0);
431
+ if (!moved) return undefined;
432
+ this.tui.requestRender();
433
+ return { handled: true };
434
+ }
435
+ if (event.button === "right" && (event.type === "press" || event.type === "click")) {
436
+ if (!doc) return undefined;
437
+ if (event.type === "press" && hits(doc.inputRect) && (this.state.inputMode || this.state.notesVisible) && !event.shift && !event.ctrl && !event.alt) void this.pasteClipboard();
438
+ return { handled: true };
439
+ }
440
+ if (event.type === "press" && event.button === "left") {
441
+ const kind = doc ? doc.dragKindAt(event.x, event.y) : this.vertical?.dragKindAt(event.x, event.y) ?? null;
442
+ if (kind === "button" || kind === "dock-button") {
443
+ // A card fills the screen; a dock hands the same content to the card form.
444
+ this.commit(kind === "dock-button" ? { kind: "panel_promote" } : { kind: "panel_maximize" });
445
+ return { handled: true };
446
+ }
447
+ if (!kind) {
448
+ const target = this.vertical?.targetAt(event.x, event.y);
449
+ if (this.document) {
450
+ if (!this.document.actionable) return { handled: true };
451
+ const index = this.document.actionAt(event.x, event.y);
452
+ if (index === undefined) return undefined;
453
+ this.commit({ kind: "nav", nextIndex: index, inputValue: this.editorText(this.inlineInput) });
454
+ if (this.itemsByTab[0]?.[index]?.kind === "option") this.dispatch("\r");
455
+ return { handled: true };
456
+ }
457
+ if (!target || !this.vertical?.actionable) return undefined;
458
+ if (target.kind === "submit" || target.kind === "review") this.commit({ kind: target.kind === "submit" ? "submit" : "review_open" });
459
+ else if (target.kind === "cancel") this.commit({ kind: this.state.cancelArmed ? "cancel" : "cancel_arm" });
460
+ else {
461
+ this.commit({ kind: "list_focus", question: target.question, ...(target.kind === "option" ? { index: target.index } : {}) });
462
+ if (target.kind === "option" && target.index === this.questions[target.question]!.options.length + 1) this.commit({ kind: "list_defer" });
463
+ else if (target.kind === "option" && target.index < this.questions[target.question]!.options.length) this.commit({ kind: "list_select", advance: !this.questions[target.question]!.multiSelect });
464
+ }
465
+ return { handled: true };
466
+ }
467
+ this.drag = { kind, x: event.x, y: event.y, panel: this.panelGeometry() };
468
+ return { handled: true, capture: true };
469
+ }
470
+ if (!this.drag) return undefined;
471
+ if (event.type === "drag" || event.type === "move") { this.applyDrag(event.x, event.y); return { handled: true, capture: true }; }
472
+ if (event.type === "release" || event.type === "click") { this.drag = undefined; return { handled: true } };
473
+ return undefined;
474
+ }
475
+
476
+ /** Panel rect in the coordinate space of incoming mouse events. */
477
+ private panelGeometry(): PanelGeometry {
478
+ const size = this.terminalSize();
479
+ const drawn = this.document?.hitRect() ?? this.vertical?.panelRect();
480
+ if (drawn && drawn.w > 0) return drawn;
481
+ return { col: 0, row: 0, w: size.columns, h: Math.min(size.rows, dockCapacity(size)) };
482
+ }
483
+
484
+ private applyDrag(x: number, y: number): void {
485
+ const drag = this.drag;
486
+ if (!drag) return;
487
+ const dCol = x - drag.x, dRow = y - drag.y;
488
+ switch (drag.kind) {
489
+ case "move": this.commit({ kind: "panel_move", col: drag.panel.col + dCol, row: drag.panel.row + dRow }); return;
490
+ case "resize-w": this.resizeTo(drag.panel.w + dCol, drag.panel.h); return;
491
+ case "resize-h": this.resizeTo(drag.panel.w, drag.panel.h + dRow); return;
492
+ case "resize-both": this.resizeTo(drag.panel.w + dCol, drag.panel.h + dRow); return;
493
+ case "dock-resize": this.resizeDock(drag.panel.h - dRow); return;
494
+ default: return;
495
+ }
496
+ }
497
+
498
+ /**
499
+ * Resize and report when this gesture reaches the minimum size for the first time.
500
+ * The comparison uses the clamped request together with the previously drawn panel:
501
+ * the host repaints asynchronously, and repeated attempts at an already-reached floor
502
+ * must not notify again.
503
+ */
504
+ private resizeTo(w: number, h: number): void {
505
+ const size = this.terminalSize();
506
+ // Read the size from state, not from the drawn view: the host repaints asynchronously,
507
+ // so repeated resizes must accumulate here regardless of render timing.
508
+ const previous = isDocked(this.state) ? this.panelGeometry() : floatPanel(this.state, size);
509
+ const clamped = clampFloat({ ...previous, w, h }, size);
510
+ this.commit({ kind: "panel_resize", w, h });
511
+ const floorWidth = Math.min(FLOAT_MIN_COLS, size.columns);
512
+ const floorHeight = Math.min(FLOAT_MIN_ROWS, size.rows);
513
+ this.reportFloor(clamped.w <= floorWidth || clamped.h <= floorHeight, clamped.w > w || clamped.h > h);
514
+ }
515
+
516
+ private resizeDock(rows: number): void {
517
+ const size = this.terminalSize();
518
+ const clamped = clampDockRows(rows, size, Boolean(this.vertical));
519
+ this.commit({ kind: "dock_resize", rows });
520
+ this.reportFloor(clamped <= clampDockRows(0, size, Boolean(this.vertical)), clamped > rows);
521
+ }
522
+
523
+ /** Announces the minimum size once per arrival: leaving the floor re-arms the notice. */
524
+ private reportFloor(atFloor: boolean, clampedRequest: boolean): void {
525
+ if (!atFloor) { this.floorNotified = false; return; }
526
+ if (!clampedRequest || this.floorNotified) return;
527
+ this.floorNotified = true;
528
+ this.onLimit?.();
529
+ }
530
+
531
+ /** `Esc` during a drag rolls the panel back instead of cancelling the questionnaire. */
532
+ private cancelDrag(): void {
533
+ const drag = this.drag;
534
+ if (!drag) return;
535
+ this.drag = undefined;
536
+ if (drag.kind === "dock-resize") this.commit({ kind: "dock_resize", rows: drag.panel.h });
537
+ else {
538
+ this.state = { ...this.state, panel: drag.panel, panelAutoSize: false };
539
+ this.viewAdapter.apply(this.state);
540
+ this.onGeometry?.(this.state);
541
+ }
542
+ }
543
+
544
+ /**
545
+ * Panel keys: `alt+arrows` move/resize (floating only), `alt+shift+arrows` resize,
546
+ * `alt+m` maximises, `alt+0` restores the default geometry. A docked panel only answers
547
+ * `alt+up/down`, which change its height, so `alt+m`/`alt+0` never change its form.
548
+ */
549
+ private geometryKey(data: string): boolean {
550
+ const panel = this.panelGeometry();
551
+ const docked = isDocked(this.state);
552
+ if (matchesKey(data, "alt+l")) { this.commit({ kind: "panel_maximize" }); return true; }
553
+ const vertical = matchesKey(data, "alt+up") || matchesKey(data, "alt+down") ||
554
+ matchesKey(data, "alt+shift+up") || matchesKey(data, "alt+shift+down");
555
+ const horizontal = matchesKey(data, "alt+left") || matchesKey(data, "alt+right") ||
556
+ matchesKey(data, "alt+shift+left") || matchesKey(data, "alt+shift+right");
557
+ const shift = matchesKey(data, "alt+shift+up") || matchesKey(data, "alt+shift+down") ||
558
+ matchesKey(data, "alt+shift+left") || matchesKey(data, "alt+shift+right");
559
+ if (docked) {
560
+ if (!vertical) return false;
561
+ const grow = matchesKey(data, "alt+up") || matchesKey(data, "alt+shift+up") ? 1 : -1;
562
+ this.resizeDock(panel.h + grow * (shift ? 3 : 1));
563
+ return true;
564
+ }
565
+
566
+ if (matchesKey(data, "alt+0")) { this.commit({ kind: "panel_reset" }); return true; }
567
+ if (!vertical && !horizontal) return false;
568
+ if (shift) {
569
+ const dw = horizontal ? (matchesKey(data, "alt+shift+right") ? 2 : -2) : 0;
570
+ const dh = vertical ? (matchesKey(data, "alt+shift+down") ? 2 : -2) : 0;
571
+ this.resizeTo(panel.w + dw, panel.h + dh);
572
+ return true;
573
+ }
574
+ const dcol = horizontal ? (matchesKey(data, "alt+right") ? 1 : -1) : 0;
575
+ const drow = vertical ? (matchesKey(data, "alt+down") ? 1 : -1) : 0;
576
+ this.commit({ kind: "panel_move", col: panel.col + dcol, row: panel.row + drow });
577
+ return true;
578
+ }
579
+
580
+ private drag: { kind: PanelDragKind; x: number; y: number; panel: PanelGeometry } | undefined;
581
+ private floorNotified = false;
582
+
583
+ private async pasteClipboard(): Promise<void> {
584
+ if (this.pasting) return;
585
+ this.pasting = true;
586
+ const epoch = this.epoch;
587
+ try {
588
+ const native = Tui as typeof Tui & { getNativeClipboard?: () => { getText(): Promise<string | null | undefined> } | undefined };
589
+ const text = await native.getNativeClipboard?.()?.getText();
590
+ if (!text || this.closed || this.inputEditorOpen || !this.component.focused || this.epoch !== epoch) return;
591
+ this.renderCache = undefined;
592
+ const editor = this.state.notesVisible ? this.notesInput : this.inlineInput;
593
+ editor.handleInput("\x1b[200~" + text + "\x1b[201~");
594
+ this.state = this.mirrorNotesDraft(this.state);
595
+ this.viewAdapter.apply(this.state);
596
+ } catch { /* Clipboard permissions must never destroy the draft. */ }
597
+ finally { this.pasting = false; }
598
+ }
599
+
600
+ /** `execute()` installs this after creating the live inline entry. */
601
+ setCollapseSink(sink: (collapsed: boolean) => void): void {
602
+ this.collapseSink = sink;
603
+ }
604
+
605
+ /** Scroll a long question-level preview by rendered rows. Returns true when this modal owns the event. */
606
+ scrollPreviewBy(lines: number): boolean {
607
+ if (this.closed || this.inputEditorOpen) return false;
608
+ const runtime = this.runtime();
609
+ if (runtime.document || (runtime.vertical && (this.questions[this.state.currentTab]?.preview || this.questions[this.state.currentTab]?.options.some(option => option.preview)))) {
610
+ const offset = this.state.previewScrollOffset ?? runtime.viewport.scrollStart;
611
+ runtime.viewport = { ...runtime.viewport, scrollStart: Math.max(0, Math.min(runtime.viewport.maxScrollStart, offset)) };
612
+ }
613
+ const action = previewScrollAction(this.state, runtime, lines);
614
+ if (!action) return false;
615
+ this.commit(action);
616
+ return true;
617
+ }
618
+
619
+ /** Scroll a long question-level preview by one viewport, retaining one overlap row. */
620
+ scrollPreviewPage(direction: -1 | 1): boolean {
621
+ const pageSize = Math.max(1, this.getViewport().pageSize - 1);
622
+ return this.scrollPreviewBy(direction * pageSize);
623
+ }
624
+
625
+ /** Reuses the current content layout; pure position changes do not rebuild it. */
626
+ measurePanel(width = this.tui.terminal.columns): PanelGeometry | undefined {
627
+ this.component.render(width);
628
+ return this.getFloatingPanel();
629
+ }
630
+
631
+ /** Host layout reads canonical geometry before rendering the local card. */
632
+ getFloatingPanel(): PanelGeometry | undefined {
633
+ if ((!this.document && !this.vertical) || isDocked(this.state)) return undefined;
634
+ const drawn = this.document?.panelRect() ?? this.vertical?.panelRect();
635
+ return drawn && drawn.w > 0 ? drawn : floatPanel(this.state, this.terminalSize());
636
+ }
637
+
638
+ /** Toggle collapsed state from the terminal input listener through the reducer. */
639
+ toggleCollapsedExternal(): void {
640
+ if (!this.inputEditorOpen) this.commit({ kind: "toggle_collapsed" });
641
+ }
642
+ }