km-card-layout-component-miniprogram 0.1.5 → 0.1.6

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.
@@ -12,25 +12,221 @@ 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
+ const mapIconGlyph = (name, fallback) => {
85
+ if (!name)
86
+ return fallback;
87
+ const glyph = icon_map_1.ICON_CODE_MAP[name];
88
+ if (glyph)
89
+ return String.fromCharCode(parseInt(glyph, 16));
90
+ return fallback || name;
91
+ };
92
+ const buildTextIcon = (el, unit) => {
93
+ var _a, _b, _c, _d;
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);
114
+ return {
115
+ name,
116
+ text,
117
+ size: size !== undefined ? withUnit(size, unit) : undefined,
118
+ gap: gap !== undefined ? withUnit(gap, unit) : undefined,
119
+ color,
120
+ align: icon.align || 'left',
121
+ };
122
+ };
123
+ const buildRenderNode = (el, data, unit) => {
124
+ var _a, _b, _c, _d, _e, _f, _g, _h;
125
+ if (!el || el.visible === false)
126
+ return null;
127
+ const wrapperStyle = buildWrapperStyle(el, unit);
128
+ if (el.type === 'layout-panel') {
129
+ const panel = el;
130
+ return {
131
+ id: el.id,
132
+ type: el.type,
133
+ wrapperStyle,
134
+ contentStyle: buildPanelContentStyle(panel, unit),
135
+ children: buildRenderNodes(panel.children || [], data, unit),
136
+ };
137
+ }
138
+ if (el.type === 'text') {
139
+ const textValue = (_b = (_a = (0, index_1.resolveBindingValue)(el.binding, data)) !== null && _a !== void 0 ? _a : el.defaultValue) !== null && _b !== void 0 ? _b : '';
140
+ return {
141
+ id: el.id,
142
+ type: el.type,
143
+ wrapperStyle,
144
+ contentStyle: buildTextContentStyle(el, unit),
145
+ text: `${textValue}`,
146
+ icon: buildTextIcon(el, unit),
147
+ };
148
+ }
149
+ if (el.type === 'image') {
150
+ const style = { ...(el.style || {}) };
151
+ const borderWidth = Number(style.borderWidth);
152
+ if (Number.isFinite(borderWidth) && borderWidth > 0) {
153
+ if (!style.borderStyle)
154
+ style.borderStyle = 'solid';
155
+ if (!style.borderColor)
156
+ style.borderColor = '#000000';
157
+ }
158
+ 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 : '';
159
+ const mode = el.fit === 'contain' ? 'aspectFit' : 'aspectFill';
160
+ return {
161
+ id: el.id,
162
+ type: el.type,
163
+ wrapperStyle,
164
+ contentStyle: (0, index_1.styleObjectToString)(style, unit),
165
+ src,
166
+ mode,
167
+ };
168
+ }
169
+ if (el.type === 'icon') {
170
+ 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 : '';
171
+ const text = mapIconGlyph(`${resolved}`, `${resolved}`);
172
+ return {
173
+ id: el.id,
174
+ type: el.type,
175
+ wrapperStyle,
176
+ contentStyle: buildBaseContentStyle(el, unit),
177
+ name: `${resolved}`,
178
+ text,
179
+ };
180
+ }
181
+ if (el.type === 'custom') {
182
+ return {
183
+ id: el.id,
184
+ type: el.type,
185
+ wrapperStyle,
186
+ contentStyle: buildBaseContentStyle(el, unit),
187
+ };
188
+ }
189
+ return null;
190
+ };
191
+ const buildRenderNodes = (children, data, unit) => {
192
+ if (!Array.isArray(children))
193
+ return [];
194
+ const nodes = [];
195
+ children.forEach(el => {
196
+ if (!el || el.visible === false)
197
+ return;
198
+ const node = buildRenderNode(el, data, unit);
199
+ if (node)
200
+ nodes.push(node);
201
+ });
202
+ return nodes;
203
+ };
204
+ const buildCards = (layouts, data, unit) => layouts.map(layout => ({
205
+ id: layout.name || layout.id || '',
206
+ cardStyle: buildCardStyle(layout, unit),
207
+ backgroundImage: layout.backgroundImage || '',
208
+ backgroundStyle: buildBackgroundStyle(layout, unit),
209
+ nodes: buildRenderNodes(layout.children || [], data, unit),
210
+ }));
15
211
  Component({
16
212
  options: {
17
- styleIsolation: 'apply-shared'
213
+ styleIsolation: 'apply-shared',
18
214
  },
19
215
  properties: {
20
216
  layout: {
21
217
  type: Array,
22
218
  optionalTypes: [Object],
23
- value: []
219
+ value: [],
24
220
  },
25
221
  data: {
26
222
  type: Object,
27
223
  value: {
28
- user: {}
29
- }
30
- }
224
+ user: {},
225
+ },
226
+ },
31
227
  },
32
228
  data: {
33
- cards: []
229
+ cards: [],
34
230
  },
35
231
  observers: {
36
232
  layout() {
@@ -38,27 +234,14 @@ Component({
38
234
  },
39
235
  data() {
40
236
  this.rebuild();
41
- }
237
+ },
42
238
  },
43
239
  lifetimes: {
44
240
  attached() {
45
241
  this.rebuild();
46
- }
242
+ },
47
243
  },
48
244
  methods: {
49
- normalizeIconGlyph(nodes) {
50
- return nodes.map(node => {
51
- if (node.type === 'icon') {
52
- const glyph = icon_map_1.ICON_CODE_MAP[node.name || node.text || ''];
53
- const text = glyph ? String.fromCharCode(parseInt(glyph, 16)) : node.text;
54
- return { ...node, text };
55
- }
56
- if (node.children && node.children.length) {
57
- return { ...node, children: this.normalizeIconGlyph(node.children) };
58
- }
59
- return node;
60
- });
61
- },
62
245
  rebuild() {
63
246
  const layoutInput = ensureArray(this.data.layout);
64
247
  const dataInput = (this.data.data || {});
@@ -66,15 +249,12 @@ Component({
66
249
  this.setData({ cards: [] });
67
250
  return;
68
251
  }
69
- const rendered = (0, index_1.buildRenderResult)(layoutInput, dataInput, 'rpx');
70
- const cards = rendered.map((card, idx) => ({
252
+ const normalizedLayouts = (0, index_1.normalizeLayout)(layoutInput);
253
+ const cards = buildCards(normalizedLayouts, dataInput, 'rpx').map((card, idx) => ({
254
+ ...card,
71
255
  id: pickCardId(layoutInput[idx], idx),
72
- cardStyle: card.cardStyle,
73
- backgroundImage: card.backgroundImage,
74
- backgroundStyle: card.backgroundStyle,
75
- nodes: this.normalizeIconGlyph(card.renderTree)
76
256
  }));
77
257
  this.setData({ cards });
78
- }
79
- }
258
+ },
259
+ },
80
260
  });
@@ -1,6 +1,6 @@
1
- <view class="km-card-layout-list">
1
+ <view class="km-card-layout">
2
2
  <block wx:for="{{cards}}" wx:key="id">
3
- <view class="km-card-layout-item">
3
+ <view class="km-card-layout__item">
4
4
  <view class="km-card" style="{{item.cardStyle}}">
5
5
  <image
6
6
  wx:if="{{item.backgroundImage}}"
@@ -31,7 +31,7 @@
31
31
  <block wx:elif="{{node.type === 'icon'}}">
32
32
  <view class="km-node km-node--icon" style="{{node.wrapperStyle}}">
33
33
  <view
34
- wx:if="{{node.name === 'dot'}}"
34
+ wx:if="{{node.name === 'dot' || node.name === 'round'}}"
35
35
  class="km-node__icon-dot"
36
36
  style="{{node.contentStyle}}"
37
37
  />
@@ -39,7 +39,7 @@
39
39
  wx:else
40
40
  class="km-node__icon icon"
41
41
  style="{{node.contentStyle}}"
42
- >{{node.text}}</view>
42
+ >{{node.text || ''}}</view>
43
43
  </view>
44
44
  </block>
45
45
  <block wx:elif="{{node.type === 'layout-panel'}}">
@@ -60,7 +60,23 @@
60
60
  </block>
61
61
  <block wx:else>
62
62
  <view class="km-node km-node--text" style="{{node.wrapperStyle}}">
63
- <view style="{{node.contentStyle}}">{{node.text}}</view>
63
+ <view class="km-node__text" style="{{node.contentStyle}}">
64
+ <block wx:if="{{node.icon && node.icon.name}}">
65
+ <view
66
+ class="km-node__text-content"
67
+ style="flex-direction: {{node.icon.align === 'right' ? 'row-reverse' : 'row'}};">
68
+ <text
69
+ class="km-node__text-icon icon"
70
+ style="font-size: {{node.icon.size || '16px'}}; color: {{node.icon.color || ''}}; margin-right: {{node.icon.align !== 'right' ? (node.icon.gap || '') : ''}}; margin-left: {{node.icon.align === 'right' ? (node.icon.gap || '') : ''}};">
71
+ {{node.icon.text || node.icon.name || ''}}
72
+ </text>
73
+ <text class="km-node__text-value">{{node.text || ''}}</text>
74
+ </view>
75
+ </block>
76
+ <block wx:else>
77
+ <text class="km-node__text-value">{{node.text || ''}}</text>
78
+ </block>
79
+ </view>
64
80
  </view>
65
81
  </block>
66
82
  </template>
@@ -1,11 +1,12 @@
1
1
 
2
2
 
3
- .km-card-layout-list {
3
+ .km-card-layout {
4
4
  display: flex;
5
5
  flex-direction: column;
6
+ gap: 16rpx;
6
7
  }
7
8
 
8
- .km-card-layout-item {
9
+ .km-card-layout__item {
9
10
  width: 100%;
10
11
  }
11
12
 
@@ -15,7 +16,7 @@
15
16
  box-sizing: border-box;
16
17
  color: inherit;
17
18
  font-family: 'PingFang SC', 'Microsoft Yahei', sans-serif;
18
- background: #0f1115;
19
+ background: transparent;
19
20
  }
20
21
 
21
22
  .km-card__bg {
@@ -32,11 +33,12 @@
32
33
  color: inherit;
33
34
  }
34
35
 
35
- .km-node--icon{
36
+ .km-node--icon {
36
37
  display: flex;
37
38
  align-items: center;
38
- justify-items: center;
39
+ justify-content: center;
39
40
  }
41
+
40
42
  .km-node__image {
41
43
  width: 100%;
42
44
  height: 100%;
@@ -73,8 +75,27 @@
73
75
  box-sizing: border-box;
74
76
  }
75
77
 
78
+ .km-node__text {
79
+ width: 100%;
80
+ height: 100%;
81
+ display: flex;
82
+ align-items: center;
83
+ }
84
+
85
+ .km-node__text-content {
86
+ display: inline-flex;
87
+ align-items: center;
88
+ }
89
+
90
+ .km-node__text-value {
91
+ display: inline-block;
92
+ white-space: pre-wrap;
93
+ word-break: break-word;
94
+ }
95
+
76
96
  .km-node--text text {
77
97
  display: block;
98
+ white-space: pre-wrap;
78
99
  word-break: break-word;
79
100
  }
80
101