@chamn/render 0.0.1-beta.0

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 (50) hide show
  1. package/.eslintignore +1 -0
  2. package/.eslintrc.js +30 -0
  3. package/.prettierrc.json +7 -0
  4. package/LICENSE +201 -0
  5. package/__tests__/demo.test.ts +3 -0
  6. package/build.config.js +20 -0
  7. package/dist/commonComponent/index.d.ts +12 -0
  8. package/dist/const/index.d.ts +2 -0
  9. package/dist/core/ReactErrorBoundary.d.ts +26 -0
  10. package/dist/core/adapter.d.ts +64 -0
  11. package/dist/core/adapterReact.d.ts +108 -0
  12. package/dist/core/designReactRender.d.ts +47 -0
  13. package/dist/core/refManager.d.ts +7 -0
  14. package/dist/core/render.d.ts +32 -0
  15. package/dist/core/storeManager.d.ts +11 -0
  16. package/dist/core/type.d.ts +9 -0
  17. package/dist/index.cjs.js +16 -0
  18. package/dist/index.cjs.js.map +1 -0
  19. package/dist/index.d.ts +5 -0
  20. package/dist/index.es.js +4432 -0
  21. package/dist/index.es.js.map +1 -0
  22. package/dist/util/assetsLoader.d.ts +15 -0
  23. package/dist/util/index.d.ts +17 -0
  24. package/dist/vite-env.d.ts +1 -0
  25. package/dist/vite.svg +1 -0
  26. package/index.html +16 -0
  27. package/jest.config.js +196 -0
  28. package/package.json +42 -0
  29. package/public/vite.svg +1 -0
  30. package/src/_dev_/components.tsx +12 -0
  31. package/src/_dev_/dev.tsx +12 -0
  32. package/src/_dev_/index.css +13 -0
  33. package/src/_dev_/page/DesignerRenderDemo.tsx +65 -0
  34. package/src/_dev_/page/RenderDemo.tsx +60 -0
  35. package/src/_dev_/router.tsx +15 -0
  36. package/src/commonComponent/index.tsx +184 -0
  37. package/src/const/index.ts +5 -0
  38. package/src/core/ReactErrorBoundary.ts +91 -0
  39. package/src/core/adapter.ts +133 -0
  40. package/src/core/adapterReact.ts +734 -0
  41. package/src/core/designReactRender.ts +325 -0
  42. package/src/core/refManager.ts +18 -0
  43. package/src/core/render.ts +123 -0
  44. package/src/core/storeManager.ts +57 -0
  45. package/src/core/type.ts +10 -0
  46. package/src/index.ts +5 -0
  47. package/src/util/assetsLoader.ts +72 -0
  48. package/src/util/index.ts +129 -0
  49. package/src/vite-env.d.ts +1 -0
  50. package/tsconfig.json +26 -0
@@ -0,0 +1,129 @@
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
+
6
+ export const isClass = function (val: any) {
7
+ if (!val) {
8
+ return false;
9
+ }
10
+ if (typeof val !== 'function') {
11
+ return false;
12
+ }
13
+ if (!val.prototype) {
14
+ return false;
15
+ }
16
+ return true;
17
+ };
18
+
19
+ // eslint-disable-next-line @typescript-eslint/ban-types
20
+ export function shouldConstruct(Component: Function) {
21
+ const prototype = Component.prototype;
22
+ return !!(prototype && prototype.isReactComponent);
23
+ }
24
+
25
+ export function canAcceptsRef(Comp: any) {
26
+ const hasSymbol = typeof Symbol === 'function' && Symbol.for;
27
+ const REACT_FORWARD_REF_TYPE = hasSymbol ? Symbol.for('react.forward_ref') : 0xead0;
28
+ return Comp?.$$typeof === REACT_FORWARD_REF_TYPE || Comp?.prototype?.isReactComponent || Comp?.prototype?.setState || Comp._forwardRef;
29
+ }
30
+
31
+ export function compWrapper(Comp: any) {
32
+ class WrapperForRef extends Component {
33
+ render() {
34
+ // 需要直接调用 react api ,避免被拦截 !!!!
35
+ return createElement(Comp, this.props);
36
+ }
37
+ }
38
+ (WrapperForRef as any).displayName = Comp.displayName;
39
+
40
+ return WrapperForRef as any;
41
+ }
42
+
43
+ export const runExpression = (expStr: string, context: any) => {
44
+ const run = (expStr: string) => {
45
+ const contextVar = Object.keys(context).map((key) => {
46
+ return `const ${key} = $$context['${key}'];`;
47
+ });
48
+ const executeCode = `
49
+ ${contextVar.join('\n')}
50
+ return ${expStr};
51
+ `;
52
+ return new Function('$$context', executeCode)(context);
53
+ };
54
+ try {
55
+ return run(expStr);
56
+ } catch (e) {
57
+ console.warn(e);
58
+ const msg = `[${expStr}] expression run failed`;
59
+ console.warn(msg);
60
+ return null;
61
+ }
62
+ };
63
+
64
+ export const convertCodeStringToFunction = (functionStr: string, $$context: ContextType, storeManager: StoreManager) => {
65
+ const newFunc = function (...args: any[]) {
66
+ try {
67
+ const codeBody = `
68
+ var f = ${functionStr};
69
+ var args = Array.from(arguments);
70
+ var __$$storeManager__ = args.pop();
71
+ var $$context = args.pop();
72
+ $$context.stateManager = __$$storeManager__.getStateSnapshot();
73
+ return f.apply(f, args)
74
+ `;
75
+ const f = new Function(codeBody);
76
+ f(...args, $$context, storeManager);
77
+ } catch (e) {
78
+ console.warn(e);
79
+ }
80
+ };
81
+ return newFunc;
82
+ };
83
+
84
+ /**
85
+ *
86
+ * @param args 对象的值
87
+ * @param argsName 对因位置的 名称
88
+ * @returns
89
+ */
90
+ export const getObjFromArrayMap = (args: any[], argsName: string[]) => {
91
+ const params: Record<any, any> = {};
92
+ argsName.forEach((paramName, index) => {
93
+ params[paramName] = args[index];
94
+ });
95
+
96
+ return params;
97
+ };
98
+
99
+ export const formatSourceStylePropertyName = (style: Record<string, string>) => {
100
+ const newStyle: Record<string, string> = {};
101
+ Object.keys(style).forEach((key) => {
102
+ // 处理 css 前缀
103
+ let preKey = key.replace('-webkit', 'Webkit');
104
+ preKey = preKey.replace('-ms', 'ms');
105
+ preKey = preKey.replace('-moz', 'Moz');
106
+ preKey = preKey.replace('-o', 'O');
107
+ let newKey = preKey.split('-');
108
+ if (newKey.length >= 2) {
109
+ newKey = newKey.map((val, index) => {
110
+ if (index !== 0) {
111
+ return capitalize(val);
112
+ } else {
113
+ return val;
114
+ }
115
+ });
116
+ }
117
+ newStyle[newKey.join('')] = style[key];
118
+ });
119
+
120
+ return newStyle;
121
+ };
122
+
123
+ export const getCSSTextValue = (cssObj: Record<string, string>) => {
124
+ let res = '';
125
+ Object.keys(cssObj).forEach((key) => {
126
+ res += `${key}:${cssObj[key]};`;
127
+ });
128
+ return res;
129
+ };
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />
package/tsconfig.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "useDefineForClassFields": true,
5
+ "lib": [
6
+ "DOM",
7
+ "DOM.Iterable",
8
+ "ESNext"
9
+ ],
10
+ "allowJs": false,
11
+ "skipLibCheck": true,
12
+ "esModuleInterop": false,
13
+ "allowSyntheticDefaultImports": true,
14
+ "strict": true,
15
+ "forceConsistentCasingInFileNames": true,
16
+ "module": "ESNext",
17
+ "moduleResolution": "Node",
18
+ "resolveJsonModule": true,
19
+ "isolatedModules": false,
20
+ "noEmit": true,
21
+ "jsx": "react"
22
+ },
23
+ "include": [
24
+ "src"
25
+ ],
26
+ }