@colixsystems/widget-sdk 0.313.0 → 0.314.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 CHANGED
@@ -90,7 +90,11 @@ See the design reference for the full architecture: [`docs/architecture/widget-m
90
90
 
91
91
  ## Status
92
92
 
93
- `v0.313.0` — pre-publish. The package surface (types, function names, export paths) is the v1 contract; runtime behaviour for some hooks is stubbed (each hook documents what's wired and what isn't). It is **not yet published to npm**.
93
+ `v0.314.0` — pre-publish. The package surface (types, function names, export paths) is the v1 contract; runtime behaviour for some hooks is stubbed (each hook documents what's wired and what isn't). It is **not yet published to npm**.
94
+
95
+ ### What's new in 0.314.0 (contract 1.254.0)
96
+
97
+ **Hosts validate typography roles through one function.** `@colixsystems/widget-sdk/host` exports `normaliseTypographyRoles(raw)` and `resolveTypographyRoles(raw)`, the single validator the web Player, the exported app and AppStudio's backend now share for a theme's `typographyRoles`. A role colour is trimmed before it is checked, like every other theme colour, so a padded hex in a hand-edited theme now renders on both hosts instead of being dropped. The widget contract is unchanged.
94
98
 
95
99
  ### What's new in 0.313.0 (contract 1.254.0)
96
100
 
package/dist/host.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // Host-integration surface — not the author-facing widget API. See host.js.
2
- import type { WidgetPropertySchema } from "./index";
2
+ import type { TextRole, WidgetPropertySchema } from "./index";
3
3
 
4
4
  /**
5
5
  * Host render-boundary helper: fills a widget's propertySchema `default`s onto
@@ -134,6 +134,18 @@ export function applyThemeComponentStyle<T = Record<string, unknown>>(
134
134
  styleSchema?: Record<string, unknown> | null,
135
135
  ): T;
136
136
 
137
+ /** One text role's validated fields (REQ-THEME-ROLE). */
138
+ export type TypographyRole = Record<string, string | number>;
139
+
140
+ /**
141
+ * A raw `typographyRoles` blob cut down to the contract's roles and fields;
142
+ * unknown or out-of-bounds values are dropped and an emptied role is omitted.
143
+ */
144
+ export function normaliseTypographyRoles(raw: unknown): Partial<Record<TextRole, TypographyRole>>;
145
+
146
+ /** Every declared role, each merged per field over the contract's default. */
147
+ export function resolveTypographyRoles(raw: unknown): Record<TextRole, TypographyRole>;
148
+
137
149
  // sc-4939 — the host half of `useToast()`. A widget only ever calls the hook;
138
150
  // these are what a platform host puts behind `WidgetContext.toast`.
139
151
 
package/dist/host.js CHANGED
@@ -23,6 +23,9 @@ export {
23
23
  applyThemeComponentStyle,
24
24
  } from "./theme-components.js";
25
25
 
26
+ // REQ-THEME-ROLE: the one typography-role validator, for both hosts and the backend.
27
+ export { normaliseTypographyRoles, resolveTypographyRoles } from "./theme-text-roles.js";
28
+
26
29
  // sc-7800: the theme-token colour bindings. `resolveThemeTokenRefs` replaces a
27
30
  // bound `type: "color"` field with the colour its token currently resolves to,
28
31
  // at the SAME render boundary on both hosts — so a rebrand repaints the Player,
@@ -27,12 +27,15 @@ const { isPlainObject } = require("./plain-object.cjs");
27
27
  // must never drop a colour the runner already accepted.
28
28
  const HEX_COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
29
29
 
30
+ // Trimmed first, so a padded hex in a hand-edited theme survives.
31
+ function coerceThemeColor(value) {
32
+ if (typeof value !== "string") return undefined;
33
+ const trimmed = value.trim();
34
+ return HEX_COLOR.test(trimmed) ? trimmed : undefined;
35
+ }
36
+
30
37
  function coerceToken(def, value) {
31
- if (def.type === "color") {
32
- if (typeof value !== "string") return undefined;
33
- const trimmed = value.trim();
34
- return HEX_COLOR.test(trimmed) ? trimmed : undefined;
35
- }
38
+ if (def.type === "color") return coerceThemeColor(value);
36
39
  if (def.type === "size") {
37
40
  const n = typeof value === "number" ? value : Number(value);
38
41
  if (!Number.isFinite(n)) return undefined;
@@ -397,6 +400,7 @@ function applyThemeComponentStyle(manifestId, theme, props, styleSchema) {
397
400
  return { ...base, style: { ...themed, ...authored } };
398
401
  }
399
402
  module.exports = {
403
+ coerceThemeColor,
400
404
  normaliseThemeComponents,
401
405
  MAX_STYLE_STRING_LENGTH,
402
406
  styleValueBytes,
@@ -22,12 +22,15 @@ import { isPlainObject } from "./plain-object.js";
22
22
  // must never drop a colour the runner already accepted.
23
23
  const HEX_COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
24
24
 
25
+ // Trimmed first, so a padded hex in a hand-edited theme survives.
26
+ export function coerceThemeColor(value) {
27
+ if (typeof value !== "string") return undefined;
28
+ const trimmed = value.trim();
29
+ return HEX_COLOR.test(trimmed) ? trimmed : undefined;
30
+ }
31
+
25
32
  function coerceToken(def, value) {
26
- if (def.type === "color") {
27
- if (typeof value !== "string") return undefined;
28
- const trimmed = value.trim();
29
- return HEX_COLOR.test(trimmed) ? trimmed : undefined;
30
- }
33
+ if (def.type === "color") return coerceThemeColor(value);
31
34
  if (def.type === "size") {
32
35
  const n = typeof value === "number" ? value : Number(value);
33
36
  if (!Number.isFinite(n)) return undefined;
@@ -0,0 +1,64 @@
1
+ // GENERATED from theme-text-roles.js by scripts/generate-cjs.js — DO NOT EDIT.
2
+ // Change theme-text-roles.js, then run `npm run generate:cjs` in packages/widget-sdk.
3
+ "use strict";
4
+
5
+ const { CONTRACT } = require("./contract.cjs");
6
+ const { coerceThemeColor } = require("./theme-components.cjs");
7
+
8
+ // REQ-THEME-ROLE: the one typography-role validator for both hosts and the backend.
9
+
10
+ // One stored role value, checked against its declared type and bounds.
11
+ function coerceRoleField(def, value) {
12
+ if (def.type === "color") return coerceThemeColor(value);
13
+ if (def.type === "enum") {
14
+ // A weight may arrive as a number; both hosts' `fontWeight` takes the string.
15
+ if (typeof value !== "string" && typeof value !== "number") return undefined;
16
+ const candidate = String(value);
17
+ return def.values.includes(candidate) ? candidate : undefined;
18
+ }
19
+ if (def.type === "textTransform") {
20
+ return CONTRACT.themeComponentTextTransforms.includes(value) ? value : undefined;
21
+ }
22
+ // `Number(true)` is 1 and `Number(["12"])` is 12: a trust boundary drops a
23
+ // wrong-typed value rather than inventing an in-range one from it.
24
+ if (typeof value !== "number" && typeof value !== "string") return undefined;
25
+ const n = typeof value === "number" ? value : Number(value);
26
+ if (!Number.isFinite(n)) return undefined;
27
+ const clamped = Math.min(def.max, Math.max(def.min, n));
28
+ if (def.type === "size") return Math.round(clamped);
29
+ // Leading and tracking must keep the decimals `size` rounds away.
30
+ if (def.type === "decimal") return Math.round(clamped * 100) / 100;
31
+ return undefined;
32
+ }
33
+
34
+ /** A raw blob cut down to the contract; an emptied role is omitted. */
35
+ function normaliseTypographyRoles(raw) {
36
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
37
+ const out = {};
38
+ for (const role of CONTRACT.themeTextRoles) {
39
+ const requested = raw[role];
40
+ if (!requested || typeof requested !== "object" || Array.isArray(requested)) continue;
41
+ const fields = {};
42
+ for (const [field, def] of Object.entries(CONTRACT.themeTextRoleFields)) {
43
+ if (requested[field] === undefined) continue;
44
+ const coerced = coerceRoleField(def, requested[field]);
45
+ if (coerced !== undefined) fields[field] = coerced;
46
+ }
47
+ if (Object.keys(fields).length) out[role] = fields;
48
+ }
49
+ return out;
50
+ }
51
+
52
+ /** Every declared role, merged per field over its default; re-validates. */
53
+ function resolveTypographyRoles(raw) {
54
+ const overrides = normaliseTypographyRoles(raw);
55
+ const merged = {};
56
+ for (const [role, defaults] of Object.entries(CONTRACT.themeTokens.typography.roles)) {
57
+ merged[role] = overrides[role] ? { ...defaults, ...overrides[role] } : { ...defaults };
58
+ }
59
+ return merged;
60
+ }
61
+ module.exports = {
62
+ normaliseTypographyRoles,
63
+ resolveTypographyRoles,
64
+ };
@@ -0,0 +1,55 @@
1
+ // REQ-THEME-ROLE: the one typography-role validator for both hosts and the backend.
2
+ import { CONTRACT } from "./contract.js";
3
+ import { coerceThemeColor } from "./theme-components.js";
4
+
5
+ // One stored role value, checked against its declared type and bounds.
6
+ function coerceRoleField(def, value) {
7
+ if (def.type === "color") return coerceThemeColor(value);
8
+ if (def.type === "enum") {
9
+ // A weight may arrive as a number; both hosts' `fontWeight` takes the string.
10
+ if (typeof value !== "string" && typeof value !== "number") return undefined;
11
+ const candidate = String(value);
12
+ return def.values.includes(candidate) ? candidate : undefined;
13
+ }
14
+ if (def.type === "textTransform") {
15
+ return CONTRACT.themeComponentTextTransforms.includes(value) ? value : undefined;
16
+ }
17
+ // `Number(true)` is 1 and `Number(["12"])` is 12: a trust boundary drops a
18
+ // wrong-typed value rather than inventing an in-range one from it.
19
+ if (typeof value !== "number" && typeof value !== "string") return undefined;
20
+ const n = typeof value === "number" ? value : Number(value);
21
+ if (!Number.isFinite(n)) return undefined;
22
+ const clamped = Math.min(def.max, Math.max(def.min, n));
23
+ if (def.type === "size") return Math.round(clamped);
24
+ // Leading and tracking must keep the decimals `size` rounds away.
25
+ if (def.type === "decimal") return Math.round(clamped * 100) / 100;
26
+ return undefined;
27
+ }
28
+
29
+ /** A raw blob cut down to the contract; an emptied role is omitted. */
30
+ export function normaliseTypographyRoles(raw) {
31
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
32
+ const out = {};
33
+ for (const role of CONTRACT.themeTextRoles) {
34
+ const requested = raw[role];
35
+ if (!requested || typeof requested !== "object" || Array.isArray(requested)) continue;
36
+ const fields = {};
37
+ for (const [field, def] of Object.entries(CONTRACT.themeTextRoleFields)) {
38
+ if (requested[field] === undefined) continue;
39
+ const coerced = coerceRoleField(def, requested[field]);
40
+ if (coerced !== undefined) fields[field] = coerced;
41
+ }
42
+ if (Object.keys(fields).length) out[role] = fields;
43
+ }
44
+ return out;
45
+ }
46
+
47
+ /** Every declared role, merged per field over its default; re-validates. */
48
+ export function resolveTypographyRoles(raw) {
49
+ const overrides = normaliseTypographyRoles(raw);
50
+ const merged = {};
51
+ for (const [role, defaults] of Object.entries(CONTRACT.themeTokens.typography.roles)) {
52
+ merged[role] = overrides[role] ? { ...defaults, ...overrides[role] } : { ...defaults };
53
+ }
54
+ return merged;
55
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colixsystems/widget-sdk",
3
- "version": "0.313.0",
3
+ "version": "0.314.0",
4
4
  "description": "Common widget interface for AppStudio. Implements WidgetManifest, WidgetContext, property schema, and helper hooks.",
5
5
  "homepage": "https://github.com/Colix-AB/AppStudio",
6
6
  "type": "module",