@dajiaoai/algeo-sdk 2.9.2 → 2.11.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 +54 -6
- package/dist/algeo-sdk.cjs.js +177 -10
- package/dist/algeo-sdk.esm.js +177 -10
- package/dist/algeo-sdk.umd.js +177 -10
- package/dist/embedded-editor.d.ts +6 -0
- package/dist/index.d.ts +1 -1
- package/dist/shared.d.ts +68 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -192,6 +192,38 @@ const editor = await create(document.getElementById('editor-root'), {
|
|
|
192
192
|
});
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
+
### 接入自有资源库
|
|
196
|
+
|
|
197
|
+
编辑模式支持由宿主提供只读图片资源库。图片仍由接入方存储和鉴权,SDK 只在内嵌页需要数据时调用 `query`:
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
const editor = await createEditor(document.getElementById('editor-root'), {
|
|
201
|
+
auth: { appId: 'xxxx' },
|
|
202
|
+
resourceLibrary: {
|
|
203
|
+
query: async (params, { signal }) => {
|
|
204
|
+
const response = await fetch(
|
|
205
|
+
`/api/material/list?page=${params.page}&pageSize=${params.pageSize}`,
|
|
206
|
+
{ signal },
|
|
207
|
+
);
|
|
208
|
+
if (!response.ok) throw new Error('资源库请求失败');
|
|
209
|
+
|
|
210
|
+
const data = await response.json();
|
|
211
|
+
return {
|
|
212
|
+
items: data.items,
|
|
213
|
+
pageInfo: {
|
|
214
|
+
page: params.page,
|
|
215
|
+
pageSize: params.pageSize,
|
|
216
|
+
hasNext: data.hasNext,
|
|
217
|
+
total: data.total,
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
`query` 返回的资源必须具有非空且不重复的 `id`、非空 `name`、`image/*` MIME 类型和完整 HTTP(S) URL。`width`、`height`、`size` 如提供,必须是正整数。搜索条件变化、面板关闭或 SDK 销毁时,请求可能通过 `signal` 被取消。
|
|
226
|
+
|
|
195
227
|
## API
|
|
196
228
|
|
|
197
229
|
### 常量
|
|
@@ -292,11 +324,20 @@ type AlgeoCreateOptions =
|
|
|
292
324
|
|
|
293
325
|
**AlgeoPresentationCreateOptions:**
|
|
294
326
|
|
|
295
|
-
| 属性 | 类型 | 默认值 | 说明
|
|
296
|
-
| --------- | ------------------------------------ | ------ |
|
|
297
|
-
| `auth` | `{ appId: string }` | - | 演示模式鉴权参数,`appId` 用于路由及白名单校验
|
|
327
|
+
| 属性 | 类型 | 默认值 | 说明 |
|
|
328
|
+
| --------- | ------------------------------------ | ------ | --------------------------------------------------------- |
|
|
329
|
+
| `auth` | `{ appId: string }` | - | 演示模式鉴权参数,`appId` 用于路由及白名单校验 |
|
|
298
330
|
| `shareId` | `string` | `''` | 演示模式初始分享 ID,会映射到 `/embed/present/:appId/:id` |
|
|
299
|
-
| `ui` | `Partial<AlgeoPresentationUiConfig>` | - | 演示模式 UI 配置
|
|
331
|
+
| `ui` | `Partial<AlgeoPresentationUiConfig>` | - | 演示模式 UI 配置 |
|
|
332
|
+
|
|
333
|
+
**AlgeoPresentationUiConfig:**
|
|
334
|
+
|
|
335
|
+
| 属性 | 类型 | 默认值 | 说明 |
|
|
336
|
+
| --------------- | --------- | ------ | ------------------ |
|
|
337
|
+
| `logo` | `boolean` | `true` | 是否展示 logo |
|
|
338
|
+
| `slidePanel` | `boolean` | `true` | 是否展示画板管理器 |
|
|
339
|
+
| `pencilToolbar` | `boolean` | `true` | 是否展示教具栏 |
|
|
340
|
+
| `zoomControl` | `boolean` | `true` | 是否展示缩放栏 |
|
|
300
341
|
|
|
301
342
|
默认路径规则:
|
|
302
343
|
|
|
@@ -488,12 +529,12 @@ const images = await editor.slides.exportImage({
|
|
|
488
529
|
mode: 'view',
|
|
489
530
|
slideIndices: [1],
|
|
490
531
|
format: 'png',
|
|
491
|
-
viewBounds: { x: -5, y: -5, width: 10, height: 10 },
|
|
532
|
+
viewBounds: { x: -5, y: -5, width: 10, height: 10, scale: 50 },
|
|
492
533
|
pixelRatio: 2,
|
|
493
534
|
});
|
|
494
535
|
```
|
|
495
536
|
|
|
496
|
-
`viewBounds`
|
|
537
|
+
`viewBounds` 的位置和宽高均为世界坐标,`scale` 为可选的相机缩放比例。省略 `scale` 时使用目标画板文件中的 `camera.scale`。
|
|
497
538
|
|
|
498
539
|
适合完整包住全部内容且不限定最终宽高时使用 `contain`:
|
|
499
540
|
|
|
@@ -556,9 +597,16 @@ const items = await editor.slides.exportLatex({
|
|
|
556
597
|
interface ExportedLatex {
|
|
557
598
|
index: number; // 1-based 画板索引
|
|
558
599
|
code: string;
|
|
600
|
+
assets: Array<{ path: string; blob: Blob; mediaType: string }>;
|
|
601
|
+
blob: Blob; // 有图片时为 ZIP,否则为 TeX
|
|
602
|
+
fileName: string;
|
|
603
|
+
format: 'tex' | 'zip';
|
|
559
604
|
}
|
|
560
605
|
```
|
|
561
606
|
|
|
607
|
+
`blob` 可直接下载;当画板包含图片时,ZIP 内同时包含 `slide-N.tex` 和
|
|
608
|
+
TikZ 通过相对路径引用的 `images/*` 文件。
|
|
609
|
+
|
|
562
610
|
#### `editor.history`
|
|
563
611
|
|
|
564
612
|
| 方法 | 说明 |
|
package/dist/algeo-sdk.cjs.js
CHANGED
|
@@ -3865,6 +3865,12 @@ var AlgeoError = class extends Error {
|
|
|
3865
3865
|
this.name = "AlgeoError";
|
|
3866
3866
|
}
|
|
3867
3867
|
};
|
|
3868
|
+
objectType({
|
|
3869
|
+
text: stringType().optional(),
|
|
3870
|
+
images: arrayType(stringType().trim().min(1)).optional(),
|
|
3871
|
+
openPanel: booleanType().optional(),
|
|
3872
|
+
focus: booleanType().optional()
|
|
3873
|
+
}).passthrough();
|
|
3868
3874
|
var openAiChatMessageV1Schema = objectType({
|
|
3869
3875
|
role: stringType(),
|
|
3870
3876
|
content: unionType([
|
|
@@ -3878,9 +3884,23 @@ var openAiChatMessageV1Schema = objectType({
|
|
|
3878
3884
|
name: stringType().optional()
|
|
3879
3885
|
}).passthrough();
|
|
3880
3886
|
objectType({
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
|
|
3887
|
+
model: stringType().trim().min(1),
|
|
3888
|
+
input: unionType([
|
|
3889
|
+
stringType(),
|
|
3890
|
+
arrayType(unionType([openAiChatMessageV1Schema, recordType(unknownType())]))
|
|
3891
|
+
]).optional(),
|
|
3892
|
+
previous_response_id: stringType().trim().min(1).optional(),
|
|
3893
|
+
instructions: stringType().optional(),
|
|
3894
|
+
temperature: numberType().min(0).max(2).optional(),
|
|
3895
|
+
max_output_tokens: numberType().int().positive().optional(),
|
|
3896
|
+
metadata: recordType(unknownType()).optional()
|
|
3897
|
+
}).passthrough().superRefine((value, ctx) => {
|
|
3898
|
+
if (!("input" in value) && !("previous_response_id" in value)) {
|
|
3899
|
+
ctx.addIssue({
|
|
3900
|
+
code: ZodIssueCode.custom,
|
|
3901
|
+
message: "input or previous_response_id is required."
|
|
3902
|
+
});
|
|
3903
|
+
}
|
|
3884
3904
|
});
|
|
3885
3905
|
objectType({
|
|
3886
3906
|
id: stringType(),
|
|
@@ -4011,7 +4031,7 @@ function convertToLatest(content) {
|
|
|
4011
4031
|
}
|
|
4012
4032
|
|
|
4013
4033
|
/** SDK 版本号,构建时由 rollup 注入 */
|
|
4014
|
-
const VERSION = '2.
|
|
4034
|
+
const VERSION = '2.11.0';
|
|
4015
4035
|
const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
|
|
4016
4036
|
const DEFAULT_PRESENTATION_PATH = '/embed/present';
|
|
4017
4037
|
const DEFAULT_EDITOR_PATH = '/embed/edit';
|
|
@@ -4050,6 +4070,21 @@ function isAiRequestMessage(msg) {
|
|
|
4050
4070
|
typeof msg.payload === 'object' &&
|
|
4051
4071
|
msg.payload !== null);
|
|
4052
4072
|
}
|
|
4073
|
+
function isResourceLibraryRequestMessage(msg) {
|
|
4074
|
+
if (typeof msg !== 'object' || msg === null) {
|
|
4075
|
+
return false;
|
|
4076
|
+
}
|
|
4077
|
+
const value = msg;
|
|
4078
|
+
if (typeof value.requestId !== 'string') {
|
|
4079
|
+
return false;
|
|
4080
|
+
}
|
|
4081
|
+
if (value.type === 'resourceLibraryCancel') {
|
|
4082
|
+
return true;
|
|
4083
|
+
}
|
|
4084
|
+
return (value.type === 'resourceLibraryQuery' &&
|
|
4085
|
+
typeof value.params === 'object' &&
|
|
4086
|
+
value.params !== null);
|
|
4087
|
+
}
|
|
4053
4088
|
function isEmbedEventMessage(msg) {
|
|
4054
4089
|
if (typeof msg !== 'object' || msg == null || !('type' in msg)) {
|
|
4055
4090
|
return false;
|
|
@@ -4256,7 +4291,9 @@ class EmbeddedTarget {
|
|
|
4256
4291
|
pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
|
|
4257
4292
|
return;
|
|
4258
4293
|
}
|
|
4259
|
-
if ((isSaveRequestMessage(data) ||
|
|
4294
|
+
if ((isSaveRequestMessage(data) ||
|
|
4295
|
+
isAiRequestMessage(data) ||
|
|
4296
|
+
isResourceLibraryRequestMessage(data)) &&
|
|
4260
4297
|
this.handleRequestMessage(data, iframe.contentWindow)) {
|
|
4261
4298
|
return;
|
|
4262
4299
|
}
|
|
@@ -4356,7 +4393,12 @@ class EmbeddedPresentation extends EmbeddedTarget {
|
|
|
4356
4393
|
super(container, 'presentation');
|
|
4357
4394
|
this.currentSlideIndex = 0;
|
|
4358
4395
|
this.slideCount = 0;
|
|
4359
|
-
this.uiConfig = {
|
|
4396
|
+
this.uiConfig = {
|
|
4397
|
+
logo: true,
|
|
4398
|
+
slidePanel: true,
|
|
4399
|
+
pencilToolbar: true,
|
|
4400
|
+
zoomControl: true,
|
|
4401
|
+
};
|
|
4360
4402
|
this.mode = {
|
|
4361
4403
|
getUiConfig: () => ({ ...this.uiConfig }),
|
|
4362
4404
|
setUiConfig: async (config) => {
|
|
@@ -4373,15 +4415,19 @@ class EmbeddedPresentation extends EmbeddedTarget {
|
|
|
4373
4415
|
};
|
|
4374
4416
|
}
|
|
4375
4417
|
async initialize(options = {}, baseUrl) {
|
|
4376
|
-
this.uiConfig =
|
|
4418
|
+
this.uiConfig = {
|
|
4419
|
+
logo: true,
|
|
4420
|
+
slidePanel: true,
|
|
4421
|
+
pencilToolbar: true,
|
|
4422
|
+
zoomControl: true,
|
|
4423
|
+
...options.ui,
|
|
4424
|
+
};
|
|
4377
4425
|
await this.init({
|
|
4378
4426
|
baseUrl,
|
|
4379
4427
|
auth: options.auth,
|
|
4380
4428
|
initialId: options.shareId,
|
|
4381
4429
|
});
|
|
4382
|
-
|
|
4383
|
-
await this.mode.setUiConfig(this.uiConfig);
|
|
4384
|
-
}
|
|
4430
|
+
await this.mode.setUiConfig(this.uiConfig);
|
|
4385
4431
|
}
|
|
4386
4432
|
acceptsEventMessage() {
|
|
4387
4433
|
return false;
|
|
@@ -4436,6 +4482,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
|
|
|
4436
4482
|
class EmbeddedEditor extends EmbeddedTarget {
|
|
4437
4483
|
constructor(container) {
|
|
4438
4484
|
super(container, 'editor');
|
|
4485
|
+
this.resourceLibraryRequests = new Map();
|
|
4439
4486
|
this.currentSlideIndex = 0;
|
|
4440
4487
|
this.slideCount = 0;
|
|
4441
4488
|
this.historyCount = 0;
|
|
@@ -4540,11 +4587,15 @@ class EmbeddedEditor extends EmbeddedTarget {
|
|
|
4540
4587
|
throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
|
|
4541
4588
|
}
|
|
4542
4589
|
this.uiConfig = options.ui ? { ...options.ui } : {};
|
|
4590
|
+
this.resourceLibrary = options.resourceLibrary;
|
|
4543
4591
|
await this.init({
|
|
4544
4592
|
baseUrl,
|
|
4545
4593
|
auth: options.auth,
|
|
4546
4594
|
initialId: options.shareId,
|
|
4547
4595
|
});
|
|
4596
|
+
await this.post('setResourceLibraryAvailability', {
|
|
4597
|
+
available: Boolean(this.resourceLibrary),
|
|
4598
|
+
});
|
|
4548
4599
|
if (Object.keys(this.uiConfig).length > 0) {
|
|
4549
4600
|
await this.mode.setUiConfig(this.uiConfig);
|
|
4550
4601
|
}
|
|
@@ -4580,6 +4631,15 @@ class EmbeddedEditor extends EmbeddedTarget {
|
|
|
4580
4631
|
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
|
|
4581
4632
|
}
|
|
4582
4633
|
handleRequestMessage(message, sourceWindow) {
|
|
4634
|
+
if (message.type === 'resourceLibraryCancel') {
|
|
4635
|
+
this.resourceLibraryRequests.get(message.requestId)?.abort();
|
|
4636
|
+
this.resourceLibraryRequests.delete(message.requestId);
|
|
4637
|
+
return true;
|
|
4638
|
+
}
|
|
4639
|
+
if (message.type === 'resourceLibraryQuery') {
|
|
4640
|
+
void this.handleResourceLibraryQuery(message, sourceWindow);
|
|
4641
|
+
return true;
|
|
4642
|
+
}
|
|
4583
4643
|
if (message.type === 'save') {
|
|
4584
4644
|
void this.handleSaveRequest(message, sourceWindow);
|
|
4585
4645
|
return true;
|
|
@@ -4591,9 +4651,116 @@ class EmbeddedEditor extends EmbeddedTarget {
|
|
|
4591
4651
|
return false;
|
|
4592
4652
|
}
|
|
4593
4653
|
async destroy() {
|
|
4654
|
+
this.resourceLibraryRequests.forEach((controller) => controller.abort());
|
|
4655
|
+
this.resourceLibraryRequests.clear();
|
|
4594
4656
|
this.cancelActiveAi('destroyed', true);
|
|
4595
4657
|
await super.destroy();
|
|
4596
4658
|
}
|
|
4659
|
+
async handleResourceLibraryQuery(message, sourceWindow) {
|
|
4660
|
+
const controller = new AbortController();
|
|
4661
|
+
this.resourceLibraryRequests.set(message.requestId, controller);
|
|
4662
|
+
const respond = (payload) => {
|
|
4663
|
+
if (!controller.signal.aborted) {
|
|
4664
|
+
sourceWindow.postMessage(payload, '*');
|
|
4665
|
+
}
|
|
4666
|
+
};
|
|
4667
|
+
try {
|
|
4668
|
+
if (!this.resourceLibrary) {
|
|
4669
|
+
throw new AlgeoError('宿主未配置 resourceLibrary.query', EMBED_ERROR_CODES.BAD_REQUEST);
|
|
4670
|
+
}
|
|
4671
|
+
this.validateResourceLibraryQuery(message.params);
|
|
4672
|
+
const result = await this.resourceLibrary.query(message.params, {
|
|
4673
|
+
signal: controller.signal,
|
|
4674
|
+
});
|
|
4675
|
+
if (controller.signal.aborted)
|
|
4676
|
+
return;
|
|
4677
|
+
this.validateResourceLibraryResult(result, message.params);
|
|
4678
|
+
respond({
|
|
4679
|
+
type: 'response',
|
|
4680
|
+
requestId: message.requestId,
|
|
4681
|
+
success: true,
|
|
4682
|
+
result,
|
|
4683
|
+
});
|
|
4684
|
+
}
|
|
4685
|
+
catch (error) {
|
|
4686
|
+
if (controller.signal.aborted)
|
|
4687
|
+
return;
|
|
4688
|
+
respond({
|
|
4689
|
+
type: 'response',
|
|
4690
|
+
requestId: message.requestId,
|
|
4691
|
+
success: false,
|
|
4692
|
+
error: {
|
|
4693
|
+
code: error instanceof AlgeoError
|
|
4694
|
+
? error.code
|
|
4695
|
+
: EMBED_ERROR_CODES.UNKNOWN_ERROR,
|
|
4696
|
+
message: error instanceof Error ? error.message : String(error),
|
|
4697
|
+
},
|
|
4698
|
+
});
|
|
4699
|
+
}
|
|
4700
|
+
finally {
|
|
4701
|
+
if (this.resourceLibraryRequests.get(message.requestId) === controller) {
|
|
4702
|
+
this.resourceLibraryRequests.delete(message.requestId);
|
|
4703
|
+
}
|
|
4704
|
+
}
|
|
4705
|
+
}
|
|
4706
|
+
validateResourceLibraryQuery(params) {
|
|
4707
|
+
if (!Number.isInteger(params.page) ||
|
|
4708
|
+
params.page < 1 ||
|
|
4709
|
+
!Number.isInteger(params.pageSize) ||
|
|
4710
|
+
params.pageSize < 1 ||
|
|
4711
|
+
params.pageSize > 100 ||
|
|
4712
|
+
(params.keyword !== undefined && typeof params.keyword !== 'string') ||
|
|
4713
|
+
(params.mediaTypes !== undefined &&
|
|
4714
|
+
(!Array.isArray(params.mediaTypes) ||
|
|
4715
|
+
params.mediaTypes.some((value) => typeof value !== 'string')))) {
|
|
4716
|
+
throw new AlgeoError('资源库查询参数无效', EMBED_ERROR_CODES.BAD_REQUEST);
|
|
4717
|
+
}
|
|
4718
|
+
}
|
|
4719
|
+
validateResourceLibraryResult(result, params) {
|
|
4720
|
+
const invalid = () => {
|
|
4721
|
+
throw new AlgeoError('资源数据格式无效', EMBED_ERROR_CODES.BAD_REQUEST);
|
|
4722
|
+
};
|
|
4723
|
+
if (!result || !Array.isArray(result.items) || !result.pageInfo)
|
|
4724
|
+
invalid();
|
|
4725
|
+
const pageInfo = result.pageInfo;
|
|
4726
|
+
if (pageInfo.page !== params.page ||
|
|
4727
|
+
pageInfo.pageSize !== params.pageSize ||
|
|
4728
|
+
typeof pageInfo.hasNext !== 'boolean' ||
|
|
4729
|
+
(pageInfo.total !== undefined &&
|
|
4730
|
+
(!Number.isInteger(pageInfo.total) || pageInfo.total < 0)))
|
|
4731
|
+
invalid();
|
|
4732
|
+
const ids = new Set();
|
|
4733
|
+
for (const item of result.items) {
|
|
4734
|
+
if (!item ||
|
|
4735
|
+
typeof item.id !== 'string' ||
|
|
4736
|
+
!item.id.trim() ||
|
|
4737
|
+
ids.has(item.id) ||
|
|
4738
|
+
typeof item.name !== 'string' ||
|
|
4739
|
+
!item.name.trim() ||
|
|
4740
|
+
typeof item.mediaType !== 'string' ||
|
|
4741
|
+
!item.mediaType.startsWith('image/') ||
|
|
4742
|
+
!this.isHttpUrl(item.url) ||
|
|
4743
|
+
(item.thumbnailUrl !== undefined && !this.isHttpUrl(item.thumbnailUrl)))
|
|
4744
|
+
invalid();
|
|
4745
|
+
ids.add(item.id);
|
|
4746
|
+
for (const value of [item.width, item.height, item.size]) {
|
|
4747
|
+
if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
|
|
4748
|
+
invalid();
|
|
4749
|
+
}
|
|
4750
|
+
}
|
|
4751
|
+
}
|
|
4752
|
+
}
|
|
4753
|
+
isHttpUrl(value) {
|
|
4754
|
+
if (typeof value !== 'string')
|
|
4755
|
+
return false;
|
|
4756
|
+
try {
|
|
4757
|
+
const url = new URL(value);
|
|
4758
|
+
return url.protocol === 'http:' || url.protocol === 'https:';
|
|
4759
|
+
}
|
|
4760
|
+
catch {
|
|
4761
|
+
return false;
|
|
4762
|
+
}
|
|
4763
|
+
}
|
|
4597
4764
|
async loadContent(content, source) {
|
|
4598
4765
|
await this.post('loadContent', { content });
|
|
4599
4766
|
this.currentContent = content;
|
package/dist/algeo-sdk.esm.js
CHANGED
|
@@ -3863,6 +3863,12 @@ var AlgeoError = class extends Error {
|
|
|
3863
3863
|
this.name = "AlgeoError";
|
|
3864
3864
|
}
|
|
3865
3865
|
};
|
|
3866
|
+
objectType({
|
|
3867
|
+
text: stringType().optional(),
|
|
3868
|
+
images: arrayType(stringType().trim().min(1)).optional(),
|
|
3869
|
+
openPanel: booleanType().optional(),
|
|
3870
|
+
focus: booleanType().optional()
|
|
3871
|
+
}).passthrough();
|
|
3866
3872
|
var openAiChatMessageV1Schema = objectType({
|
|
3867
3873
|
role: stringType(),
|
|
3868
3874
|
content: unionType([
|
|
@@ -3876,9 +3882,23 @@ var openAiChatMessageV1Schema = objectType({
|
|
|
3876
3882
|
name: stringType().optional()
|
|
3877
3883
|
}).passthrough();
|
|
3878
3884
|
objectType({
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3885
|
+
model: stringType().trim().min(1),
|
|
3886
|
+
input: unionType([
|
|
3887
|
+
stringType(),
|
|
3888
|
+
arrayType(unionType([openAiChatMessageV1Schema, recordType(unknownType())]))
|
|
3889
|
+
]).optional(),
|
|
3890
|
+
previous_response_id: stringType().trim().min(1).optional(),
|
|
3891
|
+
instructions: stringType().optional(),
|
|
3892
|
+
temperature: numberType().min(0).max(2).optional(),
|
|
3893
|
+
max_output_tokens: numberType().int().positive().optional(),
|
|
3894
|
+
metadata: recordType(unknownType()).optional()
|
|
3895
|
+
}).passthrough().superRefine((value, ctx) => {
|
|
3896
|
+
if (!("input" in value) && !("previous_response_id" in value)) {
|
|
3897
|
+
ctx.addIssue({
|
|
3898
|
+
code: ZodIssueCode.custom,
|
|
3899
|
+
message: "input or previous_response_id is required."
|
|
3900
|
+
});
|
|
3901
|
+
}
|
|
3882
3902
|
});
|
|
3883
3903
|
objectType({
|
|
3884
3904
|
id: stringType(),
|
|
@@ -4009,7 +4029,7 @@ function convertToLatest(content) {
|
|
|
4009
4029
|
}
|
|
4010
4030
|
|
|
4011
4031
|
/** SDK 版本号,构建时由 rollup 注入 */
|
|
4012
|
-
const VERSION = '2.
|
|
4032
|
+
const VERSION = '2.11.0';
|
|
4013
4033
|
const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
|
|
4014
4034
|
const DEFAULT_PRESENTATION_PATH = '/embed/present';
|
|
4015
4035
|
const DEFAULT_EDITOR_PATH = '/embed/edit';
|
|
@@ -4048,6 +4068,21 @@ function isAiRequestMessage(msg) {
|
|
|
4048
4068
|
typeof msg.payload === 'object' &&
|
|
4049
4069
|
msg.payload !== null);
|
|
4050
4070
|
}
|
|
4071
|
+
function isResourceLibraryRequestMessage(msg) {
|
|
4072
|
+
if (typeof msg !== 'object' || msg === null) {
|
|
4073
|
+
return false;
|
|
4074
|
+
}
|
|
4075
|
+
const value = msg;
|
|
4076
|
+
if (typeof value.requestId !== 'string') {
|
|
4077
|
+
return false;
|
|
4078
|
+
}
|
|
4079
|
+
if (value.type === 'resourceLibraryCancel') {
|
|
4080
|
+
return true;
|
|
4081
|
+
}
|
|
4082
|
+
return (value.type === 'resourceLibraryQuery' &&
|
|
4083
|
+
typeof value.params === 'object' &&
|
|
4084
|
+
value.params !== null);
|
|
4085
|
+
}
|
|
4051
4086
|
function isEmbedEventMessage(msg) {
|
|
4052
4087
|
if (typeof msg !== 'object' || msg == null || !('type' in msg)) {
|
|
4053
4088
|
return false;
|
|
@@ -4254,7 +4289,9 @@ class EmbeddedTarget {
|
|
|
4254
4289
|
pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
|
|
4255
4290
|
return;
|
|
4256
4291
|
}
|
|
4257
|
-
if ((isSaveRequestMessage(data) ||
|
|
4292
|
+
if ((isSaveRequestMessage(data) ||
|
|
4293
|
+
isAiRequestMessage(data) ||
|
|
4294
|
+
isResourceLibraryRequestMessage(data)) &&
|
|
4258
4295
|
this.handleRequestMessage(data, iframe.contentWindow)) {
|
|
4259
4296
|
return;
|
|
4260
4297
|
}
|
|
@@ -4354,7 +4391,12 @@ class EmbeddedPresentation extends EmbeddedTarget {
|
|
|
4354
4391
|
super(container, 'presentation');
|
|
4355
4392
|
this.currentSlideIndex = 0;
|
|
4356
4393
|
this.slideCount = 0;
|
|
4357
|
-
this.uiConfig = {
|
|
4394
|
+
this.uiConfig = {
|
|
4395
|
+
logo: true,
|
|
4396
|
+
slidePanel: true,
|
|
4397
|
+
pencilToolbar: true,
|
|
4398
|
+
zoomControl: true,
|
|
4399
|
+
};
|
|
4358
4400
|
this.mode = {
|
|
4359
4401
|
getUiConfig: () => ({ ...this.uiConfig }),
|
|
4360
4402
|
setUiConfig: async (config) => {
|
|
@@ -4371,15 +4413,19 @@ class EmbeddedPresentation extends EmbeddedTarget {
|
|
|
4371
4413
|
};
|
|
4372
4414
|
}
|
|
4373
4415
|
async initialize(options = {}, baseUrl) {
|
|
4374
|
-
this.uiConfig =
|
|
4416
|
+
this.uiConfig = {
|
|
4417
|
+
logo: true,
|
|
4418
|
+
slidePanel: true,
|
|
4419
|
+
pencilToolbar: true,
|
|
4420
|
+
zoomControl: true,
|
|
4421
|
+
...options.ui,
|
|
4422
|
+
};
|
|
4375
4423
|
await this.init({
|
|
4376
4424
|
baseUrl,
|
|
4377
4425
|
auth: options.auth,
|
|
4378
4426
|
initialId: options.shareId,
|
|
4379
4427
|
});
|
|
4380
|
-
|
|
4381
|
-
await this.mode.setUiConfig(this.uiConfig);
|
|
4382
|
-
}
|
|
4428
|
+
await this.mode.setUiConfig(this.uiConfig);
|
|
4383
4429
|
}
|
|
4384
4430
|
acceptsEventMessage() {
|
|
4385
4431
|
return false;
|
|
@@ -4434,6 +4480,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
|
|
|
4434
4480
|
class EmbeddedEditor extends EmbeddedTarget {
|
|
4435
4481
|
constructor(container) {
|
|
4436
4482
|
super(container, 'editor');
|
|
4483
|
+
this.resourceLibraryRequests = new Map();
|
|
4437
4484
|
this.currentSlideIndex = 0;
|
|
4438
4485
|
this.slideCount = 0;
|
|
4439
4486
|
this.historyCount = 0;
|
|
@@ -4538,11 +4585,15 @@ class EmbeddedEditor extends EmbeddedTarget {
|
|
|
4538
4585
|
throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
|
|
4539
4586
|
}
|
|
4540
4587
|
this.uiConfig = options.ui ? { ...options.ui } : {};
|
|
4588
|
+
this.resourceLibrary = options.resourceLibrary;
|
|
4541
4589
|
await this.init({
|
|
4542
4590
|
baseUrl,
|
|
4543
4591
|
auth: options.auth,
|
|
4544
4592
|
initialId: options.shareId,
|
|
4545
4593
|
});
|
|
4594
|
+
await this.post('setResourceLibraryAvailability', {
|
|
4595
|
+
available: Boolean(this.resourceLibrary),
|
|
4596
|
+
});
|
|
4546
4597
|
if (Object.keys(this.uiConfig).length > 0) {
|
|
4547
4598
|
await this.mode.setUiConfig(this.uiConfig);
|
|
4548
4599
|
}
|
|
@@ -4578,6 +4629,15 @@ class EmbeddedEditor extends EmbeddedTarget {
|
|
|
4578
4629
|
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
|
|
4579
4630
|
}
|
|
4580
4631
|
handleRequestMessage(message, sourceWindow) {
|
|
4632
|
+
if (message.type === 'resourceLibraryCancel') {
|
|
4633
|
+
this.resourceLibraryRequests.get(message.requestId)?.abort();
|
|
4634
|
+
this.resourceLibraryRequests.delete(message.requestId);
|
|
4635
|
+
return true;
|
|
4636
|
+
}
|
|
4637
|
+
if (message.type === 'resourceLibraryQuery') {
|
|
4638
|
+
void this.handleResourceLibraryQuery(message, sourceWindow);
|
|
4639
|
+
return true;
|
|
4640
|
+
}
|
|
4581
4641
|
if (message.type === 'save') {
|
|
4582
4642
|
void this.handleSaveRequest(message, sourceWindow);
|
|
4583
4643
|
return true;
|
|
@@ -4589,9 +4649,116 @@ class EmbeddedEditor extends EmbeddedTarget {
|
|
|
4589
4649
|
return false;
|
|
4590
4650
|
}
|
|
4591
4651
|
async destroy() {
|
|
4652
|
+
this.resourceLibraryRequests.forEach((controller) => controller.abort());
|
|
4653
|
+
this.resourceLibraryRequests.clear();
|
|
4592
4654
|
this.cancelActiveAi('destroyed', true);
|
|
4593
4655
|
await super.destroy();
|
|
4594
4656
|
}
|
|
4657
|
+
async handleResourceLibraryQuery(message, sourceWindow) {
|
|
4658
|
+
const controller = new AbortController();
|
|
4659
|
+
this.resourceLibraryRequests.set(message.requestId, controller);
|
|
4660
|
+
const respond = (payload) => {
|
|
4661
|
+
if (!controller.signal.aborted) {
|
|
4662
|
+
sourceWindow.postMessage(payload, '*');
|
|
4663
|
+
}
|
|
4664
|
+
};
|
|
4665
|
+
try {
|
|
4666
|
+
if (!this.resourceLibrary) {
|
|
4667
|
+
throw new AlgeoError('宿主未配置 resourceLibrary.query', EMBED_ERROR_CODES.BAD_REQUEST);
|
|
4668
|
+
}
|
|
4669
|
+
this.validateResourceLibraryQuery(message.params);
|
|
4670
|
+
const result = await this.resourceLibrary.query(message.params, {
|
|
4671
|
+
signal: controller.signal,
|
|
4672
|
+
});
|
|
4673
|
+
if (controller.signal.aborted)
|
|
4674
|
+
return;
|
|
4675
|
+
this.validateResourceLibraryResult(result, message.params);
|
|
4676
|
+
respond({
|
|
4677
|
+
type: 'response',
|
|
4678
|
+
requestId: message.requestId,
|
|
4679
|
+
success: true,
|
|
4680
|
+
result,
|
|
4681
|
+
});
|
|
4682
|
+
}
|
|
4683
|
+
catch (error) {
|
|
4684
|
+
if (controller.signal.aborted)
|
|
4685
|
+
return;
|
|
4686
|
+
respond({
|
|
4687
|
+
type: 'response',
|
|
4688
|
+
requestId: message.requestId,
|
|
4689
|
+
success: false,
|
|
4690
|
+
error: {
|
|
4691
|
+
code: error instanceof AlgeoError
|
|
4692
|
+
? error.code
|
|
4693
|
+
: EMBED_ERROR_CODES.UNKNOWN_ERROR,
|
|
4694
|
+
message: error instanceof Error ? error.message : String(error),
|
|
4695
|
+
},
|
|
4696
|
+
});
|
|
4697
|
+
}
|
|
4698
|
+
finally {
|
|
4699
|
+
if (this.resourceLibraryRequests.get(message.requestId) === controller) {
|
|
4700
|
+
this.resourceLibraryRequests.delete(message.requestId);
|
|
4701
|
+
}
|
|
4702
|
+
}
|
|
4703
|
+
}
|
|
4704
|
+
validateResourceLibraryQuery(params) {
|
|
4705
|
+
if (!Number.isInteger(params.page) ||
|
|
4706
|
+
params.page < 1 ||
|
|
4707
|
+
!Number.isInteger(params.pageSize) ||
|
|
4708
|
+
params.pageSize < 1 ||
|
|
4709
|
+
params.pageSize > 100 ||
|
|
4710
|
+
(params.keyword !== undefined && typeof params.keyword !== 'string') ||
|
|
4711
|
+
(params.mediaTypes !== undefined &&
|
|
4712
|
+
(!Array.isArray(params.mediaTypes) ||
|
|
4713
|
+
params.mediaTypes.some((value) => typeof value !== 'string')))) {
|
|
4714
|
+
throw new AlgeoError('资源库查询参数无效', EMBED_ERROR_CODES.BAD_REQUEST);
|
|
4715
|
+
}
|
|
4716
|
+
}
|
|
4717
|
+
validateResourceLibraryResult(result, params) {
|
|
4718
|
+
const invalid = () => {
|
|
4719
|
+
throw new AlgeoError('资源数据格式无效', EMBED_ERROR_CODES.BAD_REQUEST);
|
|
4720
|
+
};
|
|
4721
|
+
if (!result || !Array.isArray(result.items) || !result.pageInfo)
|
|
4722
|
+
invalid();
|
|
4723
|
+
const pageInfo = result.pageInfo;
|
|
4724
|
+
if (pageInfo.page !== params.page ||
|
|
4725
|
+
pageInfo.pageSize !== params.pageSize ||
|
|
4726
|
+
typeof pageInfo.hasNext !== 'boolean' ||
|
|
4727
|
+
(pageInfo.total !== undefined &&
|
|
4728
|
+
(!Number.isInteger(pageInfo.total) || pageInfo.total < 0)))
|
|
4729
|
+
invalid();
|
|
4730
|
+
const ids = new Set();
|
|
4731
|
+
for (const item of result.items) {
|
|
4732
|
+
if (!item ||
|
|
4733
|
+
typeof item.id !== 'string' ||
|
|
4734
|
+
!item.id.trim() ||
|
|
4735
|
+
ids.has(item.id) ||
|
|
4736
|
+
typeof item.name !== 'string' ||
|
|
4737
|
+
!item.name.trim() ||
|
|
4738
|
+
typeof item.mediaType !== 'string' ||
|
|
4739
|
+
!item.mediaType.startsWith('image/') ||
|
|
4740
|
+
!this.isHttpUrl(item.url) ||
|
|
4741
|
+
(item.thumbnailUrl !== undefined && !this.isHttpUrl(item.thumbnailUrl)))
|
|
4742
|
+
invalid();
|
|
4743
|
+
ids.add(item.id);
|
|
4744
|
+
for (const value of [item.width, item.height, item.size]) {
|
|
4745
|
+
if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
|
|
4746
|
+
invalid();
|
|
4747
|
+
}
|
|
4748
|
+
}
|
|
4749
|
+
}
|
|
4750
|
+
}
|
|
4751
|
+
isHttpUrl(value) {
|
|
4752
|
+
if (typeof value !== 'string')
|
|
4753
|
+
return false;
|
|
4754
|
+
try {
|
|
4755
|
+
const url = new URL(value);
|
|
4756
|
+
return url.protocol === 'http:' || url.protocol === 'https:';
|
|
4757
|
+
}
|
|
4758
|
+
catch {
|
|
4759
|
+
return false;
|
|
4760
|
+
}
|
|
4761
|
+
}
|
|
4595
4762
|
async loadContent(content, source) {
|
|
4596
4763
|
await this.post('loadContent', { content });
|
|
4597
4764
|
this.currentContent = content;
|
package/dist/algeo-sdk.umd.js
CHANGED
|
@@ -3869,6 +3869,12 @@
|
|
|
3869
3869
|
this.name = "AlgeoError";
|
|
3870
3870
|
}
|
|
3871
3871
|
};
|
|
3872
|
+
objectType({
|
|
3873
|
+
text: stringType().optional(),
|
|
3874
|
+
images: arrayType(stringType().trim().min(1)).optional(),
|
|
3875
|
+
openPanel: booleanType().optional(),
|
|
3876
|
+
focus: booleanType().optional()
|
|
3877
|
+
}).passthrough();
|
|
3872
3878
|
var openAiChatMessageV1Schema = objectType({
|
|
3873
3879
|
role: stringType(),
|
|
3874
3880
|
content: unionType([
|
|
@@ -3882,9 +3888,23 @@
|
|
|
3882
3888
|
name: stringType().optional()
|
|
3883
3889
|
}).passthrough();
|
|
3884
3890
|
objectType({
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3891
|
+
model: stringType().trim().min(1),
|
|
3892
|
+
input: unionType([
|
|
3893
|
+
stringType(),
|
|
3894
|
+
arrayType(unionType([openAiChatMessageV1Schema, recordType(unknownType())]))
|
|
3895
|
+
]).optional(),
|
|
3896
|
+
previous_response_id: stringType().trim().min(1).optional(),
|
|
3897
|
+
instructions: stringType().optional(),
|
|
3898
|
+
temperature: numberType().min(0).max(2).optional(),
|
|
3899
|
+
max_output_tokens: numberType().int().positive().optional(),
|
|
3900
|
+
metadata: recordType(unknownType()).optional()
|
|
3901
|
+
}).passthrough().superRefine((value, ctx) => {
|
|
3902
|
+
if (!("input" in value) && !("previous_response_id" in value)) {
|
|
3903
|
+
ctx.addIssue({
|
|
3904
|
+
code: ZodIssueCode.custom,
|
|
3905
|
+
message: "input or previous_response_id is required."
|
|
3906
|
+
});
|
|
3907
|
+
}
|
|
3888
3908
|
});
|
|
3889
3909
|
objectType({
|
|
3890
3910
|
id: stringType(),
|
|
@@ -4015,7 +4035,7 @@
|
|
|
4015
4035
|
}
|
|
4016
4036
|
|
|
4017
4037
|
/** SDK 版本号,构建时由 rollup 注入 */
|
|
4018
|
-
const VERSION = '2.
|
|
4038
|
+
const VERSION = '2.11.0';
|
|
4019
4039
|
const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
|
|
4020
4040
|
const DEFAULT_PRESENTATION_PATH = '/embed/present';
|
|
4021
4041
|
const DEFAULT_EDITOR_PATH = '/embed/edit';
|
|
@@ -4054,6 +4074,21 @@
|
|
|
4054
4074
|
typeof msg.payload === 'object' &&
|
|
4055
4075
|
msg.payload !== null);
|
|
4056
4076
|
}
|
|
4077
|
+
function isResourceLibraryRequestMessage(msg) {
|
|
4078
|
+
if (typeof msg !== 'object' || msg === null) {
|
|
4079
|
+
return false;
|
|
4080
|
+
}
|
|
4081
|
+
const value = msg;
|
|
4082
|
+
if (typeof value.requestId !== 'string') {
|
|
4083
|
+
return false;
|
|
4084
|
+
}
|
|
4085
|
+
if (value.type === 'resourceLibraryCancel') {
|
|
4086
|
+
return true;
|
|
4087
|
+
}
|
|
4088
|
+
return (value.type === 'resourceLibraryQuery' &&
|
|
4089
|
+
typeof value.params === 'object' &&
|
|
4090
|
+
value.params !== null);
|
|
4091
|
+
}
|
|
4057
4092
|
function isEmbedEventMessage(msg) {
|
|
4058
4093
|
if (typeof msg !== 'object' || msg == null || !('type' in msg)) {
|
|
4059
4094
|
return false;
|
|
@@ -4260,7 +4295,9 @@
|
|
|
4260
4295
|
pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
|
|
4261
4296
|
return;
|
|
4262
4297
|
}
|
|
4263
|
-
if ((isSaveRequestMessage(data) ||
|
|
4298
|
+
if ((isSaveRequestMessage(data) ||
|
|
4299
|
+
isAiRequestMessage(data) ||
|
|
4300
|
+
isResourceLibraryRequestMessage(data)) &&
|
|
4264
4301
|
this.handleRequestMessage(data, iframe.contentWindow)) {
|
|
4265
4302
|
return;
|
|
4266
4303
|
}
|
|
@@ -4360,7 +4397,12 @@
|
|
|
4360
4397
|
super(container, 'presentation');
|
|
4361
4398
|
this.currentSlideIndex = 0;
|
|
4362
4399
|
this.slideCount = 0;
|
|
4363
|
-
this.uiConfig = {
|
|
4400
|
+
this.uiConfig = {
|
|
4401
|
+
logo: true,
|
|
4402
|
+
slidePanel: true,
|
|
4403
|
+
pencilToolbar: true,
|
|
4404
|
+
zoomControl: true,
|
|
4405
|
+
};
|
|
4364
4406
|
this.mode = {
|
|
4365
4407
|
getUiConfig: () => ({ ...this.uiConfig }),
|
|
4366
4408
|
setUiConfig: async (config) => {
|
|
@@ -4377,15 +4419,19 @@
|
|
|
4377
4419
|
};
|
|
4378
4420
|
}
|
|
4379
4421
|
async initialize(options = {}, baseUrl) {
|
|
4380
|
-
this.uiConfig =
|
|
4422
|
+
this.uiConfig = {
|
|
4423
|
+
logo: true,
|
|
4424
|
+
slidePanel: true,
|
|
4425
|
+
pencilToolbar: true,
|
|
4426
|
+
zoomControl: true,
|
|
4427
|
+
...options.ui,
|
|
4428
|
+
};
|
|
4381
4429
|
await this.init({
|
|
4382
4430
|
baseUrl,
|
|
4383
4431
|
auth: options.auth,
|
|
4384
4432
|
initialId: options.shareId,
|
|
4385
4433
|
});
|
|
4386
|
-
|
|
4387
|
-
await this.mode.setUiConfig(this.uiConfig);
|
|
4388
|
-
}
|
|
4434
|
+
await this.mode.setUiConfig(this.uiConfig);
|
|
4389
4435
|
}
|
|
4390
4436
|
acceptsEventMessage() {
|
|
4391
4437
|
return false;
|
|
@@ -4440,6 +4486,7 @@
|
|
|
4440
4486
|
class EmbeddedEditor extends EmbeddedTarget {
|
|
4441
4487
|
constructor(container) {
|
|
4442
4488
|
super(container, 'editor');
|
|
4489
|
+
this.resourceLibraryRequests = new Map();
|
|
4443
4490
|
this.currentSlideIndex = 0;
|
|
4444
4491
|
this.slideCount = 0;
|
|
4445
4492
|
this.historyCount = 0;
|
|
@@ -4544,11 +4591,15 @@
|
|
|
4544
4591
|
throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
|
|
4545
4592
|
}
|
|
4546
4593
|
this.uiConfig = options.ui ? { ...options.ui } : {};
|
|
4594
|
+
this.resourceLibrary = options.resourceLibrary;
|
|
4547
4595
|
await this.init({
|
|
4548
4596
|
baseUrl,
|
|
4549
4597
|
auth: options.auth,
|
|
4550
4598
|
initialId: options.shareId,
|
|
4551
4599
|
});
|
|
4600
|
+
await this.post('setResourceLibraryAvailability', {
|
|
4601
|
+
available: Boolean(this.resourceLibrary),
|
|
4602
|
+
});
|
|
4552
4603
|
if (Object.keys(this.uiConfig).length > 0) {
|
|
4553
4604
|
await this.mode.setUiConfig(this.uiConfig);
|
|
4554
4605
|
}
|
|
@@ -4584,6 +4635,15 @@
|
|
|
4584
4635
|
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
|
|
4585
4636
|
}
|
|
4586
4637
|
handleRequestMessage(message, sourceWindow) {
|
|
4638
|
+
if (message.type === 'resourceLibraryCancel') {
|
|
4639
|
+
this.resourceLibraryRequests.get(message.requestId)?.abort();
|
|
4640
|
+
this.resourceLibraryRequests.delete(message.requestId);
|
|
4641
|
+
return true;
|
|
4642
|
+
}
|
|
4643
|
+
if (message.type === 'resourceLibraryQuery') {
|
|
4644
|
+
void this.handleResourceLibraryQuery(message, sourceWindow);
|
|
4645
|
+
return true;
|
|
4646
|
+
}
|
|
4587
4647
|
if (message.type === 'save') {
|
|
4588
4648
|
void this.handleSaveRequest(message, sourceWindow);
|
|
4589
4649
|
return true;
|
|
@@ -4595,9 +4655,116 @@
|
|
|
4595
4655
|
return false;
|
|
4596
4656
|
}
|
|
4597
4657
|
async destroy() {
|
|
4658
|
+
this.resourceLibraryRequests.forEach((controller) => controller.abort());
|
|
4659
|
+
this.resourceLibraryRequests.clear();
|
|
4598
4660
|
this.cancelActiveAi('destroyed', true);
|
|
4599
4661
|
await super.destroy();
|
|
4600
4662
|
}
|
|
4663
|
+
async handleResourceLibraryQuery(message, sourceWindow) {
|
|
4664
|
+
const controller = new AbortController();
|
|
4665
|
+
this.resourceLibraryRequests.set(message.requestId, controller);
|
|
4666
|
+
const respond = (payload) => {
|
|
4667
|
+
if (!controller.signal.aborted) {
|
|
4668
|
+
sourceWindow.postMessage(payload, '*');
|
|
4669
|
+
}
|
|
4670
|
+
};
|
|
4671
|
+
try {
|
|
4672
|
+
if (!this.resourceLibrary) {
|
|
4673
|
+
throw new AlgeoError('宿主未配置 resourceLibrary.query', EMBED_ERROR_CODES.BAD_REQUEST);
|
|
4674
|
+
}
|
|
4675
|
+
this.validateResourceLibraryQuery(message.params);
|
|
4676
|
+
const result = await this.resourceLibrary.query(message.params, {
|
|
4677
|
+
signal: controller.signal,
|
|
4678
|
+
});
|
|
4679
|
+
if (controller.signal.aborted)
|
|
4680
|
+
return;
|
|
4681
|
+
this.validateResourceLibraryResult(result, message.params);
|
|
4682
|
+
respond({
|
|
4683
|
+
type: 'response',
|
|
4684
|
+
requestId: message.requestId,
|
|
4685
|
+
success: true,
|
|
4686
|
+
result,
|
|
4687
|
+
});
|
|
4688
|
+
}
|
|
4689
|
+
catch (error) {
|
|
4690
|
+
if (controller.signal.aborted)
|
|
4691
|
+
return;
|
|
4692
|
+
respond({
|
|
4693
|
+
type: 'response',
|
|
4694
|
+
requestId: message.requestId,
|
|
4695
|
+
success: false,
|
|
4696
|
+
error: {
|
|
4697
|
+
code: error instanceof AlgeoError
|
|
4698
|
+
? error.code
|
|
4699
|
+
: EMBED_ERROR_CODES.UNKNOWN_ERROR,
|
|
4700
|
+
message: error instanceof Error ? error.message : String(error),
|
|
4701
|
+
},
|
|
4702
|
+
});
|
|
4703
|
+
}
|
|
4704
|
+
finally {
|
|
4705
|
+
if (this.resourceLibraryRequests.get(message.requestId) === controller) {
|
|
4706
|
+
this.resourceLibraryRequests.delete(message.requestId);
|
|
4707
|
+
}
|
|
4708
|
+
}
|
|
4709
|
+
}
|
|
4710
|
+
validateResourceLibraryQuery(params) {
|
|
4711
|
+
if (!Number.isInteger(params.page) ||
|
|
4712
|
+
params.page < 1 ||
|
|
4713
|
+
!Number.isInteger(params.pageSize) ||
|
|
4714
|
+
params.pageSize < 1 ||
|
|
4715
|
+
params.pageSize > 100 ||
|
|
4716
|
+
(params.keyword !== undefined && typeof params.keyword !== 'string') ||
|
|
4717
|
+
(params.mediaTypes !== undefined &&
|
|
4718
|
+
(!Array.isArray(params.mediaTypes) ||
|
|
4719
|
+
params.mediaTypes.some((value) => typeof value !== 'string')))) {
|
|
4720
|
+
throw new AlgeoError('资源库查询参数无效', EMBED_ERROR_CODES.BAD_REQUEST);
|
|
4721
|
+
}
|
|
4722
|
+
}
|
|
4723
|
+
validateResourceLibraryResult(result, params) {
|
|
4724
|
+
const invalid = () => {
|
|
4725
|
+
throw new AlgeoError('资源数据格式无效', EMBED_ERROR_CODES.BAD_REQUEST);
|
|
4726
|
+
};
|
|
4727
|
+
if (!result || !Array.isArray(result.items) || !result.pageInfo)
|
|
4728
|
+
invalid();
|
|
4729
|
+
const pageInfo = result.pageInfo;
|
|
4730
|
+
if (pageInfo.page !== params.page ||
|
|
4731
|
+
pageInfo.pageSize !== params.pageSize ||
|
|
4732
|
+
typeof pageInfo.hasNext !== 'boolean' ||
|
|
4733
|
+
(pageInfo.total !== undefined &&
|
|
4734
|
+
(!Number.isInteger(pageInfo.total) || pageInfo.total < 0)))
|
|
4735
|
+
invalid();
|
|
4736
|
+
const ids = new Set();
|
|
4737
|
+
for (const item of result.items) {
|
|
4738
|
+
if (!item ||
|
|
4739
|
+
typeof item.id !== 'string' ||
|
|
4740
|
+
!item.id.trim() ||
|
|
4741
|
+
ids.has(item.id) ||
|
|
4742
|
+
typeof item.name !== 'string' ||
|
|
4743
|
+
!item.name.trim() ||
|
|
4744
|
+
typeof item.mediaType !== 'string' ||
|
|
4745
|
+
!item.mediaType.startsWith('image/') ||
|
|
4746
|
+
!this.isHttpUrl(item.url) ||
|
|
4747
|
+
(item.thumbnailUrl !== undefined && !this.isHttpUrl(item.thumbnailUrl)))
|
|
4748
|
+
invalid();
|
|
4749
|
+
ids.add(item.id);
|
|
4750
|
+
for (const value of [item.width, item.height, item.size]) {
|
|
4751
|
+
if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
|
|
4752
|
+
invalid();
|
|
4753
|
+
}
|
|
4754
|
+
}
|
|
4755
|
+
}
|
|
4756
|
+
}
|
|
4757
|
+
isHttpUrl(value) {
|
|
4758
|
+
if (typeof value !== 'string')
|
|
4759
|
+
return false;
|
|
4760
|
+
try {
|
|
4761
|
+
const url = new URL(value);
|
|
4762
|
+
return url.protocol === 'http:' || url.protocol === 'https:';
|
|
4763
|
+
}
|
|
4764
|
+
catch {
|
|
4765
|
+
return false;
|
|
4766
|
+
}
|
|
4767
|
+
}
|
|
4601
4768
|
async loadContent(content, source) {
|
|
4602
4769
|
await this.post('loadContent', { content });
|
|
4603
4770
|
this.currentContent = content;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { EmbeddedTarget } from './embedded-target';
|
|
2
2
|
import { type AiApi, type AiCancelEvent, type AlgeoEditorCreateOptions, type ContentChangeEvent, type DocumentApi, type EmbedRequestMessage, type EmbeddedEditorEventListenerMap, type EmbeddedEditorEventMap, type EmbeddedEditorEventName, type HistoryApi, type ModeApi, type SaveEvent, type SlidesApi } from './shared';
|
|
3
3
|
export declare class EmbeddedEditor extends EmbeddedTarget<EmbeddedEditorEventMap, EmbeddedEditorEventName, EmbeddedEditorEventListenerMap> {
|
|
4
|
+
private resourceLibrary?;
|
|
5
|
+
private readonly resourceLibraryRequests;
|
|
4
6
|
readonly document: DocumentApi;
|
|
5
7
|
readonly slides: SlidesApi;
|
|
6
8
|
readonly history: HistoryApi;
|
|
@@ -21,6 +23,10 @@ export declare class EmbeddedEditor extends EmbeddedTarget<EmbeddedEditorEventMa
|
|
|
21
23
|
} | AiCancelEvent): void;
|
|
22
24
|
protected handleRequestMessage(message: EmbedRequestMessage, sourceWindow: Window): boolean;
|
|
23
25
|
destroy(): Promise<void>;
|
|
26
|
+
private handleResourceLibraryQuery;
|
|
27
|
+
private validateResourceLibraryQuery;
|
|
28
|
+
private validateResourceLibraryResult;
|
|
29
|
+
private isHttpUrl;
|
|
24
30
|
private loadContent;
|
|
25
31
|
private switchTo;
|
|
26
32
|
private addSlide;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { AlgeoError, EMBED_ERROR_CODES, VERSION } from './shared';
|
|
2
2
|
export type { FileContentLatest } from '@dajiaoai/algeo-protocol';
|
|
3
3
|
export { convertToLatest } from '@dajiaoai/algeo-protocol';
|
|
4
|
-
export type { AiApi, AiCancelEvent, AiRawSseEventV1, AiRequestEvent, AiRunPayloadV1, AiStreamEventV1, AlgeoCreateOptions, AlgeoEditorAuthOptions, AlgeoEditorCreateOptions, AlgeoEditorSaveResult, AlgeoEditorUiConfig, AlgeoEmbedMode, AlgeoErrorPayload, AlgeoPresentationCreateOptions, AlgeoPresentationUiConfig, ContentChangeEvent, DocumentApi, EmbeddedEditorEventListenerMap, EmbeddedEditorEventMap, EmbeddedEditorEventName, EmbeddedPresentationEventListenerMap, EmbeddedPresentationEventMap, EmbeddedPresentationEventName, ExportImageFormat, ExportImageOptions, ExportImageSizeMode, ExportImageViewMode, ExportImageContainMode, ExportViewBounds, ExportPadding, ExportSlideImageOptions, ExportedSlideImage, ExportSlideImageResult, ExportLatexOptions, ExportedLatex, ExportLatexResult, GetContentResult, GetSlideCountResult, HistoryApi, LoadFileResult, LoadShareByIdResult, ModeApi, PresentationModeApi, ReadyEvent, ReplResult, SlideChangeEvent, SlideIndexResult, SlidesApi, SwitchSlideResult, SaveEvent, } from './shared';
|
|
4
|
+
export type { AiApi, AiCancelEvent, AiRawSseEventV1, AiRequestEvent, AiRunPayloadV1, AiStreamEventV1, AlgeoCreateOptions, AlgeoEditorAuthOptions, AlgeoEditorCreateOptions, AlgeoEditorSaveResult, AlgeoEditorUiConfig, AlgeoEmbedMode, AlgeoErrorPayload, AlgeoPresentationCreateOptions, AlgeoPresentationUiConfig, ContentChangeEvent, DocumentApi, EmbeddedEditorEventListenerMap, EmbeddedEditorEventMap, EmbeddedEditorEventName, EmbeddedPresentationEventListenerMap, EmbeddedPresentationEventMap, EmbeddedPresentationEventName, ExportImageFormat, ExportImageOptions, ExportImageSizeMode, ExportImageViewMode, ExportImageContainMode, ExportViewBounds, ExportPadding, ExportSlideImageOptions, ExportedSlideImage, ExportSlideImageResult, ExportLatexOptions, ExportedLatex, ExportLatexResult, GetContentResult, GetSlideCountResult, HistoryApi, LoadFileResult, LoadShareByIdResult, ModeApi, PresentationModeApi, ReadyEvent, ResourceLibraryItem, ResourceLibraryPageInfo, ResourceLibraryProvider, ResourceLibraryQuery, ResourceLibraryQueryContext, ResourceLibraryResult, ReplResult, SlideChangeEvent, SlideIndexResult, SlidesApi, SwitchSlideResult, SaveEvent, } from './shared';
|
|
5
5
|
export { EmbeddedPresentation } from './embedded-presentation';
|
|
6
6
|
export { EmbeddedEditor } from './embedded-editor';
|
|
7
7
|
export { AlgeoSdk, create, createEditor, createPresentation, type EmbeddedInstance, } from './sdk';
|
package/dist/shared.d.ts
CHANGED
|
@@ -29,6 +29,9 @@ export interface AlgeoEditorUiConfig {
|
|
|
29
29
|
}
|
|
30
30
|
export interface AlgeoPresentationUiConfig {
|
|
31
31
|
logo?: boolean;
|
|
32
|
+
slidePanel?: boolean;
|
|
33
|
+
pencilToolbar?: boolean;
|
|
34
|
+
zoomControl?: boolean;
|
|
32
35
|
}
|
|
33
36
|
export type AlgeoEditorSaveResult = {
|
|
34
37
|
status: 'success';
|
|
@@ -41,6 +44,42 @@ export interface AlgeoEditorCreateOptions {
|
|
|
41
44
|
shareId?: string;
|
|
42
45
|
initialContent?: FileContentLatest;
|
|
43
46
|
ui?: AlgeoEditorUiConfig;
|
|
47
|
+
/** 接入方提供的只读图片资源库。 */
|
|
48
|
+
resourceLibrary?: ResourceLibraryProvider;
|
|
49
|
+
}
|
|
50
|
+
export interface ResourceLibraryQuery {
|
|
51
|
+
/** 页码,从 1 开始。 */
|
|
52
|
+
page: number;
|
|
53
|
+
/** 每页数量,最大 100。 */
|
|
54
|
+
pageSize: number;
|
|
55
|
+
keyword?: string;
|
|
56
|
+
mediaTypes?: string[];
|
|
57
|
+
}
|
|
58
|
+
export interface ResourceLibraryQueryContext {
|
|
59
|
+
signal: AbortSignal;
|
|
60
|
+
}
|
|
61
|
+
export interface ResourceLibraryProvider {
|
|
62
|
+
query(params: ResourceLibraryQuery, context: ResourceLibraryQueryContext): Promise<ResourceLibraryResult>;
|
|
63
|
+
}
|
|
64
|
+
export interface ResourceLibraryItem {
|
|
65
|
+
id: string;
|
|
66
|
+
name: string;
|
|
67
|
+
mediaType: string;
|
|
68
|
+
url: string;
|
|
69
|
+
thumbnailUrl?: string;
|
|
70
|
+
width?: number;
|
|
71
|
+
height?: number;
|
|
72
|
+
size?: number;
|
|
73
|
+
}
|
|
74
|
+
export interface ResourceLibraryPageInfo {
|
|
75
|
+
page: number;
|
|
76
|
+
pageSize: number;
|
|
77
|
+
hasNext: boolean;
|
|
78
|
+
total?: number;
|
|
79
|
+
}
|
|
80
|
+
export interface ResourceLibraryResult {
|
|
81
|
+
items: ResourceLibraryItem[];
|
|
82
|
+
pageInfo: ResourceLibraryPageInfo;
|
|
44
83
|
}
|
|
45
84
|
export interface AlgeoPresentationCreateOptions {
|
|
46
85
|
auth?: AlgeoEditorAuthOptions;
|
|
@@ -117,6 +156,15 @@ export interface AiRequestMessage {
|
|
|
117
156
|
requestId: string;
|
|
118
157
|
payload: AiRunPayloadV1;
|
|
119
158
|
}
|
|
159
|
+
export interface ResourceLibraryQueryMessage {
|
|
160
|
+
type: 'resourceLibraryQuery';
|
|
161
|
+
requestId: string;
|
|
162
|
+
params: ResourceLibraryQuery;
|
|
163
|
+
}
|
|
164
|
+
export interface ResourceLibraryCancelMessage {
|
|
165
|
+
type: 'resourceLibraryCancel';
|
|
166
|
+
requestId: string;
|
|
167
|
+
}
|
|
120
168
|
export interface EmbeddedEditorEventMap {
|
|
121
169
|
ready: ReadyEvent;
|
|
122
170
|
contentChange: ContentChangeEvent;
|
|
@@ -171,6 +219,11 @@ export interface ExportViewBounds {
|
|
|
171
219
|
y: number;
|
|
172
220
|
width: number;
|
|
173
221
|
height: number;
|
|
222
|
+
/**
|
|
223
|
+
* 相机缩放比例(每世界坐标单位对应的像素数)。
|
|
224
|
+
* 省略时使用目标画板文件中的 camera.scale。
|
|
225
|
+
*/
|
|
226
|
+
scale?: number;
|
|
174
227
|
}
|
|
175
228
|
/** 导出留白,单位为输出像素 px。 */
|
|
176
229
|
export type ExportPadding = number | {
|
|
@@ -196,8 +249,9 @@ export interface ExportImageSizeMode extends ExportImageBaseOptions {
|
|
|
196
249
|
minPadding?: ExportPadding;
|
|
197
250
|
}
|
|
198
251
|
/**
|
|
199
|
-
* 场景二(view):viewBounds
|
|
200
|
-
*
|
|
252
|
+
* 场景二(view):viewBounds 的位置及宽高均为世界坐标。
|
|
253
|
+
* scale 省略时从文件的 camera.scale 读取。
|
|
254
|
+
* 输出像素 = viewBounds 世界尺寸 x viewBounds.scale x pixelRatio。
|
|
201
255
|
*/
|
|
202
256
|
export interface ExportImageViewMode extends ExportImageBaseOptions {
|
|
203
257
|
mode: 'view';
|
|
@@ -247,6 +301,16 @@ export interface ExportLatexOptions {
|
|
|
247
301
|
export interface ExportedLatex {
|
|
248
302
|
index: number;
|
|
249
303
|
code: string;
|
|
304
|
+
/** TikZ 通过 path 引用的图片资源;调用方应保持相对目录结构落盘。 */
|
|
305
|
+
assets: Array<{
|
|
306
|
+
path: string;
|
|
307
|
+
blob: Blob;
|
|
308
|
+
mediaType: string;
|
|
309
|
+
}>;
|
|
310
|
+
/** 可直接下载的导出文件;包含图片时为 ZIP,否则为 TeX。 */
|
|
311
|
+
blob: Blob;
|
|
312
|
+
fileName: string;
|
|
313
|
+
format: 'tex' | 'zip';
|
|
250
314
|
}
|
|
251
315
|
export interface ExportLatexResult {
|
|
252
316
|
items: ExportedLatex[];
|
|
@@ -312,9 +376,10 @@ export declare function generateRequestId(): string;
|
|
|
312
376
|
export declare function isResponseMessage(msg: unknown): msg is EmbedResponseMessage;
|
|
313
377
|
export declare function isReadyMessage(msg: unknown): msg is EmbedReadyMessage;
|
|
314
378
|
export type EmbedEventMessage = ContentChangeEvent | SlideChangeEvent | SaveSuccessEvent | AiCancelEvent;
|
|
315
|
-
export type EmbedRequestMessage = SaveRequestMessage | AiRequestMessage;
|
|
379
|
+
export type EmbedRequestMessage = SaveRequestMessage | AiRequestMessage | ResourceLibraryQueryMessage | ResourceLibraryCancelMessage;
|
|
316
380
|
export declare function isSaveRequestMessage(msg: unknown): msg is SaveRequestMessage;
|
|
317
381
|
export declare function isAiRequestMessage(msg: unknown): msg is AiRequestMessage;
|
|
382
|
+
export declare function isResourceLibraryRequestMessage(msg: unknown): msg is ResourceLibraryQueryMessage | ResourceLibraryCancelMessage;
|
|
318
383
|
export declare function isEmbedEventMessage(msg: unknown): msg is EmbedEventMessage;
|
|
319
384
|
export declare function normalizeBaseUrl(baseUrl: string): string;
|
|
320
385
|
export declare function normalizeMode(mode?: AlgeoEmbedMode): AlgeoEmbedMode;
|