dsh-artifact-studio 0.7.3 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -11
- package/THIRD_PARTY_NOTICES.md +56 -0
- package/assets/canvas.engine.js +1 -1
- package/client.canvas.js +1 -1
- package/client.doc.js +67 -79
- package/client.js +102 -23
- package/client.sheet.js +218 -0
- package/dist/contract.js +54 -10
- package/dist/host/lib/brief.js +127 -0
- package/dist/host/lib/checks.js +62 -8
- package/dist/host/lib/design-catalog.js +149 -0
- package/dist/host/lib/design-system.js +23 -2
- package/dist/host/lib/export.js +100 -0
- package/dist/host/lib/fsutil.js +41 -0
- package/dist/host/lib/generation.js +36 -0
- package/dist/host/lib/guide.js +130 -0
- package/dist/host/lib/paths.js +2 -1
- package/dist/host/lib/recipes.js +224 -0
- package/dist/host/lib/rpc.js +20 -5
- package/dist/host/lib/service.js +226 -24
- package/dist/host/lib/skill.js +87 -11
- package/dist/host/lib/store.js +107 -25
- package/dist/host/lib/tools.js +354 -52
- package/dist/host/lib/validate.js +24 -0
- package/dist/host/lib/xlsx.js +313 -0
- package/dist/html-tags.js +51 -0
- package/dist/kit.js +224 -0
- package/dist/render.js +10 -3
- package/dist/sheet/formula.js +475 -0
- package/dist/sheet/functions.js +463 -0
- package/dist/sheet/index.js +9 -0
- package/dist/sheet/model.js +628 -0
- package/dist/sheet/operations.js +384 -0
- package/dist/sheet/output.js +170 -0
- package/examples/page-design-system.html +150 -0
- package/package.json +8 -2
|
@@ -125,3 +125,27 @@ export function optionalEdits(fields) {
|
|
|
125
125
|
return { oldText, newText };
|
|
126
126
|
});
|
|
127
127
|
}
|
|
128
|
+
export function optionalBoolean(fields, key) {
|
|
129
|
+
const value = fields[key];
|
|
130
|
+
if (value === undefined)
|
|
131
|
+
return undefined;
|
|
132
|
+
if (typeof value !== 'boolean')
|
|
133
|
+
throw new ArtifactError('artifact/bad-request', key + ' must be a boolean.');
|
|
134
|
+
return value;
|
|
135
|
+
}
|
|
136
|
+
export function designSelection(fields) {
|
|
137
|
+
return { systemId: optionalString(fields, 'systemId'),
|
|
138
|
+
systemScope: optionalEnum(fields, 'systemScope', SCOPES, 'artifact/unknown-scope'),
|
|
139
|
+
revision: optionalInteger(fields, 'systemRevision') };
|
|
140
|
+
}
|
|
141
|
+
export function designSaveOptions(fields) {
|
|
142
|
+
const systemId = optionalString(fields, 'systemId');
|
|
143
|
+
const expectedRevision = optionalInteger(fields, 'expectedRevision');
|
|
144
|
+
const createNew = optionalBoolean(fields, 'createNew');
|
|
145
|
+
const makeDefault = optionalBoolean(fields, 'makeDefault');
|
|
146
|
+
if (systemId === undefined && expectedRevision === undefined && createNew === undefined && makeDefault === undefined)
|
|
147
|
+
return undefined;
|
|
148
|
+
if (expectedRevision === undefined)
|
|
149
|
+
throw new ArtifactError('artifact/bad-request', 'Named system saves require expectedRevision from design_system_list.');
|
|
150
|
+
return { systemId, expectedRevision, createNew, makeDefault };
|
|
151
|
+
}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* XLSX export for sheet artifacts. The output is a minimal but valid OOXML
|
|
3
|
+
* workbook: one worksheet per sheet, inline strings, cell styles, column
|
|
4
|
+
* widths, formulas, and cached formula results. fflate (MIT) writes the ZIP.
|
|
5
|
+
*
|
|
6
|
+
* A formula keeps a cached result so a preview tool shows the same value as
|
|
7
|
+
* the panel, while calcPr asks Excel and LibreOffice to recalculate on load.
|
|
8
|
+
*/
|
|
9
|
+
import { zipSync, strToU8 } from 'fflate';
|
|
10
|
+
import { columnName, evaluateWorkbook, formatNumber, usedBounds } from '../../sheet/index.js';
|
|
11
|
+
const MAIN = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
|
|
12
|
+
const RELATIONSHIPS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
|
|
13
|
+
const PACKAGE_RELATIONSHIPS = 'http://schemas.openxmlformats.org/package/2006/relationships';
|
|
14
|
+
const BUILTIN_NUMBER_FORMATS = {
|
|
15
|
+
integer: 1,
|
|
16
|
+
number: 2,
|
|
17
|
+
percent: 10,
|
|
18
|
+
date: 14,
|
|
19
|
+
currency: 164,
|
|
20
|
+
};
|
|
21
|
+
const CUSTOM_NUMBER_FORMATS = '<numFmts count="1"><numFmt numFmtId="164" formatCode=""$"#,##0.00"/></numFmts>';
|
|
22
|
+
function xmlText(value) {
|
|
23
|
+
return value
|
|
24
|
+
.replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g, '')
|
|
25
|
+
.replace(/&/g, '&')
|
|
26
|
+
.replace(/</g, '<')
|
|
27
|
+
.replace(/>/g, '>');
|
|
28
|
+
}
|
|
29
|
+
function xmlAttribute(value) {
|
|
30
|
+
return xmlText(value).replace(/"/g, '"').replace(/'/g, ''');
|
|
31
|
+
}
|
|
32
|
+
function colorHex(value) {
|
|
33
|
+
const hex = value.replace('#', '').toUpperCase();
|
|
34
|
+
return 'FF' + (hex.length === 3 ? hex.split('').map((part) => part + part).join('') : hex);
|
|
35
|
+
}
|
|
36
|
+
function normaliseStyle(style) {
|
|
37
|
+
if (style === undefined)
|
|
38
|
+
return undefined;
|
|
39
|
+
const font = {
|
|
40
|
+
bold: style.bold === true,
|
|
41
|
+
italic: style.italic === true,
|
|
42
|
+
underline: style.underline === true,
|
|
43
|
+
strike: style.strike === true,
|
|
44
|
+
color: style.color ?? null,
|
|
45
|
+
};
|
|
46
|
+
const spec = {
|
|
47
|
+
font,
|
|
48
|
+
fill: style.background ?? null,
|
|
49
|
+
align: style.align ?? null,
|
|
50
|
+
numberFormat: style.numberFormat === undefined || style.numberFormat === 'general' ? null : style.numberFormat,
|
|
51
|
+
};
|
|
52
|
+
const plain = !font.bold && !font.italic && !font.underline && !font.strike && font.color === null
|
|
53
|
+
&& spec.fill === null && spec.align === null && spec.numberFormat === null;
|
|
54
|
+
return plain ? undefined : spec;
|
|
55
|
+
}
|
|
56
|
+
function fontKey(font) {
|
|
57
|
+
return (font.bold ? 'b' : '') + (font.italic ? 'i' : '') + (font.underline ? 'u' : '') + (font.strike ? 's' : '') + (font.color === null ? '' : font.color);
|
|
58
|
+
}
|
|
59
|
+
function styleKey(style) {
|
|
60
|
+
return fontKey(style.font) + '|' + (style.fill ?? '') + '|' + (style.align ?? '') + '|' + (style.numberFormat ?? '');
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Build the shared font, fill, and xf tables for one workbook.
|
|
64
|
+
* @param workbook the workbook.
|
|
65
|
+
* @returns the tables.
|
|
66
|
+
*/
|
|
67
|
+
function styleTables(workbook) {
|
|
68
|
+
const fonts = [{ bold: false, italic: false, underline: false, strike: false, color: null }];
|
|
69
|
+
const fills = [];
|
|
70
|
+
const styles = [];
|
|
71
|
+
const fontSeen = new Set(['']);
|
|
72
|
+
const fillSeen = new Set();
|
|
73
|
+
const styleSeen = new Set();
|
|
74
|
+
for (const sheet of workbook.sheets) {
|
|
75
|
+
for (const cell of Object.values(sheet.cells)) {
|
|
76
|
+
const spec = normaliseStyle(cell.style);
|
|
77
|
+
if (spec === undefined)
|
|
78
|
+
continue;
|
|
79
|
+
if (!fontSeen.has(fontKey(spec.font))) {
|
|
80
|
+
fontSeen.add(fontKey(spec.font));
|
|
81
|
+
fonts.push(spec.font);
|
|
82
|
+
}
|
|
83
|
+
if (spec.fill !== null && !fillSeen.has(spec.fill)) {
|
|
84
|
+
fillSeen.add(spec.fill);
|
|
85
|
+
fills.push(spec.fill);
|
|
86
|
+
}
|
|
87
|
+
if (!styleSeen.has(styleKey(spec))) {
|
|
88
|
+
styleSeen.add(styleKey(spec));
|
|
89
|
+
styles.push(spec);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return { fonts, fills, styles };
|
|
94
|
+
}
|
|
95
|
+
function fontIdOf(tables, spec) {
|
|
96
|
+
const index = tables.fonts.findIndex((font) => fontKey(font) === fontKey(spec.font));
|
|
97
|
+
return index < 0 ? 0 : index;
|
|
98
|
+
}
|
|
99
|
+
function fillIdOf(tables, spec) {
|
|
100
|
+
if (spec.fill === null)
|
|
101
|
+
return 0;
|
|
102
|
+
const index = tables.fills.indexOf(spec.fill);
|
|
103
|
+
return index < 0 ? 0 : index + 2;
|
|
104
|
+
}
|
|
105
|
+
function xfIdOf(tables, spec) {
|
|
106
|
+
if (spec === undefined)
|
|
107
|
+
return 0;
|
|
108
|
+
const index = tables.styles.findIndex((style) => styleKey(style) === styleKey(spec));
|
|
109
|
+
return index < 0 ? 0 : index + 1;
|
|
110
|
+
}
|
|
111
|
+
function fontXml(font) {
|
|
112
|
+
const parts = ['<font>'];
|
|
113
|
+
if (font.bold)
|
|
114
|
+
parts.push('<b/>');
|
|
115
|
+
if (font.italic)
|
|
116
|
+
parts.push('<i/>');
|
|
117
|
+
if (font.strike)
|
|
118
|
+
parts.push('<strike/>');
|
|
119
|
+
if (font.underline)
|
|
120
|
+
parts.push('<u/>');
|
|
121
|
+
parts.push('<sz val="11"/>');
|
|
122
|
+
if (font.color !== null)
|
|
123
|
+
parts.push('<color rgb="' + colorHex(font.color) + '"/>');
|
|
124
|
+
parts.push('<name val="Calibri"/><family val="2"/>');
|
|
125
|
+
parts.push('</font>');
|
|
126
|
+
return parts.join('');
|
|
127
|
+
}
|
|
128
|
+
function fillXml(color) {
|
|
129
|
+
return '<fill><patternFill patternType="solid"><fgColor rgb="' + colorHex(color) + '"/><bgColor indexed="64"/></patternFill></fill>';
|
|
130
|
+
}
|
|
131
|
+
function stylesXml(tables) {
|
|
132
|
+
const fonts = tables.fonts.map(fontXml).join('');
|
|
133
|
+
const fills = '<fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill>' + tables.fills.map(fillXml).join('');
|
|
134
|
+
const xfs = ['<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>'];
|
|
135
|
+
for (const style of tables.styles) {
|
|
136
|
+
const numFmtId = style.numberFormat === null ? 0 : BUILTIN_NUMBER_FORMATS[style.numberFormat] ?? 0;
|
|
137
|
+
const attributes = [
|
|
138
|
+
'numFmtId="' + String(numFmtId) + '"',
|
|
139
|
+
'fontId="' + String(fontIdOf(tables, style)) + '"',
|
|
140
|
+
'fillId="' + String(fillIdOf(tables, style)) + '"',
|
|
141
|
+
'borderId="0"',
|
|
142
|
+
'xfId="0"',
|
|
143
|
+
];
|
|
144
|
+
if (numFmtId !== 0)
|
|
145
|
+
attributes.push('applyNumberFormat="1"');
|
|
146
|
+
if (fontIdOf(tables, style) !== 0)
|
|
147
|
+
attributes.push('applyFont="1"');
|
|
148
|
+
if (fillIdOf(tables, style) !== 0)
|
|
149
|
+
attributes.push('applyFill="1"');
|
|
150
|
+
if (style.align !== null)
|
|
151
|
+
attributes.push('applyAlignment="1"');
|
|
152
|
+
const alignment = style.align === null ? '' : '<alignment horizontal="' + style.align + '"/>';
|
|
153
|
+
xfs.push('<xf ' + attributes.join(' ') + '>' + alignment + '</xf>');
|
|
154
|
+
}
|
|
155
|
+
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
|
|
156
|
+
'<styleSheet xmlns="' + MAIN + '">' + CUSTOM_NUMBER_FORMATS +
|
|
157
|
+
'<fonts count="' + String(tables.fonts.length) + '">' + fonts + '</fonts>' +
|
|
158
|
+
'<fills count="' + String(tables.fills.length + 2) + '">' + fills + '</fills>' +
|
|
159
|
+
'<borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders>' +
|
|
160
|
+
'<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>' +
|
|
161
|
+
'<cellXfs count="' + String(xfs.length) + '">' + xfs.join('') + '</cellXfs>' +
|
|
162
|
+
'<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles>' +
|
|
163
|
+
'</styleSheet>';
|
|
164
|
+
}
|
|
165
|
+
function numberXml(value) {
|
|
166
|
+
return Number.isFinite(value) ? String(value) : '0';
|
|
167
|
+
}
|
|
168
|
+
function cellXml(ref, cell, result, styleIndex) {
|
|
169
|
+
const styleAttribute = styleIndex === 0 ? '' : ' s="' + String(styleIndex) + '"';
|
|
170
|
+
if (cell.formula !== undefined) {
|
|
171
|
+
const formula = cell.formula.startsWith('=') ? cell.formula.slice(1) : cell.formula;
|
|
172
|
+
if (result?.error !== undefined)
|
|
173
|
+
return '<c r="' + ref + '"' + styleAttribute + ' t="e"><f>' + xmlText(formula) + '</f><v>' + xmlText(result.error) + '</v></c>';
|
|
174
|
+
if (result === undefined || result.value === null)
|
|
175
|
+
return '<c r="' + ref + '"' + styleAttribute + '><f>' + xmlText(formula) + '</f></c>';
|
|
176
|
+
if (typeof result.value === 'number')
|
|
177
|
+
return '<c r="' + ref + '"' + styleAttribute + '><f>' + xmlText(formula) + '</f><v>' + numberXml(result.value) + '</v></c>';
|
|
178
|
+
if (typeof result.value === 'boolean')
|
|
179
|
+
return '<c r="' + ref + '"' + styleAttribute + ' t="b"><f>' + xmlText(formula) + '</f><v>' + (result.value ? '1' : '0') + '</v></c>';
|
|
180
|
+
return '<c r="' + ref + '"' + styleAttribute + ' t="str"><f>' + xmlText(formula) + '</f><v>' + xmlText(result.value) + '</v></c>';
|
|
181
|
+
}
|
|
182
|
+
if (cell.value === undefined)
|
|
183
|
+
return '<c r="' + ref + '"' + styleAttribute + '/>';
|
|
184
|
+
if (typeof cell.value === 'number')
|
|
185
|
+
return '<c r="' + ref + '"' + styleAttribute + '><v>' + numberXml(cell.value) + '</v></c>';
|
|
186
|
+
if (typeof cell.value === 'boolean')
|
|
187
|
+
return '<c r="' + ref + '"' + styleAttribute + ' t="b"><v>' + (cell.value ? '1' : '0') + '</v></c>';
|
|
188
|
+
return '<c r="' + ref + '"' + styleAttribute + ' t="inlineStr"><is><t xml:space="preserve">' + xmlText(cell.value) + '</t></is></c>';
|
|
189
|
+
}
|
|
190
|
+
function columnsXml(widths) {
|
|
191
|
+
if (widths === undefined)
|
|
192
|
+
return '';
|
|
193
|
+
const columns = Object.entries(widths).map(([name, width]) => {
|
|
194
|
+
let column = 0;
|
|
195
|
+
for (const letter of name)
|
|
196
|
+
column = column * 26 + (letter.charCodeAt(0) - 64);
|
|
197
|
+
const excelWidth = Math.round((width / 7) * 100) / 100;
|
|
198
|
+
return '<col min="' + String(column) + '" max="' + String(column) + '" width="' + String(excelWidth) + '" customWidth="1"/>';
|
|
199
|
+
});
|
|
200
|
+
return columns.length === 0 ? '' : '<cols>' + columns.join('') + '</cols>';
|
|
201
|
+
}
|
|
202
|
+
function worksheetXml(sheetName, workbook, evaluated, tables) {
|
|
203
|
+
const sheet = workbook.sheets.find((item) => item.name === sheetName);
|
|
204
|
+
if (sheet === undefined)
|
|
205
|
+
return '';
|
|
206
|
+
const bounds = usedBounds(sheet);
|
|
207
|
+
const results = evaluated.results[sheet.name] ?? {};
|
|
208
|
+
const rowsXml = [];
|
|
209
|
+
for (let row = 1; row <= Math.max(1, bounds.rows); row += 1) {
|
|
210
|
+
const cells = [];
|
|
211
|
+
for (let column = 0; column < Math.max(1, bounds.columns); column += 1) {
|
|
212
|
+
const ref = columnName(column) + String(row);
|
|
213
|
+
const cell = sheet.cells[ref];
|
|
214
|
+
const result = results[ref];
|
|
215
|
+
if (cell === undefined && result === undefined)
|
|
216
|
+
continue;
|
|
217
|
+
const styleIndex = xfIdOf(tables, cell === undefined ? undefined : normaliseStyle(cell.style));
|
|
218
|
+
cells.push(cellXml(ref, cell ?? {}, result, styleIndex));
|
|
219
|
+
}
|
|
220
|
+
if (cells.length > 0)
|
|
221
|
+
rowsXml.push('<row r="' + String(row) + '">' + cells.join('') + '</row>');
|
|
222
|
+
}
|
|
223
|
+
const dimension = bounds.rows === 0 || bounds.columns === 0
|
|
224
|
+
? 'A1'
|
|
225
|
+
: 'A1:' + columnName(Math.max(0, bounds.columns - 1)) + String(Math.max(1, bounds.rows));
|
|
226
|
+
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
|
|
227
|
+
'<worksheet xmlns="' + MAIN + '" xmlns:r="' + RELATIONSHIPS + '">' +
|
|
228
|
+
'<dimension ref="' + dimension + '"/>' +
|
|
229
|
+
'<sheetViews><sheetView workbookViewId="0"/></sheetViews>' +
|
|
230
|
+
'<sheetFormatPr defaultRowHeight="15"/>' +
|
|
231
|
+
columnsXml(sheet.columnWidths) +
|
|
232
|
+
'<sheetData>' + rowsXml.join('') + '</sheetData>' +
|
|
233
|
+
'<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>' +
|
|
234
|
+
'</worksheet>';
|
|
235
|
+
}
|
|
236
|
+
function workbookXml(workbook) {
|
|
237
|
+
const sheets = workbook.sheets
|
|
238
|
+
.map((sheet, index) => '<sheet name="' + xmlAttribute(sheet.name) + '" sheetId="' + String(index + 1) + '" r:id="rId' + String(index + 1) + '"/>')
|
|
239
|
+
.join('');
|
|
240
|
+
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
|
|
241
|
+
'<workbook xmlns="' + MAIN + '" xmlns:r="' + RELATIONSHIPS + '">' +
|
|
242
|
+
'<sheets>' + sheets + '</sheets>' +
|
|
243
|
+
'<calcPr calcId="171027" fullCalcOnLoad="1"/>' +
|
|
244
|
+
'</workbook>';
|
|
245
|
+
}
|
|
246
|
+
function workbookRels(workbook) {
|
|
247
|
+
const sheets = workbook.sheets
|
|
248
|
+
.map((_sheet, index) => '<Relationship Id="rId' + String(index + 1) + '" Type="' + RELATIONSHIPS + '/worksheet" Target="worksheets/sheet' + String(index + 1) + '.xml"/>')
|
|
249
|
+
.join('');
|
|
250
|
+
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
|
|
251
|
+
'<Relationships xmlns="' + PACKAGE_RELATIONSHIPS + '">' + sheets +
|
|
252
|
+
'<Relationship Id="rId' + String(workbook.sheets.length + 1) + '" Type="' + RELATIONSHIPS + '/styles" Target="styles.xml"/>' +
|
|
253
|
+
'</Relationships>';
|
|
254
|
+
}
|
|
255
|
+
function contentTypes(workbook) {
|
|
256
|
+
const sheets = workbook.sheets
|
|
257
|
+
.map((_sheet, index) => '<Override PartName="/xl/worksheets/sheet' + String(index + 1) + '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>')
|
|
258
|
+
.join('');
|
|
259
|
+
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
|
|
260
|
+
'<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">' +
|
|
261
|
+
'<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>' +
|
|
262
|
+
'<Default Extension="xml" ContentType="application/xml"/>' +
|
|
263
|
+
'<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>' +
|
|
264
|
+
'<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>' +
|
|
265
|
+
sheets +
|
|
266
|
+
'</Types>';
|
|
267
|
+
}
|
|
268
|
+
function packageRels() {
|
|
269
|
+
return '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
|
|
270
|
+
'<Relationships xmlns="' + PACKAGE_RELATIONSHIPS + '">' +
|
|
271
|
+
'<Relationship Id="rId1" Type="' + RELATIONSHIPS + '/officeDocument" Target="xl/workbook.xml"/>' +
|
|
272
|
+
'</Relationships>';
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Write a workbook as an XLSX package.
|
|
276
|
+
* @param workbook the validated workbook.
|
|
277
|
+
* @returns the file bytes.
|
|
278
|
+
*/
|
|
279
|
+
export function workbookToXlsx(workbook) {
|
|
280
|
+
const evaluated = evaluateWorkbook(workbook);
|
|
281
|
+
const tables = styleTables(workbook);
|
|
282
|
+
const files = {
|
|
283
|
+
'[Content_Types].xml': strToU8(contentTypes(workbook)),
|
|
284
|
+
'_rels/.rels': strToU8(packageRels()),
|
|
285
|
+
'xl/workbook.xml': strToU8(workbookXml(workbook)),
|
|
286
|
+
'xl/_rels/workbook.xml.rels': strToU8(workbookRels(workbook)),
|
|
287
|
+
'xl/styles.xml': strToU8(stylesXml(tables)),
|
|
288
|
+
};
|
|
289
|
+
workbook.sheets.forEach((sheet, index) => {
|
|
290
|
+
files['xl/worksheets/sheet' + String(index + 1) + '.xml'] = strToU8(worksheetXml(sheet.name, workbook, evaluated, tables));
|
|
291
|
+
});
|
|
292
|
+
return Buffer.from(zipSync(files, { level: 6 }));
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* The display text for one evaluated value, used by CSV and print copies.
|
|
296
|
+
* @param result the evaluated result.
|
|
297
|
+
* @param style the cell style.
|
|
298
|
+
* @returns the text.
|
|
299
|
+
*/
|
|
300
|
+
export function resultText(result, style) {
|
|
301
|
+
if (result === undefined)
|
|
302
|
+
return '';
|
|
303
|
+
if (result.error !== undefined)
|
|
304
|
+
return result.error;
|
|
305
|
+
const value = result.value;
|
|
306
|
+
if (value === null)
|
|
307
|
+
return '';
|
|
308
|
+
if (typeof value === 'number')
|
|
309
|
+
return formatNumber(value, style?.numberFormat);
|
|
310
|
+
if (typeof value === 'boolean')
|
|
311
|
+
return value ? 'TRUE' : 'FALSE';
|
|
312
|
+
return value;
|
|
313
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Consume each complete tag once, including quoted attributes. Skip comments
|
|
3
|
+
* and inert/raw-text contents. This is not a sanitizer or an HTML5 parser.
|
|
4
|
+
*/
|
|
5
|
+
export function* openingTags(content) {
|
|
6
|
+
const lower = content.toLowerCase();
|
|
7
|
+
const raw = new Set(['script', 'style', 'textarea', 'title', 'template', 'noscript']);
|
|
8
|
+
let cursor = 0;
|
|
9
|
+
while (cursor < content.length) {
|
|
10
|
+
const start = content.indexOf('<', cursor);
|
|
11
|
+
if (start < 0)
|
|
12
|
+
return;
|
|
13
|
+
if (content.startsWith('<!--', start)) {
|
|
14
|
+
const end = content.indexOf('-->', start + 4);
|
|
15
|
+
if (end < 0)
|
|
16
|
+
return;
|
|
17
|
+
cursor = end + 3;
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
let end = start + 1;
|
|
21
|
+
let quote = '';
|
|
22
|
+
for (; end < content.length; end++) {
|
|
23
|
+
const char = content[end];
|
|
24
|
+
if (quote !== '') {
|
|
25
|
+
if (char === quote)
|
|
26
|
+
quote = '';
|
|
27
|
+
}
|
|
28
|
+
else if (char === '"' || char === "'")
|
|
29
|
+
quote = char;
|
|
30
|
+
else if (char === '>')
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
if (end === content.length)
|
|
34
|
+
return;
|
|
35
|
+
cursor = end + 1;
|
|
36
|
+
const text = content.slice(start, cursor);
|
|
37
|
+
const name = /^<([a-z][a-z0-9:-]*)(?=\s|\/?>)/i.exec(text)?.[1]?.toLowerCase();
|
|
38
|
+
if (name === undefined)
|
|
39
|
+
continue;
|
|
40
|
+
yield { name, text };
|
|
41
|
+
if (raw.has(name)) {
|
|
42
|
+
const close = lower.indexOf('</' + name, cursor);
|
|
43
|
+
if (close < 0)
|
|
44
|
+
return;
|
|
45
|
+
const closeEnd = content.indexOf('>', close);
|
|
46
|
+
if (closeEnd < 0)
|
|
47
|
+
return;
|
|
48
|
+
cursor = closeEnd + 1;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
package/dist/kit.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The artifact kit: versioned component CSS and behavior that the renderer
|
|
3
|
+
* injects when a page opts in with <meta name="artifact-kit" content="1">.
|
|
4
|
+
*
|
|
5
|
+
* A published kit version keeps its component definitions. This does not pin
|
|
6
|
+
* the resolved tokens, browser fonts, or authored overrides; those can still
|
|
7
|
+
* change a saved page's appearance.
|
|
8
|
+
*/
|
|
9
|
+
import { openingTags } from './html-tags.js';
|
|
10
|
+
const CSS_V1 = [
|
|
11
|
+
'*, *::before, *::after { box-sizing: border-box; }',
|
|
12
|
+
'[hidden] { display: none !important; }',
|
|
13
|
+
'html { background: var(--artifact-color-background); -webkit-text-size-adjust: 100%; }',
|
|
14
|
+
'body { margin: 0; color: var(--artifact-color-text); font: var(--artifact-font-size-2)/1.6 var(--artifact-font-family); overflow-wrap: anywhere; }',
|
|
15
|
+
'::selection { background: color-mix(in srgb, var(--artifact-color-action) 26%, transparent); }',
|
|
16
|
+
':focus-visible { outline: 2px solid var(--artifact-color-focus); outline-offset: 2px; }',
|
|
17
|
+
':where(h1, h2, h3, h4):not(:where(.ak-demo, .ak-demo *)) { margin: 0; font-family: var(--artifact-font-display); font-weight: 600; line-height: 1.2; text-wrap: balance; }',
|
|
18
|
+
':where(h1):not(:where(.ak-demo, .ak-demo *)) { font-size: clamp(1.9rem, 5vw, 2.6rem); letter-spacing: -0.02em; }',
|
|
19
|
+
':where(h2):not(:where(.ak-demo, .ak-demo *)) { font-size: clamp(1.35rem, 3.2vw, 1.75rem); letter-spacing: -0.01em; }',
|
|
20
|
+
':where(h3):not(:where(.ak-demo, .ak-demo *)) { font-size: var(--artifact-font-size-3); }',
|
|
21
|
+
':where(h4):not(:where(.ak-demo, .ak-demo *)) { font-size: var(--artifact-font-size-2); }',
|
|
22
|
+
':where(p, ul, ol, dl, figure, blockquote):not(:where(.ak-demo, .ak-demo *)) { margin: 0; }',
|
|
23
|
+
':where(a):not(:where(.ak-demo, .ak-demo *)) { color: var(--artifact-color-primary); text-underline-offset: 0.2em; }',
|
|
24
|
+
':where(code, kbd, samp, pre):not(:where(.ak-demo, .ak-demo *)) { font-family: var(--artifact-font-mono); font-size: 0.92em; }',
|
|
25
|
+
':where(:not(pre) > code):not(:where(.ak-demo, .ak-demo *)) { padding: 0.1em 0.35em; border-radius: var(--artifact-radius-sm); background: color-mix(in srgb, var(--artifact-color-text) 7%, transparent); }',
|
|
26
|
+
'button, input, select, textarea { font: inherit; color: inherit; }',
|
|
27
|
+
'img { max-width: 100%; height: auto; }',
|
|
28
|
+
'.ak-page { display: grid; gap: var(--artifact-space-6); width: min(100% - 2 * var(--artifact-space-4), 72rem); margin-inline: auto; padding-block: var(--artifact-space-5) var(--artifact-space-6); }',
|
|
29
|
+
'.ak-page--narrow { width: min(100% - 2 * var(--artifact-space-4), 48rem); }',
|
|
30
|
+
'.ak-page > * { min-width: 0; }',
|
|
31
|
+
'.ak-header { display: grid; gap: var(--artifact-space-3); padding-bottom: var(--artifact-space-5); border-bottom: 1px solid var(--artifact-color-border); }',
|
|
32
|
+
'.ak-eyebrow { color: var(--artifact-color-muted); font-size: var(--artifact-font-size-1); font-weight: 600; letter-spacing: 0.01em; }',
|
|
33
|
+
'.ak-lede { max-width: 60ch; color: var(--artifact-color-muted); font-size: calc((var(--artifact-font-size-2) + var(--artifact-font-size-3)) / 2); line-height: 1.5; }',
|
|
34
|
+
'.ak-meta { display: flex; flex-wrap: wrap; align-items: center; gap: var(--artifact-space-1) var(--artifact-space-3); padding: 0; list-style: none; color: var(--artifact-color-muted); font-size: var(--artifact-font-size-1); }',
|
|
35
|
+
'.ak-section { display: grid; gap: var(--artifact-space-4); scroll-margin-top: var(--artifact-space-4); }',
|
|
36
|
+
'.ak-section__head { display: grid; gap: var(--artifact-space-1); }',
|
|
37
|
+
'.ak-section__head p { max-width: 68ch; color: var(--artifact-color-muted); }',
|
|
38
|
+
'.ak-prose { display: grid; gap: var(--artifact-space-3); }',
|
|
39
|
+
'.ak-prose > *, .ak-section > p, .ak-section > ul, .ak-section > ol { max-width: 68ch; }',
|
|
40
|
+
'.ak-prose ul, .ak-prose ol, .ak-section > ul, .ak-section > ol { display: grid; gap: var(--artifact-space-1); padding-left: 1.3em; }',
|
|
41
|
+
'.ak-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr)); gap: var(--artifact-space-4); }',
|
|
42
|
+
'.ak-split { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 20rem), 1fr)); gap: var(--artifact-space-4); align-items: start; }',
|
|
43
|
+
'.ak-card { display: grid; align-content: start; gap: var(--artifact-space-2); padding: var(--artifact-space-4); border: 1px solid var(--artifact-color-border); border-radius: var(--artifact-radius-lg); background: var(--artifact-color-surface); }',
|
|
44
|
+
'.ak-muted { color: var(--artifact-color-muted); }',
|
|
45
|
+
'.ak-small { font-size: var(--artifact-font-size-1); }',
|
|
46
|
+
'.ak-num { font-family: var(--artifact-font-mono); font-variant-numeric: tabular-nums; }',
|
|
47
|
+
'.ak-callout { --ak-tone: var(--artifact-color-accent); display: grid; gap: var(--artifact-space-1); padding: var(--artifact-space-3) var(--artifact-space-4); border-left: 3px solid var(--ak-tone); border-radius: 0 var(--artifact-radius-md) var(--artifact-radius-md) 0; background: var(--artifact-color-raised); }',
|
|
48
|
+
'.ak-callout__title { font-weight: 650; }',
|
|
49
|
+
'.ak-callout--info { --ak-tone: var(--artifact-color-focus); }',
|
|
50
|
+
'.ak-callout--success { --ak-tone: var(--artifact-color-success); }',
|
|
51
|
+
'.ak-callout--warning { --ak-tone: var(--artifact-color-warning); }',
|
|
52
|
+
'.ak-callout--danger { --ak-tone: var(--artifact-color-danger); }',
|
|
53
|
+
'.ak-badge { --ak-tone: var(--artifact-color-muted); display: inline-flex; align-items: center; gap: 0.4em; padding: 0.1em 0.6em; border: 1px solid color-mix(in srgb, var(--ak-tone) 45%, transparent); border-radius: 999px; color: var(--ak-tone); font-size: var(--artifact-font-size-1); font-weight: 600; line-height: 1.5; white-space: nowrap; }',
|
|
54
|
+
'.ak-badge::before { content: ""; flex: none; width: 0.45em; height: 0.45em; border-radius: 50%; background: currentColor; }',
|
|
55
|
+
'.ak-badge--plain::before { display: none; }',
|
|
56
|
+
'.ak-badge--info { --ak-tone: var(--artifact-color-focus); }',
|
|
57
|
+
'.ak-badge--success { --ak-tone: var(--artifact-color-success); }',
|
|
58
|
+
'.ak-badge--warning { --ak-tone: var(--artifact-color-warning); }',
|
|
59
|
+
'.ak-badge--danger { --ak-tone: var(--artifact-color-danger); }',
|
|
60
|
+
'.ak-facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr)); gap: var(--artifact-space-3); }',
|
|
61
|
+
'.ak-facts > div { display: grid; gap: 2px; padding-top: var(--artifact-space-2); border-top: 1px solid var(--artifact-color-border); }',
|
|
62
|
+
'.ak-facts dt { color: var(--artifact-color-muted); font-size: var(--artifact-font-size-1); }',
|
|
63
|
+
'.ak-facts dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }',
|
|
64
|
+
'.ak-table { max-width: 100%; overflow-x: auto; border: 1px solid var(--artifact-color-border); border-radius: var(--artifact-radius-md); background: var(--artifact-color-surface); }',
|
|
65
|
+
'.ak-table table { width: 100%; border-collapse: collapse; font-size: var(--artifact-font-size-1); font-variant-numeric: tabular-nums; }',
|
|
66
|
+
'.ak-table caption { padding: var(--artifact-space-3) var(--artifact-space-3) var(--artifact-space-2); color: var(--artifact-color-muted); text-align: left; }',
|
|
67
|
+
'.ak-table caption strong { color: var(--artifact-color-text); }',
|
|
68
|
+
'.ak-table th, .ak-table td { padding: var(--artifact-space-2) var(--artifact-space-3); border-top: 1px solid var(--artifact-color-border); text-align: left; vertical-align: top; }',
|
|
69
|
+
'.ak-table thead th { background: var(--artifact-color-raised); color: var(--artifact-color-muted); font-weight: 600; }',
|
|
70
|
+
'.ak-table tbody th { font-weight: 600; }',
|
|
71
|
+
'@media (max-width: 640px) {',
|
|
72
|
+
' .ak-table--stack table, .ak-table--stack caption, .ak-table--stack tbody, .ak-table--stack tr, .ak-table--stack th, .ak-table--stack td { display: block; }',
|
|
73
|
+
' .ak-table--stack thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }',
|
|
74
|
+
' .ak-table--stack tr { padding: var(--artifact-space-2) 0; border-top: 1px solid var(--artifact-color-border); }',
|
|
75
|
+
' .ak-table--stack th, .ak-table--stack td { padding: var(--artifact-space-1) var(--artifact-space-3); border: 0; }',
|
|
76
|
+
' .ak-table--stack td[data-label]::before { content: attr(data-label); display: block; color: var(--artifact-color-muted); font-size: var(--artifact-font-size-0); font-weight: 600; }',
|
|
77
|
+
'}',
|
|
78
|
+
'.ak-code { display: grid; overflow: hidden; border: 1px solid var(--artifact-color-border); border-radius: var(--artifact-radius-md); background: var(--artifact-color-surface); }',
|
|
79
|
+
'.ak-code figcaption { padding: var(--artifact-space-1) var(--artifact-space-3); border-bottom: 1px solid var(--artifact-color-border); color: var(--artifact-color-muted); font: 600 var(--artifact-font-size-0)/1.6 var(--artifact-font-mono); }',
|
|
80
|
+
'.ak-code pre { margin: 0; padding: var(--artifact-space-3); overflow-x: auto; font-size: var(--artifact-font-size-1); line-height: 1.55; tab-size: 2; }',
|
|
81
|
+
'.ak-steps { display: grid; gap: var(--artifact-space-3); padding: 0; list-style: none; counter-reset: ak-step; }',
|
|
82
|
+
'.ak-steps > li { position: relative; display: grid; gap: var(--artifact-space-1); min-height: 2rem; padding-left: 2.75rem; counter-increment: ak-step; }',
|
|
83
|
+
'.ak-steps > li::before { content: counter(ak-step); position: absolute; top: 0; left: 0; display: grid; place-items: center; width: 2rem; height: 2rem; border: 1px solid var(--artifact-color-border); border-radius: 50%; background: var(--artifact-color-raised); font: 600 var(--artifact-font-size-1)/1 var(--artifact-font-mono); }',
|
|
84
|
+
'.ak-decision { display: grid; gap: var(--artifact-space-2); padding: var(--artifact-space-4); border: 1px solid var(--artifact-color-border); border-radius: var(--artifact-radius-lg); background: var(--artifact-color-surface); }',
|
|
85
|
+
'.ak-decision__pick { padding: var(--artifact-space-2) var(--artifact-space-3); border-radius: var(--artifact-radius-md); background: color-mix(in srgb, var(--artifact-color-success) 10%, var(--artifact-color-surface)); }',
|
|
86
|
+
'.ak-quote { display: grid; gap: var(--artifact-space-1); max-width: 68ch; padding-left: var(--artifact-space-4); border-left: 3px solid var(--artifact-color-border); }',
|
|
87
|
+
'.ak-quote p { font-family: var(--artifact-font-display); font-size: calc((var(--artifact-font-size-2) + var(--artifact-font-size-3)) / 2); }',
|
|
88
|
+
'.ak-quote cite { color: var(--artifact-color-muted); font-size: var(--artifact-font-size-1); font-style: normal; }',
|
|
89
|
+
'.ak-switch { display: grid; gap: var(--artifact-space-3); }',
|
|
90
|
+
'.ak-switch > * { min-width: 0; }',
|
|
91
|
+
'.ak-switch__bar { display: inline-flex; flex-wrap: wrap; justify-self: start; gap: 2px; padding: 2px; border: 1px solid var(--artifact-color-border); border-radius: var(--artifact-radius-md); background: var(--artifact-color-raised); }',
|
|
92
|
+
'.ak-switch__bar button { min-height: 36px; padding: 0 var(--artifact-space-3); border: 0; border-radius: max(0px, calc(var(--artifact-radius-md) - 2px)); background: transparent; color: var(--artifact-color-muted); font-size: var(--artifact-font-size-1); font-weight: 600; cursor: pointer; }',
|
|
93
|
+
'.ak-switch__bar button[aria-pressed="true"] { background: var(--artifact-color-surface); color: var(--artifact-color-text); box-shadow: 0 0 0 1px var(--artifact-color-control); text-decoration: underline; text-underline-offset: 0.2em; }',
|
|
94
|
+
'.ak-compare { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); gap: var(--artifact-space-4); }',
|
|
95
|
+
'.ak-compare > * { display: grid; align-content: start; gap: var(--artifact-space-2); min-width: 0; }',
|
|
96
|
+
'.ak-compare__label { color: var(--artifact-color-muted); font-size: var(--artifact-font-size-1); font-weight: 600; }',
|
|
97
|
+
'.ak-frame { display: grid; overflow: hidden; border: 1px solid var(--artifact-color-border); border-radius: var(--artifact-radius-lg); background: var(--artifact-color-surface); box-shadow: 0 1px 3px color-mix(in srgb, var(--artifact-color-text) 10%, transparent); }',
|
|
98
|
+
'.ak-frame__bar { display: flex; align-items: center; gap: var(--artifact-space-2); min-height: 34px; padding: 0 var(--artifact-space-3); overflow: hidden; border-bottom: 1px solid var(--artifact-color-border); background: var(--artifact-color-raised); color: var(--artifact-color-muted); font: var(--artifact-font-size-0)/1.4 var(--artifact-font-mono); white-space: nowrap; text-overflow: ellipsis; }',
|
|
99
|
+
'.ak-frame__bar::before { content: ""; flex: none; width: 34px; height: 8px; background: radial-gradient(circle, var(--artifact-color-control) 3px, transparent 3.5px) 0 50% / 13px 8px repeat-x; opacity: 0.55; }',
|
|
100
|
+
'.ak-frame__body { min-width: 0; overflow: auto; }',
|
|
101
|
+
'.ak-frame__caption { padding: var(--artifact-space-2) var(--artifact-space-3); border-top: 1px solid var(--artifact-color-border); color: var(--artifact-color-muted); font-size: var(--artifact-font-size-1); }',
|
|
102
|
+
'.ak-demo { color: var(--demo-text, var(--artifact-color-text)); background: var(--demo-bg, transparent); font-family: var(--demo-font, var(--artifact-font-family)); }',
|
|
103
|
+
'.ak-demo { font-size: var(--demo-font-size, 16px); line-height: var(--demo-line-height, 1.5); }',
|
|
104
|
+
':where(.ak-demo h1, .ak-demo h2, .ak-demo h3, .ak-demo h4, .ak-demo a, .ak-demo code, .ak-demo pre) { font: inherit; color: inherit; letter-spacing: normal; }',
|
|
105
|
+
'.ak-button { display: inline-flex; align-items: center; justify-content: center; gap: var(--artifact-space-1); min-height: 40px; padding: 0 var(--artifact-space-3); border: 1px solid var(--artifact-color-control); border-radius: var(--artifact-radius-sm); background: var(--artifact-color-surface); color: var(--artifact-color-text); font-size: var(--artifact-font-size-1); font-weight: 600; cursor: pointer; }',
|
|
106
|
+
'.ak-button:hover:not(:disabled) { border-color: var(--artifact-color-text); }',
|
|
107
|
+
'.ak-button--primary { border-color: var(--artifact-color-action); background: var(--artifact-color-action); color: var(--artifact-color-action-text); }',
|
|
108
|
+
'.ak-button--primary:hover:not(:disabled) { border-color: var(--artifact-color-action); background: color-mix(in srgb, var(--artifact-color-action) 88%, var(--artifact-color-text)); }',
|
|
109
|
+
'.ak-button:disabled { opacity: 0.5; cursor: not-allowed; }',
|
|
110
|
+
'.ak-actions { display: flex; flex-wrap: wrap; gap: var(--artifact-space-2); }',
|
|
111
|
+
'.ak-field { display: grid; gap: var(--artifact-space-1); font-size: var(--artifact-font-size-1); font-weight: 600; }',
|
|
112
|
+
'.ak-input { width: 100%; min-width: 0; min-height: 40px; padding: var(--artifact-space-1) var(--artifact-space-2); border: 1px solid var(--artifact-color-control); border-radius: var(--artifact-radius-sm); background: var(--artifact-color-surface); font-weight: 400; }',
|
|
113
|
+
'.ak-input::placeholder { color: var(--artifact-color-muted); opacity: 1; }',
|
|
114
|
+
'.ak-input[aria-invalid="true"] { border-color: var(--artifact-color-danger); }',
|
|
115
|
+
'.ak-footer { padding-top: var(--artifact-space-4); border-top: 1px solid var(--artifact-color-border); color: var(--artifact-color-muted); font-size: var(--artifact-font-size-1); }',
|
|
116
|
+
'@media (prefers-reduced-motion: no-preference) { .ak-button, .ak-switch__bar button { transition: background-color 0.15s ease, border-color 0.15s ease; } }',
|
|
117
|
+
'@media print { .ak-switch__bar { display: none; } .ak-switch [data-ak-panel][hidden] { display: grid !important; } .ak-page { width: auto; } }',
|
|
118
|
+
].join('\n');
|
|
119
|
+
/**
|
|
120
|
+
* Switch groups: buttons with data-ak-option show the data-ak-panel of the
|
|
121
|
+
* same name inside the nearest data-ak-switch. Nested switches stay independent.
|
|
122
|
+
* A switch announces its choice with an ak-change event so a demo can react.
|
|
123
|
+
*/
|
|
124
|
+
const SCRIPT_V1 = [
|
|
125
|
+
'(function () {',
|
|
126
|
+
' var marker = "data-ak-switch-ready";',
|
|
127
|
+
' if (document.documentElement.hasAttribute(marker)) return;',
|
|
128
|
+
' document.documentElement.setAttribute(marker, "");',
|
|
129
|
+
' function own(sw, node) { return node.closest("[data-ak-switch]") === sw; }',
|
|
130
|
+
' function choose(sw, value) {',
|
|
131
|
+
' sw.querySelectorAll("[data-ak-option]").forEach(function (b) { if (own(sw, b)) b.setAttribute("aria-pressed", String(b.getAttribute("data-ak-option") === value)); });',
|
|
132
|
+
' sw.querySelectorAll("[data-ak-panel]").forEach(function (p) { if (own(sw, p)) p.hidden = p.getAttribute("data-ak-panel") !== value; });',
|
|
133
|
+
' sw.dispatchEvent(new CustomEvent("ak-change", { bubbles: true, detail: { value: value } }));',
|
|
134
|
+
' }',
|
|
135
|
+
' document.addEventListener("click", function (event) {',
|
|
136
|
+
' var target = event.target;',
|
|
137
|
+
' if (!(target instanceof Element)) return;',
|
|
138
|
+
' var button = target.closest("button[data-ak-option]:not(:disabled)");',
|
|
139
|
+
' var sw = button && button.closest("[data-ak-switch]");',
|
|
140
|
+
' if (sw) choose(sw, button.getAttribute("data-ak-option"));',
|
|
141
|
+
' });',
|
|
142
|
+
' function init() {',
|
|
143
|
+
' document.querySelectorAll("[data-ak-switch]").forEach(function (sw) {',
|
|
144
|
+
' var options = Array.prototype.filter.call(sw.querySelectorAll("[data-ak-option]"), function (b) { return own(sw, b); });',
|
|
145
|
+
' var pick = options.find(function (b) { return b.getAttribute("aria-pressed") === "true"; }) || options[0];',
|
|
146
|
+
' if (pick) choose(sw, pick.getAttribute("data-ak-option"));',
|
|
147
|
+
' });',
|
|
148
|
+
' }',
|
|
149
|
+
' if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", init); else init();',
|
|
150
|
+
'})();',
|
|
151
|
+
].join('\n');
|
|
152
|
+
/**
|
|
153
|
+
* Collect the component classes a stylesheet defines.
|
|
154
|
+
* @param css the stylesheet.
|
|
155
|
+
* @returns sorted unique class names.
|
|
156
|
+
*/
|
|
157
|
+
function classesOf(css) {
|
|
158
|
+
return [...new Set([...css.matchAll(/\.(ak-[a-z0-9_-]+)/g)].map((match) => match[1] ?? ''))].sort();
|
|
159
|
+
}
|
|
160
|
+
export const KITS = {
|
|
161
|
+
'1': { css: CSS_V1, script: SCRIPT_V1, classes: classesOf(CSS_V1) },
|
|
162
|
+
};
|
|
163
|
+
/** Never interpret inherited object properties as kit releases. */
|
|
164
|
+
export function getKit(version) {
|
|
165
|
+
return version !== undefined && Object.hasOwn(KITS, version) ? KITS[version] : undefined;
|
|
166
|
+
}
|
|
167
|
+
/** The version new pages should declare. */
|
|
168
|
+
export const LATEST_KIT = '1';
|
|
169
|
+
/** The meta name a page uses to opt in. */
|
|
170
|
+
export const KIT_META = 'artifact-kit';
|
|
171
|
+
/**
|
|
172
|
+
* The kit version a page declares, if any.
|
|
173
|
+
* @param content the page HTML.
|
|
174
|
+
* @returns the declared version text, or undefined when the page does not opt in.
|
|
175
|
+
*/
|
|
176
|
+
export function declaredKit(content) {
|
|
177
|
+
for (const { name, text: tag } of openingTags(content)) {
|
|
178
|
+
if (name !== 'meta')
|
|
179
|
+
continue;
|
|
180
|
+
const attributes = new Map();
|
|
181
|
+
for (const attr of tag.slice(5, -1).matchAll(/([^\s=/>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g)) {
|
|
182
|
+
const key = (attr[1] ?? '').toLowerCase();
|
|
183
|
+
if (!attributes.has(key))
|
|
184
|
+
attributes.set(key, attr[2] ?? attr[3] ?? attr[4] ?? '');
|
|
185
|
+
}
|
|
186
|
+
if (attributes.get('name')?.toLowerCase() === KIT_META)
|
|
187
|
+
return attributes.get('content') ?? '';
|
|
188
|
+
}
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* The style and script the renderer adds for a page that opts in to a known
|
|
193
|
+
* kit release. An unknown version adds nothing; the page check reports it.
|
|
194
|
+
* @param content the page HTML.
|
|
195
|
+
* @returns head markup, or an empty string.
|
|
196
|
+
*/
|
|
197
|
+
export function kitHead(content) {
|
|
198
|
+
const version = declaredKit(content);
|
|
199
|
+
const kit = getKit(version);
|
|
200
|
+
if (kit === undefined)
|
|
201
|
+
return '';
|
|
202
|
+
return '<style data-artifact-kit="' + version + '">' + kit.css + '</style><script data-artifact-kit="' + version + '">' + kit.script + '</script>';
|
|
203
|
+
}
|
|
204
|
+
/** The component reference the brief gives the agent for a page. Every class it names exists in the kit. */
|
|
205
|
+
export const KIT_REFERENCE = String.raw `Kit 1. Add <meta name="artifact-kit" content="1"> and the renderer injects these classes and the switch script. The kit already styles body text, headings, links, code, focus, and selection from the tokens, so write no base styles of your own. Page CSS may add layout for content the kit does not cover.
|
|
206
|
+
|
|
207
|
+
Page: <main class="ak-page"> (72rem) or "ak-page ak-page--narrow" (48rem, reading pages).
|
|
208
|
+
Header: <header class="ak-header"><h1>Title</h1><p class="ak-lede">One-paragraph answer.</p><ul class="ak-meta"><li>Date</li><li><span class="ak-badge ak-badge--warning">Draft</span></li></ul></header>
|
|
209
|
+
Section: <section class="ak-section" data-section="key" aria-labelledby="key"><div class="ak-section__head"><h2 id="key">Heading</h2><p>Optional standfirst.</p></div>...</section>. Long text goes in <div class="ak-prose">.
|
|
210
|
+
Layout: ak-grid (cards that wrap), ak-split (two columns that stack), ak-card.
|
|
211
|
+
Callout: <aside class="ak-callout ak-callout--info"><p class="ak-callout__title">Title</p><p>Text</p></aside>. Choose ak-callout--info, ak-callout--success, ak-callout--warning, or ak-callout--danger. Use one for a rule or risk, not for emphasis.
|
|
212
|
+
Badge: <span class="ak-badge ak-badge--info">Text</span>. Choose ak-badge--info, ak-badge--success, ak-badge--warning, or ak-badge--danger; ak-badge--plain drops the dot. Always carry a text label.
|
|
213
|
+
Facts: <dl class="ak-facts"><div><dt>Label</dt><dd>Value</dd></div></dl>.
|
|
214
|
+
Table: <div class="ak-table" tabindex="0" role="region" aria-label="..."><table><caption>Scope or source of the rows</caption>...</table></div>. Add ak-table--stack and data-label on each td to stack rows on narrow panels.
|
|
215
|
+
Code: <figure class="ak-code"><figcaption>path/or/label</figcaption><pre><code>...</code></pre></figure>.
|
|
216
|
+
Steps: <ol class="ak-steps"><li><strong>Step</strong><p>Detail</p></li></ol>.
|
|
217
|
+
Decision: <article class="ak-decision"><h3>Question</h3><p>Options and evidence</p><p class="ak-decision__pick"><strong>Recommended:</strong> choice and reason</p></article>.
|
|
218
|
+
Quote: <blockquote class="ak-quote"><p>Exact words</p><cite>Who, where</cite></blockquote>.
|
|
219
|
+
Switch (before and after, scenarios, tabs of states): <div class="ak-switch" data-ak-switch><div class="ak-switch__bar" role="group" aria-label="View"><button type="button" data-ak-option="before" aria-pressed="true">Before</button><button type="button" data-ak-option="after" aria-pressed="false">After</button></div><div data-ak-panel="before">...</div><div data-ak-panel="after" hidden>...</div></div>. The switch emits an ak-change event with detail.value. Print shows every panel. Give each panel its own visible heading so its meaning survives without the switch buttons.
|
|
220
|
+
Compare (side by side): <div class="ak-compare"><div><p class="ak-compare__label">Today</p>...</div><div><p class="ak-compare__label">Proposed</p>...</div></div>.
|
|
221
|
+
Product UI replica: <figure class="ak-frame"><div class="ak-frame__bar">app.example.com/settings</div><div class="ak-frame__body ak-demo">...</div><figcaption class="ak-frame__caption">What to notice</figcaption></figure>. Inside .ak-demo, recreate the product's own look: define its colors and fonts once as custom properties on the .ak-demo element (--demo-bg, --demo-text, --demo-font, and any others you need) and use only those variables in the replica's rules. Prefix every replica selector with .ak-demo; this is a styling convention, not CSS isolation. Set --demo-font-size and --demo-line-height when needed.
|
|
222
|
+
Controls: ak-button, ak-button--primary, ak-actions (button row), <label class="ak-field">Label<input class="ak-input" name="..."></label>. Buttons use type="button".
|
|
223
|
+
Small text: ak-muted, ak-small, ak-num (tabular numbers). Footer: <footer class="ak-footer">.
|
|
224
|
+
`;
|