km-card-layout-component-miniprogram 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.
- package/example/pages/home/index.js +382 -16
- package/miniprogram_dist/components/card-layout/elements/custom-element/index.js +31 -0
- package/miniprogram_dist/components/card-layout/elements/custom-element/index.json +3 -0
- package/miniprogram_dist/components/card-layout/elements/custom-element/index.wxml +5 -0
- package/miniprogram_dist/components/card-layout/elements/custom-element/index.wxss +14 -0
- package/miniprogram_dist/components/card-layout/elements/icon-element/index.js +43 -0
- package/miniprogram_dist/components/card-layout/elements/icon-element/index.json +3 -0
- package/miniprogram_dist/components/card-layout/elements/icon-element/index.wxml +5 -0
- package/miniprogram_dist/components/card-layout/elements/icon-element/index.wxss +37 -0
- package/miniprogram_dist/components/card-layout/elements/image-element/index.js +36 -0
- package/miniprogram_dist/components/card-layout/elements/image-element/index.json +3 -0
- package/miniprogram_dist/components/card-layout/elements/image-element/index.wxml +8 -0
- package/miniprogram_dist/components/card-layout/elements/image-element/index.wxss +10 -0
- package/miniprogram_dist/components/card-layout/elements/text-element/index.js +52 -0
- package/miniprogram_dist/components/card-layout/elements/text-element/index.json +3 -0
- package/miniprogram_dist/components/card-layout/elements/text-element/index.wxml +29 -0
- package/miniprogram_dist/components/card-layout/elements/text-element/index.wxss +59 -0
- package/miniprogram_dist/components/card-layout/index.js +23 -38
- package/miniprogram_dist/components/card-layout/index.json +7 -1
- package/miniprogram_dist/components/card-layout/index.wxml +15 -63
- package/miniprogram_dist/components/card-layout/index.wxss +0 -65
- package/miniprogram_dist/utils/card-schema.js +12 -3
- package/miniprogram_dist/vendor/km-card-layout-core/bindings.js +4 -0
- package/miniprogram_dist/vendor/km-card-layout-core/data.js +1 -1
- package/miniprogram_dist/vendor/km-card-layout-core/index.js +37 -6
- package/miniprogram_dist/vendor/km-card-layout-core/interface/index.js +0 -1
- package/miniprogram_dist/vendor/km-card-layout-core/ops/changeBackground.js +8 -1
- package/miniprogram_dist/vendor/km-card-layout-core/render/helpers.js +143 -0
- package/miniprogram_dist/vendor/km-card-layout-core/render/tool.js +16 -0
- package/package.json +1 -1
- package/script/sync-core.js +3 -0
- package/src/components/card-layout/elements/custom-element/index.json +3 -0
- package/src/components/card-layout/elements/custom-element/index.ts +34 -0
- package/src/components/card-layout/elements/custom-element/index.wxml +5 -0
- package/src/components/card-layout/elements/custom-element/index.wxss +14 -0
- package/src/components/card-layout/elements/icon-element/index.json +3 -0
- package/src/components/card-layout/elements/icon-element/index.ts +46 -0
- package/src/components/card-layout/elements/icon-element/index.wxml +5 -0
- package/src/components/card-layout/elements/icon-element/index.wxss +37 -0
- package/src/components/card-layout/elements/image-element/index.json +3 -0
- package/src/components/card-layout/elements/image-element/index.ts +40 -0
- package/src/components/card-layout/elements/image-element/index.wxml +8 -0
- package/src/components/card-layout/elements/image-element/index.wxss +10 -0
- package/src/components/card-layout/elements/text-element/index.json +3 -0
- package/src/components/card-layout/elements/text-element/index.ts +57 -0
- package/src/components/card-layout/elements/text-element/index.wxml +29 -0
- package/src/components/card-layout/elements/text-element/index.wxss +59 -0
- package/src/components/card-layout/index.json +7 -1
- package/src/components/card-layout/index.ts +40 -47
- package/src/components/card-layout/index.wxml +15 -63
- package/src/components/card-layout/index.wxss +0 -65
- package/src/utils/card-schema.ts +11 -4
- package/src/vendor/km-card-layout-core/bindings.ts +3 -0
- package/src/vendor/km-card-layout-core/data.ts +1 -2
- package/src/vendor/km-card-layout-core/index.ts +37 -5
- package/src/vendor/km-card-layout-core/interface/data/payload.ts +1 -0
- package/src/vendor/km-card-layout-core/interface/index.ts +1 -2
- package/src/vendor/km-card-layout-core/ops/changeBackground.ts +14 -4
- package/src/vendor/km-card-layout-core/render/helpers.ts +159 -0
- package/src/vendor/km-card-layout-core/render/tool.ts +21 -0
- package/src/vendor/km-card-layout-core/types.d.ts +74 -15
- package/src/vendor/km-card-layout-core/utils.ts +4 -0
- package/miniprogram_dist/vendor/km-card-layout-core/interface/render.js +0 -2
- package/miniprogram_dist/vendor/km-card-layout-core/render/builder.js +0 -210
- package/src/vendor/km-card-layout-core/interface/render.ts +0 -53
- package/src/vendor/km-card-layout-core/render/builder.ts +0 -288
|
@@ -6,14 +6,22 @@
|
|
|
6
6
|
export * from './interface';
|
|
7
7
|
|
|
8
8
|
import type {
|
|
9
|
-
BindingContext,
|
|
10
9
|
CardElement,
|
|
11
10
|
CardLayoutInput,
|
|
12
11
|
CardLayoutSchema,
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
TemplateItem,
|
|
13
|
+
TemplateBackground
|
|
15
14
|
} from './interface';
|
|
16
15
|
|
|
16
|
+
export type Config = {
|
|
17
|
+
showAvatar: boolean;
|
|
18
|
+
showLogo: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* 当前背景 (0 为正面背景 1 为背面背景)
|
|
21
|
+
*/
|
|
22
|
+
currentBg: TemplateBackground[];
|
|
23
|
+
};
|
|
24
|
+
|
|
17
25
|
export function addUnit(
|
|
18
26
|
value: string | number | undefined | null,
|
|
19
27
|
unit: 'px' | 'rpx'
|
|
@@ -57,8 +65,7 @@ export function areChildrenEqual(
|
|
|
57
65
|
|
|
58
66
|
export function resolveBindingValue(
|
|
59
67
|
binding: string | undefined,
|
|
60
|
-
rootData: Record<string, any
|
|
61
|
-
context?: BindingContext
|
|
68
|
+
rootData: Record<string, any>
|
|
62
69
|
): any;
|
|
63
70
|
|
|
64
71
|
export function stripLayoutBindings(
|
|
@@ -137,15 +144,67 @@ export function updateElementsStyle(
|
|
|
137
144
|
updates: Record<string, any>
|
|
138
145
|
): CardLayoutSchema;
|
|
139
146
|
|
|
140
|
-
export function
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
147
|
+
export function buildWrapperStyle(
|
|
148
|
+
el: CardElement,
|
|
149
|
+
unit?: 'px' | 'rpx'
|
|
150
|
+
): Record<string, any>;
|
|
151
|
+
|
|
152
|
+
export function buildCardStyle(
|
|
153
|
+
layout: CardLayoutSchema,
|
|
154
|
+
unit?: 'px' | 'rpx'
|
|
155
|
+
): Record<string, any>;
|
|
146
156
|
|
|
147
|
-
export function
|
|
148
|
-
layout:
|
|
149
|
-
data: Record<string, any>,
|
|
157
|
+
export function buildBackgroundStyle(
|
|
158
|
+
layout: CardLayoutSchema,
|
|
150
159
|
unit?: 'px' | 'rpx'
|
|
151
|
-
):
|
|
160
|
+
): Record<string, any>;
|
|
161
|
+
|
|
162
|
+
export function buildBaseContentStyle(
|
|
163
|
+
el: CardElement
|
|
164
|
+
): Record<string, any>;
|
|
165
|
+
|
|
166
|
+
export function buildPanelContentStyle(
|
|
167
|
+
el: import('./interface/elements').LayoutPanelElement,
|
|
168
|
+
unit?: 'px' | 'rpx'
|
|
169
|
+
): Record<string, any>;
|
|
170
|
+
|
|
171
|
+
export function buildTextContentStyle(
|
|
172
|
+
el: import('./interface/elements').TextElement,
|
|
173
|
+
unit?: 'px' | 'rpx'
|
|
174
|
+
): Record<string, any>;
|
|
175
|
+
|
|
176
|
+
export function buildImageContentStyle(
|
|
177
|
+
el: import('./interface/elements').ImageElement
|
|
178
|
+
): Record<string, any>;
|
|
179
|
+
|
|
180
|
+
export function getTextValue(
|
|
181
|
+
el: import('./interface/elements').TextElement,
|
|
182
|
+
data?: Record<string, any>
|
|
183
|
+
): string;
|
|
184
|
+
|
|
185
|
+
export function getImageSrc(
|
|
186
|
+
el: import('./interface/elements').ImageElement,
|
|
187
|
+
data?: Record<string, any>
|
|
188
|
+
): string;
|
|
189
|
+
|
|
190
|
+
export function getIconName(
|
|
191
|
+
el: import('./interface/elements').IconElement
|
|
192
|
+
): string;
|
|
193
|
+
|
|
194
|
+
export function buildTextIconMeta(
|
|
195
|
+
el: import('./interface/elements').TextElement,
|
|
196
|
+
unit?: 'px' | 'rpx'
|
|
197
|
+
): {
|
|
198
|
+
name: string;
|
|
199
|
+
size?: string;
|
|
200
|
+
gap?: string;
|
|
201
|
+
color?: string;
|
|
202
|
+
align: 'left' | 'right';
|
|
203
|
+
wrapperStyle?: Record<string, any>;
|
|
204
|
+
} | null;
|
|
205
|
+
|
|
206
|
+
export function buildRenderData (
|
|
207
|
+
layout: CardLayoutSchema[],
|
|
208
|
+
items: TemplateItem[],
|
|
209
|
+
config: Config
|
|
210
|
+
): CardLayoutSchema[];
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildRenderResult = exports.buildRenderNodes = void 0;
|
|
4
|
-
const helpers_1 = require("../helpers");
|
|
5
|
-
const layout_1 = require("../layout");
|
|
6
|
-
const data_1 = require("../data");
|
|
7
|
-
const buildCardStyle = (layout, unit) => (0, helpers_1.styleObjectToString)({
|
|
8
|
-
width: (0, helpers_1.addUnit)(layout.width, unit),
|
|
9
|
-
height: (0, helpers_1.addUnit)(layout.height, unit),
|
|
10
|
-
color: layout.fontColor,
|
|
11
|
-
borderRadius: layout.borderRadius !== undefined
|
|
12
|
-
? (0, helpers_1.addUnit)(layout.borderRadius, unit)
|
|
13
|
-
: undefined,
|
|
14
|
-
padding: layout.padding !== undefined ? (0, helpers_1.addUnit)(layout.padding, unit) : undefined,
|
|
15
|
-
position: 'relative',
|
|
16
|
-
overflow: 'hidden',
|
|
17
|
-
boxSizing: 'border-box',
|
|
18
|
-
backgroundColor: 'transparent',
|
|
19
|
-
}, unit);
|
|
20
|
-
const buildBackgroundStyle = (layout, unit) => (0, helpers_1.styleObjectToString)({
|
|
21
|
-
zIndex: layout.backgroundZIndex,
|
|
22
|
-
borderRadius: layout.borderRadius !== undefined
|
|
23
|
-
? (0, helpers_1.addUnit)(layout.borderRadius, unit)
|
|
24
|
-
: undefined,
|
|
25
|
-
width: '100%',
|
|
26
|
-
height: '100%',
|
|
27
|
-
position: 'absolute',
|
|
28
|
-
left: 0,
|
|
29
|
-
top: 0,
|
|
30
|
-
}, unit);
|
|
31
|
-
const buildWrapperStyle = (el, unit) => {
|
|
32
|
-
const abs = (0, layout_1.getAbsLayout)(el);
|
|
33
|
-
if (!abs)
|
|
34
|
-
return '';
|
|
35
|
-
return (0, helpers_1.styleObjectToString)({
|
|
36
|
-
position: 'absolute',
|
|
37
|
-
left: (0, helpers_1.addUnit)(abs.x, unit),
|
|
38
|
-
top: (0, helpers_1.addUnit)(abs.y, unit),
|
|
39
|
-
width: (0, helpers_1.addUnit)(abs.width, unit),
|
|
40
|
-
height: (0, helpers_1.addUnit)(abs.height, unit),
|
|
41
|
-
zIndex: abs.zIndex,
|
|
42
|
-
boxSizing: 'border-box',
|
|
43
|
-
}, unit);
|
|
44
|
-
};
|
|
45
|
-
const buildPanelContentStyle = (el, unit) => (0, helpers_1.styleObjectToString)({
|
|
46
|
-
position: 'relative',
|
|
47
|
-
width: '100%',
|
|
48
|
-
height: '100%',
|
|
49
|
-
display: 'block',
|
|
50
|
-
boxSizing: 'border-box',
|
|
51
|
-
...(el.style || {}),
|
|
52
|
-
}, unit);
|
|
53
|
-
const buildTextContentStyle = (el, unit) => {
|
|
54
|
-
var _a, _b, _c;
|
|
55
|
-
const textAlign = ((_a = el.style) === null || _a === void 0 ? void 0 : _a.textAlign) || el.align || undefined;
|
|
56
|
-
const style = {
|
|
57
|
-
...(el.style || {}),
|
|
58
|
-
whiteSpace: 'pre-wrap',
|
|
59
|
-
wordBreak: 'break-word',
|
|
60
|
-
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
|
|
61
|
-
? el.style.lineHeight
|
|
62
|
-
: '1',
|
|
63
|
-
};
|
|
64
|
-
if (textAlign)
|
|
65
|
-
style.textAlign = textAlign;
|
|
66
|
-
return (0, helpers_1.styleObjectToString)(style, unit);
|
|
67
|
-
};
|
|
68
|
-
const buildBaseContentStyle = (el, unit) => (0, helpers_1.styleObjectToString)({
|
|
69
|
-
...(el.style || {}),
|
|
70
|
-
boxSizing: 'border-box',
|
|
71
|
-
}, unit);
|
|
72
|
-
const buildImageContentStyle = (el, unit) => {
|
|
73
|
-
const style = { ...(el.style || {}) };
|
|
74
|
-
const borderWidth = Number(style.borderWidth);
|
|
75
|
-
if (Number.isFinite(borderWidth) && borderWidth > 0) {
|
|
76
|
-
if (!style.borderStyle)
|
|
77
|
-
style.borderStyle = 'solid';
|
|
78
|
-
if (!style.borderColor)
|
|
79
|
-
style.borderColor = '#000000';
|
|
80
|
-
}
|
|
81
|
-
return (0, helpers_1.styleObjectToString)(style, unit);
|
|
82
|
-
};
|
|
83
|
-
const buildTextIcon = (el, unit) => {
|
|
84
|
-
var _a, _b, _c, _d;
|
|
85
|
-
const icon = el.icon;
|
|
86
|
-
if (!icon || icon.enable === false)
|
|
87
|
-
return undefined;
|
|
88
|
-
const style = icon.style || 'fill';
|
|
89
|
-
const baseName = el.key || el.binding || el.id;
|
|
90
|
-
let name;
|
|
91
|
-
if (style === 'dot')
|
|
92
|
-
name = 'round';
|
|
93
|
-
else if (style === 'line')
|
|
94
|
-
name = baseName ? `${baseName}-line` : undefined;
|
|
95
|
-
else
|
|
96
|
-
name = baseName || undefined;
|
|
97
|
-
if (!name)
|
|
98
|
-
return undefined;
|
|
99
|
-
const size = icon.size !== undefined && icon.size !== null
|
|
100
|
-
? icon.size
|
|
101
|
-
: (_a = el.style) === null || _a === void 0 ? void 0 : _a.fontSize;
|
|
102
|
-
const gap = icon.gap !== undefined && icon.gap !== null ? icon.gap : 4;
|
|
103
|
-
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);
|
|
104
|
-
return {
|
|
105
|
-
name: `${name}`,
|
|
106
|
-
text: `${name}`,
|
|
107
|
-
size: (0, helpers_1.addUnit)(size, unit),
|
|
108
|
-
gap: (0, helpers_1.addUnit)(gap, unit),
|
|
109
|
-
color: color,
|
|
110
|
-
align: icon.align || 'left',
|
|
111
|
-
wrapperStyle: (0, helpers_1.styleObjectToString)({
|
|
112
|
-
display: 'inline-flex',
|
|
113
|
-
alignItems: 'center',
|
|
114
|
-
height: ((_d = el.style) === null || _d === void 0 ? void 0 : _d.lineHeight) || '1',
|
|
115
|
-
}, unit),
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
const buildNode = (el, rootData, unit, context) => {
|
|
119
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
120
|
-
if (!el || el.visible === false)
|
|
121
|
-
return null;
|
|
122
|
-
const wrapperStyle = buildWrapperStyle(el, unit);
|
|
123
|
-
if (el.type === 'layout-panel') {
|
|
124
|
-
return {
|
|
125
|
-
id: el.id,
|
|
126
|
-
type: el.type,
|
|
127
|
-
visible: el.visible,
|
|
128
|
-
wrapperStyle,
|
|
129
|
-
contentStyle: buildPanelContentStyle(el, unit),
|
|
130
|
-
children: (0, exports.buildRenderNodes)(el.children || [], rootData, unit, context),
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
const baseStyle = buildBaseContentStyle(el, unit);
|
|
134
|
-
if (el.type === 'text') {
|
|
135
|
-
const value = (_b = (_a = (0, data_1.resolveBindingValue)(el.binding, rootData, context)) !== null && _a !== void 0 ? _a : el.defaultValue) !== null && _b !== void 0 ? _b : '';
|
|
136
|
-
return {
|
|
137
|
-
id: el.id,
|
|
138
|
-
type: el.type,
|
|
139
|
-
wrapperStyle,
|
|
140
|
-
contentStyle: buildTextContentStyle(el, unit),
|
|
141
|
-
text: `${value}`,
|
|
142
|
-
visible: el.visible,
|
|
143
|
-
icon: buildTextIcon(el, unit),
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
if (el.type === 'image') {
|
|
147
|
-
const src = (_e = (_d = (_c = (0, data_1.resolveBindingValue)(el.binding, rootData, context)) !== null && _c !== void 0 ? _c : el.defaultUrl) !== null && _d !== void 0 ? _d : el.defaultValue) !== null && _e !== void 0 ? _e : '';
|
|
148
|
-
const mode = el.fit === 'contain' ? 'aspectFit' : 'aspectFill';
|
|
149
|
-
return {
|
|
150
|
-
id: el.id,
|
|
151
|
-
type: el.type,
|
|
152
|
-
wrapperStyle,
|
|
153
|
-
contentStyle: buildImageContentStyle(el, unit),
|
|
154
|
-
src,
|
|
155
|
-
mode,
|
|
156
|
-
visible: el.visible,
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
if (el.type === 'icon') {
|
|
160
|
-
const name = (_h = (_g = (_f = (0, data_1.resolveBindingValue)(el.binding, rootData, context)) !== null && _f !== void 0 ? _f : el.name) !== null && _g !== void 0 ? _g : el.defaultValue) !== null && _h !== void 0 ? _h : '';
|
|
161
|
-
return {
|
|
162
|
-
id: el.id,
|
|
163
|
-
type: el.type,
|
|
164
|
-
wrapperStyle,
|
|
165
|
-
contentStyle: baseStyle,
|
|
166
|
-
name: `${name}`,
|
|
167
|
-
text: `${name}`,
|
|
168
|
-
visible: el.visible,
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
if (el.type === 'custom') {
|
|
172
|
-
return {
|
|
173
|
-
id: el.id,
|
|
174
|
-
type: el.type,
|
|
175
|
-
wrapperStyle,
|
|
176
|
-
contentStyle: baseStyle,
|
|
177
|
-
visible: el.visible,
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
return null;
|
|
181
|
-
};
|
|
182
|
-
const buildRenderNodes = (children, rootData, unit = 'px', context = {}) => {
|
|
183
|
-
if (!Array.isArray(children))
|
|
184
|
-
return [];
|
|
185
|
-
const nodes = [];
|
|
186
|
-
children.forEach(el => {
|
|
187
|
-
if (!el || el.visible === false)
|
|
188
|
-
return;
|
|
189
|
-
const node = buildNode(el, rootData, unit, context);
|
|
190
|
-
if (node)
|
|
191
|
-
nodes.push(node);
|
|
192
|
-
});
|
|
193
|
-
return nodes;
|
|
194
|
-
};
|
|
195
|
-
exports.buildRenderNodes = buildRenderNodes;
|
|
196
|
-
const buildRenderResult = (layoutInput, dataInput, unit = 'px') => {
|
|
197
|
-
const layouts = (0, layout_1.normalizeLayout)(layoutInput);
|
|
198
|
-
return layouts.map(layout => {
|
|
199
|
-
const cardStyle = buildCardStyle(layout, unit);
|
|
200
|
-
const bgStyle = buildBackgroundStyle(layout, unit);
|
|
201
|
-
const renderTree = (0, exports.buildRenderNodes)(layout.children || [], dataInput || {}, unit);
|
|
202
|
-
return {
|
|
203
|
-
renderTree,
|
|
204
|
-
cardStyle,
|
|
205
|
-
backgroundImage: layout.backgroundImage || '',
|
|
206
|
-
backgroundStyle: bgStyle,
|
|
207
|
-
};
|
|
208
|
-
});
|
|
209
|
-
};
|
|
210
|
-
exports.buildRenderResult = buildRenderResult;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import type { CardElementType, CardElement } from './elements';
|
|
2
|
-
import type { CardLayoutInput, CardLayoutSchema } from './layout';
|
|
3
|
-
|
|
4
|
-
export interface RenderNodeIcon {
|
|
5
|
-
name: string;
|
|
6
|
-
text?: string;
|
|
7
|
-
size?: string;
|
|
8
|
-
color?: string;
|
|
9
|
-
gap?: string;
|
|
10
|
-
align?: 'left' | 'right';
|
|
11
|
-
wrapperStyle?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface RenderNode {
|
|
15
|
-
id: string;
|
|
16
|
-
type: CardElementType | 'text';
|
|
17
|
-
wrapperStyle: string;
|
|
18
|
-
contentStyle: string;
|
|
19
|
-
name?: string;
|
|
20
|
-
text?: string;
|
|
21
|
-
src?: string;
|
|
22
|
-
mode?: string;
|
|
23
|
-
children?: RenderNode[];
|
|
24
|
-
visible?: boolean;
|
|
25
|
-
icon?: RenderNodeIcon;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface RenderPageResult {
|
|
29
|
-
renderTree: RenderNode[];
|
|
30
|
-
cardStyle: string;
|
|
31
|
-
backgroundImage: string;
|
|
32
|
-
backgroundStyle: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export type RenderResult = RenderPageResult[];
|
|
36
|
-
|
|
37
|
-
export interface BindingContext {
|
|
38
|
-
/**
|
|
39
|
-
* 当前上下文数据的绑定前缀(预留给嵌套场景)
|
|
40
|
-
*/
|
|
41
|
-
contextBinding?: string;
|
|
42
|
-
/**
|
|
43
|
-
* 当前上下文数据(预留给嵌套场景)
|
|
44
|
-
*/
|
|
45
|
-
contextData?: any;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export type {
|
|
49
|
-
CardElement,
|
|
50
|
-
CardElementType,
|
|
51
|
-
CardLayoutInput,
|
|
52
|
-
CardLayoutSchema,
|
|
53
|
-
};
|
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CardElement,
|
|
3
|
-
IconElement,
|
|
4
|
-
ImageElement,
|
|
5
|
-
LayoutPanelElement,
|
|
6
|
-
TextElement,
|
|
7
|
-
} from '../interface/elements';
|
|
8
|
-
import type { CardLayoutSchema, CardLayoutInput } from '../interface/layout';
|
|
9
|
-
import type {
|
|
10
|
-
BindingContext,
|
|
11
|
-
RenderNode,
|
|
12
|
-
RenderResult,
|
|
13
|
-
} from '../interface/render';
|
|
14
|
-
import { addUnit, styleObjectToString } from '../helpers';
|
|
15
|
-
import { getAbsLayout, normalizeLayout } from '../layout';
|
|
16
|
-
import { resolveBindingValue } from '../data';
|
|
17
|
-
|
|
18
|
-
const buildCardStyle = (layout: CardLayoutSchema, unit: 'px' | 'rpx') =>
|
|
19
|
-
styleObjectToString(
|
|
20
|
-
{
|
|
21
|
-
width: addUnit(layout.width, unit),
|
|
22
|
-
height: addUnit(layout.height, unit),
|
|
23
|
-
color: layout.fontColor,
|
|
24
|
-
borderRadius:
|
|
25
|
-
layout.borderRadius !== undefined
|
|
26
|
-
? addUnit(layout.borderRadius, unit)
|
|
27
|
-
: undefined,
|
|
28
|
-
padding:
|
|
29
|
-
layout.padding !== undefined ? addUnit(layout.padding, unit) : undefined,
|
|
30
|
-
position: 'relative',
|
|
31
|
-
overflow: 'hidden',
|
|
32
|
-
boxSizing: 'border-box',
|
|
33
|
-
backgroundColor: 'transparent',
|
|
34
|
-
},
|
|
35
|
-
unit
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
const buildBackgroundStyle = (layout: CardLayoutSchema, unit: 'px' | 'rpx') =>
|
|
39
|
-
styleObjectToString(
|
|
40
|
-
{
|
|
41
|
-
zIndex: layout.backgroundZIndex,
|
|
42
|
-
borderRadius:
|
|
43
|
-
layout.borderRadius !== undefined
|
|
44
|
-
? addUnit(layout.borderRadius, unit)
|
|
45
|
-
: undefined,
|
|
46
|
-
width: '100%',
|
|
47
|
-
height: '100%',
|
|
48
|
-
position: 'absolute',
|
|
49
|
-
left: 0,
|
|
50
|
-
top: 0,
|
|
51
|
-
},
|
|
52
|
-
unit
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
const buildWrapperStyle = (el: CardElement, unit: 'px' | 'rpx'): string => {
|
|
56
|
-
const abs = getAbsLayout(el);
|
|
57
|
-
if (!abs) return '';
|
|
58
|
-
return styleObjectToString(
|
|
59
|
-
{
|
|
60
|
-
position: 'absolute',
|
|
61
|
-
left: addUnit(abs.x, unit),
|
|
62
|
-
top: addUnit(abs.y, unit),
|
|
63
|
-
width: addUnit(abs.width, unit),
|
|
64
|
-
height: addUnit(abs.height, unit),
|
|
65
|
-
zIndex: abs.zIndex,
|
|
66
|
-
boxSizing: 'border-box',
|
|
67
|
-
},
|
|
68
|
-
unit
|
|
69
|
-
);
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const buildPanelContentStyle = (
|
|
73
|
-
el: LayoutPanelElement,
|
|
74
|
-
unit: 'px' | 'rpx'
|
|
75
|
-
) =>
|
|
76
|
-
styleObjectToString(
|
|
77
|
-
{
|
|
78
|
-
position: 'relative',
|
|
79
|
-
width: '100%',
|
|
80
|
-
height: '100%',
|
|
81
|
-
display: 'block',
|
|
82
|
-
boxSizing: 'border-box',
|
|
83
|
-
...(el.style || {}),
|
|
84
|
-
},
|
|
85
|
-
unit
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
const buildTextContentStyle = (el: TextElement, unit: 'px' | 'rpx') => {
|
|
89
|
-
const textAlign =
|
|
90
|
-
(el.style?.textAlign as string | undefined) || el.align || undefined;
|
|
91
|
-
const style: Record<string, any> = {
|
|
92
|
-
...(el.style || {}),
|
|
93
|
-
whiteSpace: 'pre-wrap',
|
|
94
|
-
wordBreak: 'break-word',
|
|
95
|
-
lineHeight:
|
|
96
|
-
el.style?.lineHeight !== undefined && el.style?.lineHeight !== null
|
|
97
|
-
? el.style.lineHeight
|
|
98
|
-
: '1',
|
|
99
|
-
};
|
|
100
|
-
if (textAlign) style.textAlign = textAlign;
|
|
101
|
-
return styleObjectToString(style, unit);
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const buildBaseContentStyle = (el: CardElement, unit: 'px' | 'rpx') =>
|
|
105
|
-
styleObjectToString(
|
|
106
|
-
{
|
|
107
|
-
...(el.style || {}),
|
|
108
|
-
boxSizing: 'border-box',
|
|
109
|
-
},
|
|
110
|
-
unit
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
const buildImageContentStyle = (el: ImageElement, unit: 'px' | 'rpx') => {
|
|
114
|
-
const style: Record<string, any> = { ...(el.style || {}) };
|
|
115
|
-
const borderWidth = Number(style.borderWidth);
|
|
116
|
-
if (Number.isFinite(borderWidth) && borderWidth > 0) {
|
|
117
|
-
if (!style.borderStyle) style.borderStyle = 'solid';
|
|
118
|
-
if (!style.borderColor) style.borderColor = '#000000';
|
|
119
|
-
}
|
|
120
|
-
return styleObjectToString(style, unit);
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
const buildTextIcon = (
|
|
124
|
-
el: TextElement,
|
|
125
|
-
unit: 'px' | 'rpx'
|
|
126
|
-
): RenderNode['icon'] | undefined => {
|
|
127
|
-
const icon = el.icon;
|
|
128
|
-
if (!icon || icon.enable === false) return undefined;
|
|
129
|
-
|
|
130
|
-
const style = icon.style || 'fill';
|
|
131
|
-
const baseName = el.key || el.binding || el.id;
|
|
132
|
-
let name: string | undefined;
|
|
133
|
-
if (style === 'dot') name = 'round';
|
|
134
|
-
else if (style === 'line') name = baseName ? `${baseName}-line` : undefined;
|
|
135
|
-
else name = baseName || undefined;
|
|
136
|
-
if (!name) return undefined;
|
|
137
|
-
|
|
138
|
-
const size =
|
|
139
|
-
icon.size !== undefined && icon.size !== null
|
|
140
|
-
? icon.size
|
|
141
|
-
: (el.style?.fontSize as any);
|
|
142
|
-
const gap = icon.gap !== undefined && icon.gap !== null ? icon.gap : 4;
|
|
143
|
-
const color =
|
|
144
|
-
icon.color ??
|
|
145
|
-
((typeof el.style?.color === 'string' ? el.style.color : undefined) as
|
|
146
|
-
| string
|
|
147
|
-
| undefined);
|
|
148
|
-
|
|
149
|
-
return {
|
|
150
|
-
name: `${name}`,
|
|
151
|
-
text: `${name}`,
|
|
152
|
-
size: addUnit(size as any, unit),
|
|
153
|
-
gap: addUnit(gap as any, unit),
|
|
154
|
-
color: color as any,
|
|
155
|
-
align: icon.align || 'left',
|
|
156
|
-
wrapperStyle: styleObjectToString(
|
|
157
|
-
{
|
|
158
|
-
display: 'inline-flex',
|
|
159
|
-
alignItems: 'center',
|
|
160
|
-
height: el.style?.lineHeight || '1',
|
|
161
|
-
},
|
|
162
|
-
unit
|
|
163
|
-
),
|
|
164
|
-
};
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
const buildNode = (
|
|
168
|
-
el: CardElement,
|
|
169
|
-
rootData: Record<string, any>,
|
|
170
|
-
unit: 'px' | 'rpx',
|
|
171
|
-
context: BindingContext
|
|
172
|
-
): RenderNode | null => {
|
|
173
|
-
if (!el || el.visible === false) return null;
|
|
174
|
-
const wrapperStyle = buildWrapperStyle(el, unit);
|
|
175
|
-
|
|
176
|
-
if (el.type === 'layout-panel') {
|
|
177
|
-
return {
|
|
178
|
-
id: el.id,
|
|
179
|
-
type: el.type,
|
|
180
|
-
visible: el.visible,
|
|
181
|
-
wrapperStyle,
|
|
182
|
-
contentStyle: buildPanelContentStyle(el as LayoutPanelElement, unit),
|
|
183
|
-
children: buildRenderNodes(el.children || [], rootData, unit, context),
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const baseStyle = buildBaseContentStyle(el, unit);
|
|
188
|
-
if (el.type === 'text') {
|
|
189
|
-
const value =
|
|
190
|
-
resolveBindingValue(el.binding, rootData, context) ??
|
|
191
|
-
el.defaultValue ??
|
|
192
|
-
'';
|
|
193
|
-
return {
|
|
194
|
-
id: el.id,
|
|
195
|
-
type: el.type,
|
|
196
|
-
wrapperStyle,
|
|
197
|
-
contentStyle: buildTextContentStyle(el as TextElement, unit),
|
|
198
|
-
text: `${value}`,
|
|
199
|
-
visible: el.visible,
|
|
200
|
-
icon: buildTextIcon(el as TextElement, unit),
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (el.type === 'image') {
|
|
205
|
-
const src =
|
|
206
|
-
resolveBindingValue(el.binding, rootData, context) ??
|
|
207
|
-
(el as ImageElement).defaultUrl ??
|
|
208
|
-
el.defaultValue ??
|
|
209
|
-
'';
|
|
210
|
-
const mode =
|
|
211
|
-
(el as ImageElement).fit === 'contain' ? 'aspectFit' : 'aspectFill';
|
|
212
|
-
return {
|
|
213
|
-
id: el.id,
|
|
214
|
-
type: el.type,
|
|
215
|
-
wrapperStyle,
|
|
216
|
-
contentStyle: buildImageContentStyle(el as ImageElement, unit),
|
|
217
|
-
src,
|
|
218
|
-
mode,
|
|
219
|
-
visible: el.visible,
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (el.type === 'icon') {
|
|
224
|
-
const name =
|
|
225
|
-
resolveBindingValue(el.binding, rootData, context) ??
|
|
226
|
-
(el as IconElement).name ??
|
|
227
|
-
el.defaultValue ??
|
|
228
|
-
'';
|
|
229
|
-
return {
|
|
230
|
-
id: el.id,
|
|
231
|
-
type: el.type,
|
|
232
|
-
wrapperStyle,
|
|
233
|
-
contentStyle: baseStyle,
|
|
234
|
-
name: `${name}`,
|
|
235
|
-
text: `${name}`,
|
|
236
|
-
visible: el.visible,
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if (el.type === 'custom') {
|
|
241
|
-
return {
|
|
242
|
-
id: el.id,
|
|
243
|
-
type: el.type,
|
|
244
|
-
wrapperStyle,
|
|
245
|
-
contentStyle: baseStyle,
|
|
246
|
-
visible: el.visible,
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return null;
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
export const buildRenderNodes = (
|
|
254
|
-
children: CardElement[],
|
|
255
|
-
rootData: Record<string, any>,
|
|
256
|
-
unit: 'px' | 'rpx' = 'px',
|
|
257
|
-
context: BindingContext = {}
|
|
258
|
-
): RenderNode[] => {
|
|
259
|
-
if (!Array.isArray(children)) return [];
|
|
260
|
-
|
|
261
|
-
const nodes: RenderNode[] = [];
|
|
262
|
-
children.forEach(el => {
|
|
263
|
-
if (!el || el.visible === false) return;
|
|
264
|
-
const node = buildNode(el, rootData, unit, context);
|
|
265
|
-
if (node) nodes.push(node);
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
return nodes;
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
export const buildRenderResult = (
|
|
272
|
-
layoutInput: CardLayoutInput,
|
|
273
|
-
dataInput: Record<string, any>,
|
|
274
|
-
unit: 'px' | 'rpx' = 'px'
|
|
275
|
-
): RenderResult => {
|
|
276
|
-
const layouts = normalizeLayout(layoutInput);
|
|
277
|
-
return layouts.map(layout => {
|
|
278
|
-
const cardStyle = buildCardStyle(layout, unit);
|
|
279
|
-
const bgStyle = buildBackgroundStyle(layout, unit);
|
|
280
|
-
const renderTree = buildRenderNodes(layout.children || [], dataInput || {}, unit);
|
|
281
|
-
return {
|
|
282
|
-
renderTree,
|
|
283
|
-
cardStyle,
|
|
284
|
-
backgroundImage: layout.backgroundImage || '',
|
|
285
|
-
backgroundStyle: bgStyle,
|
|
286
|
-
};
|
|
287
|
-
});
|
|
288
|
-
};
|