km-card-layout-core 0.1.11 → 0.1.14

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.
@@ -0,0 +1,179 @@
1
+ import type { CardElement, CardLayoutSchema } from '../interface';
2
+ import type { TemplateBackground } from '../interface/data/payload';
3
+
4
+ type BackgroundColorMap = {
5
+ name: string | string[];
6
+ color: string;
7
+ };
8
+
9
+ type CoreBackgroundConfig = {
10
+ id?: string | number;
11
+ name?: string;
12
+ imageUrl?: string;
13
+ mainColor?: string;
14
+ specialColors?: BackgroundColorMap[];
15
+ };
16
+
17
+ const toNameArray = (name?: string | string[]) => {
18
+ if (Array.isArray(name)) return name.filter(Boolean);
19
+ if (!name) return [];
20
+ return `${name}`
21
+ .split(',')
22
+ .map(n => n.trim())
23
+ .filter(Boolean);
24
+ };
25
+
26
+ export function backgroundChange(
27
+ bg: TemplateBackground,
28
+ layout: CardLayoutSchema
29
+ ): CardLayoutSchema {
30
+ const applySpecialColor = (el: CardElement): CardElement => {
31
+ const extras = bg.fontColorExtra || [];
32
+ if (!extras.length) return el;
33
+
34
+ const keys: string[] = [];
35
+ if (el.binding) keys.push(String(el.binding));
36
+ if (el.id) keys.push(String(el.id));
37
+ if (el.type === 'icon') keys.push('icon');
38
+ if (el.type === 'custom') keys.push('decor');
39
+
40
+ const matched = extras.find(sc =>
41
+ toNameArray(sc.name).some(n => keys.some(k => k?.startsWith(n)))
42
+ );
43
+ if (!matched) return el;
44
+
45
+ const baseStyle = { ...(el.style || {}) };
46
+ if (el.type === 'custom') {
47
+ return { ...el, style: { ...baseStyle, backgroundColor: matched.color } };
48
+ }
49
+ return { ...el, style: { ...baseStyle, color: matched.color } };
50
+ };
51
+
52
+ const traverse = (children: CardElement[] = []): CardElement[] =>
53
+ children.map(el => {
54
+ if (!el) return el;
55
+ if (el.type === 'layout-panel') {
56
+ return {
57
+ ...el,
58
+ children: traverse(el.children || []),
59
+ };
60
+ }
61
+ return applySpecialColor(el);
62
+ });
63
+
64
+ return {
65
+ ...layout,
66
+ backgroundImage: bg.imgUrl,
67
+ fontColor: bg.fontColor || layout.fontColor,
68
+ children: traverse(layout.children || []),
69
+ };
70
+ }
71
+
72
+ export const DEFAULT_DECOR_COLOR = '#94a3b8';
73
+
74
+ export const resolveSpecialStyle = (
75
+ element: CardElement,
76
+ background?: CoreBackgroundConfig | null
77
+ ) => {
78
+ if (!background) return {};
79
+ const keys: string[] = [];
80
+ if (element.binding) keys.push(String(element.binding));
81
+ if (element.id) keys.push(String(element.id));
82
+ if (element.type === 'icon') keys.push('icon');
83
+ if (element.type === 'custom') keys.push('decor');
84
+
85
+ const matched = background.specialColors?.find(sc => {
86
+ const names = toNameArray(sc.name);
87
+ return names.some(n => keys.some(k => k?.startsWith(n)));
88
+ });
89
+
90
+ if (!matched) return {};
91
+ if (element.type === 'custom') {
92
+ return { backgroundColor: matched.color };
93
+ }
94
+ return { color: matched.color };
95
+ };
96
+
97
+ export const applySpecialStyle = (
98
+ el: CardElement,
99
+ background: CoreBackgroundConfig | null = null,
100
+ options: { overrideExisting?: boolean; defaultDecorColor?: string } = {}
101
+ ): CardElement => {
102
+ const specialStyle = resolveSpecialStyle(el, background);
103
+ const baseStyle = { ...(el.style || {}) };
104
+ const isCustom = el.type === 'custom';
105
+ const hasExplicit = isCustom
106
+ ? Boolean(baseStyle.backgroundColor || baseStyle.background)
107
+ : baseStyle.color !== undefined && baseStyle.color !== null;
108
+
109
+ const overrideExisting = options.overrideExisting === true;
110
+ const hasSpecial = Object.keys(specialStyle).length > 0;
111
+ const defaultDecorColor = options.defaultDecorColor || DEFAULT_DECOR_COLOR;
112
+
113
+ if (hasSpecial) {
114
+ if (hasExplicit && !overrideExisting) return el;
115
+ return { ...el, style: { ...baseStyle, ...specialStyle } };
116
+ }
117
+
118
+ if (overrideExisting && hasExplicit) {
119
+ const nextStyle = { ...baseStyle };
120
+ if (isCustom) {
121
+ nextStyle.backgroundColor = background?.mainColor || defaultDecorColor;
122
+ } else {
123
+ delete nextStyle.color;
124
+ }
125
+ return { ...el, style: nextStyle };
126
+ }
127
+
128
+ return el;
129
+ };
130
+
131
+ export const applyBackground = (
132
+ layout: CardLayoutSchema,
133
+ background: CoreBackgroundConfig,
134
+ _options: {
135
+ previousBackground?: CoreBackgroundConfig | null;
136
+ defaultDecorColor?: string;
137
+ } = {}
138
+ ): CardLayoutSchema => {
139
+ const bgId = Number(background.id);
140
+ const payload: TemplateBackground = {
141
+ id: Number.isFinite(bgId) ? bgId : 0,
142
+ name: background.name!,
143
+ imgUrl: background.imageUrl!,
144
+ fontColor: background.mainColor,
145
+ fontColorExtra:
146
+ background.specialColors?.map(sc => ({
147
+ name: sc.name,
148
+ color: sc.color,
149
+ })) || [],
150
+ };
151
+ return backgroundChange(payload, layout);
152
+ };
153
+
154
+ export const updateElementsStyle = (
155
+ layout: CardLayoutSchema,
156
+ targetIds: string[],
157
+ updates: Record<string, any>
158
+ ): CardLayoutSchema => {
159
+ const targets = new Set(targetIds);
160
+ return {
161
+ ...layout,
162
+ children: layout.children.map(el =>
163
+ targets.has(el.id)
164
+ ? { ...el, style: { ...(el.style || {}), ...updates } }
165
+ : el
166
+ ),
167
+ };
168
+ };
169
+
170
+ export const setElementVisible = (
171
+ layout: CardLayoutSchema[],
172
+ key: string,
173
+ visible: boolean
174
+ ): CardLayoutSchema[] => {
175
+ return layout.map(la => ({
176
+ ...la,
177
+ children: la.children.map(el => (el.id === key ? { ...el, visible } : el)),
178
+ }));
179
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "km-card-layout-core",
3
- "version": "0.1.11",
3
+ "version": "0.1.14",
4
4
  "description": "Shared render helpers for CardMaster layout JSON (binding resolution, layout normalization).",
5
5
  "main": "dist/index.js",
6
6
  "types": "types.d.ts",
@@ -0,0 +1,287 @@
1
+ import type {
2
+ CardElement,
3
+ IconElement,
4
+ ImageElement,
5
+ LayoutPanelElement,
6
+ TextElement,
7
+ } from '../interface/elements';
8
+ import type { CardLayoutSchema, CardLayoutInput } from '../interface/layout';
9
+ import type {
10
+ BindingContext,
11
+ RenderNode,
12
+ RenderResult,
13
+ } from '../interface/render';
14
+ import { addUnit, styleObjectToString } from '../helpers';
15
+ import { getAbsLayout, normalizeLayout } from '../layout';
16
+ import { resolveBindingValue } from '../data';
17
+
18
+ const buildCardStyle = (layout: CardLayoutSchema, unit: 'px' | 'rpx') =>
19
+ styleObjectToString(
20
+ {
21
+ width: addUnit(layout.width, unit),
22
+ height: addUnit(layout.height, unit),
23
+ color: layout.fontColor,
24
+ borderRadius:
25
+ layout.borderRadius !== undefined
26
+ ? addUnit(layout.borderRadius, unit)
27
+ : undefined,
28
+ padding:
29
+ layout.padding !== undefined ? addUnit(layout.padding, unit) : undefined,
30
+ position: 'relative',
31
+ overflow: 'hidden',
32
+ boxSizing: 'border-box',
33
+ backgroundColor: 'transparent',
34
+ },
35
+ unit
36
+ );
37
+
38
+ const buildBackgroundStyle = (layout: CardLayoutSchema, unit: 'px' | 'rpx') =>
39
+ styleObjectToString(
40
+ {
41
+ zIndex: layout.backgroundZIndex,
42
+ borderRadius:
43
+ layout.borderRadius !== undefined
44
+ ? addUnit(layout.borderRadius, unit)
45
+ : undefined,
46
+ width: '100%',
47
+ height: '100%',
48
+ position: 'absolute',
49
+ left: 0,
50
+ top: 0,
51
+ },
52
+ unit
53
+ );
54
+
55
+ const buildWrapperStyle = (el: CardElement, unit: 'px' | 'rpx'): string => {
56
+ const abs = getAbsLayout(el);
57
+ if (!abs) return '';
58
+ return styleObjectToString(
59
+ {
60
+ position: 'absolute',
61
+ left: addUnit(abs.x, unit),
62
+ top: addUnit(abs.y, unit),
63
+ width: addUnit(abs.width, unit),
64
+ height: addUnit(abs.height, unit),
65
+ zIndex: abs.zIndex,
66
+ boxSizing: 'border-box',
67
+ },
68
+ unit
69
+ );
70
+ };
71
+
72
+ const buildPanelContentStyle = (
73
+ el: LayoutPanelElement,
74
+ unit: 'px' | 'rpx'
75
+ ) =>
76
+ styleObjectToString(
77
+ {
78
+ position: 'relative',
79
+ width: '100%',
80
+ height: '100%',
81
+ display: 'block',
82
+ boxSizing: 'border-box',
83
+ ...(el.style || {}),
84
+ },
85
+ unit
86
+ );
87
+
88
+ const buildTextContentStyle = (el: TextElement, unit: 'px' | 'rpx') => {
89
+ const textAlign =
90
+ (el.style?.textAlign as string | undefined) || el.align || undefined;
91
+ const style: Record<string, any> = {
92
+ ...(el.style || {}),
93
+ whiteSpace: 'pre-wrap',
94
+ wordBreak: 'break-word',
95
+ lineHeight:
96
+ el.style?.lineHeight !== undefined && el.style?.lineHeight !== null
97
+ ? el.style.lineHeight
98
+ : '1',
99
+ };
100
+ if (textAlign) style.textAlign = textAlign;
101
+ return styleObjectToString(style, unit);
102
+ };
103
+
104
+ const buildBaseContentStyle = (el: CardElement, unit: 'px' | 'rpx') =>
105
+ styleObjectToString(
106
+ {
107
+ ...(el.style || {}),
108
+ boxSizing: 'border-box',
109
+ },
110
+ unit
111
+ );
112
+
113
+ const buildImageContentStyle = (el: ImageElement, unit: 'px' | 'rpx') => {
114
+ const style: Record<string, any> = { ...(el.style || {}) };
115
+ const borderWidth = Number(style.borderWidth);
116
+ if (Number.isFinite(borderWidth) && borderWidth > 0) {
117
+ if (!style.borderStyle) style.borderStyle = 'solid';
118
+ if (!style.borderColor) style.borderColor = '#000000';
119
+ }
120
+ return styleObjectToString(style, unit);
121
+ };
122
+
123
+ const buildTextIcon = (
124
+ el: TextElement,
125
+ unit: 'px' | 'rpx'
126
+ ): RenderNode['icon'] | undefined => {
127
+ const icon = el.icon;
128
+ if (!icon || icon.enable === false) return undefined;
129
+
130
+ const style = icon.style || 'fill';
131
+ // el.extra.icon ||
132
+ const baseName = el.key || el.binding || el.id;
133
+ let name: string | undefined;
134
+ if (style === 'dot') name = 'round';
135
+ else if (style === 'line') name = baseName ? `${baseName}-line` : undefined;
136
+ else name = baseName || undefined;
137
+ if (!name) return undefined;
138
+
139
+ const size =
140
+ icon.size !== undefined && icon.size !== null
141
+ ? icon.size
142
+ : (el.style?.fontSize as any);
143
+ const gap = icon.gap !== undefined && icon.gap !== null ? icon.gap : 4;
144
+ const color =
145
+ icon.color ??
146
+ ((typeof el.style?.color === 'string' ? el.style.color : undefined) as
147
+ | string
148
+ | undefined);
149
+
150
+ return {
151
+ name: `${name}`,
152
+ text: `${name}`,
153
+ size: addUnit(size as any, unit),
154
+ gap: addUnit(gap as any, unit),
155
+ color: color as any,
156
+ align: icon.align || 'left',
157
+ wrapperStyle: styleObjectToString(
158
+ {
159
+ height: el.style?.lineHeight || el.style?.fontSize,
160
+ },
161
+ unit
162
+ ),
163
+ };
164
+ };
165
+
166
+ const buildNode = (
167
+ el: CardElement,
168
+ rootData: Record<string, any>,
169
+ unit: 'px' | 'rpx',
170
+ context: BindingContext
171
+ ): RenderNode | null => {
172
+ if (!el || el.visible === false) return null;
173
+ const wrapperStyle = buildWrapperStyle(el, unit);
174
+
175
+ if (el.type === 'layout-panel') {
176
+ return {
177
+ id: el.id,
178
+ type: el.type,
179
+ visible: el.visible,
180
+ wrapperStyle,
181
+ contentStyle: buildPanelContentStyle(el as LayoutPanelElement, unit),
182
+ children: buildRenderNodes(el.children || [], rootData, unit, context),
183
+ };
184
+ }
185
+
186
+ const baseStyle = buildBaseContentStyle(el, unit);
187
+ if (el.type === 'text') {
188
+ const value =
189
+ resolveBindingValue(el.binding, rootData, context) ??
190
+ el.defaultValue ??
191
+ '';
192
+ return {
193
+ id: el.id,
194
+ type: el.type,
195
+ wrapperStyle,
196
+ contentStyle: buildTextContentStyle(el as TextElement, unit),
197
+ text: `${value}`,
198
+ visible: el.visible,
199
+ icon: buildTextIcon(el as TextElement, unit),
200
+ };
201
+ }
202
+
203
+ if (el.type === 'image') {
204
+ const src =
205
+ resolveBindingValue(el.binding, rootData, context) ??
206
+ (el as ImageElement).defaultUrl ??
207
+ el.defaultValue ??
208
+ '';
209
+ const mode =
210
+ (el as ImageElement).fit === 'contain' ? 'aspectFit' : 'aspectFill';
211
+ return {
212
+ id: el.id,
213
+ type: el.type,
214
+ wrapperStyle,
215
+ contentStyle: buildImageContentStyle(el as ImageElement, unit),
216
+ src,
217
+ mode,
218
+ visible: el.visible,
219
+ };
220
+ }
221
+
222
+ if (el.type === 'icon') {
223
+ const name =
224
+ resolveBindingValue(el.binding, rootData, context) ??
225
+ (el as IconElement).name ??
226
+ el.defaultValue ??
227
+ '';
228
+ return {
229
+ id: el.id,
230
+ type: el.type,
231
+ wrapperStyle,
232
+ contentStyle: baseStyle,
233
+ name: `${name}`,
234
+ text: `${name}`,
235
+ visible: el.visible,
236
+ };
237
+ }
238
+
239
+ if (el.type === 'custom') {
240
+ return {
241
+ id: el.id,
242
+ type: el.type,
243
+ wrapperStyle,
244
+ contentStyle: baseStyle,
245
+ visible: el.visible,
246
+ };
247
+ }
248
+
249
+ return null;
250
+ };
251
+
252
+ export const buildRenderNodes = (
253
+ children: CardElement[],
254
+ rootData: Record<string, any>,
255
+ unit: 'px' | 'rpx' = 'px',
256
+ context: BindingContext = {}
257
+ ): RenderNode[] => {
258
+ if (!Array.isArray(children)) return [];
259
+
260
+ const nodes: RenderNode[] = [];
261
+ children.forEach(el => {
262
+ if (!el || el.visible === false) return;
263
+ const node = buildNode(el, rootData, unit, context);
264
+ if (node) nodes.push(node);
265
+ });
266
+
267
+ return nodes;
268
+ };
269
+
270
+ export const buildRenderResult = (
271
+ layoutInput: CardLayoutInput,
272
+ dataInput: Record<string, any>,
273
+ unit: 'px' | 'rpx' = 'px'
274
+ ): RenderResult => {
275
+ const layouts = normalizeLayout(layoutInput);
276
+ return layouts.map(layout => {
277
+ const cardStyle = buildCardStyle(layout, unit);
278
+ const bgStyle = buildBackgroundStyle(layout, unit);
279
+ const renderTree = buildRenderNodes(layout.children || [], dataInput || {}, unit);
280
+ return {
281
+ renderTree,
282
+ cardStyle,
283
+ backgroundImage: layout.backgroundImage || '',
284
+ backgroundStyle: bgStyle,
285
+ };
286
+ });
287
+ };
package/types.d.ts CHANGED
@@ -12,8 +12,6 @@ import type {
12
12
  CardLayoutSchema,
13
13
  RenderNode,
14
14
  RenderResult,
15
- TemplateBackground,
16
- TemplateItem,
17
15
  } from './interface';
18
16
 
19
17
  export function addUnit(
@@ -26,8 +24,37 @@ export function styleObjectToString(
26
24
  unit?: 'px' | 'rpx'
27
25
  ): string;
28
26
 
27
+ export function toNumber(value: unknown): number | undefined;
28
+
29
+ export function isObject(
30
+ val: unknown
31
+ ): val is Record<string, any> | any[];
32
+
29
33
  export function normalizeLayout(layout: CardLayoutInput): CardLayoutSchema[];
30
34
 
35
+ export function sanitizeLayout(layout: CardLayoutSchema): CardLayoutSchema;
36
+
37
+ export function sanitizeElement(element: CardElement): CardElement;
38
+
39
+ export function roundToInt(value: number): number;
40
+
41
+ export function getAbsLayout(
42
+ el: CardElement
43
+ ): import('./interface/elements').AbsoluteLayoutDefinition | null;
44
+
45
+ export function collectBindings(elements?: CardElement[]): string[];
46
+
47
+ export function normalizeId(
48
+ id?: string | number | null
49
+ ): number | string | undefined;
50
+
51
+ export function parseLayout(input: unknown): CardLayoutSchema | null;
52
+
53
+ export function areChildrenEqual(
54
+ a?: CardLayoutSchema | null,
55
+ b?: CardLayoutSchema | null
56
+ ): boolean;
57
+
31
58
  export function resolveBindingValue(
32
59
  binding: string | undefined,
33
60
  rootData: Record<string, any>,
@@ -40,9 +67,76 @@ export function stripLayoutBindings(
40
67
 
41
68
  export function applyItemCollectBindings(
42
69
  layouts: CardLayoutSchema[],
43
- items?: TemplateItem[]
70
+ items?: import('./interface/data/payload').TemplateItem[]
44
71
  ): CardLayoutSchema[];
45
72
 
73
+ export function getTemplateItems(
74
+ ids: string,
75
+ items: import('./interface/data/payload').TemplateItem[]
76
+ ): import('./interface/data/payload').TemplateItem[];
77
+
78
+ export function getTemplateBackgrounds(
79
+ ids: string,
80
+ items: import('./interface/data/payload').TemplateBackground[]
81
+ ): import('./interface/data/payload').TemplateBackground[];
82
+
83
+ export function backgroundChange(
84
+ bg: import('./interface/data/payload').TemplateBackground,
85
+ layout: CardLayoutSchema
86
+ ): CardLayoutSchema;
87
+
88
+ export const DEFAULT_DECOR_COLOR: string;
89
+
90
+ export function resolveSpecialStyle(
91
+ element: CardElement,
92
+ background?: {
93
+ id?: string | number;
94
+ name?: string;
95
+ imageUrl?: string;
96
+ mainColor?: string;
97
+ specialColors?: { name: string | string[]; color: string }[];
98
+ } | null
99
+ ): Record<string, any>;
100
+
101
+ export function applySpecialStyle(
102
+ element: CardElement,
103
+ background?: {
104
+ id?: string | number;
105
+ name?: string;
106
+ imageUrl?: string;
107
+ mainColor?: string;
108
+ specialColors?: { name: string | string[]; color: string }[];
109
+ } | null,
110
+ options?: { overrideExisting?: boolean; defaultDecorColor?: string }
111
+ ): CardElement;
112
+
113
+ export function applyBackground(
114
+ layout: CardLayoutSchema,
115
+ background: {
116
+ id?: string | number;
117
+ name?: string;
118
+ imageUrl?: string;
119
+ mainColor?: string;
120
+ specialColors?: { name: string | string[]; color: string }[];
121
+ },
122
+ options?: {
123
+ previousBackground?: {
124
+ id?: string | number;
125
+ name?: string;
126
+ imageUrl?: string;
127
+ mainColor?: string;
128
+ specialColors?: { name: string | string[]; color: string }[];
129
+ } | null;
130
+ defaultDecorColor?: string;
131
+ }
132
+ ): CardLayoutSchema;
133
+
134
+ export function updateElementsStyle(
135
+ layout: CardLayoutSchema,
136
+ targetIds: string[],
137
+ updates: Record<string, any>
138
+ ): CardLayoutSchema;
139
+
46
140
  export function buildRenderNodes(
47
141
  children: CardElement[],
48
142
  data: Record<string, any>,
@@ -55,7 +149,3 @@ export function buildRenderResult(
55
149
  data: Record<string, any>,
56
150
  unit?: 'px' | 'rpx'
57
151
  ): RenderResult;
58
-
59
- export function getTemplateItems(ids: string, items: TemplateItem[]): TemplateItem[];
60
-
61
- export function getTemplateBackgrounds(ids: string, items: TemplateBackground[]) : TemplateBackground[];