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

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/example/pages/home/index.js +4 -250
  2. package/miniprogram_dist/components/card-layout/index.js +214 -29
  3. package/miniprogram_dist/components/card-layout/index.wxml +29 -5
  4. package/miniprogram_dist/components/card-layout/index.wxss +28 -6
  5. package/miniprogram_dist/vendor/km-card-layout-core/index.js +58 -181
  6. package/miniprogram_dist/vendor/km-card-layout-core/interface/data/payload.js +2 -0
  7. package/miniprogram_dist/vendor/km-card-layout-core/interface/elements.js +2 -0
  8. package/miniprogram_dist/vendor/km-card-layout-core/interface/index.js +20 -0
  9. package/miniprogram_dist/vendor/km-card-layout-core/interface/layout.js +2 -0
  10. package/miniprogram_dist/vendor/km-card-layout-core/interface/render.js +2 -0
  11. package/miniprogram_dist/vendor/km-card-layout-core/utils.js +132 -0
  12. package/package.json +1 -1
  13. package/script/sync-core.js +10 -2
  14. package/src/components/card-layout/index.ts +340 -56
  15. package/src/components/card-layout/index.wxml +29 -5
  16. package/src/components/card-layout/index.wxss +28 -6
  17. package/src/vendor/km-card-layout-core/index.ts +153 -461
  18. package/src/vendor/km-card-layout-core/interface/data/payload.ts +45 -0
  19. package/src/vendor/km-card-layout-core/interface/elements.ts +73 -0
  20. package/src/vendor/km-card-layout-core/interface/index.ts +4 -0
  21. package/src/vendor/km-card-layout-core/interface/layout.ts +19 -0
  22. package/src/vendor/km-card-layout-core/interface/render.ts +52 -0
  23. package/src/vendor/km-card-layout-core/types.d.ts +22 -154
  24. package/src/vendor/km-card-layout-core/utils.ts +141 -0
@@ -0,0 +1,45 @@
1
+ import { CardLayoutSchema } from "../layout";
2
+
3
+ /**
4
+ * 背景项
5
+ */
6
+ export interface TemplateBackground {
7
+ id: number;
8
+ name: string;
9
+ fileId?: number;
10
+ fontColor?: string;
11
+ fontColorExtra?: { name: string | string[]; color: string }[];
12
+ imgUrl: string;
13
+ }
14
+
15
+ /**
16
+ * 元素项
17
+ */
18
+ export interface TemplateItem {
19
+ id: number;
20
+ name: string;
21
+ bind: string;
22
+ key?: string;
23
+ icon?: string;
24
+ default?: string;
25
+ type: 'text' | 'image';
26
+ cate: number;
27
+ required?: 0 | 1;
28
+ }
29
+
30
+ export interface TemplateLayoutInfo {
31
+ id: number | string;
32
+ name?: string;
33
+ previewImg?: string;
34
+ height?: number;
35
+ width?: number;
36
+ enableBgIds?: string | null;
37
+ enableItemIds?: string | null;
38
+ content?: CardLayoutSchema[];
39
+ isVip?: 0 | 1;
40
+ isDouble?: 0 | 1;
41
+ isMoreCompany?: 0 | 1;
42
+ isBgCustomize?: 0 | 1;
43
+ createAt?: string;
44
+ updateAt?: string;
45
+ }
@@ -0,0 +1,73 @@
1
+ export type CardElementType =
2
+ | 'text'
3
+ | 'image'
4
+ | 'icon'
5
+ | 'custom'
6
+ | 'layout-panel';
7
+
8
+ export interface AbsoluteLayoutDefinition {
9
+ mode: 'absolute';
10
+ x: number;
11
+ y: number;
12
+ width: number;
13
+ height: number;
14
+ zIndex?: number;
15
+ }
16
+
17
+ export interface CardElementBase {
18
+ id: string;
19
+ type: CardElementType;
20
+ layout: AbsoluteLayoutDefinition;
21
+ visible?: boolean;
22
+ binding?: string;
23
+ style?: Record<string, string | number | undefined>;
24
+ defaultValue?: string;
25
+ key?: string;
26
+ }
27
+
28
+ export interface TextIconConfig {
29
+ name?: string;
30
+ size?: number;
31
+ color?: string;
32
+ gap?: number;
33
+ align?: 'left' | 'right';
34
+ enable?: boolean;
35
+ style?: 'fill' | 'dot' | 'line';
36
+ }
37
+
38
+ export interface TextElement extends CardElementBase {
39
+ type: 'text';
40
+ align?: 'left' | 'center' | 'right';
41
+ icon?: TextIconConfig;
42
+ }
43
+
44
+ export interface ImageElement extends CardElementBase {
45
+ type: 'image';
46
+ alt?: string;
47
+ fit?: 'cover' | 'contain';
48
+ defaultUrl?: string;
49
+ }
50
+
51
+ export interface IconElement extends CardElementBase {
52
+ type: 'icon';
53
+ name?: string;
54
+ }
55
+
56
+ export interface CustomElement extends CardElementBase {
57
+ type: 'custom';
58
+ }
59
+
60
+ export interface LayoutPanelElement extends CardElementBase {
61
+ type: 'layout-panel';
62
+ container: {
63
+ mode: 'absolute';
64
+ };
65
+ children?: CardElement[];
66
+ }
67
+
68
+ export type CardElement =
69
+ | TextElement
70
+ | ImageElement
71
+ | IconElement
72
+ | CustomElement
73
+ | LayoutPanelElement;
@@ -0,0 +1,4 @@
1
+ export * from './elements';
2
+ export * from './layout';
3
+ export * from './render';
4
+ export * from './data/payload';
@@ -0,0 +1,19 @@
1
+ import type { CardElement } from './elements';
2
+
3
+ export interface CardLayoutSchema {
4
+ name?: string;
5
+ container: {
6
+ mode: 'absolute';
7
+ };
8
+ width: number;
9
+ height: number;
10
+ backgroundImage?: string;
11
+ backgroundZIndex?: number;
12
+ fontColor?: string;
13
+ borderRadius?: number;
14
+ padding?: number;
15
+ children: CardElement[];
16
+ thumbnail?: string;
17
+ }
18
+
19
+ export type CardLayoutInput = CardLayoutSchema[];
@@ -0,0 +1,52 @@
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
+ }
12
+
13
+ export interface RenderNode {
14
+ id: string;
15
+ type: CardElementType | 'text';
16
+ wrapperStyle: string;
17
+ contentStyle: string;
18
+ name?: string;
19
+ text?: string;
20
+ src?: string;
21
+ mode?: string;
22
+ children?: RenderNode[];
23
+ visible?: boolean;
24
+ icon?: RenderNodeIcon;
25
+ }
26
+
27
+ export interface RenderPageResult {
28
+ renderTree: RenderNode[];
29
+ cardStyle: string;
30
+ backgroundImage: string;
31
+ backgroundStyle: string;
32
+ }
33
+
34
+ export type RenderResult = RenderPageResult[];
35
+
36
+ export interface BindingContext {
37
+ /**
38
+ * 当前上下文数据的绑定前缀(预留给嵌套场景)
39
+ */
40
+ contextBinding?: string;
41
+ /**
42
+ * 当前上下文数据(预留给嵌套场景)
43
+ */
44
+ contextData?: any;
45
+ }
46
+
47
+ export type {
48
+ CardElement,
49
+ CardElementType,
50
+ CardLayoutInput,
51
+ CardLayoutSchema,
52
+ };
@@ -3,159 +3,16 @@
3
3
  * Mirrors the exported types/functions from index.ts.
4
4
  */
5
5
 
6
- export type CardElementType =
7
- | 'text'
8
- | 'image'
9
- | 'icon'
10
- | 'custom'
11
- | 'layout-panel'
12
- | 'repeatable-group';
6
+ export * from './interface';
13
7
 
14
- export interface AbsoluteLayoutDefinition {
15
- mode: 'absolute';
16
- x: number;
17
- y: number;
18
- width: number;
19
- height: number;
20
- zIndex?: number;
21
- }
22
-
23
- export interface FlexItemOptions {
24
- flexGrow?: number;
25
- flexShrink?: number;
26
- flexBasis?: number | string;
27
- order?: number;
28
- alignSelf?: 'flex-start' | 'flex-end' | 'center' | 'stretch' | 'baseline';
29
- }
30
-
31
- export interface FlexItemLayoutDefinition {
32
- mode: 'flex';
33
- item?: FlexItemOptions;
34
- width?: number;
35
- height?: number;
36
- }
37
-
38
- export type ElementLayout = AbsoluteLayoutDefinition | FlexItemLayoutDefinition;
39
-
40
- export interface CardElementBase {
41
- id: string;
42
- type: CardElementType;
43
- layout: ElementLayout;
44
- visible?: boolean;
45
- binding?: string;
46
- style?: Record<string, string | number | undefined>;
47
- defaultValue?: string;
48
- }
49
-
50
- export interface TextElement extends CardElementBase {
51
- type: 'text';
52
- align?: 'left' | 'center' | 'right';
53
- multiline?: boolean;
54
- }
55
-
56
- export interface ImageElement extends CardElementBase {
57
- type: 'image';
58
- alt?: string;
59
- fit?: 'cover' | 'contain';
60
- defaultUrl?: string;
61
- }
62
-
63
- export interface IconElement extends CardElementBase {
64
- type: 'icon';
65
- name?: string;
66
- }
67
-
68
- export interface CustomElement extends CardElementBase {
69
- type: 'custom';
70
- }
71
-
72
- export interface LayoutPanelElement extends CardElementBase {
73
- type: 'layout-panel';
74
- container: {
75
- mode: 'absolute' | 'flex';
76
- options?: {
77
- direction?: 'row' | 'column';
78
- wrap?: 'nowrap' | 'wrap';
79
- justifyContent?:
80
- | 'flex-start'
81
- | 'flex-end'
82
- | 'center'
83
- | 'space-between'
84
- | 'space-around'
85
- | 'space-evenly';
86
- alignItems?: 'flex-start' | 'flex-end' | 'center' | 'stretch' | 'baseline';
87
- gap?: number | { row: number; column: number };
88
- padding?: number | [number, number] | [number, number, number, number];
89
- };
90
- };
91
- children?: CardElement[];
92
- }
93
-
94
- export interface RepeatableGroupItem {
95
- id: string;
96
- elements: CardElement[];
97
- data?: Record<string, any>;
98
- }
99
-
100
- export interface RepeatableGroupElement extends CardElementBase {
101
- type: 'repeatable-group';
102
- dataPath: string;
103
- itemTemplate: CardElement[];
104
- items: RepeatableGroupItem[];
105
- maxItems?: number;
106
- mutualExcludes?: string[];
107
- }
108
-
109
- export type CardElement =
110
- | TextElement
111
- | ImageElement
112
- | IconElement
113
- | CustomElement
114
- | LayoutPanelElement
115
- | RepeatableGroupElement;
116
-
117
- export interface CardLayoutSchema {
118
- name?: string;
119
- container: {
120
- mode: 'absolute';
121
- };
122
- width: number;
123
- height: number;
124
- backgroundImage?: string;
125
- backgroundZIndex?: number;
126
- fontColor?: string;
127
- borderRadius?: number;
128
- padding?: number;
129
- children: CardElement[];
130
- thumbnail?: string;
131
- }
132
-
133
- export interface RenderNode {
134
- id: string;
135
- type: CardElementType | 'text';
136
- wrapperStyle: string;
137
- contentStyle: string;
138
- name?: string;
139
- text?: string;
140
- src?: string;
141
- mode?: string;
142
- children?: RenderNode[];
143
- visible?: boolean;
144
- }
145
-
146
- export interface RenderPageResult {
147
- renderTree: RenderNode[];
148
- cardStyle: string;
149
- backgroundImage: string;
150
- backgroundStyle: string;
151
- }
152
-
153
- export type RenderResult = RenderPageResult[];
154
-
155
- export interface BindingContext {
156
- contextBinding?: string;
157
- contextData?: any;
158
- }
8
+ import type {
9
+ BindingContext,
10
+ CardElement,
11
+ CardLayoutInput,
12
+ CardLayoutSchema,
13
+ RenderNode,
14
+ RenderResult,
15
+ } from './interface';
159
16
 
160
17
  export function addUnit(
161
18
  value: string | number | undefined | null,
@@ -167,8 +24,6 @@ export function styleObjectToString(
167
24
  unit?: 'px' | 'rpx'
168
25
  ): string;
169
26
 
170
- export type CardLayoutInput = CardLayoutSchema[] | string | null | undefined;
171
-
172
27
  export function normalizeLayout(layout: CardLayoutInput): CardLayoutSchema[];
173
28
 
174
29
  export function resolveBindingValue(
@@ -177,6 +32,15 @@ export function resolveBindingValue(
177
32
  context?: BindingContext
178
33
  ): any;
179
34
 
35
+ export function stripLayoutBindings(
36
+ layouts?: CardLayoutSchema[]
37
+ ): CardLayoutSchema[];
38
+
39
+ export function applyItemCollectBindings(
40
+ layouts: CardLayoutSchema[],
41
+ items?: TemplateItem[]
42
+ ): CardLayoutSchema[];
43
+
180
44
  export function buildRenderNodes(
181
45
  children: CardElement[],
182
46
  data: Record<string, any>,
@@ -189,3 +53,7 @@ export function buildRenderResult(
189
53
  data: Record<string, any>,
190
54
  unit?: 'px' | 'rpx'
191
55
  ): RenderResult;
56
+
57
+ export function getTemplateItems(ids: string, items: TemplateItem[]): TemplateItem[];
58
+
59
+ export function getTemplateBackgrounds(ids: string, items: TemplateBackground[]) : TemplateBackground[];
@@ -0,0 +1,141 @@
1
+ import type { CardElement, CardLayoutSchema, TemplateItem } from './interface';
2
+ import { TemplateBackground } from './interface/data/payload';
3
+
4
+
5
+ export function backgroundChange(
6
+ bg: TemplateBackground,
7
+ layout: CardLayoutSchema
8
+ ): CardLayoutSchema {
9
+ const toNameArray = (name?: string | string[]) => {
10
+ if (Array.isArray(name)) return name.filter(Boolean);
11
+ if (!name) return [];
12
+ return `${name}`
13
+ .split(',')
14
+ .map(n => n.trim())
15
+ .filter(Boolean);
16
+ };
17
+
18
+ const applySpecialColor = (el: CardElement): CardElement => {
19
+ const extras = bg.fontColorExtra || [];
20
+ if (!extras.length) return el;
21
+
22
+ const keys: string[] = [];
23
+ if (el.binding) keys.push(String(el.binding));
24
+ if (el.id) keys.push(String(el.id));
25
+ if (el.type === 'icon') keys.push('icon');
26
+ if (el.type === 'custom') keys.push('decor');
27
+
28
+ const matched = extras.find(sc =>
29
+ toNameArray(sc.name).some(n => keys.some(k => k?.startsWith(n)))
30
+ );
31
+ if (!matched) return el;
32
+
33
+ const baseStyle = { ...(el.style || {}) };
34
+ if (el.type === 'custom') {
35
+ return { ...el, style: { ...baseStyle, backgroundColor: matched.color } };
36
+ }
37
+ return { ...el, style: { ...baseStyle, color: matched.color } };
38
+ };
39
+
40
+ const traverse = (children: CardElement[] = []): CardElement[] =>
41
+ children.map(el => {
42
+ if (!el) return el;
43
+ if (el.type === 'layout-panel') {
44
+ return {
45
+ ...el,
46
+ children: traverse(el.children || []),
47
+ };
48
+ }
49
+ return applySpecialColor(el);
50
+ });
51
+
52
+ return {
53
+ ...layout,
54
+ backgroundImage: bg.imgUrl,
55
+ fontColor: bg.fontColor || layout.fontColor,
56
+ children: traverse(layout.children || []),
57
+ };
58
+ }
59
+
60
+ export function stripLayoutBindings(
61
+ layouts: CardLayoutSchema[] = []
62
+ ): CardLayoutSchema[] {
63
+ const targetLayouts = Array.isArray(layouts) ? layouts : [];
64
+ const stripElement = (el: CardElement): CardElement => {
65
+ const { binding: _b, defaultValue: _d, ...rest } = el as any;
66
+ if (el.type === 'layout-panel') {
67
+ return {
68
+ ...rest,
69
+ children: (el.children || []).map(stripElement),
70
+ } as CardElement;
71
+ }
72
+ return rest as CardElement;
73
+ };
74
+
75
+ return targetLayouts.map(layout => ({
76
+ ...layout,
77
+ children: (layout.children || []).map(stripElement),
78
+ }));
79
+ }
80
+
81
+ /**
82
+ * 应用元素数据绑定字段
83
+ * @param layouts 布局
84
+ * @param items 绑定元素数据
85
+ * @returns
86
+ */
87
+ export function applyItemCollectBindings(
88
+ layouts: CardLayoutSchema[] = [],
89
+ items: TemplateItem[] = []
90
+ ): CardLayoutSchema[] {
91
+ const targetLayouts = Array.isArray(layouts) ? layouts : [];
92
+ const metaMap = new Map<string, TemplateItem>();
93
+ const metaList = Array.isArray(items) ? items : [];
94
+ metaList.forEach(item => {
95
+ if (item && item.id !== undefined && item.id !== null) {
96
+ metaMap.set(String(item.id), item);
97
+ }
98
+ });
99
+
100
+ const assignBinding = (el: CardElement): CardElement => {
101
+ const meta = metaMap.get(String(el.id));
102
+ const binding =
103
+ meta && meta.bind !== undefined && meta.bind !== null
104
+ ? meta.bind
105
+ : el.binding;
106
+ const defaultValue =
107
+ meta && meta.default !== undefined ? meta.default : el.defaultValue;
108
+ const key = meta && meta.key !== undefined ? meta.key : el.key;
109
+ const base: any = { ...el };
110
+ if (binding !== undefined) base.binding = binding;
111
+ else delete base.binding;
112
+ if (defaultValue !== undefined) base.defaultValue = defaultValue;
113
+ else delete base.defaultValue;
114
+ if (key !== undefined) base.key = key;
115
+ else delete base.key;
116
+
117
+ if (el.type === 'layout-panel') {
118
+ return {
119
+ ...base,
120
+ children: (el.children || []).map(assignBinding),
121
+ } as CardElement;
122
+ }
123
+ return base as CardElement;
124
+ };
125
+
126
+ return targetLayouts.map(layout => ({
127
+ ...layout,
128
+ children: (layout.children || []).map(assignBinding),
129
+ }));
130
+ }
131
+
132
+ export function getTemplateItems(ids: string, items: TemplateItem[]) {
133
+ const idArray = ids.split(',').map(id => id.trim());
134
+ return items.filter(item => idArray.includes(String(item.id)));
135
+ }
136
+
137
+
138
+ export function getTemplateBackgrounds(ids: string, items: TemplateBackground[]){
139
+ const idArray = ids.split(',').map(id => id.trim());
140
+ return items.filter(item => idArray.includes(String(item.id)));
141
+ }