@floless/app 0.172.0 → 0.172.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.
@@ -0,0 +1,378 @@
1
+ // What the profile card says about a profile name (epic #3382 §7.2, slice P2).
2
+ //
3
+ // Pure: given the text in a Profile field, where its caret is and what the model knows, this decides WHICH card to show
4
+ // and what is on it, and builds its markup. It touches no DOM, so every state can be tested without a page. The page
5
+ // code (`steel-profile-card.js`) owns the popover, the timers, the pin and the 3D preview.
6
+ //
7
+ // States (`kind`):
8
+ // none nothing typed: no card
9
+ // library a library profile: its drawing and facts
10
+ // parametric the name reads as exactly one shape: the name split into sizes, the drawing, a field per size
11
+ // ambiguous the name reads more than one way: each reading, none chosen
12
+ // extra the name reads once an extra number is dropped: say so, offer "Use …" or "Keep as typed"
13
+ // incomplete a known shape with a size still to type: name the size that comes next
14
+ // excluded one of Tekla's shapes FloLess lists but does not draw yet: keeps its name
15
+ // unknown no library profile and no shape: stays as typed, with Browse or "Define this shape"
16
+ // loading the name starts like one of Tekla's extra shapes, which load on first use
17
+ // failed that load failed
18
+
19
+ import * as P from './steel-parametric-pick.js';
20
+ import { parametricDrawing } from './steel-parametric-drawing.js';
21
+ import { readParametricName, sizeAtCaret } from './steel-parametric-names.js';
22
+ import { profileFacts, profileSchematic } from './steel-catalog-picker.js';
23
+
24
+ export const esc = (value) => String(value == null ? '' : value).replace(/[&<>"']/g, (c) => ({
25
+ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;',
26
+ })[c]);
27
+
28
+ const isObj = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
29
+ const capital = (text) => (text ? text[0].toUpperCase() + text.slice(1) : text);
30
+
31
+ /**
32
+ * What the card needs to know about the model.
33
+ * @typedef {{
34
+ * definitions: any[], units: 'mm' | 'in', displayUnits: string, material?: string,
35
+ * libraryRow: (text: string) => any, libraryHasPartial: (text: string) => boolean,
36
+ * excluded: { prefix: string, section: string, summary: string, reason: string }[],
37
+ * sketched: () => { state: string },
38
+ * mentionsSketched: (text: string) => boolean,
39
+ * isParametricMember?: (member: any) => boolean,
40
+ * }} CardContext
41
+ */
42
+
43
+ /** The shape whose prefix a text starts with (the longest wins), or null. */
44
+ export function definitionByPrefix(text, definitions) {
45
+ const body = String(text || '').trim().toLowerCase();
46
+ let best = null;
47
+ for (const d of definitions || []) {
48
+ const prefix = d?.payload?.grammar?.prefix;
49
+ if (typeof prefix !== 'string' || !body.startsWith(prefix.toLowerCase())) continue;
50
+ if (!best || prefix.length > best.payload.grammar.prefix.length) best = d;
51
+ }
52
+ return best;
53
+ }
54
+
55
+ /** The text with trailing extra numbers dropped until it reads as exactly one shape, or null when no such text exists. */
56
+ function withoutExtra(text, ctx) {
57
+ let cut = String(text).trim();
58
+ for (let i = 0; i < 4; i++) {
59
+ const next = cut.replace(/[-*/xX_\s]+[\d.,/]+$/, '');
60
+ if (next === cut || !next) return null;
61
+ cut = next;
62
+ const readings = P.readingsOf(cut, ctx.definitions, ctx.units);
63
+ if (readings.length === 1) return { text: cut, reading: readings[0] };
64
+ }
65
+ return null;
66
+ }
67
+
68
+ /** A member's stored section as a reading, when the typed text is the member's own name (the stored sizes are the truth). */
69
+ function boundReading(member, text, ctx) {
70
+ const stored = member?.stored;
71
+ if (!stored || typeof member.profile !== 'string' || member.profile.trim() !== String(text).trim()) return null;
72
+ const definition = (ctx.definitions || []).find((d) => d && d.id === stored.definitionId) || stored.definition;
73
+ if (!definition || !isObj(stored.params)) return null;
74
+ return {
75
+ definition, definitionId: definition.id, params: stored.params, units: 'mm', canonical: member.profile, spans: [], bound: true,
76
+ };
77
+ }
78
+
79
+ /**
80
+ * Decide the card for a field's text.
81
+ * @param {{ text: string, caret?: number | null, ctx: any, member?: { id: string, profile: string, stored?: any, material?: string } | null }} input
82
+ * @returns {any} the state, by `kind` (see the list at the top of this file)
83
+ */
84
+ export function profileCardState({ text, caret = null, ctx, member = null }) {
85
+ const raw = String(text == null ? '' : text);
86
+ const typed = raw.trim();
87
+ if (!typed) return { kind: 'none' };
88
+
89
+ const row = ctx.libraryRow(typed);
90
+ if (row) return { kind: 'library', text: typed, row };
91
+
92
+ const bound = boundReading(member, typed, ctx);
93
+ if (bound) return parametricState(bound, typed, caret, ctx, member);
94
+
95
+ if (ctx.mentionsSketched(typed) && ctx.sketched().state !== 'ready') {
96
+ return { kind: ctx.sketched().state === 'failed' ? 'failed' : 'loading', text: typed };
97
+ }
98
+
99
+ const result = P.readingsOf(typed, ctx.definitions, ctx.units);
100
+ if (result.length === 1) return parametricState(result[0], typed, caret, ctx, member);
101
+ if (result.length > 1) return { kind: 'ambiguous', text: typed, readings: result, definition: result[0].definition };
102
+
103
+ // Half a library name ("HE", "W16", "L4X4X1/") is being typed, and the suggestion list is already helping: say nothing yet.
104
+ if (ctx.libraryHasPartial(typed)) return { kind: 'none', typing: true };
105
+
106
+ const prefixed = definitionByPrefix(typed, ctx.definitions);
107
+ // A shape's name continues with a size or a separator ("PL", "HI400-"); "ZZTOP" only happens to start with a prefix.
108
+ if (prefixed && /^(?:[\d.,\-*/xX_\s]|$)/.test(typed.slice(prefixed.payload.grammar.prefix.length))) {
109
+ const { problems } = readParametricName(typed, { definitions: ctx.definitions, units: ctx.units });
110
+ const problem = problems[0] || '';
111
+ if (/^an extra number/.test(problem)) {
112
+ const trimmed = withoutExtra(typed, ctx);
113
+ return { kind: 'extra', text: typed, definition: prefixed, problem, trimmed };
114
+ }
115
+ return { kind: 'incomplete', text: typed, definition: prefixed, problem };
116
+ }
117
+
118
+ const lower = typed.toLowerCase();
119
+ const left = (ctx.excluded || []).find((l) => lower.startsWith(String(l.prefix).toLowerCase()) && /^[\d.,]/.test(lower.slice(String(l.prefix).length)));
120
+ if (left) return { kind: 'excluded', text: typed, line: left };
121
+
122
+ return { kind: 'unknown', text: typed };
123
+ }
124
+
125
+ function parametricState(reading, text, caret, ctx, member) {
126
+ const definition = reading.definition;
127
+ const caretInfo = !reading.bound && Number.isFinite(caret) ? sizeAtCaret(text, caret, reading) : null;
128
+ return {
129
+ kind: 'parametric', text, definition, reading, caret: caretInfo,
130
+ connections: !!(member && ctx.isParametricMember && ctx.isParametricMember(member)),
131
+ bound: !!reading.bound,
132
+ };
133
+ }
134
+
135
+ // ---- the form behind a parametric state --------------------------------------------------------------------
136
+
137
+ /** The sizes the form starts with: the reading's own, in the project's units, with the exact values they came from. */
138
+ /** @returns {any} */
139
+ export function initialForm(state, ctx) {
140
+ const { definition, reading } = state;
141
+ return {
142
+ key: `${definition.id}|${state.bound ? 'bound' : 'typed'}`,
143
+ sourceText: state.text,
144
+ texts: P.textsFromParams(definition, reading.params, ctx.units),
145
+ exact: P.exactFromParams(definition, reading.params, ctx.units),
146
+ edited: false,
147
+ focus: null,
148
+ };
149
+ }
150
+
151
+ /** The form judged: the sizes read, the section they make (or why none), the weight, and what is outside the usual. */
152
+ /** @returns {any} */
153
+ export function judgeForm(state, form, ctx, material) {
154
+ return P.judgeSizes({ definition: state.definition, texts: form.texts, exact: form.exact, units: ctx.units, material: material ?? ctx.material });
155
+ }
156
+
157
+ /** The drawing at the form's sizes, or at the shape's own defaults (labelled) while they make no section. */
158
+ /** @param {any} ctx */
159
+ export function drawingFor(definition, judged, focus, ctx) {
160
+ const units = ctx.units;
161
+ if (judged.section) return parametricDrawing({ section: judged.section, definition, params: judged.params, focus, units });
162
+ const defaults = Object.fromEntries(P.formFields(definition, 'mm').map((f) => [f.symbol, P.formatField(f.default, f.kind, 'mm')]));
163
+ const fallback = P.judgeSizes({ definition, texts: defaults, units: 'mm', material: ctx.material });
164
+ if (!fallback.section) return { svg: '', label: '' };
165
+ return parametricDrawing({ section: fallback.section, definition, params: fallback.params, focus, units, atDefaults: true });
166
+ }
167
+
168
+ /**
169
+ * The name as pieces: `{ text, symbol }` for a size and `{ text, symbol: null }` for the prefix and separators, so the card can
170
+ * write each number with its size letter under it. Falls back to one piece when the text cannot be split.
171
+ */
172
+ export function nameSegments(definition, nameText, params, units) {
173
+ const whole = [{ text: nameText, symbol: null }];
174
+ if (!nameText) return whole;
175
+ const { readings } = readParametricName(nameText, { definitions: [definition], units });
176
+ const same = (a, b) => Number.isFinite(a) && Number.isFinite(b) && Math.abs(a - b) <= 1e-6 * Math.max(1, Math.abs(a));
177
+ const reading = readings.find((r) => Object.keys(params || {}).every((k) => !(k in r.params) || same(r.params[k], params[k]))) || readings[0];
178
+ if (!reading || !reading.spans.length) return whole;
179
+ const spans = [...reading.spans].sort((a, b) => a.start - b.start);
180
+ const pieces = [];
181
+ let cursor = 0;
182
+ for (const span of spans) {
183
+ if (span.start > cursor) pieces.push({ text: nameText.slice(cursor, span.start), symbol: null });
184
+ pieces.push({ text: nameText.slice(span.start, span.end), symbol: span.symbol });
185
+ cursor = span.end;
186
+ }
187
+ if (cursor < nameText.length) pieces.push({ text: nameText.slice(cursor), symbol: null });
188
+ return pieces;
189
+ }
190
+
191
+ /** The name the card shows for a form: what was typed while the form holds the typed sizes, else the sizes written in the grammar. */
192
+ export function formNameText(state, form) {
193
+ if (!form.edited && !state.bound) return state.text;
194
+ return P.typedName(state.definition, form.texts);
195
+ }
196
+
197
+ // ---- markup ----------------------------------------------------------------------------------------------
198
+
199
+ export function nameHtml(state, form, judged) {
200
+ const pieces = nameSegments(state.definition, formNameText(state, form), judged?.params || state.reading.params, state.reading.units === 'mm' ? 'mm' : 'in');
201
+ const labelOf = (symbol) => P.formFields(state.definition, 'mm').find((f) => f.symbol === symbol)?.label || symbol;
202
+ const focus = form.focus ?? state.caret?.symbol ?? null;
203
+ return pieces.map((piece, index) => {
204
+ if (!piece.symbol) return `<span class="pcd-sep${index === 0 ? ' pcd-prefix' : ''}">${esc(piece.text)}</span>`;
205
+ const on = focus === piece.symbol;
206
+ return `<span class="pcd-size${on ? ' on' : ''}" data-symbol="${esc(piece.symbol)}"${on ? ' aria-current=true' : ''} data-tip="${esc(labelOf(piece.symbol))}"><b>${esc(piece.text)}</b><i>${esc(piece.symbol)}</i></span>`;
207
+ }).join('');
208
+ }
209
+
210
+ export function orderHtml(definition) {
211
+ const t = P.shapeTitle(definition);
212
+ const words = P.formFields(definition, 'mm').map((f) => f.label.toLowerCase()).join(', ');
213
+ return `Order: <code>${esc(t.prefix)} ${esc(P.grammarText(definition))}</code>${words ? ` <span class=pcd-words>(${esc(words)})</span>` : ''}`;
214
+ }
215
+
216
+ /** The size fields, built once per shape; their text and hints are updated in place so typing keeps its caret. */
217
+ export function fieldsHtml(definition, units, form) {
218
+ return P.formFields(definition, units).map((field, index) => {
219
+ const id = `pcdF${index}`;
220
+ const on = form.focus === field.symbol;
221
+ return `<div class="pcd-row${on ? ' on' : ''}" data-symbol="${esc(field.symbol)}">`
222
+ + `<label for="${id}"><i>${esc(field.symbol)}</i> ${esc(field.label)}${field.optional ? ' <span class=pcd-opt>(optional)</span>' : ''}</label>`
223
+ + `<span class=pcd-in><input data-command-id="steel.profile-card-size#${esc(field.label)}" id="${id}" type=text inputmode=decimal autocomplete=off spellcheck=false data-sym="${esc(field.symbol)}" value="${esc(form.texts[field.symbol] ?? '')}" placeholder="${esc(P.formatField(field.default, field.kind, units))}"><u>${esc(field.unit)}</u></span>`
224
+ + `<span class=pcd-hint id="${id}H"></span></div>`;
225
+ }).join('');
226
+ }
227
+
228
+ /** The hint under one field: why a text is not a number, the usual range, or the millimetres an inch size makes. */
229
+ export function hintFor(field, text, judged, units) {
230
+ const bad = judged.invalid.find((i) => i.field.symbol === field.symbol);
231
+ if (bad) return { text: bad.problem, bad: true };
232
+ const read = P.parseSize(text, field.kind, units);
233
+ if ('value' in read) return { text: field.kind === 'length' && units === 'in' ? P.millimetreHint(read.value) : '', bad: false };
234
+ return { text: '', bad: false };
235
+ }
236
+
237
+ /** What is said under the form: the weight and area, or why there is no section, or what is still to enter. */
238
+ export function resultHtml(state, form, judged, ctx, session) {
239
+ const units = ctx.units;
240
+ const lines = [];
241
+ if (judged.status === 'missing') {
242
+ const names = judged.missing.map((f) => f.label.toLowerCase());
243
+ lines.push(`<div class="pcd-warn"><span aria-hidden=true>!</span> Enter the ${esc(names.join(', '))}.${session.canPreview ? ` ${esc(session.keepsText)}` : ''}</div>`);
244
+ } else if (judged.status === 'invalid') {
245
+ lines.push(`<div class="pcd-warn"><span aria-hidden=true>!</span> ${esc(judged.invalid[0].problem)} for the ${esc(judged.invalid[0].field.label.toLowerCase())}.${session.canPreview ? ` ${esc(session.keepsText)}` : ''}</div>`);
246
+ } else if (judged.status === 'unmet') {
247
+ lines.push(`<div class="pcd-warn"><span aria-hidden=true>!</span> ${judged.held ? '' : 'No section at these sizes: '}${esc(judged.reasons.join('; '))}.${session.canPreview ? ` ${esc(session.keepsText)}` : ''}</div>`);
248
+ } else {
249
+ const facts = [[`Area`, P.areaText(judged.area, units)], [units === 'in' ? 'Weight per foot' : 'Weight per metre', judged.kgPerM == null ? '' : P.weightText(judged.kgPerM, units)]].filter(([, v]) => v);
250
+ if (facts.length) lines.push(`<div class=pcd-facts>${facts.map(([k, v]) => `<span>${esc(k)} <b>${esc(v)}</b></span>`).join('')}</div>`);
251
+ if (judged.outside.length) lines.push(`<div class="pcd-warn"><span aria-hidden=true>!</span> ${esc(judged.outside.join(' '))} Enter still applies it.</div>`);
252
+ }
253
+ if (state.connections) lines.push('<div class=pcd-note>Connections are not modelled on this section yet, so none are placed at its ends.</div>');
254
+ return lines.join('');
255
+ }
256
+
257
+ function headHtml({ prefix, title, pinned, canPin = true }) {
258
+ return `<div class=pcd-head><span class=pcd-pfx>${esc(prefix)}</span><span class=pcd-title>${esc(title)}</span><span class=pcd-tag data-part=tag hidden>Preview, not applied</span>`
259
+ + `<span class=pcd-links>${canPin ? `<button type=button class=pcd-link data-pcd=pin data-command-id="steel.profile-card-pin" aria-pressed="${pinned ? 'true' : 'false'}">${pinned ? 'Unpin' : 'Pin in pane'}</button>` : ''}`
260
+ + `<button type=button class=pcd-link data-pcd=catalog data-command-id="steel.profile-card-open-catalog">Open catalog</button></span></div>`;
261
+ }
262
+
263
+ /**
264
+ * The whole card for a state, built once. Parts that change as sizes are typed carry a `data-part` so the page code can
265
+ * replace them without rebuilding the size fields.
266
+ */
267
+ /**
268
+ * @param {any} state
269
+ * @param {{ form?: any, judged?: any, ctx: any, pinned?: boolean, session?: any, canPin?: boolean, drawing?: any }} options
270
+ */
271
+ export function cardHtml(state, { form = null, judged = null, ctx, pinned = false, session = {}, canPin = true, drawing = null } = /** @type {any} */ ({})) {
272
+ const k = state.kind;
273
+ if (k === 'parametric') {
274
+ const t = P.shapeTitle(state.definition);
275
+ return headHtml({ prefix: t.prefix, title: t.name, pinned, canPin })
276
+ + `<div class=pcd-name data-part=name>${nameHtml(state, form, judged)}</div>`
277
+ + `<div class=pcd-order data-part=order>${orderHtml(state.definition)}</div>`
278
+ + `<div class=pcd-draw data-part=draw role=group>${drawing?.svg || '<div class=pcd-note>No drawing for this shape yet.</div>'}</div>`
279
+ + `<div class=pcd-fields data-part=fields>${fieldsHtml(state.definition, ctx.units, form)}</div>`
280
+ + `<div class=pcd-typing data-part=typing></div>`
281
+ + `<div class=pcd-result data-part=result>${resultHtml(state, form, judged, ctx, session)}</div>`
282
+ + `<div class=pcd-keys>${session.canPreview ? 'Changes show in 3D as you type. ' : 'Changes are written into the field. '}In a size: <kbd>Enter</kbd> apply <kbd>Esc</kbd> undo. From the field: <kbd>Alt</kbd>+<kbd>&darr;</kbd> moves in.</div>`;
283
+ }
284
+ if (k === 'library') {
285
+ const facts = profileFacts(state.row, ctx.displayUnits);
286
+ const draw = state.row.geometrySupported === false ? '' : profileSchematic(state.row);
287
+ return headHtml({ prefix: state.row.designation, title: `${state.row.family || ''} profile`, pinned, canPin })
288
+ + (draw ? `<div class="pcd-draw pcd-lib" role=img aria-label="Section sketch of ${esc(state.row.designation)}">${draw}</div>` : '')
289
+ + (facts.length ? `<dl class=pcd-lfacts>${facts.map(([a, b]) => `<dt>${esc(a)}</dt><dd>${esc(b)}</dd>`).join('')}</dl>` : '')
290
+ + '<div class=pcd-note>This is a library profile, so the card shows its facts rather than sizes to edit.</div>';
291
+ }
292
+ if (k === 'ambiguous') {
293
+ const t = P.shapeTitle(state.definition);
294
+ return headHtml({ prefix: t.prefix, title: 'More than one reading', pinned, canPin })
295
+ + `<div class=pcd-note>FloLess can read <b>${esc(state.text)}</b> ${state.readings.length === 2 ? 'two' : state.readings.length} ways, and does not choose for you.</div>`
296
+ + `<div class=pcd-readings>${state.readings.map((r, i) => `<div class=pcd-reading><div><code>${esc(r.canonical)}</code><span class=pcd-note>${esc(r.note || '')}</span></div>`
297
+ + `<button type=button class="pcd-btn" data-pcd=reading data-index="${i}" data-command-id="steel.profile-card-read-as#${esc(r.canonical)}">Use this</button></div>`).join('')}</div>`;
298
+ }
299
+ if (k === 'extra') {
300
+ const t = P.shapeTitle(state.definition);
301
+ const fixed = state.trimmed;
302
+ return headHtml({ prefix: t.prefix, title: t.name, pinned, canPin })
303
+ + `<div class=pcd-name>${fixed && state.text.startsWith(fixed.text) ? `${esc(fixed.text)}<s class=pcd-extra>${esc(state.text.slice(fixed.text.length))}</s>` : esc(state.text)}</div>`
304
+ + `<div class="pcd-warn"><span aria-hidden=true>!</span> ${esc(extraSentence(state))}</div>`
305
+ + '<div class=pcd-note>Keep as typed leaves the name as it is, unresolved: FloLess cannot draw or weigh it.</div>'
306
+ + `<div class=pcd-actions>${fixed ? `<button type=button class="pcd-btn primary" data-pcd=use data-command-id="steel.profile-card-use-text">Use ${esc(fixed.text)}</button>` : ''}`
307
+ + `<button type=button class=pcd-btn data-pcd=keep data-command-id="steel.profile-card-keep-text">Keep as typed</button></div>`;
308
+ }
309
+ if (k === 'incomplete') {
310
+ const t = P.shapeTitle(state.definition);
311
+ return headHtml({ prefix: t.prefix, title: t.name, pinned, canPin })
312
+ + `<div class=pcd-name>${esc(state.text)}</div>`
313
+ + `<div class=pcd-order>${orderHtml(state.definition)}</div>`
314
+ + `<div class=pcd-typing>Next: <b>${esc(state.problem)}</b></div>`
315
+ + `<div class=pcd-note>${esc(session.keepsText || 'Until the name is complete, the member keeps the section it has.')}</div>`
316
+ + `<div class=pcd-actions><button type=button class=pcd-btn data-pcd=keep data-command-id="steel.profile-card-keep-text">Keep as typed</button></div>`;
317
+ }
318
+ if (k === 'excluded') {
319
+ return headHtml({ prefix: state.line.prefix, title: P.excludedTitle(state.line), pinned, canPin })
320
+ + `<div class=pcd-name>${esc(state.text)}</div>`
321
+ + `<div class=pcd-note>This is a Tekla shape FloLess has not been taught to draw yet. ${esc(state.line.reason || '')} The member keeps the name and still goes back to Tekla unchanged; FloLess does not draw it until the shape is defined.</div>`
322
+ + `<div class=pcd-actions><button type=button class="pcd-btn primary" data-pcd=define data-command-id="steel.profile-card-define">Define this shape</button></div>`;
323
+ }
324
+ if (k === 'loading') return headHtml({ prefix: '…', title: 'Loading Tekla’s shapes', pinned, canPin }) + '<div class=pcd-note role=status>Tekla’s extra shapes load the first time they are needed. This takes a moment.</div>';
325
+ if (k === 'failed') {
326
+ return headHtml({ prefix: '!', title: 'Shapes not loaded', pinned, canPin })
327
+ + '<div class="pcd-warn"><span aria-hidden=true>!</span> Tekla’s extra shapes could not be loaded, so this name cannot be read yet. Check your connection, then try again.</div>'
328
+ + '<div class=pcd-actions><button type=button class="pcd-btn" data-pcd=retry data-command-id="steel.profile-card-retry-shapes">Try again</button></div>';
329
+ }
330
+ // unknown
331
+ return headHtml({ prefix: '?', title: 'Not in the catalog or the shapes', pinned, canPin })
332
+ + `<div class=pcd-name>${esc(state.text)}</div>`
333
+ + '<div class=pcd-note>FloLess cannot draw or weigh this name yet. It stays exactly as typed. Pick a profile from the catalog, or define it as a shape.</div>'
334
+ + '<div class=pcd-actions><button type=button class="pcd-btn primary" data-pcd=catalog data-command-id="steel.profile-card-open-catalog">Browse the catalog</button>'
335
+ + '<button type=button class=pcd-btn data-pcd=define data-command-id="steel.profile-card-define">Define this shape</button></div>';
336
+ }
337
+
338
+ /** `thickness and width`: what a shape's name asks for, in words. */
339
+ export function sizeWords(definition) {
340
+ const fields = P.formFields(definition, 'mm').filter((f) => !f.optional);
341
+ const names = fields.map((f) => f.label.toLowerCase());
342
+ if (names.length <= 1) return names[0] || 'its sizes';
343
+ return `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`;
344
+ }
345
+
346
+ /** The polite announcement of a parametric name: its decomposition, for a screen reader. */
347
+ export function announcement(state, judged) {
348
+ if (state.kind === 'ambiguous') return `${state.text} can be read ${state.readings.length} ways. Choose one.`;
349
+ if (state.kind === 'extra') return `${extraSentence(state)}${state.trimmed ? ` Use ${state.trimmed.text}, or keep as typed.` : ''}`;
350
+ if (state.kind === 'incomplete') return `Next: ${state.problem}.`;
351
+ if (state.kind === 'excluded') return `${state.text} is a Tekla shape FloLess does not draw yet. It keeps its name.`;
352
+ if (state.kind === 'unknown') return `${state.text} is not in the catalog or the shapes. It stays as typed.`;
353
+ if (state.kind === 'library') return `Library profile ${state.row.designation}.`;
354
+ if (state.kind !== 'parametric') return '';
355
+ const t = P.shapeTitle(state.definition);
356
+ const fields = P.formFields(state.definition, 'mm');
357
+ const params = judged?.params || state.reading.params;
358
+ const parts = fields.filter((f) => Number.isFinite(params?.[f.symbol])).map((f) => `${f.label.toLowerCase()} ${P.formatField(params[f.symbol], f.kind, 'mm')}`);
359
+ return `${t.name}: ${parts.join(', ')}.`;
360
+ }
361
+
362
+ /** What the typing help says for a caret: the size the caret is in, or the size that comes next. */
363
+ export function typingText(state) {
364
+ const c = state.caret;
365
+ if (!c) return '';
366
+ if (c.symbol) return `Typing <i>${esc(c.symbol)}</i>, <b>${esc(c.label)}</b>${c.next ? `. Next: ${esc(c.next.label)}` : ''}`;
367
+ if (c.next) return `Next: <b>${esc(c.next.label)}</b>`;
368
+ return 'Every size is entered.';
369
+ }
370
+
371
+ /** What is wrong with an extra number, in a detailer's words: what the shape takes, and which value has no meaning. */
372
+ export function extraSentence(state) {
373
+ const fields = P.formFields(state.definition, 'mm').filter((f) => !f.optional);
374
+ const labels = fields.map((f) => f.label.toLowerCase());
375
+ const extra = /“([^”]*)”/.exec(state.problem || '')?.[1];
376
+ const takes = `${P.shapeTitle(state.definition).name} takes ${fields.length === 1 ? 'one size' : `${fields.length} sizes`} (${labels.join(', ')}).`;
377
+ return `${takes} ${extra ? `The extra number ${extra} has` : 'The extra number has'} no meaning here.`;
378
+ }