km-card-layout-component-miniprogram 0.1.7 → 0.1.8

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 (24) hide show
  1. package/miniprogram_dist/components/card-layout/index.js +25 -193
  2. package/miniprogram_dist/components/card-layout/index.wxss +3 -1
  3. package/miniprogram_dist/vendor/km-card-layout-core/bindings.js +74 -0
  4. package/miniprogram_dist/vendor/km-card-layout-core/data.js +38 -0
  5. package/miniprogram_dist/vendor/km-card-layout-core/helpers.js +72 -0
  6. package/miniprogram_dist/vendor/km-card-layout-core/index.js +15 -367
  7. package/miniprogram_dist/vendor/km-card-layout-core/layout.js +117 -0
  8. package/miniprogram_dist/vendor/km-card-layout-core/ops/changeBackground.js +135 -0
  9. package/miniprogram_dist/vendor/km-card-layout-core/render/builder.js +210 -0
  10. package/miniprogram_dist/vendor/km-card-layout-core/utils.js +23 -130
  11. package/package.json +1 -1
  12. package/script/sync-core.js +10 -2
  13. package/src/components/card-layout/index.ts +30 -287
  14. package/src/components/card-layout/index.wxss +3 -1
  15. package/src/vendor/km-card-layout-core/bindings.ts +84 -0
  16. package/src/vendor/km-card-layout-core/data.ts +38 -0
  17. package/src/vendor/km-card-layout-core/helpers.ts +76 -0
  18. package/src/vendor/km-card-layout-core/index.ts +21 -460
  19. package/src/vendor/km-card-layout-core/interface/render.ts +1 -0
  20. package/src/vendor/km-card-layout-core/layout.ts +129 -0
  21. package/src/vendor/km-card-layout-core/ops/changeBackground.ts +169 -0
  22. package/src/vendor/km-card-layout-core/render/builder.ts +288 -0
  23. package/src/vendor/km-card-layout-core/types.d.ts +97 -5
  24. package/src/vendor/km-card-layout-core/utils.ts +9 -141
@@ -12,75 +12,6 @@ const pickCardId = (layout, idx) => {
12
12
  return layout.name || layout.id;
13
13
  return `card-${idx}`;
14
14
  };
15
- const withUnit = (value, unit) => (0, index_1.addUnit)(value, unit);
16
- const buildCardStyle = (layout, unit) => (0, index_1.styleObjectToString)({
17
- width: withUnit(layout.width, unit),
18
- height: withUnit(layout.height, unit),
19
- color: layout.fontColor,
20
- borderRadius: layout.borderRadius !== undefined
21
- ? withUnit(layout.borderRadius, unit)
22
- : undefined,
23
- padding: layout.padding !== undefined ? withUnit(layout.padding, unit) : undefined,
24
- position: 'relative',
25
- overflow: 'hidden',
26
- boxSizing: 'border-box',
27
- backgroundColor: 'transparent',
28
- }, unit);
29
- const buildBackgroundStyle = (layout, unit) => (0, index_1.styleObjectToString)({
30
- zIndex: layout.backgroundZIndex,
31
- borderRadius: layout.borderRadius !== undefined
32
- ? withUnit(layout.borderRadius, unit)
33
- : undefined,
34
- width: '100%',
35
- height: '100%',
36
- position: 'absolute',
37
- left: 0,
38
- top: 0,
39
- }, unit);
40
- const buildWrapperStyle = (el, unit) => {
41
- if (!el.layout || el.layout.mode !== 'absolute')
42
- return '';
43
- return (0, index_1.styleObjectToString)({
44
- position: 'absolute',
45
- left: withUnit(el.layout.x, unit),
46
- top: withUnit(el.layout.y, unit),
47
- width: withUnit(el.layout.width, unit),
48
- height: withUnit(el.layout.height, unit),
49
- zIndex: el.layout.zIndex,
50
- boxSizing: 'border-box',
51
- display: 'flex',
52
- alignItems: 'center',
53
- }, unit);
54
- };
55
- const buildPanelContentStyle = (el, unit) => (0, index_1.styleObjectToString)({
56
- position: 'relative',
57
- width: '100%',
58
- height: '100%',
59
- display: 'block',
60
- boxSizing: 'border-box',
61
- ...(el.style || {}),
62
- }, unit);
63
- const buildTextContentStyle = (el, unit) => {
64
- var _a, _b, _c;
65
- const textAlign = ((_a = el.style) === null || _a === void 0 ? void 0 : _a.textAlign) || el.align || undefined;
66
- const style = {
67
- ...el.style,
68
- whiteSpace: 'pre-wrap',
69
- wordBreak: 'break-word',
70
- 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
71
- ? el.style.lineHeight
72
- : '1.2',
73
- display: 'inline-flex',
74
- alignItems: 'center',
75
- };
76
- if (textAlign)
77
- style.textAlign = textAlign;
78
- return (0, index_1.styleObjectToString)(style, unit);
79
- };
80
- const buildBaseContentStyle = (el, unit) => (0, index_1.styleObjectToString)({
81
- ...(el.style || {}),
82
- boxSizing: 'border-box',
83
- }, unit);
84
15
  const mapIconGlyph = (name, fallback) => {
85
16
  if (!name)
86
17
  return fallback;
@@ -89,130 +20,34 @@ const mapIconGlyph = (name, fallback) => {
89
20
  return String.fromCharCode(parseInt(glyph, 16));
90
21
  return fallback || name;
91
22
  };
92
- const buildTextIcon = (el, unit) => {
93
- var _a, _b, _c, _d, _e;
94
- const icon = el.icon;
95
- if (!icon || icon.enable === false)
96
- return undefined;
97
- const style = icon.style || 'fill';
98
- const baseName = el.key || el.binding || el.id;
99
- let name;
100
- if (style === 'dot')
101
- name = 'round';
102
- else if (style === 'line')
103
- name = baseName ? `${baseName}-line` : undefined;
104
- else
105
- name = baseName || undefined;
106
- if (!name)
107
- return undefined;
108
- const size = icon.size !== undefined && icon.size !== null
109
- ? icon.size
110
- : (_a = el.style) === null || _a === void 0 ? void 0 : _a.fontSize;
111
- const gap = icon.gap !== undefined && icon.gap !== null ? icon.gap : 4;
112
- const color = (_d = (_b = icon.color) !== null && _b !== void 0 ? _b : (_c = el.style) === null || _c === void 0 ? void 0 : _c.color) !== null && _d !== void 0 ? _d : undefined;
113
- const text = mapIconGlyph(name, name);
23
+ const mapRenderNode = (node) => {
24
+ var _a;
25
+ const icon = node.icon
26
+ ? {
27
+ ...node.icon,
28
+ text: mapIconGlyph(node.icon.name, node.icon.text),
29
+ }
30
+ : undefined;
31
+ const children = (_a = node.children) === null || _a === void 0 ? void 0 : _a.map(mapRenderNode);
32
+ const mappedText = node.type === 'icon' ? mapIconGlyph(node.name, node.text) : node.text;
114
33
  return {
115
- name,
116
- text,
117
- size: size !== undefined ? withUnit(size, unit) : undefined,
118
- gap: gap !== undefined ? withUnit(gap, unit) : undefined,
119
- wrapperStyle: (0, index_1.styleObjectToString)({
120
- display: 'inline-flex',
121
- alignItems: 'center',
122
- height: ((_e = el.style) === null || _e === void 0 ? void 0 : _e.lineHeight) || 'auto',
123
- }, unit),
124
- color,
125
- align: icon.align || 'left',
34
+ ...node,
35
+ text: mappedText,
36
+ icon,
37
+ children,
126
38
  };
127
39
  };
128
- const buildRenderNode = (el, data, unit) => {
129
- var _a, _b, _c, _d, _e, _f, _g, _h;
130
- if (!el || el.visible === false)
131
- return null;
132
- const wrapperStyle = buildWrapperStyle(el, unit);
133
- if (el.type === 'layout-panel') {
134
- const panel = el;
135
- return {
136
- id: el.id,
137
- type: el.type,
138
- wrapperStyle,
139
- contentStyle: buildPanelContentStyle(panel, unit),
140
- children: buildRenderNodes(panel.children || [], data, unit),
141
- };
142
- }
143
- if (el.type === 'text') {
144
- const textValue = (_b = (_a = (0, index_1.resolveBindingValue)(el.binding, data)) !== null && _a !== void 0 ? _a : el.defaultValue) !== null && _b !== void 0 ? _b : '';
145
- return {
146
- id: el.id,
147
- type: el.type,
148
- wrapperStyle,
149
- contentStyle: buildTextContentStyle(el, unit),
150
- text: `${textValue}`,
151
- icon: buildTextIcon(el, unit),
152
- };
153
- }
154
- if (el.type === 'image') {
155
- const style = { ...(el.style || {}) };
156
- const borderWidth = Number(style.borderWidth);
157
- if (Number.isFinite(borderWidth) && borderWidth > 0) {
158
- if (!style.borderStyle)
159
- style.borderStyle = 'solid';
160
- if (!style.borderColor)
161
- style.borderColor = '#000000';
162
- }
163
- const src = (_e = (_d = (_c = (0, index_1.resolveBindingValue)(el.binding, data)) !== null && _c !== void 0 ? _c : el.defaultUrl) !== null && _d !== void 0 ? _d : el.defaultValue) !== null && _e !== void 0 ? _e : '';
164
- const mode = el.fit === 'contain' ? 'aspectFit' : 'aspectFill';
165
- return {
166
- id: el.id,
167
- type: el.type,
168
- wrapperStyle,
169
- contentStyle: (0, index_1.styleObjectToString)(style, unit),
170
- src,
171
- mode,
172
- };
173
- }
174
- if (el.type === 'icon') {
175
- const resolved = (_h = (_g = (_f = (0, index_1.resolveBindingValue)(el.binding, data)) !== null && _f !== void 0 ? _f : el.name) !== null && _g !== void 0 ? _g : el.defaultValue) !== null && _h !== void 0 ? _h : '';
176
- const text = mapIconGlyph(`${resolved}`, `${resolved}`);
177
- return {
178
- id: el.id,
179
- type: el.type,
180
- wrapperStyle,
181
- contentStyle: buildBaseContentStyle(el, unit),
182
- name: `${resolved}`,
183
- text,
184
- };
185
- }
186
- if (el.type === 'custom') {
187
- return {
188
- id: el.id,
189
- type: el.type,
190
- wrapperStyle,
191
- contentStyle: buildBaseContentStyle(el, unit),
192
- };
193
- }
194
- return null;
195
- };
196
- const buildRenderNodes = (children, data, unit) => {
197
- if (!Array.isArray(children))
198
- return [];
199
- const nodes = [];
200
- children.forEach(el => {
201
- if (!el || el.visible === false)
202
- return;
203
- const node = buildRenderNode(el, data, unit);
204
- if (node)
205
- nodes.push(node);
206
- });
207
- return nodes;
40
+ const mapRenderTree = (nodes) => Array.isArray(nodes) ? nodes.map(mapRenderNode) : [];
41
+ const buildCards = (layouts, data) => {
42
+ const renders = (0, index_1.buildRenderResult)(layouts, data, 'rpx');
43
+ return renders.map((render, idx) => ({
44
+ id: pickCardId(layouts[idx], idx),
45
+ cardStyle: render.cardStyle,
46
+ backgroundImage: render.backgroundImage || '',
47
+ backgroundStyle: render.backgroundStyle,
48
+ nodes: mapRenderTree(render.renderTree),
49
+ }));
208
50
  };
209
- const buildCards = (layouts, data, unit) => layouts.map(layout => ({
210
- id: layout.name || layout.id || '',
211
- cardStyle: buildCardStyle(layout, unit),
212
- backgroundImage: layout.backgroundImage || '',
213
- backgroundStyle: buildBackgroundStyle(layout, unit),
214
- nodes: buildRenderNodes(layout.children || [], data, unit),
215
- }));
216
51
  Component({
217
52
  options: {
218
53
  styleIsolation: 'apply-shared',
@@ -255,10 +90,7 @@ Component({
255
90
  return;
256
91
  }
257
92
  const normalizedLayouts = (0, index_1.normalizeLayout)(layoutInput);
258
- const cards = buildCards(normalizedLayouts, dataInput, 'rpx').map((card, idx) => ({
259
- ...card,
260
- id: pickCardId(layoutInput[idx], idx),
261
- }));
93
+ const cards = buildCards(normalizedLayouts, dataInput);
262
94
  this.setData({ cards });
263
95
  },
264
96
  },
@@ -95,7 +95,9 @@
95
95
  word-break: break-word;
96
96
  vertical-align: middle;
97
97
  }
98
-
98
+ .km-node--text{
99
+ /* 溢出隐藏 */
100
+ }
99
101
  .km-node--text text {
100
102
  display: inline;
101
103
  }
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTemplateBackgrounds = exports.getTemplateItems = exports.applyItemCollectBindings = exports.stripLayoutBindings = void 0;
4
+ function stripLayoutBindings(layouts = []) {
5
+ const targetLayouts = Array.isArray(layouts) ? layouts : [];
6
+ const stripElement = (el) => {
7
+ const { binding: _b, defaultValue: _d, ...rest } = el;
8
+ if (el.type === 'layout-panel') {
9
+ return {
10
+ ...rest,
11
+ children: (el.children || []).map(stripElement),
12
+ };
13
+ }
14
+ return rest;
15
+ };
16
+ return targetLayouts.map(layout => ({
17
+ ...layout,
18
+ children: (layout.children || []).map(stripElement),
19
+ }));
20
+ }
21
+ exports.stripLayoutBindings = stripLayoutBindings;
22
+ function applyItemCollectBindings(layouts = [], items = []) {
23
+ const targetLayouts = Array.isArray(layouts) ? layouts : [];
24
+ const metaMap = new Map();
25
+ const metaList = Array.isArray(items) ? items : [];
26
+ metaList.forEach(item => {
27
+ if (item && item.id !== undefined && item.id !== null) {
28
+ metaMap.set(String(item.id), item);
29
+ }
30
+ });
31
+ const assignBinding = (el) => {
32
+ const meta = metaMap.get(String(el.id));
33
+ const binding = meta && meta.bind !== undefined && meta.bind !== null
34
+ ? meta.bind
35
+ : el.binding;
36
+ const defaultValue = meta && meta.default !== undefined ? meta.default : el.defaultValue;
37
+ const key = meta && meta.key !== undefined ? meta.key : el.key;
38
+ const base = { ...el };
39
+ if (binding !== undefined)
40
+ base.binding = binding;
41
+ else
42
+ delete base.binding;
43
+ if (defaultValue !== undefined)
44
+ base.defaultValue = defaultValue;
45
+ else
46
+ delete base.defaultValue;
47
+ if (key !== undefined)
48
+ base.key = key;
49
+ else
50
+ delete base.key;
51
+ if (el.type === 'layout-panel') {
52
+ return {
53
+ ...base,
54
+ children: (el.children || []).map(assignBinding),
55
+ };
56
+ }
57
+ return base;
58
+ };
59
+ return targetLayouts.map(layout => ({
60
+ ...layout,
61
+ children: (layout.children || []).map(assignBinding),
62
+ }));
63
+ }
64
+ exports.applyItemCollectBindings = applyItemCollectBindings;
65
+ function getTemplateItems(ids, items) {
66
+ const idArray = ids.split(',').map(id => id.trim());
67
+ return items.filter(item => idArray.includes(String(item.id)));
68
+ }
69
+ exports.getTemplateItems = getTemplateItems;
70
+ function getTemplateBackgrounds(ids, items) {
71
+ const idArray = ids.split(',').map(id => id.trim());
72
+ return items.filter(item => idArray.includes(String(item.id)));
73
+ }
74
+ exports.getTemplateBackgrounds = getTemplateBackgrounds;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveBindingValue = void 0;
4
+ const helpers_1 = require("./helpers");
5
+ const pathToSegments = (path) => `${path || ''}`
6
+ .replace(/\[(\d+)\]/g, '.$1')
7
+ .split('.')
8
+ .map(p => p.trim())
9
+ .filter(Boolean);
10
+ const readByPath = (data, path) => {
11
+ if (path === undefined || path === null || path === '')
12
+ return data;
13
+ const segments = pathToSegments(path);
14
+ let cursor = data;
15
+ for (let i = 0; i < segments.length; i += 1) {
16
+ if (!(0, helpers_1.isObject)(cursor) && !Array.isArray(cursor))
17
+ return undefined;
18
+ const key = segments[i];
19
+ if (Array.isArray(cursor)) {
20
+ const idx = Number(key);
21
+ cursor = Number.isNaN(idx) ? undefined : cursor[idx];
22
+ }
23
+ else {
24
+ cursor = cursor[key];
25
+ }
26
+ if (cursor === undefined || cursor === null) {
27
+ return cursor;
28
+ }
29
+ }
30
+ return cursor;
31
+ };
32
+ const resolveBindingValue = (binding, rootData, context) => {
33
+ if (!binding)
34
+ return undefined;
35
+ const value = readByPath(rootData, binding);
36
+ return value === undefined ? undefined : value;
37
+ };
38
+ exports.resolveBindingValue = resolveBindingValue;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isObject = exports.styleObjectToString = exports.addUnit = exports.toNumber = void 0;
4
+ const DIMENSION_PROPS = new Set([
5
+ 'width',
6
+ 'height',
7
+ 'top',
8
+ 'right',
9
+ 'bottom',
10
+ 'left',
11
+ 'padding',
12
+ 'paddingTop',
13
+ 'paddingBottom',
14
+ 'paddingLeft',
15
+ 'paddingRight',
16
+ 'margin',
17
+ 'marginTop',
18
+ 'marginBottom',
19
+ 'marginLeft',
20
+ 'marginRight',
21
+ 'fontSize',
22
+ 'lineHeight',
23
+ 'borderRadius',
24
+ 'borderWidth',
25
+ 'letterSpacing',
26
+ 'gap',
27
+ 'rowGap',
28
+ 'columnGap',
29
+ ]);
30
+ const toNumber = (value) => {
31
+ const num = Number(value);
32
+ return Number.isFinite(num) ? num : undefined;
33
+ };
34
+ exports.toNumber = toNumber;
35
+ const toKebab = (key) => key.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
36
+ const addUnit = (value, unit) => {
37
+ if (value === undefined || value === null || value === '')
38
+ return undefined;
39
+ if (typeof value === 'number') {
40
+ const ratio = unit === 'rpx' ? 2 : 1;
41
+ return `${value * ratio}${unit}`;
42
+ }
43
+ if (typeof value === 'string') {
44
+ const parsed = Number(value);
45
+ if (Number.isFinite(parsed)) {
46
+ const ratio = unit === 'rpx' ? 2 : 1;
47
+ return `${parsed * ratio}${unit}`;
48
+ }
49
+ }
50
+ return `${value}`;
51
+ };
52
+ exports.addUnit = addUnit;
53
+ const styleObjectToString = (style, unit = 'px') => {
54
+ if (!style)
55
+ return '';
56
+ const pairs = [];
57
+ Object.keys(style).forEach(key => {
58
+ const value = style[key];
59
+ if (value === undefined || value === null || value === '')
60
+ return;
61
+ const useUnit = DIMENSION_PROPS.has(key)
62
+ ? (0, exports.addUnit)(value, unit)
63
+ : value;
64
+ if (useUnit === undefined || useUnit === null || useUnit === '')
65
+ return;
66
+ pairs.push(`${toKebab(key)}:${useUnit}`);
67
+ });
68
+ return pairs.join(';');
69
+ };
70
+ exports.styleObjectToString = styleObjectToString;
71
+ const isObject = (val) => Boolean(val) && typeof val === 'object';
72
+ exports.isObject = isObject;