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/bindings.ts +84 -0
- package/data.ts +38 -0
- package/dist/bindings.js +73 -0
- package/dist/data.js +38 -0
- package/dist/helpers.js +72 -0
- package/dist/index.js +15 -394
- package/dist/layout.js +117 -0
- package/dist/ops/changeBackground.js +135 -0
- package/dist/render/builder.js +210 -0
- package/dist/utils.js +23 -129
- package/helpers.ts +76 -0
- package/index.ts +21 -513
- package/layout.ts +129 -0
- package/ops/changeBackground.ts +169 -0
- package/package.json +1 -1
- package/render/builder.ts +288 -0
- package/types.d.ts +97 -7
- package/utils.ts +9 -141
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* CardMaster 布局 JSON
|
|
3
|
+
* CardMaster 布局 JSON 渲染核心
|
|
4
4
|
*
|
|
5
5
|
* - 平台无关:不依赖 DOM/小程序 API,方便在 Web/小程序/Node 复用。
|
|
6
|
-
* - 职责:将布局 Schema
|
|
6
|
+
* - 职责:将布局 Schema 与业务数据合成可渲染树,外层只需将节点映射到各端组件。
|
|
7
7
|
*/
|
|
8
8
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
9
|
if (k2 === undefined) k2 = k;
|
|
@@ -20,396 +20,17 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
20
20
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.
|
|
23
|
+
exports.updateElementsStyle = exports.applyBackground = exports.applySpecialStyle = exports.resolveSpecialStyle = exports.DEFAULT_DECOR_COLOR = exports.backgroundChange = void 0;
|
|
24
24
|
__exportStar(require("./interface/index"), exports);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
'paddingBottom',
|
|
38
|
-
'paddingLeft',
|
|
39
|
-
'paddingRight',
|
|
40
|
-
'margin',
|
|
41
|
-
'marginTop',
|
|
42
|
-
'marginBottom',
|
|
43
|
-
'marginLeft',
|
|
44
|
-
'marginRight',
|
|
45
|
-
'fontSize',
|
|
46
|
-
'lineHeight',
|
|
47
|
-
'borderRadius',
|
|
48
|
-
'borderWidth',
|
|
49
|
-
'letterSpacing',
|
|
50
|
-
'gap',
|
|
51
|
-
'rowGap',
|
|
52
|
-
'columnGap',
|
|
53
|
-
]);
|
|
54
|
-
/** ---------- 基础工具 ---------- */
|
|
55
|
-
/**
|
|
56
|
-
* 安全转 number,非数值或 NaN 返回 undefined。
|
|
57
|
-
*/
|
|
58
|
-
const toNumber = (value) => {
|
|
59
|
-
const num = Number(value);
|
|
60
|
-
return Number.isFinite(num) ? num : undefined;
|
|
61
|
-
};
|
|
62
|
-
/**
|
|
63
|
-
* camelCase 转 kebab-case(用于内联样式 key)。
|
|
64
|
-
*/
|
|
65
|
-
const toKebab = (key) => key.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
|
|
66
|
-
/**
|
|
67
|
-
* 数字追加单位,非数字原样转字符串。
|
|
68
|
-
* 数字追加单位,非数字原样转字符串。
|
|
69
|
-
*/
|
|
70
|
-
const addUnit = (value, unit) => {
|
|
71
|
-
if (value === undefined || value === null || value === '')
|
|
72
|
-
return undefined;
|
|
73
|
-
if (typeof value === 'number') {
|
|
74
|
-
const ratio = unit === 'rpx' ? 2 : 1;
|
|
75
|
-
return `${value * ratio}${unit}`;
|
|
76
|
-
}
|
|
77
|
-
if (typeof value === 'string') {
|
|
78
|
-
const parsed = Number(value);
|
|
79
|
-
if (Number.isFinite(parsed)) {
|
|
80
|
-
const ratio = unit === 'rpx' ? 2 : 1;
|
|
81
|
-
return `${parsed * ratio}${unit}`;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return `${value}`;
|
|
85
|
-
};
|
|
86
|
-
exports.addUnit = addUnit;
|
|
87
|
-
/**
|
|
88
|
-
* 样式对象转内联样式字符串,对需要单位的字段自动补单位。
|
|
89
|
-
*/
|
|
90
|
-
const styleObjectToString = (style, unit = 'px') => {
|
|
91
|
-
if (!style)
|
|
92
|
-
return '';
|
|
93
|
-
const pairs = [];
|
|
94
|
-
Object.keys(style).forEach(key => {
|
|
95
|
-
const value = style[key];
|
|
96
|
-
if (value === undefined || value === null || value === '')
|
|
97
|
-
return;
|
|
98
|
-
const useUnit = DIMENSION_PROPS.has(key)
|
|
99
|
-
? (0, exports.addUnit)(value, unit)
|
|
100
|
-
: value;
|
|
101
|
-
if (useUnit === undefined || useUnit === null || useUnit === '')
|
|
102
|
-
return;
|
|
103
|
-
pairs.push(`${toKebab(key)}:${useUnit}`);
|
|
104
|
-
});
|
|
105
|
-
return pairs.join(';');
|
|
106
|
-
};
|
|
107
|
-
exports.styleObjectToString = styleObjectToString;
|
|
108
|
-
/**
|
|
109
|
-
* 容错 JSON 解析:字符串用 JSON.parse,其他保持原样或返回 null。
|
|
110
|
-
*/
|
|
111
|
-
const parseJson = (input) => {
|
|
112
|
-
if (typeof input !== 'string')
|
|
113
|
-
return input !== null && input !== void 0 ? input : null;
|
|
114
|
-
try {
|
|
115
|
-
return JSON.parse(input);
|
|
116
|
-
}
|
|
117
|
-
catch (err) {
|
|
118
|
-
console.warn('[km-card-layout-core] JSON parse failed', err);
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
const isObject = (val) => Boolean(val) && typeof val === 'object';
|
|
123
|
-
const getAbsLayout = (el) => el.layout && el.layout.mode === 'absolute'
|
|
124
|
-
? el.layout
|
|
125
|
-
: null;
|
|
126
|
-
/** ---------- 布局与数据解析 ---------- */
|
|
127
|
-
/**
|
|
128
|
-
* 归一化布局输入(对象或 JSON 字符串),补齐宽高/容器/children 默认值。
|
|
129
|
-
*/
|
|
130
|
-
const normalizeLayout = (layout) => {
|
|
131
|
-
if (!Array.isArray(layout))
|
|
132
|
-
return [];
|
|
133
|
-
return layout
|
|
134
|
-
.map(item => {
|
|
135
|
-
var _a, _b;
|
|
136
|
-
if (!item || typeof item !== 'object')
|
|
137
|
-
return null;
|
|
138
|
-
const parsed = item;
|
|
139
|
-
return {
|
|
140
|
-
...parsed,
|
|
141
|
-
width: (_a = toNumber(parsed.width)) !== null && _a !== void 0 ? _a : DEFAULT_CARD_WIDTH,
|
|
142
|
-
height: (_b = toNumber(parsed.height)) !== null && _b !== void 0 ? _b : DEFAULT_CARD_HEIGHT,
|
|
143
|
-
container: parsed.container || { mode: 'absolute' },
|
|
144
|
-
children: Array.isArray(parsed.children)
|
|
145
|
-
? parsed.children
|
|
146
|
-
: [],
|
|
147
|
-
};
|
|
148
|
-
})
|
|
149
|
-
.filter((v) => Boolean(v));
|
|
150
|
-
};
|
|
151
|
-
exports.normalizeLayout = normalizeLayout;
|
|
152
|
-
/**
|
|
153
|
-
* 将绑定路径拆分为片段,支持点语法与数组下标。
|
|
154
|
-
*/
|
|
155
|
-
const pathToSegments = (path) => `${path || ''}`
|
|
156
|
-
.replace(/\[(\d+)\]/g, '.$1')
|
|
157
|
-
.split('.')
|
|
158
|
-
.map(p => p.trim())
|
|
159
|
-
.filter(Boolean);
|
|
160
|
-
/**
|
|
161
|
-
* 按路径访问对象/数组,缺失时返回 undefined/null。
|
|
162
|
-
*/
|
|
163
|
-
const readByPath = (data, path) => {
|
|
164
|
-
if (path === undefined || path === null || path === '')
|
|
165
|
-
return data;
|
|
166
|
-
const segments = pathToSegments(path);
|
|
167
|
-
let cursor = data;
|
|
168
|
-
for (let i = 0; i < segments.length; i += 1) {
|
|
169
|
-
if (!isObject(cursor) && !Array.isArray(cursor))
|
|
170
|
-
return undefined;
|
|
171
|
-
const key = segments[i];
|
|
172
|
-
if (Array.isArray(cursor)) {
|
|
173
|
-
const idx = Number(key);
|
|
174
|
-
cursor = Number.isNaN(idx) ? undefined : cursor[idx];
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
cursor = cursor[key];
|
|
178
|
-
}
|
|
179
|
-
if (cursor === undefined || cursor === null) {
|
|
180
|
-
return cursor;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return cursor;
|
|
184
|
-
};
|
|
185
|
-
/**
|
|
186
|
-
* Resolve element binding against provided data.
|
|
187
|
-
*/
|
|
188
|
-
const resolveBindingValue = (binding, rootData, context) => {
|
|
189
|
-
if (!binding)
|
|
190
|
-
return undefined;
|
|
191
|
-
const value = readByPath(rootData, binding);
|
|
192
|
-
return value === undefined ? undefined : value;
|
|
193
|
-
};
|
|
194
|
-
exports.resolveBindingValue = resolveBindingValue;
|
|
195
|
-
/** ---------- 样式构建 ---------- */
|
|
196
|
-
const buildCardStyle = (layout, unit) => (0, exports.styleObjectToString)({
|
|
197
|
-
width: (0, exports.addUnit)(layout.width, unit),
|
|
198
|
-
height: (0, exports.addUnit)(layout.height, unit),
|
|
199
|
-
color: layout.fontColor,
|
|
200
|
-
borderRadius: layout.borderRadius !== undefined
|
|
201
|
-
? (0, exports.addUnit)(layout.borderRadius, unit)
|
|
202
|
-
: undefined,
|
|
203
|
-
padding: layout.padding !== undefined ? (0, exports.addUnit)(layout.padding, unit) : undefined,
|
|
204
|
-
position: 'relative',
|
|
205
|
-
overflow: 'hidden',
|
|
206
|
-
boxSizing: 'border-box',
|
|
207
|
-
backgroundColor: 'transparent',
|
|
208
|
-
}, unit);
|
|
209
|
-
const buildBackgroundStyle = (layout, unit) => (0, exports.styleObjectToString)({
|
|
210
|
-
zIndex: layout.backgroundZIndex,
|
|
211
|
-
borderRadius: layout.borderRadius !== undefined
|
|
212
|
-
? (0, exports.addUnit)(layout.borderRadius, unit)
|
|
213
|
-
: undefined,
|
|
214
|
-
width: '100%',
|
|
215
|
-
height: '100%',
|
|
216
|
-
position: 'absolute',
|
|
217
|
-
left: 0,
|
|
218
|
-
top: 0,
|
|
219
|
-
}, unit);
|
|
220
|
-
/**
|
|
221
|
-
* 生成元素外层样式(绝对/弹性布局),始终返回内联样式字符串。
|
|
222
|
-
*/
|
|
223
|
-
const buildWrapperStyle = (el, unit) => {
|
|
224
|
-
const abs = getAbsLayout(el);
|
|
225
|
-
if (!abs)
|
|
226
|
-
return '';
|
|
227
|
-
return (0, exports.styleObjectToString)({
|
|
228
|
-
position: 'absolute',
|
|
229
|
-
left: (0, exports.addUnit)(abs.x, unit),
|
|
230
|
-
top: (0, exports.addUnit)(abs.y, unit),
|
|
231
|
-
width: (0, exports.addUnit)(abs.width, unit),
|
|
232
|
-
height: (0, exports.addUnit)(abs.height, unit),
|
|
233
|
-
zIndex: abs.zIndex,
|
|
234
|
-
boxSizing: 'border-box',
|
|
235
|
-
}, unit);
|
|
236
|
-
};
|
|
237
|
-
const buildPanelContentStyle = (el, unit) => (0, exports.styleObjectToString)({
|
|
238
|
-
position: 'relative',
|
|
239
|
-
width: '100%',
|
|
240
|
-
height: '100%',
|
|
241
|
-
display: 'block',
|
|
242
|
-
boxSizing: 'border-box',
|
|
243
|
-
...(el.style || {}),
|
|
244
|
-
}, unit);
|
|
245
|
-
const buildTextContentStyle = (el, unit) => {
|
|
246
|
-
var _a, _b, _c;
|
|
247
|
-
const textAlign = ((_a = el.style) === null || _a === void 0 ? void 0 : _a.textAlign) || el.align || undefined;
|
|
248
|
-
const style = {
|
|
249
|
-
...(el.style || {}),
|
|
250
|
-
whiteSpace: 'pre-wrap',
|
|
251
|
-
wordBreak: 'break-word',
|
|
252
|
-
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
|
|
253
|
-
? el.style.lineHeight
|
|
254
|
-
: '1.2',
|
|
255
|
-
display: 'inline-flex',
|
|
256
|
-
alignItems: 'center',
|
|
257
|
-
};
|
|
258
|
-
if (textAlign)
|
|
259
|
-
style.textAlign = textAlign;
|
|
260
|
-
return (0, exports.styleObjectToString)(style, unit);
|
|
261
|
-
};
|
|
262
|
-
const buildBaseContentStyle = (el, unit) => (0, exports.styleObjectToString)({
|
|
263
|
-
...(el.style || {}),
|
|
264
|
-
boxSizing: 'border-box',
|
|
265
|
-
}, unit);
|
|
266
|
-
const buildImageContentStyle = (el, unit) => {
|
|
267
|
-
const style = { ...(el.style || {}) };
|
|
268
|
-
const borderWidth = Number(style.borderWidth);
|
|
269
|
-
if (Number.isFinite(borderWidth) && borderWidth > 0) {
|
|
270
|
-
if (!style.borderStyle)
|
|
271
|
-
style.borderStyle = 'solid';
|
|
272
|
-
if (!style.borderColor)
|
|
273
|
-
style.borderColor = '#000000';
|
|
274
|
-
}
|
|
275
|
-
return (0, exports.styleObjectToString)(style, unit);
|
|
276
|
-
};
|
|
277
|
-
const buildTextIcon = (el, unit) => {
|
|
278
|
-
var _a, _b, _c, _d;
|
|
279
|
-
const icon = el.icon;
|
|
280
|
-
if (!icon || icon.enable === false)
|
|
281
|
-
return undefined;
|
|
282
|
-
const style = icon.style || 'fill';
|
|
283
|
-
const baseName = el.key || el.binding || el.id;
|
|
284
|
-
let name;
|
|
285
|
-
if (style === 'dot')
|
|
286
|
-
name = 'round';
|
|
287
|
-
else if (style === 'line')
|
|
288
|
-
name = baseName ? `${baseName}-line` : undefined;
|
|
289
|
-
else
|
|
290
|
-
name = baseName || undefined;
|
|
291
|
-
if (!name)
|
|
292
|
-
return undefined;
|
|
293
|
-
const size = icon.size !== undefined && icon.size !== null
|
|
294
|
-
? icon.size
|
|
295
|
-
: (_a = el.style) === null || _a === void 0 ? void 0 : _a.fontSize;
|
|
296
|
-
const gap = icon.gap !== undefined && icon.gap !== null ? icon.gap : 4;
|
|
297
|
-
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);
|
|
298
|
-
return {
|
|
299
|
-
name: `${name}`,
|
|
300
|
-
text: `${name}`,
|
|
301
|
-
size: (0, exports.addUnit)(size, unit),
|
|
302
|
-
gap: (0, exports.addUnit)(gap, unit),
|
|
303
|
-
color: color,
|
|
304
|
-
align: icon.align || 'left',
|
|
305
|
-
wrapperStyle: (0, exports.styleObjectToString)({
|
|
306
|
-
display: 'inline-flex',
|
|
307
|
-
alignItems: 'center',
|
|
308
|
-
height: ((_d = el.style) === null || _d === void 0 ? void 0 : _d.lineHeight) || 'auto',
|
|
309
|
-
}, unit),
|
|
310
|
-
};
|
|
311
|
-
};
|
|
312
|
-
/** ---------- 渲染树构建 ---------- */
|
|
313
|
-
/**
|
|
314
|
-
* 将 children 展开为渲染节点。
|
|
315
|
-
*/
|
|
316
|
-
const buildRenderNodes = (children, rootData, unit = 'px', context = {}) => {
|
|
317
|
-
if (!Array.isArray(children))
|
|
318
|
-
return [];
|
|
319
|
-
const nodes = [];
|
|
320
|
-
children.forEach(el => {
|
|
321
|
-
if (!el || el.visible === false)
|
|
322
|
-
return;
|
|
323
|
-
const node = buildNode(el, rootData, unit, context);
|
|
324
|
-
if (node)
|
|
325
|
-
nodes.push(node);
|
|
326
|
-
});
|
|
327
|
-
return nodes;
|
|
328
|
-
};
|
|
329
|
-
exports.buildRenderNodes = buildRenderNodes;
|
|
330
|
-
/**
|
|
331
|
-
* 构建单个渲染节点(layout-panel 递归处理子元素)。
|
|
332
|
-
*/
|
|
333
|
-
const buildNode = (el, rootData, unit, context) => {
|
|
334
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
335
|
-
if (!el || el.visible === false)
|
|
336
|
-
return null;
|
|
337
|
-
const wrapperStyle = buildWrapperStyle(el, unit);
|
|
338
|
-
if (el.type === 'layout-panel') {
|
|
339
|
-
return {
|
|
340
|
-
id: el.id,
|
|
341
|
-
type: el.type,
|
|
342
|
-
visible: el.visible,
|
|
343
|
-
wrapperStyle,
|
|
344
|
-
contentStyle: buildPanelContentStyle(el, unit),
|
|
345
|
-
children: (0, exports.buildRenderNodes)(el.children || [], rootData, unit, context),
|
|
346
|
-
};
|
|
347
|
-
}
|
|
348
|
-
const baseStyle = buildBaseContentStyle(el, unit);
|
|
349
|
-
if (el.type === 'text') {
|
|
350
|
-
const value = (_b = (_a = (0, exports.resolveBindingValue)(el.binding, rootData, context)) !== null && _a !== void 0 ? _a : el.defaultValue) !== null && _b !== void 0 ? _b : '';
|
|
351
|
-
return {
|
|
352
|
-
id: el.id,
|
|
353
|
-
type: el.type,
|
|
354
|
-
wrapperStyle,
|
|
355
|
-
contentStyle: buildTextContentStyle(el, unit),
|
|
356
|
-
text: `${value}`,
|
|
357
|
-
visible: el.visible,
|
|
358
|
-
icon: buildTextIcon(el, unit),
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
if (el.type === 'image') {
|
|
362
|
-
const src = (_e = (_d = (_c = (0, exports.resolveBindingValue)(el.binding, rootData, context)) !== null && _c !== void 0 ? _c : el.defaultUrl) !== null && _d !== void 0 ? _d : el.defaultValue) !== null && _e !== void 0 ? _e : '';
|
|
363
|
-
const mode = el.fit === 'contain' ? 'aspectFit' : 'aspectFill';
|
|
364
|
-
return {
|
|
365
|
-
id: el.id,
|
|
366
|
-
type: el.type,
|
|
367
|
-
wrapperStyle,
|
|
368
|
-
contentStyle: buildImageContentStyle(el, unit),
|
|
369
|
-
src,
|
|
370
|
-
mode,
|
|
371
|
-
visible: el.visible,
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
if (el.type === 'icon') {
|
|
375
|
-
const name = (_h = (_g = (_f = (0, exports.resolveBindingValue)(el.binding, rootData, context)) !== null && _f !== void 0 ? _f : el.name) !== null && _g !== void 0 ? _g : el.defaultValue) !== null && _h !== void 0 ? _h : '';
|
|
376
|
-
return {
|
|
377
|
-
id: el.id,
|
|
378
|
-
type: el.type,
|
|
379
|
-
wrapperStyle,
|
|
380
|
-
contentStyle: baseStyle,
|
|
381
|
-
name: `${name}`,
|
|
382
|
-
text: `${name}`,
|
|
383
|
-
visible: el.visible,
|
|
384
|
-
};
|
|
385
|
-
}
|
|
386
|
-
if (el.type === 'custom') {
|
|
387
|
-
return {
|
|
388
|
-
id: el.id,
|
|
389
|
-
type: el.type,
|
|
390
|
-
wrapperStyle,
|
|
391
|
-
contentStyle: baseStyle,
|
|
392
|
-
visible: el.visible,
|
|
393
|
-
};
|
|
394
|
-
}
|
|
395
|
-
return null;
|
|
396
|
-
};
|
|
397
|
-
/**
|
|
398
|
-
* 主入口:合并布局 Schema 与数据,生成供前端使用的渲染结果。
|
|
399
|
-
*/
|
|
400
|
-
const buildRenderResult = (layoutInput, dataInput, unit = 'px') => {
|
|
401
|
-
const layouts = (0, exports.normalizeLayout)(layoutInput);
|
|
402
|
-
return layouts.map(layout => {
|
|
403
|
-
const cardStyle = buildCardStyle(layout, unit);
|
|
404
|
-
const bgStyle = buildBackgroundStyle(layout, unit);
|
|
405
|
-
const renderTree = (0, exports.buildRenderNodes)(layout.children || [], dataInput || {}, unit);
|
|
406
|
-
return {
|
|
407
|
-
renderTree,
|
|
408
|
-
cardStyle,
|
|
409
|
-
backgroundImage: layout.backgroundImage || '',
|
|
410
|
-
backgroundStyle: bgStyle,
|
|
411
|
-
};
|
|
412
|
-
});
|
|
413
|
-
};
|
|
414
|
-
exports.buildRenderResult = buildRenderResult;
|
|
415
|
-
__exportStar(require("./utils"), exports);
|
|
25
|
+
__exportStar(require("./helpers"), exports);
|
|
26
|
+
__exportStar(require("./layout"), exports);
|
|
27
|
+
__exportStar(require("./data"), exports);
|
|
28
|
+
__exportStar(require("./bindings"), exports);
|
|
29
|
+
var changeBackground_1 = require("./ops/changeBackground");
|
|
30
|
+
Object.defineProperty(exports, "backgroundChange", { enumerable: true, get: function () { return changeBackground_1.backgroundChange; } });
|
|
31
|
+
Object.defineProperty(exports, "DEFAULT_DECOR_COLOR", { enumerable: true, get: function () { return changeBackground_1.DEFAULT_DECOR_COLOR; } });
|
|
32
|
+
Object.defineProperty(exports, "resolveSpecialStyle", { enumerable: true, get: function () { return changeBackground_1.resolveSpecialStyle; } });
|
|
33
|
+
Object.defineProperty(exports, "applySpecialStyle", { enumerable: true, get: function () { return changeBackground_1.applySpecialStyle; } });
|
|
34
|
+
Object.defineProperty(exports, "applyBackground", { enumerable: true, get: function () { return changeBackground_1.applyBackground; } });
|
|
35
|
+
Object.defineProperty(exports, "updateElementsStyle", { enumerable: true, get: function () { return changeBackground_1.updateElementsStyle; } });
|
|
36
|
+
__exportStar(require("./render/builder"), exports);
|
package/dist/layout.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
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;
|
|
4
|
+
const helpers_1 = require("./helpers");
|
|
5
|
+
const DEFAULT_CARD_WIDTH = 343;
|
|
6
|
+
const DEFAULT_CARD_HEIGHT = 210;
|
|
7
|
+
const roundToInt = (value) => Number.isFinite(value) ? Math.round(value) : value;
|
|
8
|
+
exports.roundToInt = roundToInt;
|
|
9
|
+
const getAbsLayout = (el) => el.layout && el.layout.mode === 'absolute'
|
|
10
|
+
? el.layout
|
|
11
|
+
: null;
|
|
12
|
+
exports.getAbsLayout = getAbsLayout;
|
|
13
|
+
const roundAbsLayout = (layout) => ({
|
|
14
|
+
...layout,
|
|
15
|
+
x: (0, exports.roundToInt)(layout.x),
|
|
16
|
+
y: (0, exports.roundToInt)(layout.y),
|
|
17
|
+
width: (0, exports.roundToInt)(layout.width),
|
|
18
|
+
height: (0, exports.roundToInt)(layout.height),
|
|
19
|
+
zIndex: layout.zIndex !== undefined ? (0, exports.roundToInt)(layout.zIndex) : layout.zIndex,
|
|
20
|
+
});
|
|
21
|
+
const sanitizeElement = (el) => {
|
|
22
|
+
const layout = (0, exports.getAbsLayout)(el);
|
|
23
|
+
const base = layout ? { ...el, layout: roundAbsLayout(layout) } : el;
|
|
24
|
+
if (el.type === 'layout-panel' && el.children) {
|
|
25
|
+
return {
|
|
26
|
+
...base,
|
|
27
|
+
children: el.children.map(child => (0, exports.sanitizeElement)(child)),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
return base;
|
|
31
|
+
};
|
|
32
|
+
exports.sanitizeElement = sanitizeElement;
|
|
33
|
+
const sanitizeLayout = (layout) => ({
|
|
34
|
+
...layout,
|
|
35
|
+
width: (0, exports.roundToInt)(layout.width),
|
|
36
|
+
height: (0, exports.roundToInt)(layout.height),
|
|
37
|
+
backgroundZIndex: layout.backgroundZIndex !== undefined
|
|
38
|
+
? (0, exports.roundToInt)(layout.backgroundZIndex)
|
|
39
|
+
: layout.backgroundZIndex,
|
|
40
|
+
children: (layout.children || []).map(child => (0, exports.sanitizeElement)(child)),
|
|
41
|
+
});
|
|
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
|
+
const normalizeId = (id) => {
|
|
56
|
+
if (id === undefined || id === null)
|
|
57
|
+
return undefined;
|
|
58
|
+
const num = Number(id);
|
|
59
|
+
return Number.isFinite(num) ? num : id;
|
|
60
|
+
};
|
|
61
|
+
exports.normalizeId = normalizeId;
|
|
62
|
+
const safeParseJson = (input) => {
|
|
63
|
+
try {
|
|
64
|
+
return JSON.parse(input);
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
console.warn('[km-card-layout-core] parseLayout failed', err);
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const parseSingleLayout = (value) => {
|
|
72
|
+
if (!value)
|
|
73
|
+
return null;
|
|
74
|
+
if (Array.isArray(value))
|
|
75
|
+
return parseSingleLayout(value[0]);
|
|
76
|
+
if (typeof value === 'string') {
|
|
77
|
+
return parseSingleLayout(safeParseJson(value));
|
|
78
|
+
}
|
|
79
|
+
if ((0, helpers_1.isObject)(value)) {
|
|
80
|
+
const content = value.content;
|
|
81
|
+
if (content !== undefined) {
|
|
82
|
+
const nested = parseSingleLayout(content);
|
|
83
|
+
if (nested)
|
|
84
|
+
return nested;
|
|
85
|
+
}
|
|
86
|
+
return (0, exports.sanitizeLayout)(value);
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
};
|
|
90
|
+
const parseLayout = (input) => parseSingleLayout(input);
|
|
91
|
+
exports.parseLayout = parseLayout;
|
|
92
|
+
const normalizeLayout = (layout) => {
|
|
93
|
+
if (!Array.isArray(layout))
|
|
94
|
+
return [];
|
|
95
|
+
return layout
|
|
96
|
+
.map(item => {
|
|
97
|
+
var _a, _b;
|
|
98
|
+
if (!item || typeof item !== 'object')
|
|
99
|
+
return null;
|
|
100
|
+
const parsed = item;
|
|
101
|
+
return {
|
|
102
|
+
...parsed,
|
|
103
|
+
width: (_a = (0, helpers_1.toNumber)(parsed.width)) !== null && _a !== void 0 ? _a : DEFAULT_CARD_WIDTH,
|
|
104
|
+
height: (_b = (0, helpers_1.toNumber)(parsed.height)) !== null && _b !== void 0 ? _b : DEFAULT_CARD_HEIGHT,
|
|
105
|
+
container: parsed.container || { mode: 'absolute' },
|
|
106
|
+
children: Array.isArray(parsed.children)
|
|
107
|
+
? parsed.children
|
|
108
|
+
: [],
|
|
109
|
+
};
|
|
110
|
+
})
|
|
111
|
+
.filter((value) => Boolean(value));
|
|
112
|
+
};
|
|
113
|
+
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;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.updateElementsStyle = exports.applyBackground = exports.applySpecialStyle = exports.resolveSpecialStyle = exports.DEFAULT_DECOR_COLOR = void 0;
|
|
4
|
+
exports.backgroundChange = backgroundChange;
|
|
5
|
+
const toNameArray = (name) => {
|
|
6
|
+
if (Array.isArray(name))
|
|
7
|
+
return name.filter(Boolean);
|
|
8
|
+
if (!name)
|
|
9
|
+
return [];
|
|
10
|
+
return `${name}`
|
|
11
|
+
.split(',')
|
|
12
|
+
.map(n => n.trim())
|
|
13
|
+
.filter(Boolean);
|
|
14
|
+
};
|
|
15
|
+
function backgroundChange(bg, layout) {
|
|
16
|
+
const applySpecialColor = (el) => {
|
|
17
|
+
const extras = bg.fontColorExtra || [];
|
|
18
|
+
if (!extras.length)
|
|
19
|
+
return el;
|
|
20
|
+
const keys = [];
|
|
21
|
+
if (el.binding)
|
|
22
|
+
keys.push(String(el.binding));
|
|
23
|
+
if (el.id)
|
|
24
|
+
keys.push(String(el.id));
|
|
25
|
+
if (el.type === 'icon')
|
|
26
|
+
keys.push('icon');
|
|
27
|
+
if (el.type === 'custom')
|
|
28
|
+
keys.push('decor');
|
|
29
|
+
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
|
+
const baseStyle = { ...(el.style || {}) };
|
|
33
|
+
if (el.type === 'custom') {
|
|
34
|
+
return { ...el, style: { ...baseStyle, backgroundColor: matched.color } };
|
|
35
|
+
}
|
|
36
|
+
return { ...el, style: { ...baseStyle, color: matched.color } };
|
|
37
|
+
};
|
|
38
|
+
const traverse = (children = []) => children.map(el => {
|
|
39
|
+
if (!el)
|
|
40
|
+
return el;
|
|
41
|
+
if (el.type === 'layout-panel') {
|
|
42
|
+
return {
|
|
43
|
+
...el,
|
|
44
|
+
children: traverse(el.children || []),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return applySpecialColor(el);
|
|
48
|
+
});
|
|
49
|
+
return {
|
|
50
|
+
...layout,
|
|
51
|
+
backgroundImage: bg.imgUrl,
|
|
52
|
+
fontColor: bg.fontColor || layout.fontColor,
|
|
53
|
+
children: traverse(layout.children || []),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
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;
|