pi-pocket 0.26.0 → 0.27.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/CONTRIBUTING.md +1 -1
  2. package/docs/design/design-system.md +165 -0
  3. package/docs/design/ds-notes-b.md +61 -0
  4. package/docs/design/ds-notes-g.md +170 -0
  5. package/docs/design/ds-notes-t.md +303 -0
  6. package/docs/design/ds-notes-u.md +220 -0
  7. package/docs/design/ds-notes-v.md +186 -0
  8. package/docs/design/ds-rounds.md +160 -0
  9. package/docs/design/ds-u-bridge.patch +732 -0
  10. package/docs/examples/design-system/README.md +15 -0
  11. package/docs/examples/design-system/pocket/.pi/design/components/badge.html +112 -0
  12. package/docs/examples/design-system/pocket/.pi/design/components/button.html +203 -0
  13. package/docs/examples/design-system/pocket/.pi/design/components/card.html +125 -0
  14. package/docs/examples/design-system/pocket/.pi/design/components/code-block.html +155 -0
  15. package/docs/examples/design-system/pocket/.pi/design/components/input.html +156 -0
  16. package/docs/examples/design-system/pocket/.pi/design/components/menu-row.html +167 -0
  17. package/docs/examples/design-system/pocket/.pi/design/components/notice.html +171 -0
  18. package/docs/examples/design-system/pocket/.pi/design/components/segmented.html +120 -0
  19. package/docs/examples/design-system/pocket/.pi/design/components/switch.html +117 -0
  20. package/docs/examples/design-system/pocket/.pi/design/components/tool-step.html +163 -0
  21. package/docs/examples/design-system/pocket/.pi/design/dist/components.css +730 -0
  22. package/docs/examples/design-system/pocket/.pi/design/dist/names.md +495 -0
  23. package/docs/examples/design-system/pocket/.pi/design/dist/tailwind.css +1132 -0
  24. package/docs/examples/design-system/pocket/.pi/design/dist/tokens.css +1126 -0
  25. package/docs/examples/design-system/pocket/.pi/design/dist/tokens.dark.resolved.json +2022 -0
  26. package/docs/examples/design-system/pocket/.pi/design/dist/tokens.high-contrast.resolved.json +2022 -0
  27. package/docs/examples/design-system/pocket/.pi/design/dist/tokens.light.resolved.json +2022 -0
  28. package/docs/examples/design-system/pocket/.pi/design/dist/tokens.resolved.json +371 -0
  29. package/docs/examples/design-system/pocket/.pi/design/dist/tokens.studio.json +5915 -0
  30. package/docs/examples/design-system/pocket/.pi/design/guidelines.md +81 -0
  31. package/docs/examples/design-system/pocket/.pi/design/tokens.dark.json +182 -0
  32. package/docs/examples/design-system/pocket/.pi/design/tokens.high-contrast.json +185 -0
  33. package/docs/examples/design-system/pocket/.pi/design/tokens.json +2034 -0
  34. package/docs/features.md +6 -2
  35. package/docs/index.md +1 -0
  36. package/docs/map.md +34 -25
  37. package/docs/release-notes.md +1 -1
  38. package/docs/releases.md +45 -0
  39. package/package.json +2 -1
  40. package/src/server/design/check-cli.ts +283 -0
  41. package/src/server/design/color.ts +654 -0
  42. package/src/server/design/components.ts +328 -0
  43. package/src/server/design/docsite-assets.ts +29 -0
  44. package/src/server/design/docsite-color-section.ts +366 -0
  45. package/src/server/design/docsite-color.ts +303 -0
  46. package/src/server/design/docsite-components.ts +89 -0
  47. package/src/server/design/docsite-core.ts +634 -0
  48. package/src/server/design/docsite-css-base.ts +397 -0
  49. package/src/server/design/docsite-css-content.ts +477 -0
  50. package/src/server/design/docsite-css-phone.ts +251 -0
  51. package/src/server/design/docsite-css-reference.ts +374 -0
  52. package/src/server/design/docsite-css-specimens.ts +465 -0
  53. package/src/server/design/docsite-js-code.ts +96 -0
  54. package/src/server/design/docsite-js-core.ts +72 -0
  55. package/src/server/design/docsite-js-demos.ts +24 -0
  56. package/src/server/design/docsite-js-links.ts +25 -0
  57. package/src/server/design/docsite-js-nav.ts +73 -0
  58. package/src/server/design/docsite-js-stages.ts +81 -0
  59. package/src/server/design/docsite-js-table.ts +81 -0
  60. package/src/server/design/docsite-markdown.ts +221 -0
  61. package/src/server/design/docsite-motion.ts +110 -0
  62. package/src/server/design/docsite-reference.ts +269 -0
  63. package/src/server/design/docsite-sanitize.ts +193 -0
  64. package/src/server/design/docsite-shadows.ts +45 -0
  65. package/src/server/design/docsite-space.ts +289 -0
  66. package/src/server/design/docsite-type.ts +167 -0
  67. package/src/server/design/docsite.ts +508 -0
  68. package/src/server/design/export.ts +86 -9
  69. package/src/server/design/fix.ts +666 -0
  70. package/src/server/design/guide.ts +137 -22
  71. package/src/server/design/index.ts +368 -15
  72. package/src/server/design/lint-color.ts +293 -0
  73. package/src/server/design/lint-page-checks.ts +204 -0
  74. package/src/server/design/lint-page-elements.ts +252 -0
  75. package/src/server/design/lint-page-focus.ts +165 -0
  76. package/src/server/design/lint-page-literals.ts +293 -0
  77. package/src/server/design/lint-page-setup.ts +102 -0
  78. package/src/server/design/lint-page-states.ts +121 -0
  79. package/src/server/design/lint-page-values.ts +329 -0
  80. package/src/server/design/lint-page.ts +101 -0
  81. package/src/server/design/lint-suggest.ts +340 -0
  82. package/src/server/design/lint.ts +1136 -0
  83. package/src/server/design/render.ts +369 -18
  84. package/src/server/design/scan-braces.ts +115 -0
  85. package/src/server/design/scan-categorize.ts +187 -0
  86. package/src/server/design/scan-channels.ts +113 -0
  87. package/src/server/design/scan-color-base.ts +209 -0
  88. package/src/server/design/scan-color-names.ts +403 -0
  89. package/src/server/design/scan-color-palette.ts +91 -0
  90. package/src/server/design/scan-color-roles.ts +565 -0
  91. package/src/server/design/scan-colors.ts +22 -0
  92. package/src/server/design/scan-facts-antd.ts +171 -0
  93. package/src/server/design/scan-facts-files.ts +300 -0
  94. package/src/server/design/scan-facts-lists.ts +538 -0
  95. package/src/server/design/scan-facts-sass.ts +113 -0
  96. package/src/server/design/scan-facts-theme.ts +400 -0
  97. package/src/server/design/scan-facts-use.ts +674 -0
  98. package/src/server/design/scan-facts-wordpress.ts +374 -0
  99. package/src/server/design/scan-facts.ts +285 -0
  100. package/src/server/design/scan-files.ts +384 -0
  101. package/src/server/design/scan-import-convert.ts +277 -0
  102. package/src/server/design/scan-import-eval.ts +583 -0
  103. package/src/server/design/scan-import-material.ts +745 -0
  104. package/src/server/design/scan-import-sd-config.ts +370 -0
  105. package/src/server/design/scan-import-sd.ts +381 -0
  106. package/src/server/design/scan-import-studio.ts +242 -0
  107. package/src/server/design/scan-import.ts +336 -0
  108. package/src/server/design/scan-infer.ts +313 -0
  109. package/src/server/design/scan-js-eval.ts +1267 -0
  110. package/src/server/design/scan-palette.ts +485 -0
  111. package/src/server/design/scan-parse.ts +1134 -0
  112. package/src/server/design/scan-prepare.ts +402 -0
  113. package/src/server/design/scan-render-pages.ts +367 -0
  114. package/src/server/design/scan-render-probe.ts +229 -0
  115. package/src/server/design/scan-render.ts +524 -0
  116. package/src/server/design/scan-review.ts +54 -0
  117. package/src/server/design/scan-roles.ts +259 -0
  118. package/src/server/design/scan-sass-colors.ts +275 -0
  119. package/src/server/design/scan-sass-eval.ts +269 -0
  120. package/src/server/design/scan-sass-expr.ts +911 -0
  121. package/src/server/design/scan-sass-modules.ts +353 -0
  122. package/src/server/design/scan-sass-run.ts +1003 -0
  123. package/src/server/design/scan-sass-stmt.ts +256 -0
  124. package/src/server/design/scan-sass-values.ts +886 -0
  125. package/src/server/design/scan-scales.ts +983 -0
  126. package/src/server/design/scan-tailwind.ts +34 -0
  127. package/src/server/design/scan-theme-shapes.ts +579 -0
  128. package/src/server/design/scan-tree.ts +290 -0
  129. package/src/server/design/scan-util.ts +44 -0
  130. package/src/server/design/scan-variables.ts +470 -0
  131. package/src/server/design/scan.ts +703 -0
  132. package/src/server/design/system-schema.ts +68 -0
  133. package/src/server/design/system-types.ts +279 -0
  134. package/src/server/design/system.ts +510 -82
  135. package/src/server/design/tokens-base.ts +123 -0
  136. package/src/server/design/tokens-compile.ts +440 -0
  137. package/src/server/design/tokens-dtcg.ts +354 -0
  138. package/src/server/design/tokens-io.ts +417 -0
  139. package/src/server/design/tokens-model.ts +1163 -0
  140. package/src/server/design/tokens-names.ts +112 -0
  141. package/src/server/design/tokens-ramps.ts +112 -0
  142. package/src/server/design/tokens-summary.ts +317 -0
  143. package/src/server/design/tokens-tailwind.ts +324 -0
  144. package/src/server/design/tokens-values.ts +921 -0
  145. package/src/server/design/tokens.ts +533 -0
  146. package/src/server/docs.ts +8 -0
  147. package/src/server/extensions/artifacts.ts +87 -2
  148. package/src/server/extensions/design/app.js +280 -0
  149. package/src/server/extensions/design/canvas.js +142 -17
  150. package/src/server/extensions/design/check.js +656 -0
  151. package/src/server/extensions/design/color.js +383 -0
  152. package/src/server/extensions/design/data.js +102 -7
  153. package/src/server/extensions/design/devices.js +26 -0
  154. package/src/server/extensions/design/docks.js +117 -3
  155. package/src/server/extensions/design/index.js +171 -219
  156. package/src/server/extensions/design/messages.js +49 -0
  157. package/src/server/extensions/design/parts.js +15 -1
  158. package/src/server/extensions/design/paths.js +4 -0
  159. package/src/server/extensions/design/picker.js +123 -0
  160. package/src/server/extensions/design/specimen.js +55 -0
  161. package/src/server/extensions/design/state.js +72 -7
  162. package/src/server/extensions/design/style.css +546 -0
  163. package/src/server/extensions/design/system-actions.js +214 -0
  164. package/src/server/extensions/design/system-api.ts +62 -0
  165. package/src/server/extensions/design/system-data.js +315 -0
  166. package/src/server/extensions/design/system-preview.js +41 -0
  167. package/src/server/extensions/design/system-routes.ts +1122 -0
  168. package/src/server/extensions/design/system-sections.js +1344 -0
  169. package/src/server/extensions/design/system-ui.js +32 -0
  170. package/src/server/extensions/design/system-view.js +1196 -0
  171. package/src/server/extensions/design/system-widgets.js +942 -0
  172. package/src/server/extensions/design/system.css +2313 -0
  173. package/src/server/extensions/design/system.js +1479 -0
  174. package/src/server/extensions/design/unsaved.js +20 -0
  175. package/src/server/extensions/design/views.js +2 -2
  176. package/src/server/extensions/design/workspace.js +200 -105
  177. package/src/server/extensions/design.ts +89 -6
  178. package/src/server/extensions/updates/index.js +31 -9
  179. package/src/server/extensions/updates/style.css +11 -0
  180. package/src/server/extensions/updates.ts +29 -1
  181. package/src/server/host.ts +4 -1
  182. package/src/server/http/artifact-inspect.ts +338 -24
  183. package/src/server/updates.ts +235 -53
  184. package/src/server/versions.ts +71 -0
  185. package/web/artifact-bridge.js +34 -1
  186. package/npm-shrinkwrap.json +0 -3162
@@ -0,0 +1,1196 @@
1
+ // The System view: the project's design system as a place to work in. The bar (the pinned picker item, the mode in view, undo
2
+ // and redo), the sections, the unsaved edits and the bar that saves them, and, when there is room, the open design beside it
3
+ // that shows every edit live. A person who cannot steer sees all of it and changes none of it.
4
+ import { clampSize, DEVICES } from "./devices.js";
5
+ import { modeTitle } from "./system-widgets.js";
6
+ import {
7
+ changeOf,
8
+ createModel,
9
+ editsOf,
10
+ linkOf,
11
+ SECTIONS,
12
+ sectionCounts,
13
+ sectionOf,
14
+ shadowValue,
15
+ tokenEdits,
16
+ unsavedCount,
17
+ withPart,
18
+ } from "./system.js";
19
+
20
+ /** How often the open view asks whether Pi (or someone else) changed the system on disk. */
21
+ const REFRESH_MS = 20_000;
22
+
23
+ const MODE_ICONS = { light: "sun", dark: "moon" };
24
+
25
+ /** Whether the keys go to a field: undo and save then belong to it. */
26
+ const typing = (target) =>
27
+ target instanceof HTMLElement &&
28
+ (target.isContentEditable || ["INPUT", "TEXTAREA", "SELECT"].includes(target.tagName));
29
+
30
+ export function createSystemView(env, canvas, sections, widgets) {
31
+ const { pocket, store, parts, systemData } = env;
32
+ const { html, useEffect, useMemo, useRef, useState } = pocket;
33
+ const { Ico, Popover } = parts;
34
+
35
+ // ─── The bar ─────────────────────────────────────────────────────────────
36
+
37
+ /** Which mode is in view: each mode, and the modes side by side. */
38
+ function ModeSwitch({ s, modes, narrow }) {
39
+ const [menu, setMenu] = useState(false);
40
+
41
+ if (modes.length < 2) {
42
+ return null;
43
+ }
44
+
45
+ const current =
46
+ s.sysMode === "both" ? "both" : modes.includes(s.sysMode) ? s.sysMode : modes[0];
47
+ const options = [
48
+ ...modes.map((mode) => ({
49
+ value: mode,
50
+ label: modeTitle(mode),
51
+ icon: MODE_ICONS[mode] ?? "contrast",
52
+ })),
53
+ { value: "both", label: modes.length === 2 ? "Both" : "All", icon: "compare" },
54
+ ];
55
+
56
+ // Up to four modes: a segmented control, a button each (an icon on a phone, the name too where there is room).
57
+ // More than that: one button that shows the mode in view and opens the list.
58
+ if (modes.length > 4) {
59
+ const shown = options.find((option) => option.value === current) ?? options[0];
60
+
61
+ return html`<span class="dsy-modemenu">
62
+ <button
63
+ type="button"
64
+ class="dsy-modemenu-button"
65
+ aria-haspopup="dialog"
66
+ aria-expanded=${menu}
67
+ aria-label=${`Mode: ${shown.label}`}
68
+ onClick=${() => setMenu(!menu)}
69
+ >
70
+ <${Ico} name=${shown.icon} size=${15} />
71
+ <span>${shown.label}</span>
72
+ <${Ico} name="down" size=${14} />
73
+ </button>
74
+ ${
75
+ menu &&
76
+ html`<${Popover}
77
+ label="Mode"
78
+ align="end"
79
+ sheet=${narrow}
80
+ onClose=${() => setMenu(false)}
81
+ >
82
+ <div class="dsn-menu" role="group" aria-label="Mode">
83
+ ${options.map(
84
+ (option) =>
85
+ html`<button
86
+ type="button"
87
+ key=${option.value}
88
+ class=${`dsn-menu-item ${current === option.value ? "on" : ""}`}
89
+ aria-pressed=${current === option.value}
90
+ onClick=${() => {
91
+ setMenu(false);
92
+ env.setSysMode(option.value);
93
+ }}
94
+ >
95
+ <${Ico} name=${option.icon} size=${16} />
96
+ <span class="grow">${option.label}</span>
97
+ ${current === option.value && html`<${Ico} name="check" size=${15} />`}
98
+ </button>`,
99
+ )}
100
+ </div>
101
+ <//>`
102
+ }
103
+ </span>`;
104
+ }
105
+
106
+ return html`<div class="dsn-seg dsy-modes" role="group" aria-label="Mode">
107
+ ${options.map(
108
+ (option) =>
109
+ html`<button
110
+ type="button"
111
+ key=${option.value}
112
+ class=${current === option.value ? "on" : ""}
113
+ aria-pressed=${current === option.value}
114
+ title=${option.value === "both" ? "Show the modes side by side" : `Show ${option.label}`}
115
+ aria-label=${option.label}
116
+ onClick=${() => env.setSysMode(option.value)}
117
+ >
118
+ <${Ico} name=${option.icon} size=${15} />
119
+ ${!narrow && html`<span>${option.label}</span>`}
120
+ </button>`,
121
+ )}
122
+ </div>`;
123
+ }
124
+
125
+ /** What the system's menu offers: publish, export, modes, reload, and the conversation's room. */
126
+ function SystemMenu({ s, data, canEdit, onClose, onModes }) {
127
+ const [busy, setBusy] = useState(null);
128
+ const errors = data?.problems.some((problem) => problem.level === "error") ?? false;
129
+
130
+ const run = async (what, job) => {
131
+ setBusy(what);
132
+ await job();
133
+ setBusy(null);
134
+ onClose();
135
+ };
136
+
137
+ const item = (icon, label, hint, props) =>
138
+ html`<button type="button" class="dsn-menu-item" ...${props}>
139
+ <${Ico} name=${icon} size=${16} /><span class="grow">${label}</span
140
+ >${hint && html`<span class="muted small">${hint}</span>`}
141
+ </button>`;
142
+
143
+ return html`<${Popover}
144
+ label="Design system menu"
145
+ onClose=${onClose}
146
+ align="end"
147
+ sheet=${env.sizeClass(s) === "narrow"}
148
+ >
149
+ <div class="dsn-menu">
150
+ ${data && html`<div class="dsn-menu-label">${data.name}</div>`}
151
+ ${
152
+ data &&
153
+ canEdit &&
154
+ item(
155
+ "book",
156
+ busy === "docs" ? "Publishing…" : "Publish documentation",
157
+ "a design",
158
+ {
159
+ disabled: busy !== null || errors,
160
+ title: errors
161
+ ? "Fix the errors first"
162
+ : "Make the system's documentation page a design",
163
+ onClick: () =>
164
+ run("docs", async () => {
165
+ const made = await systemData.publishDocs();
166
+
167
+ if (made) {
168
+ env.open(made.id);
169
+ pocket.notify(
170
+ "info",
171
+ `Published as ${made.id}, version ${made.version}. Share it from the menu.`,
172
+ );
173
+ }
174
+ }),
175
+ },
176
+ )
177
+ }
178
+ ${
179
+ data &&
180
+ canEdit &&
181
+ item(
182
+ "box",
183
+ busy === "export" ? "Making the files…" : "Export for code",
184
+ "CSS, Tailwind, JSON",
185
+ {
186
+ disabled: busy !== null || errors,
187
+ title: errors
188
+ ? "Fix the errors first"
189
+ : "Download the generated files as a zip",
190
+ onClick: () => run("export", () => systemData.exportCode()),
191
+ },
192
+ )
193
+ }
194
+ ${
195
+ data &&
196
+ canEdit &&
197
+ item("layers", "Modes…", `${data.modes.length}`, {
198
+ onClick: () => {
199
+ onClose();
200
+ onModes();
201
+ },
202
+ })
203
+ }
204
+ ${item("reload", "Read it again", null, { onClick: () => run("reload", () => systemData.load()) })}
205
+ ${
206
+ env.beside() &&
207
+ item(
208
+ s.focus ? "unfocus" : "focus",
209
+ s.focus ? "Show the conversation" : "Hide the conversation",
210
+ null,
211
+ {
212
+ onClick: () => {
213
+ env.toggleFocus();
214
+ onClose();
215
+ },
216
+ },
217
+ )
218
+ }
219
+ </div>
220
+ <//>`;
221
+ }
222
+
223
+ /** Add a mode, or remove one. A removed mode loses its file. */
224
+ function ModesPopover({ data, onClose, narrow }) {
225
+ const [name, setName] = useState("");
226
+ const [busy, setBusy] = useState(false);
227
+ const [asking, setAsking] = useState(null);
228
+ const slug = name
229
+ .trim()
230
+ .toLowerCase()
231
+ .replace(/[^a-z0-9]+/g, "-")
232
+ .replace(/^-+|-+$/g, "");
233
+ const taken = data.modes.includes(slug);
234
+ const valid = /^[a-z][a-z0-9-]{0,31}$/.test(slug) && !taken && data.modes.length < 8;
235
+
236
+ return html`<${Popover} label="Modes" onClose=${onClose} sheet=${narrow}>
237
+ <div class="dsn-menu dsy-modes-pop">
238
+ <div class="dsn-menu-label">Modes</div>
239
+ ${data.modes.map(
240
+ (mode, index) =>
241
+ html`<div class="dsy-mode-row" key=${mode}>
242
+ <span class="dsy-mode-name">${modeTitle(mode)}</span>
243
+ <span class="muted small"
244
+ >${index === 0 ? "the default: tokens.json" : `tokens.${mode}.json`}</span
245
+ >
246
+ <span class="grow"></span>
247
+ ${
248
+ index > 0 &&
249
+ asking !== mode &&
250
+ html`<button
251
+ type="button"
252
+ class="dsn-link"
253
+ onClick=${() => setAsking(mode)}
254
+ >
255
+ Remove
256
+ </button>`
257
+ }
258
+ ${
259
+ asking === mode &&
260
+ html`<button
261
+ type="button"
262
+ class="button small danger"
263
+ disabled=${busy}
264
+ onClick=${async () => {
265
+ setBusy(true);
266
+ await systemData.changeModes(mode, null);
267
+ setBusy(false);
268
+ setAsking(null);
269
+ }}
270
+ >
271
+ Delete its file
272
+ </button>`
273
+ }
274
+ </div>`,
275
+ )}
276
+ <form
277
+ class="dsy-addmode"
278
+ onSubmit=${async (event) => {
279
+ event.preventDefault();
280
+
281
+ if (valid && !busy) {
282
+ setBusy(true);
283
+
284
+ const ok = await systemData.changeModes(slug, {});
285
+
286
+ setBusy(false);
287
+
288
+ if (ok) {
289
+ setName("");
290
+ onClose();
291
+ }
292
+ }
293
+ }}
294
+ >
295
+ <input
296
+ type="text"
297
+ class="dsy-text"
298
+ placeholder="Add a mode: dark, high-contrast…"
299
+ value=${name}
300
+ maxlength="32"
301
+ autocapitalize="none"
302
+ spellcheck="false"
303
+ aria-label="Name of the new mode"
304
+ onInput=${(event) => setName(event.currentTarget.value)}
305
+ />
306
+ <button type="submit" class="button small primary" disabled=${!valid || busy}>
307
+ Add
308
+ </button>
309
+ </form>
310
+ ${slug !== "" && taken && html`<p class="dsy-problem">There is a mode by that name.</p>`}
311
+ <p class="muted small dsy-note">
312
+ A new mode starts with the default's values. Change the tokens that differ, and
313
+ Save.
314
+ </p>
315
+ </div>
316
+ <//>`;
317
+ }
318
+
319
+ /** The section tabs: only the sections that have something in them, and Components and Guidelines. */
320
+ function SectionTabs({ s, data, split, hasDesign }) {
321
+ const unsaved = unsavedCount(data, s.history);
322
+ const bar = useRef(null);
323
+ // What a tab counts is what its section shows open (`sectionCounts`): the roles of Colors; a folded group has its own count.
324
+ const counts = useMemo(() => sectionCounts(data?.tokens ?? []), [data]);
325
+ const present = useMemo(
326
+ () => new Set((data?.tokens ?? []).map((token) => sectionOf(token))),
327
+ [data],
328
+ );
329
+ const list = SECTIONS.filter(
330
+ (section) =>
331
+ section.id === "components" ||
332
+ section.id === "guidelines" ||
333
+ (data !== null && present.has(section.id)),
334
+ );
335
+ const current = list.some((section) => section.id === s.sysSection)
336
+ ? s.sysSection
337
+ : list[0]?.id;
338
+
339
+ // The tab that is on comes into view in a bar that scrolls (a phone's).
340
+ useEffect(() => {
341
+ const box = bar.current;
342
+ const tab = box?.querySelector('[aria-selected="true"]');
343
+
344
+ if (box && tab) {
345
+ box.scrollTo({
346
+ left: Math.max(0, tab.offsetLeft - (box.clientWidth - tab.offsetWidth) / 2),
347
+ behavior: "smooth",
348
+ });
349
+ }
350
+ }, [current]);
351
+
352
+ // A bar that scrolls marks where more of it is out of sight (`data-more`, `data-less`). A tab that is not whole, or that
353
+ // is under a mark, is left out (`data-cut`) at once, while the bar scrolls too: it never shows half a letter, only whole
354
+ // tabs and a mark toward the rest.
355
+ useEffect(() => {
356
+ const box = bar.current;
357
+
358
+ if (!box) {
359
+ return undefined;
360
+ }
361
+
362
+ // The width of the mark at an edge that has more: a tab under it is not read, so it is not whole.
363
+ const FADE = 26;
364
+
365
+ const mark = () => {
366
+ const room = box.scrollWidth - box.clientWidth;
367
+ const more = room > 2 && box.scrollLeft < room - 2;
368
+ const less = box.scrollLeft > 2;
369
+ const left = box.scrollLeft + (less ? FADE : 0);
370
+ const right = box.scrollLeft + box.clientWidth - (more ? FADE : 0);
371
+
372
+ box.dataset.more = more ? "right" : "none";
373
+ box.dataset.less = less ? "left" : "none";
374
+
375
+ for (const tab of box.querySelectorAll('[role="tab"]')) {
376
+ const whole =
377
+ tab.offsetLeft >= left - 1 && tab.offsetLeft + tab.offsetWidth <= right + 1;
378
+
379
+ tab.toggleAttribute("data-cut", room > 2 && !whole);
380
+ }
381
+ };
382
+
383
+ const watch = new ResizeObserver(mark);
384
+
385
+ mark();
386
+ box.addEventListener("scroll", mark, { passive: true });
387
+ watch.observe(box);
388
+
389
+ return () => {
390
+ box.removeEventListener("scroll", mark);
391
+ watch.disconnect();
392
+ };
393
+ }, [list.length, data === null, current]);
394
+
395
+ // With no system there are no sections to go between.
396
+ if (data === null) {
397
+ return null;
398
+ }
399
+
400
+ // Arrow keys move between the tabs (and Home and End go to the ends); Tab goes past them, to the next control.
401
+ const move = (event) => {
402
+ const at = list.findIndex((section) => section.id === current);
403
+ const to =
404
+ event.key === "ArrowRight"
405
+ ? (at + 1) % list.length
406
+ : event.key === "ArrowLeft"
407
+ ? (at - 1 + list.length) % list.length
408
+ : event.key === "Home"
409
+ ? 0
410
+ : event.key === "End"
411
+ ? list.length - 1
412
+ : -1;
413
+
414
+ if (to !== -1) {
415
+ event.preventDefault();
416
+ env.setSection(list[to].id);
417
+ requestAnimationFrame(() =>
418
+ bar.current?.querySelector('[role="tab"][aria-selected="true"]')?.focus(),
419
+ );
420
+ }
421
+ };
422
+
423
+ return html`<div class="dsy-tabbar">
424
+ <div
425
+ class="dsn-tabs dsy-tabs"
426
+ role="tablist"
427
+ aria-label="Sections"
428
+ ref=${bar}
429
+ onKeyDown=${move}
430
+ >
431
+ ${list.map((section) => {
432
+ const count =
433
+ section.id === "components"
434
+ ? (data?.components.length ?? 0)
435
+ : (counts[section.id]?.open ?? 0);
436
+ const more = counts[section.id]?.folded ?? 0;
437
+
438
+ return html`<button
439
+ type="button"
440
+ role="tab"
441
+ id=${`dsy-tab-${section.id}`}
442
+ title=${more > 0 ? `${count} shown open, and ${more} more in folded groups` : undefined}
443
+ key=${section.id}
444
+ class=${`dsn-tab ${current === section.id ? "on" : ""}`}
445
+ aria-selected=${current === section.id}
446
+ aria-controls="dsy-panel"
447
+ tabindex=${current === section.id ? 0 : -1}
448
+ onClick=${() => env.setSection(section.id)}
449
+ >
450
+ <span>${section.label}</span>
451
+ ${count > 0 && html`<span class="dsn-count">${count}</span>`}
452
+ </button>`;
453
+ })}
454
+ </div>
455
+ <span class="dsy-tabs-end">
456
+ ${
457
+ data &&
458
+ !split &&
459
+ hasDesign &&
460
+ html`<button
461
+ type="button"
462
+ class=${`icon-button dsy-eye ${s.sysPane === "preview" ? "on" : ""}`}
463
+ title=${s.sysPane === "preview" ? "Back to the system" : "See the open design"}
464
+ aria-label=${s.sysPane === "preview" ? "Back to the system" : "See the open design"}
465
+ aria-pressed=${s.sysPane === "preview"}
466
+ onClick=${() => store.set({ sysPane: s.sysPane === "preview" ? "system" : "preview" })}
467
+ >
468
+ <${Ico} name="eye" size=${16} />
469
+ ${unsaved > 0 && html`<i class="dsy-eye-dot"></i>`}
470
+ </button>`
471
+ }
472
+ ${
473
+ data &&
474
+ html`<button
475
+ type="button"
476
+ class=${`icon-button ${s.sysSearch ? "on" : ""}`}
477
+ title="Find a token"
478
+ aria-label="Find a token"
479
+ aria-pressed=${s.sysSearch}
480
+ onClick=${() => store.set({ sysSearch: !s.sysSearch, sysQuery: s.sysSearch ? "" : s.sysQuery })}
481
+ >
482
+ <${Ico} name="search" size=${16} />
483
+ </button>`
484
+ }</span
485
+ >
486
+ </div>`;
487
+ }
488
+
489
+ /** The bar of the System view: the picker, the mode in view, undo and redo, and the menu. */
490
+ function SystemBar({ s, designs, design, shown, room, canEdit, Picker }) {
491
+ const [open, setOpen] = useState(null);
492
+ const data = s.system.data;
493
+ const narrow = env.sizeClass(s) === "narrow";
494
+ const split = room >= 900;
495
+ const edits = s.history.past.length > 0;
496
+
497
+ return html`<div class="dsn-bars dsy-bars">
498
+ <div class="dsn-bar">
499
+ <button
500
+ type="button"
501
+ class="dsn-picker"
502
+ aria-haspopup="dialog"
503
+ onClick=${() => setOpen("designs")}
504
+ >
505
+ <${Ico} name="palette" size=${16} />
506
+ <span class="dsn-picker-title">Design system</span>
507
+ ${data && !narrow && html`<span class="dsn-version-tag mono">${data.name}</span>`}
508
+ <${Ico} name="down" size=${14} />
509
+ </button>
510
+ ${data && html`<${ModeSwitch} s=${s} modes=${data.modes} narrow=${narrow || !split} />`}
511
+ ${
512
+ canEdit &&
513
+ data &&
514
+ (edits || s.history.future.length > 0) &&
515
+ html`<span class="dsy-history">
516
+ <button
517
+ type="button"
518
+ class="icon-button"
519
+ title="Undo (Ctrl+Z)"
520
+ aria-label="Undo"
521
+ disabled=${!edits}
522
+ onClick=${env.undo}
523
+ >
524
+ <${Ico} name="undo" size=${16} />
525
+ </button>
526
+ <button
527
+ type="button"
528
+ class="icon-button"
529
+ title="Redo (Ctrl+Shift+Z)"
530
+ aria-label="Redo"
531
+ disabled=${s.history.future.length === 0}
532
+ onClick=${env.redo}
533
+ >
534
+ <${Ico} name="redo" size=${16} />
535
+ </button>
536
+ </span>`
537
+ }
538
+ ${
539
+ env.beside() &&
540
+ html`<button
541
+ type="button"
542
+ class=${`icon-button ${s.focus ? "on" : ""}`}
543
+ title=${s.focus ? "Show the conversation" : "Hide the conversation"}
544
+ aria-label=${s.focus ? "Show the conversation" : "Hide the conversation"}
545
+ aria-pressed=${s.focus}
546
+ onClick=${env.toggleFocus}
547
+ >
548
+ <${Ico} name=${s.focus ? "unfocus" : "focus"} size=${17} />
549
+ </button>`
550
+ }
551
+ <button
552
+ type="button"
553
+ class="icon-button"
554
+ title="More"
555
+ aria-label="More"
556
+ aria-haspopup="dialog"
557
+ onClick=${() => setOpen("more")}
558
+ >
559
+ <${Ico} name="more" size=${18} />
560
+ </button>
561
+ </div>
562
+ <${SectionTabs} s=${s} data=${data} split=${split} hasDesign=${design !== null} />
563
+ ${
564
+ open === "designs" &&
565
+ html`<${Picker}
566
+ s=${s}
567
+ designs=${designs}
568
+ design=${design}
569
+ onClose=${() => setOpen(null)}
570
+ />`
571
+ }
572
+ ${
573
+ open === "more" &&
574
+ html`<${SystemMenu}
575
+ s=${s}
576
+ data=${data}
577
+ canEdit=${canEdit}
578
+ onClose=${() => setOpen(null)}
579
+ onModes=${() => setOpen("modes")}
580
+ />`
581
+ }
582
+ ${open === "modes" && data && html`<${ModesPopover} data=${data} narrow=${narrow} onClose=${() => setOpen(null)} />`}
583
+ </div>`;
584
+ }
585
+
586
+ // ─── Under the bar ───────────────────────────────────────────────────────
587
+
588
+ /** What the validator found: errors stop a save, warnings do not. A tap on one with a token goes to the token. */
589
+ function Problems({ problems, has, narrow }) {
590
+ const errors = problems.filter((problem) => problem.level === "error").length;
591
+ // Errors stay open where there is room; on a phone, and for warnings, it is one line with a count until a tap.
592
+ const [open, setOpen] = useState(errors > 0 && !narrow);
593
+ const [all, setAll] = useState(false);
594
+
595
+ if (problems.length === 0) {
596
+ return null;
597
+ }
598
+
599
+ const list = all ? problems : problems.slice(0, 3);
600
+ const title =
601
+ errors > 0
602
+ ? `${errors} error${errors === 1 ? "" : "s"}`
603
+ : `${problems.length} warning${problems.length === 1 ? "" : "s"}`;
604
+ const more =
605
+ errors > 0 && problems.length > errors ? `and ${problems.length - errors} more` : "";
606
+
607
+ return html`<div
608
+ class=${`dsy-problems ${errors > 0 ? "error" : "warn"} ${open ? "open" : ""}`}
609
+ role="status"
610
+ >
611
+ <button
612
+ type="button"
613
+ class="dsy-problems-head"
614
+ aria-expanded=${open}
615
+ onClick=${() => setOpen(!open)}
616
+ >
617
+ <${Ico} name="alert" size=${15} />
618
+ <strong>${title}</strong>
619
+ ${more && html`<span class="muted">${more}</span>`}
620
+ <span class="grow"></span>
621
+ <span class="muted dsy-problems-hint">${open ? "Hide" : "Show"}</span>
622
+ <${Ico} name="down" size=${14} />
623
+ </button>
624
+ ${
625
+ open &&
626
+ html`<ul>
627
+ ${list.map(
628
+ (problem, index) =>
629
+ html`<li key=${index} class=${problem.level}>
630
+ ${
631
+ problem.token && has(problem.token)
632
+ ? html`<button
633
+ type="button"
634
+ class="dsy-chip"
635
+ onClick=${() => env.openToken(problem.token)}
636
+ >
637
+ ${problem.token}
638
+ </button>`
639
+ : problem.token
640
+ ? html`<span class="dsy-chip-static mono"
641
+ >${problem.token}</span
642
+ >`
643
+ : problem.file &&
644
+ html`<span class="dsy-chip-static mono"
645
+ >${problem.file}</span
646
+ >`
647
+ }
648
+ <span>${problem.message}</span>
649
+ </li>`,
650
+ )}
651
+ </ul>`
652
+ }
653
+ ${
654
+ open &&
655
+ problems.length > 3 &&
656
+ html`<button type="button" class="dsn-link" onClick=${() => setAll(!all)}>
657
+ ${all ? "Show fewer" : `Show all ${problems.length}`}
658
+ </button>`
659
+ }
660
+ </div>`;
661
+ }
662
+
663
+ /**
664
+ * What the system says that needs no action (`notes`): a contrast pair that is below its minimum on purpose (a disabled or
665
+ * decorative color: exempt). One quiet line that opens; it is not a problem, so it never shows as a warning. It is about
666
+ * colors, so it shows on the Colors tab only.
667
+ */
668
+ function Notes({ notes, has }) {
669
+ if (!Array.isArray(notes) || notes.length === 0) {
670
+ return null;
671
+ }
672
+
673
+ return html`<details class="dsy-notes">
674
+ <summary>
675
+ ${`${notes.length} note${notes.length === 1 ? "" : "s"}, no action needed`}
676
+ </summary>
677
+ <ul>
678
+ ${notes.map(
679
+ (note, index) =>
680
+ html`<li key=${index}>
681
+ ${
682
+ note.token && has(note.token)
683
+ ? html`<button
684
+ type="button"
685
+ class="dsy-chip"
686
+ onClick=${() => env.openToken(note.token)}
687
+ >
688
+ ${note.token}
689
+ </button> `
690
+ : ""
691
+ }${note.message}
692
+ </li>`,
693
+ )}
694
+ </ul>
695
+ </details>`;
696
+ }
697
+
698
+ /** Unsaved edits and the save: how many, undo, give up, and Save. */
699
+ function SaveBar({ s, count, data, canEdit }) {
700
+ // "Discard" with more than one change asks again, for a few seconds: nothing brings the changes back. The question is
701
+ // about the changes there are when it is asked: if their number moves, it is no longer the question.
702
+ const [askedFor, setAskedFor] = useState(null);
703
+ const asking = askedFor === count && count > 1;
704
+
705
+ useEffect(() => {
706
+ if (askedFor === null) {
707
+ return undefined;
708
+ }
709
+
710
+ const timer = setTimeout(() => setAskedFor(null), 4000);
711
+
712
+ return () => clearTimeout(timer);
713
+ }, [askedFor]);
714
+
715
+ if (!canEdit || !data) {
716
+ return null;
717
+ }
718
+
719
+ const { status, error, problems, note } = s.save;
720
+
721
+ if (count === 0 && status !== "saved" && status !== "conflict" && status !== "failed") {
722
+ return null;
723
+ }
724
+
725
+ return html`<footer
726
+ class=${`dsy-savebar ${status}`}
727
+ role="region"
728
+ aria-label="Unsaved changes"
729
+ >
730
+ ${
731
+ status === "failed" &&
732
+ html`<div class="dsy-save-error" role="alert">
733
+ <${Ico} name="alert" size=${14} />
734
+ <span>${error}</span>
735
+ ${problems.slice(0, 3).map((problem) => html`<span class="muted">${problem.message}</span>`)}
736
+ </div>`
737
+ }
738
+ ${
739
+ status === "conflict" &&
740
+ html`<div class="dsy-save-error" role="alert">
741
+ <${Ico} name="alert" size=${14} />
742
+ <span
743
+ >The design system changed on disk after you opened it. Your edits are
744
+ kept.</span
745
+ >
746
+ <button type="button" class="dsn-link" onClick=${() => systemData.reload()}>
747
+ Load the new version
748
+ </button>
749
+ </div>`
750
+ }
751
+ <div class="dsy-save-row">
752
+ ${
753
+ status === "saved" && count === 0
754
+ ? html`<span class="dsy-saved"
755
+ ><${Ico} name="check" size=${15} /> ${note}</span
756
+ >`
757
+ : html`<span class="dsy-count-note"
758
+ ><i></i>${count} unsaved change${count === 1 ? "" : "s"}</span
759
+ >`
760
+ }
761
+ ${
762
+ (s.history.past.length > 0 || s.history.future.length > 0) &&
763
+ html`<span class="dsy-bar-history" role="group" aria-label="Undo and redo">
764
+ <button
765
+ type="button"
766
+ class="icon-button"
767
+ title="Undo"
768
+ aria-label="Undo"
769
+ disabled=${s.history.past.length === 0 || status === "saving"}
770
+ onClick=${() => env.undo()}
771
+ >
772
+ <${Ico} name="undo" size=${16} />
773
+ </button>
774
+ <button
775
+ type="button"
776
+ class="icon-button"
777
+ title="Redo"
778
+ aria-label="Redo"
779
+ disabled=${s.history.future.length === 0 || status === "saving"}
780
+ onClick=${() => env.redo()}
781
+ >
782
+ <${Ico} name="redo" size=${16} />
783
+ </button>
784
+ </span>`
785
+ }
786
+ <span class="grow"></span>
787
+ ${
788
+ count > 0 &&
789
+ html`<button
790
+ type="button"
791
+ class=${`button small ${asking ? "danger" : ""}`}
792
+ disabled=${status === "saving"}
793
+ onClick=${() => {
794
+ if (count > 1 && !asking) {
795
+ setAskedFor(count);
796
+ } else {
797
+ env.discard();
798
+ }
799
+ }}
800
+ >
801
+ ${asking ? `Discard ${count}?` : "Discard"}
802
+ </button>
803
+ <button
804
+ type="button"
805
+ class="button small primary"
806
+ disabled=${status === "saving" || status === "conflict"}
807
+ onClick=${() => systemData.save()}
808
+ >
809
+ ${status === "saving" ? "Saving…" : status === "failed" ? "Try again" : "Save"}
810
+ </button>`
811
+ }
812
+ </div>
813
+ </footer>`;
814
+ }
815
+
816
+ /** What shows when the project has no tokens yet. */
817
+ function Blank({ s, canEdit }) {
818
+ const legacy = s.system.legacy;
819
+
820
+ return html`<div class="dsy-blank big">
821
+ <span class="dsy-blank-mark"><${Ico} name="palette" size=${30} /></span>
822
+ <h3>No design system yet</h3>
823
+ <p class="muted">
824
+ A design system is the colors, type, spacing, and components of a product, kept in
825
+ the project. Pi reads it before it designs, and checks each design against it. Here
826
+ you see it, change it, and publish it.
827
+ </p>
828
+ ${
829
+ legacy !== null &&
830
+ html`<p class="dsy-legacy">
831
+ <${Ico} name="book" size=${15} />
832
+ <span>
833
+ This project has a Markdown design system
834
+ (<code>.pi/design-system.md</code>${`, ${legacy < 1024 ? `${legacy} bytes` : `${(legacy / 1024).toFixed(1)} KB`}). `}Pi
835
+ still reads it for its designs. Turn it into tokens to see and edit it here.
836
+ </span>
837
+ </p>`
838
+ }
839
+ ${
840
+ canEdit
841
+ ? html`<div class="dsy-blank-actions">
842
+ <button
843
+ type="button"
844
+ class="button primary"
845
+ onClick=${() => env.askPi(legacy === null ? "create" : "convert")}
846
+ >
847
+ <${Ico} name="sparkle" size=${15} />
848
+ ${legacy === null ? "Ask Pi to scan this project" : "Ask Pi to convert it"}
849
+ </button>
850
+ ${
851
+ legacy !== null &&
852
+ html`<button
853
+ type="button"
854
+ class="button"
855
+ onClick=${() => env.askPi("create")}
856
+ >
857
+ Scan the project instead
858
+ </button>`
859
+ }
860
+ </div>`
861
+ : html`<p class="muted small">
862
+ People who can steer the session can ask Pi to make one.
863
+ </p>`
864
+ }
865
+ </div>`;
866
+ }
867
+
868
+ // ─── The open design beside the view ─────────────────────────────────────
869
+
870
+ /** The open design, live with the edits: a choice of design and size, and the page. */
871
+ function Preview({ s, designs, design, shown, size, canEdit, pane }) {
872
+ const narrowHead = pane === "page";
873
+ const edits = unsavedCount(s.system.data, s.history);
874
+ const box = useRef(null);
875
+ const room = parts.useSize(box);
876
+ // Beside the view, with no size chosen, the page has the room that the pane has: a page at the size of a desktop in a
877
+ // pane a third as wide is a card in a corner. A size that was chosen (a phone, a tablet, a desktop) is kept.
878
+ const fitted =
879
+ s.device === "auto" && pane === "side" && room.w > 0 && room.h > 0
880
+ ? { w: clampSize(room.w - 36), h: clampSize(room.h - 78) }
881
+ : size;
882
+
883
+ if (!design) {
884
+ return html`<div class="dsy-preview">
885
+ <div class="dsy-preview-empty">
886
+ <${Ico} name="design" size=${28} />
887
+ <p class="muted">
888
+ Open a design to see the system's changes on it as you make them. Designs
889
+ that link
890
+ <code>ds/tokens.css</code> follow the system.
891
+ </p>
892
+ </div>
893
+ </div>`;
894
+ }
895
+
896
+ return html`<div class="dsy-preview">
897
+ <div class="dsy-preview-head">
898
+ ${
899
+ narrowHead &&
900
+ html`<button
901
+ type="button"
902
+ class="button small"
903
+ onClick=${() => store.set({ sysPane: "system" })}
904
+ >
905
+ <${Ico} name="left" size=${14} /> System
906
+ </button>`
907
+ }
908
+ <select
909
+ class="dsy-preview-select"
910
+ aria-label="The design to preview"
911
+ value=${design.id}
912
+ onChange=${(event) => env.previewDesign(event.currentTarget.value)}
913
+ >
914
+ ${designs.map((each) => html`<option value=${each.id}>${each.title}</option>`)}
915
+ </select>
916
+ <div class="dsn-seg" role="group" aria-label="Size">
917
+ ${["mobile", "tablet", "desktop"].map(
918
+ (device) =>
919
+ html`<button
920
+ type="button"
921
+ key=${device}
922
+ class=${s.device === device || (s.device === "auto" && device === "mobile" && narrowHead) ? "on" : ""}
923
+ aria-pressed=${s.device === device}
924
+ title=${`${DEVICES[device].label}, ${DEVICES[device].w} × ${DEVICES[device].h}`}
925
+ aria-label=${DEVICES[device].label}
926
+ onClick=${() => env.setDevice(device)}
927
+ >
928
+ <${Ico} name=${DEVICES[device].icon} size=${15} />
929
+ </button>`,
930
+ )}
931
+ </div>
932
+ ${
933
+ canEdit &&
934
+ html`<button
935
+ type="button"
936
+ class="button small"
937
+ title="Check this design against the system"
938
+ aria-label="Check this design"
939
+ onClick=${() => env.openCheck()}
940
+ >
941
+ <${Ico} name="shield" size=${14} />
942
+ <span class="dsy-check-label">Check</span>
943
+ </button>`
944
+ }
945
+ </div>
946
+ ${
947
+ edits > 0 &&
948
+ html`<div class="dsy-preview-note">
949
+ <i></i> Showing ${edits} unsaved change${edits === 1 ? "" : "s"}. Designs that
950
+ use the system's variables follow it.
951
+ </div>`
952
+ }
953
+ <div class="dsy-preview-canvas" ref=${box}>
954
+ <${canvas.CanvasView}
955
+ s=${s}
956
+ design=${design}
957
+ version=${shown}
958
+ size=${fitted}
959
+ readOnly=${true}
960
+ />
961
+ </div>
962
+ </div>`;
963
+ }
964
+
965
+ // ─── The whole ───────────────────────────────────────────────────────────
966
+
967
+ function SystemView({ s, designs, design, shown, size, room, canEdit }) {
968
+ const system = s.system;
969
+ const data = system.data;
970
+ const scroll = useRef(null);
971
+ const narrow = env.sizeClass(s) === "narrow";
972
+ const split = room >= 900;
973
+ const edits = useMemo(() => tokenEdits(s.history), [s.history]);
974
+ const model = useMemo(() => (data ? createModel(data, edits) : null), [data, edits]);
975
+ const names = data?.modes ?? [];
976
+ const modes =
977
+ s.sysMode === "both"
978
+ ? names.slice(0, 3)
979
+ : [names.includes(s.sysMode) ? s.sysMode : names[0]].filter(Boolean);
980
+ const guidelines = editsOf(s.history).get("guidelines")?.text ?? data?.guidelines ?? "";
981
+ const count = useMemo(
982
+ () => (data ? changeOf(data, s.history).count : 0),
983
+ [data, s.history],
984
+ );
985
+ const available = SECTIONS.filter(
986
+ (section) =>
987
+ section.id === "components" ||
988
+ section.id === "guidelines" ||
989
+ (model?.tokens ?? []).some((token) => sectionOf(token) === section.id),
990
+ );
991
+ const current = available.some((section) => section.id === s.sysSection)
992
+ ? s.sysSection
993
+ : (available[0]?.id ?? "colors");
994
+ const ctx = {
995
+ s,
996
+ model,
997
+ modes,
998
+ canEdit,
999
+ narrow,
1000
+ guidelines,
1001
+ query: s.sysSearch ? s.sysQuery.trim() : "",
1002
+ };
1003
+
1004
+ // The first read, and then a quiet look every so often: Pi changes the files too.
1005
+ useEffect(() => {
1006
+ if (system.status === "idle") {
1007
+ void systemData.load();
1008
+ }
1009
+
1010
+ const timer = setInterval(() => {
1011
+ if (!document.hidden) {
1012
+ void systemData.load({ quiet: true });
1013
+ }
1014
+ }, REFRESH_MS);
1015
+
1016
+ return () => clearInterval(timer);
1017
+ }, []);
1018
+
1019
+ // Undo, redo, and save, as the keys that do them elsewhere (a field keeps its own).
1020
+ useEffect(() => {
1021
+ const onKey = (event) => {
1022
+ if (
1023
+ !(event.metaKey || event.ctrlKey) ||
1024
+ event.altKey ||
1025
+ typing(event.target) ||
1026
+ !canEdit
1027
+ ) {
1028
+ return;
1029
+ }
1030
+
1031
+ const key = event.key.toLowerCase();
1032
+
1033
+ if (key === "z") {
1034
+ event.preventDefault();
1035
+ (event.shiftKey ? env.redo : env.undo)();
1036
+ } else if (key === "y") {
1037
+ event.preventDefault();
1038
+ env.redo();
1039
+ } else if (key === "s") {
1040
+ event.preventDefault();
1041
+ void systemData.save();
1042
+ }
1043
+ };
1044
+
1045
+ document.addEventListener("keydown", onKey);
1046
+
1047
+ return () => document.removeEventListener("keydown", onKey);
1048
+ }, [canEdit]);
1049
+
1050
+ // A section starts at its top.
1051
+ useEffect(() => scroll.current?.scrollTo({ top: 0 }), [current]);
1052
+
1053
+ let body;
1054
+
1055
+ if (system.status === "loading" || system.status === "idle") {
1056
+ body = html`<${pocket.Loader} label="Reading the design system" />`;
1057
+ } else if (system.status === "failed" && !data) {
1058
+ body = html`<div class="dsy-blank">
1059
+ <${Ico} name="alert" size=${26} />
1060
+ <h3>The design system could not be read</h3>
1061
+ <p class="muted">${system.error}</p>
1062
+ <button type="button" class="button" onClick=${() => systemData.load()}>
1063
+ Try again
1064
+ </button>
1065
+ </div>`;
1066
+ } else if (!data) {
1067
+ body = html`<${Blank} s=${s} canEdit=${canEdit} />`;
1068
+ } else {
1069
+ const Section = {
1070
+ colors: sections.Colors,
1071
+ type: sections.Type,
1072
+ spacing: sections.Spacing,
1073
+ radii: sections.Radii,
1074
+ shadows: sections.Shadows,
1075
+ motion: sections.Motion,
1076
+ other: sections.Other,
1077
+ components: sections.Components,
1078
+ guidelines: sections.Guidelines,
1079
+ }[current];
1080
+
1081
+ body = html`${
1082
+ s.sysSearch &&
1083
+ html`<div class="dsy-search">
1084
+ <${Ico} name="search" size=${15} />
1085
+ <input
1086
+ type="search"
1087
+ value=${s.sysQuery}
1088
+ placeholder=${`Find in ${available.find((section) => section.id === current)?.label ?? "the system"}: a name, a value, a word`}
1089
+ aria-label="Find a token"
1090
+ autofocus
1091
+ onInput=${(event) => store.set({ sysQuery: event.currentTarget.value })}
1092
+ />
1093
+ ${
1094
+ s.sysQuery !== "" &&
1095
+ html`<button
1096
+ type="button"
1097
+ class="icon-button"
1098
+ aria-label="Clear"
1099
+ onClick=${() => store.set({ sysQuery: "" })}
1100
+ >
1101
+ <${Ico} name="close" size=${14} />
1102
+ </button>`
1103
+ }
1104
+ </div>`
1105
+ }
1106
+ <${Problems}
1107
+ problems=${data.problems}
1108
+ has=${(name) => model.has(name)}
1109
+ narrow=${narrow}
1110
+ />
1111
+ ${current === "colors" && html`<${Notes} notes=${data.notes} has=${(name) => model.has(name)} />`}
1112
+ ${!canEdit && html`<p class="dsy-readonly muted small"><${Ico} name="eye" size=${13} /> You can look at the system here. Only people who can steer the session change it.</p>`}
1113
+ <${Section} ctx=${ctx} />`;
1114
+ }
1115
+
1116
+ // What a screen reader is told after a save: a place that is always there, so that the words put in it are read out.
1117
+ // (A refusal and a conflict have their own `alert`s.)
1118
+ const said = s.save.status === "saved" ? s.save.note : "";
1119
+
1120
+ const column = html`<div class="dsy-column">
1121
+ <div
1122
+ class="dsy-scroll"
1123
+ id="dsy-panel"
1124
+ role="tabpanel"
1125
+ aria-labelledby=${`dsy-tab-${current}`}
1126
+ tabindex=${current === "guidelines" && !s.sysWriting ? 0 : undefined}
1127
+ ref=${scroll}
1128
+ data-no-swipe
1129
+ >
1130
+ ${body}
1131
+ </div>
1132
+ <div class="dsy-sr" role="status" aria-live="polite" aria-atomic="true">${said}</div>
1133
+ <${SaveBar} s=${s} count=${count} data=${data} canEdit=${canEdit} />
1134
+ </div>`;
1135
+
1136
+ const preview = html`<${Preview}
1137
+ s=${s}
1138
+ designs=${designs}
1139
+ design=${design}
1140
+ shown=${shown}
1141
+ size=${size}
1142
+ canEdit=${canEdit}
1143
+ pane=${split ? "side" : "page"}
1144
+ />`;
1145
+
1146
+ // The list of tokens to link to is drawn here, over the whole view: a row would hold it inside its own box.
1147
+ const picking = s.sysPick && model?.has(s.sysPick.token) ? s.sysPick : null;
1148
+
1149
+ return html`<div class="dsy-main" data-split=${split} data-pane=${s.sysPane}>
1150
+ ${split ? html`${column}${preview}` : s.sysPane === "preview" ? preview : column}
1151
+ ${
1152
+ picking &&
1153
+ html`<${widgets.AliasPicker}
1154
+ model=${model}
1155
+ token=${model.token(picking.token)}
1156
+ mode=${picking.mode}
1157
+ narrow=${narrow}
1158
+ onClose=${env.closePick}
1159
+ part=${picking.part}
1160
+ current=${
1161
+ picking.part
1162
+ ? linkOf(
1163
+ model.shadowRaw(picking.token, picking.mode).layers[
1164
+ picking.part.layer
1165
+ ]?.[picking.part.key],
1166
+ )
1167
+ : undefined
1168
+ }
1169
+ onPick=${(name) => {
1170
+ env.closePick();
1171
+
1172
+ if (picking.part) {
1173
+ // A part of a shadow links to the token: the member is written as a reference, as in the file.
1174
+ const { layers } = model.shadowRaw(picking.token, picking.mode);
1175
+
1176
+ env.editToken(picking.token, picking.mode, {
1177
+ value: shadowValue(
1178
+ withPart(
1179
+ layers,
1180
+ picking.part.layer,
1181
+ picking.part.key,
1182
+ `{${name}}`,
1183
+ ),
1184
+ ),
1185
+ });
1186
+ } else {
1187
+ env.editToken(picking.token, picking.mode, { alias: name });
1188
+ }
1189
+ }}
1190
+ />`
1191
+ }
1192
+ </div>`;
1193
+ }
1194
+
1195
+ return { SystemBar, SystemView };
1196
+ }