@michaeltangseng/schemaai 0.0.1

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/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ // schemaAI npm-package public entry (初始迁移阶段)
2
+ // 当前仅导出最小可用 Renderer,用于 schema → React 的运行态渲染测试。
3
+ // `EditorStore` 仍在迁移中,暂不作为对外 API 参与构建。
4
+
5
+ export { default as schemaAIRenderer } from './renderer/Renderer';
6
+
7
+
@@ -0,0 +1,82 @@
1
+ import React from 'react';
2
+ // 为了在 demo 中独立运行,这里不用主工程的类型/组件注册表,使用最小内联类型与渲染逻辑。
3
+
4
+ export interface DemoNodeSchema {
5
+ id: string;
6
+ type: string;
7
+ props?: Record<string, any>;
8
+ style?: React.CSSProperties;
9
+ children?: DemoNodeSchema[];
10
+ }
11
+
12
+ /**
13
+ * 最小可用版本:
14
+ * - 只负责把 schema 渲染成 React 组件树
15
+ * - 不包含拖拽、选中、高亮、事件编辑等「编辑器态」能力
16
+ * - 主要用于 npm 包在运行态(runtime)下的集成测试
17
+ */
18
+
19
+ export interface SchemaAIRendererProps {
20
+ schema: DemoNodeSchema;
21
+ pageId?: string;
22
+ }
23
+
24
+ const renderNode = (node: DemoNodeSchema): React.ReactElement => {
25
+ const children = (node.children || []).map(child => renderNode(child));
26
+
27
+ // 最小映射:只处理极少数内置类型,其余用 div 包裹
28
+ if (node.type === 'PluginText') {
29
+ return (
30
+ <p
31
+ key={node.id}
32
+ style={node.style}
33
+ >
34
+ {node.props?.text ?? ''}
35
+ {children}
36
+ </p>
37
+ );
38
+ }
39
+
40
+ if (node.type === 'Container') {
41
+ return (
42
+ <div
43
+ key={node.id}
44
+ style={node.style}
45
+ >
46
+ {node.props?.title && (
47
+ <h2 style={{ margin: 0, marginBottom: 8, fontSize: 16 }}>{node.props.title}</h2>
48
+ )}
49
+ {children}
50
+ </div>
51
+ );
52
+ }
53
+
54
+ // 其他类型:简单兜底渲染
55
+ return (
56
+ <div
57
+ key={node.id}
58
+ style={{
59
+ padding: 8,
60
+ border: '1px dashed #f97316',
61
+ fontSize: 12,
62
+ color: '#c2410c',
63
+ borderRadius: 4,
64
+ margin: 4,
65
+ ...(node.style || {}),
66
+ }}
67
+ >
68
+ <div style={{ marginBottom: 4 }}>
69
+ Unknown component type: <code>{node.type}</code>
70
+ </div>
71
+ {children}
72
+ </div>
73
+ );
74
+ };
75
+
76
+ const SchemaAIRenderer = ({ schema }: SchemaAIRendererProps): JSX.Element => {
77
+ return renderNode(schema);
78
+ };
79
+
80
+ export default SchemaAIRenderer;
81
+
82
+
@@ -0,0 +1,13 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <title>schemaAI React Demo</title>
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ </head>
8
+ <body>
9
+ <div id="root"></div>
10
+ <script type="module" src="/src/main.tsx"></script>
11
+ </body>
12
+ </html>
13
+
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "schemaai-react-demo",
3
+ "version": "0.0.1",
4
+ "private": true,
5
+ "scripts": {
6
+ "dev": "vite",
7
+ "build": "vite build",
8
+ "preview": "vite preview"
9
+ },
10
+ "dependencies": {
11
+ "react": "^18.3.1",
12
+ "react-dom": "^18.3.1"
13
+ },
14
+ "devDependencies": {
15
+ "@types/react": "^18.3.3",
16
+ "@types/react-dom": "^18.3.0",
17
+ "@vitejs/plugin-react-swc": "^3.7.0",
18
+ "typescript": "^5.6.3",
19
+ "vite": "^5.4.8"
20
+ }
21
+ }