@dajiaoai/algeo-sdk 1.2.1 → 2.5.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.
package/README.md CHANGED
@@ -11,7 +11,7 @@
11
11
  npm install @dajiaoai/algeo-sdk
12
12
 
13
13
  # 安装指定版本(推荐生产环境锁定版本)
14
- npm install @dajiaoai/algeo-sdk@1.2.1
14
+ npm install @dajiaoai/algeo-sdk@2.5.0
15
15
  ```
16
16
 
17
17
  在 `package.json` 中:
@@ -19,7 +19,7 @@ npm install @dajiaoai/algeo-sdk@1.2.1
19
19
  ```json
20
20
  {
21
21
  "dependencies": {
22
- "@dajiaoai/algeo-sdk": "1.2.1" // 精确版本,生产推荐
22
+ "@dajiaoai/algeo-sdk": "2.5.0" // 精确版本,生产推荐
23
23
  }
24
24
  }
25
25
  ```
@@ -29,7 +29,7 @@ npm install @dajiaoai/algeo-sdk@1.2.1
29
29
  ```json
30
30
  {
31
31
  "dependencies": {
32
- "@dajiaoai/algeo-sdk": "^1.2.1" // 兼容 1.x 的更新
32
+ "@dajiaoai/algeo-sdk": "^2.5.0" // 兼容 2.x 的更新
33
33
  }
34
34
  }
35
35
  ```
@@ -39,14 +39,14 @@ npm install @dajiaoai/algeo-sdk@1.2.1
39
39
  **unpkg**:
40
40
 
41
41
  ```html
42
- <script src="https://unpkg.com/@dajiaoai/algeo-sdk@1.2.1/dist/algeo-sdk.umd.js"></script>
42
+ <script src="https://unpkg.com/@dajiaoai/algeo-sdk@2.5.0/dist/algeo-sdk.umd.js"></script>
43
43
  <script src="https://unpkg.com/@dajiaoai/algeo-sdk@latest/dist/algeo-sdk.umd.js"></script>
44
44
  ```
45
45
 
46
46
  **jsDelivr**:
47
47
 
48
48
  ```html
49
- <script src="https://cdn.jsdelivr.net/npm/@dajiaoai/algeo-sdk@1.2.1/dist/algeo-sdk.umd.js"></script>
49
+ <script src="https://cdn.jsdelivr.net/npm/@dajiaoai/algeo-sdk@2.5.0/dist/algeo-sdk.umd.js"></script>
50
50
  <script src="https://cdn.jsdelivr.net/npm/@dajiaoai/algeo-sdk@latest/dist/algeo-sdk.umd.js"></script>
51
51
  ```
52
52
 
@@ -54,31 +54,29 @@ npm install @dajiaoai/algeo-sdk@1.2.1
54
54
 
55
55
  ## 使用方式
56
56
 
57
- ### 方式一:SDK(绑定 DOM)
57
+ ### 方式一:演示模式
58
58
 
59
59
  ```javascript
60
- import { AlgeoSdk } from '@dajiaoai/algeo-sdk';
60
+ import { createPresentation } from '@dajiaoai/algeo-sdk';
61
61
 
62
62
  const container = document.getElementById('algeo-container');
63
- const sdk = await AlgeoSdk.create(container, {
64
- baseUrl: 'https://dajiaoai.com',
65
- initialId: '33TA3484', // 可选,初始加载的分享 ID
63
+ const presentation = await createPresentation(container, {
64
+ appId: 'YTVJDQZR',
65
+ shareId: '33TA3484',
66
66
  });
67
67
 
68
- // 等待就绪(可选)
69
- window.addEventListener('message', (e) => {
70
- if (e.data?.type === 'ready') {
71
- console.log('内嵌页就绪', e.data.version);
72
- }
68
+ presentation.on('ready', (event) => {
69
+ console.log('演示模式已就绪', event.version);
73
70
  });
74
71
 
75
- // 调用方法
76
- sdk.loadShareById('33TA3484').then(() => console.log('加载成功'));
77
- sdk.getSlideCount().then(({ count }) => console.log('画板数量:', count));
78
- sdk.switchSlide(1).then(() => console.log('切换画板'));
72
+ presentation.loadShareById('33TA3484').then(() => console.log('加载成功'));
73
+ presentation
74
+ .getSlideCount()
75
+ .then(({ count }) => console.log('画板数量:', count));
76
+ presentation.switchSlide(1).then(() => console.log('切换画板'));
79
77
 
80
78
  // 销毁
81
- // sdk.destroy();
79
+ // presentation.destroy();
82
80
  ```
83
81
 
84
82
  ### 方式二:直接 iframe(无 SDK)
@@ -90,6 +88,7 @@ sdk.switchSlide(1).then(() => console.log('切换画板'));
90
88
  id="algeo-embed"
91
89
  src="https://dajiaoai.com/e/33TA3484"
92
90
  allow="fullscreen"
91
+ referrerpolicy="origin"
93
92
  ></iframe>
94
93
  ```
95
94
 
@@ -97,6 +96,81 @@ sdk.switchSlide(1).then(() => console.log('切换画板'));
97
96
 
98
97
  > **全屏支持**:建议在 iframe 上添加 `allow="fullscreen"`,以便内嵌画板使用全屏功能。SDK 方式创建的 iframe 已自动包含此属性。
99
98
 
99
+ > **Referrer 透传**:SDK 方式创建的 iframe 会自动设置 `referrerpolicy="origin"`,便于内嵌页稳定获取宿主域名信息;若使用直接 iframe 方式,建议手动补上该属性。
100
+
101
+ ### 方式三:编辑模式
102
+
103
+ ```javascript
104
+ import { createEditor } from '@dajiaoai/algeo-sdk';
105
+
106
+ const editor = await createEditor(document.getElementById('editor-root'), {
107
+ auth: {
108
+ appId: 'xxxx',
109
+ },
110
+ initialContent,
111
+ ui: {
112
+ navbar: true,
113
+ slidePanel: true,
114
+ toolboxPanel: true,
115
+ algebraPanel: false,
116
+ docPanel: false,
117
+ },
118
+ });
119
+
120
+ editor.on('ready', (event) => {
121
+ console.log('iframe 已完成初始化', event);
122
+ });
123
+
124
+ editor.on('contentChange', (event) => {
125
+ console.log('用户编辑后返回完整内容', event.content);
126
+ });
127
+
128
+ editor.on('slideChange', (event) => {
129
+ console.log('用户切换后的最新画板索引', event.index);
130
+ });
131
+
132
+ editor.on('save', async (event) => {
133
+ if (event.stage === 'request') {
134
+ const response = await fetch('/api/geometry-doc/save', {
135
+ method: 'POST',
136
+ headers: { 'Content-Type': 'application/json' },
137
+ body: JSON.stringify({
138
+ content: event.content,
139
+ }),
140
+ });
141
+
142
+ if (!response.ok) {
143
+ return {
144
+ status: 'error',
145
+ message: '保存失败',
146
+ };
147
+ }
148
+
149
+ return {
150
+ status: 'success',
151
+ };
152
+ }
153
+
154
+ console.log('用户点击保存按钮且宿主返回成功后的完整内容', event.content);
155
+ });
156
+ ```
157
+
158
+ 编辑器内嵌页点击保存按钮时,会触发 `editor.on('save', listener)` 的请求阶段监听器。只有宿主返回 `{ status: 'success' }` 后,iframe 才会展示成功态,并向外触发 `save` 的成功阶段事件。
159
+
160
+ 如需通过统一入口分发模式,也可以使用 `create`:
161
+
162
+ ```javascript
163
+ import { create } from '@dajiaoai/algeo-sdk';
164
+
165
+ const editor = await create(document.getElementById('editor-root'), {
166
+ mode: 'editor',
167
+ editor: {
168
+ auth: { appId: 'xxxx' },
169
+ initialContent,
170
+ },
171
+ });
172
+ ```
173
+
100
174
  ## API
101
175
 
102
176
  ### 常量
@@ -112,35 +186,107 @@ console.log('Algeo SDK version:', VERSION);
112
186
 
113
187
  ---
114
188
 
115
- ### 类 `AlgeoSdk`
189
+ ### 入口函数
190
+
191
+ #### `create(container, options): Promise<EmbeddedEditor | EmbeddedPresentation>`
192
+
193
+ 统一入口,按 `mode` 分发到具体实例:
194
+
195
+ - `mode: 'editor'` -> `EmbeddedEditor`
196
+ - `mode: 'presentation'` -> `EmbeddedPresentation`
197
+
198
+ #### `createEditor(container, options): Promise<EmbeddedEditor>`
199
+
200
+ 编辑模式快捷入口。该函数负责真正创建并初始化 `EmbeddedEditor`。
201
+
202
+ 该快捷入口不暴露 `baseUrl` 配置。如需自定义宿主地址,请使用统一入口 `create(container, { mode: 'editor', baseUrl, editor: options })`。
116
203
 
117
- #### `AlgeoSdk.create(container, options?): Promise<AlgeoSdk>`
204
+ ```javascript
205
+ create(container, {
206
+ mode: 'editor',
207
+ baseUrl: 'https://your-host.example',
208
+ editor: options,
209
+ });
210
+ ```
211
+
212
+ #### `createPresentation(container, options): Promise<EmbeddedPresentation>`
118
213
 
119
- 异步创建并初始化 SDK 实例。在 iframe 加载完成并收到 ready 消息后 resolve。
214
+ 演示模式快捷入口。该函数负责真正创建并初始化 `EmbeddedPresentation`。
120
215
 
121
- | 参数 | 类型 | 说明 |
122
- | ----------- | ----------------- | ----------------------- |
123
- | `container` | `HTMLElement` | 挂载 iframe 的 DOM 容器 |
124
- | `options` | `AlgeoSdkOptions` | 可选配置 |
216
+ 创建成功后,SDK 会额外调用 `https://open.dajiaoai.com/console/api/whitelist/check`,使用 `options.appId` 与当前页面 `hostname` 做白名单校验。若未命中白名单,iframe 仍会完成初始化并呈现页面,`createPresentation` 仍会成功返回实例,但后续 `loadShareById`、`loadFile`、`switchSlide`、`getSlideCount`、`repl` 等演示模式 API 会被拒绝,并在控制台输出包含方法名的错误,便于接入方感知并处理。
217
+
218
+ ```javascript
219
+ create(container, {
220
+ mode: 'presentation',
221
+ baseUrl: 'https://your-host.example',
222
+ presentation: options,
223
+ });
224
+ ```
225
+
226
+ #### `create(container, options)`
227
+
228
+ 异步创建并初始化具体实例。在 iframe 加载完成并收到 ready 消息后 resolve;若 30 秒内未收到 ready,则会以 `TIMEOUT` 错误 reject。
229
+
230
+ | 参数 | 类型 | 说明 |
231
+ | ----------- | -------------------- | ----------------------- |
232
+ | `container` | `HTMLElement` | 挂载 iframe 的 DOM 容器 |
233
+ | `options` | `AlgeoCreateOptions` | 创建配置 |
234
+
235
+ **AlgeoCreateOptions:**
236
+
237
+ ```typescript
238
+ type AlgeoCreateOptions =
239
+ | {
240
+ baseUrl?: string;
241
+ mode: 'editor';
242
+ editor: AlgeoEditorCreateOptions;
243
+ }
244
+ | {
245
+ baseUrl?: string;
246
+ mode: 'presentation';
247
+ presentation: AlgeoPresentationCreateOptions;
248
+ };
249
+ ```
125
250
 
126
- **AlgeoSdkOptions:**
251
+ **AlgeoEditorCreateOptions:**
127
252
 
128
- | 属性 | 类型 | 默认值 | 说明 |
129
- | ----------- | -------- | ---------------------- | ------------------------------------- |
130
- | `baseUrl` | `string` | `https://dajiaoai.com` | 内嵌页基础 URL |
131
- | `initialId` | `string` | `''` | 初始加载的分享 ID,为空则加载空白画板 |
253
+ | 属性 | 类型 | 默认值 | 说明 |
254
+ | ---------------- | ------------------------------ | ------ | ------------------------------------------------------ |
255
+ | `auth` | `{ appId: string }` | - | 编辑模式鉴权参数,其中 `appId` 会参与默认路由生成 |
256
+ | `shareId` | `string` | `''` | 编辑模式初始分享 ID,会映射到 `/embed/edit/:appId/:id` |
257
+ | `initialContent` | `FileContentV10` | - | 初始化后自动注入的文件内容 |
258
+ | `ui` | `Partial<AlgeoEditorUiConfig>` | - | 编辑器 UI 开关配置 |
259
+
260
+ **AlgeoPresentationCreateOptions:**
261
+
262
+ | 属性 | 类型 | 默认值 | 说明 |
263
+ | --------- | -------- | ------ | -------------------------------------------------------- |
264
+ | `appId` | `string` | `''` | 演示模式白名单校验使用的应用标识,SDK 会用它请求校验接口 |
265
+ | `shareId` | `string` | `''` | 演示模式初始分享 ID,会映射到 `/e/:id` |
266
+
267
+ 默认路径规则:
268
+
269
+ - `mode: 'presentation'` -> `/e`,若传 `presentation.shareId` 则生成 `/e/:id`
270
+ - `mode: 'editor'` -> `/embed/edit/:appId`,其中 `:appId` 来自 `editor.auth.appId`;若传 `editor.shareId` 则生成 `/embed/edit/:appId/:id`
132
271
 
133
272
  **示例:**
134
273
 
135
274
  ```javascript
136
- const sdk = await AlgeoSdk.create(container, {
137
- baseUrl: 'https://dajiaoai.com',
138
- initialId: '33TA3484',
275
+ const presentation = await create(container, {
276
+ mode: 'presentation',
277
+ presentation: {
278
+ appId: 'xxxx',
279
+ shareId: '33TA3484',
280
+ },
139
281
  });
140
282
  ```
141
283
 
142
284
  ---
143
285
 
286
+ ### 类 `EmbeddedPresentation`
287
+
288
+ 演示模式实例,保留当前已上线的 iframe 能力。
289
+
144
290
  #### 实例属性(只读)
145
291
 
146
292
  | 属性 | 类型 | 说明 |
@@ -148,11 +294,21 @@ const sdk = await AlgeoSdk.create(container, {
148
294
  | `ready` | `boolean` | 是否已就绪(收到 iframe ready 通知) |
149
295
  | `version` | `string \| null` | 内嵌页协议版本 |
150
296
 
297
+ #### 事件订阅
298
+
299
+ ##### `presentation.on(event, listener): () => void`
300
+
301
+ 当前稳定提供以下事件:
302
+
303
+ | 事件名 | 说明 |
304
+ | ------- | ----------------- |
305
+ | `ready` | iframe 完成初始化 |
306
+
151
307
  ---
152
308
 
153
309
  #### 实例方法
154
310
 
155
- ##### `sdk.loadShareById(id: string): Promise<LoadShareByIdResult>`
311
+ ##### `presentation.loadShareById(id: string): Promise<LoadShareByIdResult>`
156
312
 
157
313
  按分享 ID 加载内容。
158
314
 
@@ -164,7 +320,7 @@ const sdk = await AlgeoSdk.create(container, {
164
320
 
165
321
  ---
166
322
 
167
- ##### `sdk.loadFile(content: FileContent): Promise<LoadFileResult>`
323
+ ##### `presentation.loadFile(content: FileContent): Promise<LoadFileResult>`
168
324
 
169
325
  加载完整文件数据(覆盖式),需符合 FileContentV10 格式。
170
326
 
@@ -189,7 +345,7 @@ interface FileContent {
189
345
 
190
346
  ---
191
347
 
192
- ##### `sdk.getSlideCount(): Promise<GetSlideCountResult>`
348
+ ##### `presentation.getSlideCount(): Promise<GetSlideCountResult>`
193
349
 
194
350
  查询当前画板数量。
195
351
 
@@ -197,7 +353,7 @@ interface FileContent {
197
353
 
198
354
  ---
199
355
 
200
- ##### `sdk.switchSlide(index: number): Promise<SwitchSlideResult>`
356
+ ##### `presentation.switchSlide(index: number): Promise<SwitchSlideResult>`
201
357
 
202
358
  切换到指定索引的画板(索引从 0 开始)。
203
359
 
@@ -209,7 +365,7 @@ interface FileContent {
209
365
 
210
366
  ---
211
367
 
212
- ##### `sdk.repl(command: string): Promise<ReplResult>`
368
+ ##### `presentation.repl(command: string): Promise<ReplResult>`
213
369
 
214
370
  执行 REPL 指令,返回面向 AI 的文档/文本内容。需先通过 `loadShareById` 或 `loadFile` 加载内容后,部分指令才有数据可查。
215
371
 
@@ -230,7 +386,78 @@ interface FileContent {
230
386
 
231
387
  ---
232
388
 
233
- ##### `sdk.destroy(): void`
389
+ ### 类 `EmbeddedEditor`
390
+
391
+ 编辑模式实例不再暴露一组扁平方法,而是拆成 capability units。
392
+
393
+ #### 实例属性(只读)
394
+
395
+ | 属性 | 类型 | 说明 |
396
+ | ---------- | ------------- | ------------------------------------ |
397
+ | `ready` | `boolean` | 是否已就绪(收到 iframe ready 通知) |
398
+ | `version` | `string` | 内嵌页协议版本 |
399
+ | `document` | `DocumentApi` | 画板文件相关能力 |
400
+ | `slides` | `SlidesApi` | 多画板管理能力 |
401
+ | `history` | `HistoryApi` | 历史记录能力 |
402
+ | `mode` | `ModeApi` | 编辑器展示模式控制 |
403
+
404
+ #### `editor.document`
405
+
406
+ | 方法 | 说明 |
407
+ | ---------------------- | -------------------------------- |
408
+ | `loadContent(content)` | 加载完整画板文件内容 |
409
+ | `getContent()` | 异步获取 iframe 当前实时画板内容 |
410
+
411
+ #### `editor.slides`
412
+
413
+ | 方法 | 说明 |
414
+ | -------------------------------- | -------------------- |
415
+ | `getCount()` | 获取当前已知画板数量 |
416
+ | `getCurrentIndex()` | 获取当前已知画板索引 |
417
+ | `switchTo(index)` | 切换画板 |
418
+ | `add()` | 在末尾新增画板 |
419
+ | `addAt(index)` | 在指定位置新增画板 |
420
+ | `remove(index)` | 删除指定画板 |
421
+ | `duplicate(index, targetIndex?)` | 复制画板 |
422
+ | `reorder(fromIndex, toIndex)` | 重排画板 |
423
+
424
+ #### `editor.history`
425
+
426
+ | 方法 | 说明 |
427
+ | ------------------- | -------------------- |
428
+ | `getCount()` | 获取当前已知历史步数 |
429
+ | `getCurrentIndex()` | 获取当前已知历史游标 |
430
+ | `undo()` | 撤销 |
431
+ | `redo()` | 重做 |
432
+ | `jumpTo(index)` | 跳转到指定历史位置 |
433
+ | `canUndo()` | 判断是否可撤销 |
434
+ | `canRedo()` | 判断是否可重做 |
435
+ | `clear()` | 清空历史 |
436
+
437
+ #### `editor.mode`
438
+
439
+ | 方法 | 说明 |
440
+ | --------------------- | ---------------- |
441
+ | `getUiConfig()` | 获取当前 UI 配置 |
442
+ | `setUiConfig(config)` | 更新 UI 配置 |
443
+
444
+ #### 编辑器事件
445
+
446
+ `editor.on(event, listener)` 当前支持以下事件:
447
+
448
+ | 事件名 | 事件数据 | 说明 |
449
+ | --------------- | ---------------------------------------------------------- | -------------------------------------------------------------------------------- |
450
+ | `ready` | `{ type: 'ready', mode, version }` | iframe 初始化完成 |
451
+ | `contentChange` | `{ type: 'contentChange', source: 'user', content }` | 用户在 iframe 内编辑后回传完整 `FileContentV10` |
452
+ | `slideChange` | `{ type: 'slideChange', index }` | 用户在 iframe 内切换画板后回传当前索引 |
453
+ | `save` | `{ type: 'save', stage: 'request' \| 'success', content }` | `stage: 'request'` 时用于宿主处理保存,`stage: 'success'` 时表示保存成功后的通知 |
454
+
455
+ #### 事件与销毁
456
+
457
+ - `editor.on(event, listener)` / `editor.off(event, listener)`
458
+ - `editor.destroy(): Promise<void>`
459
+
460
+ ##### `presentation.destroy(): Promise<void>`
234
461
 
235
462
  销毁实例,移除 iframe 与事件监听,并拒绝所有未完成的 pending 请求。
236
463
 
@@ -252,9 +479,10 @@ try {
252
479
 
253
480
  **常见错误码:**
254
481
 
255
- | 错误码 | 说明 |
256
- | ------------------ | --------------------------- |
257
- | `IFRAME_NOT_READY` | iframe 未加载完成即调用方法 |
258
- | `TIMEOUT` | 请求超时(30 秒) |
259
- | `DESTROYED` | SDK 已销毁 |
260
- | `BAD_REQUEST` | 非法请求(如重复 init) |
482
+ | 错误码 | 说明 |
483
+ | ------------------ | ------------------------------------------------- |
484
+ | `IFRAME_NOT_READY` | iframe 未加载完成即调用方法 |
485
+ | `TIMEOUT` | 请求或初始化超时(30 秒) |
486
+ | `DESTROYED` | SDK 已销毁 |
487
+ | `MISSING_APP_ID` | 编辑模式缺少 `auth.appId` |
488
+ | `BAD_REQUEST` | 非法请求(如重复 init、演示模式白名单校验未通过) |