@gem-sdk/core 1.12.0-next.4 → 1.12.0-next.45
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/dist/cjs/components/Render.liquid.js +92 -43
- package/dist/cjs/helpers/compose-advance-style.js +9 -1
- package/dist/cjs/helpers/make-style.js +10 -2
- package/dist/cjs/helpers/render.js +7 -0
- package/dist/cjs/helpers/typography.js +68 -0
- package/dist/cjs/hooks/shop.js +12 -0
- package/dist/cjs/hooks/useFormatMoney.js +54 -14
- package/dist/cjs/hooks/useMoney.js +60 -207
- package/dist/cjs/index.js +12 -2
- package/dist/esm/components/Render.liquid.js +92 -44
- package/dist/esm/helpers/compose-advance-style.js +9 -1
- package/dist/esm/helpers/make-style.js +10 -3
- package/dist/esm/helpers/render.js +6 -1
- package/dist/esm/helpers/typography.js +64 -1
- package/dist/esm/hooks/shop.js +12 -1
- package/dist/esm/hooks/useFormatMoney.js +55 -13
- package/dist/esm/hooks/useMoney.js +60 -207
- package/dist/esm/index.js +6 -6
- package/dist/types/index.d.ts +1628 -45
- package/package.json +3 -3
|
@@ -16,20 +16,35 @@ const componentTexts = [
|
|
|
16
16
|
'Text',
|
|
17
17
|
'Heading'
|
|
18
18
|
];
|
|
19
|
-
const Render = ({ uid , builder , components , parentId , ...passProps })=>{
|
|
19
|
+
const Render = ({ uid , builder , components , parentId , extraFiles ={} , ...passProps })=>{
|
|
20
20
|
const item = builder[uid];
|
|
21
21
|
const Component = components[item?.tag];
|
|
22
22
|
if (!Component) {
|
|
23
23
|
console.log('Miss component: ', item);
|
|
24
|
-
return
|
|
24
|
+
return {
|
|
25
|
+
liquid: '',
|
|
26
|
+
extraFiles
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (!item) {
|
|
30
|
+
return {
|
|
31
|
+
liquid: '',
|
|
32
|
+
extraFiles
|
|
33
|
+
};
|
|
25
34
|
}
|
|
26
|
-
if (!item) return null;
|
|
27
35
|
if (item?.type === 'section') {
|
|
28
|
-
return
|
|
36
|
+
return {
|
|
37
|
+
liquid: '',
|
|
38
|
+
extraFiles
|
|
39
|
+
};
|
|
29
40
|
} else {
|
|
41
|
+
const customProps = {
|
|
42
|
+
extraFiles: {}
|
|
43
|
+
};
|
|
30
44
|
const style = composeAdvanceStyle.composeAdvanceStyle(item.advanced, item.tag);
|
|
45
|
+
let liquid = '';
|
|
31
46
|
if (constant.disableWrap.includes(item.tag)) {
|
|
32
|
-
|
|
47
|
+
liquid = render.RenderIf(item?.childrens?.length, ()=>{
|
|
33
48
|
return Component({
|
|
34
49
|
builderProps: {
|
|
35
50
|
uid,
|
|
@@ -46,17 +61,19 @@ const Render = ({ uid , builder , components , parentId , ...passProps })=>{
|
|
|
46
61
|
...builder[id],
|
|
47
62
|
uid: id,
|
|
48
63
|
builder,
|
|
49
|
-
components
|
|
64
|
+
components,
|
|
65
|
+
customProps
|
|
50
66
|
};
|
|
51
67
|
}),
|
|
52
|
-
children: item.childrens.map((id)=>
|
|
68
|
+
children: item.childrens.map((id)=>RenderChildren({
|
|
53
69
|
uid: id,
|
|
54
70
|
builder,
|
|
55
71
|
components,
|
|
56
|
-
|
|
72
|
+
customProps
|
|
57
73
|
})).join('')
|
|
58
74
|
});
|
|
59
|
-
}, ()=>
|
|
75
|
+
}, ()=>{
|
|
76
|
+
return Component({
|
|
60
77
|
builderProps: {
|
|
61
78
|
uid,
|
|
62
79
|
builderData: item
|
|
@@ -68,42 +85,74 @@ const Render = ({ uid , builder , components , parentId , ...passProps })=>{
|
|
|
68
85
|
isText: componentTexts.includes(item.tag) ? true : null,
|
|
69
86
|
parentId: componentsRenderWithParentId.includes(item.tag) ? parentId : null,
|
|
70
87
|
...passProps
|
|
71
|
-
})
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
} else {
|
|
91
|
+
liquid = render.template`<div style="${style}" class="${item.uid}">
|
|
92
|
+
${render.RenderIf(item?.childrens?.length, ()=>{
|
|
93
|
+
return Component({
|
|
94
|
+
builderProps: {
|
|
95
|
+
uid,
|
|
96
|
+
builderData: item
|
|
97
|
+
},
|
|
98
|
+
styles: item.styles,
|
|
99
|
+
setting: item.settings,
|
|
100
|
+
...passProps,
|
|
101
|
+
rawChildren: item.childrens.map((id)=>{
|
|
102
|
+
return {
|
|
103
|
+
...builder[id],
|
|
104
|
+
uid: id,
|
|
105
|
+
builder,
|
|
106
|
+
components,
|
|
107
|
+
customProps
|
|
108
|
+
};
|
|
109
|
+
}),
|
|
110
|
+
children: item.childrens.map((id)=>RenderChildren({
|
|
111
|
+
uid: id,
|
|
112
|
+
builder,
|
|
113
|
+
components,
|
|
114
|
+
customProps
|
|
115
|
+
})).join('')
|
|
116
|
+
});
|
|
117
|
+
}, ()=>{
|
|
118
|
+
return Component({
|
|
119
|
+
builderProps: {
|
|
120
|
+
uid,
|
|
121
|
+
builderData: item
|
|
122
|
+
},
|
|
123
|
+
styles: item.styles,
|
|
124
|
+
setting: item.settings,
|
|
125
|
+
isText: componentTexts.includes(item.tag) ? true : null,
|
|
126
|
+
...passProps
|
|
127
|
+
});
|
|
128
|
+
})}
|
|
129
|
+
</div>`;
|
|
72
130
|
}
|
|
73
|
-
return
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}), ()=>Component({
|
|
96
|
-
builderProps: {
|
|
97
|
-
uid,
|
|
98
|
-
builderData: item
|
|
99
|
-
},
|
|
100
|
-
styles: item.styles,
|
|
101
|
-
setting: item.settings,
|
|
102
|
-
isText: componentTexts.includes(item.tag) ? true : null,
|
|
103
|
-
...passProps
|
|
104
|
-
}))}
|
|
105
|
-
</div>`;
|
|
131
|
+
return {
|
|
132
|
+
liquid,
|
|
133
|
+
extraFiles: customProps.extraFiles
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const RenderChildren = (props)=>{
|
|
138
|
+
const data = Render(props);
|
|
139
|
+
// Append to prop parent
|
|
140
|
+
for(const key in data.extraFiles){
|
|
141
|
+
if (Object.prototype.hasOwnProperty.call(data.extraFiles, key)) {
|
|
142
|
+
const value = data.extraFiles[key];
|
|
143
|
+
props.customProps.extraFiles[key] = value;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// Fix limit 256kb
|
|
147
|
+
const textEncoder = new TextEncoder();
|
|
148
|
+
const size = data?.liquid ? textEncoder.encode(data.liquid).length : 0;
|
|
149
|
+
if (Math.ceil(size / 1024) >= 180) {
|
|
150
|
+
const fileName = `gp-section-snippet-${props.uid}`;
|
|
151
|
+
props.customProps.extraFiles[fileName] = data.liquid;
|
|
152
|
+
return `{% render '${fileName}', product: product, variant: variant, product_form_id: product_form_id %}`;
|
|
106
153
|
}
|
|
154
|
+
return data.liquid;
|
|
107
155
|
};
|
|
108
156
|
|
|
157
|
+
exports.RenderChildren = RenderChildren;
|
|
109
158
|
exports.default = Render;
|
|
@@ -117,7 +117,15 @@ function composeAdvanceStyle(data, tag) {
|
|
|
117
117
|
}
|
|
118
118
|
} else {
|
|
119
119
|
if (attr === 'border') {
|
|
120
|
-
|
|
120
|
+
const borderStyle = value;
|
|
121
|
+
if (borderStyle.border === 'none') {
|
|
122
|
+
borderStyle.border = 'solid';
|
|
123
|
+
borderStyle.color = '#121212';
|
|
124
|
+
borderStyle.isCustom = true;
|
|
125
|
+
borderStyle.width = '0px';
|
|
126
|
+
borderStyle.position = 'all';
|
|
127
|
+
}
|
|
128
|
+
Object.assign(styles, borders.getBorderStyle(borderStyle));
|
|
121
129
|
}
|
|
122
130
|
if (attr === 'rounded') {
|
|
123
131
|
Object.assign(styles, radius.getCornerCSSFromGlobal(value));
|
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
var getResonsiveValue = require('./get-resonsive-value.js');
|
|
4
4
|
|
|
5
|
+
const removeNullUndefined = (obj)=>Object.entries(obj).reduce((a, [k, v])=>{
|
|
6
|
+
if (v === null || v === undefined) return a;
|
|
7
|
+
return {
|
|
8
|
+
...a,
|
|
9
|
+
[k]: v
|
|
10
|
+
};
|
|
11
|
+
}, {});
|
|
5
12
|
const makeStyleKey = (name)=>{
|
|
6
13
|
return [
|
|
7
14
|
name,
|
|
@@ -16,10 +23,10 @@ const makeStyleKey = (name)=>{
|
|
|
16
23
|
].map((key)=>`--${key}`);
|
|
17
24
|
};
|
|
18
25
|
const makeStyle = (style)=>{
|
|
19
|
-
return Object.fromEntries(Object.entries(style).map(([key, value])=>[
|
|
26
|
+
return removeNullUndefined(Object.fromEntries(Object.entries(style).map(([key, value])=>[
|
|
20
27
|
`--${key}`,
|
|
21
28
|
value
|
|
22
|
-
]));
|
|
29
|
+
])));
|
|
23
30
|
};
|
|
24
31
|
const makeStyleState = (name, value)=>{
|
|
25
32
|
if (!value) return {};
|
|
@@ -118,3 +125,4 @@ exports.makeStyleResponsive = makeStyleResponsive;
|
|
|
118
125
|
exports.makeStyleResponsiveState = makeStyleResponsiveState;
|
|
119
126
|
exports.makeStyleState = makeStyleState;
|
|
120
127
|
exports.makeWidth = makeWidth;
|
|
128
|
+
exports.removeNullUndefined = removeNullUndefined;
|
|
@@ -39,6 +39,9 @@ const styles = (strings, ...keys)=>{
|
|
|
39
39
|
}
|
|
40
40
|
return styleArr.join(';');
|
|
41
41
|
};
|
|
42
|
+
const dataStringify = (obj)=>{
|
|
43
|
+
return JSON.stringify(obj).replace(/\\"/g, '"');
|
|
44
|
+
};
|
|
42
45
|
const template = (strings, ...keys)=>{
|
|
43
46
|
let str = '';
|
|
44
47
|
strings.forEach((item, index)=>{
|
|
@@ -57,8 +60,12 @@ const template = (strings, ...keys)=>{
|
|
|
57
60
|
});
|
|
58
61
|
return str;
|
|
59
62
|
};
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
64
|
+
const composeMemo = (fn, _)=>fn();
|
|
60
65
|
|
|
61
66
|
exports.RenderIf = RenderIf;
|
|
67
|
+
exports.composeMemo = composeMemo;
|
|
68
|
+
exports.dataStringify = dataStringify;
|
|
62
69
|
exports.props = props;
|
|
63
70
|
exports.styles = styles;
|
|
64
71
|
exports.template = template;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var colors = require('./colors.js');
|
|
4
|
+
var makeStyle = require('./make-style.js');
|
|
5
|
+
|
|
3
6
|
const genTypoClass = (name)=>{
|
|
4
7
|
return `g-${name}`;
|
|
5
8
|
};
|
|
@@ -15,6 +18,71 @@ const composeTypographyCss = (typography)=>{
|
|
|
15
18
|
${lineHeight ? `line-height: ${lineHeight};` : ''}
|
|
16
19
|
`;
|
|
17
20
|
};
|
|
21
|
+
function getCustomCSSByDevice(typography, device) {
|
|
22
|
+
if (!typography || !device) return {};
|
|
23
|
+
const suffix = device === 'desktop' || !device ? '' : `-${device}`;
|
|
24
|
+
const { fontFamily } = typography?.[device] ?? {};
|
|
25
|
+
return {
|
|
26
|
+
[`--size${suffix}`]: typography?.[device]?.fontSize,
|
|
27
|
+
[`--lh${suffix}`]: typography?.[device]?.lineHeight,
|
|
28
|
+
[`--fs${suffix}`]: typography?.[device]?.fontStyle,
|
|
29
|
+
[`--ff${suffix}`]: fontFamily ? `var(--g-font-${fontFamily}, ${fontFamily})` : undefined,
|
|
30
|
+
[`--weight${suffix}`]: typography?.[device]?.fontWeight,
|
|
31
|
+
[`--ls${suffix}`]: typography?.[device]?.letterSpacing
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const composeTypography = (typography)=>{
|
|
35
|
+
if (!typography) return {};
|
|
36
|
+
return makeStyle.removeNullUndefined({
|
|
37
|
+
...getCustomCSSByDevice(typography, 'desktop'),
|
|
38
|
+
...getCustomCSSByDevice(typography, 'tablet'),
|
|
39
|
+
...getCustomCSSByDevice(typography, 'mobile')
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
const composeTypographyV2 = (value, attrs)=>{
|
|
43
|
+
if (!value) return {};
|
|
44
|
+
return makeStyle.removeNullUndefined({
|
|
45
|
+
...makeStyle.makeStyle({
|
|
46
|
+
fs: !attrs?.italic && value?.fontStyle,
|
|
47
|
+
ff: value?.fontFamily ? `var(--g-font-${value.fontFamily}, ${value.fontFamily})` : undefined,
|
|
48
|
+
weight: !attrs?.bold && value?.fontWeight,
|
|
49
|
+
ls: value?.letterSpacing,
|
|
50
|
+
lh: value?.lineHeight
|
|
51
|
+
}),
|
|
52
|
+
...makeStyle.makeStyleResponsive('size', value?.fontSize)
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
const composeTypographyAttr = (attrs)=>{
|
|
56
|
+
if (!attrs) return {};
|
|
57
|
+
return makeStyle.removeNullUndefined({
|
|
58
|
+
...makeStyle.makeStyle({
|
|
59
|
+
fs: attrs?.italic && 'italic',
|
|
60
|
+
weight: attrs?.bold && 'bold',
|
|
61
|
+
c: colors.getSingleColorVariable(attrs?.color),
|
|
62
|
+
tt: attrs?.transform,
|
|
63
|
+
tdl: attrs?.underline ? 'underline' : undefined
|
|
64
|
+
})
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
const composeTypographyClassName = (typo, typography)=>{
|
|
68
|
+
return typo ? typo?.type && !typo.custom ? genTypoClass(typo.type) : '' : typography?.type && genTypoClass(typography?.type);
|
|
69
|
+
};
|
|
70
|
+
const composeTypographyStyle = (typo, typography, disableAttr)=>{
|
|
71
|
+
if (typo) {
|
|
72
|
+
return {
|
|
73
|
+
...composeTypographyV2(typo.custom),
|
|
74
|
+
...!disableAttr && composeTypographyAttr(typo.attrs)
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
...!typography?.type ? composeTypography(typography?.custom) : {}
|
|
79
|
+
};
|
|
80
|
+
};
|
|
18
81
|
|
|
82
|
+
exports.composeTypography = composeTypography;
|
|
83
|
+
exports.composeTypographyAttr = composeTypographyAttr;
|
|
84
|
+
exports.composeTypographyClassName = composeTypographyClassName;
|
|
19
85
|
exports.composeTypographyCss = composeTypographyCss;
|
|
86
|
+
exports.composeTypographyStyle = composeTypographyStyle;
|
|
87
|
+
exports.composeTypographyV2 = composeTypographyV2;
|
|
20
88
|
exports.genTypoClass = genTypoClass;
|
package/dist/cjs/hooks/shop.js
CHANGED
|
@@ -26,6 +26,17 @@ const useCurrency = ()=>{
|
|
|
26
26
|
changeCurrency
|
|
27
27
|
]);
|
|
28
28
|
};
|
|
29
|
+
const useMoneyFormat = ()=>{
|
|
30
|
+
const moneyFormat = ShopContext.useShopStore((state)=>state.moneyFormat);
|
|
31
|
+
const moneyWithCurrencyFormat = ShopContext.useShopStore((state)=>state.moneyWithCurrencyFormat);
|
|
32
|
+
return react.useMemo(()=>({
|
|
33
|
+
moneyFormat,
|
|
34
|
+
moneyWithCurrencyFormat
|
|
35
|
+
}), [
|
|
36
|
+
moneyFormat,
|
|
37
|
+
moneyWithCurrencyFormat
|
|
38
|
+
]);
|
|
39
|
+
};
|
|
29
40
|
const useSwatches = ()=>{
|
|
30
41
|
const swatches = ShopContext.useShopStore((state)=>state.swatches);
|
|
31
42
|
const changeSwatches = ShopContext.useShopStore((state)=>state.changeSwatches);
|
|
@@ -108,6 +119,7 @@ exports.useIsStorefrontProduct = useIsStorefrontProduct;
|
|
|
108
119
|
exports.useLocale = useLocale;
|
|
109
120
|
exports.useMatchMutate = useMatchMutate;
|
|
110
121
|
exports.useMobileOnly = useMobileOnly;
|
|
122
|
+
exports.useMoneyFormat = useMoneyFormat;
|
|
111
123
|
exports.usePageType = usePageType;
|
|
112
124
|
exports.usePluginEnable = usePluginEnable;
|
|
113
125
|
exports.useStoreFront = useStoreFront;
|
|
@@ -1,21 +1,61 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var react = require('react');
|
|
6
3
|
var shop = require('./shop.js');
|
|
7
4
|
|
|
8
|
-
const useFormatMoney = ()=>{
|
|
9
|
-
const {
|
|
10
|
-
const formatMoney =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
const useFormatMoney = (amount, withCurrency)=>{
|
|
6
|
+
const { moneyFormat , moneyWithCurrencyFormat } = shop.useMoneyFormat();
|
|
7
|
+
const formatMoney = function(cents, format) {
|
|
8
|
+
let value = '';
|
|
9
|
+
const placeholderRegex = /\{\{\s*(\w+)\s*\}\}/;
|
|
10
|
+
const formatString = format || '${{amount}}';
|
|
11
|
+
if (typeof cents == 'string') {
|
|
12
|
+
cents = cents.replace('.', '');
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* check default
|
|
16
|
+
* @param opt opt
|
|
17
|
+
* @param def def
|
|
18
|
+
* @returns any
|
|
19
|
+
*/ function defaultOption(opt, def) {
|
|
20
|
+
return typeof opt == 'undefined' ? def : opt;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* formatWithDelimiters
|
|
24
|
+
* @param number number
|
|
25
|
+
* @param precision precision
|
|
26
|
+
* @param thousands thousands
|
|
27
|
+
* @param decimal decimal
|
|
28
|
+
* @returns any
|
|
29
|
+
*/ // eslint-disable-next-line max-params
|
|
30
|
+
function formatWithDelimiters(number, precision, thousands, decimal) {
|
|
31
|
+
precision = defaultOption(precision, 2);
|
|
32
|
+
thousands = defaultOption(thousands, ',');
|
|
33
|
+
decimal = defaultOption(decimal, '.');
|
|
34
|
+
if (isNaN(number) || number == null) {
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
// shopify làm tròn bằng cách cắt đi các số ở đằng sau chứ không sử dụng toFixed để làm tròn như toán học
|
|
38
|
+
number = parseFloat(`${number}`).toFixed(Number(precision) + 1).slice(0, -1);
|
|
39
|
+
const parts = number.split('.'), dollars = parts[0]?.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + thousands), cents = parts[1] ? decimal + parts[1] : '';
|
|
40
|
+
return dollars + cents;
|
|
41
|
+
}
|
|
42
|
+
switch(formatString.match(placeholderRegex)[1]){
|
|
43
|
+
case 'amount':
|
|
44
|
+
value = formatWithDelimiters(cents, 2);
|
|
45
|
+
break;
|
|
46
|
+
case 'amount_no_decimals':
|
|
47
|
+
value = formatWithDelimiters(cents, 0);
|
|
48
|
+
break;
|
|
49
|
+
case 'amount_with_comma_separator':
|
|
50
|
+
value = formatWithDelimiters(cents, 2, '.', ',');
|
|
51
|
+
break;
|
|
52
|
+
case 'amount_no_decimals_with_comma_separator':
|
|
53
|
+
value = formatWithDelimiters(cents, 0, '.', ',');
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
return formatString.replace(placeholderRegex, value);
|
|
18
57
|
};
|
|
58
|
+
return withCurrency ? formatMoney(`${amount}`, moneyWithCurrencyFormat || moneyFormat) : formatMoney(`${amount}`, moneyFormat);
|
|
19
59
|
};
|
|
20
60
|
|
|
21
|
-
exports.
|
|
61
|
+
exports.useFormatMoney = useFormatMoney;
|