soma-kit 0.1.6 → 0.1.9

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.
@@ -23,6 +23,8 @@ import { redirectFragment } from './redirect';
23
23
  import { guardRemoveActions } from './remove-guard';
24
24
  import { bizKinds, frameKinds, resolveBiz, resolveFrame } from './registry';
25
25
  import { checkPageStyleCoverage, collectPageCandidates, compileStyles } from './styles';
26
+ import type { KitAppTheme } from "../app/app-config";
27
+ import { resolveNineDimThemeCss } from "../theme/nine-dim-palettes";
26
28
  import type {
27
29
  KitAppContext,
28
30
  KitCompileResult,
@@ -59,6 +61,93 @@ export function stableHash(text: string): string {
59
61
 
60
62
  // ---------------------------------------------------------------- 第 1 步:校验
61
63
 
64
+
65
+ function escapeHtml(text: string): string {
66
+ return text
67
+ .replace(/&/g, "&")
68
+ .replace(/</g, "&lt;")
69
+ .replace(/>/g, "&gt;")
70
+ .replace(/"/g, "&quot;")
71
+ .replace(/'/g, "&#39;");
72
+ }
73
+
74
+ function renderInheritedHeader(header: NonNullable<KitAppContext["layout"]>["header"]): string {
75
+ if (!header) return "";
76
+ let brandHtml = "";
77
+ if (header.brand?.text) {
78
+ const b = header.brand;
79
+ brandHtml = b.href
80
+ ? `<a href="${escapeHtml(b.href)}" class="font-semibold text-fg hover:text-primary transition-colors">${escapeHtml(b.text)}</a>`
81
+ : `<span class="font-semibold text-fg">${escapeHtml(b.text)}</span>`;
82
+ }
83
+
84
+ let navHtml = "";
85
+ if (header.nav && header.nav.length > 0) {
86
+ const navItems = header.nav.map((it) => {
87
+ const target = it.url ?? it.href ?? "#";
88
+ return `<a href="${escapeHtml(target)}" class="text-sm text-fg-muted hover:text-fg transition-colors">${escapeHtml(it.label)}</a>`;
89
+ }).join("\n");
90
+ navHtml = `<nav class="flex items-center gap-4">\n${navItems}\n</nav>`;
91
+ }
92
+
93
+ let actionsHtml = "";
94
+ if (header.actions && header.actions.length > 0) {
95
+ const actionItems = header.actions.map((it) => {
96
+ const target = it.url ?? it.href ?? "#";
97
+ return `<a href="${escapeHtml(target)}" class="text-sm font-medium text-fg hover:text-primary transition-colors">${escapeHtml(it.label)}</a>`;
98
+ }).join("\n");
99
+ actionsHtml = `<div class="flex items-center gap-3">\n${actionItems}\n</div>`;
100
+ }
101
+
102
+ const leftPart = [brandHtml, navHtml].filter(Boolean).join("\n");
103
+ return `<header data-kit-layout-header="true" class="border-b border-subtle bg-surface px-6 py-3 flex items-center justify-between gap-4">\n<div class="flex items-center gap-6">\n${leftPart}\n</div>\n${actionsHtml}\n</header>`;
104
+ }
105
+
106
+ function renderInheritedSidebar(sidebar: NonNullable<KitAppContext["layout"]>["sidebar"]): string {
107
+ if (!sidebar) return "";
108
+ let sidebarInner = "";
109
+ if (sidebar.title) {
110
+ sidebarInner += `<div class="mb-4 text-xs font-semibold uppercase tracking-wider text-fg-muted">${escapeHtml(sidebar.title)}</div>\n`;
111
+ }
112
+ if (sidebar.menu && sidebar.menu.length > 0) {
113
+ const menuItems = sidebar.menu.map((it) => {
114
+ const target = it.url ?? it.href ?? "#";
115
+ return `<a href="${escapeHtml(target)}" class="rounded px-2 py-1.5 text-sm text-fg hover:bg-surface transition-colors">${escapeHtml(it.label)}</a>`;
116
+ }).join("\n");
117
+ sidebarInner += `<nav class="flex flex-col gap-1">\n${menuItems}\n</nav>`;
118
+ }
119
+ return sidebarInner;
120
+ }
121
+
122
+ function renderInheritedFooter(footer: NonNullable<KitAppContext["layout"]>["footer"]): string {
123
+ if (!footer) return "";
124
+ const textHtml = footer.text ? `<span>${escapeHtml(footer.text)}</span>` : "<span></span>";
125
+ let linksHtml = "";
126
+ if (footer.links && footer.links.length > 0) {
127
+ const linkItems = footer.links.map((link) => {
128
+ return `<a href="${escapeHtml(link.href)}" class="hover:text-fg transition-colors">${escapeHtml(link.label)}</a>`;
129
+ }).join("\n");
130
+ linksHtml = `<div class="flex items-center gap-4">\n${linkItems}\n</div>`;
131
+ }
132
+ return `<footer data-kit-layout-footer="true" class="border-t border-subtle bg-surface-sunken px-6 py-3 text-xs text-fg-muted flex items-center justify-between gap-4">\n${textHtml}\n${linksHtml}\n</footer>`;
133
+ }
134
+
135
+ function renderInheritedLayout(framedHtml: string, layout: NonNullable<KitAppContext["layout"]>): string {
136
+ const headerHtml = layout.header ? renderInheritedHeader(layout.header) : "";
137
+ let bodyHtml = framedHtml;
138
+ if (layout.sidebar !== undefined) {
139
+ const sidebarContent = renderInheritedSidebar(layout.sidebar);
140
+ bodyHtml = `<div class="flex min-h-screen"><aside data-kit-layout-sidebar="true" class="w-56 shrink-0 border-r border-subtle bg-surface-sunken p-4">\n${sidebarContent}\n</aside><div class="flex-1 min-w-0">\n${framedHtml}\n</div></div>`;
141
+ }
142
+ const footerHtml = layout.footer ? renderInheritedFooter(layout.footer) : "";
143
+ const parts = [headerHtml, bodyHtml, footerHtml].filter(Boolean);
144
+ return `<div data-kit-inherited-layout="true">\n${parts.join("\n")}\n</div>`;
145
+ }
146
+
147
+ function compileThemeRootCss(theme: KitAppTheme): string {
148
+ return resolveNineDimThemeCss(theme);
149
+ }
150
+
62
151
  function checkClassesAt(decl: KitPageDecl, out: KitDiagnostic[]): void {
63
152
  const f = decl.frame;
64
153
  out.push(...checkClasses(f.class, 'frame.class'));
@@ -314,11 +403,59 @@ async function compileKitPageCore(page: unknown, app: KitAppContext): Promise<Ki
314
403
  const crossDiagnostics = mergeFragments(...allFragments).diagnostics;
315
404
  if (crossDiagnostics.length > 0) return { diagnostics: crossDiagnostics };
316
405
 
406
+ // 9 维正交页面主题 DSL(theme)
407
+ const appTheme = (app as { appConfig?: { theme?: KitAppTheme }; theme?: KitAppTheme; themeConfig?: KitAppTheme }).appConfig?.theme
408
+ ?? (app as { theme?: KitAppTheme }).theme
409
+ ?? ((app as { themeConfig?: KitAppTheme }).themeConfig);
410
+
411
+ const is9DimTheme = Boolean(
412
+ appTheme && (
413
+ appTheme.palette !== undefined ||
414
+ appTheme.color_depth !== undefined ||
415
+ appTheme.colorDepth !== undefined ||
416
+ appTheme.color_saturation !== undefined ||
417
+ appTheme.colorSaturation !== undefined ||
418
+ appTheme.font_family !== undefined ||
419
+ appTheme.fontFamily !== undefined ||
420
+ appTheme.font_scale !== undefined ||
421
+ appTheme.fontScale !== undefined ||
422
+ appTheme.boundary !== undefined ||
423
+ appTheme.shape !== undefined ||
424
+ appTheme.surface_texture !== undefined ||
425
+ appTheme.surfaceTexture !== undefined ||
426
+ appTheme.density === "COMPACT_DENSE" ||
427
+ appTheme.density === "STANDARD_BALANCED" ||
428
+ appTheme.density === "SPACIOUS_AIRY"
429
+ )
430
+ );
431
+
432
+ // 继承应用级默认布局(layout)
433
+ const appLayout = app.layout;
434
+ const shouldInheritLayout =
435
+ appLayout !== undefined &&
436
+ decl.page.inheritLayout !== false &&
437
+ decl.page.redirect === undefined &&
438
+ decl.frame.kind !== "blank-frame" &&
439
+ decl.frame.kind !== "workspace-frame";
440
+
441
+ if (shouldInheritLayout) {
442
+ if (decl.page.maxWidth === undefined && appLayout.maxWidth !== undefined && (decl.frame.kind === "single-frame" || decl.frame.kind === "top-main-frame")) {
443
+ decl.page.maxWidth = appLayout.maxWidth;
444
+ }
445
+ if (decl.page.sticky === undefined && appLayout.stickyHeader !== undefined) {
446
+ decl.page.sticky = appLayout.stickyHeader;
447
+ }
448
+ }
449
+
317
450
  // 第 3 步:装入页面框
318
451
  const frameInput: KitFrameInput = { page: decl.page, slots: slotFragments };
319
452
  frameInput.registeredActions = app.registeredActions;
320
453
  frameInput.locale = pageLocale;
321
- if (app.themeConfig?.density !== undefined) frameInput.density = app.themeConfig.density;
454
+ if (is9DimTheme && appTheme?.density !== undefined) {
455
+ frameInput.density = (appTheme.density === "COMPACT_DENSE" || appTheme.density === "compact") ? "compact" : "standard";
456
+ } else if (app.themeConfig?.density !== undefined) {
457
+ frameInput.density = app.themeConfig.density;
458
+ }
322
459
  if (decl.frame.class !== undefined) frameInput.class = decl.frame.class;
323
460
  if (decl.frame.slotClass !== undefined) frameInput.slotClass = decl.frame.slotClass;
324
461
  if (decl.frame.config !== undefined) frameInput.config = decl.frame.config;
@@ -388,15 +525,23 @@ async function compileKitPageCore(page: unknown, app: KitAppContext): Promise<Ki
388
525
  // 第 4 步:候选类(片段 + HTML + 基础 / 数据组件自带 + 应用组件)
389
526
  const langAttr = pageLocale !== '' ? ` lang="${pageLocale}"` : '';
390
527
  const dirAttr = pageDir !== undefined && (pageLocale !== '' || decl.page.dir !== undefined) ? ` dir="${pageDir}"` : '';
391
- const html = `<div data-soma-host="${KIT_ROOT_HOST}"${langAttr}${dirAttr}>\n${framed.html}\n</div>`;
528
+ const pageContent = shouldInheritLayout && (appLayout.header !== undefined || appLayout.sidebar !== undefined || appLayout.footer !== undefined)
529
+ ? renderInheritedLayout(framed.html, appLayout)
530
+ : framed.html;
531
+ const themeAttrs = is9DimTheme && appTheme
532
+ ? ` data-kit-palette="${appTheme.palette ?? "CLASSIC_TECH_BLUE"}" data-kit-boundary="${appTheme.boundary ?? "HAIRLINE_BORDER"}" data-kit-shape="${appTheme.shape ?? "STANDARD_ROUNDED"}" data-kit-texture="${appTheme.surface_texture ?? appTheme.surfaceTexture ?? "SOLID_MATTE"}" data-kit-density="${appTheme.density ?? "STANDARD_BALANCED"}"`
533
+ : "";
534
+ const html = `<div data-soma-host="${KIT_ROOT_HOST}"${themeAttrs}${langAttr}${dirAttr}>\n${pageContent}\n</div>`;
392
535
  const candidates = collectPageCandidates(framed, html, app);
393
536
  // 安全网:按页裁剪样式后,页面里出现的内置组件必须已并入候选类(漏登记在编译期报 KIT-STYLE-002)
394
537
  const coverage = checkPageStyleCoverage(framed, html, app);
395
538
  if (coverage.length > 0) return { diagnostics: coverage };
396
539
 
397
540
  // 第 5 步:样式 + 四件套
398
- const style = await compileStyles(candidates, app, blockTheme.css);
541
+ const themeRootCss = is9DimTheme && appTheme ? compileThemeRootCss(appTheme) : "";
542
+ const style = await compileStyles(candidates, app, `${themeRootCss ? `${themeRootCss}\n` : ""}${blockTheme.css}`);
399
543
  if (style.diagnostics.length > 0) return { diagnostics: style.diagnostics };
544
+ const finalCss = themeRootCss ? `${style.css}\n${themeRootCss}` : style.css;
400
545
 
401
546
  const envelope: Record<string, unknown> = { url: decl.page.url, title: decl.page.title };
402
547
  if (decl.page.subtitle !== undefined) envelope.subtitle = decl.page.subtitle;
@@ -466,7 +611,7 @@ async function compileKitPageCore(page: unknown, app: KitAppContext): Promise<Ki
466
611
  html,
467
612
  uiYaml,
468
613
  eventYaml,
469
- styles: [{ key: `kit-${style.digest}`, css: style.css }],
614
+ styles: [{ key: `kit-${style.digest}`, css: finalCss }],
470
615
  version: `kit-${stableHash(html + uiYaml + eventYaml + style.digest)}`,
471
616
  };
472
617
  return { artifact, diagnostics: [] };
@@ -264,6 +264,9 @@ export function parsePageDecl(raw: unknown, diagnostics: KitDiagnostic[]): KitPa
264
264
  shape('page.locale', 'page.locale 必须是语言标签(如 zh-CN / en / ar)', diagnostics);
265
265
  }
266
266
  if (rawDir !== undefined && rawDir !== 'ltr' && rawDir !== 'rtl') shape('page.dir', 'page.dir 只许 ltr / rtl', diagnostics);
267
+ if (page.inherit_layout !== undefined && typeof page.inherit_layout !== 'boolean') {
268
+ shape('page.inherit_layout', 'page.inherit_layout 必须是布尔值', diagnostics);
269
+ }
267
270
  if (raw.locales !== undefined) {
268
271
  const lc = raw.locales;
269
272
  const ok = isRecord(lc) && Object.values(lc).every((t) => isRecord(t) && Object.values(t).every((s) => typeof s === 'string'));
@@ -277,6 +280,7 @@ export function parsePageDecl(raw: unknown, diagnostics: KitDiagnostic[]): KitPa
277
280
  if (typeof eyebrow === 'string' && eyebrow !== '') meta.eyebrow = eyebrow;
278
281
  if (Array.isArray(roles)) meta.roles = roles.filter((r): r is string => typeof r === 'string');
279
282
  if (crumbs !== undefined && crumbs.length > 0) meta.breadcrumbs = crumbs;
283
+ if (typeof page.inherit_layout === 'boolean') meta.inheritLayout = page.inherit_layout;
280
284
  if (redirectDecl !== undefined) meta.redirect = redirectDecl;
281
285
  Object.assign(meta, header);
282
286
  Object.assign(meta, parsePageParams(page, url, diagnostics));
@@ -44,7 +44,7 @@ export interface KitTabDecl {
44
44
 
45
45
  /** 解析后的页面声明 */
46
46
  export interface KitPageDecl {
47
- page: { url: string; title: string; subtitle?: string; eyebrow?: string; roles?: string[]; breadcrumbs?: Array<{ label: string; href?: string }>; redirect?: KitRedirectDecl; params?: Record<string, Record<string, unknown>>; query?: Record<string, Record<string, unknown>>; locale?: string; dir?: 'ltr' | 'rtl'; theme?: string | Record<string, unknown>; envelope?: import('../app/envelope').KitEnvelope } & Pick<import('../frames/types').KitPageMeta, 'actions' | 'back' | 'status' | 'statusFollow' | 'maxWidth' | 'sticky'>;
47
+ page: { url: string; title: string; subtitle?: string; eyebrow?: string; roles?: string[]; breadcrumbs?: Array<{ label: string; href?: string }>; redirect?: KitRedirectDecl; params?: Record<string, Record<string, unknown>>; query?: Record<string, Record<string, unknown>>; locale?: string; dir?: 'ltr' | 'rtl'; theme?: string | Record<string, unknown>; envelope?: import('../app/envelope').KitEnvelope; inheritLayout?: boolean } & Pick<import('../frames/types').KitPageMeta, 'actions' | 'back' | 'status' | 'statusFollow' | 'maxWidth' | 'sticky'>;
48
48
  frame: {
49
49
  kind: string;
50
50
  class?: string;
@@ -72,6 +72,10 @@ export interface KitAppContext {
72
72
  envelope?: import('../app/envelope').KitEnvelope;
73
73
  /** 整站主题声明(规整后的 kit.app.yaml#theme):块级主题求解以它为页面基底;缺省走 foundation 默认 */
74
74
  themeConfig?: import('../theme/theme-tokens').KitThemeConfig;
75
+ /** 整站 9 维正交主题声明(规整后的 kit.app.yaml#theme) */
76
+ theme?: import('../app/app-config').KitAppTheme;
77
+ /** 应用级默认页面布局继承配置(kit.app.yaml 的 layout:) */
78
+ layout?: import('../app/app-config').KitAppLayout;
75
79
  /** 应用扩展的业务组件(与内置同名需自行保证不重名;重名以应用的为准) */
76
80
  bizComponents?: Record<string, KitBizDefinition>;
77
81
  /** 应用扩展的页面框 */
package/src/data/embed.ts CHANGED
@@ -89,6 +89,7 @@ export interface KitSelectFieldOptions {
89
89
  /** 加在 Select 根上的类(如固定宽度 w-36;须已在调用方的候选类里) */
90
90
  className?: string;
91
91
  ariaLabel?: string;
92
+ name?: string;
92
93
  /** 用户选中后调(hidden 载体的 value 已更新) */
93
94
  onChange?(value: string): void;
94
95
  }
@@ -118,6 +119,7 @@ export function mountKitSelectField(opts: KitSelectFieldOptions): KitSelectField
118
119
  disabled: opts.disabled === true,
119
120
  searchable: opts.searchable === true,
120
121
  aria_label: opts.ariaLabel ?? '',
122
+ name: opts.name ?? '',
121
123
  class: opts.className ?? '',
122
124
  }, {
123
125
  change: (payload: { value?: unknown }) => {
@@ -36,11 +36,11 @@ const SLOT_END ='ms-auto flex shrink-0 items-center gap-2';
36
36
  const BRAND_CLASS = `flex min-w-0 cursor-pointer items-center gap-3 rounded-md ${FOCUS}`;
37
37
  const LOGO_CLASS = 'flex h-8 w-8 shrink-0 items-center justify-center rounded-md bg-primary text-sm font-semibold text-primary-fg';
38
38
  const BRAND_TEXT_CLASS = 'min-w-0 truncate text-base font-semibold text-fg';
39
- const MENU_BTN_CLASS = `inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-md border border-border bg-surface text-fg transition-colors hover:bg-surface-sunken @4xl:hidden ${FOCUS}`;
39
+ const MENU_BTN_CLASS = `inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-md bg-surface-raised text-fg shadow-xs transition-colors hover:bg-surface-sunken @4xl:hidden ${FOCUS}`;
40
40
  const BTN_BASE = `inline-flex h-9 items-center justify-center gap-2 rounded-md px-3 text-sm font-medium transition-colors ${FOCUS}`;
41
41
  const VARIANT_CLASS: Record<string, string> = {
42
42
  ghost: `${BTN_BASE} text-fg-muted hover:bg-surface-sunken hover:text-fg`,
43
- outline: `${BTN_BASE} border border-border bg-surface text-fg hover:bg-surface-sunken`,
43
+ outline: `${BTN_BASE} bg-surface-raised text-fg shadow-xs hover:bg-surface-sunken`,
44
44
  primary: `${BTN_BASE} bg-primary text-primary-fg hover:bg-primary-hover active:bg-primary-active`,
45
45
  };
46
46
  const DISABLED_CLASS = 'inline-flex h-9 cursor-not-allowed items-center justify-center gap-2 rounded-md px-3 text-sm font-medium text-fg-subtle opacity-60';
@@ -1,6 +1,6 @@
1
1
  // 生成物,不得改写:由 soma-kit/scripts/build-kit-grammar-runtime.mjs 从 spec/kit-grammar.json 抽出。
2
2
  // 浏览器侧只读这份运行期切片(闭集与限值);编译侧读完整真源。改真源后运行 `npm run grammar:runtime -w soma-kit` 重新生成。
3
- // 真源指纹 sha256:3ab3728363470542a29b171c0706dadee96abdddc0ae84bcad56231d0a9eb76c
3
+ // 真源指纹 sha256:1e6ab689403573b4a191a24658525cd03183351b0d1a8a5c5298f40674193cf6
4
4
 
5
5
  export const KIT_GRAMMAR_RUNTIME = {
6
6
  "i18n": {
@@ -288,7 +288,7 @@ export const KIT_GRAMMAR_RUNTIME = {
288
288
  "KIT-PAGE-004": "页面结构非法(缺 page / product 或形状不对),或应用内页面 URL 重复",
289
289
  "KIT-PAGE-005": "对象名或角色名用了保留名(frame / tabs / page / p / ctrl)或非字母数字下划线,或字段名含点号",
290
290
  "KIT-APP-001": "kit.app.yaml 非法,或其登记(组件 accept 等)与 soma.app.ts 代码登记不一致",
291
- "KIT-APP-002": "kit.app.yaml 的 theme 取值不在闭集内(palette_scheme / font_scheme 取自 soma-design 方案库,color 为 #rgb / #rrggbb,mode 为 light / dark / system / soft)",
291
+ "KIT-APP-002": "kit.app.yaml 的 theme 取值不在闭集内(9 维正交页面主题 DSL:palette、color_depth、color_saturation、font_family、font_scale、boundary、shape、surface_texture、density,或兼容旧字段)",
292
292
  "KIT-APP-003": "theme 交 soma-foundation 令牌求解失败(折叠或求解阶段返回错误诊断)",
293
293
  "KIT-THEME-001": "块级 theme 形状非法:不是简写字符串或对象、空对象、或对象里的值不是字符串(page.theme / product.theme)",
294
294
  "KIT-THEME-002": "块级 theme 含未知词:对象键不在 mode / color,简写不在闭集,词条不属于该维度",
@@ -365,7 +365,9 @@ export const KIT_GRAMMAR_RUNTIME = {
365
365
  "KIT-AIT-003": "AI 页意图蓝图含未知键,或取值在闭集之外(意图词 frame / product、字段 type、字段 display、内置动作、tone、permission 档)",
366
366
  "KIT-AIT-004": "AI 页意图蓝图引用了授权接口之外的 api / write_api / delete_api,或 delete_api 指向的不是授权表里的写类方法",
367
367
  "KIT-AIT-005": "AI 页意图蓝图的对象描述不足以编译(form 视图没有 write_api、角色缺必填对象、既无接口也无样本、display 与字段类型不匹配或不在 list / summary 视图、remove 缺 delete_api 等)",
368
- "KIT-AIT-006": "AI 页提交面拒收:persist 取了 registry(kit 目标只取 runtime / memory),或提交的源不是本目标 accept 出的"
368
+ "KIT-AIT-006": "AI 页提交面拒收:persist 取了 registry(kit 目标只取 runtime / memory),或提交的源不是本目标 accept 出的",
369
+ "KIT-APP-004": "应用扩展组件未经唯一接入路径登记(必须经 wrapKitDataComponent、composeKitDataComponents 或 declareKitUses 显式声明依赖组件与候选类,且候选类不得含非法任意值、依赖组件名必须在内置闭集内)",
370
+ "KIT-APP-005": "应用扩展组件或组件目录源码私造已被 soma-kit 标准组件接管的非标原生控件(如原生 select、range、date/time)或书写内联 style 属性"
369
371
  },
370
372
  "actionBuiltins": {
371
373
  "removeTitleFields": [
@@ -2482,7 +2484,8 @@ export const KIT_AI_WRITER: KitAiWriter = {
2482
2484
  "locale": "ai",
2483
2485
  "dir": "human-only",
2484
2486
  "theme": "ai",
2485
- "envelope": "human-only"
2487
+ "envelope": "human-only",
2488
+ "inherit_layout": "human-only"
2486
2489
  },
2487
2490
  "frame": {
2488
2491
  "kind": "ai",