@juicesharp/rpiv-ask-user-question 1.0.7 → 1.0.9
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/package.json +2 -1
- package/state/build-questionnaire.ts +214 -140
- package/state/key-router.ts +2 -1
- package/state/questionnaire-session.ts +10 -17
- package/state/selectors/contract.ts +23 -0
- package/state/selectors/projections.ts +34 -100
- package/state/state-reducer.ts +170 -144
- package/state/state.ts +2 -0
- package/view/component-binding.ts +10 -74
- package/view/props-adapter.ts +2 -6
- package/view/tab-components.ts +6 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@juicesharp/rpiv-ask-user-question",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "Pi extension: lets the model ask you a clarifying question with structured options instead of guessing",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"state/key-router.ts",
|
|
36
36
|
"state/questionnaire-session.ts",
|
|
37
37
|
"state/row-intent.ts",
|
|
38
|
+
"state/selectors/contract.ts",
|
|
38
39
|
"state/selectors/derivations.ts",
|
|
39
40
|
"state/selectors/focus.ts",
|
|
40
41
|
"state/selectors/projections.ts",
|
|
@@ -11,14 +11,17 @@ import { ChatRowView } from "../view/components/chat-row-view.js";
|
|
|
11
11
|
import { MultiSelectView } from "../view/components/multi-select-view.js";
|
|
12
12
|
import { OptionListView } from "../view/components/option-list-view.js";
|
|
13
13
|
import { PreviewBlockRenderer } from "../view/components/preview/preview-block-renderer.js";
|
|
14
|
+
import type { PreviewPaneProps } from "../view/components/preview/preview-pane.js";
|
|
14
15
|
import { PreviewPane } from "../view/components/preview/preview-pane.js";
|
|
15
16
|
import { SubmitPicker } from "../view/components/submit-picker.js";
|
|
16
17
|
import { TabBar } from "../view/components/tab-bar.js";
|
|
17
18
|
import type { WrappingSelectItem, WrappingSelectTheme } from "../view/components/wrapping-select.js";
|
|
18
19
|
import { DialogView } from "../view/dialog-builder.js";
|
|
19
20
|
import { QuestionnairePropsAdapter } from "../view/props-adapter.js";
|
|
20
|
-
import type {
|
|
21
|
+
import type { StatefulView } from "../view/stateful-view.js";
|
|
22
|
+
import type { TabBodyHeights, TabComponents } from "../view/tab-components.js";
|
|
21
23
|
import type { InputBuffer } from "./input-buffer.js";
|
|
24
|
+
import type { PerTabSelector } from "./selectors/contract.js";
|
|
22
25
|
import { selectActivePreviewPaneIndex } from "./selectors/derivations.js";
|
|
23
26
|
import {
|
|
24
27
|
selectChatRowProps,
|
|
@@ -49,6 +52,28 @@ export interface QuestionnaireBuilt {
|
|
|
49
52
|
invalidate: () => void;
|
|
50
53
|
}
|
|
51
54
|
|
|
55
|
+
interface HeightComputers {
|
|
56
|
+
global: (width: number) => number;
|
|
57
|
+
current: (width: number) => number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function previewBodyHeights(pane: PreviewPane): (width: number) => TabBodyHeights {
|
|
61
|
+
return (width) => ({
|
|
62
|
+
current: pane.naturalHeight(width),
|
|
63
|
+
max: pane.maxNaturalHeight(width),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function multiSelectBodyHeights(view: MultiSelectView): (width: number) => TabBodyHeights {
|
|
68
|
+
return (width) => {
|
|
69
|
+
const h = view.naturalHeight(width);
|
|
70
|
+
return { current: h, max: h };
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const isActiveTab: PerTabSelector<boolean> = (s, ctx) =>
|
|
75
|
+
ctx.i === selectActivePreviewPaneIndex(s.currentTab, ctx.totalQuestions);
|
|
76
|
+
|
|
52
77
|
/**
|
|
53
78
|
* Pure factory: assembles every TUI component, the props adapter, and a
|
|
54
79
|
* lifecycle handle. Session-state dependencies arrive via `getCurrentTab` and
|
|
@@ -57,159 +82,208 @@ export interface QuestionnaireBuilt {
|
|
|
57
82
|
* no selector is invoked here.
|
|
58
83
|
*/
|
|
59
84
|
export function buildQuestionnaire(config: QuestionnaireBuildConfig): QuestionnaireBuilt {
|
|
60
|
-
|
|
61
|
-
|
|
85
|
+
return new QuestionnaireBuilder(config).build();
|
|
86
|
+
}
|
|
62
87
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Programming-by-intention assembly: each private method names one
|
|
90
|
+
* construction step from `build()`. Read top-down; the class is build-only and
|
|
91
|
+
* discarded once the handle is returned.
|
|
92
|
+
*/
|
|
93
|
+
class QuestionnaireBuilder {
|
|
94
|
+
private readonly tui: QuestionnaireBuildConfig["tui"];
|
|
95
|
+
private readonly theme: Theme;
|
|
96
|
+
private readonly questions: readonly QuestionData[];
|
|
97
|
+
private readonly itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
|
|
98
|
+
private readonly isMulti: boolean;
|
|
99
|
+
private readonly initialState: QuestionnaireState;
|
|
100
|
+
private readonly inputBuffer: InputBuffer;
|
|
101
|
+
private readonly getCurrentTab: () => number;
|
|
68
102
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
103
|
+
private readonly selectTheme: WrappingSelectTheme;
|
|
104
|
+
private readonly markdownTheme = getMarkdownTheme();
|
|
105
|
+
private readonly chatRow: ChatRowView;
|
|
106
|
+
private readonly notesInput = new Input();
|
|
107
|
+
private readonly getTerminalWidth = () => this.tui.terminal.columns;
|
|
74
108
|
|
|
75
|
-
|
|
76
|
-
|
|
109
|
+
constructor(config: QuestionnaireBuildConfig) {
|
|
110
|
+
this.tui = config.tui;
|
|
111
|
+
this.theme = config.theme;
|
|
112
|
+
this.questions = config.questions;
|
|
113
|
+
this.itemsByTab = config.itemsByTab;
|
|
114
|
+
this.isMulti = config.isMulti;
|
|
115
|
+
this.initialState = config.initialState;
|
|
116
|
+
this.inputBuffer = config.inputBuffer;
|
|
117
|
+
this.getCurrentTab = config.getCurrentTab;
|
|
118
|
+
|
|
119
|
+
this.selectTheme = this.makeSelectTheme();
|
|
120
|
+
this.chatRow = new ChatRowView({
|
|
121
|
+
item: { kind: "chat", label: SENTINEL_LABELS.chat },
|
|
122
|
+
theme: this.selectTheme,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
77
125
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
126
|
+
build(): QuestionnaireBuilt {
|
|
127
|
+
const tabs = this.buildTabComponents();
|
|
128
|
+
const submitPicker = this.buildSubmitPicker();
|
|
129
|
+
const tabBar = this.buildTabBar();
|
|
130
|
+
const heights = this.buildHeightComputers(tabs);
|
|
131
|
+
const dialog = this.buildDialog(tabs, submitPicker, tabBar, heights);
|
|
132
|
+
const globalBindings = this.buildGlobalBindings(dialog, submitPicker, tabBar);
|
|
133
|
+
const perTabBindings = this.buildPerTabBindings();
|
|
134
|
+
const adapter = this.buildAdapter(tabs, globalBindings, perTabBindings);
|
|
135
|
+
return this.handle(adapter, dialog);
|
|
136
|
+
}
|
|
82
137
|
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
138
|
+
private makeSelectTheme(): WrappingSelectTheme {
|
|
139
|
+
const t = this.theme;
|
|
140
|
+
return {
|
|
141
|
+
selectedText: (s) => t.fg("accent", t.bold(s)),
|
|
142
|
+
description: (s) => t.fg("muted", s),
|
|
143
|
+
scrollInfo: (s) => t.fg("dim", s),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private buildTabComponents(): ReadonlyArray<TabComponents> {
|
|
148
|
+
return this.questions.map((q, i) => this.buildTabFor(q, i));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private buildTabFor(question: QuestionData, index: number): TabComponents {
|
|
152
|
+
const optionList = new OptionListView({
|
|
153
|
+
items: this.itemsByTab[index] ?? [],
|
|
154
|
+
theme: this.selectTheme,
|
|
155
|
+
});
|
|
156
|
+
const previewBlock = new PreviewBlockRenderer({
|
|
157
|
+
question,
|
|
158
|
+
theme: this.theme,
|
|
159
|
+
markdownTheme: this.markdownTheme,
|
|
160
|
+
});
|
|
86
161
|
const preview = new PreviewPane({
|
|
87
|
-
question
|
|
88
|
-
getTerminalWidth,
|
|
162
|
+
question,
|
|
163
|
+
getTerminalWidth: this.getTerminalWidth,
|
|
89
164
|
optionListView: optionList,
|
|
90
165
|
previewBlock,
|
|
91
166
|
});
|
|
92
|
-
const multiSelect =
|
|
167
|
+
const multiSelect = question.multiSelect ? new MultiSelectView(this.theme, question) : undefined;
|
|
168
|
+
const bodyHeights = this.buildBodyHeights(question, preview, multiSelect);
|
|
169
|
+
return { optionList, preview, multiSelect, bodyHeights };
|
|
170
|
+
}
|
|
93
171
|
|
|
94
|
-
|
|
95
|
-
|
|
172
|
+
private buildBodyHeights(
|
|
173
|
+
question: QuestionData,
|
|
174
|
+
preview: PreviewPane,
|
|
175
|
+
multiSelect: MultiSelectView | undefined,
|
|
176
|
+
): (width: number) => TabBodyHeights {
|
|
177
|
+
return question.multiSelect ? multiSelectBodyHeights(multiSelect!) : previewBodyHeights(preview);
|
|
178
|
+
}
|
|
96
179
|
|
|
97
|
-
|
|
98
|
-
|
|
180
|
+
private buildSubmitPicker(): SubmitPicker | undefined {
|
|
181
|
+
return this.isMulti ? new SubmitPicker(this.theme) : undefined;
|
|
182
|
+
}
|
|
99
183
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const computeGlobalContentHeight = (width: number): number => {
|
|
104
|
-
let max = 0;
|
|
105
|
-
for (let i = 0; i < questions.length; i++) {
|
|
106
|
-
const q = questions[i];
|
|
107
|
-
const h = q?.multiSelect
|
|
108
|
-
? (multiSelectViewsByTab[i]?.naturalHeight(width) ?? 0)
|
|
109
|
-
: (previewPanesByTab[i]?.maxNaturalHeight(width) ?? 0);
|
|
110
|
-
if (h > max) max = h;
|
|
111
|
-
}
|
|
112
|
-
return Math.max(1, max);
|
|
113
|
-
};
|
|
114
|
-
const computeCurrentContentHeight = (width: number): number => {
|
|
115
|
-
const idx = Math.min(getCurrentTab(), questions.length - 1);
|
|
116
|
-
const q = questions[idx];
|
|
117
|
-
if (!q) return 0;
|
|
118
|
-
const h = q.multiSelect
|
|
119
|
-
? (multiSelectViewsByTab[idx]?.naturalHeight(width) ?? 0)
|
|
120
|
-
: (previewPanesByTab[idx]?.naturalHeight(width) ?? 0);
|
|
121
|
-
return Math.max(0, h);
|
|
122
|
-
};
|
|
184
|
+
private buildTabBar(): TabBar | undefined {
|
|
185
|
+
return this.isMulti ? new TabBar(this.theme) : undefined;
|
|
186
|
+
}
|
|
123
187
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
},
|
|
140
|
-
{ state: initialState, activePreviewPane: initialActivePreviewPane },
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
const globalBindings: ReadonlyArray<BoundGlobalBinding> = [
|
|
144
|
-
globalBinding({
|
|
145
|
-
component: dialog,
|
|
146
|
-
select: (s, ctx) => selectDialogProps(s, ctx.activePreviewPane),
|
|
147
|
-
}),
|
|
148
|
-
globalBinding({
|
|
149
|
-
component: chatRow,
|
|
150
|
-
select: (s, ctx) => selectChatRowProps(s, ctx.itemsByTab, ctx.totalQuestions, ctx.activeView),
|
|
151
|
-
}),
|
|
152
|
-
...(submitPicker
|
|
153
|
-
? [
|
|
154
|
-
globalBinding({
|
|
155
|
-
component: submitPicker,
|
|
156
|
-
select: (s, ctx) => selectSubmitPickerProps(s, ctx.totalQuestions, ctx.activeView),
|
|
157
|
-
}),
|
|
158
|
-
]
|
|
159
|
-
: []),
|
|
160
|
-
...(tabBar
|
|
161
|
-
? [
|
|
162
|
-
globalBinding({
|
|
163
|
-
component: tabBar,
|
|
164
|
-
select: (s, ctx) => selectTabBarProps(s, ctx.questions),
|
|
165
|
-
}),
|
|
166
|
-
]
|
|
167
|
-
: []),
|
|
168
|
-
];
|
|
169
|
-
|
|
170
|
-
const isActiveTab = (s: QuestionnaireState, ctx: { i: number; totalQuestions: number }): boolean => {
|
|
171
|
-
const paneIdx = ctx.totalQuestions <= 0 ? 0 : Math.min(s.currentTab, ctx.totalQuestions - 1);
|
|
172
|
-
return ctx.i === paneIdx;
|
|
173
|
-
};
|
|
188
|
+
private buildHeightComputers(tabs: ReadonlyArray<TabComponents>): HeightComputers {
|
|
189
|
+
const global = (width: number): number => {
|
|
190
|
+
let max = 0;
|
|
191
|
+
for (const tab of tabs) {
|
|
192
|
+
const h = tab.bodyHeights(width).max;
|
|
193
|
+
if (h > max) max = h;
|
|
194
|
+
}
|
|
195
|
+
return Math.max(1, max);
|
|
196
|
+
};
|
|
197
|
+
const current = (width: number): number => {
|
|
198
|
+
const idx = Math.min(this.getCurrentTab(), tabs.length - 1);
|
|
199
|
+
return Math.max(0, tabs[idx]?.bodyHeights(width).current ?? 0);
|
|
200
|
+
};
|
|
201
|
+
return { global, current };
|
|
202
|
+
}
|
|
174
203
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
204
|
+
private pickInitialActivePreview(tabs: ReadonlyArray<TabComponents>): StatefulView<PreviewPaneProps> {
|
|
205
|
+
const idx = selectActivePreviewPaneIndex(this.initialState.currentTab, this.questions.length);
|
|
206
|
+
return tabs[idx]?.preview ?? tabs[0]!.preview;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
private buildDialog(
|
|
210
|
+
tabs: ReadonlyArray<TabComponents>,
|
|
211
|
+
submitPicker: SubmitPicker | undefined,
|
|
212
|
+
tabBar: TabBar | undefined,
|
|
213
|
+
heights: HeightComputers,
|
|
214
|
+
): DialogView {
|
|
215
|
+
return new DialogView(
|
|
216
|
+
{
|
|
217
|
+
theme: this.theme,
|
|
218
|
+
questions: this.questions,
|
|
219
|
+
tabBar,
|
|
220
|
+
notesInput: this.notesInput,
|
|
221
|
+
chatRow: this.chatRow,
|
|
222
|
+
isMulti: this.isMulti,
|
|
223
|
+
tabsByIndex: tabs,
|
|
224
|
+
submitPicker,
|
|
225
|
+
getBodyHeight: heights.global,
|
|
226
|
+
getCurrentBodyHeight: heights.current,
|
|
194
227
|
},
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const adapter = new QuestionnairePropsAdapter({
|
|
199
|
-
tui,
|
|
200
|
-
questions,
|
|
201
|
-
itemsByTab,
|
|
202
|
-
tabsByIndex,
|
|
203
|
-
inputBuffer,
|
|
204
|
-
globalBindings,
|
|
205
|
-
perTabBindings,
|
|
206
|
-
extraInvalidatables: [notesInput],
|
|
207
|
-
});
|
|
228
|
+
{ state: this.initialState, activePreviewPane: this.pickInitialActivePreview(tabs) },
|
|
229
|
+
);
|
|
230
|
+
}
|
|
208
231
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
232
|
+
private buildGlobalBindings(
|
|
233
|
+
dialog: DialogView,
|
|
234
|
+
submitPicker: SubmitPicker | undefined,
|
|
235
|
+
tabBar: TabBar | undefined,
|
|
236
|
+
): ReadonlyArray<BoundGlobalBinding> {
|
|
237
|
+
return [
|
|
238
|
+
globalBinding({ component: dialog, select: selectDialogProps }),
|
|
239
|
+
globalBinding({ component: this.chatRow, select: selectChatRowProps }),
|
|
240
|
+
...(submitPicker ? [globalBinding({ component: submitPicker, select: selectSubmitPickerProps })] : []),
|
|
241
|
+
...(tabBar ? [globalBinding({ component: tabBar, select: selectTabBarProps })] : []),
|
|
242
|
+
];
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private buildPerTabBindings(): ReadonlyArray<BoundPerTabBinding> {
|
|
246
|
+
return [
|
|
247
|
+
perTabBinding({
|
|
248
|
+
resolve: (tab) => tab.optionList,
|
|
249
|
+
predicate: isActiveTab,
|
|
250
|
+
select: selectOptionListProps,
|
|
251
|
+
}),
|
|
252
|
+
perTabBinding({
|
|
253
|
+
resolve: (tab) => tab.preview,
|
|
254
|
+
predicate: isActiveTab,
|
|
255
|
+
select: selectPreviewPaneProps,
|
|
256
|
+
}),
|
|
257
|
+
perTabBinding({
|
|
258
|
+
resolve: (tab) => tab.multiSelect,
|
|
259
|
+
select: selectMultiSelectProps,
|
|
260
|
+
}),
|
|
261
|
+
];
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private buildAdapter(
|
|
265
|
+
tabs: ReadonlyArray<TabComponents>,
|
|
266
|
+
globalBindings: ReadonlyArray<BoundGlobalBinding>,
|
|
267
|
+
perTabBindings: ReadonlyArray<BoundPerTabBinding>,
|
|
268
|
+
): QuestionnairePropsAdapter {
|
|
269
|
+
return new QuestionnairePropsAdapter({
|
|
270
|
+
tui: this.tui,
|
|
271
|
+
questions: this.questions,
|
|
272
|
+
itemsByTab: this.itemsByTab,
|
|
273
|
+
tabsByIndex: tabs,
|
|
274
|
+
inputBuffer: this.inputBuffer,
|
|
275
|
+
globalBindings,
|
|
276
|
+
perTabBindings,
|
|
277
|
+
extraInvalidatables: [this.notesInput],
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private handle(adapter: QuestionnairePropsAdapter, dialog: DialogView): QuestionnaireBuilt {
|
|
282
|
+
return {
|
|
283
|
+
adapter,
|
|
284
|
+
notesInput: this.notesInput,
|
|
285
|
+
render: (w) => dialog.render(w),
|
|
286
|
+
invalidate: () => adapter.invalidate(),
|
|
287
|
+
};
|
|
288
|
+
}
|
|
215
289
|
}
|
package/state/key-router.ts
CHANGED
|
@@ -28,6 +28,7 @@ export type QuestionnaireAction =
|
|
|
28
28
|
* `[chat, option0, …, optionLast]`.
|
|
29
29
|
*/
|
|
30
30
|
| { kind: "focus_options"; optionIndex: number }
|
|
31
|
+
| { kind: "notes_forward"; data: string }
|
|
31
32
|
| { kind: "ignore" };
|
|
32
33
|
|
|
33
34
|
export interface QuestionnaireKeybindings {
|
|
@@ -149,7 +150,7 @@ export function routeKey(data: string, state: QuestionnaireState, runtime: Quest
|
|
|
149
150
|
if (state.notesVisible) {
|
|
150
151
|
if (kb.matches(data, KEYBIND_CANCEL)) return { kind: "notes_exit" };
|
|
151
152
|
if (kb.matches(data, KEYBIND_CONFIRM)) return { kind: "notes_exit" };
|
|
152
|
-
return { kind: "
|
|
153
|
+
return { kind: "notes_forward", data };
|
|
153
154
|
}
|
|
154
155
|
|
|
155
156
|
if (state.chatFocused) {
|
|
@@ -39,6 +39,7 @@ function initialState(): QuestionnaireState {
|
|
|
39
39
|
notesByTab: new Map(),
|
|
40
40
|
focusedOptionHasPreview: false,
|
|
41
41
|
submitChoiceIndex: 0,
|
|
42
|
+
notesDraft: "",
|
|
42
43
|
};
|
|
43
44
|
}
|
|
44
45
|
|
|
@@ -96,23 +97,7 @@ export class QuestionnaireSession {
|
|
|
96
97
|
this.viewAdapter.apply(this.state);
|
|
97
98
|
}
|
|
98
99
|
|
|
99
|
-
/**
|
|
100
|
-
* Single dispatch entry point. Two-pass when `notesVisible` is active — once to probe
|
|
101
|
-
* for `notes_exit`, then forward to the Input on every other key. The head-guard pattern
|
|
102
|
-
* is load-bearing (any non-Esc/Enter key must reach `Input.handleInput`).
|
|
103
|
-
*/
|
|
104
100
|
dispatch(data: string): void {
|
|
105
|
-
if (this.state.notesVisible) {
|
|
106
|
-
const preAction = routeKey(data, this.state, this.runtime());
|
|
107
|
-
if (preAction.kind === "notes_exit") {
|
|
108
|
-
this.commit(preAction);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
this.notesInput.handleInput(data);
|
|
112
|
-
this.tui.requestRender();
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
101
|
const action = routeKey(data, this.state, this.runtime());
|
|
117
102
|
if (action.kind === "ignore") {
|
|
118
103
|
this.handleIgnoreInline(data);
|
|
@@ -125,9 +110,15 @@ export class QuestionnaireSession {
|
|
|
125
110
|
const result = reduce(this.state, action, this.applyContext());
|
|
126
111
|
this.state = result.state;
|
|
127
112
|
for (const effect of result.effects) this.runEffect(effect);
|
|
113
|
+
this.state = this.mirrorNotesDraft(this.state);
|
|
128
114
|
this.viewAdapter.apply(this.state);
|
|
129
115
|
}
|
|
130
116
|
|
|
117
|
+
private mirrorNotesDraft(s: QuestionnaireState): QuestionnaireState {
|
|
118
|
+
const draft = this.notesInput.getValue();
|
|
119
|
+
return s.notesDraft === draft ? s : { ...s, notesDraft: draft };
|
|
120
|
+
}
|
|
121
|
+
|
|
131
122
|
private runEffect(effect: Effect): void {
|
|
132
123
|
switch (effect.kind) {
|
|
133
124
|
case "set_input_buffer":
|
|
@@ -142,6 +133,9 @@ export class QuestionnaireSession {
|
|
|
142
133
|
case "set_notes_focused":
|
|
143
134
|
this.notesInput.focused = effect.focused;
|
|
144
135
|
return;
|
|
136
|
+
case "forward_notes_keystroke":
|
|
137
|
+
this.notesInput.handleInput(effect.data);
|
|
138
|
+
return;
|
|
145
139
|
case "done":
|
|
146
140
|
this.done(effect.result);
|
|
147
141
|
return;
|
|
@@ -179,7 +173,6 @@ export class QuestionnaireSession {
|
|
|
179
173
|
return {
|
|
180
174
|
questions: this.questions,
|
|
181
175
|
itemsByTab: this.itemsByTab,
|
|
182
|
-
pendingNotesValue: this.notesInput.getValue().trim(),
|
|
183
176
|
};
|
|
184
177
|
}
|
|
185
178
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { QuestionData } from "../../tool/types.js";
|
|
2
|
+
import type { PreviewPaneProps } from "../../view/components/preview/preview-pane.js";
|
|
3
|
+
import type { WrappingSelectItem } from "../../view/components/wrapping-select.js";
|
|
4
|
+
import type { ActiveView, StatefulView } from "../../view/stateful-view.js";
|
|
5
|
+
import type { TabComponents } from "../../view/tab-components.js";
|
|
6
|
+
import type { QuestionnaireState } from "../state.js";
|
|
7
|
+
|
|
8
|
+
export interface BindingContext {
|
|
9
|
+
readonly questions: readonly QuestionData[];
|
|
10
|
+
readonly itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
|
|
11
|
+
readonly totalQuestions: number;
|
|
12
|
+
readonly activeView: ActiveView;
|
|
13
|
+
readonly inputBuffer: string;
|
|
14
|
+
readonly activePreviewPane: StatefulView<PreviewPaneProps>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface PerTabBindingContext extends BindingContext {
|
|
18
|
+
readonly tab: TabComponents;
|
|
19
|
+
readonly i: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type GlobalSelector<P> = (state: QuestionnaireState, ctx: BindingContext) => P;
|
|
23
|
+
export type PerTabSelector<P> = (state: QuestionnaireState, ctx: PerTabBindingContext) => P;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { QuestionData } from "../../tool/types.js";
|
|
2
1
|
import { SENTINEL_LABELS } from "../../tool/types.js";
|
|
3
2
|
import type { ChatRowViewProps } from "../../view/components/chat-row-view.js";
|
|
4
3
|
import { MULTI_SUBMIT_LABEL, type MultiSelectViewProps } from "../../view/components/multi-select-view.js";
|
|
@@ -6,19 +5,14 @@ import type { OptionListViewProps } from "../../view/components/option-list-view
|
|
|
6
5
|
import type { PreviewPaneProps } from "../../view/components/preview/preview-pane.js";
|
|
7
6
|
import type { SubmitPickerProps } from "../../view/components/submit-picker.js";
|
|
8
7
|
import type { TabBarProps } from "../../view/components/tab-bar.js";
|
|
9
|
-
import type { WrappingSelectItem } from "../../view/components/wrapping-select.js";
|
|
10
8
|
import type { DialogProps } from "../../view/dialog-builder.js";
|
|
11
|
-
import type {
|
|
12
|
-
import type { QuestionnaireState } from "../state.js";
|
|
9
|
+
import type { GlobalSelector, PerTabSelector } from "./contract.js";
|
|
13
10
|
import { chatNumberingFor, selectActiveTabItems, selectConfirmedIndicator } from "./derivations.js";
|
|
14
11
|
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
question:
|
|
18
|
-
activeView
|
|
19
|
-
isLastQuestion: boolean,
|
|
20
|
-
): MultiSelectViewProps {
|
|
21
|
-
const focused = activeView === "options";
|
|
12
|
+
export const selectMultiSelectProps: PerTabSelector<MultiSelectViewProps> = (state, ctx) => {
|
|
13
|
+
const question = ctx.questions[ctx.i];
|
|
14
|
+
if (!question) return { rows: [], nextActive: false, nextLabel: SENTINEL_LABELS.next };
|
|
15
|
+
const focused = ctx.activeView === "options";
|
|
22
16
|
const rows: { checked: boolean; active: boolean }[] = [];
|
|
23
17
|
for (let i = 0; i < question.options.length; i++) {
|
|
24
18
|
rows.push({
|
|
@@ -27,79 +21,41 @@ export function selectMultiSelectProps(
|
|
|
27
21
|
});
|
|
28
22
|
}
|
|
29
23
|
const nextActive = focused && state.optionIndex === question.options.length;
|
|
24
|
+
const isLastQuestion = ctx.i === ctx.questions.length - 1;
|
|
30
25
|
const nextLabel = isLastQuestion ? MULTI_SUBMIT_LABEL : SENTINEL_LABELS.next;
|
|
31
26
|
return { rows, nextActive, nextLabel };
|
|
32
|
-
}
|
|
27
|
+
};
|
|
33
28
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*/
|
|
39
|
-
export function selectOptionListProps(
|
|
40
|
-
state: QuestionnaireState,
|
|
41
|
-
items: readonly WrappingSelectItem[],
|
|
42
|
-
questions: readonly QuestionData[],
|
|
43
|
-
activeView: ActiveView,
|
|
44
|
-
inputBuffer: string,
|
|
45
|
-
): OptionListViewProps {
|
|
46
|
-
const focused = activeView === "options";
|
|
47
|
-
const confirmed = selectConfirmedIndicator(questions, state.currentTab, state.answers, items);
|
|
29
|
+
export const selectOptionListProps: PerTabSelector<OptionListViewProps> = (state, ctx) => {
|
|
30
|
+
const items = ctx.itemsByTab[ctx.i] ?? [];
|
|
31
|
+
const focused = ctx.activeView === "options";
|
|
32
|
+
const confirmed = selectConfirmedIndicator(ctx.questions, state.currentTab, state.answers, items);
|
|
48
33
|
return {
|
|
49
34
|
selectedIndex: state.optionIndex,
|
|
50
35
|
focused,
|
|
51
|
-
inputBuffer,
|
|
36
|
+
inputBuffer: ctx.inputBuffer,
|
|
52
37
|
...(confirmed ? { confirmed } : {}),
|
|
53
38
|
};
|
|
54
|
-
}
|
|
39
|
+
};
|
|
55
40
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
* Cancel); only `active` per row varies. Focus derives from the
|
|
59
|
-
* `activeView === "submit"` discriminant.
|
|
60
|
-
*/
|
|
61
|
-
export function selectSubmitPickerProps(
|
|
62
|
-
state: QuestionnaireState,
|
|
63
|
-
totalQuestions: number,
|
|
64
|
-
activeView: ActiveView,
|
|
65
|
-
): SubmitPickerProps {
|
|
66
|
-
const focused = activeView === "submit";
|
|
67
|
-
void totalQuestions;
|
|
41
|
+
export const selectSubmitPickerProps: GlobalSelector<SubmitPickerProps> = (state, ctx) => {
|
|
42
|
+
const focused = ctx.activeView === "submit";
|
|
68
43
|
return {
|
|
69
44
|
rows: [
|
|
70
45
|
{ active: focused && state.submitChoiceIndex === 0 },
|
|
71
46
|
{ active: focused && state.submitChoiceIndex === 1 },
|
|
72
47
|
],
|
|
73
48
|
};
|
|
74
|
-
}
|
|
49
|
+
};
|
|
75
50
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
* canonical state per tick.
|
|
82
|
-
*/
|
|
83
|
-
export function selectPreviewPaneProps(state: QuestionnaireState, activeView: ActiveView): PreviewPaneProps {
|
|
84
|
-
return {
|
|
85
|
-
notesVisible: state.notesVisible,
|
|
86
|
-
selectedIndex: state.optionIndex,
|
|
87
|
-
focused: activeView === "options",
|
|
88
|
-
};
|
|
89
|
-
}
|
|
51
|
+
export const selectPreviewPaneProps: PerTabSelector<PreviewPaneProps> = (state, ctx) => ({
|
|
52
|
+
notesVisible: state.notesVisible,
|
|
53
|
+
selectedIndex: state.optionIndex,
|
|
54
|
+
focused: ctx.activeView === "options",
|
|
55
|
+
});
|
|
90
56
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
* (`allAnswered`, `answered`, `isActive`, `submitActive`) out of `tab-bar.ts`
|
|
94
|
-
* into the selector. The Submit slot is an explicit `submit` field, not a
|
|
95
|
-
* hidden `totalTabs` index. `Q{n}` fallback label is computed here once per
|
|
96
|
-
* tab; `header` reads directly from question data.
|
|
97
|
-
*/
|
|
98
|
-
export function selectTabBarProps(
|
|
99
|
-
state: QuestionnaireState,
|
|
100
|
-
questions: ReadonlyArray<{ header?: string; question: string }>,
|
|
101
|
-
): TabBarProps {
|
|
102
|
-
const tabs = questions.map((q, i) => ({
|
|
57
|
+
export const selectTabBarProps: GlobalSelector<TabBarProps> = (state, ctx) => {
|
|
58
|
+
const tabs = ctx.questions.map((q, i) => ({
|
|
103
59
|
label: q.header && q.header.length > 0 ? q.header : `Q${i + 1}`,
|
|
104
60
|
answered: state.answers.has(i),
|
|
105
61
|
active: i === state.currentTab,
|
|
@@ -107,43 +63,21 @@ export function selectTabBarProps(
|
|
|
107
63
|
return {
|
|
108
64
|
tabs,
|
|
109
65
|
submit: {
|
|
110
|
-
active: state.currentTab === questions.length,
|
|
111
|
-
allAnswered: state.answers.size === questions.length && questions.length > 0,
|
|
66
|
+
active: state.currentTab === ctx.questions.length,
|
|
67
|
+
allAnswered: state.answers.size === ctx.questions.length && ctx.questions.length > 0,
|
|
112
68
|
},
|
|
113
69
|
};
|
|
114
|
-
}
|
|
70
|
+
};
|
|
115
71
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
* the numbering derivation (`chatNumberingFor` over the active tab's
|
|
119
|
-
* items). `activeView === "chat"` is observably equivalent to
|
|
120
|
-
* `state.chatFocused` because the dispatcher cascade and reducer's defensive
|
|
121
|
-
* clears ensure `chatFocused` and `notesVisible`/Submit-tab cannot be true
|
|
122
|
-
* simultaneously.
|
|
123
|
-
*/
|
|
124
|
-
export function selectChatRowProps(
|
|
125
|
-
state: QuestionnaireState,
|
|
126
|
-
itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>,
|
|
127
|
-
totalQuestions: number,
|
|
128
|
-
activeView: ActiveView,
|
|
129
|
-
): ChatRowViewProps {
|
|
130
|
-
const activeItems = selectActiveTabItems(itemsByTab, state.currentTab, totalQuestions);
|
|
72
|
+
export const selectChatRowProps: GlobalSelector<ChatRowViewProps> = (state, ctx) => {
|
|
73
|
+
const activeItems = selectActiveTabItems(ctx.itemsByTab, state.currentTab, ctx.totalQuestions);
|
|
131
74
|
return {
|
|
132
|
-
focused: activeView === "chat",
|
|
75
|
+
focused: ctx.activeView === "chat",
|
|
133
76
|
numbering: chatNumberingFor(activeItems),
|
|
134
77
|
};
|
|
135
|
-
}
|
|
78
|
+
};
|
|
136
79
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
* passed in as a `StatefulView<PreviewPaneProps>` reference — not a derived
|
|
142
|
-
* state value — so the selector signature takes it as a separate arg.
|
|
143
|
-
*/
|
|
144
|
-
export function selectDialogProps(
|
|
145
|
-
state: QuestionnaireState,
|
|
146
|
-
activePreviewPane: StatefulView<PreviewPaneProps>,
|
|
147
|
-
): DialogProps {
|
|
148
|
-
return { state, activePreviewPane };
|
|
149
|
-
}
|
|
80
|
+
export const selectDialogProps: GlobalSelector<DialogProps> = (state, ctx) => ({
|
|
81
|
+
state,
|
|
82
|
+
activePreviewPane: ctx.activePreviewPane,
|
|
83
|
+
});
|
package/state/state-reducer.ts
CHANGED
|
@@ -5,18 +5,10 @@ import { ROW_INTENT_META } from "./row-intent.js";
|
|
|
5
5
|
import { computeFocusedOptionHasPreview } from "./selectors/derivations.js";
|
|
6
6
|
import type { QuestionnaireState } from "./state.js";
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* Per-dispatch context that doesn't live on canonical state. Captured by the runtime each
|
|
10
|
-
* tick (`pendingNotesValue` is `notesInput.getValue().trim()`; `itemsByTab` is constant for
|
|
11
|
-
* the session lifetime). The reducer never touches a live component — every IO call is
|
|
12
|
-
* emitted as an `Effect` in the result.
|
|
13
|
-
*/
|
|
8
|
+
/** Session-lifetime constants. No live-component reads — peripheral values live on canonical state. */
|
|
14
9
|
export interface ApplyContext {
|
|
15
10
|
questions: readonly QuestionData[];
|
|
16
11
|
itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
|
|
17
|
-
/** Trimmed notes input value at dispatch time. The reducer uses this for `notes_exit`
|
|
18
|
-
* to decide whether to merge notes into the current answer or delete pending notes. */
|
|
19
|
-
pendingNotesValue: string;
|
|
20
12
|
}
|
|
21
13
|
|
|
22
14
|
/**
|
|
@@ -30,6 +22,7 @@ export type Effect =
|
|
|
30
22
|
| { kind: "clear_input_buffer" }
|
|
31
23
|
| { kind: "set_notes_value"; value: string }
|
|
32
24
|
| { kind: "set_notes_focused"; focused: boolean }
|
|
25
|
+
| { kind: "forward_notes_keystroke"; data: string }
|
|
33
26
|
| { kind: "done"; result: QuestionnaireResult };
|
|
34
27
|
|
|
35
28
|
export interface ApplyResult {
|
|
@@ -96,6 +89,7 @@ function persistMultiSelectAnswer(state: QuestionnaireState, ctx: ApplyContext):
|
|
|
96
89
|
}
|
|
97
90
|
|
|
98
91
|
function switchTabResult(state: QuestionnaireState, nextTab: number, ctx: ApplyContext): ApplyResult {
|
|
92
|
+
const notesValue = state.notesByTab.get(nextTab) ?? state.answers.get(nextTab)?.notes ?? "";
|
|
99
93
|
const transitioned: QuestionnaireState = {
|
|
100
94
|
...state,
|
|
101
95
|
currentTab: nextTab,
|
|
@@ -105,9 +99,9 @@ function switchTabResult(state: QuestionnaireState, nextTab: number, ctx: ApplyC
|
|
|
105
99
|
chatFocused: false,
|
|
106
100
|
submitChoiceIndex: 0,
|
|
107
101
|
multiSelectChecked: syncMultiSelectFromAnswers(state.answers, ctx.questions, nextTab),
|
|
102
|
+
notesDraft: notesValue,
|
|
108
103
|
};
|
|
109
104
|
const finalState = withFocusedOptionHasPreview(transitioned, ctx.questions);
|
|
110
|
-
const notesValue = state.notesByTab.get(nextTab) ?? state.answers.get(nextTab)?.notes ?? "";
|
|
111
105
|
return {
|
|
112
106
|
state: finalState,
|
|
113
107
|
effects: [
|
|
@@ -123,142 +117,174 @@ function doneFor(state: QuestionnaireState, ctx: ApplyContext, cancelled: boolea
|
|
|
123
117
|
}
|
|
124
118
|
|
|
125
119
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* Actions NOT routed through this reducer (handled by the runtime directly):
|
|
130
|
-
* - `ignore` — per-keystroke input-buffer mutation; no canonical-state change. The runtime
|
|
131
|
-
* mutates the session-owned `InputBuffer` cell directly via `handleIgnoreInline`.
|
|
132
|
-
* - `notes_visible` two-pass forward — the runtime probes the dispatcher for `notes_exit`;
|
|
133
|
-
* every other key forwards to the pi-tui `Input` without reducer involvement.
|
|
120
|
+
* Per-kind handler signature: action payload narrows to the matching union member
|
|
121
|
+
* via `Extract`, so handlers consume fully-typed actions without `as` casts.
|
|
134
122
|
*/
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
const pendingNotes = state.notesByTab.get(answer.questionIndex);
|
|
167
|
-
if (pendingNotes && pendingNotes.length > 0) {
|
|
168
|
-
answer = { ...answer, notes: pendingNotes };
|
|
169
|
-
}
|
|
170
|
-
const answers = new Map(state.answers);
|
|
171
|
-
answers.set(answer.questionIndex, answer);
|
|
172
|
-
const next: QuestionnaireState = { ...state, answers };
|
|
173
|
-
if (answer.kind === "chat") return doneFor(next, ctx, false);
|
|
174
|
-
if (action.autoAdvanceTab !== undefined) return switchTabResult(next, action.autoAdvanceTab, ctx);
|
|
175
|
-
return doneFor(next, ctx, false);
|
|
176
|
-
}
|
|
177
|
-
case "toggle": {
|
|
178
|
-
const checked = new Set(state.multiSelectChecked);
|
|
179
|
-
if (checked.has(action.index)) checked.delete(action.index);
|
|
180
|
-
else checked.add(action.index);
|
|
181
|
-
const intermediate: QuestionnaireState = { ...state, multiSelectChecked: checked };
|
|
182
|
-
const answers = persistMultiSelectAnswer(intermediate, ctx);
|
|
183
|
-
return { state: { ...intermediate, answers }, effects: [] };
|
|
184
|
-
}
|
|
185
|
-
case "multi_confirm": {
|
|
186
|
-
const q = ctx.questions[state.currentTab];
|
|
187
|
-
if (!q) return { state, effects: [] };
|
|
188
|
-
const pendingNotes = state.notesByTab.get(state.currentTab);
|
|
189
|
-
const answers = new Map(state.answers);
|
|
190
|
-
answers.set(state.currentTab, {
|
|
191
|
-
questionIndex: state.currentTab,
|
|
192
|
-
question: q.question,
|
|
193
|
-
kind: "multi",
|
|
194
|
-
answer: null,
|
|
195
|
-
selected: action.selected,
|
|
196
|
-
...(pendingNotes && pendingNotes.length > 0 ? { notes: pendingNotes } : {}),
|
|
197
|
-
});
|
|
198
|
-
const synced: QuestionnaireState = {
|
|
199
|
-
...state,
|
|
200
|
-
answers,
|
|
201
|
-
multiSelectChecked: syncMultiSelectFromAnswers(answers, ctx.questions, state.currentTab),
|
|
202
|
-
};
|
|
203
|
-
if (action.autoAdvanceTab !== undefined) return switchTabResult(synced, action.autoAdvanceTab, ctx);
|
|
204
|
-
return doneFor(synced, ctx, false);
|
|
205
|
-
}
|
|
206
|
-
case "cancel": {
|
|
207
|
-
return doneFor(state, ctx, true);
|
|
208
|
-
}
|
|
209
|
-
case "notes_enter": {
|
|
210
|
-
const value = state.answers.get(state.currentTab)?.notes ?? "";
|
|
211
|
-
return {
|
|
212
|
-
state: { ...state, notesVisible: true },
|
|
213
|
-
effects: [
|
|
214
|
-
{ kind: "set_notes_value", value },
|
|
215
|
-
{ kind: "set_notes_focused", focused: true },
|
|
216
|
-
],
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
case "notes_exit": {
|
|
220
|
-
const trimmed = ctx.pendingNotesValue;
|
|
221
|
-
const notes = new Map(state.notesByTab);
|
|
222
|
-
const answers = new Map(state.answers);
|
|
223
|
-
if (trimmed.length === 0) {
|
|
224
|
-
notes.delete(state.currentTab);
|
|
225
|
-
const prev = answers.get(state.currentTab);
|
|
226
|
-
if (prev?.notes) {
|
|
227
|
-
const stripped = { ...prev };
|
|
228
|
-
delete (stripped as { notes?: string }).notes;
|
|
229
|
-
answers.set(state.currentTab, stripped);
|
|
230
|
-
}
|
|
231
|
-
} else {
|
|
232
|
-
notes.set(state.currentTab, trimmed);
|
|
233
|
-
const prev = answers.get(state.currentTab);
|
|
234
|
-
if (prev) answers.set(state.currentTab, { ...prev, notes: trimmed });
|
|
235
|
-
}
|
|
236
|
-
return {
|
|
237
|
-
state: { ...state, notesByTab: notes, answers, notesVisible: false },
|
|
238
|
-
effects: [{ kind: "set_notes_focused", focused: false }],
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
case "submit": {
|
|
242
|
-
return doneFor(state, ctx, false);
|
|
243
|
-
}
|
|
244
|
-
case "submit_nav": {
|
|
245
|
-
return { state: { ...state, submitChoiceIndex: action.nextIndex }, effects: [] };
|
|
246
|
-
}
|
|
247
|
-
case "focus_chat": {
|
|
248
|
-
return { state: { ...state, chatFocused: true }, effects: [] };
|
|
249
|
-
}
|
|
250
|
-
case "focus_options": {
|
|
251
|
-
const items = ctx.itemsByTab[state.currentTab] ?? [];
|
|
252
|
-
const focused = items[action.optionIndex];
|
|
253
|
-
const inputMode = focused ? ROW_INTENT_META[focused.kind].activatesInputMode : false;
|
|
254
|
-
const next = withFocusedOptionHasPreview(
|
|
255
|
-
{ ...state, chatFocused: false, optionIndex: action.optionIndex, inputMode },
|
|
256
|
-
ctx.questions,
|
|
257
|
-
);
|
|
258
|
-
return { state: next, effects: inputMode ? [] : [{ kind: "clear_input_buffer" }] };
|
|
123
|
+
type Handler<K extends QuestionnaireAction["kind"]> = (
|
|
124
|
+
state: QuestionnaireState,
|
|
125
|
+
action: Extract<QuestionnaireAction, { kind: K }>,
|
|
126
|
+
ctx: ApplyContext,
|
|
127
|
+
) => ApplyResult;
|
|
128
|
+
|
|
129
|
+
const navHandler: Handler<"nav"> = (state, action, ctx) => {
|
|
130
|
+
const items = ctx.itemsByTab[state.currentTab] ?? [];
|
|
131
|
+
const item = items[action.nextIndex];
|
|
132
|
+
const inputMode = item ? ROW_INTENT_META[item.kind].activatesInputMode : false;
|
|
133
|
+
const next = withFocusedOptionHasPreview({ ...state, optionIndex: action.nextIndex, inputMode }, ctx.questions);
|
|
134
|
+
if (!inputMode) {
|
|
135
|
+
return { state: next, effects: [{ kind: "clear_input_buffer" }] };
|
|
136
|
+
}
|
|
137
|
+
const prior = state.answers.get(state.currentTab);
|
|
138
|
+
if (prior?.kind === "custom" && typeof prior.answer === "string") {
|
|
139
|
+
return { state: next, effects: [{ kind: "set_input_buffer", value: prior.answer }] };
|
|
140
|
+
}
|
|
141
|
+
return { state: next, effects: [] };
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const tabSwitchHandler: Handler<"tab_switch"> = (state, action, ctx) => switchTabResult(state, action.nextTab, ctx);
|
|
145
|
+
|
|
146
|
+
const confirmHandler: Handler<"confirm"> = (state, action, ctx) => {
|
|
147
|
+
let answer = action.answer;
|
|
148
|
+
if (answer.kind === "option" && answer.answer) {
|
|
149
|
+
const q = ctx.questions[answer.questionIndex];
|
|
150
|
+
const matched = q?.options.find((o) => o.label === answer.answer);
|
|
151
|
+
if (matched?.preview && matched.preview.length > 0) {
|
|
152
|
+
answer = { ...answer, preview: matched.preview };
|
|
259
153
|
}
|
|
260
|
-
|
|
261
|
-
|
|
154
|
+
}
|
|
155
|
+
const pendingNotes = state.notesByTab.get(answer.questionIndex);
|
|
156
|
+
if (pendingNotes && pendingNotes.length > 0) {
|
|
157
|
+
answer = { ...answer, notes: pendingNotes };
|
|
158
|
+
}
|
|
159
|
+
const answers = new Map(state.answers);
|
|
160
|
+
answers.set(answer.questionIndex, answer);
|
|
161
|
+
const next: QuestionnaireState = { ...state, answers };
|
|
162
|
+
if (answer.kind === "chat") return doneFor(next, ctx, false);
|
|
163
|
+
if (action.autoAdvanceTab !== undefined) return switchTabResult(next, action.autoAdvanceTab, ctx);
|
|
164
|
+
return doneFor(next, ctx, false);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const toggleHandler: Handler<"toggle"> = (state, action, ctx) => {
|
|
168
|
+
const checked = new Set(state.multiSelectChecked);
|
|
169
|
+
if (checked.has(action.index)) checked.delete(action.index);
|
|
170
|
+
else checked.add(action.index);
|
|
171
|
+
const intermediate: QuestionnaireState = { ...state, multiSelectChecked: checked };
|
|
172
|
+
const answers = persistMultiSelectAnswer(intermediate, ctx);
|
|
173
|
+
return { state: { ...intermediate, answers }, effects: [] };
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const multiConfirmHandler: Handler<"multi_confirm"> = (state, action, ctx) => {
|
|
177
|
+
const q = ctx.questions[state.currentTab];
|
|
178
|
+
if (!q) return { state, effects: [] };
|
|
179
|
+
const pendingNotes = state.notesByTab.get(state.currentTab);
|
|
180
|
+
const answers = new Map(state.answers);
|
|
181
|
+
answers.set(state.currentTab, {
|
|
182
|
+
questionIndex: state.currentTab,
|
|
183
|
+
question: q.question,
|
|
184
|
+
kind: "multi",
|
|
185
|
+
answer: null,
|
|
186
|
+
selected: action.selected,
|
|
187
|
+
...(pendingNotes && pendingNotes.length > 0 ? { notes: pendingNotes } : {}),
|
|
188
|
+
});
|
|
189
|
+
const synced: QuestionnaireState = {
|
|
190
|
+
...state,
|
|
191
|
+
answers,
|
|
192
|
+
multiSelectChecked: syncMultiSelectFromAnswers(answers, ctx.questions, state.currentTab),
|
|
193
|
+
};
|
|
194
|
+
if (action.autoAdvanceTab !== undefined) return switchTabResult(synced, action.autoAdvanceTab, ctx);
|
|
195
|
+
return doneFor(synced, ctx, false);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const notesEnterHandler: Handler<"notes_enter"> = (state, _action, _ctx) => {
|
|
199
|
+
const value = state.answers.get(state.currentTab)?.notes ?? "";
|
|
200
|
+
return {
|
|
201
|
+
state: { ...state, notesVisible: true, notesDraft: value },
|
|
202
|
+
effects: [
|
|
203
|
+
{ kind: "set_notes_value", value },
|
|
204
|
+
{ kind: "set_notes_focused", focused: true },
|
|
205
|
+
],
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const notesExitHandler: Handler<"notes_exit"> = (state, _action, _ctx) => {
|
|
210
|
+
const trimmed = state.notesDraft.trim();
|
|
211
|
+
const notes = new Map(state.notesByTab);
|
|
212
|
+
const answers = new Map(state.answers);
|
|
213
|
+
if (trimmed.length === 0) {
|
|
214
|
+
notes.delete(state.currentTab);
|
|
215
|
+
const prev = answers.get(state.currentTab);
|
|
216
|
+
if (prev?.notes) {
|
|
217
|
+
const stripped = { ...prev };
|
|
218
|
+
delete (stripped as { notes?: string }).notes;
|
|
219
|
+
answers.set(state.currentTab, stripped);
|
|
262
220
|
}
|
|
221
|
+
} else {
|
|
222
|
+
notes.set(state.currentTab, trimmed);
|
|
223
|
+
const prev = answers.get(state.currentTab);
|
|
224
|
+
if (prev) answers.set(state.currentTab, { ...prev, notes: trimmed });
|
|
263
225
|
}
|
|
226
|
+
return {
|
|
227
|
+
state: { ...state, notesByTab: notes, answers, notesVisible: false },
|
|
228
|
+
effects: [{ kind: "set_notes_focused", focused: false }],
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const focusOptionsHandler: Handler<"focus_options"> = (state, action, ctx) => {
|
|
233
|
+
const items = ctx.itemsByTab[state.currentTab] ?? [];
|
|
234
|
+
const focused = items[action.optionIndex];
|
|
235
|
+
const inputMode = focused ? ROW_INTENT_META[focused.kind].activatesInputMode : false;
|
|
236
|
+
const next = withFocusedOptionHasPreview(
|
|
237
|
+
{ ...state, chatFocused: false, optionIndex: action.optionIndex, inputMode },
|
|
238
|
+
ctx.questions,
|
|
239
|
+
);
|
|
240
|
+
return { state: next, effects: inputMode ? [] : [{ kind: "clear_input_buffer" }] };
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const cancelHandler: Handler<"cancel"> = (s, _a, c) => doneFor(s, c, true);
|
|
244
|
+
const submitHandler: Handler<"submit"> = (s, _a, c) => doneFor(s, c, false);
|
|
245
|
+
const submitNavHandler: Handler<"submit_nav"> = (s, a, _c) => ({
|
|
246
|
+
state: { ...s, submitChoiceIndex: a.nextIndex },
|
|
247
|
+
effects: [],
|
|
248
|
+
});
|
|
249
|
+
const focusChatHandler: Handler<"focus_chat"> = (s, _a, _c) => ({
|
|
250
|
+
state: { ...s, chatFocused: true },
|
|
251
|
+
effects: [],
|
|
252
|
+
});
|
|
253
|
+
const notesForwardHandler: Handler<"notes_forward"> = (s, a, _c) => ({
|
|
254
|
+
state: s,
|
|
255
|
+
effects: [{ kind: "forward_notes_keystroke", data: a.data }],
|
|
256
|
+
});
|
|
257
|
+
const ignoreHandler: Handler<"ignore"> = (s, _a, _c) => ({ state: s, effects: [] });
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Compile-time-exhaustive dispatch table. `{ [K in Kind]: Handler<K> }` requires
|
|
261
|
+
* an entry per union member — adding a new `QuestionnaireAction` variant fails to
|
|
262
|
+
* compile here until a handler is registered, mirroring the `Record<RowKind, …>`
|
|
263
|
+
* pattern used by `ROW_INTENT_META`.
|
|
264
|
+
*/
|
|
265
|
+
const HANDLERS: { [K in QuestionnaireAction["kind"]]: Handler<K> } = {
|
|
266
|
+
nav: navHandler,
|
|
267
|
+
tab_switch: tabSwitchHandler,
|
|
268
|
+
confirm: confirmHandler,
|
|
269
|
+
toggle: toggleHandler,
|
|
270
|
+
multi_confirm: multiConfirmHandler,
|
|
271
|
+
cancel: cancelHandler,
|
|
272
|
+
notes_enter: notesEnterHandler,
|
|
273
|
+
notes_exit: notesExitHandler,
|
|
274
|
+
notes_forward: notesForwardHandler,
|
|
275
|
+
submit: submitHandler,
|
|
276
|
+
submit_nav: submitNavHandler,
|
|
277
|
+
focus_chat: focusChatHandler,
|
|
278
|
+
focus_options: focusOptionsHandler,
|
|
279
|
+
ignore: ignoreHandler,
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Pure reducer: (state, action, ctx) → (state, Effect[]). Mirrors `rpiv-todo`'s `applyTaskMutation`.
|
|
284
|
+
* Delegates to `HANDLERS` — per-kind handlers above are pure, named, and individually testable.
|
|
285
|
+
* `ignore` is also handled outside the reducer by `handleIgnoreInline` in the runtime fast path.
|
|
286
|
+
*/
|
|
287
|
+
export function reduce(state: QuestionnaireState, action: QuestionnaireAction, ctx: ApplyContext): ApplyResult {
|
|
288
|
+
const handler = HANDLERS[action.kind] as Handler<typeof action.kind>;
|
|
289
|
+
return handler(state, action as never, ctx);
|
|
264
290
|
}
|
package/state/state.ts
CHANGED
|
@@ -23,6 +23,8 @@ export interface QuestionnaireState {
|
|
|
23
23
|
focusedOptionHasPreview: boolean;
|
|
24
24
|
/** Focused row in the Submit-tab picker (0 = Submit, 1 = Cancel). Reset on tab switch. */
|
|
25
25
|
submitChoiceIndex: number;
|
|
26
|
+
/** Canonical mirror of the in-flight notes editor; runtime mirrors after `forward_notes_keystroke`. */
|
|
27
|
+
notesDraft: string;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
/**
|
|
@@ -1,90 +1,33 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BindingContext,
|
|
3
|
+
GlobalSelector,
|
|
4
|
+
PerTabBindingContext,
|
|
5
|
+
PerTabSelector,
|
|
6
|
+
} from "../state/selectors/contract.js";
|
|
1
7
|
import type { QuestionnaireState } from "../state/state.js";
|
|
2
|
-
import type {
|
|
3
|
-
import type { PreviewPaneProps } from "./components/preview/preview-pane.js";
|
|
4
|
-
import type { WrappingSelectItem } from "./components/wrapping-select.js";
|
|
5
|
-
import type { ActiveView, StatefulView } from "./stateful-view.js";
|
|
8
|
+
import type { StatefulView } from "./stateful-view.js";
|
|
6
9
|
import type { TabComponents } from "./tab-components.js";
|
|
7
10
|
|
|
8
|
-
/**
|
|
9
|
-
* Per-tick context shared across all bindings. Built once at the top of
|
|
10
|
-
* `apply()`; threaded into every projector closure unchanged. Carries every
|
|
11
|
-
* free variable today's selectors take beyond `state` itself.
|
|
12
|
-
*
|
|
13
|
-
* `activePreviewPane` is typed as `StatefulView<PreviewPaneProps>` (not the
|
|
14
|
-
* concrete `PreviewPane` class) so the adapter can thread the resolved pane
|
|
15
|
-
* through without `as unknown as` casts; the dialog and tab-content-strategy
|
|
16
|
-
* consume it as a `Component` only.
|
|
17
|
-
*/
|
|
18
|
-
export interface BindingContext {
|
|
19
|
-
readonly questions: readonly QuestionData[];
|
|
20
|
-
readonly itemsByTab: ReadonlyArray<readonly WrappingSelectItem[]>;
|
|
21
|
-
readonly totalQuestions: number;
|
|
22
|
-
readonly activeView: ActiveView;
|
|
23
|
-
readonly inputBuffer: string;
|
|
24
|
-
readonly activePreviewPane: StatefulView<PreviewPaneProps>;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Per-tab extension of `BindingContext`. The nested per-tab loop builds this
|
|
29
|
-
* once per tab (extending the global ctx with `tab` + `i`) and threads it to
|
|
30
|
-
* every per-tab binding's projector + predicate.
|
|
31
|
-
*/
|
|
32
|
-
export interface PerTabBindingContext extends BindingContext {
|
|
33
|
-
readonly tab: TabComponents;
|
|
34
|
-
readonly i: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Global axis binding spec: paired component + selector, generic in `<P>`. Used
|
|
39
|
-
* at construction sites only; never lands in the adapter's iteration array.
|
|
40
|
-
* `globalBinding(spec)` erases `<P>` via the closure form below — TS verifies
|
|
41
|
-
* `select(s, ctx) → P` matches `component.setProps(P)` at the call site.
|
|
42
|
-
*/
|
|
43
11
|
export interface ComponentBinding<P> {
|
|
44
12
|
readonly component: StatefulView<P>;
|
|
45
|
-
readonly select:
|
|
13
|
+
readonly select: GlobalSelector<P>;
|
|
46
14
|
}
|
|
47
15
|
|
|
48
|
-
/**
|
|
49
|
-
* Per-tab axis binding spec: resolver picks the per-tab component instance
|
|
50
|
-
* from the `TabComponents` slot (returning `undefined` when absent — e.g.,
|
|
51
|
-
* `multiSelect` on non-multi tabs). Optional predicate gates the projection
|
|
52
|
-
* (e.g., `optionList` and `preview` only fire on `i === paneIndex`). Bound
|
|
53
|
-
* via `perTabBinding(spec)` for the same reason as `ComponentBinding`.
|
|
54
|
-
*/
|
|
55
16
|
export interface PerTabBinding<P> {
|
|
56
17
|
readonly resolve: (tab: TabComponents) => StatefulView<P> | undefined;
|
|
57
|
-
readonly select:
|
|
58
|
-
readonly predicate?:
|
|
18
|
+
readonly select: PerTabSelector<P>;
|
|
19
|
+
readonly predicate?: PerTabSelector<boolean>;
|
|
59
20
|
}
|
|
60
21
|
|
|
61
|
-
/**
|
|
62
|
-
* Bound global binding consumed by the adapter's iteration. `<P>` is captured
|
|
63
|
-
* inside the closures returned by `globalBinding`; from the array's
|
|
64
|
-
* perspective each entry is just an `apply` + `invalidate` pair, no generic
|
|
65
|
-
* surface.
|
|
66
|
-
*/
|
|
67
22
|
export interface BoundGlobalBinding {
|
|
68
23
|
apply(state: QuestionnaireState, ctx: BindingContext): void;
|
|
69
24
|
invalidate(): void;
|
|
70
25
|
}
|
|
71
26
|
|
|
72
|
-
/**
|
|
73
|
-
* Bound per-tab binding. Mirrors `BoundGlobalBinding`: the adapter calls
|
|
74
|
-
* `apply(state, perTabCtx)` per tab and the closure handles
|
|
75
|
-
* `predicate → resolve → setProps` internally. Missing per-tab components
|
|
76
|
-
* silently skip.
|
|
77
|
-
*/
|
|
78
27
|
export interface BoundPerTabBinding {
|
|
79
28
|
apply(state: QuestionnaireState, ctx: PerTabBindingContext): void;
|
|
80
29
|
}
|
|
81
30
|
|
|
82
|
-
/**
|
|
83
|
-
* Existential wrapper for a global binding. Inside this generic helper `<P>`
|
|
84
|
-
* is a free type variable, so TS verifies that `spec.select(...) : P` lines
|
|
85
|
-
* up with `spec.component.setProps(P)` at the call site. Once bound into
|
|
86
|
-
* `BoundGlobalBinding` the array is monomorphic.
|
|
87
|
-
*/
|
|
88
31
|
export function globalBinding<P>(spec: ComponentBinding<P>): BoundGlobalBinding {
|
|
89
32
|
return {
|
|
90
33
|
apply: (state, ctx) => spec.component.setProps(spec.select(state, ctx)),
|
|
@@ -92,13 +35,6 @@ export function globalBinding<P>(spec: ComponentBinding<P>): BoundGlobalBinding
|
|
|
92
35
|
};
|
|
93
36
|
}
|
|
94
37
|
|
|
95
|
-
/**
|
|
96
|
-
* Existential wrapper for a per-tab binding. Same trick as `globalBinding`:
|
|
97
|
-
* `<P>` is local to this function, so the predicate, resolver, and selector
|
|
98
|
-
* all check against one `P`. Resolves the component lazily inside `apply` so
|
|
99
|
-
* missing slots (e.g., non-multi `multiSelect`) skip without a guard at the
|
|
100
|
-
* iteration site.
|
|
101
|
-
*/
|
|
102
38
|
export function perTabBinding<P>(spec: PerTabBinding<P>): BoundPerTabBinding {
|
|
103
39
|
return {
|
|
104
40
|
apply: (state, ctx) => {
|
package/view/props-adapter.ts
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
import type { InputBuffer } from "../state/input-buffer.js";
|
|
2
|
+
import type { BindingContext, PerTabBindingContext } from "../state/selectors/contract.js";
|
|
2
3
|
import { selectActivePreviewPaneIndex } from "../state/selectors/derivations.js";
|
|
3
4
|
import { selectActiveView } from "../state/selectors/focus.js";
|
|
4
5
|
import type { QuestionnaireState } from "../state/state.js";
|
|
5
6
|
import type { QuestionData } from "../tool/types.js";
|
|
6
|
-
import type {
|
|
7
|
-
BindingContext,
|
|
8
|
-
BoundGlobalBinding,
|
|
9
|
-
BoundPerTabBinding,
|
|
10
|
-
PerTabBindingContext,
|
|
11
|
-
} from "./component-binding.js";
|
|
7
|
+
import type { BoundGlobalBinding, BoundPerTabBinding } from "./component-binding.js";
|
|
12
8
|
import type { WrappingSelectItem } from "./components/wrapping-select.js";
|
|
13
9
|
import type { TabComponents } from "./tab-components.js";
|
|
14
10
|
|
package/view/tab-components.ts
CHANGED
|
@@ -3,16 +3,14 @@ import type { OptionListViewProps } from "./components/option-list-view.js";
|
|
|
3
3
|
import type { PreviewPaneProps } from "./components/preview/preview-pane.js";
|
|
4
4
|
import type { StatefulView } from "./stateful-view.js";
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* arrays for the height callbacks (`naturalHeight` / `maxNaturalHeight` are
|
|
12
|
-
* not on `StatefulView<P>`).
|
|
13
|
-
*/
|
|
6
|
+
export interface TabBodyHeights {
|
|
7
|
+
current: number;
|
|
8
|
+
max: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
14
11
|
export interface TabComponents {
|
|
15
12
|
optionList: StatefulView<OptionListViewProps>;
|
|
16
13
|
preview: StatefulView<PreviewPaneProps>;
|
|
17
14
|
multiSelect?: StatefulView<MultiSelectViewProps>;
|
|
15
|
+
bodyHeights: (width: number) => TabBodyHeights;
|
|
18
16
|
}
|