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