@mccaskie-labs/workbench-settings 0.0.0-stage → 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 +37 -0
- package/README.md +54 -2
- package/dist/check.d.ts +40 -0
- package/dist/check.js +263 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +50 -0
- package/dist/design.d.ts +20 -0
- package/dist/design.js +59 -0
- package/dist/email.d.ts +2 -0
- package/dist/email.js +46 -0
- package/dist/errors.d.ts +31 -0
- package/dist/errors.js +14 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +18 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +329 -0
- package/dist/page.d.ts +58 -0
- package/dist/page.js +232 -0
- package/dist/samples.d.ts +18 -0
- package/dist/samples.js +96 -0
- package/dist/settings.d.ts +136 -0
- package/dist/settings.js +288 -0
- package/dist/styles.d.ts +34 -0
- package/dist/styles.js +84 -0
- package/dist/types.d.ts +120 -0
- package/dist/types.js +5 -0
- package/dist/words.d.ts +15 -0
- package/dist/words.js +269 -0
- package/package.json +52 -4
package/dist/settings.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
// createSettings: the values an app's owner set on the Settings page, worked
|
|
2
|
+
// out over the project's answers and each tool's default (docs/project-settings.md §2),
|
|
3
|
+
// saved and put back with a record of who did it, and a checked change proved
|
|
4
|
+
// with an emailed code before it is used (§5).
|
|
5
|
+
import { checkValue, sameValue, settingsProblems } from "./check.js";
|
|
6
|
+
import { SettingsError } from "./errors.js";
|
|
7
|
+
import { phrase, settingsWord } from "./words.js";
|
|
8
|
+
const ACCOUNT = "";
|
|
9
|
+
const id = (tool, key) => `${tool}\u0000${key}`;
|
|
10
|
+
function sixDigits() {
|
|
11
|
+
const a = new Uint32Array(1);
|
|
12
|
+
// Rejection sampling keeps every code equally likely.
|
|
13
|
+
for (;;) {
|
|
14
|
+
crypto.getRandomValues(a);
|
|
15
|
+
if (a[0] < 4_294_000_000)
|
|
16
|
+
return String(a[0] % 1_000_000).padStart(6, "0");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function createSettings(opts) {
|
|
20
|
+
const problems = settingsProblems(opts.tools);
|
|
21
|
+
if (problems.length)
|
|
22
|
+
throw new SettingsError("bad_declaration", problems.join("; "));
|
|
23
|
+
if (!opts.appName?.trim())
|
|
24
|
+
throw new SettingsError("bad_declaration", "appName is required, for the code email");
|
|
25
|
+
const now = opts.now ?? (() => new Date());
|
|
26
|
+
const cacheMs = opts.cacheMs ?? 60_000;
|
|
27
|
+
const makeCode = opts.code ?? sixDigits;
|
|
28
|
+
let snap = null;
|
|
29
|
+
// Bumped on every write, so a read that started before it can't put old values back.
|
|
30
|
+
let generation = 0;
|
|
31
|
+
const fresh = () => {
|
|
32
|
+
snap = null;
|
|
33
|
+
generation++;
|
|
34
|
+
};
|
|
35
|
+
/** The admin's name or email as stored: trimmed, 1 to 200 characters. */
|
|
36
|
+
const who = (by) => {
|
|
37
|
+
const b = String(by ?? "").trim();
|
|
38
|
+
if (!b || [...b].length > 200)
|
|
39
|
+
throw new SettingsError("bad_input", "by must be the admin's name or email, 1 to 200 characters");
|
|
40
|
+
return b;
|
|
41
|
+
};
|
|
42
|
+
const defOf = (tool, key) => {
|
|
43
|
+
const def = opts.tools.find((t) => t.tool === tool)?.settings.find((s) => s.key === key);
|
|
44
|
+
if (!def)
|
|
45
|
+
throw new SettingsError("unknown_setting", `No setting ${tool}.${key} is declared`);
|
|
46
|
+
return def;
|
|
47
|
+
};
|
|
48
|
+
async function call(fn, args) {
|
|
49
|
+
const { data, error } = await opts.db.rpc(fn, args);
|
|
50
|
+
if (error)
|
|
51
|
+
throw new SettingsError("store_failed", `${fn} failed: ${error.message ?? error.code ?? "unknown error"}`);
|
|
52
|
+
return data;
|
|
53
|
+
}
|
|
54
|
+
async function load() {
|
|
55
|
+
if (snap && now().getTime() - snap.at < cacheMs)
|
|
56
|
+
return snap;
|
|
57
|
+
const started = generation;
|
|
58
|
+
const data = await call("workbench_settings_read", { p_account: ACCOUNT });
|
|
59
|
+
const read = {
|
|
60
|
+
at: now().getTime(),
|
|
61
|
+
values: new Map(data.values.map((v) => [id(v.tool, v.key), v])),
|
|
62
|
+
pending: new Map(data.pending.map((p) => [id(p.tool, p.key), p])),
|
|
63
|
+
carried: new Map((data.carried ?? []).map((c) => [id(c.tool, c.key), c])),
|
|
64
|
+
};
|
|
65
|
+
if (started === generation)
|
|
66
|
+
snap = read;
|
|
67
|
+
return read;
|
|
68
|
+
}
|
|
69
|
+
/** The project's answer for a declared setting, when it passes the setting's own check. */
|
|
70
|
+
function answerOf(tool, def) {
|
|
71
|
+
if (!def.answers)
|
|
72
|
+
return null;
|
|
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;
|
|
76
|
+
if (raw === undefined || raw === null)
|
|
77
|
+
return null;
|
|
78
|
+
const c = checkValue(def, raw);
|
|
79
|
+
return c.ok ? c.value : null;
|
|
80
|
+
}
|
|
81
|
+
const toolOf = (tool) => opts.tools.find((t) => t.tool === tool);
|
|
82
|
+
function stateOf(tool, def, s) {
|
|
83
|
+
const stored = s.values.get(id(tool, def.key));
|
|
84
|
+
const waiting = s.pending.get(id(tool, def.key));
|
|
85
|
+
const pending = waiting && new Date(waiting.expires_at).getTime() > now().getTime() ? { value: waiting.value, expiresAt: waiting.expires_at } : undefined;
|
|
86
|
+
const extra = { ...(pending ? { pending } : {}) };
|
|
87
|
+
// A stored value that no longer passes its check (a declaration tightened in an update) is ignored.
|
|
88
|
+
const owned = stored && checkValue(def, stored.value).ok ? stored : undefined;
|
|
89
|
+
if (toolOf(tool)?.carries) {
|
|
90
|
+
// What the live site holds is the owner's value as last carried; nothing carried yet means "not changed".
|
|
91
|
+
const carried = s.carried.get(id(tool, def.key))?.value ?? null;
|
|
92
|
+
Object.assign(extra, { carry: sameValue(owned?.value ?? null, carried) ? "carried" : "test_only" });
|
|
93
|
+
}
|
|
94
|
+
if (owned)
|
|
95
|
+
return { tool, key: def.key, def, value: owned.value, from: "owner", changedBy: owned.changed_by, changedAt: owned.changed_at, ...extra };
|
|
96
|
+
const answer = answerOf(tool, def);
|
|
97
|
+
if (answer !== null)
|
|
98
|
+
return { tool, key: def.key, def, value: answer, from: "answers", ...extra };
|
|
99
|
+
return { tool, key: def.key, def, value: def.default ?? null, from: "default", ...extra };
|
|
100
|
+
}
|
|
101
|
+
const carriedTool = (tool) => {
|
|
102
|
+
const t = toolOf(tool);
|
|
103
|
+
if (!t)
|
|
104
|
+
throw new SettingsError("unknown_setting", `No tool ${tool} is declared`);
|
|
105
|
+
if (!t.carries)
|
|
106
|
+
throw new SettingsError("bad_input", `${tool} isn't marked carries`);
|
|
107
|
+
return t;
|
|
108
|
+
};
|
|
109
|
+
return {
|
|
110
|
+
tools: opts.tools,
|
|
111
|
+
async get(tool, key) {
|
|
112
|
+
const def = defOf(tool, key);
|
|
113
|
+
return stateOf(tool, def, await load()).value;
|
|
114
|
+
},
|
|
115
|
+
async states() {
|
|
116
|
+
const s = await load();
|
|
117
|
+
return opts.tools.flatMap((t) => t.settings.map((def) => stateOf(t.tool, def, s)));
|
|
118
|
+
},
|
|
119
|
+
async save(tool, key, raw, by, o = {}) {
|
|
120
|
+
const def = defOf(tool, key);
|
|
121
|
+
const c = checkValue(def, raw);
|
|
122
|
+
if (!c.ok)
|
|
123
|
+
throw new SettingsError("bad_value", `${tool}.${key}: ${c.problem}`, { word: c.problem, ...(c.values ? { values: c.values } : {}), ...(c.words ? { words: c.words } : {}) });
|
|
124
|
+
by = who(by);
|
|
125
|
+
const current = stateOf(tool, def, await load());
|
|
126
|
+
if (sameValue(current.value, c.value)) {
|
|
127
|
+
// Typing the value already in force drops a change waiting for its code.
|
|
128
|
+
if (current.pending)
|
|
129
|
+
await this.cancel(tool, key);
|
|
130
|
+
return { status: "unchanged" };
|
|
131
|
+
}
|
|
132
|
+
if (c.value === null) {
|
|
133
|
+
// An empty field is a put-back for a setting the owner changed; otherwise nothing to do.
|
|
134
|
+
if (current.from !== "owner")
|
|
135
|
+
return { status: "unchanged" };
|
|
136
|
+
await this.putBack(tool, key, by);
|
|
137
|
+
return { status: "saved", value: null };
|
|
138
|
+
}
|
|
139
|
+
if (def.kind === "checked") {
|
|
140
|
+
if (!opts.mailer)
|
|
141
|
+
throw new SettingsError("mailer_needed", `${tool}.${key} is checked by an emailed code, and createSettings was given no mailer`);
|
|
142
|
+
const code = makeCode();
|
|
143
|
+
const r = await call("workbench_settings_request", {
|
|
144
|
+
p_tool: tool,
|
|
145
|
+
p_key: key,
|
|
146
|
+
p_account: ACCOUNT,
|
|
147
|
+
p_value: c.value,
|
|
148
|
+
p_code: code,
|
|
149
|
+
p_by: by,
|
|
150
|
+
p_now: now().toISOString(),
|
|
151
|
+
});
|
|
152
|
+
fresh();
|
|
153
|
+
if (r.status === "too_soon")
|
|
154
|
+
throw new SettingsError("too_soon", `${tool}.${key}: a code was sent less than a minute ago`);
|
|
155
|
+
if (r.status === "too_many")
|
|
156
|
+
throw new SettingsError("too_many", `${tool}.${key}: five codes were sent in the last day`);
|
|
157
|
+
const lang = o.lang ?? "en";
|
|
158
|
+
const values = { app: opts.appName, code, setting: phrase(def.label, lang) };
|
|
159
|
+
try {
|
|
160
|
+
await opts.mailer.send({
|
|
161
|
+
to: String(c.value),
|
|
162
|
+
subject: settingsWord("email.subject", lang, values, o.t),
|
|
163
|
+
text: settingsWord("email.body", lang, values, o.t),
|
|
164
|
+
template: "settings_code",
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
// Nothing waits for a code that never went.
|
|
169
|
+
await call("workbench_settings_cancel", { p_tool: tool, p_key: key, p_account: ACCOUNT }).catch(() => { });
|
|
170
|
+
fresh();
|
|
171
|
+
throw new SettingsError("send_failed", `${tool}.${key}: the code email couldn't be sent (${e instanceof Error ? e.name : "error"})`);
|
|
172
|
+
}
|
|
173
|
+
return { status: "pending", value: c.value, expiresAt: r.expires_at };
|
|
174
|
+
}
|
|
175
|
+
await call("workbench_settings_save", { p_tool: tool, p_key: key, p_account: ACCOUNT, p_value: c.value, p_by: by, p_now: now().toISOString() });
|
|
176
|
+
fresh();
|
|
177
|
+
return { status: "saved", value: c.value };
|
|
178
|
+
},
|
|
179
|
+
async cancel(tool, key) {
|
|
180
|
+
defOf(tool, key);
|
|
181
|
+
const r = await call("workbench_settings_cancel", { p_tool: tool, p_key: key, p_account: ACCOUNT });
|
|
182
|
+
fresh();
|
|
183
|
+
return r;
|
|
184
|
+
},
|
|
185
|
+
async confirm(tool, key, code, by) {
|
|
186
|
+
defOf(tool, key);
|
|
187
|
+
const r = await call("workbench_settings_confirm", {
|
|
188
|
+
p_tool: tool,
|
|
189
|
+
p_key: key,
|
|
190
|
+
p_account: ACCOUNT,
|
|
191
|
+
p_code: String(code).trim(),
|
|
192
|
+
p_by: who(by),
|
|
193
|
+
p_now: now().toISOString(),
|
|
194
|
+
});
|
|
195
|
+
fresh();
|
|
196
|
+
if (r.status === "saved")
|
|
197
|
+
return { status: "saved", value: r.value };
|
|
198
|
+
if (r.status === "wrong")
|
|
199
|
+
return { status: "wrong", left: r.left ?? 0 };
|
|
200
|
+
return { status: r.status };
|
|
201
|
+
},
|
|
202
|
+
async putBack(tool, key, by) {
|
|
203
|
+
defOf(tool, key);
|
|
204
|
+
const r = await call("workbench_settings_put_back", { p_tool: tool, p_key: key, p_account: ACCOUNT, p_by: who(by), p_now: now().toISOString() });
|
|
205
|
+
fresh();
|
|
206
|
+
return { changed: r.changed };
|
|
207
|
+
},
|
|
208
|
+
async history(limit = 50) {
|
|
209
|
+
const rows = await call("workbench_settings_history", { p_account: ACCOUNT, p_limit: limit });
|
|
210
|
+
return rows.map((r) => ({ tool: r.tool, key: r.key, action: r.action, before: r.before, after: r.after, by: r.changed_by, at: r.changed_at }));
|
|
211
|
+
},
|
|
212
|
+
async cleanup() {
|
|
213
|
+
return call("workbench_settings_cleanup", { p_now: now().toISOString() });
|
|
214
|
+
},
|
|
215
|
+
forget() {
|
|
216
|
+
fresh();
|
|
217
|
+
},
|
|
218
|
+
async exportGroup(tool) {
|
|
219
|
+
const t = carriedTool(tool);
|
|
220
|
+
const s = await load();
|
|
221
|
+
return t.settings.map((def) => {
|
|
222
|
+
const st = stateOf(tool, def, s);
|
|
223
|
+
return { key: def.key, value: st.from === "owner" ? st.value : null };
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
async importGroup(tool, rows, by) {
|
|
227
|
+
const t = carriedTool(tool);
|
|
228
|
+
by = who(by);
|
|
229
|
+
const byKey = new Map(rows.map((r) => [r.key, r.value]));
|
|
230
|
+
const checked = t.settings.map((def) => {
|
|
231
|
+
const c = checkValue(def, byKey.get(def.key) ?? null);
|
|
232
|
+
if (!c.ok)
|
|
233
|
+
throw new SettingsError("bad_value", `${tool}.${def.key}: ${c.problem}`, { word: c.problem });
|
|
234
|
+
return { def, value: c.value };
|
|
235
|
+
});
|
|
236
|
+
for (const r of rows)
|
|
237
|
+
if (!t.settings.some((d) => d.key === r.key))
|
|
238
|
+
throw new SettingsError("unknown_setting", `No setting ${tool}.${r.key} is declared`);
|
|
239
|
+
const s = await load();
|
|
240
|
+
let changed = 0;
|
|
241
|
+
for (const { def, value } of checked) {
|
|
242
|
+
const st = stateOf(tool, def, s);
|
|
243
|
+
if (value === null) {
|
|
244
|
+
if (st.from === "owner") {
|
|
245
|
+
await call("workbench_settings_put_back", { p_tool: tool, p_key: def.key, p_account: ACCOUNT, p_by: by, p_now: now().toISOString() });
|
|
246
|
+
changed++;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
else if (!(st.from === "owner" && sameValue(st.value, value))) {
|
|
250
|
+
await call("workbench_settings_save", { p_tool: tool, p_key: def.key, p_account: ACCOUNT, p_value: value, p_by: by, p_now: now().toISOString() });
|
|
251
|
+
changed++;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
fresh();
|
|
255
|
+
return { changed };
|
|
256
|
+
},
|
|
257
|
+
async markCarried(tool, rows, by) {
|
|
258
|
+
const t = carriedTool(tool);
|
|
259
|
+
for (const r of rows) {
|
|
260
|
+
const def = t.settings.find((d) => d.key === r.key);
|
|
261
|
+
if (!def)
|
|
262
|
+
throw new SettingsError("unknown_setting", `No setting ${tool}.${r.key} is declared`);
|
|
263
|
+
if (!checkValue(def, r.value).ok)
|
|
264
|
+
throw new SettingsError("bad_value", `${tool}.${r.key}: doesn't pass its check`);
|
|
265
|
+
}
|
|
266
|
+
await call("workbench_settings_carried", {
|
|
267
|
+
p_tool: tool,
|
|
268
|
+
p_account: ACCOUNT,
|
|
269
|
+
p_rows: rows.map((r) => ({ key: r.key, value: r.value })),
|
|
270
|
+
p_by: who(by),
|
|
271
|
+
p_now: now().toISOString(),
|
|
272
|
+
});
|
|
273
|
+
fresh();
|
|
274
|
+
},
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Gives a tool's `business` callback the owner's reply-to address: the
|
|
279
|
+
* address on the Settings page, else what `base` returns. Bookings, orders,
|
|
280
|
+
* progress, tracker and agreements take this shape.
|
|
281
|
+
*/
|
|
282
|
+
export function withReplyTo(settings, base) {
|
|
283
|
+
return async (...args) => {
|
|
284
|
+
const b = await base(...args);
|
|
285
|
+
const replyTo = await settings.get("email", "reply_to");
|
|
286
|
+
return typeof replyTo === "string" ? { ...b, replyTo } : b;
|
|
287
|
+
};
|
|
288
|
+
}
|
package/dist/styles.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** One CSS custom property the rules read, with its default. */
|
|
2
|
+
interface StyleVariable {
|
|
3
|
+
/** `--wb-<tool>-<what>`. */
|
|
4
|
+
name: string;
|
|
5
|
+
value: string;
|
|
6
|
+
about: string;
|
|
7
|
+
}
|
|
8
|
+
/** A rule for a class in another state, or for an element inside it. */
|
|
9
|
+
interface StyleState {
|
|
10
|
+
selector: string;
|
|
11
|
+
about: string;
|
|
12
|
+
css: string;
|
|
13
|
+
}
|
|
14
|
+
/** One class the tool puts on an element, with its default rule. */
|
|
15
|
+
interface StyleClass {
|
|
16
|
+
name: string;
|
|
17
|
+
element: string;
|
|
18
|
+
about: string;
|
|
19
|
+
css: string;
|
|
20
|
+
states?: readonly StyleState[];
|
|
21
|
+
}
|
|
22
|
+
/** Every class and variable the tool draws with. */
|
|
23
|
+
export interface ToolStyles {
|
|
24
|
+
format: 1;
|
|
25
|
+
tool: string;
|
|
26
|
+
package: string;
|
|
27
|
+
title: string;
|
|
28
|
+
prefix: string;
|
|
29
|
+
variables: readonly StyleVariable[];
|
|
30
|
+
classes: readonly StyleClass[];
|
|
31
|
+
}
|
|
32
|
+
/** The style hooks of the Settings page (renderSettingsPage). */
|
|
33
|
+
export declare const SETTINGS_STYLES: ToolStyles;
|
|
34
|
+
export {};
|
package/dist/styles.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// The look the Settings page starts with (docs/capabilities-manifest.md §10).
|
|
2
|
+
// Every element renderSettingsPage draws carries one of these classes, and
|
|
3
|
+
// every class has a default rule here, so the page looks finished out of the
|
|
4
|
+
// box and a project restyles it from its own CSS by the same names. `npx
|
|
5
|
+
// workbench-ui styles` writes these into the project's
|
|
6
|
+
// public/styles/workbench.css. The names are public: renaming one is a
|
|
7
|
+
// breaking change.
|
|
8
|
+
//
|
|
9
|
+
// Colours, fonts and corners default to the shared theme tokens
|
|
10
|
+
// (`--wb-accent`, `--wb-text`: workbench-ui's THEME_TOKENS, docs/theming.md),
|
|
11
|
+
// so the project's theme reaches the page; each has a plain fallback for a
|
|
12
|
+
// project whose workbench.css predates the theme layer.
|
|
13
|
+
const v = (name) => `var(--wb-settings-${name})`;
|
|
14
|
+
const focus = { selector: ":focus-visible", about: "Has focus", css: `outline:none;box-shadow:0 0 0 3px ${v("focus")}` };
|
|
15
|
+
const control = `box-sizing:border-box;width:100%;max-width:28rem;min-height:2.75rem;padding:.5rem .75rem;font:inherit;color:${v("text")};background:${v("surface")};border:1px solid ${v("line")};border-radius:${v("radius-small")}`;
|
|
16
|
+
const button = `display:inline-flex;align-items:center;justify-content:center;min-height:2.75rem;padding:0 1.25rem;font:inherit;font-weight:600;border-radius:${v("radius-small")};cursor:pointer`;
|
|
17
|
+
const invalid = { selector: '[aria-invalid="true"]', about: "What was typed has a problem", css: `border-color:${v("danger")}` };
|
|
18
|
+
/** The style hooks of the Settings page (renderSettingsPage). */
|
|
19
|
+
export const SETTINGS_STYLES = {
|
|
20
|
+
format: 1,
|
|
21
|
+
tool: "settings",
|
|
22
|
+
package: "@mccaskie-labs/workbench-settings",
|
|
23
|
+
title: "Settings",
|
|
24
|
+
prefix: "wb-settings-",
|
|
25
|
+
variables: [
|
|
26
|
+
{ name: "--wb-settings-text", value: "var(--wb-text, #1d1d22)", about: "Text" },
|
|
27
|
+
{ name: "--wb-settings-muted", value: "var(--wb-muted, #5d5d68)", about: "Quieter text: help, where a value came from, the history" },
|
|
28
|
+
{ name: "--wb-settings-surface", value: "var(--wb-surface, #ffffff)", about: "Background of each section and the fields" },
|
|
29
|
+
{ name: "--wb-settings-line", value: "var(--wb-line, #dcdce2)", about: "Borders and rules" },
|
|
30
|
+
{ name: "--wb-settings-accent", value: "var(--wb-accent, #1b66d6)", about: "The Save button" },
|
|
31
|
+
{ name: "--wb-settings-accent-hover", value: "var(--wb-accent-hover, #1554b3)", about: "The Save button under the pointer" },
|
|
32
|
+
{ name: "--wb-settings-on-accent", value: "var(--wb-on-accent, #ffffff)", about: "Text on the Save button" },
|
|
33
|
+
{ name: "--wb-settings-success", value: "var(--wb-success, #1f7a3d)", about: "The saved notice" },
|
|
34
|
+
{ name: "--wb-settings-warning", value: "var(--wb-warning, #8a5a00)", about: "The waiting-for-a-code note" },
|
|
35
|
+
{ name: "--wb-settings-danger", value: "var(--wb-danger, #b3261e)", about: "A problem with what was typed" },
|
|
36
|
+
{ name: "--wb-settings-focus", value: "var(--wb-focus, rgba(27,102,214,.35))", about: "The ring round a control with focus" },
|
|
37
|
+
{ name: "--wb-settings-font", value: "var(--wb-font, system-ui, sans-serif)", about: "The page's text" },
|
|
38
|
+
{ name: "--wb-settings-font-heading", value: "var(--wb-font-heading, var(--wb-settings-font))", about: "The headings" },
|
|
39
|
+
{ name: "--wb-settings-radius", value: "var(--wb-radius, 8px)", about: "Corner rounding of each section" },
|
|
40
|
+
{ name: "--wb-settings-radius-small", value: "var(--wb-radius-small, 6px)", about: "Corner rounding of fields and buttons" },
|
|
41
|
+
{ name: "--wb-settings-width", value: "48rem", about: "The page's widest" },
|
|
42
|
+
],
|
|
43
|
+
classes: [
|
|
44
|
+
{ name: "wb-settings", element: "div", about: "The whole page body", css: `display:grid;gap:1.25rem;box-sizing:border-box;max-width:${v("width")};margin:0 auto;color:${v("text")};font-family:${v("font")}` },
|
|
45
|
+
{ name: "wb-settings-title", element: "h1", about: "\"Settings\"", css: `margin:0;font-family:${v("font-heading")};font-size:1.75rem;line-height:1.2` },
|
|
46
|
+
{ name: "wb-settings-intro", element: "p", about: "The line under the title", css: `margin:0;color:${v("muted")}` },
|
|
47
|
+
{ name: "wb-settings-notice", element: "p", about: "What the last save did: \"Saved\"", css: `margin:0;padding:.75rem 1rem;border-left:4px solid ${v("success")};background:${v("surface")}` },
|
|
48
|
+
{ name: "wb-settings-problem", element: "p", about: "The settings couldn't be saved", css: `margin:0;padding:.75rem 1rem;border-left:4px solid ${v("danger")};background:${v("surface")}` },
|
|
49
|
+
{ name: "wb-settings-section", element: "section", about: "One tool's settings: \"Emails\"", css: `display:grid;gap:1rem;padding:1.25rem;background:${v("surface")};border:1px solid ${v("line")};border-radius:${v("radius")}` },
|
|
50
|
+
{ name: "wb-settings-heading", element: "h2", about: "A section's heading", css: `margin:0;font-family:${v("font-heading")};font-size:1.25rem;line-height:1.3` },
|
|
51
|
+
{ name: "wb-settings-about", element: "p", about: "The line under a section's heading", css: `margin:0;color:${v("muted")}` },
|
|
52
|
+
{ name: "wb-settings-setting", element: "div", about: "One setting, with its code form while one waits", css: `display:grid;gap:.75rem;padding-top:1rem;border-top:1px solid ${v("line")}` },
|
|
53
|
+
{ name: "wb-settings-item", element: "form", about: "One setting's form", css: "display:grid;gap:.5rem;margin:0" },
|
|
54
|
+
{ name: "wb-settings-label", element: "label", about: "A setting's name", css: "font-weight:600" },
|
|
55
|
+
{ name: "wb-settings-help", element: "p", about: "What a setting does", css: `margin:0;color:${v("muted")};font-size:.9375rem` },
|
|
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" },
|
|
58
|
+
{ name: "wb-settings-select", element: "select", about: "A choice", css: control, states: [focus, invalid] },
|
|
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" },
|
|
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` },
|
|
61
|
+
{ name: "wb-settings-source", element: "p", about: "Where the value came from: \"Set when the app was built\"", css: `margin:0;color:${v("muted")};font-size:.875rem` },
|
|
62
|
+
{ name: "wb-settings-carry", element: "p", about: "\"On the test site only\": a design choice the live site hasn't been given yet", css: `margin:0;padding-left:.75rem;border-left:4px solid ${v("warning")};font-size:.875rem` },
|
|
63
|
+
{ name: "wb-settings-map", element: "ul", about: "A list of choices made elsewhere (design mode's layouts)", css: "margin:0;padding-left:1.25rem;display:grid;gap:.25rem" },
|
|
64
|
+
{ name: "wb-settings-map-item", element: "li", about: "One choice in the list: \"bookings: list\"", css: "overflow-wrap:anywhere" },
|
|
65
|
+
{ name: "wb-settings-actions", element: "div", about: "A form's buttons", css: "display:flex;flex-wrap:wrap;gap:.5rem" },
|
|
66
|
+
{
|
|
67
|
+
name: "wb-settings-save",
|
|
68
|
+
element: "button",
|
|
69
|
+
about: "Save, or Use this address",
|
|
70
|
+
css: `${button};color:${v("on-accent")};background:${v("accent")};border:1px solid ${v("accent")}`,
|
|
71
|
+
states: [{ selector: ":hover", about: "Under the pointer", css: `background:${v("accent-hover")};border-color:${v("accent-hover")}` }, focus],
|
|
72
|
+
},
|
|
73
|
+
{ name: "wb-settings-put-back", element: "button", about: "Put back to how the app was built", css: `${button};color:${v("text")};background:transparent;border:1px solid ${v("line")}`, states: [focus] },
|
|
74
|
+
{ name: "wb-settings-cancel", element: "button", about: "Cancel a change waiting for its code", css: `${button};color:${v("text")};background:transparent;border:1px solid ${v("line")}`, states: [focus] },
|
|
75
|
+
{ name: "wb-settings-pending", element: "form", about: "The code form while a new address waits", css: `display:grid;gap:.5rem;margin:0;padding:1rem;border-left:4px solid ${v("warning")};background:${v("surface")}` },
|
|
76
|
+
{ name: "wb-settings-pending-note", element: "p", about: "\"We've emailed a code to …\"", css: "margin:0" },
|
|
77
|
+
{ name: "wb-settings-history", element: "section", about: "Recent changes", css: "display:grid;gap:.75rem;overflow-x:auto" },
|
|
78
|
+
{ name: "wb-settings-empty", element: "p", about: "\"Nothing has been changed yet\"", css: `margin:0;color:${v("muted")}` },
|
|
79
|
+
{ name: "wb-settings-table", element: "table", about: "The changes", css: "width:100%;border-collapse:collapse;font-size:.9375rem" },
|
|
80
|
+
{ name: "wb-settings-row", element: "tr", about: "One change", css: "vertical-align:top" },
|
|
81
|
+
{ name: "wb-settings-th", element: "th", about: "A column's heading", css: `text-align:left;padding:.5rem .25rem;border-bottom:2px solid ${v("line")};color:${v("muted")};font-weight:600` },
|
|
82
|
+
{ name: "wb-settings-td", element: "td", about: "A cell", css: `padding:.5rem .25rem;border-bottom:1px solid ${v("line")};vertical-align:top` },
|
|
83
|
+
],
|
|
84
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/** The languages the page's words come in. A missing one falls back to English. */
|
|
2
|
+
export type SettingsLang = "en" | "tl" | "taglish";
|
|
3
|
+
/** A phrase in each language the page carries; `en` is required. */
|
|
4
|
+
export type Words = {
|
|
5
|
+
en: string;
|
|
6
|
+
} & Partial<Record<Exclude<SettingsLang, "en">, string>>;
|
|
7
|
+
/** The kind of value, which sets the field drawn and the check run. */
|
|
8
|
+
export type SettingType = "text" | "email" | "choice" | "whole" | "duration" | "money" | "map";
|
|
9
|
+
/**
|
|
10
|
+
* How a change takes effect (§5).
|
|
11
|
+
* - `live`: saved and used at once.
|
|
12
|
+
* - `checked`: proved first. For an email address, a six-digit code is
|
|
13
|
+
* emailed to the new address and the owner types it in; the old value
|
|
14
|
+
* stays until then.
|
|
15
|
+
*/
|
|
16
|
+
export type SettingKind = "live" | "checked";
|
|
17
|
+
/** One value an owner may change. */
|
|
18
|
+
export interface SettingDef {
|
|
19
|
+
/** Stable, `[a-z][a-z0-9_]*`. Never renamed: a rename is a new key. */
|
|
20
|
+
key: string;
|
|
21
|
+
type: SettingType;
|
|
22
|
+
kind: SettingKind;
|
|
23
|
+
label: Words;
|
|
24
|
+
help?: Words;
|
|
25
|
+
/** The value when neither the project's answers nor the owner set one. */
|
|
26
|
+
default?: string | number | null;
|
|
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
|
+
*/
|
|
32
|
+
answers?: string;
|
|
33
|
+
/** Text: the longest. Whole: the largest. Duration: the longest, in minutes. Money: the most, in cents. */
|
|
34
|
+
max?: number;
|
|
35
|
+
/** Text: the shortest (default 1). Whole: the smallest. Duration: the shortest, in minutes. Money: the least, in cents. */
|
|
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;
|
|
44
|
+
/** Choice: the values, in the order shown. */
|
|
45
|
+
choices?: readonly {
|
|
46
|
+
value: string;
|
|
47
|
+
label: Words;
|
|
48
|
+
}[];
|
|
49
|
+
/**
|
|
50
|
+
* True when the value decides where people's personal information goes
|
|
51
|
+
* (a reply-to address, where notices are sent). Such a setting can't be
|
|
52
|
+
* `live`: settingsProblems refuses it.
|
|
53
|
+
*/
|
|
54
|
+
personal?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* An extra check run at every save, after the type's own: null to accept
|
|
57
|
+
* the value, or the words to show the owner ("That colour doesn't read on
|
|
58
|
+
* white"). Keep it quick and pure: it runs on reads too, and a stored
|
|
59
|
+
* value it refuses is ignored.
|
|
60
|
+
*/
|
|
61
|
+
check?: (value: SettingValue) => Words | null;
|
|
62
|
+
}
|
|
63
|
+
/** Every setting one tool lets an owner change. */
|
|
64
|
+
export interface ToolSettings {
|
|
65
|
+
format: 1;
|
|
66
|
+
/** The tool's id, as its manifest's component: `email`, `bookings`. */
|
|
67
|
+
tool: string;
|
|
68
|
+
/** The section's heading: "Emails". */
|
|
69
|
+
title: Words;
|
|
70
|
+
/** One line under the heading. */
|
|
71
|
+
about?: Words;
|
|
72
|
+
settings: readonly SettingDef[];
|
|
73
|
+
/**
|
|
74
|
+
* The owner's values for this tool are tried on the test site and carried
|
|
75
|
+
* to the live one by the next release the owner approves
|
|
76
|
+
* (settings.exportGroup, importGroup, markCarried). Until then the page
|
|
77
|
+
* says "On the test site only". A carried tool can't hold a checked
|
|
78
|
+
* setting: a code proves an address on one site only.
|
|
79
|
+
*/
|
|
80
|
+
carries?: boolean;
|
|
81
|
+
}
|
|
82
|
+
/** A map setting's value: names to names, such as { bookings: "list", "orders@/shop": "grid" }. */
|
|
83
|
+
export type SettingMap = Readonly<Record<string, string>>;
|
|
84
|
+
/** A stored value: what the JSON column holds. */
|
|
85
|
+
export type SettingValue = string | number | SettingMap;
|
|
86
|
+
/** Where the value in force came from (§2). */
|
|
87
|
+
export type SettingSource = "default" | "answers" | "owner";
|
|
88
|
+
/** One setting as the page shows it. */
|
|
89
|
+
export interface SettingState {
|
|
90
|
+
tool: string;
|
|
91
|
+
key: string;
|
|
92
|
+
def: SettingDef;
|
|
93
|
+
value: SettingValue | null;
|
|
94
|
+
from: SettingSource;
|
|
95
|
+
/** Who changed it and when, when `from` is `owner`. */
|
|
96
|
+
changedBy?: string;
|
|
97
|
+
changedAt?: string;
|
|
98
|
+
/** A checked change waiting for its code: the new value and when the code stops working. */
|
|
99
|
+
pending?: {
|
|
100
|
+
value: SettingValue;
|
|
101
|
+
expiresAt: string;
|
|
102
|
+
};
|
|
103
|
+
/** For a tool that carries: "test_only" when the value in force here differs from the one last carried to the live site. */
|
|
104
|
+
carry?: "carried" | "test_only";
|
|
105
|
+
}
|
|
106
|
+
/** One setting of a carried tool, as exportGroup gives it and importGroup takes it: null puts it back. */
|
|
107
|
+
export interface CarriedSetting {
|
|
108
|
+
key: string;
|
|
109
|
+
value: SettingValue | null;
|
|
110
|
+
}
|
|
111
|
+
/** One row of Recent changes. */
|
|
112
|
+
export interface SettingChange {
|
|
113
|
+
tool: string;
|
|
114
|
+
key: string;
|
|
115
|
+
action: "save" | "put_back";
|
|
116
|
+
before: SettingValue | null;
|
|
117
|
+
after: SettingValue | null;
|
|
118
|
+
by: string;
|
|
119
|
+
at: string;
|
|
120
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// What a tool declares about the settings an app's owner may change on the
|
|
2
|
+
// Settings page (docs/project-settings.md §3). Data only: the page draws
|
|
3
|
+
// from it, the save checks against it, and the tool reads the value back
|
|
4
|
+
// with settings.get(tool, key).
|
|
5
|
+
export {};
|
package/dist/words.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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.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
|
+
export type SettingsWordKey = (typeof SETTINGS_WORD_KEYS)[number];
|
|
4
|
+
/** The words by language, as `ui.wbsettings.<key>` would hold them. */
|
|
5
|
+
export declare const SETTINGS_WORDS: Readonly<Record<SettingsLang, Readonly<Record<SettingsWordKey, string>>>>;
|
|
6
|
+
/** The app's own words for a key, or undefined to use the package's. */
|
|
7
|
+
export type SettingsStrings = (key: `ui.wbsettings.${SettingsWordKey}`, lang: SettingsLang) => string | undefined;
|
|
8
|
+
/** A word, from the app's catalogue first, then the package's, with {name} filled in. */
|
|
9
|
+
export declare function settingsWord(key: SettingsWordKey, lang: SettingsLang, values?: Readonly<Record<string, string>>, t?: SettingsStrings): string;
|
|
10
|
+
/** A declared phrase in a language, falling back to English. */
|
|
11
|
+
export declare function phrase(words: {
|
|
12
|
+
en: string;
|
|
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;
|