@dajiaoai/algeo-sdk 1.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/README.md +167 -0
- package/dist/algeo-sdk.cjs.js +159 -0
- package/dist/algeo-sdk.esm.js +155 -0
- package/dist/algeo-sdk.umd.js +165 -0
- package/dist/index.d.ts +89 -0
- package/package.json +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# Algeo SDK
|
|
2
|
+
|
|
3
|
+
大角几何内嵌画板 SDK,支持在任意网页中嵌入几何画板,通过 postMessage 与内嵌页通信。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
### npm
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# 安装最新版本
|
|
11
|
+
npm install @dajiaoai/algeo-sdk
|
|
12
|
+
|
|
13
|
+
# 安装指定版本(推荐生产环境锁定版本)
|
|
14
|
+
npm install @dajiaoai/algeo-sdk@1.0.0
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
在 `package.json` 中:
|
|
18
|
+
|
|
19
|
+
```json
|
|
20
|
+
{
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@dajiaoai/algeo-sdk": "1.0.0" // 精确版本,生产推荐
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
或使用 semver 范围:
|
|
28
|
+
|
|
29
|
+
```json
|
|
30
|
+
{
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@dajiaoai/algeo-sdk": "^1.0.0" // 兼容 1.x 的更新
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### CDN
|
|
38
|
+
|
|
39
|
+
**api.dajiaoai.com(推荐)**:
|
|
40
|
+
|
|
41
|
+
```html
|
|
42
|
+
<!-- 引用具体版本(推荐生产环境) -->
|
|
43
|
+
<script src="https://api.dajiaoai.com/js/algeo-sdk@1.0.0/algeo-sdk.umd.js"></script>
|
|
44
|
+
|
|
45
|
+
<!-- 引用最新版本 -->
|
|
46
|
+
<script src="https://api.dajiaoai.com/js/algeo-sdk@latest/algeo-sdk.umd.js"></script>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**unpkg**:
|
|
50
|
+
|
|
51
|
+
```html
|
|
52
|
+
<script src="https://unpkg.com/@dajiaoai/algeo-sdk@1.0.0/dist/algeo-sdk.umd.js"></script>
|
|
53
|
+
<script src="https://unpkg.com/@dajiaoai/algeo-sdk@latest/dist/algeo-sdk.umd.js"></script>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**jsDelivr**:
|
|
57
|
+
|
|
58
|
+
```html
|
|
59
|
+
<script src="https://cdn.jsdelivr.net/npm/@dajiaoai/algeo-sdk@1.0.0/dist/algeo-sdk.umd.js"></script>
|
|
60
|
+
<script src="https://cdn.jsdelivr.net/npm/@dajiaoai/algeo-sdk@latest/dist/algeo-sdk.umd.js"></script>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
> 生产环境建议使用**具体版本号**,避免自动升级带来的兼容性风险。
|
|
64
|
+
|
|
65
|
+
## 使用方式
|
|
66
|
+
|
|
67
|
+
### 方式一:SDK(绑定 DOM)
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
import { AlgeoSdk } from '@dajiaoai/algeo-sdk';
|
|
71
|
+
|
|
72
|
+
const container = document.getElementById('algeo-container');
|
|
73
|
+
const sdk = new AlgeoSdk(container, {
|
|
74
|
+
baseUrl: 'https://dajiaoai.com',
|
|
75
|
+
initialId: 'E8NHN7OP', // 可选,初始加载的分享 ID
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// 等待就绪(可选)
|
|
79
|
+
window.addEventListener('message', (e) => {
|
|
80
|
+
if (e.data?.type === 'ready') {
|
|
81
|
+
console.log('内嵌页就绪', e.data.version);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// 调用方法
|
|
86
|
+
sdk.loadShareById('E8NHN7OP').then(() => console.log('加载成功'));
|
|
87
|
+
sdk.getSlideCount().then(({ count }) => console.log('画板数量:', count));
|
|
88
|
+
sdk.switchSlide(1).then(() => console.log('切换画板'));
|
|
89
|
+
|
|
90
|
+
// 销毁
|
|
91
|
+
// sdk.destroy();
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 方式二:直接 iframe(无 SDK)
|
|
95
|
+
|
|
96
|
+
直接在 iframe 的 `src` 中指定分享 ID,无需引入 SDK:
|
|
97
|
+
|
|
98
|
+
```html
|
|
99
|
+
<iframe id="algeo-embed" src="https://dajiaoai.com/e/E8NHN7OP" allow="fullscreen"></iframe>
|
|
100
|
+
<script>
|
|
101
|
+
window.addEventListener('message', (e) => {
|
|
102
|
+
if (e.data?.type === 'ready') console.log('就绪', e.data.version);
|
|
103
|
+
});
|
|
104
|
+
</script>
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
如需动态加载、切换画板等能力,请使用方式一(SDK)。详见 [postMessage 协议](#postmessage-协议)。
|
|
108
|
+
|
|
109
|
+
> **全屏支持**:建议在 iframe 上添加 `allow="fullscreen"`,以便内嵌画板使用全屏功能。SDK 方式创建的 iframe 已自动包含此属性。
|
|
110
|
+
|
|
111
|
+
## API
|
|
112
|
+
|
|
113
|
+
### `VERSION`
|
|
114
|
+
|
|
115
|
+
SDK 版本号字符串,构建时注入,可用于运行时校验:
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
import { VERSION } from '@dajiaoai/algeo-sdk';
|
|
119
|
+
console.log('Algeo SDK version:', VERSION);
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### `new AlgeoSdk(container, options?)`
|
|
123
|
+
|
|
124
|
+
- `container`: 挂载的 DOM 元素
|
|
125
|
+
- `options.baseUrl`: 内嵌页基础 URL,默认 `https://dajiaoai.com`
|
|
126
|
+
- `options.initialId`: 初始加载的分享 ID,可选
|
|
127
|
+
|
|
128
|
+
### `sdk.loadShareById(id: string): Promise<LoadShareByIdResult>`
|
|
129
|
+
|
|
130
|
+
按分享 ID 加载内容。
|
|
131
|
+
|
|
132
|
+
### `sdk.loadFile(content: FileContent): Promise<LoadFileResult>`
|
|
133
|
+
|
|
134
|
+
加载完整文件数据(覆盖式)。
|
|
135
|
+
|
|
136
|
+
### `sdk.getSlideCount(): Promise<GetSlideCountResult>`
|
|
137
|
+
|
|
138
|
+
查询当前画板数量。返回 `{ count: number }`。
|
|
139
|
+
|
|
140
|
+
### `sdk.switchSlide(index: number): Promise<SwitchSlideResult>`
|
|
141
|
+
|
|
142
|
+
切换到指定索引的画板。
|
|
143
|
+
|
|
144
|
+
### `sdk.repl(command: string): Promise<ReplResult>`
|
|
145
|
+
|
|
146
|
+
执行 REPL 指令,返回面向 AI 的文档/文本内容。`command` 为 REPL 可用的单个指令,如 `help`、`list`、`list_slides`、`eval` 等。返回 `{ output: string }`。
|
|
147
|
+
|
|
148
|
+
### `sdk.destroy(): void`
|
|
149
|
+
|
|
150
|
+
销毁实例,移除 iframe 与事件监听。
|
|
151
|
+
|
|
152
|
+
## postMessage 协议
|
|
153
|
+
|
|
154
|
+
| 方法 | 请求 payload | 说明 |
|
|
155
|
+
| --------------- | ----------------------------------------------------------- | --------------------------------------- |
|
|
156
|
+
| `loadShareById` | `{ type: 'loadShareById', id: string, requestId? }` | 按分享 ID 加载 |
|
|
157
|
+
| `loadFile` | `{ type: 'loadFile', content: FileContentV10, requestId? }` | 加载文件数据 |
|
|
158
|
+
| `getSlideCount` | `{ type: 'getSlideCount', requestId? }` | 查询画板数量 |
|
|
159
|
+
| `switchSlide` | `{ type: 'switchSlide', index: number, requestId? }` | 切换画板 |
|
|
160
|
+
| `repl` | `{ type: 'repl', command: string, requestId? }` | 执行 REPL 指令,返回面向 AI 的文档/文本 |
|
|
161
|
+
|
|
162
|
+
响应格式:
|
|
163
|
+
|
|
164
|
+
- 成功:`{ type: 'response', requestId, success: true, result? }`
|
|
165
|
+
- 失败:`{ type: 'response', requestId, success: false, error: { code, message, details? } }`
|
|
166
|
+
|
|
167
|
+
内嵌页就绪时向父页面发送:`{ type: 'ready', version }`
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 大角几何内嵌画板 SDK
|
|
5
|
+
* 绑定 DOM 容器,自动创建 iframe 并封装 postMessage 通信
|
|
6
|
+
*/
|
|
7
|
+
/** SDK 版本号,构建时由 rollup 注入 */
|
|
8
|
+
const VERSION = '1.0.1';
|
|
9
|
+
const DEFAULT_EMBED_BASE = 'http://10.23.0.251:8083';
|
|
10
|
+
let requestIdCounter = 0;
|
|
11
|
+
function generateRequestId() {
|
|
12
|
+
return `req-${Date.now()}-${++requestIdCounter}`;
|
|
13
|
+
}
|
|
14
|
+
class AlgeoSdkError extends Error {
|
|
15
|
+
constructor(message, code, details) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.details = details;
|
|
19
|
+
this.name = 'AlgeoSdkError';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function isResponseMessage(msg) {
|
|
23
|
+
return (typeof msg === 'object' &&
|
|
24
|
+
msg !== null &&
|
|
25
|
+
'type' in msg &&
|
|
26
|
+
msg.type === 'response' &&
|
|
27
|
+
'requestId' in msg);
|
|
28
|
+
}
|
|
29
|
+
function isReadyMessage(msg) {
|
|
30
|
+
return (typeof msg === 'object' &&
|
|
31
|
+
msg !== null &&
|
|
32
|
+
'type' in msg &&
|
|
33
|
+
msg.type === 'ready');
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 大角几何内嵌画板 SDK
|
|
37
|
+
*/
|
|
38
|
+
class AlgeoSdk {
|
|
39
|
+
/** 是否已就绪(收到 iframe ready 通知) */
|
|
40
|
+
get ready() {
|
|
41
|
+
return this._ready;
|
|
42
|
+
}
|
|
43
|
+
/** 内嵌页协议版本 */
|
|
44
|
+
get version() {
|
|
45
|
+
return this._version;
|
|
46
|
+
}
|
|
47
|
+
constructor(container, options = {}) {
|
|
48
|
+
this.pending = new Map();
|
|
49
|
+
this._ready = false;
|
|
50
|
+
this._version = null;
|
|
51
|
+
this.container = container;
|
|
52
|
+
this.baseUrl = options.baseUrl ?? DEFAULT_EMBED_BASE;
|
|
53
|
+
const initialId = options.initialId ?? '';
|
|
54
|
+
const src = initialId
|
|
55
|
+
? `${this.baseUrl}/e/${encodeURIComponent(initialId)}`
|
|
56
|
+
: `${this.baseUrl}/e`;
|
|
57
|
+
this.iframe = document.createElement('iframe');
|
|
58
|
+
this.iframe.id = 'algeo-embed';
|
|
59
|
+
this.iframe.src = src;
|
|
60
|
+
this.iframe.allow = 'fullscreen';
|
|
61
|
+
this.iframe.style.width = '100%';
|
|
62
|
+
this.iframe.style.height = '100%';
|
|
63
|
+
this.iframe.style.border = 'none';
|
|
64
|
+
this.container.appendChild(this.iframe);
|
|
65
|
+
this.messageHandler = (e) => {
|
|
66
|
+
const { data } = e;
|
|
67
|
+
if (isReadyMessage(data)) {
|
|
68
|
+
this._ready = true;
|
|
69
|
+
this._version = data.version;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (isResponseMessage(data)) {
|
|
73
|
+
const { requestId, success } = data;
|
|
74
|
+
const pending = this.pending.get(requestId);
|
|
75
|
+
if (pending) {
|
|
76
|
+
this.pending.delete(requestId);
|
|
77
|
+
if (success) {
|
|
78
|
+
pending.resolve(data.result);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
const err = data.error;
|
|
82
|
+
pending.reject(new AlgeoSdkError(err.message, err.code, err.details));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
window.addEventListener('message', this.messageHandler);
|
|
88
|
+
}
|
|
89
|
+
post(type, payload) {
|
|
90
|
+
const requestId = generateRequestId();
|
|
91
|
+
const msg = { type, requestId, ...payload };
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
this.pending.set(requestId, {
|
|
94
|
+
resolve: resolve,
|
|
95
|
+
reject,
|
|
96
|
+
});
|
|
97
|
+
const target = this.iframe.contentWindow;
|
|
98
|
+
if (!target) {
|
|
99
|
+
this.pending.delete(requestId);
|
|
100
|
+
reject(new AlgeoSdkError('iframe 未加载完成', 'IFRAME_NOT_READY'));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
target.postMessage(msg, '*');
|
|
104
|
+
// 超时兜底
|
|
105
|
+
setTimeout(() => {
|
|
106
|
+
if (this.pending.has(requestId)) {
|
|
107
|
+
this.pending.delete(requestId);
|
|
108
|
+
reject(new AlgeoSdkError('请求超时', 'TIMEOUT'));
|
|
109
|
+
}
|
|
110
|
+
}, 30000);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 按分享 ID 加载内容
|
|
115
|
+
*/
|
|
116
|
+
loadShareById(id) {
|
|
117
|
+
return this.post('loadShareById', { id });
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 加载完整文件内容(覆盖式)
|
|
121
|
+
*/
|
|
122
|
+
loadFile(content) {
|
|
123
|
+
return this.post('loadFile', { content });
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 切换到指定索引的画板
|
|
127
|
+
*/
|
|
128
|
+
switchSlide(index) {
|
|
129
|
+
return this.post('switchSlide', { index });
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* 查询画板数量
|
|
133
|
+
*/
|
|
134
|
+
getSlideCount() {
|
|
135
|
+
return this.post('getSlideCount', {});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 执行 REPL 指令,返回面向 AI 的文档/文本内容
|
|
139
|
+
* @param command REPL 可用的单个 command 指令,如 help、list、list_slides、eval 等
|
|
140
|
+
*/
|
|
141
|
+
repl(command) {
|
|
142
|
+
return this.post('repl', { command });
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* 销毁实例,移除 iframe 与事件监听
|
|
146
|
+
*/
|
|
147
|
+
destroy() {
|
|
148
|
+
window.removeEventListener('message', this.messageHandler);
|
|
149
|
+
this.pending.forEach(({ reject }) => reject(new AlgeoSdkError('SDK 已销毁', 'DESTROYED')));
|
|
150
|
+
this.pending.clear();
|
|
151
|
+
if (this.iframe.parentNode) {
|
|
152
|
+
this.iframe.parentNode.removeChild(this.iframe);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
exports.AlgeoSdk = AlgeoSdk;
|
|
158
|
+
exports.AlgeoSdkError = AlgeoSdkError;
|
|
159
|
+
exports.VERSION = VERSION;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 大角几何内嵌画板 SDK
|
|
3
|
+
* 绑定 DOM 容器,自动创建 iframe 并封装 postMessage 通信
|
|
4
|
+
*/
|
|
5
|
+
/** SDK 版本号,构建时由 rollup 注入 */
|
|
6
|
+
const VERSION = '1.0.1';
|
|
7
|
+
const DEFAULT_EMBED_BASE = 'http://10.23.0.251:8083';
|
|
8
|
+
let requestIdCounter = 0;
|
|
9
|
+
function generateRequestId() {
|
|
10
|
+
return `req-${Date.now()}-${++requestIdCounter}`;
|
|
11
|
+
}
|
|
12
|
+
class AlgeoSdkError extends Error {
|
|
13
|
+
constructor(message, code, details) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.details = details;
|
|
17
|
+
this.name = 'AlgeoSdkError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function isResponseMessage(msg) {
|
|
21
|
+
return (typeof msg === 'object' &&
|
|
22
|
+
msg !== null &&
|
|
23
|
+
'type' in msg &&
|
|
24
|
+
msg.type === 'response' &&
|
|
25
|
+
'requestId' in msg);
|
|
26
|
+
}
|
|
27
|
+
function isReadyMessage(msg) {
|
|
28
|
+
return (typeof msg === 'object' &&
|
|
29
|
+
msg !== null &&
|
|
30
|
+
'type' in msg &&
|
|
31
|
+
msg.type === 'ready');
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 大角几何内嵌画板 SDK
|
|
35
|
+
*/
|
|
36
|
+
class AlgeoSdk {
|
|
37
|
+
/** 是否已就绪(收到 iframe ready 通知) */
|
|
38
|
+
get ready() {
|
|
39
|
+
return this._ready;
|
|
40
|
+
}
|
|
41
|
+
/** 内嵌页协议版本 */
|
|
42
|
+
get version() {
|
|
43
|
+
return this._version;
|
|
44
|
+
}
|
|
45
|
+
constructor(container, options = {}) {
|
|
46
|
+
this.pending = new Map();
|
|
47
|
+
this._ready = false;
|
|
48
|
+
this._version = null;
|
|
49
|
+
this.container = container;
|
|
50
|
+
this.baseUrl = options.baseUrl ?? DEFAULT_EMBED_BASE;
|
|
51
|
+
const initialId = options.initialId ?? '';
|
|
52
|
+
const src = initialId
|
|
53
|
+
? `${this.baseUrl}/e/${encodeURIComponent(initialId)}`
|
|
54
|
+
: `${this.baseUrl}/e`;
|
|
55
|
+
this.iframe = document.createElement('iframe');
|
|
56
|
+
this.iframe.id = 'algeo-embed';
|
|
57
|
+
this.iframe.src = src;
|
|
58
|
+
this.iframe.allow = 'fullscreen';
|
|
59
|
+
this.iframe.style.width = '100%';
|
|
60
|
+
this.iframe.style.height = '100%';
|
|
61
|
+
this.iframe.style.border = 'none';
|
|
62
|
+
this.container.appendChild(this.iframe);
|
|
63
|
+
this.messageHandler = (e) => {
|
|
64
|
+
const { data } = e;
|
|
65
|
+
if (isReadyMessage(data)) {
|
|
66
|
+
this._ready = true;
|
|
67
|
+
this._version = data.version;
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (isResponseMessage(data)) {
|
|
71
|
+
const { requestId, success } = data;
|
|
72
|
+
const pending = this.pending.get(requestId);
|
|
73
|
+
if (pending) {
|
|
74
|
+
this.pending.delete(requestId);
|
|
75
|
+
if (success) {
|
|
76
|
+
pending.resolve(data.result);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
const err = data.error;
|
|
80
|
+
pending.reject(new AlgeoSdkError(err.message, err.code, err.details));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
window.addEventListener('message', this.messageHandler);
|
|
86
|
+
}
|
|
87
|
+
post(type, payload) {
|
|
88
|
+
const requestId = generateRequestId();
|
|
89
|
+
const msg = { type, requestId, ...payload };
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
this.pending.set(requestId, {
|
|
92
|
+
resolve: resolve,
|
|
93
|
+
reject,
|
|
94
|
+
});
|
|
95
|
+
const target = this.iframe.contentWindow;
|
|
96
|
+
if (!target) {
|
|
97
|
+
this.pending.delete(requestId);
|
|
98
|
+
reject(new AlgeoSdkError('iframe 未加载完成', 'IFRAME_NOT_READY'));
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
target.postMessage(msg, '*');
|
|
102
|
+
// 超时兜底
|
|
103
|
+
setTimeout(() => {
|
|
104
|
+
if (this.pending.has(requestId)) {
|
|
105
|
+
this.pending.delete(requestId);
|
|
106
|
+
reject(new AlgeoSdkError('请求超时', 'TIMEOUT'));
|
|
107
|
+
}
|
|
108
|
+
}, 30000);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 按分享 ID 加载内容
|
|
113
|
+
*/
|
|
114
|
+
loadShareById(id) {
|
|
115
|
+
return this.post('loadShareById', { id });
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 加载完整文件内容(覆盖式)
|
|
119
|
+
*/
|
|
120
|
+
loadFile(content) {
|
|
121
|
+
return this.post('loadFile', { content });
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* 切换到指定索引的画板
|
|
125
|
+
*/
|
|
126
|
+
switchSlide(index) {
|
|
127
|
+
return this.post('switchSlide', { index });
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 查询画板数量
|
|
131
|
+
*/
|
|
132
|
+
getSlideCount() {
|
|
133
|
+
return this.post('getSlideCount', {});
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 执行 REPL 指令,返回面向 AI 的文档/文本内容
|
|
137
|
+
* @param command REPL 可用的单个 command 指令,如 help、list、list_slides、eval 等
|
|
138
|
+
*/
|
|
139
|
+
repl(command) {
|
|
140
|
+
return this.post('repl', { command });
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* 销毁实例,移除 iframe 与事件监听
|
|
144
|
+
*/
|
|
145
|
+
destroy() {
|
|
146
|
+
window.removeEventListener('message', this.messageHandler);
|
|
147
|
+
this.pending.forEach(({ reject }) => reject(new AlgeoSdkError('SDK 已销毁', 'DESTROYED')));
|
|
148
|
+
this.pending.clear();
|
|
149
|
+
if (this.iframe.parentNode) {
|
|
150
|
+
this.iframe.parentNode.removeChild(this.iframe);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export { AlgeoSdk, AlgeoSdkError, VERSION };
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.AlgeoSdk = {}));
|
|
5
|
+
})(this, (function (exports) { 'use strict';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 大角几何内嵌画板 SDK
|
|
9
|
+
* 绑定 DOM 容器,自动创建 iframe 并封装 postMessage 通信
|
|
10
|
+
*/
|
|
11
|
+
/** SDK 版本号,构建时由 rollup 注入 */
|
|
12
|
+
const VERSION = '1.0.1';
|
|
13
|
+
const DEFAULT_EMBED_BASE = 'http://10.23.0.251:8083';
|
|
14
|
+
let requestIdCounter = 0;
|
|
15
|
+
function generateRequestId() {
|
|
16
|
+
return `req-${Date.now()}-${++requestIdCounter}`;
|
|
17
|
+
}
|
|
18
|
+
class AlgeoSdkError extends Error {
|
|
19
|
+
constructor(message, code, details) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.code = code;
|
|
22
|
+
this.details = details;
|
|
23
|
+
this.name = 'AlgeoSdkError';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function isResponseMessage(msg) {
|
|
27
|
+
return (typeof msg === 'object' &&
|
|
28
|
+
msg !== null &&
|
|
29
|
+
'type' in msg &&
|
|
30
|
+
msg.type === 'response' &&
|
|
31
|
+
'requestId' in msg);
|
|
32
|
+
}
|
|
33
|
+
function isReadyMessage(msg) {
|
|
34
|
+
return (typeof msg === 'object' &&
|
|
35
|
+
msg !== null &&
|
|
36
|
+
'type' in msg &&
|
|
37
|
+
msg.type === 'ready');
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 大角几何内嵌画板 SDK
|
|
41
|
+
*/
|
|
42
|
+
class AlgeoSdk {
|
|
43
|
+
/** 是否已就绪(收到 iframe ready 通知) */
|
|
44
|
+
get ready() {
|
|
45
|
+
return this._ready;
|
|
46
|
+
}
|
|
47
|
+
/** 内嵌页协议版本 */
|
|
48
|
+
get version() {
|
|
49
|
+
return this._version;
|
|
50
|
+
}
|
|
51
|
+
constructor(container, options = {}) {
|
|
52
|
+
this.pending = new Map();
|
|
53
|
+
this._ready = false;
|
|
54
|
+
this._version = null;
|
|
55
|
+
this.container = container;
|
|
56
|
+
this.baseUrl = options.baseUrl ?? DEFAULT_EMBED_BASE;
|
|
57
|
+
const initialId = options.initialId ?? '';
|
|
58
|
+
const src = initialId
|
|
59
|
+
? `${this.baseUrl}/e/${encodeURIComponent(initialId)}`
|
|
60
|
+
: `${this.baseUrl}/e`;
|
|
61
|
+
this.iframe = document.createElement('iframe');
|
|
62
|
+
this.iframe.id = 'algeo-embed';
|
|
63
|
+
this.iframe.src = src;
|
|
64
|
+
this.iframe.allow = 'fullscreen';
|
|
65
|
+
this.iframe.style.width = '100%';
|
|
66
|
+
this.iframe.style.height = '100%';
|
|
67
|
+
this.iframe.style.border = 'none';
|
|
68
|
+
this.container.appendChild(this.iframe);
|
|
69
|
+
this.messageHandler = (e) => {
|
|
70
|
+
const { data } = e;
|
|
71
|
+
if (isReadyMessage(data)) {
|
|
72
|
+
this._ready = true;
|
|
73
|
+
this._version = data.version;
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (isResponseMessage(data)) {
|
|
77
|
+
const { requestId, success } = data;
|
|
78
|
+
const pending = this.pending.get(requestId);
|
|
79
|
+
if (pending) {
|
|
80
|
+
this.pending.delete(requestId);
|
|
81
|
+
if (success) {
|
|
82
|
+
pending.resolve(data.result);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const err = data.error;
|
|
86
|
+
pending.reject(new AlgeoSdkError(err.message, err.code, err.details));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
window.addEventListener('message', this.messageHandler);
|
|
92
|
+
}
|
|
93
|
+
post(type, payload) {
|
|
94
|
+
const requestId = generateRequestId();
|
|
95
|
+
const msg = { type, requestId, ...payload };
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
this.pending.set(requestId, {
|
|
98
|
+
resolve: resolve,
|
|
99
|
+
reject,
|
|
100
|
+
});
|
|
101
|
+
const target = this.iframe.contentWindow;
|
|
102
|
+
if (!target) {
|
|
103
|
+
this.pending.delete(requestId);
|
|
104
|
+
reject(new AlgeoSdkError('iframe 未加载完成', 'IFRAME_NOT_READY'));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
target.postMessage(msg, '*');
|
|
108
|
+
// 超时兜底
|
|
109
|
+
setTimeout(() => {
|
|
110
|
+
if (this.pending.has(requestId)) {
|
|
111
|
+
this.pending.delete(requestId);
|
|
112
|
+
reject(new AlgeoSdkError('请求超时', 'TIMEOUT'));
|
|
113
|
+
}
|
|
114
|
+
}, 30000);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 按分享 ID 加载内容
|
|
119
|
+
*/
|
|
120
|
+
loadShareById(id) {
|
|
121
|
+
return this.post('loadShareById', { id });
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* 加载完整文件内容(覆盖式)
|
|
125
|
+
*/
|
|
126
|
+
loadFile(content) {
|
|
127
|
+
return this.post('loadFile', { content });
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 切换到指定索引的画板
|
|
131
|
+
*/
|
|
132
|
+
switchSlide(index) {
|
|
133
|
+
return this.post('switchSlide', { index });
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 查询画板数量
|
|
137
|
+
*/
|
|
138
|
+
getSlideCount() {
|
|
139
|
+
return this.post('getSlideCount', {});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 执行 REPL 指令,返回面向 AI 的文档/文本内容
|
|
143
|
+
* @param command REPL 可用的单个 command 指令,如 help、list、list_slides、eval 等
|
|
144
|
+
*/
|
|
145
|
+
repl(command) {
|
|
146
|
+
return this.post('repl', { command });
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 销毁实例,移除 iframe 与事件监听
|
|
150
|
+
*/
|
|
151
|
+
destroy() {
|
|
152
|
+
window.removeEventListener('message', this.messageHandler);
|
|
153
|
+
this.pending.forEach(({ reject }) => reject(new AlgeoSdkError('SDK 已销毁', 'DESTROYED')));
|
|
154
|
+
this.pending.clear();
|
|
155
|
+
if (this.iframe.parentNode) {
|
|
156
|
+
this.iframe.parentNode.removeChild(this.iframe);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
exports.AlgeoSdk = AlgeoSdk;
|
|
162
|
+
exports.AlgeoSdkError = AlgeoSdkError;
|
|
163
|
+
exports.VERSION = VERSION;
|
|
164
|
+
|
|
165
|
+
}));
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 大角几何内嵌画板 SDK
|
|
3
|
+
* 绑定 DOM 容器,自动创建 iframe 并封装 postMessage 通信
|
|
4
|
+
*/
|
|
5
|
+
/** SDK 版本号,构建时由 rollup 注入 */
|
|
6
|
+
export declare const VERSION = "__ALGEO_SDK_VERSION__";
|
|
7
|
+
/** loadFile 时传入的文件内容,需符合 FileContentV10 格式 */
|
|
8
|
+
export interface FileContent {
|
|
9
|
+
slides: unknown[];
|
|
10
|
+
messages: unknown[];
|
|
11
|
+
metadata: {
|
|
12
|
+
version: string;
|
|
13
|
+
shareOptions?: unknown;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export interface AlgeoSdkOptions {
|
|
17
|
+
/** 内嵌页基础 URL,默认 https://dajiaoai.com */
|
|
18
|
+
baseUrl?: string;
|
|
19
|
+
/** 初始加载的内容 ID,可选 */
|
|
20
|
+
initialId?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface LoadShareByIdResult {
|
|
23
|
+
success: true;
|
|
24
|
+
}
|
|
25
|
+
export interface LoadFileResult {
|
|
26
|
+
success: true;
|
|
27
|
+
}
|
|
28
|
+
export interface SwitchSlideResult {
|
|
29
|
+
success: true;
|
|
30
|
+
}
|
|
31
|
+
export interface GetSlideCountResult {
|
|
32
|
+
count: number;
|
|
33
|
+
}
|
|
34
|
+
export interface ReplResult {
|
|
35
|
+
output: string;
|
|
36
|
+
}
|
|
37
|
+
export interface AlgeoErrorPayload {
|
|
38
|
+
code: string;
|
|
39
|
+
message: string;
|
|
40
|
+
details?: unknown;
|
|
41
|
+
}
|
|
42
|
+
export declare class AlgeoSdkError extends Error {
|
|
43
|
+
code: string;
|
|
44
|
+
details?: unknown | undefined;
|
|
45
|
+
constructor(message: string, code: string, details?: unknown | undefined);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 大角几何内嵌画板 SDK
|
|
49
|
+
*/
|
|
50
|
+
export declare class AlgeoSdk {
|
|
51
|
+
private container;
|
|
52
|
+
private iframe;
|
|
53
|
+
private baseUrl;
|
|
54
|
+
private pending;
|
|
55
|
+
private messageHandler;
|
|
56
|
+
private _ready;
|
|
57
|
+
private _version;
|
|
58
|
+
/** 是否已就绪(收到 iframe ready 通知) */
|
|
59
|
+
get ready(): boolean;
|
|
60
|
+
/** 内嵌页协议版本 */
|
|
61
|
+
get version(): string | null;
|
|
62
|
+
constructor(container: HTMLElement, options?: AlgeoSdkOptions);
|
|
63
|
+
private post;
|
|
64
|
+
/**
|
|
65
|
+
* 按分享 ID 加载内容
|
|
66
|
+
*/
|
|
67
|
+
loadShareById(id: string): Promise<LoadShareByIdResult>;
|
|
68
|
+
/**
|
|
69
|
+
* 加载完整文件内容(覆盖式)
|
|
70
|
+
*/
|
|
71
|
+
loadFile(content: FileContent): Promise<LoadFileResult>;
|
|
72
|
+
/**
|
|
73
|
+
* 切换到指定索引的画板
|
|
74
|
+
*/
|
|
75
|
+
switchSlide(index: number): Promise<SwitchSlideResult>;
|
|
76
|
+
/**
|
|
77
|
+
* 查询画板数量
|
|
78
|
+
*/
|
|
79
|
+
getSlideCount(): Promise<GetSlideCountResult>;
|
|
80
|
+
/**
|
|
81
|
+
* 执行 REPL 指令,返回面向 AI 的文档/文本内容
|
|
82
|
+
* @param command REPL 可用的单个 command 指令,如 help、list、list_slides、eval 等
|
|
83
|
+
*/
|
|
84
|
+
repl(command: string): Promise<ReplResult>;
|
|
85
|
+
/**
|
|
86
|
+
* 销毁实例,移除 iframe 与事件监听
|
|
87
|
+
*/
|
|
88
|
+
destroy(): void;
|
|
89
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dajiaoai/algeo-sdk",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "大角几何内嵌画板 SDK,支持 iframe 挂载与 postMessage 通信",
|
|
5
|
+
"main": "dist/algeo-sdk.cjs.js",
|
|
6
|
+
"module": "dist/algeo-sdk.esm.js",
|
|
7
|
+
"unpkg": "dist/algeo-sdk.umd.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "rollup -c",
|
|
15
|
+
"typecheck": "tsc --noEmit",
|
|
16
|
+
"examples": "npx serve ."
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"algeo",
|
|
20
|
+
"dajiaoai",
|
|
21
|
+
"geometry",
|
|
22
|
+
"embed"
|
|
23
|
+
],
|
|
24
|
+
"author": "大角几何",
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"type": "module",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@rollup/plugin-replace": "^6.0.1",
|
|
29
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
30
|
+
"rollup": "^4.28.1",
|
|
31
|
+
"tslib": "^2.8.0",
|
|
32
|
+
"typescript": "^5.9.3"
|
|
33
|
+
}
|
|
34
|
+
}
|