@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.
@@ -16,6 +16,7 @@
16
16
  REPL_EXECUTE_FAILED: "EMBED_REPL_EXECUTE_FAILED",
17
17
  UNKNOWN_METHOD: "EMBED_UNKNOWN_METHOD",
18
18
  UNKNOWN_ERROR: "EMBED_UNKNOWN_ERROR",
19
+ MISSING_APP_ID: "EMBED_MISSING_APP_ID",
19
20
  BAD_REQUEST: "EMBED_BAD_REQUEST",
20
21
  IFRAME_NOT_READY: "EMBED_IFRAME_NOT_READY",
21
22
  TIMEOUT: "EMBED_TIMEOUT",
@@ -3899,13 +3900,12 @@
3899
3900
  metadata: fileContentV10MetadataSchema
3900
3901
  });
3901
3902
 
3902
- /**
3903
- * 大角几何内嵌画板 SDK
3904
- * 绑定 DOM 容器,自动创建 iframe 并封装 postMessage 通信
3905
- */
3906
3903
  /** SDK 版本号,构建时由 rollup 注入 */
3907
- const VERSION = '1.2.1';
3904
+ const VERSION = '2.5.0';
3908
3905
  const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
3906
+ const DEFAULT_PRESENTATION_PATH = '/e';
3907
+ const DEFAULT_EDITOR_PATH = '/embed/edit';
3908
+ const EMBED_TIMEOUT_MS = 30000;
3909
3909
  let requestIdCounter = 0;
3910
3910
  function generateRequestId() {
3911
3911
  return `req-${Date.now()}-${++requestIdCounter}`;
@@ -3923,71 +3923,211 @@
3923
3923
  'type' in msg &&
3924
3924
  msg.type === 'ready');
3925
3925
  }
3926
- /**
3927
- * 大角几何内嵌画板 SDK
3928
- */
3929
- class AlgeoSdk {
3930
- /** 是否已就绪(收到 iframe ready 通知) */
3926
+ function isSaveRequestMessage(msg) {
3927
+ return (typeof msg === 'object' &&
3928
+ msg !== null &&
3929
+ 'type' in msg &&
3930
+ msg.type === 'save' &&
3931
+ typeof msg.requestId === 'string' &&
3932
+ typeof msg.content === 'object');
3933
+ }
3934
+ function isEmbedEventMessage(msg) {
3935
+ if (typeof msg !== 'object' || msg == null || !('type' in msg)) {
3936
+ return false;
3937
+ }
3938
+ if ('requestId' in msg) {
3939
+ return false;
3940
+ }
3941
+ const type = msg.type;
3942
+ if (type === 'slideChange') {
3943
+ return typeof msg.index === 'number';
3944
+ }
3945
+ if (type === 'save') {
3946
+ const stage = msg.stage;
3947
+ return (typeof msg.content === 'object' &&
3948
+ (stage === undefined || stage === 'success'));
3949
+ }
3950
+ if (type === 'contentChange') {
3951
+ return true;
3952
+ }
3953
+ return false;
3954
+ }
3955
+ function normalizeBaseUrl(baseUrl) {
3956
+ return baseUrl.replace(/\/+$/, '');
3957
+ }
3958
+ function normalizeMode(mode) {
3959
+ return mode === 'editor' ? 'editor' : 'presentation';
3960
+ }
3961
+ function getEmbedPath(mode) {
3962
+ return mode === 'editor' ? DEFAULT_EDITOR_PATH : DEFAULT_PRESENTATION_PATH;
3963
+ }
3964
+ function buildEmbedSrc(options) {
3965
+ const baseUrl = normalizeBaseUrl(options.baseUrl ?? DEFAULT_EMBED_BASE);
3966
+ const mode = normalizeMode(options.mode);
3967
+ const path = getEmbedPath(mode);
3968
+ const authAppId = options.auth?.appId?.trim() ?? '';
3969
+ const initialId = options.initialId?.trim();
3970
+ if (mode === 'editor') {
3971
+ if (!initialId) {
3972
+ return `${baseUrl}${path}/${encodeURIComponent(authAppId)}`;
3973
+ }
3974
+ return `${baseUrl}${path}/${encodeURIComponent(authAppId)}/${encodeURIComponent(initialId)}`;
3975
+ }
3976
+ if (!initialId) {
3977
+ return `${baseUrl}${path}`;
3978
+ }
3979
+ return `${baseUrl}${path}/${encodeURIComponent(initialId)}`;
3980
+ }
3981
+
3982
+ class EmbeddedTarget {
3983
+ constructor(container, embedMode) {
3984
+ this.container = container;
3985
+ this.embedMode = embedMode;
3986
+ this.pending = new Map();
3987
+ this.listenerBuckets = new Map();
3988
+ this.destroyed = false;
3989
+ this._ready = false;
3990
+ this._version = null;
3991
+ }
3931
3992
  get ready() {
3932
3993
  return this._ready;
3933
3994
  }
3934
- /** 内嵌页协议版本 */
3935
3995
  get version() {
3936
3996
  return this._version;
3937
3997
  }
3938
- constructor(container) {
3939
- this.pending = new Map();
3998
+ on(event, listener) {
3999
+ let bucket = this.listenerBuckets.get(event);
4000
+ if (!bucket) {
4001
+ bucket = new Set();
4002
+ this.listenerBuckets.set(event, bucket);
4003
+ }
4004
+ bucket.add(listener);
4005
+ return () => this.off(event, listener);
4006
+ }
4007
+ getListeners(event) {
4008
+ const bucket = this.listenerBuckets.get(event);
4009
+ return bucket ? Array.from(bucket) : [];
4010
+ }
4011
+ off(event, listener) {
4012
+ const bucket = this.listenerBuckets.get(event);
4013
+ bucket?.delete(listener);
4014
+ }
4015
+ emit(event, payload) {
4016
+ const bucket = this.listenerBuckets.get(event);
4017
+ bucket?.forEach((listener) => listener(payload));
4018
+ }
4019
+ handleEventMessage(_event) { }
4020
+ acceptsEventMessage() {
4021
+ return true;
4022
+ }
4023
+ handleRequestMessage(_message, _sourceWindow) {
4024
+ return false;
4025
+ }
4026
+ cleanupMessageHandler() {
4027
+ if (this.messageHandler) {
4028
+ window.removeEventListener('message', this.messageHandler);
4029
+ this.messageHandler = undefined;
4030
+ }
4031
+ }
4032
+ cleanupIframe() {
4033
+ if (this.iframe?.parentNode) {
4034
+ this.iframe.parentNode.removeChild(this.iframe);
4035
+ }
4036
+ this.iframe = undefined;
4037
+ }
4038
+ resetRuntimeState() {
3940
4039
  this._ready = false;
3941
4040
  this._version = null;
3942
- this.container = container;
3943
- }
3944
- static async create(container, options) {
3945
- const ret = new AlgeoSdk(container);
3946
- await ret.init(options);
3947
- return ret;
3948
4041
  }
3949
- async init(options = {}) {
4042
+ async init(options) {
3950
4043
  if (this.iframe) {
3951
4044
  throw new AlgeoError('请勿多次调用init方法。', EMBED_ERROR_CODES.BAD_REQUEST);
3952
4045
  }
3953
4046
  return new Promise((resolve, reject) => {
3954
- const baseUrl = options.baseUrl ?? DEFAULT_EMBED_BASE;
3955
- const initialId = options.initialId ?? '';
3956
- const src = initialId
3957
- ? `${baseUrl}/e/${encodeURIComponent(initialId)}`
3958
- : `${baseUrl}/e`;
3959
- const iframe = this.iframe = document.createElement('iframe');
4047
+ const src = buildEmbedSrc({
4048
+ ...options,
4049
+ mode: this.embedMode,
4050
+ });
4051
+ let settled = false;
4052
+ let initTimeoutId;
4053
+ const finalizeFailure = (error) => {
4054
+ if (settled) {
4055
+ return;
4056
+ }
4057
+ settled = true;
4058
+ if (initTimeoutId) {
4059
+ clearTimeout(initTimeoutId);
4060
+ }
4061
+ this.resetRuntimeState();
4062
+ this.cleanupMessageHandler();
4063
+ this.cleanupIframe();
4064
+ reject(error);
4065
+ };
4066
+ const finalizeSuccess = () => {
4067
+ if (settled) {
4068
+ return;
4069
+ }
4070
+ settled = true;
4071
+ if (initTimeoutId) {
4072
+ clearTimeout(initTimeoutId);
4073
+ }
4074
+ resolve();
4075
+ };
4076
+ const iframe = (this.iframe = document.createElement('iframe'));
3960
4077
  iframe.id = 'algeo-embed';
3961
4078
  iframe.src = src;
3962
4079
  iframe.allow = 'fullscreen';
4080
+ iframe.referrerPolicy = 'origin';
3963
4081
  iframe.style.width = '100%';
3964
4082
  iframe.style.height = '100%';
3965
4083
  iframe.style.border = 'none';
3966
- iframe.addEventListener('error', reject);
4084
+ iframe.addEventListener('error', () => {
4085
+ finalizeFailure(new AlgeoError('iframe 初始化失败', EMBED_ERROR_CODES.IFRAME_NOT_READY));
4086
+ });
3967
4087
  this.container.appendChild(iframe);
4088
+ initTimeoutId = setTimeout(() => {
4089
+ finalizeFailure(new AlgeoError('iframe 初始化超时', EMBED_ERROR_CODES.TIMEOUT));
4090
+ }, EMBED_TIMEOUT_MS);
3968
4091
  this.messageHandler = (e) => {
3969
- if (e.source !== iframe.contentWindow)
4092
+ if (e.source !== iframe.contentWindow) {
3970
4093
  return;
4094
+ }
3971
4095
  const { data } = e;
3972
4096
  if (isReadyMessage(data)) {
4097
+ if (this._ready) {
4098
+ return;
4099
+ }
3973
4100
  this._ready = true;
3974
4101
  this._version = data.version;
3975
- resolve();
4102
+ this.emit('ready', {
4103
+ type: 'ready',
4104
+ mode: this.embedMode,
4105
+ version: this._version,
4106
+ });
4107
+ finalizeSuccess();
3976
4108
  return;
3977
4109
  }
3978
4110
  if (isResponseMessage(data)) {
3979
- const { requestId, success } = data;
3980
- const pending = this.pending.get(requestId);
3981
- if (pending) {
3982
- this.pending.delete(requestId);
3983
- if (success) {
3984
- pending.resolve(data.result);
3985
- }
3986
- else {
3987
- const err = data.error;
3988
- pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
3989
- }
4111
+ const pending = this.pending.get(data.requestId);
4112
+ if (!pending) {
4113
+ return;
4114
+ }
4115
+ this.pending.delete(data.requestId);
4116
+ if (data.success) {
4117
+ pending.resolve(data.result);
4118
+ return;
3990
4119
  }
4120
+ const err = data.error;
4121
+ pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
4122
+ return;
4123
+ }
4124
+ if (isSaveRequestMessage(data) &&
4125
+ this.handleRequestMessage(data, iframe.contentWindow)) {
4126
+ return;
4127
+ }
4128
+ if (this.acceptsEventMessage() && isEmbedEventMessage(data)) {
4129
+ this.handleEventMessage(data);
4130
+ this.emit(data.type, data);
3991
4131
  }
3992
4132
  };
3993
4133
  window.addEventListener('message', this.messageHandler);
@@ -3997,79 +4137,374 @@
3997
4137
  const requestId = generateRequestId();
3998
4138
  const msg = { type, requestId, ...payload };
3999
4139
  return new Promise((resolve, reject) => {
4140
+ if (this.destroyed) {
4141
+ reject(new AlgeoError('SDK 已销毁', EMBED_ERROR_CODES.DESTROYED));
4142
+ return;
4143
+ }
4000
4144
  if (!this._ready) {
4001
4145
  reject(new AlgeoError('iframe 未加载完成', EMBED_ERROR_CODES.IFRAME_NOT_READY));
4146
+ return;
4002
4147
  }
4003
- this.pending.set(requestId, {
4004
- resolve: resolve,
4005
- reject,
4006
- });
4007
4148
  const target = this.iframe?.contentWindow;
4008
4149
  if (!target) {
4009
- this.pending.delete(requestId);
4010
4150
  reject(new AlgeoError('iframe 未加载完成', EMBED_ERROR_CODES.IFRAME_NOT_READY));
4011
4151
  return;
4012
4152
  }
4153
+ this.pending.set(requestId, {
4154
+ resolve: resolve,
4155
+ reject,
4156
+ });
4013
4157
  target.postMessage(msg, '*');
4014
- // 超时兜底
4015
4158
  setTimeout(() => {
4016
4159
  if (this.pending.has(requestId)) {
4017
4160
  this.pending.delete(requestId);
4018
4161
  reject(new AlgeoError('请求超时', EMBED_ERROR_CODES.TIMEOUT));
4019
4162
  }
4020
- }, 30000);
4163
+ }, EMBED_TIMEOUT_MS);
4021
4164
  });
4022
4165
  }
4023
- /**
4024
- * 按分享 ID 加载内容
4025
- */
4026
- loadShareById(id) {
4027
- return this.post('loadShareById', { id });
4166
+ async destroy() {
4167
+ if (this.destroyed) {
4168
+ return;
4169
+ }
4170
+ this.destroyed = true;
4171
+ this.cleanupMessageHandler();
4172
+ this.resetRuntimeState();
4173
+ this.pending.forEach(({ reject }) => {
4174
+ reject(new AlgeoError('SDK 已销毁', EMBED_ERROR_CODES.DESTROYED));
4175
+ });
4176
+ this.pending.clear();
4177
+ this.cleanupIframe();
4028
4178
  }
4029
- /**
4030
- * 加载完整文件内容(覆盖式)
4031
- */
4032
- loadFile(content) {
4033
- return this.post('loadFile', { content });
4179
+ }
4180
+
4181
+ class EmbeddedPresentation extends EmbeddedTarget {
4182
+ constructor(container) {
4183
+ super(container, 'presentation');
4184
+ this.currentSlideIndex = 0;
4185
+ this.slideCount = 0;
4186
+ }
4187
+ async initialize(options = {}, baseUrl) {
4188
+ await this.init({
4189
+ baseUrl,
4190
+ initialId: options.shareId,
4191
+ });
4034
4192
  }
4035
- /**
4036
- * 切换到指定索引的画板
4037
- */
4038
- switchSlide(index) {
4039
- return this.post('switchSlide', { index });
4193
+ acceptsEventMessage() {
4194
+ return false;
4040
4195
  }
4041
- /**
4042
- * 查询画板数量
4043
- */
4044
- getSlideCount() {
4045
- return this.post('getSlideCount', {});
4196
+ setWhitelistError(error) {
4197
+ this.whitelistError = error;
4046
4198
  }
4047
- /**
4048
- * 执行 REPL 指令,返回面向 AI 的文档/文本内容
4049
- * @param command REPL 可用的单个 command 指令,如 help、list、list_slides、eval 等
4050
- */
4051
- repl(command) {
4199
+ ensureWhitelistAccess(methodName) {
4200
+ if (!this.whitelistError) {
4201
+ return;
4202
+ }
4203
+ const error = new AlgeoError(`演示模式调用 ${methodName} 被白名单限制。${this.whitelistError.message}`, this.whitelistError.code, this.whitelistError.details);
4204
+ console.error(error);
4205
+ throw error;
4206
+ }
4207
+ async loadShareById(id) {
4208
+ this.ensureWhitelistAccess('loadShareById');
4209
+ const result = await this.post('loadShareById', {
4210
+ id,
4211
+ });
4212
+ this.currentContent = undefined;
4213
+ this.currentSlideIndex = 0;
4214
+ this.slideCount = 0;
4215
+ return result;
4216
+ }
4217
+ async loadFile(content) {
4218
+ this.ensureWhitelistAccess('loadFile');
4219
+ const result = await this.post('loadContent', { content });
4220
+ this.currentContent = content;
4221
+ this.currentSlideIndex = 0;
4222
+ this.slideCount = content.slides.length;
4223
+ return result;
4224
+ }
4225
+ async switchSlide(index) {
4226
+ this.ensureWhitelistAccess('switchSlide');
4227
+ const result = await this.post('switchSlide', { index });
4228
+ this.currentSlideIndex = index;
4229
+ return result;
4230
+ }
4231
+ async getSlideCount() {
4232
+ this.ensureWhitelistAccess('getSlideCount');
4233
+ const result = await this.post('getSlideCount', {});
4234
+ this.slideCount = result.count;
4235
+ return result;
4236
+ }
4237
+ async repl(command) {
4238
+ this.ensureWhitelistAccess('repl');
4052
4239
  return this.post('repl', { command });
4053
4240
  }
4054
- /**
4055
- * 销毁实例,移除 iframe 与事件监听
4056
- */
4057
- destroy() {
4058
- if (this.messageHandler) {
4059
- window.removeEventListener('message', this.messageHandler);
4060
- this.messageHandler = undefined;
4241
+ }
4242
+
4243
+ class EmbeddedEditor extends EmbeddedTarget {
4244
+ constructor(container) {
4245
+ super(container, 'editor');
4246
+ this.currentSlideIndex = 0;
4247
+ this.slideCount = 0;
4248
+ this.historyCount = 0;
4249
+ this.historyCurrentIndex = -1;
4250
+ this.uiConfig = {};
4251
+ this.document = {
4252
+ loadContent: async (content) => {
4253
+ await this.loadContent(content, 'loadContent');
4254
+ },
4255
+ getContent: async () => {
4256
+ const result = await this.post('getContent', {});
4257
+ this.currentContent = result.content;
4258
+ this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(result.content.slides.length - 1, 0));
4259
+ this.slideCount = result.content.slides.length;
4260
+ await this.refreshHistoryState();
4261
+ return result.content;
4262
+ },
4263
+ };
4264
+ this.slides = {
4265
+ getCount: () => this.slideCount,
4266
+ getCurrentIndex: () => this.currentSlideIndex,
4267
+ switchTo: async (index) => {
4268
+ await this.switchTo(index);
4269
+ },
4270
+ add: async () => this.addSlide('addSlide', {}),
4271
+ addAt: async (index) => this.addSlide('addSlideAt', { index }),
4272
+ remove: async (index) => {
4273
+ await this.post('removeSlide', { index });
4274
+ this.slideCount = Math.max(0, this.slideCount - 1);
4275
+ this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
4276
+ await this.refreshHistoryState();
4277
+ },
4278
+ duplicate: async (index, targetIndex) => this.addSlide('duplicateSlide', { index, targetIndex }),
4279
+ reorder: async (fromIndex, toIndex) => {
4280
+ await this.post('reorderSlide', { fromIndex, toIndex });
4281
+ if (this.currentSlideIndex === fromIndex) {
4282
+ this.currentSlideIndex = toIndex;
4283
+ }
4284
+ await this.refreshHistoryState();
4285
+ },
4286
+ };
4287
+ this.history = {
4288
+ getCount: () => this.historyCount,
4289
+ getCurrentIndex: () => this.historyCurrentIndex,
4290
+ undo: async () => {
4291
+ await this.post('undo', {});
4292
+ await this.refreshHistoryState();
4293
+ },
4294
+ redo: async () => {
4295
+ await this.post('redo', {});
4296
+ await this.refreshHistoryState();
4297
+ },
4298
+ jumpTo: async (index) => {
4299
+ await this.post('jumpHistory', { index });
4300
+ await this.refreshHistoryState();
4301
+ },
4302
+ canUndo: () => this.historyCurrentIndex >= 0,
4303
+ canRedo: () => this.historyCurrentIndex < this.historyCount - 1,
4304
+ clear: async () => {
4305
+ await this.post('clearHistory', {});
4306
+ await this.refreshHistoryState();
4307
+ },
4308
+ };
4309
+ this.mode = {
4310
+ getUiConfig: () => ({ ...this.uiConfig }),
4311
+ setUiConfig: async (config) => {
4312
+ await this.post('setUiConfig', { config });
4313
+ this.uiConfig = {
4314
+ ...this.uiConfig,
4315
+ ...config,
4316
+ };
4317
+ },
4318
+ };
4319
+ }
4320
+ async initialize(options = {}, baseUrl) {
4321
+ if (!options.auth?.appId?.trim()) {
4322
+ throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
4061
4323
  }
4062
- this.pending.forEach(({ reject }) => reject(new AlgeoError('SDK 已销毁', EMBED_ERROR_CODES.DESTROYED)));
4063
- this.pending.clear();
4064
- if (this.iframe?.parentNode) {
4065
- this.iframe.parentNode.removeChild(this.iframe);
4324
+ this.uiConfig = options.ui || {};
4325
+ await this.init({
4326
+ baseUrl,
4327
+ auth: options.auth,
4328
+ initialId: options.shareId,
4329
+ });
4330
+ const content = await this.document.getContent();
4331
+ this.currentContent = content;
4332
+ this.slideCount = content.slides.length;
4333
+ this.currentSlideIndex = 0;
4334
+ await this.refreshHistoryState();
4335
+ if (options.initialContent) {
4336
+ await this.loadContent(options.initialContent, 'initialContent');
4066
4337
  }
4067
4338
  }
4339
+ handleEventMessage(event) {
4340
+ if (event.type === 'contentChange') {
4341
+ if (event.content) {
4342
+ this.currentContent = event.content;
4343
+ this.slideCount = event.content.slides.length;
4344
+ this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
4345
+ }
4346
+ return;
4347
+ }
4348
+ if (event.type === 'slideChange') {
4349
+ this.currentSlideIndex = event.index;
4350
+ return;
4351
+ }
4352
+ this.currentContent = event.content;
4353
+ this.slideCount = event.content.slides.length;
4354
+ this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
4355
+ }
4356
+ handleRequestMessage(message, sourceWindow) {
4357
+ if (message.type !== 'save') {
4358
+ return false;
4359
+ }
4360
+ void this.handleSaveRequest(message, sourceWindow);
4361
+ return true;
4362
+ }
4363
+ async loadContent(content, source) {
4364
+ await this.post('loadContent', { content });
4365
+ this.currentContent = content;
4366
+ this.currentSlideIndex = 0;
4367
+ this.slideCount = content.slides.length;
4368
+ await this.refreshHistoryState();
4369
+ }
4370
+ async switchTo(index) {
4371
+ await this.post('switchSlide', { index });
4372
+ this.currentSlideIndex = index;
4373
+ }
4374
+ async addSlide(command, payload) {
4375
+ const result = await this.post(command, payload);
4376
+ this.slideCount = Math.max(this.slideCount + 1, result.index + 1);
4377
+ this.currentSlideIndex = result.index;
4378
+ await this.refreshHistoryState();
4379
+ return result;
4380
+ }
4381
+ async refreshHistoryState() {
4382
+ const state = await this.post('getHistoryState', {});
4383
+ this.historyCount = state.count;
4384
+ this.historyCurrentIndex = state.currentIndex;
4385
+ return state;
4386
+ }
4387
+ async handleSaveRequest(message, sourceWindow) {
4388
+ const respond = (payload) => {
4389
+ sourceWindow.postMessage(payload, '*');
4390
+ };
4391
+ try {
4392
+ const result = await this.resolveSaveResult(message.content);
4393
+ respond({
4394
+ type: 'response',
4395
+ requestId: message.requestId,
4396
+ success: true,
4397
+ result,
4398
+ });
4399
+ }
4400
+ catch (error) {
4401
+ respond({
4402
+ type: 'response',
4403
+ requestId: message.requestId,
4404
+ success: false,
4405
+ error: {
4406
+ code: EMBED_ERROR_CODES.UNKNOWN_ERROR,
4407
+ message: error instanceof Error ? error.message : String(error),
4408
+ },
4409
+ });
4410
+ }
4411
+ }
4412
+ async resolveSaveResult(content) {
4413
+ const requestEvent = {
4414
+ type: 'save',
4415
+ stage: 'request',
4416
+ content,
4417
+ };
4418
+ for (const listener of this.getListeners('save')) {
4419
+ const result = await listener(requestEvent);
4420
+ if (result && typeof result === 'object' && 'status' in result) {
4421
+ return result;
4422
+ }
4423
+ }
4424
+ return {
4425
+ status: 'error',
4426
+ message: '宿主未配置 save 事件处理器',
4427
+ };
4428
+ }
4429
+ }
4430
+
4431
+ const WHITELIST_CHECK_BASE_URL = 'https://open.dajiaoai.com/console';
4432
+ const WHITELIST_CHECK_PATH = '/api/whitelist/check';
4433
+ async function checkPresentationWhitelist(options) {
4434
+ const appId = options.appId?.trim() ?? '';
4435
+ const host = window.location.host.trim();
4436
+ const url = new URL(`${normalizeBaseUrl(WHITELIST_CHECK_BASE_URL)}${WHITELIST_CHECK_PATH}`);
4437
+ url.searchParams.set('appId', appId);
4438
+ url.searchParams.set('host', host);
4439
+ let matched = false;
4440
+ try {
4441
+ const response = await fetch(url.toString(), {
4442
+ method: 'GET',
4443
+ cache: 'no-store',
4444
+ });
4445
+ if (response.ok) {
4446
+ const payload = (await response.json());
4447
+ matched = payload.matched === true;
4448
+ }
4449
+ }
4450
+ catch {
4451
+ matched = false;
4452
+ }
4453
+ if (matched) {
4454
+ return null;
4455
+ }
4456
+ return new AlgeoError(`白名单校验未通过,appId="${appId || '(empty)'}",host="${host || '(empty)'}"。`, EMBED_ERROR_CODES.BAD_REQUEST);
4457
+ }
4458
+ async function createEditorInstance(container, options = {}, baseUrl) {
4459
+ const editor = new EmbeddedEditor(container);
4460
+ await editor.initialize(options, baseUrl);
4461
+ return editor;
4462
+ }
4463
+ async function createPresentationInstance(container, options = {}, baseUrl) {
4464
+ const presentation = new EmbeddedPresentation(container);
4465
+ await presentation.initialize(options, baseUrl);
4466
+ const whitelistError = await checkPresentationWhitelist(options);
4467
+ if (whitelistError) {
4468
+ presentation.setWhitelistError(whitelistError);
4469
+ }
4470
+ return presentation;
4471
+ }
4472
+ function createEmbeddedInstance(container, options) {
4473
+ if (options.mode === 'editor') {
4474
+ return createEditorInstance(container, options.editor, options.baseUrl);
4475
+ }
4476
+ return createPresentationInstance(container, options.presentation, options.baseUrl);
4477
+ }
4478
+ class AlgeoSdk {
4479
+ constructor() { }
4480
+ static async create(container, options) {
4481
+ return createEmbeddedInstance(container, options);
4482
+ }
4483
+ static async createEditor(container, options = {}) {
4484
+ return createEditor(container, options);
4485
+ }
4486
+ static async createPresentation(container, options = {}) {
4487
+ return createPresentation(container, options);
4488
+ }
4489
+ }
4490
+ async function create(container, options) {
4491
+ return createEmbeddedInstance(container, options);
4492
+ }
4493
+ async function createEditor(container, options = {}) {
4494
+ return createEditorInstance(container, options);
4495
+ }
4496
+ async function createPresentation(container, options = {}) {
4497
+ return createPresentationInstance(container, options);
4068
4498
  }
4069
4499
 
4070
4500
  exports.AlgeoError = AlgeoError;
4071
4501
  exports.AlgeoSdk = AlgeoSdk;
4072
4502
  exports.EMBED_ERROR_CODES = EMBED_ERROR_CODES;
4503
+ exports.EmbeddedEditor = EmbeddedEditor;
4504
+ exports.EmbeddedPresentation = EmbeddedPresentation;
4073
4505
  exports.VERSION = VERSION;
4506
+ exports.create = create;
4507
+ exports.createEditor = createEditor;
4508
+ exports.createPresentation = createPresentation;
4074
4509
 
4075
4510
  }));