km-card-layout-core 0.1.26 → 0.1.28

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
@@ -8,9 +8,8 @@ Utilities for working with card layout schemas: sanitize layouts, handle binding
8
8
  - `resolveBindingValue(path, data, context?)`: read bound data with optional context.
9
9
  - `styleObjectToString(style, unit?)`: convert a style object to a CSS string.
10
10
  - `addUnit(value, unit)`: append px/rpx to numbers or numeric strings.
11
- - Background helpers: `applyBackground`, `applySpecialStyle`, `backgroundChange`, `buildRenderData`, etc.
11
+ - Background helper: `backgroundChange` to apply template background colors.
12
12
 
13
13
  ## Usage
14
14
 
15
15
  Use the layout schema and card elements directly for rendering on web or mini-program targets—no extra RenderNode transformation layer is required. The core is platform-agnostic and can be imported via `require('km-card-layout-core')`.
16
-
package/bindings.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  import type { CardElement, CardLayoutSchema } from './interface';
2
- import type {
3
- TemplateBackground,
4
- TemplateItem,
5
- } from './interface/data/payload';
2
+ import type { TemplateItem } from './interface/data/payload';
6
3
 
7
4
  export function stripLayoutBindings(
8
5
  layouts: CardLayoutSchema[] = []
@@ -29,6 +26,8 @@ export function applyItemCollectBindings(
29
26
  layouts: CardLayoutSchema[] = [],
30
27
  items: TemplateItem[] = []
31
28
  ): CardLayoutSchema[] {
29
+ console.log('applyItemCollectBindings', layouts, items);
30
+
32
31
  const targetLayouts = Array.isArray(layouts) ? layouts : [];
33
32
  const metaMap = new Map<string, TemplateItem>();
34
33
  const metaList = Array.isArray(items) ? items : [];
@@ -47,15 +46,17 @@ export function applyItemCollectBindings(
47
46
  const defaultValue =
48
47
  meta && meta.default !== undefined ? meta.default : el.defaultValue;
49
48
  const key = meta && meta.key !== undefined ? meta.key : el.key;
50
- const base: any = { ...el };
49
+ const base: any = { ...el, label: meta?.name };
51
50
  if (binding !== undefined) base.binding = binding;
52
51
  else delete base.binding;
53
52
  if (defaultValue !== undefined) base.defaultValue = defaultValue;
54
53
  else delete base.defaultValue;
55
54
  if (key !== undefined) base.key = key;
56
55
  else delete base.key;
57
- if (el.type === 'text' && meta?.extra?.icon) {
58
- base.icon = { ...(base.icon || {}), name: meta.extra.icon };
56
+ if (el.type === 'text') {
57
+ // if (meta?.extra?.icon) {
58
+ // base.icon = { ...(base.icon || {}), name: meta.extra.icon };
59
+ // }
59
60
  }
60
61
 
61
62
  if (el.type === 'layout-panel') {
@@ -64,6 +65,7 @@ export function applyItemCollectBindings(
64
65
  children: (el.children || []).map(assignBinding),
65
66
  } as CardElement;
66
67
  }
68
+
67
69
  return base as CardElement;
68
70
  };
69
71
 
@@ -72,16 +74,3 @@ export function applyItemCollectBindings(
72
74
  children: (layout.children || []).map(assignBinding),
73
75
  }));
74
76
  }
75
-
76
- export function getTemplateItems(ids: string, items: TemplateItem[]) {
77
- const idArray = ids.split(',').map(id => id.trim());
78
- return items.filter(item => idArray.includes(String(item.id)));
79
- }
80
-
81
- export function getTemplateBackgrounds(
82
- ids: string,
83
- items: TemplateBackground[]
84
- ) {
85
- const idArray = ids.split(',').map(id => id.trim());
86
- return items.filter(item => idArray.includes(String(item.id)));
87
- }
package/dist/bindings.js CHANGED
@@ -2,8 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.stripLayoutBindings = stripLayoutBindings;
4
4
  exports.applyItemCollectBindings = applyItemCollectBindings;
5
- exports.getTemplateItems = getTemplateItems;
6
- exports.getTemplateBackgrounds = getTemplateBackgrounds;
7
5
  function stripLayoutBindings(layouts = []) {
8
6
  const targetLayouts = Array.isArray(layouts) ? layouts : [];
9
7
  const stripElement = (el) => {
@@ -22,6 +20,7 @@ function stripLayoutBindings(layouts = []) {
22
20
  }));
23
21
  }
24
22
  function applyItemCollectBindings(layouts = [], items = []) {
23
+ console.log('applyItemCollectBindings', layouts, items);
25
24
  const targetLayouts = Array.isArray(layouts) ? layouts : [];
26
25
  const metaMap = new Map();
27
26
  const metaList = Array.isArray(items) ? items : [];
@@ -31,14 +30,13 @@ function applyItemCollectBindings(layouts = [], items = []) {
31
30
  }
32
31
  });
33
32
  const assignBinding = (el) => {
34
- var _a;
35
33
  const meta = metaMap.get(String(el.id));
36
34
  const binding = meta && meta.bind !== undefined && meta.bind !== null
37
35
  ? meta.bind
38
36
  : el.binding;
39
37
  const defaultValue = meta && meta.default !== undefined ? meta.default : el.defaultValue;
40
38
  const key = meta && meta.key !== undefined ? meta.key : el.key;
41
- const base = { ...el };
39
+ const base = { ...el, label: meta === null || meta === void 0 ? void 0 : meta.name };
42
40
  if (binding !== undefined)
43
41
  base.binding = binding;
44
42
  else
@@ -51,8 +49,10 @@ function applyItemCollectBindings(layouts = [], items = []) {
51
49
  base.key = key;
52
50
  else
53
51
  delete base.key;
54
- if (el.type === 'text' && ((_a = meta === null || meta === void 0 ? void 0 : meta.extra) === null || _a === void 0 ? void 0 : _a.icon)) {
55
- base.icon = { ...(base.icon || {}), name: meta.extra.icon };
52
+ if (el.type === 'text') {
53
+ // if (meta?.extra?.icon) {
54
+ // base.icon = { ...(base.icon || {}), name: meta.extra.icon };
55
+ // }
56
56
  }
57
57
  if (el.type === 'layout-panel') {
58
58
  return {
@@ -67,11 +67,3 @@ function applyItemCollectBindings(layouts = [], items = []) {
67
67
  children: (layout.children || []).map(assignBinding),
68
68
  }));
69
69
  }
70
- function getTemplateItems(ids, items) {
71
- const idArray = ids.split(',').map(id => id.trim());
72
- return items.filter(item => idArray.includes(String(item.id)));
73
- }
74
- function getTemplateBackgrounds(ids, items) {
75
- const idArray = ids.split(',').map(id => id.trim());
76
- return items.filter(item => idArray.includes(String(item.id)));
77
- }
package/dist/index.js CHANGED
@@ -1,10 +1,4 @@
1
1
  "use strict";
2
- /**
3
- * CardMaster 布局 JSON 渲染核心
4
- *
5
- * - 平台无关:不依赖 DOM/小程序 API,方便在 Web/小程序/Node 复用。
6
- * - 职责:将布局 Schema 与业务数据合成可渲染树,外层只需将节点映射到各端组件。
7
- */
8
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
3
  if (k2 === undefined) k2 = k;
10
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -20,7 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
20
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
21
15
  };
22
16
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.buildTextIconMeta = exports.getIconName = exports.getImageSrc = exports.getTextValue = exports.buildImageContentStyle = exports.buildTextContentStyle = exports.buildPanelContentStyle = exports.buildBaseContentStyle = exports.buildBackgroundStyle = exports.buildCardStyle = exports.buildWrapperStyle = exports.buildRenderData = exports.updateElementsStyle = exports.applyBackground = exports.applySpecialStyle = exports.resolveSpecialStyle = exports.DEFAULT_DECOR_COLOR = exports.backgroundChange = exports.getTemplateBackgrounds = exports.getTemplateItems = exports.applyItemCollectBindings = exports.stripLayoutBindings = exports.resolveBindingValue = exports.areChildrenEqual = exports.parseLayout = exports.normalizeId = exports.collectBindings = exports.getAbsLayout = exports.roundToInt = exports.sanitizeElement = exports.sanitizeLayout = exports.normalizeLayout = exports.isObject = exports.toNumber = exports.styleObjectToString = exports.addUnit = void 0;
17
+ exports.processCardLayout = exports.handleSpecialFields = exports.buildTextIconMeta = exports.getIconName = exports.getImageSrc = exports.getTextValue = exports.buildImageContentStyle = exports.buildTextValueStyle = exports.buildTextContentStyle = exports.buildPanelContentStyle = exports.buildBaseContentStyle = exports.buildBackgroundStyle = exports.buildCardStyle = exports.buildWrapperStyle = exports.backgroundChange = exports.applyItemCollectBindings = exports.stripLayoutBindings = exports.resolveBindingValue = exports.parseLayout = exports.normalizeId = exports.getAbsLayout = exports.roundToInt = exports.sanitizeElement = exports.sanitizeLayout = exports.normalizeLayout = exports.isObject = exports.toNumber = exports.styleObjectToString = exports.addUnit = void 0;
18
+ /**
19
+ * Core exports for card layout helpers.
20
+ * Platform-agnostic utilities shared by web/mini-program renderers.
21
+ */
24
22
  __exportStar(require("./interface/index"), exports);
25
23
  var helpers_1 = require("./helpers");
26
24
  Object.defineProperty(exports, "addUnit", { enumerable: true, get: function () { return helpers_1.addUnit; } });
@@ -33,26 +31,15 @@ Object.defineProperty(exports, "sanitizeLayout", { enumerable: true, get: functi
33
31
  Object.defineProperty(exports, "sanitizeElement", { enumerable: true, get: function () { return layout_1.sanitizeElement; } });
34
32
  Object.defineProperty(exports, "roundToInt", { enumerable: true, get: function () { return layout_1.roundToInt; } });
35
33
  Object.defineProperty(exports, "getAbsLayout", { enumerable: true, get: function () { return layout_1.getAbsLayout; } });
36
- Object.defineProperty(exports, "collectBindings", { enumerable: true, get: function () { return layout_1.collectBindings; } });
37
34
  Object.defineProperty(exports, "normalizeId", { enumerable: true, get: function () { return layout_1.normalizeId; } });
38
35
  Object.defineProperty(exports, "parseLayout", { enumerable: true, get: function () { return layout_1.parseLayout; } });
39
- Object.defineProperty(exports, "areChildrenEqual", { enumerable: true, get: function () { return layout_1.areChildrenEqual; } });
40
36
  var data_1 = require("./data");
41
37
  Object.defineProperty(exports, "resolveBindingValue", { enumerable: true, get: function () { return data_1.resolveBindingValue; } });
42
38
  var bindings_1 = require("./bindings");
43
39
  Object.defineProperty(exports, "stripLayoutBindings", { enumerable: true, get: function () { return bindings_1.stripLayoutBindings; } });
44
40
  Object.defineProperty(exports, "applyItemCollectBindings", { enumerable: true, get: function () { return bindings_1.applyItemCollectBindings; } });
45
- Object.defineProperty(exports, "getTemplateItems", { enumerable: true, get: function () { return bindings_1.getTemplateItems; } });
46
- Object.defineProperty(exports, "getTemplateBackgrounds", { enumerable: true, get: function () { return bindings_1.getTemplateBackgrounds; } });
47
41
  var changeBackground_1 = require("./ops/changeBackground");
48
42
  Object.defineProperty(exports, "backgroundChange", { enumerable: true, get: function () { return changeBackground_1.backgroundChange; } });
49
- Object.defineProperty(exports, "DEFAULT_DECOR_COLOR", { enumerable: true, get: function () { return changeBackground_1.DEFAULT_DECOR_COLOR; } });
50
- Object.defineProperty(exports, "resolveSpecialStyle", { enumerable: true, get: function () { return changeBackground_1.resolveSpecialStyle; } });
51
- Object.defineProperty(exports, "applySpecialStyle", { enumerable: true, get: function () { return changeBackground_1.applySpecialStyle; } });
52
- Object.defineProperty(exports, "applyBackground", { enumerable: true, get: function () { return changeBackground_1.applyBackground; } });
53
- Object.defineProperty(exports, "updateElementsStyle", { enumerable: true, get: function () { return changeBackground_1.updateElementsStyle; } });
54
- var tool_1 = require("./render/tool");
55
- Object.defineProperty(exports, "buildRenderData", { enumerable: true, get: function () { return tool_1.buildRenderData; } });
56
43
  var helpers_2 = require("./render/helpers");
57
44
  Object.defineProperty(exports, "buildWrapperStyle", { enumerable: true, get: function () { return helpers_2.buildWrapperStyle; } });
58
45
  Object.defineProperty(exports, "buildCardStyle", { enumerable: true, get: function () { return helpers_2.buildCardStyle; } });
@@ -60,8 +47,12 @@ Object.defineProperty(exports, "buildBackgroundStyle", { enumerable: true, get:
60
47
  Object.defineProperty(exports, "buildBaseContentStyle", { enumerable: true, get: function () { return helpers_2.buildBaseContentStyle; } });
61
48
  Object.defineProperty(exports, "buildPanelContentStyle", { enumerable: true, get: function () { return helpers_2.buildPanelContentStyle; } });
62
49
  Object.defineProperty(exports, "buildTextContentStyle", { enumerable: true, get: function () { return helpers_2.buildTextContentStyle; } });
50
+ Object.defineProperty(exports, "buildTextValueStyle", { enumerable: true, get: function () { return helpers_2.buildTextValueStyle; } });
63
51
  Object.defineProperty(exports, "buildImageContentStyle", { enumerable: true, get: function () { return helpers_2.buildImageContentStyle; } });
64
52
  Object.defineProperty(exports, "getTextValue", { enumerable: true, get: function () { return helpers_2.getTextValue; } });
65
53
  Object.defineProperty(exports, "getImageSrc", { enumerable: true, get: function () { return helpers_2.getImageSrc; } });
66
54
  Object.defineProperty(exports, "getIconName", { enumerable: true, get: function () { return helpers_2.getIconName; } });
67
55
  Object.defineProperty(exports, "buildTextIconMeta", { enumerable: true, get: function () { return helpers_2.buildTextIconMeta; } });
56
+ var init_1 = require("./render/init");
57
+ Object.defineProperty(exports, "handleSpecialFields", { enumerable: true, get: function () { return init_1.handleSpecialFields; } });
58
+ Object.defineProperty(exports, "processCardLayout", { enumerable: true, get: function () { return init_1.processCardLayout; } });
package/dist/layout.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.areChildrenEqual = exports.normalizeLayout = exports.parseLayout = exports.normalizeId = exports.collectBindings = exports.sanitizeLayout = exports.sanitizeElement = exports.getAbsLayout = exports.roundToInt = void 0;
3
+ exports.normalizeLayout = exports.parseLayout = exports.normalizeId = exports.sanitizeLayout = exports.sanitizeElement = exports.getAbsLayout = exports.roundToInt = void 0;
4
4
  const helpers_1 = require("./helpers");
5
5
  const DEFAULT_CARD_WIDTH = 343;
6
6
  const DEFAULT_CARD_HEIGHT = 210;
@@ -40,18 +40,6 @@ const sanitizeLayout = (layout) => ({
40
40
  children: (layout.children || []).map(child => (0, exports.sanitizeElement)(child)),
41
41
  });
42
42
  exports.sanitizeLayout = sanitizeLayout;
43
- const collectBindings = (elements = []) => {
44
- const result = [];
45
- elements.forEach(el => {
46
- if (el.binding)
47
- result.push(el.binding);
48
- if (el.type === 'layout-panel' && el.children) {
49
- result.push(...(0, exports.collectBindings)(el.children));
50
- }
51
- });
52
- return Array.from(new Set(result));
53
- };
54
- exports.collectBindings = collectBindings;
55
43
  const normalizeId = (id) => {
56
44
  if (id === undefined || id === null)
57
45
  return undefined;
@@ -111,7 +99,3 @@ const normalizeLayout = (layout) => {
111
99
  .filter((value) => Boolean(value));
112
100
  };
113
101
  exports.normalizeLayout = normalizeLayout;
114
- const areChildrenEqual = (a, b) => {
115
- return JSON.stringify(a === null || a === void 0 ? void 0 : a.children) === JSON.stringify(b === null || b === void 0 ? void 0 : b.children);
116
- };
117
- exports.areChildrenEqual = areChildrenEqual;
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.setElementVisible = exports.updateElementsStyle = exports.applyBackground = exports.applySpecialStyle = exports.resolveSpecialStyle = exports.DEFAULT_DECOR_COLOR = void 0;
4
3
  exports.backgroundChange = backgroundChange;
5
4
  const toNameArray = (name) => {
6
5
  if (Array.isArray(name))
@@ -13,10 +12,10 @@ const toNameArray = (name) => {
13
12
  .filter(Boolean);
14
13
  };
15
14
  function backgroundChange(bg, layout) {
15
+ var _a;
16
+ const extras = bg.fontColorExtra || [];
17
+ const iconSpecialColor = (_a = extras.find(sc => toNameArray(sc.name).some(n => n === 'icon'))) === null || _a === void 0 ? void 0 : _a.color;
16
18
  const applySpecialColor = (el) => {
17
- const extras = bg.fontColorExtra || [];
18
- if (!extras.length)
19
- return el;
20
19
  const keys = [];
21
20
  if (el.binding)
22
21
  keys.push(String(el.binding));
@@ -27,13 +26,48 @@ function backgroundChange(bg, layout) {
27
26
  if (el.type === 'custom')
28
27
  keys.push('decor');
29
28
  const matched = extras.find(sc => toNameArray(sc.name).some(n => keys.some(k => k === null || k === void 0 ? void 0 : k.startsWith(n))));
30
- if (!matched)
31
- return el;
32
29
  const baseStyle = { ...(el.style || {}) };
33
- if (el.type === 'custom') {
34
- return { ...el, style: { ...baseStyle, backgroundColor: matched.color } };
30
+ let nextEl = el;
31
+ if (matched) {
32
+ if (el.type === 'custom') {
33
+ nextEl = { ...el, style: { ...baseStyle, backgroundColor: matched.color } };
34
+ }
35
+ else {
36
+ nextEl = { ...el, style: { ...baseStyle, color: matched.color } };
37
+ }
38
+ }
39
+ else {
40
+ const nextStyle = { ...baseStyle };
41
+ let changed = false;
42
+ if (el.type === 'custom') {
43
+ if ('backgroundColor' in nextStyle) {
44
+ delete nextStyle.backgroundColor;
45
+ changed = true;
46
+ }
47
+ if ('background' in nextStyle) {
48
+ delete nextStyle.background;
49
+ changed = true;
50
+ }
51
+ }
52
+ if ('color' in nextStyle) {
53
+ delete nextStyle.color;
54
+ changed = true;
55
+ }
56
+ if (changed) {
57
+ nextEl = { ...el, style: nextStyle };
58
+ }
59
+ }
60
+ if (nextEl.type === 'text' && nextEl.icon) {
61
+ const nextIcon = { ...nextEl.icon };
62
+ if (iconSpecialColor) {
63
+ nextIcon.color = iconSpecialColor;
64
+ }
65
+ else if ('color' in nextIcon) {
66
+ delete nextIcon.color;
67
+ }
68
+ nextEl = { ...nextEl, icon: nextIcon };
35
69
  }
36
- return { ...el, style: { ...baseStyle, color: matched.color } };
70
+ return nextEl;
37
71
  };
38
72
  const traverse = (children = []) => children.map(el => {
39
73
  if (!el)
@@ -46,97 +80,11 @@ function backgroundChange(bg, layout) {
46
80
  }
47
81
  return applySpecialColor(el);
48
82
  });
83
+ const nextChildren = traverse(layout.children || []);
49
84
  return {
50
85
  ...layout,
51
86
  backgroundImage: bg.imgUrl,
52
87
  fontColor: bg.fontColor || layout.fontColor,
53
- children: traverse(layout.children || []),
88
+ children: nextChildren,
54
89
  };
55
90
  }
56
- exports.DEFAULT_DECOR_COLOR = '#94a3b8';
57
- const resolveSpecialStyle = (element, background) => {
58
- var _a;
59
- if (!background)
60
- return {};
61
- const keys = [];
62
- if (element.binding)
63
- keys.push(String(element.binding));
64
- if (element.id)
65
- keys.push(String(element.id));
66
- if (element.type === 'icon')
67
- keys.push('icon');
68
- if (element.type === 'custom')
69
- keys.push('decor');
70
- const matched = (_a = background.specialColors) === null || _a === void 0 ? void 0 : _a.find(sc => {
71
- const names = toNameArray(sc.name);
72
- return names.some(n => keys.some(k => k === null || k === void 0 ? void 0 : k.startsWith(n)));
73
- });
74
- if (!matched)
75
- return {};
76
- if (element.type === 'custom') {
77
- return { backgroundColor: matched.color };
78
- }
79
- return { color: matched.color };
80
- };
81
- exports.resolveSpecialStyle = resolveSpecialStyle;
82
- const applySpecialStyle = (el, background = null, options = {}) => {
83
- const specialStyle = (0, exports.resolveSpecialStyle)(el, background);
84
- const baseStyle = { ...(el.style || {}) };
85
- const isCustom = el.type === 'custom';
86
- const hasExplicit = isCustom
87
- ? Boolean(baseStyle.backgroundColor || baseStyle.background)
88
- : baseStyle.color !== undefined && baseStyle.color !== null;
89
- const overrideExisting = options.overrideExisting === true;
90
- const hasSpecial = Object.keys(specialStyle).length > 0;
91
- const defaultDecorColor = options.defaultDecorColor || exports.DEFAULT_DECOR_COLOR;
92
- if (hasSpecial) {
93
- if (hasExplicit && !overrideExisting)
94
- return el;
95
- return { ...el, style: { ...baseStyle, ...specialStyle } };
96
- }
97
- if (overrideExisting && hasExplicit) {
98
- const nextStyle = { ...baseStyle };
99
- if (isCustom) {
100
- nextStyle.backgroundColor = (background === null || background === void 0 ? void 0 : background.mainColor) || defaultDecorColor;
101
- }
102
- else {
103
- delete nextStyle.color;
104
- }
105
- return { ...el, style: nextStyle };
106
- }
107
- return el;
108
- };
109
- exports.applySpecialStyle = applySpecialStyle;
110
- const applyBackground = (layout, background, _options = {}) => {
111
- var _a;
112
- const bgId = Number(background.id);
113
- const payload = {
114
- id: Number.isFinite(bgId) ? bgId : 0,
115
- name: background.name,
116
- imgUrl: background.imageUrl,
117
- fontColor: background.mainColor,
118
- fontColorExtra: ((_a = background.specialColors) === null || _a === void 0 ? void 0 : _a.map(sc => ({
119
- name: sc.name,
120
- color: sc.color,
121
- }))) || [],
122
- };
123
- return backgroundChange(payload, layout);
124
- };
125
- exports.applyBackground = applyBackground;
126
- const updateElementsStyle = (layout, targetIds, updates) => {
127
- const targets = new Set(targetIds);
128
- return {
129
- ...layout,
130
- children: layout.children.map(el => targets.has(el.id)
131
- ? { ...el, style: { ...(el.style || {}), ...updates } }
132
- : el),
133
- };
134
- };
135
- exports.updateElementsStyle = updateElementsStyle;
136
- const setElementVisible = (layout, key, visible) => {
137
- return layout.map(la => ({
138
- ...la,
139
- children: la.children.map(el => (el.key === key ? { ...el, visible } : el)),
140
- }));
141
- };
142
- exports.setElementVisible = setElementVisible;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildTextIconMeta = exports.getIconName = exports.getImageSrc = exports.getTextValue = exports.buildImageContentStyle = exports.buildTextContentStyle = exports.buildPanelContentStyle = exports.buildBaseContentStyle = exports.buildBackgroundStyle = exports.buildCardStyle = exports.buildWrapperStyle = void 0;
3
+ exports.buildTextIconMeta = exports.getIconName = exports.getImageSrc = exports.getTextValue = exports.buildImageContentStyle = exports.buildTextValueStyle = exports.buildTextContentStyle = exports.buildPanelContentStyle = exports.buildBaseContentStyle = exports.buildBackgroundStyle = exports.buildCardStyle = exports.buildWrapperStyle = void 0;
4
4
  const helpers_1 = require("../helpers");
5
5
  const layout_1 = require("../layout");
6
6
  const data_1 = require("../data");
@@ -23,9 +23,6 @@ const buildCardStyle = (layout, unit = 'px') => ({
23
23
  width: (0, helpers_1.addUnit)(layout.width, unit),
24
24
  height: (0, helpers_1.addUnit)(layout.height, unit),
25
25
  color: layout.fontColor,
26
- borderRadius: layout.borderRadius !== undefined
27
- ? (0, helpers_1.addUnit)(layout.borderRadius, unit)
28
- : undefined,
29
26
  padding: layout.padding !== undefined ? (0, helpers_1.addUnit)(layout.padding, unit) : undefined,
30
27
  position: 'relative',
31
28
  overflow: 'hidden',
@@ -35,11 +32,12 @@ const buildCardStyle = (layout, unit = 'px') => ({
35
32
  exports.buildCardStyle = buildCardStyle;
36
33
  const buildBackgroundStyle = (layout, unit = 'px') => ({
37
34
  zIndex: layout.backgroundZIndex,
38
- borderRadius: layout.borderRadius !== undefined
39
- ? (0, helpers_1.addUnit)(layout.borderRadius, unit)
40
- : undefined,
41
- width: '100%',
42
- height: '100%',
35
+ width: layout.backgroundWidth
36
+ ? (0, helpers_1.addUnit)(layout.backgroundWidth, unit)
37
+ : '100%',
38
+ height: layout.backgroundHeight
39
+ ? (0, helpers_1.addUnit)(layout.backgroundHeight, unit)
40
+ : '100%',
43
41
  position: 'absolute',
44
42
  left: 0,
45
43
  top: 0,
@@ -76,15 +74,27 @@ const buildTextContentStyle = (el, unit = 'px') => {
76
74
  return style;
77
75
  };
78
76
  exports.buildTextContentStyle = buildTextContentStyle;
79
- const buildImageContentStyle = (el) => {
77
+ const buildTextValueStyle = (el, unit = 'px') => {
78
+ var _a, _b;
79
+ const style = {
80
+ letterSpacing: ((_a = el.style) === null || _a === void 0 ? void 0 : _a.letterSpacing) !== undefined && ((_b = el.style) === null || _b === void 0 ? void 0 : _b.letterSpacing) !== null
81
+ ? (0, helpers_1.addUnit)(el.style.letterSpacing, unit)
82
+ : '0',
83
+ };
84
+ return style;
85
+ };
86
+ exports.buildTextValueStyle = buildTextValueStyle;
87
+ const buildImageContentStyle = (el, unit = 'px') => {
88
+ var _a, _b, _c, _d;
80
89
  const style = { ...(el.style || {}) };
81
- const borderWidth = Number(style.borderWidth);
82
- if (Number.isFinite(borderWidth) && borderWidth > 0) {
83
- if (!style.borderStyle)
84
- style.borderStyle = 'solid';
85
- if (!style.borderColor)
86
- style.borderColor = '#000000';
87
- }
90
+ style.borderWidth =
91
+ ((_a = el.style) === null || _a === void 0 ? void 0 : _a.borderWidth) !== undefined && ((_b = el.style) === null || _b === void 0 ? void 0 : _b.borderWidth) !== null
92
+ ? (0, helpers_1.addUnit)(el.style.borderWidth, unit)
93
+ : '0';
94
+ style.borderRadius =
95
+ ((_c = el.style) === null || _c === void 0 ? void 0 : _c.borderRadius) !== undefined && ((_d = el.style) === null || _d === void 0 ? void 0 : _d.borderRadius) !== null
96
+ ? (0, helpers_1.addUnit)(el.style.borderRadius, unit)
97
+ : '0';
88
98
  return style;
89
99
  };
90
100
  exports.buildImageContentStyle = buildImageContentStyle;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleSpecialFields = handleSpecialFields;
4
+ exports.processCardLayout = processCardLayout;
5
+ function handleSpecialFields(data) {
6
+ var _a;
7
+ const user = (_a = data.user) !== null && _a !== void 0 ? _a : {};
8
+ return {
9
+ ...data,
10
+ user: {
11
+ ...user,
12
+ // 1. 构造 baseCompanyDuty
13
+ baseCompanyDuty: user.company && user.duty
14
+ ? `${user.company} ${user.duty}`
15
+ : user.baseCompanyDuty,
16
+ // 2. 构造 moreCardInfo.company
17
+ moreCardInfo: user.moreCardInfo
18
+ ? {
19
+ ...user.moreCardInfo,
20
+ company: Array.isArray(user.moreCardInfo.company)
21
+ ? user.moreCardInfo.company.map(item => `${item.company} ${item.duty}`)
22
+ : user.moreCardInfo.company,
23
+ }
24
+ : user.moreCardInfo,
25
+ },
26
+ };
27
+ }
28
+ const SHOW_KEYS = new Set([
29
+ "company_duty_custom",
30
+ "company_duty_1",
31
+ "company_duty_2",
32
+ ]);
33
+ const HIDE_KEYS = new Set(["company", "duty"]);
34
+ function getCompanyLen(data) {
35
+ var _a, _b;
36
+ const company = (_b = (_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.moreCardInfo) === null || _b === void 0 ? void 0 : _b.company;
37
+ return Array.isArray(company) ? company.length : 0;
38
+ }
39
+ function updateElementsVisible(elements, showMore) {
40
+ if (!elements)
41
+ return elements;
42
+ return elements.map((el) => {
43
+ // 先递归处理 layout-panel 的 children
44
+ let next = el;
45
+ if (el.type === "layout-panel") {
46
+ next = {
47
+ ...el,
48
+ children: updateElementsVisible(el.children, showMore),
49
+ };
50
+ }
51
+ const key = next.key;
52
+ if (!key)
53
+ return next;
54
+ // showMore=true: 显示 company_duty_*,隐藏 company/duty
55
+ // showMore=false: 反过来
56
+ if (SHOW_KEYS.has(key)) {
57
+ return { ...next, visible: showMore };
58
+ }
59
+ if (HIDE_KEYS.has(key)) {
60
+ return { ...next, visible: !showMore };
61
+ }
62
+ return next;
63
+ });
64
+ }
65
+ /**
66
+ * 根据 user.moreCardInfo.company 数组长度切换元素 visible:
67
+ * - length > 0: key in [company_duty_custom, company_duty_1, company_duty_2] => visible=true
68
+ * key in [company, duty] => visible=false
69
+ * - length === 0: 反之
70
+ */
71
+ function processCardLayout(layout, data) {
72
+ const companyLen = getCompanyLen(data);
73
+ const showMore = companyLen > 0;
74
+ return layout.map((card) => {
75
+ var _a;
76
+ return ({
77
+ ...card,
78
+ children: ((_a = updateElementsVisible(card.children, showMore)) !== null && _a !== void 0 ? _a : card.children),
79
+ });
80
+ });
81
+ }
package/index.ts CHANGED
@@ -1,14 +1,7 @@
1
1
  /**
2
- * CardMaster 布局 JSON 渲染核心
3
- *
4
- * - 平台无关:不依赖 DOM/小程序 API,方便在 Web/小程序/Node 复用。
5
- * - 职责:将布局 Schema 与业务数据合成可渲染树,外层只需将节点映射到各端组件。
2
+ * Core exports for card layout helpers.
3
+ * Platform-agnostic utilities shared by web/mini-program renderers.
6
4
  */
7
-
8
-
9
-
10
-
11
-
12
5
  export * from './interface/index';
13
6
  export { addUnit, styleObjectToString, toNumber, isObject } from './helpers';
14
7
  export {
@@ -17,27 +10,12 @@ export {
17
10
  sanitizeElement,
18
11
  roundToInt,
19
12
  getAbsLayout,
20
- collectBindings,
21
13
  normalizeId,
22
14
  parseLayout,
23
- areChildrenEqual,
24
15
  } from './layout';
25
16
  export { resolveBindingValue } from './data';
26
- export {
27
- stripLayoutBindings,
28
- applyItemCollectBindings,
29
- getTemplateItems,
30
- getTemplateBackgrounds,
31
- } from './bindings';
32
- export {
33
- backgroundChange,
34
- DEFAULT_DECOR_COLOR,
35
- resolveSpecialStyle,
36
- applySpecialStyle,
37
- applyBackground,
38
- updateElementsStyle,
39
- } from './ops/changeBackground';
40
- export { buildRenderData } from './render/tool';
17
+ export { stripLayoutBindings, applyItemCollectBindings } from './bindings';
18
+ export { backgroundChange } from './ops/changeBackground';
41
19
  export {
42
20
  buildWrapperStyle,
43
21
  buildCardStyle,
@@ -45,9 +23,12 @@ export {
45
23
  buildBaseContentStyle,
46
24
  buildPanelContentStyle,
47
25
  buildTextContentStyle,
26
+ buildTextValueStyle,
48
27
  buildImageContentStyle,
49
28
  getTextValue,
50
29
  getImageSrc,
51
30
  getIconName,
52
31
  buildTextIconMeta,
53
32
  } from './render/helpers';
33
+
34
+ export { handleSpecialFields, processCardLayout } from './render/init';
@@ -1,4 +1,4 @@
1
- import { CardLayoutSchema } from "../layout";
1
+ import { CardLayoutSchema } from '../layout';
2
2
 
3
3
  /**
4
4
  * 背景项
@@ -20,11 +20,10 @@ export interface TemplateItem {
20
20
  name: string;
21
21
  bind: string;
22
22
  key?: string;
23
- icon?: string;
24
23
  extra?: { icon?: string };
25
24
  default?: string;
26
25
  type: 'text' | 'image';
27
- cate: number;
26
+ cate: number; // 1 内置 | 2 特殊
28
27
  required?: 0 | 1;
29
28
  }
30
29
 
@@ -43,4 +42,5 @@ export interface TemplateLayoutInfo {
43
42
  isBgCustomize?: 0 | 1;
44
43
  createAt?: string;
45
44
  updateAt?: string;
45
+ illustration?: string;
46
46
  }
@@ -32,13 +32,14 @@ export interface TextIconConfig {
32
32
  gap?: number;
33
33
  align?: 'left' | 'right';
34
34
  enable?: boolean;
35
- style?: 'fill' | 'dot' | 'line';
35
+ style?: 'fill' | 'dot' | 'line' | 'text';
36
36
  }
37
37
 
38
38
  export interface TextElement extends CardElementBase {
39
39
  type: 'text';
40
40
  align?: 'left' | 'center' | 'right';
41
41
  icon?: TextIconConfig;
42
+ label?: string;
42
43
  }
43
44
 
44
45
  export interface ImageElement extends CardElementBase {