@chamn/render 0.0.10 → 0.0.11

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/src/util/index.ts DELETED
@@ -1,164 +0,0 @@
1
- import { capitalize } from 'lodash-es';
2
- import { Component, createElement } from 'react';
3
- import { ContextType } from '../core/adapter';
4
- import { StoreManager } from '../core/storeManager';
5
- import { AssetPackage } from '@chamn/model';
6
-
7
- export const isClass = function (val: any) {
8
- if (!val) {
9
- return false;
10
- }
11
- if (typeof val !== 'function') {
12
- return false;
13
- }
14
- if (!val.prototype) {
15
- return false;
16
- }
17
- return true;
18
- };
19
-
20
- // eslint-disable-next-line @typescript-eslint/ban-types
21
- export function shouldConstruct(Component: Function) {
22
- const prototype = Component.prototype;
23
- return !!(prototype && prototype.isReactComponent);
24
- }
25
-
26
- export function canAcceptsRef(Comp: any) {
27
- const hasSymbol = typeof Symbol === 'function' && Symbol.for;
28
- const REACT_FORWARD_REF_TYPE = hasSymbol ? Symbol.for('react.forward_ref') : 0xead0;
29
- return Comp?.$$typeof === REACT_FORWARD_REF_TYPE || Comp?.prototype?.isReactComponent || Comp?.prototype?.setState || Comp._forwardRef;
30
- }
31
-
32
- export function compWrapper(Comp: any) {
33
- class WrapperForRef extends Component {
34
- render() {
35
- // 需要直接调用 react api ,避免被拦截 !!!!
36
- return createElement(Comp, this.props);
37
- }
38
- }
39
- (WrapperForRef as any).displayName = Comp.displayName;
40
-
41
- return WrapperForRef as any;
42
- }
43
-
44
- export const runExpression = (expStr: string, context: any) => {
45
- const run = (expStr: string) => {
46
- const contextVar = Object.keys(context).map((key) => {
47
- return `const ${key} = $$context['${key}'];`;
48
- });
49
- const executeCode = `
50
- ${contextVar.join('\n')}
51
- return ${expStr};
52
- `;
53
- return new Function('$$context', executeCode)(context);
54
- };
55
- try {
56
- return run(expStr);
57
- } catch (e) {
58
- console.warn(e);
59
- const msg = `[${expStr}] expression run failed`;
60
- console.warn(msg);
61
- return null;
62
- }
63
- };
64
-
65
- export const convertCodeStringToFunction = (functionStr: string, $$context: ContextType, storeManager: StoreManager) => {
66
- const newFunc = function (...args: any[]) {
67
- try {
68
- const codeBody = `
69
- var f = ${functionStr};
70
- var args = Array.from(arguments);
71
- var __$$storeManager__ = args.pop();
72
- var $$context = args.pop();
73
- $$context.stateManager = __$$storeManager__.getStateSnapshot();
74
- return f.apply(f, args)
75
- `;
76
- const f = new Function(codeBody);
77
- f(...args, $$context, storeManager);
78
- } catch (e) {
79
- console.warn(e);
80
- }
81
- };
82
- return newFunc;
83
- };
84
-
85
- /**
86
- *
87
- * @param args 对象的值
88
- * @param argsName 对因位置的 名称
89
- * @returns
90
- */
91
- export const getObjFromArrayMap = (args: any[], argsName: string[]) => {
92
- const params: Record<any, any> = {};
93
- argsName.forEach((paramName, index) => {
94
- params[paramName] = args[index];
95
- });
96
-
97
- return params;
98
- };
99
-
100
- export const formatSourceStylePropertyName = (style: Record<string, string>) => {
101
- const newStyle: Record<string, string> = {};
102
- Object.keys(style).forEach((key) => {
103
- // 处理 css 前缀
104
- let preKey = key.replace('-webkit', 'Webkit');
105
- preKey = preKey.replace('-ms', 'ms');
106
- preKey = preKey.replace('-moz', 'Moz');
107
- preKey = preKey.replace('-o', 'O');
108
- let newKey = preKey.split('-');
109
- if (newKey.length >= 2) {
110
- newKey = newKey.map((val, index) => {
111
- if (index !== 0) {
112
- return capitalize(val);
113
- } else {
114
- return val;
115
- }
116
- });
117
- }
118
- newStyle[newKey.join('')] = style[key];
119
- });
120
-
121
- return newStyle;
122
- };
123
-
124
- export const getCSSTextValue = (cssObj: Record<string, string>) => {
125
- let res = '';
126
- Object.keys(cssObj).forEach((key) => {
127
- res += `${key}:${cssObj[key]};`;
128
- });
129
- return res;
130
- };
131
-
132
- export const collectVariable = (assetPackages: AssetPackage[], win: Window) => {
133
- const res: Record<string, any> = {};
134
- assetPackages.forEach((el) => {
135
- if (el.globalName) {
136
- const target = (win as any)[el.globalName];
137
- if (target) {
138
- res[el.globalName] = target;
139
- }
140
- }
141
- });
142
- return res;
143
- };
144
-
145
- export const flatObject = (obj: Record<string, any>, level = 1) => {
146
- let count = 0;
147
- let currentObj = obj;
148
- let newObj: Record<string, any> = {};
149
- let res = {};
150
- while (count < level) {
151
- Object.keys(currentObj).forEach((key) => {
152
- newObj = {
153
- ...newObj,
154
- ...currentObj[key],
155
- };
156
- });
157
- res = newObj;
158
- currentObj = newObj;
159
- newObj = {};
160
- count += 1;
161
- }
162
-
163
- return res;
164
- };
package/src/vite-env.d.ts DELETED
@@ -1 +0,0 @@
1
- /// <reference types="vite/client" />