@mccaskie-labs/workbench-settings 0.2.0 → 0.3.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @mccaskie-labs/workbench-settings
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - New setting types for the tools' owner settings (brief 7, `docs/builder-ready.md` B10):
8
+
9
+ - `duration`: written as tools write it (`30m`, `24h`, `2d`), kept in its shortest spelling, `min` and `max` in minutes. `durationMinutes` gives its length.
10
+ - `money`: typed as an amount ("50", "A$1,250.00"; a comma only groups thousands), kept as whole cents, shown in the setting's `currency` (A$50.00, ₱300.00). `formatMoney` gives the words.
11
+ - `lines: true` on `text`: a box of several lines (a new `wb-settings-lines` class that grows with what is typed), keeping its line breaks, up to 2000 characters (`MAX_LINES_TEXT`), for opening hours or an answer to a common question.
12
+ - A duration's bounds are shown in words ("14 days", `durationText`), and one longest (`textMax`) is used by the field and the check.
13
+ - A setting's `answers` may be a dotted path inside the answers file, such as `invoice.due_days`.
14
+
3
15
  ## 0.2.0
4
16
 
5
17
  ### Minor Changes
package/README.md CHANGED
@@ -25,6 +25,14 @@ Refuse a post from another site before calling `settingsAction` (check the Origi
25
25
  2. Add the email template `settings_code` to the app's email catalogue.
26
26
  3. Call `settings.cleanup()` once a day from the app's cron.
27
27
 
28
+ ## Types
29
+
30
+ `text` (one line, up to 500 characters; with `lines: true` a box of several lines, up to 2000, for opening hours or an answer to a common question), `email`, `choice`, `whole`, `duration` (as tools write it: `30m`, `24h`, `2d`; `min` and `max` in minutes), `money` (typed as an amount, kept as whole cents, shown in the setting's `currency` as A$50.00; `min` and `max` in cents) and `map` (design mode's). A setting's `answers` may be a dotted path inside the answers file, such as `invoice.due_days`.
31
+
32
+ - A money value from an answers file or a default is whole cents (2500 is A$25.00); typed text is an amount, and a comma only groups thousands ("50,5" is refused).
33
+ - Several-line text keeps its line breaks as `\n`: a tool that shows it on a page escapes it and keeps the breaks (`white-space: pre-line`, or one `<br>` per line).
34
+ - A setting that changes what a customer is charged, or where money or personal details go, is never `live` (`docs/project-settings.md` §5): until the guarded kind is built, leave it out.
35
+
28
36
  Restyle the page in the project's `public/styles/project.css` with the `wb-settings-*` classes and `--wb-settings-*` variables (`SETTINGS_STYLES`); they default to the project's theme tokens.
29
37
 
30
38
  ## Design mode (0.2.0)
package/dist/check.d.ts CHANGED
@@ -1,7 +1,20 @@
1
1
  import type { SettingDef, SettingValue, ToolSettings, Words } from "./types.ts";
2
2
  import type { SettingsWordKey } from "./words.ts";
3
- /** The longest text any setting may hold. */
3
+ /** The longest one-line text any setting may hold. */
4
4
  export declare const MAX_TEXT = 500;
5
+ /** The longest text a setting drawn as several lines (`lines: true`) may hold. */
6
+ export declare const MAX_LINES_TEXT = 2000;
7
+ /** A text setting's longest when its declaration gives no `max`: one line, and several lines. */
8
+ export declare const DEFAULT_TEXT_MAX = 200;
9
+ export declare const DEFAULT_LINES_MAX = 1000;
10
+ /** The longest duration any setting may hold: a year, in minutes. */
11
+ export declare const MAX_DURATION_MINUTES = 525600;
12
+ /** The longest a text setting takes, as the check and the page's field both use it. */
13
+ export declare function textMax(def: SettingDef): number;
14
+ /** A duration's length in minutes ("2d" is 2880), or null when it isn't one. */
15
+ export declare function durationMinutes(text: string): number | null;
16
+ /** Cents as the page shows them: 5000 in AUD is "A$50.00"; with no currency, "50.00". */
17
+ export declare function formatMoney(cents: number, currency?: string): string;
5
18
  export declare const MAX_EMAIL = 254;
6
19
  /** A map's most names, and its largest size as JSON (the store holds up to 8 KB). */
7
20
  export declare const MAX_MAP_ENTRIES = 200;
package/dist/check.js CHANGED
@@ -7,8 +7,53 @@ const EMAIL = /^[^\s@<>()",;:\\[\]]+@[^\s@<>()",;:\\[\]]+\.[^\s@<>()",;:\\[\]]+$
7
7
  /** Control, invisible and direction-changing characters never belong in a setting (they could disguise a sender's name). */
8
8
  // eslint-disable-next-line no-control-regex
9
9
  const CONTROL = /[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u180e\u200b-\u200f\u2028-\u202e\u2060-\u206f\ufeff]/;
10
- /** The longest text any setting may hold. */
10
+ /** The longest one-line text any setting may hold. */
11
11
  export const MAX_TEXT = 500;
12
+ /** The longest text a setting drawn as several lines (`lines: true`) may hold. */
13
+ export const MAX_LINES_TEXT = 2000;
14
+ /** A text setting's longest when its declaration gives no `max`: one line, and several lines. */
15
+ export const DEFAULT_TEXT_MAX = 200;
16
+ export const DEFAULT_LINES_MAX = 1000;
17
+ /** The longest duration any setting may hold: a year, in minutes. */
18
+ export const MAX_DURATION_MINUTES = 525_600;
19
+ /** A duration as tools write them: "30m", "24h", "2d". */
20
+ const DURATION = /^(\d{1,6})\s*(m|h|d)$/;
21
+ const UNIT_MINUTES = { m: 1, h: 60, d: 1440 };
22
+ const CURRENCY = /^[A-Z]{3}$/;
23
+ /** The longest a text setting takes, as the check and the page's field both use it. */
24
+ export function textMax(def) {
25
+ return def.lines ? Math.min(def.max ?? DEFAULT_LINES_MAX, MAX_LINES_TEXT) : Math.min(def.max ?? DEFAULT_TEXT_MAX, MAX_TEXT);
26
+ }
27
+ /** A duration's length in minutes ("2d" is 2880), or null when it isn't one. */
28
+ export function durationMinutes(text) {
29
+ const m = DURATION.exec(text.trim().toLowerCase());
30
+ if (!m)
31
+ return null;
32
+ return Number(m[1]) * UNIT_MINUTES[m[2]];
33
+ }
34
+ /** Cents as the page shows them: 5000 in AUD is "A$50.00"; with no currency, "50.00". */
35
+ export function formatMoney(cents, currency) {
36
+ if (currency && CURRENCY.test(currency)) {
37
+ try {
38
+ return new Intl.NumberFormat("en-US", { style: "currency", currency, minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(cents / 100);
39
+ }
40
+ catch {
41
+ // An unknown code falls through to the plain amount.
42
+ }
43
+ }
44
+ return (cents / 100).toFixed(2);
45
+ }
46
+ /** What an owner typed as an amount ("50", "50.5", "A$1,250.00", "₱300") in cents, or null. */
47
+ function parseMoney(text) {
48
+ const amount = text.replace(/^[A-Za-z]{0,3}\s*[$₱€£¥]?\s*/, "").trim();
49
+ // A comma only ever groups thousands ("1,250"): "50,5" is refused, never read as 505.
50
+ if (!/^(\d{1,3}(,\d{3})+|\d{1,9})(\.\d{1,2})?$/.test(amount))
51
+ return null;
52
+ const m = /^(\d{1,9})(?:\.(\d{1,2}))?$/.exec(amount.replace(/,/g, ""));
53
+ if (!m)
54
+ return null;
55
+ return Number(m[1]) * 100 + Number((m[2] ?? "0").padEnd(2, "0"));
56
+ }
12
57
  export const MAX_EMAIL = 254;
13
58
  /** A map's most names, and its largest size as JSON (the store holds up to 8 KB). */
14
59
  export const MAX_MAP_ENTRIES = 200;
@@ -78,16 +123,18 @@ function checkType(def, raw) {
78
123
  return { ok: true, value: null };
79
124
  if (def.type === "map")
80
125
  return checkMap(raw);
81
- const text = typeof raw === "number" ? String(raw) : typeof raw === "string" ? raw.trim() : null;
126
+ // A box of several lines keeps its line breaks (as \n, whatever the browser sent); no other control character is allowed.
127
+ const lines = def.type === "text" && def.lines === true;
128
+ const text = typeof raw === "number" ? String(raw) : typeof raw === "string" ? (lines ? raw.replace(/\r\n?/g, "\n") : raw).trim() : null;
82
129
  if (text === null)
83
130
  return { ok: false, problem: "problem.text" };
84
131
  if (text === "")
85
132
  return (def.min ?? 0) > 0 && def.type !== "whole" ? { ok: false, problem: "problem.required" } : { ok: true, value: null };
86
- if (CONTROL.test(text))
133
+ if (CONTROL.test(lines ? text.replace(/\n/g, "") : text))
87
134
  return { ok: false, problem: "problem.text" };
88
135
  switch (def.type) {
89
136
  case "text": {
90
- const max = Math.min(def.max ?? 200, MAX_TEXT);
137
+ const max = textMax(def);
91
138
  const min = def.min ?? 1;
92
139
  const n = [...text].length;
93
140
  if (n > max)
@@ -118,10 +165,40 @@ function checkType(def, raw) {
118
165
  return { ok: false, problem: "problem.too_big", values: { max: String(def.max) } };
119
166
  return { ok: true, value: n };
120
167
  }
168
+ case "duration": {
169
+ const n = durationMinutes(text);
170
+ if (n === null || n > MAX_DURATION_MINUTES)
171
+ return { ok: false, problem: "problem.duration" };
172
+ if (def.min !== undefined && n < def.min)
173
+ return { ok: false, problem: "problem.too_short_time", values: { min: durationWords(def.min) } };
174
+ if (def.max !== undefined && n > def.max)
175
+ return { ok: false, problem: "problem.too_long_time", values: { max: durationWords(def.max) } };
176
+ const m = DURATION.exec(text.trim().toLowerCase());
177
+ return { ok: true, value: `${Number(m[1])}${m[2]}` };
178
+ }
179
+ case "money": {
180
+ // A number from a file or a default is already cents; typed text is an amount.
181
+ const n = typeof raw === "number" ? (Number.isSafeInteger(raw) && raw >= 0 ? raw : null) : parseMoney(text);
182
+ if (n === null)
183
+ return { ok: false, problem: "problem.money" };
184
+ if (def.min !== undefined && n < def.min)
185
+ return { ok: false, problem: "problem.too_small", values: { min: formatMoney(def.min, def.currency) } };
186
+ if (def.max !== undefined && n > def.max)
187
+ return { ok: false, problem: "problem.too_big", values: { max: formatMoney(def.max, def.currency) } };
188
+ return { ok: true, value: n };
189
+ }
121
190
  default:
122
191
  return { ok: false, problem: "problem.text" };
123
192
  }
124
193
  }
194
+ /** Minutes as a duration's own shortest spelling: 2880 is "2d", 90 is "90m". */
195
+ function durationWords(minutes) {
196
+ if (minutes % 1440 === 0)
197
+ return `${minutes / 1440}d`;
198
+ if (minutes % 60 === 0)
199
+ return `${minutes / 60}h`;
200
+ return `${minutes}m`;
201
+ }
125
202
  /** Every problem with a set of declarations, or an empty list. */
126
203
  export function settingsProblems(tools) {
127
204
  const out = [];
@@ -166,8 +243,18 @@ export function settingsProblems(tools) {
166
243
  for (const [name, n] of [["min", s.min], ["max", s.max]])
167
244
  if (n !== undefined && !Number.isSafeInteger(n))
168
245
  out.push(`${where}: ${name} must be a whole number`);
169
- if (s.type === "text" && s.max !== undefined && (s.max < 1 || s.max > MAX_TEXT))
170
- out.push(`${where}: max must be 1 to ${MAX_TEXT}`);
246
+ if (s.type === "text" && s.max !== undefined && (s.max < 1 || s.max > (s.lines ? MAX_LINES_TEXT : MAX_TEXT)))
247
+ out.push(`${where}: max must be 1 to ${s.lines ? MAX_LINES_TEXT : MAX_TEXT}`);
248
+ if (s.lines !== undefined && s.type !== "text")
249
+ out.push(`${where}: only text can be drawn as lines`);
250
+ if (s.currency !== undefined && (s.type !== "money" || !CURRENCY.test(s.currency)))
251
+ out.push(`${where}: currency is a three-letter code, on money only`);
252
+ if (s.type === "duration" && s.max !== undefined && (s.max < 1 || s.max > MAX_DURATION_MINUTES))
253
+ out.push(`${where}: max must be 1 to ${MAX_DURATION_MINUTES} minutes`);
254
+ if ((s.type === "duration" || s.type === "money") && s.min !== undefined && s.min < 0)
255
+ out.push(`${where}: min can't be below 0`);
256
+ if (s.min !== undefined && s.max !== undefined && s.min > s.max)
257
+ out.push(`${where}: min is above max`);
171
258
  if (s.default !== undefined && s.default !== null && !checkValue(s, s.default).ok)
172
259
  out.push(`${where}: its default doesn't pass its own check`);
173
260
  }
package/dist/cli.js CHANGED
File without changes
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  export { createSettings, withReplyTo, type ConfirmResult, type SaveResult, type Settings, type SettingsClient, type SettingsMailer, type SettingsOptions } from "./settings.ts";
2
- export { checkValue, MAX_EMAIL, MAX_MAP_BYTES, MAX_MAP_ENTRIES, MAX_TEXT, sameValue, settingsProblems, type Checked } from "./check.ts";
2
+ export { checkValue, DEFAULT_LINES_MAX, DEFAULT_TEXT_MAX, durationMinutes, formatMoney, MAX_DURATION_MINUTES, MAX_EMAIL, MAX_LINES_TEXT, MAX_MAP_BYTES, MAX_MAP_ENTRIES, MAX_TEXT, sameValue, settingsProblems, textMax, type Checked } from "./check.ts";
3
3
  export { drawSettingsPage, renderSettingsPage, settingsAction, type SettingsOutcome, type SettingsPageOptions } from "./page.ts";
4
4
  export { EMAIL_SETTINGS } from "./email.ts";
5
5
  export { designSettings, type DesignChoice, type DesignOptions } from "./design.ts";
6
6
  export { settingsMigration } from "./migration.ts";
7
- export { phrase, SETTINGS_WORD_KEYS, SETTINGS_WORDS, settingsWord, type SettingsStrings, type SettingsWordKey } from "./words.ts";
7
+ export { durationText, phrase, SETTINGS_WORD_KEYS, SETTINGS_WORDS, settingsWord, type SettingsStrings, type SettingsWordKey } from "./words.ts";
8
8
  export { SETTINGS_STYLES } from "./styles.ts";
9
9
  export { SETTINGS_SAMPLES } from "./samples.ts";
10
10
  export { SettingsError, type SettingsErrorCode } from "./errors.ts";
package/dist/index.js CHANGED
@@ -7,12 +7,12 @@
7
7
  // it is used. No AI and no Gateway call of its own: the code email goes
8
8
  // through the app's own mailer.
9
9
  export { createSettings, withReplyTo } from "./settings.js";
10
- export { checkValue, MAX_EMAIL, MAX_MAP_BYTES, MAX_MAP_ENTRIES, MAX_TEXT, sameValue, settingsProblems } from "./check.js";
10
+ export { checkValue, DEFAULT_LINES_MAX, DEFAULT_TEXT_MAX, durationMinutes, formatMoney, MAX_DURATION_MINUTES, MAX_EMAIL, MAX_LINES_TEXT, MAX_MAP_BYTES, MAX_MAP_ENTRIES, MAX_TEXT, sameValue, settingsProblems, textMax } from "./check.js";
11
11
  export { drawSettingsPage, renderSettingsPage, settingsAction } from "./page.js";
12
12
  export { EMAIL_SETTINGS } from "./email.js";
13
13
  export { designSettings } from "./design.js";
14
14
  export { settingsMigration } from "./migration.js";
15
- export { phrase, SETTINGS_WORD_KEYS, SETTINGS_WORDS, settingsWord } from "./words.js";
15
+ export { durationText, phrase, SETTINGS_WORD_KEYS, SETTINGS_WORDS, settingsWord } from "./words.js";
16
16
  export { SETTINGS_STYLES } from "./styles.js";
17
17
  export { SETTINGS_SAMPLES } from "./samples.js";
18
18
  export { SettingsError } from "./errors.js";
package/dist/page.js CHANGED
@@ -5,9 +5,9 @@
5
5
  // its own admin shell (workbench-ui's shell({ admin: true })), behind its
6
6
  // sign-in, for its admins only. Every element carries a wb-settings-* class
7
7
  // (SETTINGS_STYLES).
8
- import { checkValue } from "./check.js";
8
+ import { checkValue, formatMoney, textMax } from "./check.js";
9
9
  import { SettingsError } from "./errors.js";
10
- import { phrase, settingsWord } from "./words.js";
10
+ import { durationText, phrase, settingsWord } from "./words.js";
11
11
  const esc = (s) => s.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
12
12
  function defaultWhen(timeZone, lang) {
13
13
  const f = new Intl.DateTimeFormat(lang === "en" ? "en-AU" : "en-PH", { day: "numeric", month: "short", year: "numeric", hour: "numeric", minute: "2-digit", timeZone });
@@ -19,8 +19,18 @@ const shown = (v, lang, t, def) => {
19
19
  if (typeof v === "object")
20
20
  return Object.entries(v).map(([name, value]) => `${name}: ${value}`).join(", ");
21
21
  const choice = def?.choices?.find((c) => c.value === v);
22
- return choice ? phrase(choice.label, lang) : String(v);
22
+ if (choice)
23
+ return phrase(choice.label, lang);
24
+ if (def?.type === "money" && typeof v === "number")
25
+ return formatMoney(v, def.currency);
26
+ return String(v);
23
27
  };
28
+ /** A duration bound in a problem ("too long: 14d at most") as words: "14 days". */
29
+ function readable(word, values, lang, t) {
30
+ if (!values || (word !== "problem.too_short_time" && word !== "problem.too_long_time"))
31
+ return values;
32
+ return Object.fromEntries(Object.entries(values).map(([k, v]) => [k, durationText(v, lang, t)]));
33
+ }
24
34
  function field(s, fid, value, lang, invalid, describedBy) {
25
35
  const d = s.def;
26
36
  const aria = `${invalid ? ' aria-invalid="true"' : ""} aria-describedby="${describedBy}"`;
@@ -28,12 +38,19 @@ function field(s, fid, value, lang, invalid, describedBy) {
28
38
  const opts = (d.choices ?? []).map((c) => `<option value="${esc(c.value)}"${c.value === value ? " selected" : ""}>${esc(phrase(c.label, lang))}</option>`).join("");
29
39
  return `<select class="wb-settings-select" id="${fid}" name="value"${aria}>${opts}</select>`;
30
40
  }
41
+ if (d.type === "text" && d.lines) {
42
+ return `<textarea class="wb-settings-input wb-settings-lines" id="${fid}" name="value" rows="4" maxlength="${textMax(d)}"${aria}>${esc(value)}</textarea>`;
43
+ }
31
44
  const type = d.type === "email" ? "email" : d.type === "whole" ? "number" : "text";
32
45
  const extra = d.type === "email"
33
46
  ? ' autocomplete="email" spellcheck="false" maxlength="254"'
34
47
  : d.type === "whole"
35
48
  ? ` inputmode="numeric" step="1"${d.min !== undefined ? ` min="${d.min}"` : ""}${d.max !== undefined ? ` max="${d.max}"` : ""}`
36
- : ` maxlength="${Math.min(d.max ?? 200, 500)}"`;
49
+ : d.type === "money"
50
+ ? ' inputmode="decimal" autocomplete="off" maxlength="20"'
51
+ : d.type === "duration"
52
+ ? ' autocomplete="off" spellcheck="false" maxlength="10" placeholder="24h"'
53
+ : ` maxlength="${textMax(d)}"`;
37
54
  return `<input class="wb-settings-input" id="${fid}" name="value" type="${type}" value="${esc(value)}"${extra}${aria}>`;
38
55
  }
39
56
  function source(s, lang, t, when) {
@@ -66,7 +83,7 @@ function item(s, o, lang, when) {
66
83
  const fid = `wb-settings-${s.tool}-${s.key}`.replace(/_/g, "-");
67
84
  const mine = o.outcome && o.outcome.tool === s.tool && o.outcome.key === s.key ? o.outcome : undefined;
68
85
  const problem = mine?.problem && !mine.notice && mine.problem.word.startsWith("problem.") ? mine.problem : undefined;
69
- const value = problem && mine?.typed !== undefined ? mine.typed : s.value === null ? "" : String(s.value);
86
+ const value = problem && mine?.typed !== undefined ? mine.typed : s.value === null ? "" : s.def.type === "money" && typeof s.value === "number" ? formatMoney(s.value) : String(s.value);
70
87
  const ids = [`${fid}-source`, ...(s.def.help ? [`${fid}-help`] : []), ...(problem ? [`${fid}-problem`] : [])].join(" ");
71
88
  const extra = Object.entries(o.fields ?? {})
72
89
  .map(([n, v]) => `<input type="hidden" name="${esc(n)}" value="${esc(v)}">`)
@@ -80,7 +97,7 @@ function item(s, o, lang, when) {
80
97
  `<label class="wb-settings-label" for="${fid}">${esc(phrase(s.def.label, lang))}</label>`,
81
98
  s.def.help ? `<p class="wb-settings-help" id="${fid}-help">${esc(phrase(s.def.help, lang))}</p>` : "",
82
99
  field(s, fid, value, lang, Boolean(problem), ids),
83
- problem ? `<p class="wb-settings-field-problem" id="${fid}-problem" role="alert">${esc(settingsWord(problem.word, lang, problem.values, t))}</p>` : "",
100
+ problem ? `<p class="wb-settings-field-problem" id="${fid}-problem" role="alert">${esc(settingsWord(problem.word, lang, readable(problem.word, problem.values, lang, t), t))}</p>` : "",
84
101
  `<p class="wb-settings-source" id="${fid}-source">${esc(source(s, lang, t, when))}</p>`,
85
102
  carryNote(s, lang, t),
86
103
  `<div class="wb-settings-actions">`,
package/dist/samples.js CHANGED
@@ -26,9 +26,12 @@ const CLASSES = {
26
26
  ],
27
27
  default: "2w",
28
28
  },
29
+ { key: "cancel_until", type: "duration", kind: "live", label: { en: "Cancel online until" }, help: { en: "How long before a class people can still cancel it themselves: 30m, 12h or 2d." }, default: "12h" },
30
+ { key: "drop_in", type: "money", kind: "live", currency: "AUD", min: 0, label: { en: "Drop-in price" }, default: 2500 },
31
+ { key: "opening_words", type: "text", lines: true, max: 2000, kind: "live", label: { en: "Opening hours" }, help: { en: "As your pages show them." } },
29
32
  ],
30
33
  };
31
- const bookAhead = CLASSES.settings[0];
34
+ const [bookAhead, cancelUntil, dropIn, openingWords] = CLASSES.settings;
32
35
  const DESIGN = designSettings({
33
36
  looks: [{ id: "clean", title: "Clean" }, { id: "calm", title: "Calm" }],
34
37
  schemes: [{ id: "light", title: "Light" }, { id: "dark", title: "Dark" }],
@@ -53,6 +56,9 @@ const problems = [
53
56
  { tool: "email", key: "reply_to", def: replyTo, value: "hello@saltbush.example", from: "answers" },
54
57
  { tool: "email", key: "from_name", def: fromName, value: null, from: "default" },
55
58
  { tool: "classes", key: "book_ahead", def: bookAhead, value: "2w", from: "default" },
59
+ { tool: "classes", key: "cancel_until", def: cancelUntil, value: "12h", from: "default" },
60
+ { tool: "classes", key: "drop_in", def: dropIn, value: 2500, from: "default" },
61
+ { tool: "classes", key: "opening_words", def: openingWords, value: "Monday to Friday, 6am to 8pm\nSaturday, 7am to noon", from: "owner", changedBy: "maria@saltbush.example", changedAt: "2026-11-24T08:30:00.000Z" },
56
62
  ];
57
63
  /** Every sample of the Settings page. */
58
64
  export const SETTINGS_SAMPLES = {
package/dist/settings.js CHANGED
@@ -70,7 +70,9 @@ export function createSettings(opts) {
70
70
  function answerOf(tool, def) {
71
71
  if (!def.answers)
72
72
  return null;
73
- const raw = opts.answers?.[tool]?.[def.answers];
73
+ let raw = opts.answers?.[tool];
74
+ for (const part of def.answers.split("."))
75
+ raw = raw !== null && typeof raw === "object" && !Array.isArray(raw) && Object.hasOwn(raw, part) ? raw[part] : undefined;
74
76
  if (raw === undefined || raw === null)
75
77
  return null;
76
78
  const c = checkValue(def, raw);
package/dist/styles.js CHANGED
@@ -54,6 +54,7 @@ export const SETTINGS_STYLES = {
54
54
  { name: "wb-settings-label", element: "label", about: "A setting's name", css: "font-weight:600" },
55
55
  { name: "wb-settings-help", element: "p", about: "What a setting does", css: `margin:0;color:${v("muted")};font-size:.9375rem` },
56
56
  { name: "wb-settings-input", element: "input", about: "A text, email, number or code field", css: control, states: [focus, invalid] },
57
+ { name: "wb-settings-lines", element: "textarea", about: "A field of several lines (also wb-settings-input): grows with what is typed, then scrolls", css: "min-height:5.5rem;max-height:20rem;field-sizing:content;resize:vertical;overflow-y:auto" },
57
58
  { name: "wb-settings-select", element: "select", about: "A choice", css: control, states: [focus, invalid] },
58
59
  { name: "wb-settings-code", element: "input", about: "The code field (also wb-settings-input)", css: "max-width:10rem;letter-spacing:.2em;font-variant-numeric:tabular-nums" },
59
60
  { name: "wb-settings-field-problem", element: "p", about: "What's wrong with what was typed", css: `margin:0;color:${v("danger")};font-weight:600` },
package/dist/types.d.ts CHANGED
@@ -5,7 +5,7 @@ export type Words = {
5
5
  en: string;
6
6
  } & Partial<Record<Exclude<SettingsLang, "en">, string>>;
7
7
  /** The kind of value, which sets the field drawn and the check run. */
8
- export type SettingType = "text" | "email" | "choice" | "whole" | "map";
8
+ export type SettingType = "text" | "email" | "choice" | "whole" | "duration" | "money" | "map";
9
9
  /**
10
10
  * How a change takes effect (§5).
11
11
  * - `live`: saved and used at once.
@@ -24,12 +24,23 @@ export interface SettingDef {
24
24
  help?: Words;
25
25
  /** The value when neither the project's answers nor the owner set one. */
26
26
  default?: string | number | null;
27
- /** The answers-file key this overrides (workbench/<tool>.json), so the page can say "set when the app was built". */
27
+ /**
28
+ * The answers-file key this overrides (workbench/<tool>.json), so the page
29
+ * can say "set when the app was built". A dotted path reaches inside an
30
+ * object: "invoice.due_days".
31
+ */
28
32
  answers?: string;
29
- /** Text: the longest. Whole: the largest. */
33
+ /** Text: the longest. Whole: the largest. Duration: the longest, in minutes. Money: the most, in cents. */
30
34
  max?: number;
31
- /** Text: the shortest (default 1). Whole: the smallest. */
35
+ /** Text: the shortest (default 1). Whole: the smallest. Duration: the shortest, in minutes. Money: the least, in cents. */
32
36
  min?: number;
37
+ /**
38
+ * Text only: drawn as a box of several lines (opening hours, an answer to
39
+ * a common question), and `max` may go up to MAX_LINES_TEXT.
40
+ */
41
+ lines?: boolean;
42
+ /** Money only: the ISO 4217 code the amount is shown in, such as AUD (A$50.00). Left out: the amount alone (50.00). */
43
+ currency?: string;
33
44
  /** Choice: the values, in the order shown. */
34
45
  choices?: readonly {
35
46
  value: string;
package/dist/words.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { SettingsLang } from "./types.ts";
2
- export declare const SETTINGS_WORD_KEYS: readonly ["title", "intro", "from.default", "from.answers", "from.owner", "not_set", "save", "put_back", "saved", "unchanged", "put_back.done", "pending.sent", "pending.code", "pending.confirm", "pending.confirmed", "pending.cancel", "pending.cancelled", "code.wrong", "code.expired", "code.locked", "code.none", "history.title", "history.empty", "history.col.when", "history.col.who", "history.col.what", "history.col.change", "history.put_back", "history.not_set", "problem.text", "problem.required", "problem.too_long", "problem.too_short", "problem.email", "problem.choice", "problem.whole", "problem.too_small", "problem.too_big", "problem.failed", "problem.too_soon", "problem.too_many", "problem.send_failed", "problem.mailer_needed", "email.subject", "email.body", "problem.map", "problem.check", "map.empty", "carry.test_only"];
2
+ export declare const SETTINGS_WORD_KEYS: readonly ["title", "intro", "from.default", "from.answers", "from.owner", "not_set", "save", "put_back", "saved", "unchanged", "put_back.done", "pending.sent", "pending.code", "pending.confirm", "pending.confirmed", "pending.cancel", "pending.cancelled", "code.wrong", "code.expired", "code.locked", "code.none", "history.title", "history.empty", "history.col.when", "history.col.who", "history.col.what", "history.col.change", "history.put_back", "history.not_set", "problem.text", "problem.required", "problem.too_long", "problem.too_short", "problem.email", "problem.choice", "problem.whole", "problem.too_small", "problem.too_big", "problem.duration", "problem.money", "problem.too_short_time", "problem.too_long_time", "problem.failed", "problem.too_soon", "problem.too_many", "problem.send_failed", "problem.mailer_needed", "email.subject", "email.body", "problem.map", "problem.check", "map.empty", "carry.test_only", "time.m", "time.m_one", "time.h", "time.h_one", "time.d", "time.d_one"];
3
3
  export type SettingsWordKey = (typeof SETTINGS_WORD_KEYS)[number];
4
4
  /** The words by language, as `ui.wbsettings.<key>` would hold them. */
5
5
  export declare const SETTINGS_WORDS: Readonly<Record<SettingsLang, Readonly<Record<SettingsWordKey, string>>>>;
@@ -11,3 +11,5 @@ export declare function settingsWord(key: SettingsWordKey, lang: SettingsLang, v
11
11
  export declare function phrase(words: {
12
12
  en: string;
13
13
  } & Partial<Record<SettingsLang, string>>, lang: SettingsLang): string;
14
+ /** A duration as the owner reads it: "14d" is "14 days", "1h" is "1 hour". Anything else is shown as it is. */
15
+ export declare function durationText(value: string, lang: SettingsLang, t?: SettingsStrings): string;
package/dist/words.js CHANGED
@@ -43,6 +43,10 @@ export const SETTINGS_WORD_KEYS = [
43
43
  "problem.whole",
44
44
  "problem.too_small",
45
45
  "problem.too_big",
46
+ "problem.duration",
47
+ "problem.money",
48
+ "problem.too_short_time",
49
+ "problem.too_long_time",
46
50
  "problem.failed",
47
51
  "problem.too_soon",
48
52
  "problem.too_many",
@@ -54,6 +58,12 @@ export const SETTINGS_WORD_KEYS = [
54
58
  "problem.check",
55
59
  "map.empty",
56
60
  "carry.test_only",
61
+ "time.m",
62
+ "time.m_one",
63
+ "time.h",
64
+ "time.h_one",
65
+ "time.d",
66
+ "time.d_one",
57
67
  ];
58
68
  const en = {
59
69
  title: "Settings",
@@ -91,6 +101,10 @@ const en = {
91
101
  "problem.whole": "Type a whole number, such as 3.",
92
102
  "problem.too_small": "That's too small: {min} at least.",
93
103
  "problem.too_big": "That's too big: {max} at most.",
104
+ "problem.duration": "Type a length of time, such as 30m, 24h or 2d (minutes, hours or days).",
105
+ "problem.money": "Type an amount, such as 50 or 50.00.",
106
+ "problem.too_short_time": "That's too short: at least {min}.",
107
+ "problem.too_long_time": "That's too long: {max} at most.",
94
108
  "problem.failed": "The settings can't be saved right now. Try again in a minute.",
95
109
  "email.subject": "Your code for {app}",
96
110
  "email.body": "Someone asked for {app} to use this address for \"{setting}\".\n\nIf that was you, type this code on the Settings page: {code}\n\nIt works for 24 hours. If it wasn't you, ignore this email: nothing changes.",
@@ -105,6 +119,12 @@ const en = {
105
119
  "problem.check": "{message}",
106
120
  "map.empty": "Nothing chosen yet.",
107
121
  "carry.test_only": "On the test site only: it reaches the live site with the next release you approve.",
122
+ "time.m": "{n} minutes",
123
+ "time.m_one": "1 minute",
124
+ "time.h": "{n} hours",
125
+ "time.h_one": "1 hour",
126
+ "time.d": "{n} days",
127
+ "time.d_one": "1 day",
108
128
  };
109
129
  const tl = {
110
130
  title: "Mga setting",
@@ -142,6 +162,10 @@ const tl = {
142
162
  "problem.whole": "Mag-type ng buong numero, gaya ng 3.",
143
163
  "problem.too_small": "Masyadong maliit: kahit {min}.",
144
164
  "problem.too_big": "Masyadong malaki: hanggang {max} lang.",
165
+ "problem.duration": "Mag-type ng haba ng oras, gaya ng 30m, 24h o 2d (minuto, oras o araw).",
166
+ "problem.money": "Mag-type ng halaga, gaya ng 50 o 50.00.",
167
+ "problem.too_short_time": "Masyadong maikli: hindi bababa sa {min}.",
168
+ "problem.too_long_time": "Masyadong mahaba: hanggang {max} lang.",
145
169
  "problem.failed": "Hindi ma-save ang mga setting ngayon. Subukan ulit pagkalipas ng isang minuto.",
146
170
  "email.subject": "Ang code mo para sa {app}",
147
171
  "email.body": "May humiling na gamitin ng {app} ang address na ito para sa \"{setting}\".\n\nKung ikaw iyon, i-type ang code na ito sa page ng Mga setting: {code}\n\nGumagana ito nang 24 oras. Kung hindi ikaw, huwag pansinin ang email na ito: walang magbabago.",
@@ -156,6 +180,12 @@ const tl = {
156
180
  "problem.check": "{message}",
157
181
  "map.empty": "Wala pang napili.",
158
182
  "carry.test_only": "Sa test site lang: mapupunta ito sa live site sa susunod na release na aaprubahan mo.",
183
+ "time.m": "{n} minuto",
184
+ "time.m_one": "1 minuto",
185
+ "time.h": "{n} oras",
186
+ "time.h_one": "1 oras",
187
+ "time.d": "{n} araw",
188
+ "time.d_one": "1 araw",
159
189
  };
160
190
  const taglish = {
161
191
  title: "Settings",
@@ -193,6 +223,10 @@ const taglish = {
193
223
  "problem.whole": "Mag-type ng whole number, like 3.",
194
224
  "problem.too_small": "Masyadong maliit: at least {min}.",
195
225
  "problem.too_big": "Masyadong malaki: {max} max.",
226
+ "problem.duration": "Mag-type ng length of time, like 30m, 24h o 2d (minutes, hours o days).",
227
+ "problem.money": "Mag-type ng amount, like 50 o 50.00.",
228
+ "problem.too_short_time": "Masyadong short: at least {min}.",
229
+ "problem.too_long_time": "Masyadong long: {max} max.",
196
230
  "problem.failed": "Hindi ma-save ang settings ngayon. Try ulit after a minute.",
197
231
  "email.subject": "Ang code mo para sa {app}",
198
232
  "email.body": "May nag-request na gamitin ng {app} ang address na ito para sa \"{setting}\".\n\nKung ikaw yun, i-type ang code na ito sa Settings page: {code}\n\nValid ito for 24 hours. Kung hindi ikaw, i-ignore lang ang email na ito: walang magbabago.",
@@ -207,6 +241,12 @@ const taglish = {
207
241
  "problem.check": "{message}",
208
242
  "map.empty": "Wala pang napili.",
209
243
  "carry.test_only": "Sa test site lang: mapupunta ito sa live site sa next release na i-approve mo.",
244
+ "time.m": "{n} minutes",
245
+ "time.m_one": "1 minute",
246
+ "time.h": "{n} hours",
247
+ "time.h_one": "1 hour",
248
+ "time.d": "{n} days",
249
+ "time.d_one": "1 day",
210
250
  };
211
251
  /** The words by language, as `ui.wbsettings.<key>` would hold them. */
212
252
  export const SETTINGS_WORDS = { en, tl, taglish };
@@ -219,3 +259,11 @@ export function settingsWord(key, lang, values = {}, t) {
219
259
  export function phrase(words, lang) {
220
260
  return words[lang] ?? words.en;
221
261
  }
262
+ /** A duration as the owner reads it: "14d" is "14 days", "1h" is "1 hour". Anything else is shown as it is. */
263
+ export function durationText(value, lang, t) {
264
+ const m = /^(\d+)(m|h|d)$/.exec(value);
265
+ if (!m)
266
+ return value;
267
+ const key = `time.${m[2]}${m[1] === "1" ? "_one" : ""}`;
268
+ return settingsWord(key, lang, { n: m[1] }, t);
269
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mccaskie-labs/workbench-settings",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "One Settings page in a Workbench app's admin: each tool declares the settings an owner may change, the page checks and saves them in the app's own database, and the tools read them when they run, so a change needs no redeploy. A new email address is proved with an emailed code first. No Gateway call of its own.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",