km-card-layout-core 0.1.8 → 0.1.10

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 -7
  2. package/index.ts +196 -198
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -232,8 +232,6 @@ const buildWrapperStyle = (el, unit) => {
232
232
  height: (0, exports.addUnit)(abs.height, unit),
233
233
  zIndex: abs.zIndex,
234
234
  boxSizing: 'border-box',
235
- display: 'flex',
236
- alignItems: 'center',
237
235
  }, unit);
238
236
  };
239
237
  const buildPanelContentStyle = (el, unit) => (0, exports.styleObjectToString)({
@@ -341,7 +339,7 @@ const buildNode = (el, rootData, unit, context) => {
341
339
  return {
342
340
  id: el.id,
343
341
  type: el.type,
344
- visible: !!el.visible,
342
+ visible: el.visible,
345
343
  wrapperStyle,
346
344
  contentStyle: buildPanelContentStyle(el, unit),
347
345
  children: (0, exports.buildRenderNodes)(el.children || [], rootData, unit, context),
@@ -356,7 +354,7 @@ const buildNode = (el, rootData, unit, context) => {
356
354
  wrapperStyle,
357
355
  contentStyle: buildTextContentStyle(el, unit),
358
356
  text: `${value}`,
359
- visible: !!el.visible,
357
+ visible: el.visible,
360
358
  icon: buildTextIcon(el, unit),
361
359
  };
362
360
  }
@@ -370,7 +368,7 @@ const buildNode = (el, rootData, unit, context) => {
370
368
  contentStyle: buildImageContentStyle(el, unit),
371
369
  src,
372
370
  mode,
373
- visible: !!el.visible,
371
+ visible: el.visible,
374
372
  };
375
373
  }
376
374
  if (el.type === 'icon') {
@@ -382,7 +380,7 @@ const buildNode = (el, rootData, unit, context) => {
382
380
  contentStyle: baseStyle,
383
381
  name: `${name}`,
384
382
  text: `${name}`,
385
- visible: !!el.visible,
383
+ visible: el.visible,
386
384
  };
387
385
  }
388
386
  if (el.type === 'custom') {
@@ -391,7 +389,7 @@ const buildNode = (el, rootData, unit, context) => {
391
389
  type: el.type,
392
390
  wrapperStyle,
393
391
  contentStyle: baseStyle,
394
- visible: !!el.visible,
392
+ visible: el.visible,
395
393
  };
396
394
  }
397
395
  return null;
package/index.ts CHANGED
@@ -204,131 +204,129 @@ 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
+ },
269
+ unit
270
+ );
271
+ };
272
+ const buildPanelContentStyle = (
273
+ el: LayoutPanelElement,
274
+ unit: 'px' | 'rpx'
275
+ ): string =>
276
+ styleObjectToString(
277
+ {
278
+ position: 'relative',
279
+ width: '100%',
280
+ height: '100%',
281
+ display: 'block',
282
+ boxSizing: 'border-box',
283
+ ...(el.style || {}),
284
+ },
285
+ unit
286
+ );
287
+
288
+ const buildTextContentStyle = (el: TextElement, unit: 'px' | 'rpx') => {
289
+ const textAlign =
290
+ (el.style?.textAlign as string | undefined) || el.align || undefined;
291
+ const style: Record<string, any> = {
292
+ ...(el.style || {}),
293
+ whiteSpace: 'pre-wrap',
294
+ wordBreak: 'break-word',
295
+ lineHeight:
296
+ el.style?.lineHeight !== undefined && el.style?.lineHeight !== null
297
+ ? el.style.lineHeight
298
+ : '1.2',
299
+ display: 'inline-flex',
300
+ alignItems: 'center',
301
+ };
302
+ if (textAlign) style.textAlign = textAlign;
303
+ return styleObjectToString(style, unit);
304
+ };
305
+
306
+ const buildBaseContentStyle = (
307
+ el: CardElement,
308
+ unit: 'px' | 'rpx'
309
+ ): string =>
310
+ styleObjectToString(
311
+ {
312
+ ...(el.style || {}),
313
+ boxSizing: 'border-box',
314
+ },
315
+ unit
316
+ );
317
+
318
+ const buildImageContentStyle = (
319
+ el: ImageElement,
320
+ unit: 'px' | 'rpx'
321
+ ): string => {
322
+ const style: Record<string, any> = { ...(el.style || {}) };
323
+ const borderWidth = Number(style.borderWidth);
324
+ if (Number.isFinite(borderWidth) && borderWidth > 0) {
325
+ if (!style.borderStyle) style.borderStyle = 'solid';
326
+ if (!style.borderColor) style.borderColor = '#000000';
327
+ }
328
+ return styleObjectToString(style, unit);
329
+ };
332
330
 
333
331
  const buildTextIcon = (
334
332
  el: TextElement,
@@ -357,23 +355,23 @@ const buildTextIcon = (
357
355
  | string
358
356
  | undefined);
359
357
 
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
- };
358
+ return {
359
+ name: `${name}`,
360
+ text: `${name}`,
361
+ size: addUnit(size as any, unit),
362
+ gap: addUnit(gap as any, unit),
363
+ color: color as any,
364
+ align: icon.align || 'left',
365
+ wrapperStyle: styleObjectToString(
366
+ {
367
+ display: 'inline-flex',
368
+ alignItems: 'center',
369
+ height: el.style?.lineHeight || 'auto',
370
+ },
371
+ unit
372
+ ),
373
+ };
374
+ };
377
375
 
378
376
  /** ---------- 渲染树构建 ---------- */
379
377
  /**
@@ -408,52 +406,52 @@ const buildNode = (
408
406
  if (!el || el.visible === false) return null;
409
407
  const wrapperStyle = buildWrapperStyle(el, unit);
410
408
 
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 ??
409
+ if (el.type === 'layout-panel') {
410
+ return {
411
+ id: el.id,
412
+ type: el.type,
413
+ visible: el.visible,
414
+ wrapperStyle,
415
+ contentStyle: buildPanelContentStyle(el as LayoutPanelElement, unit),
416
+ children: buildRenderNodes(el.children || [], rootData, unit, context),
417
+ };
418
+ }
419
+
420
+ const baseStyle = buildBaseContentStyle(el, unit);
421
+ if (el.type === 'text') {
422
+ const value =
423
+ resolveBindingValue(el.binding, rootData, context) ??
424
+ el.defaultValue ??
425
+ '';
426
+ return {
427
+ id: el.id,
428
+ type: el.type,
429
+ wrapperStyle,
430
+ contentStyle: buildTextContentStyle(el as TextElement, unit),
431
+ text: `${value}`,
432
+ visible: el.visible,
433
+ icon: buildTextIcon(el as TextElement, unit),
434
+ };
435
+ }
436
+
437
+ if (el.type === 'image') {
438
+ const src =
439
+ resolveBindingValue(el.binding, rootData, context) ??
440
+ (el as ImageElement).defaultUrl ??
441
+ el.defaultValue ??
444
442
  '';
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
- }
443
+ const mode =
444
+ (el as ImageElement).fit === 'contain' ? 'aspectFit' : 'aspectFill';
445
+ return {
446
+ id: el.id,
447
+ type: el.type,
448
+ wrapperStyle,
449
+ contentStyle: buildImageContentStyle(el as ImageElement, unit),
450
+ src,
451
+ mode,
452
+ visible: el.visible,
453
+ };
454
+ }
457
455
 
458
456
  if (el.type === 'icon') {
459
457
  const name =
@@ -468,7 +466,7 @@ const buildNode = (
468
466
  contentStyle: baseStyle,
469
467
  name: `${name}`,
470
468
  text: `${name}`,
471
- visible: !!el.visible,
469
+ visible: el.visible,
472
470
  };
473
471
  }
474
472
 
@@ -478,7 +476,7 @@ const buildNode = (
478
476
  type: el.type,
479
477
  wrapperStyle,
480
478
  contentStyle: baseStyle,
481
- visible: !!el.visible,
479
+ visible: el.visible,
482
480
  };
483
481
  }
484
482
 
@@ -492,14 +490,14 @@ export const buildRenderResult = (
492
490
  dataInput: Record<string, any>,
493
491
  unit: 'px' | 'rpx' = 'px'
494
492
  ): 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 || {},
493
+ const layouts = normalizeLayout(layoutInput);
494
+ return layouts.map(layout => {
495
+ const cardStyle = buildCardStyle(layout, unit);
496
+ const bgStyle = buildBackgroundStyle(layout, unit);
497
+
498
+ const renderTree = buildRenderNodes(
499
+ layout.children || [],
500
+ dataInput || {},
503
501
  unit
504
502
  );
505
503
 
@@ -512,4 +510,4 @@ export const buildRenderResult = (
512
510
  });
513
511
  };
514
512
 
515
- export * from './utils';
513
+ 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.10",
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",