km-card-layout-core 0.1.8 → 0.1.9

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.
Files changed (3) hide show
  1. package/dist/index.js +5 -5
  2. package/index.ts +198 -198
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -341,7 +341,7 @@ const buildNode = (el, rootData, unit, context) => {
341
341
  return {
342
342
  id: el.id,
343
343
  type: el.type,
344
- visible: !!el.visible,
344
+ visible: el.visible,
345
345
  wrapperStyle,
346
346
  contentStyle: buildPanelContentStyle(el, unit),
347
347
  children: (0, exports.buildRenderNodes)(el.children || [], rootData, unit, context),
@@ -356,7 +356,7 @@ const buildNode = (el, rootData, unit, context) => {
356
356
  wrapperStyle,
357
357
  contentStyle: buildTextContentStyle(el, unit),
358
358
  text: `${value}`,
359
- visible: !!el.visible,
359
+ visible: el.visible,
360
360
  icon: buildTextIcon(el, unit),
361
361
  };
362
362
  }
@@ -370,7 +370,7 @@ const buildNode = (el, rootData, unit, context) => {
370
370
  contentStyle: buildImageContentStyle(el, unit),
371
371
  src,
372
372
  mode,
373
- visible: !!el.visible,
373
+ visible: el.visible,
374
374
  };
375
375
  }
376
376
  if (el.type === 'icon') {
@@ -382,7 +382,7 @@ const buildNode = (el, rootData, unit, context) => {
382
382
  contentStyle: baseStyle,
383
383
  name: `${name}`,
384
384
  text: `${name}`,
385
- visible: !!el.visible,
385
+ visible: el.visible,
386
386
  };
387
387
  }
388
388
  if (el.type === 'custom') {
@@ -391,7 +391,7 @@ const buildNode = (el, rootData, unit, context) => {
391
391
  type: el.type,
392
392
  wrapperStyle,
393
393
  contentStyle: baseStyle,
394
- visible: !!el.visible,
394
+ visible: el.visible,
395
395
  };
396
396
  }
397
397
  return null;
package/index.ts CHANGED
@@ -204,131 +204,131 @@ export const resolveBindingValue = (
204
204
  return value === undefined ? undefined : value;
205
205
  };
206
206
 
207
- /** ---------- 样式构建 ---------- */
208
-
209
- const buildCardStyle = (
210
- layout: CardLayoutSchema,
211
- unit: 'px' | 'rpx'
212
- ): string =>
213
- styleObjectToString(
214
- {
215
- width: addUnit(layout.width, unit),
216
- height: addUnit(layout.height, unit),
217
- color: layout.fontColor,
218
- borderRadius:
219
- layout.borderRadius !== undefined
220
- ? addUnit(layout.borderRadius, unit)
221
- : undefined,
222
- padding:
223
- layout.padding !== undefined ? addUnit(layout.padding, unit) : undefined,
224
- position: 'relative',
225
- overflow: 'hidden',
226
- boxSizing: 'border-box',
227
- backgroundColor: 'transparent',
228
- },
229
- unit
230
- );
231
-
232
- const buildBackgroundStyle = (
233
- layout: CardLayoutSchema,
234
- unit: 'px' | 'rpx'
235
- ): string =>
236
- styleObjectToString(
237
- {
238
- zIndex: layout.backgroundZIndex,
239
- borderRadius:
240
- layout.borderRadius !== undefined
241
- ? addUnit(layout.borderRadius, unit)
242
- : undefined,
243
- width: '100%',
244
- height: '100%',
245
- position: 'absolute',
246
- left: 0,
247
- top: 0,
248
- },
249
- unit
250
- );
251
-
252
- /**
253
- * 生成元素外层样式(绝对/弹性布局),始终返回内联样式字符串。
254
- */
255
- const buildWrapperStyle = (el: CardElement, unit: 'px' | 'rpx'): string => {
256
- const abs = getAbsLayout(el);
257
- if (!abs) return '';
258
-
259
- return styleObjectToString(
260
- {
261
- position: 'absolute',
262
- left: addUnit(abs.x, unit),
263
- top: addUnit(abs.y, unit),
264
- width: addUnit(abs.width, unit),
265
- height: addUnit(abs.height, unit),
266
- zIndex: abs.zIndex,
267
- boxSizing: 'border-box',
268
- display: 'flex',
269
- alignItems: 'center',
270
- },
271
- unit
272
- );
273
- };
274
- const buildPanelContentStyle = (
275
- el: LayoutPanelElement,
276
- unit: 'px' | 'rpx'
277
- ): string =>
278
- styleObjectToString(
279
- {
280
- position: 'relative',
281
- width: '100%',
282
- height: '100%',
283
- display: 'block',
284
- boxSizing: 'border-box',
285
- ...(el.style || {}),
286
- },
287
- unit
288
- );
289
-
290
- const buildTextContentStyle = (el: TextElement, unit: 'px' | 'rpx') => {
291
- const textAlign =
292
- (el.style?.textAlign as string | undefined) || el.align || undefined;
293
- const style: Record<string, any> = {
294
- ...(el.style || {}),
295
- whiteSpace: 'pre-wrap',
296
- wordBreak: 'break-word',
297
- lineHeight:
298
- el.style?.lineHeight !== undefined && el.style?.lineHeight !== null
299
- ? el.style.lineHeight
300
- : '1.2',
301
- display: 'inline-flex',
302
- alignItems: 'center',
303
- };
304
- if (textAlign) style.textAlign = textAlign;
305
- return styleObjectToString(style, unit);
306
- };
307
-
308
- const buildBaseContentStyle = (
309
- el: CardElement,
310
- unit: 'px' | 'rpx'
311
- ): string =>
312
- styleObjectToString(
313
- {
314
- ...(el.style || {}),
315
- boxSizing: 'border-box',
316
- },
317
- unit
318
- );
319
-
320
- const buildImageContentStyle = (
321
- el: ImageElement,
322
- unit: 'px' | 'rpx'
323
- ): string => {
324
- const style: Record<string, any> = { ...(el.style || {}) };
325
- const borderWidth = Number(style.borderWidth);
326
- if (Number.isFinite(borderWidth) && borderWidth > 0) {
327
- if (!style.borderStyle) style.borderStyle = 'solid';
328
- if (!style.borderColor) style.borderColor = '#000000';
329
- }
330
- return styleObjectToString(style, unit);
331
- };
207
+ /** ---------- 样式构建 ---------- */
208
+
209
+ const buildCardStyle = (
210
+ layout: CardLayoutSchema,
211
+ unit: 'px' | 'rpx'
212
+ ): string =>
213
+ styleObjectToString(
214
+ {
215
+ width: addUnit(layout.width, unit),
216
+ height: addUnit(layout.height, unit),
217
+ color: layout.fontColor,
218
+ borderRadius:
219
+ layout.borderRadius !== undefined
220
+ ? addUnit(layout.borderRadius, unit)
221
+ : undefined,
222
+ padding:
223
+ layout.padding !== undefined ? addUnit(layout.padding, unit) : undefined,
224
+ position: 'relative',
225
+ overflow: 'hidden',
226
+ boxSizing: 'border-box',
227
+ backgroundColor: 'transparent',
228
+ },
229
+ unit
230
+ );
231
+
232
+ const buildBackgroundStyle = (
233
+ layout: CardLayoutSchema,
234
+ unit: 'px' | 'rpx'
235
+ ): string =>
236
+ styleObjectToString(
237
+ {
238
+ zIndex: layout.backgroundZIndex,
239
+ borderRadius:
240
+ layout.borderRadius !== undefined
241
+ ? addUnit(layout.borderRadius, unit)
242
+ : undefined,
243
+ width: '100%',
244
+ height: '100%',
245
+ position: 'absolute',
246
+ left: 0,
247
+ top: 0,
248
+ },
249
+ unit
250
+ );
251
+
252
+ /**
253
+ * 生成元素外层样式(绝对/弹性布局),始终返回内联样式字符串。
254
+ */
255
+ const buildWrapperStyle = (el: CardElement, unit: 'px' | 'rpx'): string => {
256
+ const abs = getAbsLayout(el);
257
+ if (!abs) return '';
258
+
259
+ return styleObjectToString(
260
+ {
261
+ position: 'absolute',
262
+ left: addUnit(abs.x, unit),
263
+ top: addUnit(abs.y, unit),
264
+ width: addUnit(abs.width, unit),
265
+ height: addUnit(abs.height, unit),
266
+ zIndex: abs.zIndex,
267
+ boxSizing: 'border-box',
268
+ display: 'flex',
269
+ alignItems: 'center',
270
+ },
271
+ unit
272
+ );
273
+ };
274
+ const buildPanelContentStyle = (
275
+ el: LayoutPanelElement,
276
+ unit: 'px' | 'rpx'
277
+ ): string =>
278
+ styleObjectToString(
279
+ {
280
+ position: 'relative',
281
+ width: '100%',
282
+ height: '100%',
283
+ display: 'block',
284
+ boxSizing: 'border-box',
285
+ ...(el.style || {}),
286
+ },
287
+ unit
288
+ );
289
+
290
+ const buildTextContentStyle = (el: TextElement, unit: 'px' | 'rpx') => {
291
+ const textAlign =
292
+ (el.style?.textAlign as string | undefined) || el.align || undefined;
293
+ const style: Record<string, any> = {
294
+ ...(el.style || {}),
295
+ whiteSpace: 'pre-wrap',
296
+ wordBreak: 'break-word',
297
+ lineHeight:
298
+ el.style?.lineHeight !== undefined && el.style?.lineHeight !== null
299
+ ? el.style.lineHeight
300
+ : '1.2',
301
+ display: 'inline-flex',
302
+ alignItems: 'center',
303
+ };
304
+ if (textAlign) style.textAlign = textAlign;
305
+ return styleObjectToString(style, unit);
306
+ };
307
+
308
+ const buildBaseContentStyle = (
309
+ el: CardElement,
310
+ unit: 'px' | 'rpx'
311
+ ): string =>
312
+ styleObjectToString(
313
+ {
314
+ ...(el.style || {}),
315
+ boxSizing: 'border-box',
316
+ },
317
+ unit
318
+ );
319
+
320
+ const buildImageContentStyle = (
321
+ el: ImageElement,
322
+ unit: 'px' | 'rpx'
323
+ ): string => {
324
+ const style: Record<string, any> = { ...(el.style || {}) };
325
+ const borderWidth = Number(style.borderWidth);
326
+ if (Number.isFinite(borderWidth) && borderWidth > 0) {
327
+ if (!style.borderStyle) style.borderStyle = 'solid';
328
+ if (!style.borderColor) style.borderColor = '#000000';
329
+ }
330
+ return styleObjectToString(style, unit);
331
+ };
332
332
 
333
333
  const buildTextIcon = (
334
334
  el: TextElement,
@@ -357,23 +357,23 @@ const buildTextIcon = (
357
357
  | string
358
358
  | undefined);
359
359
 
360
- return {
361
- name: `${name}`,
362
- text: `${name}`,
363
- size: addUnit(size as any, unit),
364
- gap: addUnit(gap as any, unit),
365
- color: color as any,
366
- align: icon.align || 'left',
367
- wrapperStyle: styleObjectToString(
368
- {
369
- display: 'inline-flex',
370
- alignItems: 'center',
371
- height: el.style?.lineHeight || 'auto',
372
- },
373
- unit
374
- ),
375
- };
376
- };
360
+ return {
361
+ name: `${name}`,
362
+ text: `${name}`,
363
+ size: addUnit(size as any, unit),
364
+ gap: addUnit(gap as any, unit),
365
+ color: color as any,
366
+ align: icon.align || 'left',
367
+ wrapperStyle: styleObjectToString(
368
+ {
369
+ display: 'inline-flex',
370
+ alignItems: 'center',
371
+ height: el.style?.lineHeight || 'auto',
372
+ },
373
+ unit
374
+ ),
375
+ };
376
+ };
377
377
 
378
378
  /** ---------- 渲染树构建 ---------- */
379
379
  /**
@@ -408,52 +408,52 @@ const buildNode = (
408
408
  if (!el || el.visible === false) return null;
409
409
  const wrapperStyle = buildWrapperStyle(el, unit);
410
410
 
411
- if (el.type === 'layout-panel') {
412
- return {
413
- id: el.id,
414
- type: el.type,
415
- visible: !!el.visible,
416
- wrapperStyle,
417
- contentStyle: buildPanelContentStyle(el as LayoutPanelElement, unit),
418
- children: buildRenderNodes(el.children || [], rootData, unit, context),
419
- };
420
- }
421
-
422
- const baseStyle = buildBaseContentStyle(el, unit);
423
- if (el.type === 'text') {
424
- const value =
425
- resolveBindingValue(el.binding, rootData, context) ??
426
- el.defaultValue ??
427
- '';
428
- return {
429
- id: el.id,
430
- type: el.type,
431
- wrapperStyle,
432
- contentStyle: buildTextContentStyle(el as TextElement, unit),
433
- text: `${value}`,
434
- visible: !!el.visible,
435
- icon: buildTextIcon(el as TextElement, unit),
436
- };
437
- }
438
-
439
- if (el.type === 'image') {
440
- const src =
441
- resolveBindingValue(el.binding, rootData, context) ??
442
- (el as ImageElement).defaultUrl ??
443
- el.defaultValue ??
411
+ if (el.type === 'layout-panel') {
412
+ return {
413
+ id: el.id,
414
+ type: el.type,
415
+ visible: el.visible,
416
+ wrapperStyle,
417
+ contentStyle: buildPanelContentStyle(el as LayoutPanelElement, unit),
418
+ children: buildRenderNodes(el.children || [], rootData, unit, context),
419
+ };
420
+ }
421
+
422
+ const baseStyle = buildBaseContentStyle(el, unit);
423
+ if (el.type === 'text') {
424
+ const value =
425
+ resolveBindingValue(el.binding, rootData, context) ??
426
+ el.defaultValue ??
427
+ '';
428
+ return {
429
+ id: el.id,
430
+ type: el.type,
431
+ wrapperStyle,
432
+ contentStyle: buildTextContentStyle(el as TextElement, unit),
433
+ text: `${value}`,
434
+ visible: el.visible,
435
+ icon: buildTextIcon(el as TextElement, unit),
436
+ };
437
+ }
438
+
439
+ if (el.type === 'image') {
440
+ const src =
441
+ resolveBindingValue(el.binding, rootData, context) ??
442
+ (el as ImageElement).defaultUrl ??
443
+ el.defaultValue ??
444
444
  '';
445
- const mode =
446
- (el as ImageElement).fit === 'contain' ? 'aspectFit' : 'aspectFill';
447
- return {
448
- id: el.id,
449
- type: el.type,
450
- wrapperStyle,
451
- contentStyle: buildImageContentStyle(el as ImageElement, unit),
452
- src,
453
- mode,
454
- visible: !!el.visible,
455
- };
456
- }
445
+ const mode =
446
+ (el as ImageElement).fit === 'contain' ? 'aspectFit' : 'aspectFill';
447
+ return {
448
+ id: el.id,
449
+ type: el.type,
450
+ wrapperStyle,
451
+ contentStyle: buildImageContentStyle(el as ImageElement, unit),
452
+ src,
453
+ mode,
454
+ visible: el.visible,
455
+ };
456
+ }
457
457
 
458
458
  if (el.type === 'icon') {
459
459
  const name =
@@ -468,7 +468,7 @@ const buildNode = (
468
468
  contentStyle: baseStyle,
469
469
  name: `${name}`,
470
470
  text: `${name}`,
471
- visible: !!el.visible,
471
+ visible: el.visible,
472
472
  };
473
473
  }
474
474
 
@@ -478,7 +478,7 @@ const buildNode = (
478
478
  type: el.type,
479
479
  wrapperStyle,
480
480
  contentStyle: baseStyle,
481
- visible: !!el.visible,
481
+ visible: el.visible,
482
482
  };
483
483
  }
484
484
 
@@ -492,14 +492,14 @@ export const buildRenderResult = (
492
492
  dataInput: Record<string, any>,
493
493
  unit: 'px' | 'rpx' = 'px'
494
494
  ): RenderResult => {
495
- const layouts = normalizeLayout(layoutInput);
496
- return layouts.map(layout => {
497
- const cardStyle = buildCardStyle(layout, unit);
498
- const bgStyle = buildBackgroundStyle(layout, unit);
499
-
500
- const renderTree = buildRenderNodes(
501
- layout.children || [],
502
- dataInput || {},
495
+ const layouts = normalizeLayout(layoutInput);
496
+ return layouts.map(layout => {
497
+ const cardStyle = buildCardStyle(layout, unit);
498
+ const bgStyle = buildBackgroundStyle(layout, unit);
499
+
500
+ const renderTree = buildRenderNodes(
501
+ layout.children || [],
502
+ dataInput || {},
503
503
  unit
504
504
  );
505
505
 
@@ -512,4 +512,4 @@ export const buildRenderResult = (
512
512
  });
513
513
  };
514
514
 
515
- export * from './utils';
515
+ export * from './utils';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "km-card-layout-core",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
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",