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.
@@ -0,0 +1,210 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildRenderResult = exports.buildRenderNodes = void 0;
4
+ const helpers_1 = require("../helpers");
5
+ const layout_1 = require("../layout");
6
+ const data_1 = require("../data");
7
+ const buildCardStyle = (layout, unit) => (0, helpers_1.styleObjectToString)({
8
+ width: (0, helpers_1.addUnit)(layout.width, unit),
9
+ height: (0, helpers_1.addUnit)(layout.height, unit),
10
+ color: layout.fontColor,
11
+ borderRadius: layout.borderRadius !== undefined
12
+ ? (0, helpers_1.addUnit)(layout.borderRadius, unit)
13
+ : undefined,
14
+ padding: layout.padding !== undefined ? (0, helpers_1.addUnit)(layout.padding, unit) : undefined,
15
+ position: 'relative',
16
+ overflow: 'hidden',
17
+ boxSizing: 'border-box',
18
+ backgroundColor: 'transparent',
19
+ }, unit);
20
+ const buildBackgroundStyle = (layout, unit) => (0, helpers_1.styleObjectToString)({
21
+ zIndex: layout.backgroundZIndex,
22
+ borderRadius: layout.borderRadius !== undefined
23
+ ? (0, helpers_1.addUnit)(layout.borderRadius, unit)
24
+ : undefined,
25
+ width: '100%',
26
+ height: '100%',
27
+ position: 'absolute',
28
+ left: 0,
29
+ top: 0,
30
+ }, unit);
31
+ const buildWrapperStyle = (el, unit) => {
32
+ const abs = (0, layout_1.getAbsLayout)(el);
33
+ if (!abs)
34
+ return '';
35
+ return (0, helpers_1.styleObjectToString)({
36
+ position: 'absolute',
37
+ left: (0, helpers_1.addUnit)(abs.x, unit),
38
+ top: (0, helpers_1.addUnit)(abs.y, unit),
39
+ width: (0, helpers_1.addUnit)(abs.width, unit),
40
+ height: (0, helpers_1.addUnit)(abs.height, unit),
41
+ zIndex: abs.zIndex,
42
+ boxSizing: 'border-box',
43
+ }, unit);
44
+ };
45
+ const buildPanelContentStyle = (el, unit) => (0, helpers_1.styleObjectToString)({
46
+ position: 'relative',
47
+ width: '100%',
48
+ height: '100%',
49
+ display: 'block',
50
+ boxSizing: 'border-box',
51
+ ...(el.style || {}),
52
+ }, unit);
53
+ const buildTextContentStyle = (el, unit) => {
54
+ var _a, _b, _c;
55
+ const textAlign = ((_a = el.style) === null || _a === void 0 ? void 0 : _a.textAlign) || el.align || undefined;
56
+ const style = {
57
+ ...(el.style || {}),
58
+ whiteSpace: 'pre-wrap',
59
+ wordBreak: 'break-word',
60
+ lineHeight: ((_b = el.style) === null || _b === void 0 ? void 0 : _b.lineHeight) !== undefined && ((_c = el.style) === null || _c === void 0 ? void 0 : _c.lineHeight) !== null
61
+ ? el.style.lineHeight
62
+ : '1',
63
+ };
64
+ if (textAlign)
65
+ style.textAlign = textAlign;
66
+ return (0, helpers_1.styleObjectToString)(style, unit);
67
+ };
68
+ const buildBaseContentStyle = (el, unit) => (0, helpers_1.styleObjectToString)({
69
+ ...(el.style || {}),
70
+ boxSizing: 'border-box',
71
+ }, unit);
72
+ const buildImageContentStyle = (el, unit) => {
73
+ const style = { ...(el.style || {}) };
74
+ const borderWidth = Number(style.borderWidth);
75
+ if (Number.isFinite(borderWidth) && borderWidth > 0) {
76
+ if (!style.borderStyle)
77
+ style.borderStyle = 'solid';
78
+ if (!style.borderColor)
79
+ style.borderColor = '#000000';
80
+ }
81
+ return (0, helpers_1.styleObjectToString)(style, unit);
82
+ };
83
+ const buildTextIcon = (el, unit) => {
84
+ var _a, _b, _c, _d;
85
+ const icon = el.icon;
86
+ if (!icon || icon.enable === false)
87
+ return undefined;
88
+ const style = icon.style || 'fill';
89
+ const baseName = el.key || el.binding || el.id;
90
+ let name;
91
+ if (style === 'dot')
92
+ name = 'round';
93
+ else if (style === 'line')
94
+ name = baseName ? `${baseName}-line` : undefined;
95
+ else
96
+ name = baseName || undefined;
97
+ if (!name)
98
+ return undefined;
99
+ const size = icon.size !== undefined && icon.size !== null
100
+ ? icon.size
101
+ : (_a = el.style) === null || _a === void 0 ? void 0 : _a.fontSize;
102
+ const gap = icon.gap !== undefined && icon.gap !== null ? icon.gap : 4;
103
+ const color = (_b = icon.color) !== null && _b !== void 0 ? _b : (typeof ((_c = el.style) === null || _c === void 0 ? void 0 : _c.color) === 'string' ? el.style.color : undefined);
104
+ return {
105
+ name: `${name}`,
106
+ text: `${name}`,
107
+ size: (0, helpers_1.addUnit)(size, unit),
108
+ gap: (0, helpers_1.addUnit)(gap, unit),
109
+ color: color,
110
+ align: icon.align || 'left',
111
+ wrapperStyle: (0, helpers_1.styleObjectToString)({
112
+ display: 'inline-flex',
113
+ alignItems: 'center',
114
+ height: ((_d = el.style) === null || _d === void 0 ? void 0 : _d.lineHeight) || '1',
115
+ }, unit),
116
+ };
117
+ };
118
+ const buildNode = (el, rootData, unit, context) => {
119
+ var _a, _b, _c, _d, _e, _f, _g, _h;
120
+ if (!el || el.visible === false)
121
+ return null;
122
+ const wrapperStyle = buildWrapperStyle(el, unit);
123
+ if (el.type === 'layout-panel') {
124
+ return {
125
+ id: el.id,
126
+ type: el.type,
127
+ visible: el.visible,
128
+ wrapperStyle,
129
+ contentStyle: buildPanelContentStyle(el, unit),
130
+ children: (0, exports.buildRenderNodes)(el.children || [], rootData, unit, context),
131
+ };
132
+ }
133
+ const baseStyle = buildBaseContentStyle(el, unit);
134
+ if (el.type === 'text') {
135
+ const value = (_b = (_a = (0, data_1.resolveBindingValue)(el.binding, rootData, context)) !== null && _a !== void 0 ? _a : el.defaultValue) !== null && _b !== void 0 ? _b : '';
136
+ return {
137
+ id: el.id,
138
+ type: el.type,
139
+ wrapperStyle,
140
+ contentStyle: buildTextContentStyle(el, unit),
141
+ text: `${value}`,
142
+ visible: el.visible,
143
+ icon: buildTextIcon(el, unit),
144
+ };
145
+ }
146
+ if (el.type === 'image') {
147
+ const src = (_e = (_d = (_c = (0, data_1.resolveBindingValue)(el.binding, rootData, context)) !== null && _c !== void 0 ? _c : el.defaultUrl) !== null && _d !== void 0 ? _d : el.defaultValue) !== null && _e !== void 0 ? _e : '';
148
+ const mode = el.fit === 'contain' ? 'aspectFit' : 'aspectFill';
149
+ return {
150
+ id: el.id,
151
+ type: el.type,
152
+ wrapperStyle,
153
+ contentStyle: buildImageContentStyle(el, unit),
154
+ src,
155
+ mode,
156
+ visible: el.visible,
157
+ };
158
+ }
159
+ if (el.type === 'icon') {
160
+ const name = (_h = (_g = (_f = (0, data_1.resolveBindingValue)(el.binding, rootData, context)) !== null && _f !== void 0 ? _f : el.name) !== null && _g !== void 0 ? _g : el.defaultValue) !== null && _h !== void 0 ? _h : '';
161
+ return {
162
+ id: el.id,
163
+ type: el.type,
164
+ wrapperStyle,
165
+ contentStyle: baseStyle,
166
+ name: `${name}`,
167
+ text: `${name}`,
168
+ visible: el.visible,
169
+ };
170
+ }
171
+ if (el.type === 'custom') {
172
+ return {
173
+ id: el.id,
174
+ type: el.type,
175
+ wrapperStyle,
176
+ contentStyle: baseStyle,
177
+ visible: el.visible,
178
+ };
179
+ }
180
+ return null;
181
+ };
182
+ const buildRenderNodes = (children, rootData, unit = 'px', context = {}) => {
183
+ if (!Array.isArray(children))
184
+ return [];
185
+ const nodes = [];
186
+ children.forEach(el => {
187
+ if (!el || el.visible === false)
188
+ return;
189
+ const node = buildNode(el, rootData, unit, context);
190
+ if (node)
191
+ nodes.push(node);
192
+ });
193
+ return nodes;
194
+ };
195
+ exports.buildRenderNodes = buildRenderNodes;
196
+ const buildRenderResult = (layoutInput, dataInput, unit = 'px') => {
197
+ const layouts = (0, layout_1.normalizeLayout)(layoutInput);
198
+ return layouts.map(layout => {
199
+ const cardStyle = buildCardStyle(layout, unit);
200
+ const bgStyle = buildBackgroundStyle(layout, unit);
201
+ const renderTree = (0, exports.buildRenderNodes)(layout.children || [], dataInput || {}, unit);
202
+ return {
203
+ renderTree,
204
+ cardStyle,
205
+ backgroundImage: layout.backgroundImage || '',
206
+ backgroundStyle: bgStyle,
207
+ };
208
+ });
209
+ };
210
+ exports.buildRenderResult = buildRenderResult;
package/dist/utils.js CHANGED
@@ -1,131 +1,25 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.backgroundChange = backgroundChange;
4
- exports.stripLayoutBindings = stripLayoutBindings;
5
- exports.applyItemCollectBindings = applyItemCollectBindings;
6
- exports.getTemplateItems = getTemplateItems;
7
- exports.getTemplateBackgrounds = getTemplateBackgrounds;
8
- function backgroundChange(bg, layout) {
9
- const toNameArray = (name) => {
10
- if (Array.isArray(name))
11
- return name.filter(Boolean);
12
- if (!name)
13
- return [];
14
- return `${name}`
15
- .split(',')
16
- .map(n => n.trim())
17
- .filter(Boolean);
18
- };
19
- const applySpecialColor = (el) => {
20
- const extras = bg.fontColorExtra || [];
21
- if (!extras.length)
22
- return el;
23
- const keys = [];
24
- if (el.binding)
25
- keys.push(String(el.binding));
26
- if (el.id)
27
- keys.push(String(el.id));
28
- if (el.type === 'icon')
29
- keys.push('icon');
30
- if (el.type === 'custom')
31
- keys.push('decor');
32
- const matched = extras.find(sc => toNameArray(sc.name).some(n => keys.some(k => k === null || k === void 0 ? void 0 : k.startsWith(n))));
33
- if (!matched)
34
- return el;
35
- const baseStyle = { ...(el.style || {}) };
36
- if (el.type === 'custom') {
37
- return { ...el, style: { ...baseStyle, backgroundColor: matched.color } };
38
- }
39
- return { ...el, style: { ...baseStyle, color: matched.color } };
40
- };
41
- const traverse = (children = []) => children.map(el => {
42
- if (!el)
43
- return el;
44
- if (el.type === 'layout-panel') {
45
- return {
46
- ...el,
47
- children: traverse(el.children || []),
48
- };
49
- }
50
- return applySpecialColor(el);
51
- });
52
- return {
53
- ...layout,
54
- backgroundImage: bg.imgUrl,
55
- fontColor: bg.fontColor || layout.fontColor,
56
- children: traverse(layout.children || []),
57
- };
58
- }
59
- function stripLayoutBindings(layouts = []) {
60
- const targetLayouts = Array.isArray(layouts) ? layouts : [];
61
- const stripElement = (el) => {
62
- const { binding: _b, defaultValue: _d, ...rest } = el;
63
- if (el.type === 'layout-panel') {
64
- return {
65
- ...rest,
66
- children: (el.children || []).map(stripElement),
67
- };
68
- }
69
- return rest;
70
- };
71
- return targetLayouts.map(layout => ({
72
- ...layout,
73
- children: (layout.children || []).map(stripElement),
74
- }));
75
- }
76
- /**
77
- * 应用元素数据绑定字段
78
- * @param layouts 布局
79
- * @param items 绑定元素数据
80
- * @returns
81
- */
82
- function applyItemCollectBindings(layouts = [], items = []) {
83
- const targetLayouts = Array.isArray(layouts) ? layouts : [];
84
- const metaMap = new Map();
85
- const metaList = Array.isArray(items) ? items : [];
86
- metaList.forEach(item => {
87
- if (item && item.id !== undefined && item.id !== null) {
88
- metaMap.set(String(item.id), item);
89
- }
90
- });
91
- const assignBinding = (el) => {
92
- const meta = metaMap.get(String(el.id));
93
- const binding = meta && meta.bind !== undefined && meta.bind !== null
94
- ? meta.bind
95
- : el.binding;
96
- const defaultValue = meta && meta.default !== undefined ? meta.default : el.defaultValue;
97
- const key = meta && meta.key !== undefined ? meta.key : el.key;
98
- const base = { ...el };
99
- if (binding !== undefined)
100
- base.binding = binding;
101
- else
102
- delete base.binding;
103
- if (defaultValue !== undefined)
104
- base.defaultValue = defaultValue;
105
- else
106
- delete base.defaultValue;
107
- if (key !== undefined)
108
- base.key = key;
109
- else
110
- delete base.key;
111
- if (el.type === 'layout-panel') {
112
- return {
113
- ...base,
114
- children: (el.children || []).map(assignBinding),
115
- };
116
- }
117
- return base;
118
- };
119
- return targetLayouts.map(layout => ({
120
- ...layout,
121
- children: (layout.children || []).map(assignBinding),
122
- }));
123
- }
124
- function getTemplateItems(ids, items) {
125
- const idArray = ids.split(',').map(id => id.trim());
126
- return items.filter(item => idArray.includes(String(item.id)));
127
- }
128
- function getTemplateBackgrounds(ids, items) {
129
- const idArray = ids.split(',').map(id => id.trim());
130
- return items.filter(item => idArray.includes(String(item.id)));
131
- }
17
+ exports.DEFAULT_DECOR_COLOR = exports.resolveSpecialStyle = exports.updateElementsStyle = exports.applyBackground = exports.applySpecialStyle = exports.backgroundChange = void 0;
18
+ __exportStar(require("./bindings"), exports);
19
+ var changeBackground_1 = require("./ops/changeBackground");
20
+ Object.defineProperty(exports, "backgroundChange", { enumerable: true, get: function () { return changeBackground_1.backgroundChange; } });
21
+ Object.defineProperty(exports, "applySpecialStyle", { enumerable: true, get: function () { return changeBackground_1.applySpecialStyle; } });
22
+ Object.defineProperty(exports, "applyBackground", { enumerable: true, get: function () { return changeBackground_1.applyBackground; } });
23
+ Object.defineProperty(exports, "updateElementsStyle", { enumerable: true, get: function () { return changeBackground_1.updateElementsStyle; } });
24
+ Object.defineProperty(exports, "resolveSpecialStyle", { enumerable: true, get: function () { return changeBackground_1.resolveSpecialStyle; } });
25
+ Object.defineProperty(exports, "DEFAULT_DECOR_COLOR", { enumerable: true, get: function () { return changeBackground_1.DEFAULT_DECOR_COLOR; } });
package/helpers.ts ADDED
@@ -0,0 +1,76 @@
1
+ const DIMENSION_PROPS = new Set([
2
+ 'width',
3
+ 'height',
4
+ 'top',
5
+ 'right',
6
+ 'bottom',
7
+ 'left',
8
+ 'padding',
9
+ 'paddingTop',
10
+ 'paddingBottom',
11
+ 'paddingLeft',
12
+ 'paddingRight',
13
+ 'margin',
14
+ 'marginTop',
15
+ 'marginBottom',
16
+ 'marginLeft',
17
+ 'marginRight',
18
+ 'fontSize',
19
+ 'lineHeight',
20
+ 'borderRadius',
21
+ 'borderWidth',
22
+ 'letterSpacing',
23
+ 'gap',
24
+ 'rowGap',
25
+ 'columnGap',
26
+ ]);
27
+
28
+ export const toNumber = (value: unknown): number | undefined => {
29
+ const num = Number(value);
30
+ return Number.isFinite(num) ? num : undefined;
31
+ };
32
+
33
+ const toKebab = (key: string): string =>
34
+ key.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
35
+
36
+ export const addUnit = (
37
+ value: string | number | undefined | null,
38
+ unit: 'px' | 'rpx'
39
+ ): string | undefined => {
40
+ if (value === undefined || value === null || value === '') return undefined;
41
+ if (typeof value === 'number') {
42
+ const ratio = unit === 'rpx' ? 2 : 1;
43
+ return `${value * ratio}${unit}`;
44
+ }
45
+ if (typeof value === 'string') {
46
+ const parsed = Number(value);
47
+ if (Number.isFinite(parsed)) {
48
+ const ratio = unit === 'rpx' ? 2 : 1;
49
+ return `${parsed * ratio}${unit}`;
50
+ }
51
+ }
52
+ return `${value}`;
53
+ };
54
+
55
+ export const styleObjectToString = (
56
+ style?: Record<string, any>,
57
+ unit: 'px' | 'rpx' = 'px'
58
+ ): string => {
59
+ if (!style) return '';
60
+ const pairs: string[] = [];
61
+ Object.keys(style).forEach(key => {
62
+ const value = style[key];
63
+ if (value === undefined || value === null || value === '') return;
64
+ const useUnit = DIMENSION_PROPS.has(key)
65
+ ? addUnit(value as any, unit)
66
+ : value;
67
+ if (useUnit === undefined || useUnit === null || useUnit === '') return;
68
+ pairs.push(`${toKebab(key)}:${useUnit}`);
69
+ });
70
+ return pairs.join(';');
71
+ };
72
+
73
+ export const isObject = (
74
+ val: unknown
75
+ ): val is Record<string, any> | any[] =>
76
+ Boolean(val) && typeof val === 'object';