km-card-layout-core 0.1.11 → 0.1.12
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/bindings.ts +84 -0
- package/data.ts +38 -0
- package/dist/bindings.js +73 -0
- package/dist/data.js +38 -0
- package/dist/helpers.js +72 -0
- package/dist/index.js +15 -392
- package/dist/layout.js +117 -0
- package/dist/ops/changeBackground.js +135 -0
- package/dist/render/builder.js +210 -0
- package/dist/utils.js +23 -129
- package/helpers.ts +76 -0
- package/index.ts +21 -511
- package/layout.ts +129 -0
- package/ops/changeBackground.ts +169 -0
- package/package.json +1 -1
- package/render/builder.ts +288 -0
- package/types.d.ts +97 -7
- package/utils.ts +9 -141
package/bindings.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { CardElement, CardLayoutSchema } from './interface';
|
|
2
|
+
import type {
|
|
3
|
+
TemplateBackground,
|
|
4
|
+
TemplateItem,
|
|
5
|
+
} from './interface/data/payload';
|
|
6
|
+
|
|
7
|
+
export function stripLayoutBindings(
|
|
8
|
+
layouts: CardLayoutSchema[] = []
|
|
9
|
+
): CardLayoutSchema[] {
|
|
10
|
+
const targetLayouts = Array.isArray(layouts) ? layouts : [];
|
|
11
|
+
const stripElement = (el: CardElement): CardElement => {
|
|
12
|
+
const { binding: _b, defaultValue: _d, ...rest } = el as any;
|
|
13
|
+
if (el.type === 'layout-panel') {
|
|
14
|
+
return {
|
|
15
|
+
...rest,
|
|
16
|
+
children: (el.children || []).map(stripElement),
|
|
17
|
+
} as CardElement;
|
|
18
|
+
}
|
|
19
|
+
return rest as CardElement;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
return targetLayouts.map(layout => ({
|
|
23
|
+
...layout,
|
|
24
|
+
children: (layout.children || []).map(stripElement),
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function applyItemCollectBindings(
|
|
29
|
+
layouts: CardLayoutSchema[] = [],
|
|
30
|
+
items: TemplateItem[] = []
|
|
31
|
+
): CardLayoutSchema[] {
|
|
32
|
+
const targetLayouts = Array.isArray(layouts) ? layouts : [];
|
|
33
|
+
const metaMap = new Map<string, TemplateItem>();
|
|
34
|
+
const metaList = Array.isArray(items) ? items : [];
|
|
35
|
+
metaList.forEach(item => {
|
|
36
|
+
if (item && item.id !== undefined && item.id !== null) {
|
|
37
|
+
metaMap.set(String(item.id), item);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const assignBinding = (el: CardElement): CardElement => {
|
|
42
|
+
const meta = metaMap.get(String(el.id));
|
|
43
|
+
const binding =
|
|
44
|
+
meta && meta.bind !== undefined && meta.bind !== null
|
|
45
|
+
? meta.bind
|
|
46
|
+
: el.binding;
|
|
47
|
+
const defaultValue =
|
|
48
|
+
meta && meta.default !== undefined ? meta.default : el.defaultValue;
|
|
49
|
+
const key = meta && meta.key !== undefined ? meta.key : el.key;
|
|
50
|
+
const base: any = { ...el };
|
|
51
|
+
if (binding !== undefined) base.binding = binding;
|
|
52
|
+
else delete base.binding;
|
|
53
|
+
if (defaultValue !== undefined) base.defaultValue = defaultValue;
|
|
54
|
+
else delete base.defaultValue;
|
|
55
|
+
if (key !== undefined) base.key = key;
|
|
56
|
+
else delete base.key;
|
|
57
|
+
|
|
58
|
+
if (el.type === 'layout-panel') {
|
|
59
|
+
return {
|
|
60
|
+
...base,
|
|
61
|
+
children: (el.children || []).map(assignBinding),
|
|
62
|
+
} as CardElement;
|
|
63
|
+
}
|
|
64
|
+
return base as CardElement;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return targetLayouts.map(layout => ({
|
|
68
|
+
...layout,
|
|
69
|
+
children: (layout.children || []).map(assignBinding),
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function getTemplateItems(ids: string, items: TemplateItem[]) {
|
|
74
|
+
const idArray = ids.split(',').map(id => id.trim());
|
|
75
|
+
return items.filter(item => idArray.includes(String(item.id)));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function getTemplateBackgrounds(
|
|
79
|
+
ids: string,
|
|
80
|
+
items: TemplateBackground[]
|
|
81
|
+
) {
|
|
82
|
+
const idArray = ids.split(',').map(id => id.trim());
|
|
83
|
+
return items.filter(item => idArray.includes(String(item.id)));
|
|
84
|
+
}
|
package/data.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { isObject } from './helpers';
|
|
2
|
+
|
|
3
|
+
const pathToSegments = (path: string): string[] =>
|
|
4
|
+
`${path || ''}`
|
|
5
|
+
.replace(/\[(\d+)\]/g, '.$1')
|
|
6
|
+
.split('.')
|
|
7
|
+
.map(p => p.trim())
|
|
8
|
+
.filter(Boolean);
|
|
9
|
+
|
|
10
|
+
const readByPath = (data: any, path: string): any => {
|
|
11
|
+
if (path === undefined || path === null || path === '') return data;
|
|
12
|
+
const segments = pathToSegments(path);
|
|
13
|
+
let cursor: any = data;
|
|
14
|
+
for (let i = 0; i < segments.length; i += 1) {
|
|
15
|
+
if (!isObject(cursor) && !Array.isArray(cursor)) return undefined;
|
|
16
|
+
const key = segments[i];
|
|
17
|
+
if (Array.isArray(cursor)) {
|
|
18
|
+
const idx = Number(key);
|
|
19
|
+
cursor = Number.isNaN(idx) ? undefined : cursor[idx];
|
|
20
|
+
} else {
|
|
21
|
+
cursor = (cursor as Record<string, any>)[key];
|
|
22
|
+
}
|
|
23
|
+
if (cursor === undefined || cursor === null) {
|
|
24
|
+
return cursor;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return cursor;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const resolveBindingValue = (
|
|
31
|
+
binding: string | undefined,
|
|
32
|
+
rootData: Record<string, any>,
|
|
33
|
+
context?: Record<string, any>
|
|
34
|
+
): any => {
|
|
35
|
+
if (!binding) return undefined;
|
|
36
|
+
const value = readByPath(rootData, binding);
|
|
37
|
+
return value === undefined ? undefined : value;
|
|
38
|
+
};
|
package/dist/bindings.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stripLayoutBindings = stripLayoutBindings;
|
|
4
|
+
exports.applyItemCollectBindings = applyItemCollectBindings;
|
|
5
|
+
exports.getTemplateItems = getTemplateItems;
|
|
6
|
+
exports.getTemplateBackgrounds = getTemplateBackgrounds;
|
|
7
|
+
function stripLayoutBindings(layouts = []) {
|
|
8
|
+
const targetLayouts = Array.isArray(layouts) ? layouts : [];
|
|
9
|
+
const stripElement = (el) => {
|
|
10
|
+
const { binding: _b, defaultValue: _d, ...rest } = el;
|
|
11
|
+
if (el.type === 'layout-panel') {
|
|
12
|
+
return {
|
|
13
|
+
...rest,
|
|
14
|
+
children: (el.children || []).map(stripElement),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return rest;
|
|
18
|
+
};
|
|
19
|
+
return targetLayouts.map(layout => ({
|
|
20
|
+
...layout,
|
|
21
|
+
children: (layout.children || []).map(stripElement),
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
function applyItemCollectBindings(layouts = [], items = []) {
|
|
25
|
+
const targetLayouts = Array.isArray(layouts) ? layouts : [];
|
|
26
|
+
const metaMap = new Map();
|
|
27
|
+
const metaList = Array.isArray(items) ? items : [];
|
|
28
|
+
metaList.forEach(item => {
|
|
29
|
+
if (item && item.id !== undefined && item.id !== null) {
|
|
30
|
+
metaMap.set(String(item.id), item);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
const assignBinding = (el) => {
|
|
34
|
+
const meta = metaMap.get(String(el.id));
|
|
35
|
+
const binding = meta && meta.bind !== undefined && meta.bind !== null
|
|
36
|
+
? meta.bind
|
|
37
|
+
: el.binding;
|
|
38
|
+
const defaultValue = meta && meta.default !== undefined ? meta.default : el.defaultValue;
|
|
39
|
+
const key = meta && meta.key !== undefined ? meta.key : el.key;
|
|
40
|
+
const base = { ...el };
|
|
41
|
+
if (binding !== undefined)
|
|
42
|
+
base.binding = binding;
|
|
43
|
+
else
|
|
44
|
+
delete base.binding;
|
|
45
|
+
if (defaultValue !== undefined)
|
|
46
|
+
base.defaultValue = defaultValue;
|
|
47
|
+
else
|
|
48
|
+
delete base.defaultValue;
|
|
49
|
+
if (key !== undefined)
|
|
50
|
+
base.key = key;
|
|
51
|
+
else
|
|
52
|
+
delete base.key;
|
|
53
|
+
if (el.type === 'layout-panel') {
|
|
54
|
+
return {
|
|
55
|
+
...base,
|
|
56
|
+
children: (el.children || []).map(assignBinding),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return base;
|
|
60
|
+
};
|
|
61
|
+
return targetLayouts.map(layout => ({
|
|
62
|
+
...layout,
|
|
63
|
+
children: (layout.children || []).map(assignBinding),
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
function getTemplateItems(ids, items) {
|
|
67
|
+
const idArray = ids.split(',').map(id => id.trim());
|
|
68
|
+
return items.filter(item => idArray.includes(String(item.id)));
|
|
69
|
+
}
|
|
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
|
+
}
|
package/dist/data.js
ADDED
|
@@ -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;
|
package/dist/helpers.js
ADDED
|
@@ -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;
|