km-card-layout-core 0.1.10 → 0.1.12

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/utils.ts CHANGED
@@ -1,141 +1,9 @@
1
- import type { CardElement, CardLayoutSchema, TemplateItem } from './interface';
2
- import { TemplateBackground } from './interface/data/payload';
3
-
4
-
5
- export function backgroundChange(
6
- bg: TemplateBackground,
7
- layout: CardLayoutSchema
8
- ): CardLayoutSchema {
9
- const toNameArray = (name?: string | string[]) => {
10
- if (Array.isArray(name)) return name.filter(Boolean);
11
- if (!name) return [];
12
- return `${name}`
13
- .split(',')
14
- .map(n => n.trim())
15
- .filter(Boolean);
16
- };
17
-
18
- const applySpecialColor = (el: CardElement): CardElement => {
19
- const extras = bg.fontColorExtra || [];
20
- if (!extras.length) return el;
21
-
22
- const keys: string[] = [];
23
- if (el.binding) keys.push(String(el.binding));
24
- if (el.id) keys.push(String(el.id));
25
- if (el.type === 'icon') keys.push('icon');
26
- if (el.type === 'custom') keys.push('decor');
27
-
28
- const matched = extras.find(sc =>
29
- toNameArray(sc.name).some(n => keys.some(k => k?.startsWith(n)))
30
- );
31
- if (!matched) return el;
32
-
33
- const baseStyle = { ...(el.style || {}) };
34
- if (el.type === 'custom') {
35
- return { ...el, style: { ...baseStyle, backgroundColor: matched.color } };
36
- }
37
- return { ...el, style: { ...baseStyle, color: matched.color } };
38
- };
39
-
40
- const traverse = (children: CardElement[] = []): CardElement[] =>
41
- children.map(el => {
42
- if (!el) return el;
43
- if (el.type === 'layout-panel') {
44
- return {
45
- ...el,
46
- children: traverse(el.children || []),
47
- };
48
- }
49
- return applySpecialColor(el);
50
- });
51
-
52
- return {
53
- ...layout,
54
- backgroundImage: bg.imgUrl,
55
- fontColor: bg.fontColor || layout.fontColor,
56
- children: traverse(layout.children || []),
57
- };
58
- }
59
-
60
- export function stripLayoutBindings(
61
- layouts: CardLayoutSchema[] = []
62
- ): CardLayoutSchema[] {
63
- const targetLayouts = Array.isArray(layouts) ? layouts : [];
64
- const stripElement = (el: CardElement): CardElement => {
65
- const { binding: _b, defaultValue: _d, ...rest } = el as any;
66
- if (el.type === 'layout-panel') {
67
- return {
68
- ...rest,
69
- children: (el.children || []).map(stripElement),
70
- } as CardElement;
71
- }
72
- return rest as CardElement;
73
- };
74
-
75
- return targetLayouts.map(layout => ({
76
- ...layout,
77
- children: (layout.children || []).map(stripElement),
78
- }));
79
- }
80
-
81
- /**
82
- * 应用元素数据绑定字段
83
- * @param layouts 布局
84
- * @param items 绑定元素数据
85
- * @returns
86
- */
87
- export function applyItemCollectBindings(
88
- layouts: CardLayoutSchema[] = [],
89
- items: TemplateItem[] = []
90
- ): CardLayoutSchema[] {
91
- const targetLayouts = Array.isArray(layouts) ? layouts : [];
92
- const metaMap = new Map<string, TemplateItem>();
93
- const metaList = Array.isArray(items) ? items : [];
94
- metaList.forEach(item => {
95
- if (item && item.id !== undefined && item.id !== null) {
96
- metaMap.set(String(item.id), item);
97
- }
98
- });
99
-
100
- const assignBinding = (el: CardElement): CardElement => {
101
- const meta = metaMap.get(String(el.id));
102
- const binding =
103
- meta && meta.bind !== undefined && meta.bind !== null
104
- ? meta.bind
105
- : el.binding;
106
- const defaultValue =
107
- meta && meta.default !== undefined ? meta.default : el.defaultValue;
108
- const key = meta && meta.key !== undefined ? meta.key : el.key;
109
- const base: any = { ...el };
110
- if (binding !== undefined) base.binding = binding;
111
- else delete base.binding;
112
- if (defaultValue !== undefined) base.defaultValue = defaultValue;
113
- else delete base.defaultValue;
114
- if (key !== undefined) base.key = key;
115
- else delete base.key;
116
-
117
- if (el.type === 'layout-panel') {
118
- return {
119
- ...base,
120
- children: (el.children || []).map(assignBinding),
121
- } as CardElement;
122
- }
123
- return base as CardElement;
124
- };
125
-
126
- return targetLayouts.map(layout => ({
127
- ...layout,
128
- children: (layout.children || []).map(assignBinding),
129
- }));
130
- }
131
-
132
- export function getTemplateItems(ids: string, items: TemplateItem[]) {
133
- const idArray = ids.split(',').map(id => id.trim());
134
- return items.filter(item => idArray.includes(String(item.id)));
135
- }
136
-
137
-
138
- export function getTemplateBackgrounds(ids: string, items: TemplateBackground[]){
139
- const idArray = ids.split(',').map(id => id.trim());
140
- return items.filter(item => idArray.includes(String(item.id)));
141
- }
1
+ export * from './bindings';
2
+ export {
3
+ backgroundChange,
4
+ applySpecialStyle,
5
+ applyBackground,
6
+ updateElementsStyle,
7
+ resolveSpecialStyle,
8
+ DEFAULT_DECOR_COLOR,
9
+ } from './ops/changeBackground';