sendproof-core 0.1.0.alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +55 -0
  4. data/lib/sendproof/assets.rb +70 -0
  5. data/lib/sendproof/attribution.rb +98 -0
  6. data/lib/sendproof/capture.rb +66 -0
  7. data/lib/sendproof/clock.rb +54 -0
  8. data/lib/sendproof/configuration.rb +241 -0
  9. data/lib/sendproof/data_store.rb +34 -0
  10. data/lib/sendproof/dynamodb/store.rb +183 -0
  11. data/lib/sendproof/dynamodb.rb +17 -0
  12. data/lib/sendproof/elasticsearch/store.rb +24 -0
  13. data/lib/sendproof/elasticsearch.rb +17 -0
  14. data/lib/sendproof/firestore/interceptor.rb +38 -0
  15. data/lib/sendproof/firestore/store.rb +150 -0
  16. data/lib/sendproof/firestore.rb +99 -0
  17. data/lib/sendproof/handler.rb +23 -0
  18. data/lib/sendproof/identity.rb +37 -0
  19. data/lib/sendproof/limits.rb +113 -0
  20. data/lib/sendproof/middleware.rb +68 -0
  21. data/lib/sendproof/mongo/engine.rb +692 -0
  22. data/lib/sendproof/mongo/guarded.rb +100 -0
  23. data/lib/sendproof/mongo/plain.rb +45 -0
  24. data/lib/sendproof/mongo/store.rb +54 -0
  25. data/lib/sendproof/mongo/subscriber.rb +21 -0
  26. data/lib/sendproof/mongo.rb +41 -0
  27. data/lib/sendproof/mongoid/store.rb +58 -0
  28. data/lib/sendproof/mongoid.rb +65 -0
  29. data/lib/sendproof/observed_request.rb +108 -0
  30. data/lib/sendproof/opensearch/store.rb +21 -0
  31. data/lib/sendproof/opensearch.rb +17 -0
  32. data/lib/sendproof/operation_matcher.rb +28 -0
  33. data/lib/sendproof/pool_hooks.rb +80 -0
  34. data/lib/sendproof/preview_request.rb +44 -0
  35. data/lib/sendproof/preview_transport.rb +331 -0
  36. data/lib/sendproof/protocol.rb +81 -0
  37. data/lib/sendproof/provider.rb +83 -0
  38. data/lib/sendproof/recorder.rb +298 -0
  39. data/lib/sendproof/redaction.rb +98 -0
  40. data/lib/sendproof/redis/commands.rb +76 -0
  41. data/lib/sendproof/redis/store.rb +205 -0
  42. data/lib/sendproof/redis.rb +18 -0
  43. data/lib/sendproof/search/middleware.rb +39 -0
  44. data/lib/sendproof/search/store.rb +213 -0
  45. data/lib/sendproof/search.rb +82 -0
  46. data/lib/sendproof/stores/classify.rb +346 -0
  47. data/lib/sendproof/stores/guard.rb +127 -0
  48. data/lib/sendproof/stores/tables.rb +1328 -0
  49. data/lib/sendproof/stores.rb +12 -0
  50. data/lib/sendproof/trace_store.rb +110 -0
  51. data/lib/sendproof/transaction_control.rb +187 -0
  52. data/lib/sendproof/ui/action-bar.js +91 -0
  53. data/lib/sendproof/ui/api.js +239 -0
  54. data/lib/sendproof/ui/app.css +848 -0
  55. data/lib/sendproof/ui/app.js +742 -0
  56. data/lib/sendproof/ui/autofill.js +137 -0
  57. data/lib/sendproof/ui/code-copy.js +71 -0
  58. data/lib/sendproof/ui/code-picker.js +149 -0
  59. data/lib/sendproof/ui/commands.js +197 -0
  60. data/lib/sendproof/ui/components.js +77 -0
  61. data/lib/sendproof/ui/context-bar.js +82 -0
  62. data/lib/sendproof/ui/copy.js +92 -0
  63. data/lib/sendproof/ui/coverage-popover.js +35 -0
  64. data/lib/sendproof/ui/crumb-fit.js +80 -0
  65. data/lib/sendproof/ui/deprecation.js +164 -0
  66. data/lib/sendproof/ui/display-settings.js +175 -0
  67. data/lib/sendproof/ui/doc-diff.js +209 -0
  68. data/lib/sendproof/ui/docs-view.js +347 -0
  69. data/lib/sendproof/ui/favicon.png +0 -0
  70. data/lib/sendproof/ui/favicon.svg +1 -0
  71. data/lib/sendproof/ui/field-help.js +52 -0
  72. data/lib/sendproof/ui/field-modules.js +23 -0
  73. data/lib/sendproof/ui/fields/controls.js +179 -0
  74. data/lib/sendproof/ui/fields/dom.js +36 -0
  75. data/lib/sendproof/ui/fields/infer.js +71 -0
  76. data/lib/sendproof/ui/fields/lookup-source.js +63 -0
  77. data/lib/sendproof/ui/fields/lookup.js +285 -0
  78. data/lib/sendproof/ui/fields/render.js +496 -0
  79. data/lib/sendproof/ui/fields/select.js +175 -0
  80. data/lib/sendproof/ui/fields/validate.js +363 -0
  81. data/lib/sendproof/ui/fields/validation-formats.js +164 -0
  82. data/lib/sendproof/ui/fields/validation-references.js +68 -0
  83. data/lib/sendproof/ui/fields/validation-values.js +137 -0
  84. data/lib/sendproof/ui/generate-menu.js +44 -0
  85. data/lib/sendproof/ui/generate.js +184 -0
  86. data/lib/sendproof/ui/header-editor.js +777 -0
  87. data/lib/sendproof/ui/headers.js +188 -0
  88. data/lib/sendproof/ui/highlight.js +257 -0
  89. data/lib/sendproof/ui/history-popover.js +24 -0
  90. data/lib/sendproof/ui/i18n.js +253 -0
  91. data/lib/sendproof/ui/icons.js +87 -0
  92. data/lib/sendproof/ui/index.html +70 -0
  93. data/lib/sendproof/ui/issues.js +187 -0
  94. data/lib/sendproof/ui/json-view.js +166 -0
  95. data/lib/sendproof/ui/lang-icons.js +62 -0
  96. data/lib/sendproof/ui/layout.js +262 -0
  97. data/lib/sendproof/ui/listbox.js +334 -0
  98. data/lib/sendproof/ui/locales/ar-capture.js +366 -0
  99. data/lib/sendproof/ui/locales/ar-stores.js +116 -0
  100. data/lib/sendproof/ui/locales/ar.js +561 -0
  101. data/lib/sendproof/ui/locales/cs-capture.js +442 -0
  102. data/lib/sendproof/ui/locales/cs-stores.js +116 -0
  103. data/lib/sendproof/ui/locales/cs.js +689 -0
  104. data/lib/sendproof/ui/locales/da-capture.js +347 -0
  105. data/lib/sendproof/ui/locales/da-stores.js +116 -0
  106. data/lib/sendproof/ui/locales/da.js +522 -0
  107. data/lib/sendproof/ui/locales/de-capture.js +347 -0
  108. data/lib/sendproof/ui/locales/de-stores.js +116 -0
  109. data/lib/sendproof/ui/locales/de.js +519 -0
  110. data/lib/sendproof/ui/locales/el-capture.js +347 -0
  111. data/lib/sendproof/ui/locales/el-stores.js +116 -0
  112. data/lib/sendproof/ui/locales/el.js +520 -0
  113. data/lib/sendproof/ui/locales/en-capture.js +349 -0
  114. data/lib/sendproof/ui/locales/en-stores.js +95 -0
  115. data/lib/sendproof/ui/locales/en.js +522 -0
  116. data/lib/sendproof/ui/locales/es-capture.js +353 -0
  117. data/lib/sendproof/ui/locales/es-stores.js +116 -0
  118. data/lib/sendproof/ui/locales/es.js +525 -0
  119. data/lib/sendproof/ui/locales/fa-capture.js +348 -0
  120. data/lib/sendproof/ui/locales/fa-stores.js +116 -0
  121. data/lib/sendproof/ui/locales/fa.js +521 -0
  122. data/lib/sendproof/ui/locales/fi-capture.js +347 -0
  123. data/lib/sendproof/ui/locales/fi-stores.js +116 -0
  124. data/lib/sendproof/ui/locales/fi.js +523 -0
  125. data/lib/sendproof/ui/locales/fr-capture.js +353 -0
  126. data/lib/sendproof/ui/locales/fr-stores.js +116 -0
  127. data/lib/sendproof/ui/locales/fr.js +525 -0
  128. data/lib/sendproof/ui/locales/he-capture.js +358 -0
  129. data/lib/sendproof/ui/locales/he-stores.js +116 -0
  130. data/lib/sendproof/ui/locales/he.js +529 -0
  131. data/lib/sendproof/ui/locales/hi-capture.js +348 -0
  132. data/lib/sendproof/ui/locales/hi-stores.js +116 -0
  133. data/lib/sendproof/ui/locales/hi.js +523 -0
  134. data/lib/sendproof/ui/locales/id-capture.js +347 -0
  135. data/lib/sendproof/ui/locales/id-stores.js +116 -0
  136. data/lib/sendproof/ui/locales/id.js +521 -0
  137. data/lib/sendproof/ui/locales/it-capture.js +361 -0
  138. data/lib/sendproof/ui/locales/it-stores.js +116 -0
  139. data/lib/sendproof/ui/locales/it.js +523 -0
  140. data/lib/sendproof/ui/locales/ja-capture.js +346 -0
  141. data/lib/sendproof/ui/locales/ja-stores.js +116 -0
  142. data/lib/sendproof/ui/locales/ja.js +521 -0
  143. data/lib/sendproof/ui/locales/ko-capture.js +346 -0
  144. data/lib/sendproof/ui/locales/ko-stores.js +116 -0
  145. data/lib/sendproof/ui/locales/ko.js +522 -0
  146. data/lib/sendproof/ui/locales/nb-capture.js +349 -0
  147. data/lib/sendproof/ui/locales/nb-stores.js +116 -0
  148. data/lib/sendproof/ui/locales/nb.js +521 -0
  149. data/lib/sendproof/ui/locales/nl-capture.js +355 -0
  150. data/lib/sendproof/ui/locales/nl-stores.js +116 -0
  151. data/lib/sendproof/ui/locales/nl.js +520 -0
  152. data/lib/sendproof/ui/locales/pl-capture.js +432 -0
  153. data/lib/sendproof/ui/locales/pl-stores.js +116 -0
  154. data/lib/sendproof/ui/locales/pl.js +528 -0
  155. data/lib/sendproof/ui/locales/pt-BR-capture.js +350 -0
  156. data/lib/sendproof/ui/locales/pt-BR-stores.js +116 -0
  157. data/lib/sendproof/ui/locales/pt-BR.js +523 -0
  158. data/lib/sendproof/ui/locales/pt-PT-capture.js +350 -0
  159. data/lib/sendproof/ui/locales/pt-PT-stores.js +116 -0
  160. data/lib/sendproof/ui/locales/pt-PT.js +523 -0
  161. data/lib/sendproof/ui/locales/ru-capture.js +437 -0
  162. data/lib/sendproof/ui/locales/ru-stores.js +116 -0
  163. data/lib/sendproof/ui/locales/ru.js +624 -0
  164. data/lib/sendproof/ui/locales/sv-capture.js +349 -0
  165. data/lib/sendproof/ui/locales/sv-stores.js +116 -0
  166. data/lib/sendproof/ui/locales/sv.js +518 -0
  167. data/lib/sendproof/ui/locales/th-capture.js +347 -0
  168. data/lib/sendproof/ui/locales/th-stores.js +116 -0
  169. data/lib/sendproof/ui/locales/th.js +521 -0
  170. data/lib/sendproof/ui/locales/tr-capture.js +348 -0
  171. data/lib/sendproof/ui/locales/tr-stores.js +116 -0
  172. data/lib/sendproof/ui/locales/tr.js +521 -0
  173. data/lib/sendproof/ui/locales/uk-capture.js +442 -0
  174. data/lib/sendproof/ui/locales/uk-stores.js +116 -0
  175. data/lib/sendproof/ui/locales/uk.js +630 -0
  176. data/lib/sendproof/ui/locales/vi-capture.js +346 -0
  177. data/lib/sendproof/ui/locales/vi-stores.js +116 -0
  178. data/lib/sendproof/ui/locales/vi.js +523 -0
  179. data/lib/sendproof/ui/locales/zh-Hans-capture.js +347 -0
  180. data/lib/sendproof/ui/locales/zh-Hans-stores.js +116 -0
  181. data/lib/sendproof/ui/locales/zh-Hans.js +523 -0
  182. data/lib/sendproof/ui/locales/zh-Hant-capture.js +346 -0
  183. data/lib/sendproof/ui/locales/zh-Hant-stores.js +116 -0
  184. data/lib/sendproof/ui/locales/zh-Hant.js +522 -0
  185. data/lib/sendproof/ui/manifest.json +866 -0
  186. data/lib/sendproof/ui/markdown.js +39 -0
  187. data/lib/sendproof/ui/model-view.js +182 -0
  188. data/lib/sendproof/ui/navigation.js +233 -0
  189. data/lib/sendproof/ui/operation-header.js +76 -0
  190. data/lib/sendproof/ui/operations.js +7 -0
  191. data/lib/sendproof/ui/overview-view.js +63 -0
  192. data/lib/sendproof/ui/personas.js +59 -0
  193. data/lib/sendproof/ui/popover.js +71 -0
  194. data/lib/sendproof/ui/preview-false.js +108 -0
  195. data/lib/sendproof/ui/request-editor.js +496 -0
  196. data/lib/sendproof/ui/response-metadata.js +354 -0
  197. data/lib/sendproof/ui/response-viewer.js +618 -0
  198. data/lib/sendproof/ui/result-blocked.js +155 -0
  199. data/lib/sendproof/ui/result-capture.js +25 -0
  200. data/lib/sendproof/ui/result-database.js +185 -0
  201. data/lib/sendproof/ui/result-effects.js +176 -0
  202. data/lib/sendproof/ui/result-headers.js +106 -0
  203. data/lib/sendproof/ui/result-logs.js +408 -0
  204. data/lib/sendproof/ui/result-states.js +213 -0
  205. data/lib/sendproof/ui/results.js +371 -0
  206. data/lib/sendproof/ui/router.js +118 -0
  207. data/lib/sendproof/ui/schema-model.js +336 -0
  208. data/lib/sendproof/ui/schema-tree.js +344 -0
  209. data/lib/sendproof/ui/schema.js +397 -0
  210. data/lib/sendproof/ui/security.js +31 -0
  211. data/lib/sendproof/ui/seeded-menu.js +69 -0
  212. data/lib/sendproof/ui/settings.js +93 -0
  213. data/lib/sendproof/ui/shortcuts-sheet.js +73 -0
  214. data/lib/sendproof/ui/shortcuts.js +190 -0
  215. data/lib/sendproof/ui/sidebar.js +289 -0
  216. data/lib/sendproof/ui/signin.js +259 -0
  217. data/lib/sendproof/ui/snippets.js +530 -0
  218. data/lib/sendproof/ui/store-marks.js +47 -0
  219. data/lib/sendproof/ui/stores.js +131 -0
  220. data/lib/sendproof/ui/styles.css +971 -0
  221. data/lib/sendproof/ui/toggletip.js +53 -0
  222. data/lib/sendproof/ui/uuid.js +10 -0
  223. data/lib/sendproof/ui/view.js +122 -0
  224. data/lib/sendproof/ui/views.css +653 -0
  225. data/lib/sendproof/version.rb +9 -0
  226. data/lib/sendproof/workbench.rb +102 -0
  227. data/lib/sendproof-core.rb +3 -0
  228. data/lib/sendproof.rb +121 -0
  229. metadata +292 -0
@@ -0,0 +1,166 @@
1
+ // The JSON viewer shared by response bodies, examples and JSON Schema views. A value becomes
2
+ // logical lines (with fold ids), every object and array starts open, and only a very large body
3
+ // (over LARGE_LINES lines) starts collapsed below the second level, with a note saying so.
4
+ // A collapsed node shows its size and, when it has one, a label: "5 keys · PAY-2026-0042".
5
+ // Port of mocks/schema.js (jsonLines, jsonView).
6
+ import { esc, icon } from './components.js';
7
+ import { formatBytes, formatNumber, t } from './i18n.js';
8
+ // Importing it installs the copy handler that keeps line numbers out of copied code.
9
+ import './code-copy.js';
10
+
11
+ /** Bodies with more lines than this start collapsed below the second level. */
12
+ export const LARGE_LINES = 2000;
13
+
14
+ const token = (text, kind) => ({ text, kind });
15
+ const punctuation = text => token(text, 'p');
16
+ const scalar = (value, source) => token(source ?? JSON.stringify(value), typeof value === 'string' ? 's' : typeof value === 'number' ? 'n' : 'b');
17
+ /** The id of a path in the line model. */
18
+ export const pathId = path => JSON.stringify(path);
19
+ const own = (value, key) => Object.hasOwn(value, key) ? value[key] : undefined;
20
+
21
+ /** A short name for a collapsed object: its number, name, reference, display name, IBAN or id. */
22
+ export function labelOf(value) {
23
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return '';
24
+ for (const key of ['number', 'name', 'reference', 'displayName', 'iban', 'id']) {
25
+ const found = own(value, key);
26
+ if (typeof found === 'string' && found.trim() || typeof found === 'number') return String(found).trim().slice(0, 32);
27
+ }
28
+ return '';
29
+ }
30
+
31
+ /**
32
+ * Turns a parsed JSON value into logical lines. Each line is `{ id, depth, path, ancestors,
33
+ * tokens, folded?, fold?, copy? }`. `branches` are the ids of foldable nodes (every object and
34
+ * array below the root); `initiallyFolded` is empty unless the body is large.
35
+ *
36
+ * @param {{ body: unknown, numbers?: WeakMap<object, Map<string,string>>, rootNumber?: string }} content
37
+ */
38
+ export function buildJsonLines({ body, numbers, rootNumber }) {
39
+ const lines = [];
40
+ const branches = new Set();
41
+ function visit(value, source, depth, path, ancestors, key, last) {
42
+ const before = key === undefined ? [] : [token(JSON.stringify(key), 'k'), punctuation(': ')];
43
+ const after = last ? [] : [punctuation(',')];
44
+ const id = pathId(path);
45
+ const copy = depth ? { value, source } : undefined;
46
+ if (value === null || typeof value !== 'object') {
47
+ lines.push({ id, depth, path, ancestors, tokens: [...before, scalar(value, source), ...after], copy });
48
+ return;
49
+ }
50
+ const list = Array.isArray(value);
51
+ const entries = Object.entries(value);
52
+ const open = punctuation(list ? '[' : '{');
53
+ const close = punctuation(list ? ']' : '}');
54
+ const first = entries[0]?.[1];
55
+ // Empty containers and one-scalar arrays are a single line with nothing to fold.
56
+ if (!entries.length || (list && entries.length === 1 && (first === null || typeof first !== 'object') && JSON.stringify(value).length <= 80)) {
57
+ const contents = entries.length ? [scalar(first, numbers?.get(value)?.get('0'))] : [];
58
+ lines.push({ id, depth, path, ancestors, tokens: [...before, open, ...contents, close, ...after], copy });
59
+ return;
60
+ }
61
+ const label = labelOf(value);
62
+ const summary = t(list ? 'resp.summary.items' : 'resp.summary.keys', { n: entries.length });
63
+ // The root can fold too, but Collapse all leaves it open (so something stays visible).
64
+ const fold = true;
65
+ if (depth > 0) branches.add(id);
66
+ const folded = [...before, open, token(summary + (label ? ` · ${label}` : ''), 'folded'), close, ...after];
67
+ lines.push({ id, depth, path, ancestors, tokens: [...before, open], folded, fold, copy });
68
+ const parents = [...ancestors, id];
69
+ entries.forEach(([name, entry], index) =>
70
+ visit(entry, numbers?.get(value)?.get(name), depth + 1, [...path, name], parents, list ? undefined : name, index === entries.length - 1));
71
+ lines.push({ id: id + ':end', depth, ancestors: parents, tokens: [close, ...after] });
72
+ }
73
+ visit(body, rootNumber, 0, [], [], undefined, true);
74
+ const large = lines.length > LARGE_LINES;
75
+ // Large bodies open the root and its direct children and fold everything deeper.
76
+ const initiallyFolded = new Set(large ? lines.filter(line => line.fold && line.depth >= 2).map(line => line.id) : []);
77
+ return { lines, branches, initiallyFolded, large };
78
+ }
79
+
80
+ /** HTML for a token list. `folded` tokens are the small "N keys" badge. */
81
+ export const tokensHtml = tokens => tokens.map(({ text, kind }) => kind ? `<span class="${kind === 'folded' ? kind : 'tok-' + kind}">${esc(text)}</span>` : esc(text)).join('');
82
+
83
+ /**
84
+ * One line of a code view: line number, fold button (only for foldable nodes), the source and
85
+ * optional actions. `html` is the already-marked-up source (search marks, tokens).
86
+ */
87
+ export function codeLine(line, number, folded, html, actions = '', { cls = '', note = '' } = {}) {
88
+ const fold = line.fold
89
+ ? `<button type="button" class="fold" data-fold-line="${esc(line.id)}" aria-label="${esc(t(folded ? 'resp.expandName' : 'resp.collapseName', { name: line.path.at(-1) ?? t('json.root') }))}" aria-expanded="${!folded}">${icon(folded ? 'chevron-right' : 'chevron-down', true)}</button>`
90
+ : '<span></span>';
91
+ // The number is drawn by CSS from data-n (.ln .no::before), so it is not text: selecting or
92
+ // copying lines can never pick it up, in Selection.toString() as well as in the clipboard.
93
+ // A line the schema check flagged carries a quiet reason at its end (mock 101).
94
+ return `<div class="ln${cls ? ' ' + cls : ''}"><span class="no" data-n="${formatNumber(number, { useGrouping: false })}" aria-hidden="true"></span>${fold}<code class="src" style="--d:${line.depth}">${html}${note ? `<span class="ln-note">${icon('warn', true)}${esc(note)}</span>` : ''}</code>${actions}</div>`;
95
+ }
96
+
97
+ /** The note shown above a large body. */
98
+ export const largeNote = (bytes, lines) => `<div class="inline-note flush large-note" role="note">${icon('info', true)}<span>${esc(t('json.large', { size: formatBytes(bytes), n: formatNumber(lines) }))}</span></div>`;
99
+
100
+ /** Visible lines for a fold set: a line is hidden when any ancestor is folded. */
101
+ export const visibleLines = (lines, folded) => lines.filter(line => !line.ancestors.some(id => folded.has(id)));
102
+
103
+ const views = new Map();
104
+ const bound = new WeakSet();
105
+ let viewCounter = 0;
106
+
107
+ /**
108
+ * A read-only JSON viewer for examples and JSON Schema. Returns `{ html, lines, large }`; call
109
+ * {@link bindJsonViews} on a container once `html` is in the document. The viewer starts with
110
+ * every node open unless the value is large (see LARGE_LINES).
111
+ */
112
+ export function jsonView(value, { label = 'JSON' } = {}) {
113
+ const model = buildJsonLines({ body: value });
114
+ const key = `json${++viewCounter}`;
115
+ const folded = new Set(model.initiallyFolded);
116
+ views.set(key, { model, folded });
117
+ const bytes = JSON.stringify(value)?.length ?? 0;
118
+ return {
119
+ key, lines: model.lines.length, large: model.large, foldable: model.branches.size > 0,
120
+ html: `<div data-json-view="${key}" data-json-label="${esc(label)}">${model.large ? largeNote(bytes, model.lines.length) : ''}${viewHtml(model, folded, label)}</div>`,
121
+ };
122
+ }
123
+
124
+ function viewHtml(model, folded, label) {
125
+ const lines = visibleLines(model.lines, folded);
126
+ return `<div class="code-view ltr" role="group" aria-label="${esc(label)}">${lines.map((line, index) =>
127
+ codeLine(line, index + 1, folded.has(line.id), tokensHtml(folded.has(line.id) ? line.folded : line.tokens))).join('')}</div>`;
128
+ }
129
+
130
+ /** Wires folding for every static viewer under a container. */
131
+ export function bindJsonViews(container) {
132
+ for (const element of container.querySelectorAll('[data-json-view]')) {
133
+ const entry = views.get(element.dataset.jsonView);
134
+ if (!entry) continue;
135
+ views.delete(element.dataset.jsonView);
136
+ element.jsonView = entry;
137
+ }
138
+ if (bound.has(container)) return;
139
+ bound.add(container);
140
+ container.addEventListener('click', event => {
141
+ const button = event.target.closest?.('[data-fold-line]');
142
+ const element = button?.closest('[data-json-view]');
143
+ if (!element?.jsonView) return;
144
+ const { folded } = element.jsonView;
145
+ const id = button.dataset.foldLine;
146
+ if (folded.has(id)) folded.delete(id); else folded.add(id);
147
+ redraw(element);
148
+ element.querySelector(`[data-fold-line="${CSS.escape(id)}"]`)?.focus({ preventScroll: true });
149
+ });
150
+ }
151
+
152
+ function redraw(element) {
153
+ const { model, folded } = element.jsonView;
154
+ element.querySelector('.code-view').outerHTML = viewHtml(model, folded, element.dataset.jsonLabel);
155
+ }
156
+
157
+ /** Whether any foldable node of a static viewer is collapsed (the button then says Expand all). */
158
+ export const hasCollapsed = element => !!element?.jsonView && element.jsonView.folded.size > 0;
159
+
160
+ /** Collapses (`collapse` true) or expands every foldable node of a static viewer. */
161
+ export function setAllFolded(element, collapse) {
162
+ const entry = element?.jsonView;
163
+ if (!entry) return;
164
+ entry.folded = new Set(collapse ? entry.model.branches : []);
165
+ redraw(element);
166
+ }
@@ -0,0 +1,62 @@
1
+ // Language and tool marks for the code sample picker.
2
+ //
3
+ // The brand paths are from Simple Icons (https://simpleicons.org, v16.38.0), released under CC0 1.0
4
+ // (public domain), and are vendored here so the UI needs no network or build step. Trademarks
5
+ // belong to their owners; the marks only name the language a sample is written in.
6
+ //
7
+ // curl -> curl javascript -> JavaScript (also used for fetch) python -> Python go -> Go
8
+ // kotlin -> Kotlin php -> PHP ruby -> Ruby rust -> Rust
9
+ //
10
+ // Simple Icons has no C#, Java or PowerShell mark that reads at 16 px (the .NET wordmark is a 6 px
11
+ // strip and the OpenJDK figure dissolves into hairlines). Those three are drawn here as geometric
12
+ // outline marks on the same 24 x 24 grid: a hexagon holding C and # for C#, a coffee cup for Java
13
+ // (a generic cup, not Oracle's logo) and a rounded tile with the `>_` prompt for PowerShell.
14
+ //
15
+ // Optical sizing: every mark carries its measured bounding box `[x, y, width, height]` and the
16
+ // `size` its larger side should have on the 24 x 24 grid. The transform below centres the box and
17
+ // scales it to that size, so solid squares (JavaScript, Kotlin) are drawn smaller than outlines and
18
+ // the short, wide wordmarks (Go, PHP) are drawn larger than the grid; the icon's SVG leaves
19
+ // overflow visible for them. Change `size`, never the CSS, to rebalance a mark.
20
+ //
21
+ // Every mark paints with currentColor, so it takes the surrounding text colour in both themes.
22
+ // Marks are decorative (aria-hidden); the language name next to them is the accessible label.
23
+ import { esc } from './components.js';
24
+
25
+ /** Centres a mark's bounding box on the grid and scales its larger side to `size`. */
26
+ const place = ([x, y, width, height], size) => {
27
+ const scale = size / Math.max(width, height);
28
+ return { scale, transform: `translate(12 12) scale(${+scale.toFixed(4)}) translate(${-(x + width / 2)} ${-(y + height / 2)})` };
29
+ };
30
+ /** A filled brand path. */
31
+ const fill = (path, box, size) => `<g transform="${place(box, size).transform}"><path d="${path}"/></g>`;
32
+ /** An outline mark; the stroke is divided by the scale so every mark has the same line weight. */
33
+ const outline = (body, box, size) => {
34
+ const { scale, transform } = place(box, size);
35
+ return `<g transform="${transform}" fill="none" stroke="currentColor" stroke-width="${+(1.7 / scale).toFixed(3)}" stroke-linecap="round" stroke-linejoin="round">${body}</g>`;
36
+ };
37
+
38
+ const MARKS = {
39
+ curl: fill("M.803 14.8169c0-.5342.433-.9665.9665-.9665.5335 0 .9665.4323.9665.9665 0 .5335-.433.9657-.9665.9657-.5335 0-.9666-.4322-.9666-.9657m2.736 0c0-.1963-.0532-.376-.1119-.5525-.2344-.7024-.876-1.2169-1.6575-1.2169-.1249 0-.2344.0465-.3524.0708C.6149 13.2865 0 13.9646 0 14.817c0 .9764.7923 1.7694 1.7695 1.7694.9772 0 1.7694-.793 1.7694-1.7694m-1.7694-7.149c.5335 0 .9665.433.9665.9665 0 .5335-.433.9665-.9665.9665-.5343 0-.9666-.433-.9666-.9665 0-.5335.4323-.9665.9666-.9665m0 2.7359c.9772 0 1.7694-.7923 1.7694-1.7694 0-.1956-.0532-.376-.1119-.5525-.2344-.7024-.8767-1.2169-1.6575-1.2169-.1249 0-.2344.0465-.3524.0716C.6149 7.104 0 7.782 0 8.6344c0 .9771.7923 1.7694 1.7695 1.7694m13.221-5.694c-.5342 0-.9665-.433-.9665-.9664a.966.966 0 01.9666-.9665c.5335 0 .9658.4322.9658.9665 0 .5334-.4323.9664-.9658.9664m-9.6 16.5133c-.5335 0-.9666-.433-.9666-.9665 0-.5342.433-.9665.9666-.9665a.966.966 0 01.9665.9665c0 .5335-.4323.9665-.9665.9665m9.6-19.2491c-.978 0-1.7695.7922-1.7695 1.7694 0 .2085.0525.4025.1187.5882L5.039 18.5581c-.803.1681-1.4179.8462-1.4179 1.6985 0 .9772.7923 1.7694 1.7695 1.7694.9772 0 1.7694-.7922 1.7694-1.7694 0-.1963-.0525-.3759-.111-.5525l8.3427-14.2728c.7778-.1865 1.3683-.8531 1.3683-1.688 0-.977-.793-1.7693-1.7694-1.7693m7.24 2.7359c-.5343 0-.9666-.433-.9666-.9665a.966.966 0 01.9665-.9665c.5335 0 .9666.4322.9666.9665 0 .5334-.433.9665-.9666.9665M12.6313 21.223c-.5343 0-.9665-.433-.9665-.9665a.966.966 0 01.9665-.9665c.5335 0 .9658.4323.9658.9665 0 .5335-.4323.9665-.9658.9665M22.2305 1.974c-.9772 0-1.7694.7922-1.7694 1.7694 0 .2085.0525.4025.1187.5882l-8.3009 14.2265c-.8021.1681-1.417.8462-1.417 1.6985 0 .9772.7922 1.7694 1.7694 1.7694.9764 0 1.7687-.7922 1.7687-1.7694 0-.1963-.0525-.3759-.1111-.5525l8.3427-14.2728C23.4094 5.2448 24 4.5782 24 3.7433c0-.977-.7923-1.7693-1.7695-1.7693", [0, 1.97, 24, 20.05], 22),
40
+ javascript: fill("M0 0h24v24H0V0zm22.034 18.276c-.175-1.095-.888-2.015-3.003-2.873-.736-.345-1.554-.585-1.797-1.14-.091-.33-.105-.51-.046-.705.15-.646.915-.84 1.515-.66.39.12.75.42.976.9 1.034-.676 1.034-.676 1.755-1.125-.27-.42-.404-.601-.586-.78-.63-.705-1.469-1.065-2.834-1.034l-.705.089c-.676.165-1.32.525-1.71 1.005-1.14 1.291-.811 3.541.569 4.471 1.365 1.02 3.361 1.244 3.616 2.205.24 1.17-.87 1.545-1.966 1.41-.811-.18-1.26-.586-1.755-1.336l-1.83 1.051c.21.48.45.689.81 1.109 1.74 1.756 6.09 1.666 6.871-1.004.029-.09.24-.705.074-1.65l.046.067zm-8.983-7.245h-2.248c0 1.938-.009 3.864-.009 5.805 0 1.232.063 2.363-.138 2.711-.33.689-1.18.601-1.566.48-.396-.196-.597-.466-.83-.855-.063-.105-.11-.196-.127-.196l-1.825 1.125c.305.63.75 1.172 1.324 1.517.855.51 2.004.675 3.207.405.783-.226 1.458-.691 1.811-1.411.51-.93.402-2.07.397-3.346.012-2.054 0-4.109 0-6.179l.004-.056z", [0, 0, 24, 24], 19),
41
+ python: fill("M11.858 0c-.978.005-1.913.088-2.736.234C6.7.662 6.26 1.558 6.26 3.21v2.182h5.725v.727H4.111c-1.664 0-3.121 1-3.577 2.903-.526 2.181-.549 3.542 0 5.82.407 1.694 1.379 2.902 3.043 2.902h1.969v-2.616c0-1.89 1.635-3.557 3.576-3.557h5.72c1.591 0 2.862-1.31 2.862-2.91v-5.45c0-1.553-1.309-2.718-2.863-2.977A18 18 0 0 0 11.858 0M8.762 1.755c.591 0 1.074.491 1.074 1.095 0 .601-.483 1.087-1.074 1.087A1.08 1.08 0 0 1 7.688 2.85c0-.604.48-1.095 1.074-1.095m9.656 4.364v2.543c0 1.971-1.671 3.63-3.577 3.63H9.122c-1.566 0-2.862 1.34-2.862 2.91v5.452c0 1.551 1.349 2.464 2.862 2.91 1.813.532 3.55.628 5.72 0 1.44-.418 2.862-1.258 2.862-2.91V18.47h-5.719v-.727h8.582c1.664 0 2.284-1.16 2.862-2.903.598-1.793.573-3.518 0-5.819-.41-1.656-1.196-2.903-2.862-2.903zm-3.216 13.807a1.08 1.08 0 0 1 1.074 1.088c0 .604-.48 1.094-1.074 1.094-.592 0-1.075-.49-1.075-1.094 0-.601.483-1.088 1.075-1.088", [0.13, 0, 23.74, 24], 21),
42
+ go: fill("M1.811 10.231c-.047 0-.058-.023-.035-.059l.246-.315c.023-.035.081-.058.128-.058h4.172c.046 0 .058.035.035.07l-.199.303c-.023.036-.082.07-.117.07zM.047 11.306c-.047 0-.059-.023-.035-.058l.245-.316c.023-.035.082-.058.129-.058h5.328c.047 0 .07.035.058.07l-.093.28c-.012.047-.058.07-.105.07zm2.828 1.075c-.047 0-.059-.035-.035-.07l.163-.292c.023-.035.07-.07.117-.07h2.337c.047 0 .07.035.07.082l-.023.28c0 .047-.047.082-.082.082zm12.129-2.36c-.736.187-1.239.327-1.963.514-.176.046-.187.058-.34-.117-.174-.199-.303-.327-.548-.444-.737-.362-1.45-.257-2.115.175-.795.514-1.204 1.274-1.192 2.22.011.935.654 1.706 1.577 1.835.795.105 1.46-.175 1.987-.77.105-.13.198-.27.315-.434H10.47c-.245 0-.304-.152-.222-.35.152-.362.432-.97.596-1.274a.315.315 0 01.292-.187h4.253c-.023.316-.023.631-.07.947a4.983 4.983 0 01-.958 2.29c-.841 1.11-1.94 1.8-3.33 1.986-1.145.152-2.209-.07-3.143-.77-.865-.655-1.356-1.52-1.484-2.595-.152-1.274.222-2.419.993-3.424.83-1.086 1.928-1.776 3.272-2.02 1.098-.2 2.15-.07 3.096.571.62.41 1.063.97 1.356 1.648.07.105.023.164-.117.2m3.868 6.461c-1.064-.024-2.034-.328-2.852-1.029a3.665 3.665 0 01-1.262-2.255c-.21-1.32.152-2.489.947-3.529.853-1.122 1.881-1.706 3.272-1.95 1.192-.21 2.314-.095 3.33.595.923.63 1.496 1.484 1.648 2.605.198 1.578-.257 2.863-1.344 3.962-.771.783-1.718 1.273-2.805 1.495-.315.06-.63.07-.934.106zm2.78-4.72c-.011-.153-.011-.27-.034-.387-.21-1.157-1.274-1.81-2.384-1.554-1.087.245-1.788.935-2.045 2.033-.21.912.234 1.835 1.075 2.21.643.28 1.285.244 1.905-.07.923-.48 1.425-1.228 1.484-2.233z", [0, 7.52, 24, 8.97], 30),
43
+ kotlin: fill("M24 24H0V0h24L12 12Z", [0, 0, 24, 24], 19),
44
+ php: fill("M7.01 10.207h-.944l-.515 2.648h.838c.556 0 .97-.105 1.242-.314.272-.21.455-.559.55-1.049.092-.47.05-.802-.124-.995-.175-.193-.523-.29-1.047-.29zM12 5.688C5.373 5.688 0 8.514 0 12s5.373 6.313 12 6.313S24 15.486 24 12c0-3.486-5.373-6.312-12-6.312zm-3.26 7.451c-.261.25-.575.438-.917.551-.336.108-.765.164-1.285.164H5.357l-.327 1.681H3.652l1.23-6.326h2.65c.797 0 1.378.209 1.744.628.366.418.476 1.002.33 1.752a2.836 2.836 0 0 1-.305.847c-.143.255-.33.49-.561.703zm4.024.715l.543-2.799c.063-.318.039-.536-.068-.651-.107-.116-.336-.174-.687-.174H11.46l-.704 3.625H9.388l1.23-6.327h1.367l-.327 1.682h1.218c.767 0 1.295.134 1.586.401s.378.7.263 1.299l-.572 2.944h-1.389zm7.597-2.265a2.782 2.782 0 0 1-.305.847c-.143.255-.33.49-.561.703a2.44 2.44 0 0 1-.917.551c-.336.108-.765.164-1.286.164h-1.18l-.327 1.682h-1.378l1.23-6.326h2.649c.797 0 1.378.209 1.744.628.366.417.477 1.001.331 1.751zM17.766 10.207h-.943l-.516 2.648h.838c.557 0 .971-.105 1.242-.314.272-.21.455-.559.551-1.049.092-.47.049-.802-.125-.995s-.524-.29-1.047-.29z", [0, 5.69, 24, 12.63], 27),
45
+ ruby: fill("M20.156.083c3.033.525 3.893 2.598 3.829 4.77L24 4.822 22.635 22.71 4.89 23.926h.016C3.433 23.864.15 23.729 0 19.139l1.645-3 2.819 6.586.503 1.172 2.805-9.144-.03.007.016-.03 9.255 2.956-1.396-5.431-.99-3.9 8.82-.569-.615-.51L16.5 2.114 20.159.073l-.003.01zM0 19.089zM5.13 5.073c3.561-3.533 8.157-5.621 9.922-3.84 1.762 1.777-.105 6.105-3.673 9.636-3.563 3.532-8.103 5.734-9.864 3.957-1.766-1.777.045-6.217 3.612-9.75l.003-.003z", [0, 0.07, 24, 23.85], 22),
46
+ rust: fill("M23.8346 11.7033l-1.0073-.6236a13.7268 13.7268 0 00-.0283-.2936l.8656-.8069a.3483.3483 0 00-.1154-.578l-1.1066-.414a8.4958 8.4958 0 00-.087-.2856l.6904-.9587a.3462.3462 0 00-.2257-.5446l-1.1663-.1894a9.3574 9.3574 0 00-.1407-.2622l.49-1.0761a.3437.3437 0 00-.0274-.3361.3486.3486 0 00-.3006-.154l-1.1845.0416a6.7444 6.7444 0 00-.1873-.2268l.2723-1.153a.3472.3472 0 00-.417-.4172l-1.1532.2724a14.0183 14.0183 0 00-.2278-.1873l.0415-1.1845a.3442.3442 0 00-.49-.328l-1.076.491c-.0872-.0476-.1742-.0952-.2623-.1407l-.1903-1.1673A.3483.3483 0 0016.256.955l-.9597.6905a8.4867 8.4867 0 00-.2855-.086l-.414-1.1066a.3483.3483 0 00-.5781-.1154l-.8069.8666a9.2936 9.2936 0 00-.2936-.0284L12.2946.1683a.3462.3462 0 00-.5892 0l-.6236 1.0073a13.7383 13.7383 0 00-.2936.0284L9.9803.3374a.3462.3462 0 00-.578.1154l-.4141 1.1065c-.0962.0274-.1903.0567-.2855.086L7.744.955a.3483.3483 0 00-.5447.2258L7.009 2.348a9.3574 9.3574 0 00-.2622.1407l-1.0762-.491a.3462.3462 0 00-.49.328l.0416 1.1845a7.9826 7.9826 0 00-.2278.1873L3.8413 3.425a.3472.3472 0 00-.4171.4171l.2713 1.1531c-.0628.075-.1255.1509-.1863.2268l-1.1845-.0415a.3462.3462 0 00-.328.49l.491 1.0761a9.167 9.167 0 00-.1407.2622l-1.1662.1894a.3483.3483 0 00-.2258.5446l.6904.9587a13.303 13.303 0 00-.087.2855l-1.1065.414a.3483.3483 0 00-.1155.5781l.8656.807a9.2936 9.2936 0 00-.0283.2935l-1.0073.6236a.3442.3442 0 000 .5892l1.0073.6236c.008.0982.0182.1964.0283.2936l-.8656.8079a.3462.3462 0 00.1155.578l1.1065.4141c.0273.0962.0567.1914.087.2855l-.6904.9587a.3452.3452 0 00.2268.5447l1.1662.1893c.0456.088.0922.1751.1408.2622l-.491 1.0762a.3462.3462 0 00.328.49l1.1834-.0415c.0618.0769.1235.1528.1873.2277l-.2713 1.1541a.3462.3462 0 00.4171.4161l1.153-.2713c.075.0638.151.1255.2279.1863l-.0415 1.1845a.3442.3442 0 00.49.327l1.0761-.49c.087.0486.1741.0951.2622.1407l.1903 1.1662a.3483.3483 0 00.5447.2268l.9587-.6904a9.299 9.299 0 00.2855.087l.414 1.1066a.3452.3452 0 00.5781.1154l.8079-.8656c.0972.0111.1954.0203.2936.0294l.6236 1.0073a.3472.3472 0 00.5892 0l.6236-1.0073c.0982-.0091.1964-.0183.2936-.0294l.8069.8656a.3483.3483 0 00.578-.1154l.4141-1.1066a8.4626 8.4626 0 00.2855-.087l.9587.6904a.3452.3452 0 00.5447-.2268l.1903-1.1662c.088-.0456.1751-.0931.2622-.1407l1.0762.49a.3472.3472 0 00.49-.327l-.0415-1.1845a6.7267 6.7267 0 00.2267-.1863l1.1531.2713a.3472.3472 0 00.4171-.416l-.2713-1.1542c.0628-.0749.1255-.1508.1863-.2278l1.1845.0415a.3442.3442 0 00.328-.49l-.49-1.076c.0475-.0872.0951-.1742.1407-.2623l1.1662-.1893a.3483.3483 0 00.2258-.5447l-.6904-.9587.087-.2855 1.1066-.414a.3462.3462 0 00.1154-.5781l-.8656-.8079c.0101-.0972.0202-.1954.0283-.2936l1.0073-.6236a.3442.3442 0 000-.5892zm-6.7413 8.3551a.7138.7138 0 01.2986-1.396.714.714 0 11-.2997 1.396zm-.3422-2.3142a.649.649 0 00-.7715.5l-.3573 1.6685c-1.1035.501-2.3285.7795-3.6193.7795a8.7368 8.7368 0 01-3.6951-.814l-.3574-1.6684a.648.648 0 00-.7714-.499l-1.473.3158a8.7216 8.7216 0 01-.7613-.898h7.1676c.081 0 .1356-.0141.1356-.088v-2.536c0-.074-.0536-.0881-.1356-.0881h-2.0966v-1.6077h2.2677c.2065 0 1.1065.0587 1.394 1.2088.0901.3533.2875 1.5044.4232 1.8729.1346.413.6833 1.2381 1.2685 1.2381h3.5716a.7492.7492 0 00.1296-.0131 8.7874 8.7874 0 01-.8119.9526zM6.8369 20.024a.714.714 0 11-.2997-1.396.714.714 0 01.2997 1.396zM4.1177 8.9972a.7137.7137 0 11-1.304.5791.7137.7137 0 011.304-.579zm-.8352 1.9813l1.5347-.6824a.65.65 0 00.33-.8585l-.3158-.7147h1.2432v5.6025H3.5669a8.7753 8.7753 0 01-.2834-3.348zm6.7343-.5437V8.7836h2.9601c.153 0 1.0792.1772 1.0792.8697 0 .575-.7107.7815-1.2948.7815zm10.7574 1.4862c0 .2187-.008.4363-.0243.651h-.9c-.09 0-.1265.0586-.1265.1477v.413c0 .973-.5487 1.1846-1.0296 1.2382-.4576.0517-.9648-.1913-1.0275-.4717-.2704-1.5186-.7198-1.8436-1.4305-2.4034.8817-.5599 1.799-1.386 1.799-2.4915 0-1.1936-.819-1.9458-1.3769-2.3153-.7825-.5163-1.6491-.6195-1.883-.6195H5.4682a8.7651 8.7651 0 014.907-2.7699l1.0974 1.151a.648.648 0 00.9182.0213l1.227-1.1743a8.7753 8.7753 0 016.0044 4.2762l-.8403 1.8982a.652.652 0 00.33.8585l1.6178.7188c.0283.2875.0425.577.0425.8717zm-9.3006-9.5993a.7128.7128 0 11.984 1.0316.7137.7137 0 01-.984-1.0316zm8.3389 6.71a.7107.7107 0 01.9395-.3625.7137.7137 0 11-.9405.3635z", [0, 0, 24, 23.99], 22),
47
+ // C#: hexagon, a C, and a # made of two slanted and two level bars.
48
+ csharp: outline('<path d="M12 2.5 20.2 7.25v9.5L12 21.5l-8.2-4.75v-9.5z"/><path d="M10.4 9.6a3.1 3.1 0 1 0 0 4.8"/><path d="M14 10.2h4.6M14 13.8h4.6M15.7 8.6l-.5 6.8M17.9 8.6l-.5 6.8"/>', [3.8, 2.5, 16.4, 19], 20),
49
+ // Java: a cup with a handle, a saucer line and two wisps of steam.
50
+ java: outline('<path d="M4.5 10.5h11v4.5a4.5 4.5 0 0 1-4.5 4.5H9a4.5 4.5 0 0 1-4.5-4.5z"/><path d="M15.5 11.5h1.2a2.6 2.6 0 0 1 0 5.2h-1.2"/><path d="M3.5 22.2h14"/><path d="M8 3.2c-1.1 1.4 1.1 2.4 0 4.2M12 3.2c-1.1 1.4 1.1 2.4 0 4.2"/>', [3.5, 3, 15.4, 19.7], 21),
51
+ // PowerShell: a rounded tile holding the prompt chevron and cursor bar.
52
+ powershell: outline('<rect x="2.5" y="4.5" width="19" height="15" rx="3"/><path d="m7.5 9.5 3.5 2.5-3.5 2.5"/><path d="M12.5 15h4"/>', [2.5, 4.5, 19, 15], 21),
53
+ };
54
+
55
+ /** Ids with a mark, in picker order. */
56
+ export const langIconIds = Object.keys(MARKS);
57
+
58
+ /** The decorative SVG for a language id (curl, python, ...). Unknown ids draw a generic code mark. */
59
+ export function langIcon(id) {
60
+ const body = MARKS[id] ?? outline('<path d="m16 18 6-6-6-6"/><path d="m8 6-6 6 6 6"/>', [2, 6, 20, 12], 20);
61
+ return `<svg class="lang-ico" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" focusable="false" data-lang-icon="${esc(id)}">${body}</svg>`;
62
+ }
@@ -0,0 +1,262 @@
1
+ import { formatNumber, t } from "./i18n.js";
2
+
3
+ const DESKTOP_BREAKPOINT = 56;
4
+ const SIDEBAR_MIN_REM = 14;
5
+ const SIDEBAR_MAX_REM = 24;
6
+ const PANE_MIN_REM = 18;
7
+ const KEYBOARD_STEP_REM = 1;
8
+
9
+ const cssPixels = (value) => Number.parseFloat(value) || 0;
10
+
11
+ export function clampTrack(value, minimum, maximum) {
12
+ return Math.min(Math.max(value, minimum), Math.max(minimum, maximum));
13
+ }
14
+
15
+ export function keyboardTrackValue(value, key, minimum, maximum, step) {
16
+ if (key === "Home") return minimum;
17
+ if (key === "End") return maximum;
18
+ const multiplier = key === "PageUp" || key === "PageDown" ? 4 : 1;
19
+ const direction =
20
+ key === "ArrowLeft" || key === "ArrowUp" || key === "PageUp" ? -1 : 1;
21
+ return clampTrack(value + direction * step * multiplier, minimum, maximum);
22
+ }
23
+
24
+ /** Add desktop splitters while leaving the mobile drawer and view switcher intact. */
25
+ export function createLayout(workspace, preferenceKey) {
26
+ const handles = [...workspace.querySelectorAll("[data-layout-divider]")];
27
+ const sidebar = workspace.querySelector(".sidebar");
28
+ // The main column is whichever pane is showing (Try it's request, or a reading pane), so the
29
+ // divider is shared by every view.
30
+ const mainPane = () => workspace.querySelector(":scope > .pane:not([hidden])");
31
+ let resizing = null;
32
+ let resizeObserver;
33
+ const storageKey = preferenceKey && `sendproof-layout:${preferenceKey}`;
34
+ let saved;
35
+ if (storageKey) {
36
+ try {
37
+ const value = JSON.parse(localStorage.getItem(storageKey) || "null");
38
+ if (Number.isFinite(value?.sidebar) && Number.isFinite(value?.request)) saved = value;
39
+ } catch { /* A stored preference can be invalid or inaccessible. */ }
40
+ }
41
+
42
+ const isDesktop = () =>
43
+ typeof window === "undefined" ||
44
+ window.matchMedia(`(min-width: ${DESKTOP_BREAKPOINT}rem)`).matches;
45
+
46
+ const rootFontSize = () =>
47
+ cssPixels(getComputedStyle(document.documentElement).fontSize) || 16;
48
+
49
+ const measurements = () => {
50
+ const workspaceWidth = workspace.getBoundingClientRect().width;
51
+ const rem = rootFontSize();
52
+ const sidebarWidth = sidebar.getBoundingClientRect().width;
53
+ const requestWidth = mainPane()?.getBoundingClientRect().width ?? 0;
54
+ const paneMinimum = Math.max(
55
+ PANE_MIN_REM * rem,
56
+ Math.min(24 * rem, workspaceWidth * 0.25),
57
+ );
58
+ const sidebarMinimum = SIDEBAR_MIN_REM * rem;
59
+ const sidebarMaximum = Math.min(
60
+ SIDEBAR_MAX_REM * rem,
61
+ workspaceWidth - paneMinimum * 2,
62
+ );
63
+ const currentSidebar = clampTrack(
64
+ sidebarWidth,
65
+ sidebarMinimum,
66
+ sidebarMaximum,
67
+ );
68
+ const requestMinimum = paneMinimum;
69
+ const requestMaximum = workspaceWidth - currentSidebar - paneMinimum;
70
+ return {
71
+ workspaceWidth,
72
+ rem,
73
+ sidebarWidth: currentSidebar,
74
+ requestWidth: clampTrack(requestWidth, requestMinimum, requestMaximum),
75
+ sidebarMinimum,
76
+ sidebarMaximum,
77
+ requestMinimum,
78
+ requestMaximum,
79
+ };
80
+ };
81
+
82
+ const updateHandle = (handle, value, minimum, maximum) => {
83
+ handle.setAttribute("aria-valuemin", String(Math.round(minimum)));
84
+ handle.setAttribute("aria-valuemax", String(Math.round(maximum)));
85
+ handle.setAttribute("aria-valuenow", String(Math.round(value)));
86
+ const px = formatNumber(Math.round(value));
87
+ handle.setAttribute("aria-valuetext", t("shell.pixels", { px }));
88
+ // Dividers are placed from the inline start, so they sit correctly in right-to-left layouts.
89
+ if (handle.style) handle.style.insetInlineStart = `${Math.round(handle === handles[0] ? value : value + sidebar.getBoundingClientRect().width)}px`;
90
+ const tip = handle.querySelector?.(".size-tip");
91
+ if (tip) tip.textContent = t("shell.sizeTip", { px });
92
+ };
93
+
94
+ const apply = (sidebarWidth, requestWidth) => {
95
+ const metrics = measurements();
96
+ const nextSidebar = clampTrack(
97
+ sidebarWidth,
98
+ metrics.sidebarMinimum,
99
+ metrics.sidebarMaximum,
100
+ );
101
+ const nextRequest = clampTrack(
102
+ requestWidth,
103
+ metrics.requestMinimum,
104
+ metrics.workspaceWidth - nextSidebar - metrics.requestMinimum,
105
+ );
106
+ workspace.style.setProperty("--sidebar-width", `${nextSidebar}px`);
107
+ workspace.style.setProperty("--request-width", `${nextRequest}px`);
108
+ updateHandle(
109
+ handles[0],
110
+ nextSidebar,
111
+ metrics.sidebarMinimum,
112
+ metrics.sidebarMaximum,
113
+ );
114
+ updateHandle(
115
+ handles[1],
116
+ nextRequest,
117
+ metrics.requestMinimum,
118
+ metrics.workspaceWidth - nextSidebar - metrics.requestMinimum,
119
+ );
120
+ };
121
+
122
+ const sync = () => {
123
+ if (!isDesktop()) return;
124
+ if (workspace.dataset?.sidebar === "closed") {
125
+ const divider = handles[1];
126
+ if (divider.style) divider.style.insetInlineStart = `${Math.round(mainPane()?.getBoundingClientRect().width ?? 0)}px`;
127
+ return;
128
+ }
129
+ const metrics = measurements();
130
+ if (saved) {
131
+ const side = clampTrack(saved.sidebar * metrics.rem, metrics.sidebarMinimum, metrics.sidebarMaximum);
132
+ apply(side, (metrics.workspaceWidth - side) * saved.request);
133
+ return;
134
+ }
135
+ updateHandle(handles[0], metrics.sidebarWidth, metrics.sidebarMinimum, metrics.sidebarMaximum);
136
+ updateHandle(handles[1], metrics.requestWidth, metrics.requestMinimum, metrics.requestMaximum);
137
+ };
138
+
139
+ const save = () => {
140
+ const metrics = measurements();
141
+ saved = { sidebar: metrics.sidebarWidth / metrics.rem,
142
+ request: metrics.requestWidth / (metrics.workspaceWidth - metrics.sidebarWidth) };
143
+ if (storageKey) localStorage.setItem(storageKey, JSON.stringify(saved));
144
+ };
145
+
146
+ const reset = () => {
147
+ saved = null;
148
+ if (storageKey) localStorage.removeItem(storageKey);
149
+ workspace.style.removeProperty("--sidebar-width");
150
+ workspace.style.removeProperty("--request-width");
151
+ sync();
152
+ };
153
+
154
+ const showTip = (handle, visible) => {
155
+ handle.classList?.toggle("is-active", visible);
156
+ const tip = handle.querySelector?.(".size-tip");
157
+ if (tip) tip.hidden = !visible;
158
+ };
159
+
160
+ const move = (clientX) => {
161
+ if (!resizing || !isDesktop()) return;
162
+ const metrics = measurements();
163
+ // Distance from the workspace's inline start: its left edge, or its right edge in RTL.
164
+ const bounds = workspace.getBoundingClientRect();
165
+ const rtl = typeof document !== "undefined" && document.documentElement.dir === "rtl";
166
+ const offset = rtl ? bounds.right - clientX : clientX - bounds.left;
167
+ if (resizing.type === "sidebar") {
168
+ apply(offset, metrics.requestWidth);
169
+ } else {
170
+ apply(metrics.sidebarWidth, offset - metrics.sidebarWidth);
171
+ }
172
+ };
173
+
174
+ const stop = () => {
175
+ if (!resizing) return;
176
+ showTip(resizing.handle, false);
177
+ resizing = null;
178
+ save();
179
+ document.body.classList.remove("layout-resizing");
180
+ document.removeEventListener("pointermove", onPointerMove);
181
+ document.removeEventListener("pointerup", stop);
182
+ document.removeEventListener("pointercancel", stop);
183
+ };
184
+
185
+ const onPointerMove = (event) => {
186
+ event.preventDefault();
187
+ move(event.clientX);
188
+ };
189
+
190
+ const onPointerDown = (event) => {
191
+ if (!isDesktop() || workspace.dataset?.sidebar === "closed" || event.button !== 0) return;
192
+ event.preventDefault();
193
+ const handle = event.currentTarget;
194
+ resizing = { type: handle.dataset.layoutDivider, handle };
195
+ showTip(handle, true);
196
+ document.body.classList.add("layout-resizing");
197
+ document.addEventListener("pointermove", onPointerMove, { passive: false });
198
+ document.addEventListener("pointerup", stop, { once: true });
199
+ document.addEventListener("pointercancel", stop, { once: true });
200
+ };
201
+
202
+ const onKeyDown = (event) => {
203
+ // Chords such as ⌥← belong to the workbench's own shortcuts, not to the divider.
204
+ if (event.altKey || event.metaKey || event.ctrlKey) return;
205
+ if (!isDesktop() || workspace.dataset?.sidebar === "closed") return;
206
+ const handle = event.currentTarget;
207
+ const metrics = measurements();
208
+ const isSidebar = handle.dataset.layoutDivider === "sidebar";
209
+ const minimum = isSidebar ? metrics.sidebarMinimum : metrics.requestMinimum;
210
+ const maximum = isSidebar
211
+ ? metrics.sidebarMaximum
212
+ : metrics.workspaceWidth - metrics.sidebarWidth - metrics.requestMinimum;
213
+ if (
214
+ !["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", "Home", "End", "PageUp", "PageDown"].includes(event.key)
215
+ ) return;
216
+ event.preventDefault();
217
+ const current = isSidebar ? metrics.sidebarWidth : metrics.requestWidth;
218
+ // In right-to-left layouts the left arrow widens the start-side pane.
219
+ const mirrored = document.documentElement.dir === "rtl" ? { ArrowLeft: "ArrowRight", ArrowRight: "ArrowLeft" }[event.key] ?? event.key : event.key;
220
+ const value = keyboardTrackValue(
221
+ current,
222
+ mirrored,
223
+ minimum,
224
+ maximum,
225
+ KEYBOARD_STEP_REM * metrics.rem,
226
+ );
227
+ apply(isSidebar ? value : metrics.sidebarWidth, isSidebar ? metrics.requestWidth : value);
228
+ save();
229
+ showTip(handle, true);
230
+ };
231
+
232
+ const onBlur = event => showTip(event.currentTarget, false);
233
+ handles.forEach((handle) => {
234
+ handle.addEventListener("pointerdown", onPointerDown);
235
+ handle.addEventListener("keydown", onKeyDown);
236
+ handle.addEventListener("blur", onBlur);
237
+ handle.addEventListener("dblclick", reset);
238
+ });
239
+ sync();
240
+
241
+ if (typeof ResizeObserver !== "undefined") {
242
+ resizeObserver = new ResizeObserver(sync);
243
+ resizeObserver.observe(workspace);
244
+ } else {
245
+ window.addEventListener("resize", sync);
246
+ }
247
+
248
+ return {
249
+ refresh: sync,
250
+ destroy() {
251
+ stop();
252
+ resizeObserver?.disconnect();
253
+ if (!resizeObserver) window.removeEventListener("resize", sync);
254
+ handles.forEach((handle) => {
255
+ handle.removeEventListener("pointerdown", onPointerDown);
256
+ handle.removeEventListener("keydown", onKeyDown);
257
+ handle.removeEventListener("blur", onBlur);
258
+ handle.removeEventListener("dblclick", reset);
259
+ });
260
+ },
261
+ };
262
+ }