@svadmin/core 0.75.0 → 0.76.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@svadmin/core",
3
- "version": "0.75.0",
3
+ "version": "0.76.0",
4
4
  "author": "zuohuadong",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.ts CHANGED
@@ -285,9 +285,9 @@ export type {
285
285
  SchemaFormValidationError,
286
286
  } from './schema-form';
287
287
  export {
288
- auditThemeContrast, failingContrastChecks, parseDtcgTheme, presetFromDtcg,
288
+ auditThemeContrast, exportDtcgTheme, failingContrastChecks, parseDtcgTheme, presetFromDtcg,
289
289
  } from './theme-dtcg';
290
- export type { ContrastCheck, DtcgIssue, DtcgTheme } from './theme-dtcg';
290
+ export type { ContrastCheck, DtcgColorToken, DtcgIssue, DtcgTheme, DtcgThemeDocument } from './theme-dtcg';
291
291
  export { useExport } from './export-hooks.svelte';
292
292
  export { snapshotExportTaskResult, downloadExportArtifact, safeArtifactUrl } from './export-format';
293
293
  export type { ExportTaskResult } from './export-format';
package/src/theme-dtcg.ts CHANGED
@@ -27,6 +27,18 @@ export interface DtcgTheme {
27
27
  issues: DtcgIssue[];
28
28
  }
29
29
 
30
+ export interface DtcgColorToken {
31
+ $type: 'color';
32
+ $value: string;
33
+ }
34
+
35
+ export interface DtcgThemeDocument {
36
+ color: {
37
+ light: Record<string, DtcgColorToken>;
38
+ dark: Record<string, DtcgColorToken>;
39
+ };
40
+ }
41
+
30
42
  const MAX_DEPTH = 32;
31
43
  const MAX_TOKENS = 2000;
32
44
  const CSS_VAR_PATTERN = /^--[a-z0-9-]{1,64}$/i;
@@ -163,6 +175,44 @@ export function parseDtcgTheme(document: unknown): DtcgTheme {
163
175
  return result;
164
176
  }
165
177
 
178
+ function cssVarToDtcgName(variable: string): string {
179
+ if (!CSS_VAR_PATTERN.test(variable)) {
180
+ throw new TypeError(`Invalid CSS variable name: ${variable}`);
181
+ }
182
+ const segments = variable.slice(2).split('-').filter(Boolean);
183
+ if (segments.length === 0) throw new TypeError(`Invalid CSS variable name: ${variable}`);
184
+ return segments.map((segment, index) => (
185
+ index === 0 ? segment : `${segment[0]?.toUpperCase() ?? ''}${segment.slice(1)}`
186
+ )).join('');
187
+ }
188
+
189
+ function exportDtcgMode(tokens: Record<string, string>, mode: 'light' | 'dark'): Record<string, DtcgColorToken> {
190
+ const result: Record<string, DtcgColorToken> = {};
191
+ for (const [variable, rawValue] of Object.entries(tokens)) {
192
+ const value = rawValue.trim();
193
+ if (!COLOR_VALUE_PATTERN.test(value) || /[;{}]/.test(value)) {
194
+ throw new TypeError(`${mode}.${variable} is not a portable color token value`);
195
+ }
196
+ const name = cssVarToDtcgName(variable);
197
+ if (result[name] !== undefined) {
198
+ throw new TypeError(`${mode} contains duplicate DTCG token name: ${name}`);
199
+ }
200
+ result[name] = { $type: 'color', $value: value };
201
+ }
202
+ if (Object.keys(result).length === 0) throw new TypeError(`${mode} must declare at least one color token`);
203
+ return result;
204
+ }
205
+
206
+ /** Serialize CSS color variables into a portable W3C DTCG token document. */
207
+ export function exportDtcgTheme(input: Pick<DtcgTheme, 'light' | 'dark'>): DtcgThemeDocument {
208
+ return {
209
+ color: {
210
+ light: exportDtcgMode(input.light, 'light'),
211
+ dark: exportDtcgMode(input.dark, 'dark'),
212
+ },
213
+ };
214
+ }
215
+
166
216
  /**
167
217
  * Build a complete preset from a DTCG document. Tokens present in the document
168
218
  * win; missing keys fall back to the generated palette for the same brand color.
@@ -276,4 +326,4 @@ export function formatContrastReport(presetName: string, checks: readonly Contra
276
326
  '| --- | --- | --- | --- | --- |',
277
327
  ...rows,
278
328
  ].join('\n');
279
- }
329
+ }