@blocklet/pages-kit 0.4.98 → 0.4.100

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.
@@ -3,3 +3,4 @@ export * from './core';
3
3
  export * from './state';
4
4
  export * from './preload';
5
5
  export * from './builtin';
6
+ export * from './style';
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,4 @@
1
+ import isNil from 'lodash/isNil';
1
2
  import * as yaml from 'yaml';
2
3
  export function componentUMDName({ componentId }) {
3
4
  return `PagesCustomComponent${componentId}`;
@@ -65,7 +66,42 @@ export function safeYamlParse(value) {
65
66
  }
66
67
  return yaml.parse(value);
67
68
  }
68
- export function parsePropertyValue(property, value, { locale, defaultLocale }) {
69
+ export const isBrowserEnv = () => {
70
+ return typeof globalThis.window !== 'undefined' && typeof globalThis.document !== 'undefined';
71
+ };
72
+ let dynamicPropertyHandlers = null;
73
+ let initDynamicPropertyHandlersPromise = null;
74
+ // 根据前后端环境初始化动态的属性处理函数
75
+ export const initDynamicParsePropertyValueHandlers = () => {
76
+ if (dynamicPropertyHandlers)
77
+ return Promise.resolve(dynamicPropertyHandlers);
78
+ if (!initDynamicPropertyHandlersPromise) {
79
+ initDynamicPropertyHandlersPromise = (async () => {
80
+ dynamicPropertyHandlers = {};
81
+ if (isBrowserEnv()) {
82
+ // colorConvert 需要前端环境才能处理,依赖 mui theme
83
+ const { colorConvert } = await import('./style');
84
+ dynamicPropertyHandlers.color = (value) => colorConvert(value);
85
+ }
86
+ return dynamicPropertyHandlers;
87
+ })();
88
+ }
89
+ return initDynamicPropertyHandlersPromise;
90
+ };
91
+ initDynamicParsePropertyValueHandlers();
92
+ export function parsePropertyValue(property, value, { locale, defaultLocale, propertyHandlers, }) {
93
+ // 混合动态和自定义的属性处理函数,这个需要放在最前面,因为有可能存在覆盖下面的处理
94
+ const mixedPropertyHandlers = {
95
+ ...(dynamicPropertyHandlers ?? {}),
96
+ ...(propertyHandlers ?? {}),
97
+ };
98
+ if (mixedPropertyHandlers && property.type && typeof mixedPropertyHandlers[property.type] === 'function') {
99
+ const handler = mixedPropertyHandlers[property.type];
100
+ const result = handler?.(value);
101
+ if (!isNil(result)) {
102
+ return result;
103
+ }
104
+ }
69
105
  if (property.type === 'json') {
70
106
  if (!value)
71
107
  return undefined;
@@ -0,0 +1,155 @@
1
+ import { createTheme } from '@arcblock/ux/lib/Theme';
2
+ import gradient from 'gradient-parser';
3
+ import get from 'lodash/get';
4
+ import tinycolor from 'tinycolor2';
5
+ const GRADIENT_START = 0;
6
+ const GRADIENT_END = 100;
7
+ const baseTheme = createTheme({});
8
+ const blockletTheme = createTheme({
9
+ breakpoints: {
10
+ values: {
11
+ xs: 0,
12
+ sm: baseTheme?.breakpoints?.values?.sm ?? 600,
13
+ md: baseTheme?.breakpoints?.values?.md ?? 900,
14
+ lg: baseTheme?.breakpoints?.values?.lg ?? 1200,
15
+ xl: baseTheme?.breakpoints?.values?.xl ?? 1536,
16
+ // @ts-ignore
17
+ section: 1440,
18
+ },
19
+ },
20
+ });
21
+ export function getBlockletTheme() {
22
+ return blockletTheme;
23
+ }
24
+ export function isGradient(color) {
25
+ if (color?.trim()) {
26
+ try {
27
+ gradient.parse(color);
28
+ return true;
29
+ }
30
+ catch {
31
+ return false;
32
+ }
33
+ }
34
+ return false;
35
+ }
36
+ export function isColorString(color) {
37
+ if (typeof color !== 'string') {
38
+ return false;
39
+ }
40
+ if (isGradient(color)) {
41
+ return true;
42
+ }
43
+ const currentColor = tinycolor(color);
44
+ if (currentColor.isValid()) {
45
+ return true;
46
+ }
47
+ return false;
48
+ }
49
+ /**
50
+ * 检查字符串是否为有效的MUI颜色键
51
+ */
52
+ export function isMuiColorKey(value) {
53
+ // special case
54
+ if (value === 'transparent') {
55
+ return true;
56
+ }
57
+ return /^(primary|secondary|error|warning|info|success|grey|background|text|action)\.[a-z0-9]+$/.test(value);
58
+ }
59
+ /**
60
+ * 处理渐变的解析,并转换为rgba格式
61
+ */
62
+ export function getSafeGradient(color) {
63
+ try {
64
+ const colorAst = gradient.parse(color);
65
+ const safeAst = colorAst.map((astItem) => {
66
+ const colorStops = astItem.colorStops.map((colorStop, index) => {
67
+ let { value } = colorStop;
68
+ if (typeof value !== 'string' && !Array.isArray(value)) {
69
+ throw new Error('Invalid color stop value type');
70
+ }
71
+ let { type } = colorStop;
72
+ // 转换为 rgba 格式
73
+ if (type !== 'rgba') {
74
+ const rgbaValue = tinycolor(value).toRgb();
75
+ value = [`${rgbaValue.r}`, `${rgbaValue.g}`, `${rgbaValue.b}`, `${rgbaValue.a}`];
76
+ type = 'rgba';
77
+ }
78
+ // 如果已有进度,保持原样
79
+ if (colorStop.length) {
80
+ return {
81
+ ...colorStop,
82
+ value,
83
+ type,
84
+ };
85
+ }
86
+ // 没有进度时,按 CSS 默认行为处理
87
+ const totalStops = astItem.colorStops.length;
88
+ let percentage;
89
+ if (index === 0) {
90
+ percentage = GRADIENT_START;
91
+ }
92
+ else if (index === totalStops - 1) {
93
+ percentage = GRADIENT_END;
94
+ }
95
+ else {
96
+ // 中间的颜色均匀分布
97
+ percentage = (index / (totalStops - 1)) * 100;
98
+ }
99
+ return {
100
+ ...colorStop,
101
+ value,
102
+ type,
103
+ length: {
104
+ value: `${percentage}`,
105
+ type: '%',
106
+ },
107
+ };
108
+ });
109
+ return {
110
+ ...astItem,
111
+ colorStops,
112
+ };
113
+ });
114
+ const safeColor = gradient.stringify(safeAst);
115
+ return safeColor.replace(/NaN/g, '100');
116
+ }
117
+ catch (error) {
118
+ if (isColorString(color)) {
119
+ return `${color}`.replace(/NaN/g, '100');
120
+ }
121
+ return '';
122
+ }
123
+ }
124
+ /**
125
+ * 解析颜色字符串
126
+ */
127
+ export function parseColor(color = '') {
128
+ if (isGradient(color)) {
129
+ return color;
130
+ }
131
+ if (isColorString(color)) {
132
+ const currentColor = tinycolor(color);
133
+ return currentColor.toRgbString();
134
+ }
135
+ return color;
136
+ }
137
+ /**
138
+ * 转换颜色
139
+ * @param color - 颜色字符串
140
+ * @param _theme - 主题对象,默认为全局主题
141
+ * @returns 转换后的颜色字符串
142
+ */
143
+ export function colorConvert(color, _theme = blockletTheme) {
144
+ if (isMuiColorKey(color)) {
145
+ return get(_theme.palette, color);
146
+ }
147
+ // 处理渐变和普通颜色
148
+ if (isGradient(color)) {
149
+ return getSafeGradient(color);
150
+ }
151
+ if (isColorString(color)) {
152
+ return color;
153
+ }
154
+ return color || '';
155
+ }
@@ -0,0 +1,25 @@
1
+ interface ColorInfo {
2
+ colorKey: string;
3
+ colorValue: string;
4
+ group: string;
5
+ }
6
+ /**
7
+ * Hook 用于获取和操作 MUI 主题色
8
+ */
9
+ export declare function useMuiColorPalette(): {
10
+ muiPaletteColors: ColorInfo[];
11
+ groupedMuiColors: Record<string, {
12
+ colorKey: string;
13
+ colorValue: string;
14
+ }[]>;
15
+ presetColors: string[];
16
+ getMuiKeyByColor: (colorValue: string) => string | null;
17
+ getColorByMuiKey: (muiKey: string) => string | null;
18
+ isMuiColorKey: (value: string) => boolean;
19
+ theme: import("@mui/material/styles").Theme;
20
+ };
21
+ /**
22
+ * React Hook,用于实时转换颜色值(包括MUI主题色键)
23
+ */
24
+ export declare function useColorConvert(): (color?: string) => string;
25
+ export {};