@dajiaoai/algeo-sdk 2.16.0 → 2.16.2

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.
@@ -1,3 +1,31 @@
1
+ const SDK_LOGO = `<svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" fill="none" viewBox="0 0 36 36">
2
+ <g clip-path="url(#a)">
3
+ <path stroke="#A668B8" stroke-width="3.819" d="m30.261 29.85-5.368-23.9c-.163-.723-1.126-.875-1.503-.237l-9.082 15.349a.818.818 0 0 0 .288 1.12l14.45 8.551a.818.818 0 0 0 1.215-.884Z"/>
4
+ <path stroke="#01B88B" stroke-width="3.819" d="M5.335 11.746V30.12c6.605.315 14.644.647 15.468-8.963.165-1.922-.34-4.035-.824-5.9-.851-3.276-3.854-5.913-12.59-5.913"/>
5
+ <path fill="#A668B8" d="m16.483 13.62 2.182-3.682 3.273 1.91-3.682 6.137-1.773-4.364Z"/>
6
+ </g>
7
+ <defs>
8
+ <clipPath id="a">
9
+ <path fill="#fff" d="M0 0h36v36H0z"/>
10
+ </clipPath>
11
+ </defs>
12
+ </svg>`;
13
+ const BADGE_STYLE = 'background-color: #f1faf7; background-position: 9px center;' +
14
+ 'background-repeat: no-repeat; background-size: 24px 24px;' +
15
+ 'border: 1px solid #9cd4c5; border-radius: 4px; padding: 6px 12px 6px 42px;' +
16
+ 'color: #123e35; font-size: 14px; font-weight: bold;';
17
+ const TITLE_STYLE = 'background-color: #174e42; color: #fff; font-weight: bold; padding: 2px 8px;';
18
+ const DIVIDER = '+------------------------------------------------------+';
19
+ function logSdk(...entry) {
20
+ if (entry[0] === 'debug') {
21
+ const [, message, ...details] = entry;
22
+ console.debug('[Algeo SDK]', message, ...details);
23
+ return;
24
+ }
25
+ const [, { title, lines }] = entry;
26
+ console.info(`%cALGEO SDK / 大角几何%c\n\n${DIVIDER}\n| %c%s%c\n%s\n${DIVIDER}`, `background-image: url(data:image/svg+xml;base64,${btoa(SDK_LOGO)});${BADGE_STYLE}`, '', TITLE_STYLE, title, '', ['|', ...lines.map((line) => (line ? `| ${line}` : '|'))].join('\n'));
27
+ }
28
+
1
29
  var util;
2
30
  (function (util) {
3
31
  util.assertEqual = (_) => { };
@@ -4009,11 +4037,42 @@ function convertToLatest(content) {
4009
4037
  }
4010
4038
 
4011
4039
  /** SDK 版本号,构建时由 rollup 注入 */
4012
- const VERSION = '2.16.0';
4040
+ const VERSION = '2.16.2';
4013
4041
  const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
4014
4042
  const DEFAULT_PRESENTATION_PATH = '/embed/present';
4015
4043
  const DEFAULT_EDITOR_PATH = '/embed/edit';
4016
4044
  const EMBED_TIMEOUT_MS = 30000;
4045
+ function isLocalFileHost() {
4046
+ return typeof window !== 'undefined' && window.location.protocol === 'file:';
4047
+ }
4048
+ function isLocalLoopbackHost() {
4049
+ if (typeof window === 'undefined')
4050
+ return false;
4051
+ const { protocol, hostname } = window.location;
4052
+ return ((protocol === 'http:' || protocol === 'https:') &&
4053
+ ['localhost', '127.0.0.1'].includes(hostname));
4054
+ }
4055
+ function readEmbedAppId(auth) {
4056
+ const appId = auth?.appId?.trim() ?? '';
4057
+ if (!appId) {
4058
+ logSdk('notice', {
4059
+ title: '未配置应用 ID(auth.appId)',
4060
+ lines: [
4061
+ '注册应用 > 配置域名白名单 > 填写 auth.appId',
4062
+ '开放平台 https://open.dajiaoai.com/console',
4063
+ '使用许可 https://open.dajiaoai.com/sdk/2/auth',
4064
+ '',
4065
+ '版权 © 大角几何',
4066
+ ],
4067
+ });
4068
+ }
4069
+ return appId;
4070
+ }
4071
+ function requireHttpHostForAppId(appId) {
4072
+ if (appId && isLocalFileHost()) {
4073
+ throw new AlgeoError('file:// 没有可校验的宿主域名。请通过本地 HTTP 服务打开页面,将该主机加入应用白名单后重试。', EMBED_ERROR_CODES.BAD_REQUEST);
4074
+ }
4075
+ }
4017
4076
  let requestIdCounter = 0;
4018
4077
  function generateRequestId() {
4019
4078
  return `req-${Date.now()}-${++requestIdCounter}`;
@@ -4128,12 +4187,24 @@ function buildEmbedSrc(options) {
4128
4187
  const baseUrl = normalizeBaseUrl(options.baseUrl ?? DEFAULT_EMBED_BASE);
4129
4188
  const mode = normalizeMode(options.mode);
4130
4189
  const path = getEmbedPath(mode);
4131
- const appId = options.auth?.appId?.trim() ?? '';
4190
+ const appId = options.auth?.appId?.trim();
4191
+ const localFile = !appId && isLocalFileHost();
4192
+ const localLoopback = !appId && isLocalLoopbackHost();
4132
4193
  const initialId = options.initialId?.trim();
4133
- const src = `${baseUrl}${path}/${encodeURIComponent(appId)}${initialId ? `/${encodeURIComponent(initialId)}` : ''}`;
4134
- return mode === 'editor' && options.cameraMode
4135
- ? `${src}?cameraMode=${encodeURIComponent(options.cameraMode)}`
4136
- : src;
4194
+ const initialPath = initialId ? `/${encodeURIComponent(initialId)}` : '';
4195
+ const routeAppId = appId || (localFile || localLoopback ? 'local' : 'public');
4196
+ const src = `${baseUrl}${path}/${encodeURIComponent(routeAppId)}${initialPath}`;
4197
+ const params = new URLSearchParams();
4198
+ if (mode === 'editor' && options.cameraMode) {
4199
+ params.set('cameraMode', options.cameraMode);
4200
+ }
4201
+ if (mode === 'editor' && localFile) {
4202
+ params.set('localFile', '1');
4203
+ }
4204
+ if (mode === 'editor' && localLoopback) {
4205
+ params.set('localLoopback', '1');
4206
+ }
4207
+ return params.size ? `${src}?${params}` : src;
4137
4208
  }
4138
4209
  const MASTER_TEMPLATE_OPERATIONS = [
4139
4210
  'listSystem',
@@ -4232,6 +4303,7 @@ class EmbeddedTarget {
4232
4303
  ...options,
4233
4304
  mode: this.embedMode,
4234
4305
  });
4306
+ const iframeOrigin = new URL(src, window.location.href).origin;
4235
4307
  let settled = false;
4236
4308
  let initTimeoutId;
4237
4309
  const finalizeFailure = (error) => {
@@ -4278,6 +4350,25 @@ class EmbeddedTarget {
4278
4350
  return;
4279
4351
  }
4280
4352
  const { data } = e;
4353
+ if (data?.type === 'localFileHostCheck' &&
4354
+ typeof data.requestId === 'string' &&
4355
+ isLocalFileHost()) {
4356
+ iframe.contentWindow?.postMessage({ type: 'localFileHostConfirmed', requestId: data.requestId }, '*');
4357
+ return;
4358
+ }
4359
+ if (data?.type === 'localLoopbackHostCheck' &&
4360
+ typeof data.requestId === 'string' &&
4361
+ !options.auth?.appId?.trim() &&
4362
+ isLocalLoopbackHost()) {
4363
+ iframe.contentWindow?.postMessage({ type: 'localLoopbackHostConfirmed', requestId: data.requestId }, iframeOrigin);
4364
+ return;
4365
+ }
4366
+ if (this.embedMode === 'editor' &&
4367
+ e.origin === iframeOrigin &&
4368
+ data?.type === 'embedEditAuthorizationFailed') {
4369
+ finalizeFailure(new AlgeoError('白名单校验未通过。请在开放平台将当前主机加入应用白名单后重试。', EMBED_ERROR_CODES.BAD_REQUEST));
4370
+ return;
4371
+ }
4281
4372
  if (isReadyMessage(data)) {
4282
4373
  if (this._ready) {
4283
4374
  return;
@@ -4410,6 +4501,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4410
4501
  super(container, 'presentation');
4411
4502
  this.currentSlideIndex = 0;
4412
4503
  this.slideCount = 0;
4504
+ this.appId = '';
4413
4505
  this.uiConfig = {
4414
4506
  logo: true,
4415
4507
  slidePanel: true,
@@ -4419,19 +4511,30 @@ class EmbeddedPresentation extends EmbeddedTarget {
4419
4511
  this.mode = {
4420
4512
  getUiConfig: () => ({ ...this.uiConfig }),
4421
4513
  setUiConfig: async (config) => {
4514
+ await this.ensureWhitelistAccess('setUiConfig');
4422
4515
  await this.post('setUiConfig', { config });
4423
4516
  this.uiConfig = {
4424
4517
  ...this.uiConfig,
4425
4518
  ...config,
4426
4519
  };
4427
4520
  },
4428
- setMasterTemplate: (template) => {
4429
- this.ensureWhitelistAccess('setMasterTemplate');
4521
+ setMasterTemplate: async (template) => {
4522
+ await this.ensureWhitelistAccess('setMasterTemplate');
4430
4523
  return this.post('setMasterTemplate', { template });
4431
4524
  },
4432
4525
  };
4433
4526
  }
4434
4527
  async initialize(options = {}, baseUrl) {
4528
+ const appId = readEmbedAppId(options.auth);
4529
+ if (!this.iframe) {
4530
+ this.appId = appId;
4531
+ this.whitelistCheck = undefined;
4532
+ }
4533
+ // 未配置应用的部署页面按默认样式展示公开分享。
4534
+ if (!appId && !isLocalFileHost() && !isLocalLoopbackHost()) {
4535
+ await this.init({ baseUrl, initialId: options.shareId });
4536
+ return;
4537
+ }
4435
4538
  this.uiConfig = {
4436
4539
  logo: true,
4437
4540
  slidePanel: true,
@@ -4450,7 +4553,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4450
4553
  if (options.fonts) {
4451
4554
  await this.post('setFontConfig', { config: options.fonts });
4452
4555
  }
4453
- await this.mode.setUiConfig(this.uiConfig);
4556
+ await this.post('setUiConfig', { config: this.uiConfig });
4454
4557
  }
4455
4558
  acceptsEventMessage() {
4456
4559
  return false;
@@ -4458,16 +4561,44 @@ class EmbeddedPresentation extends EmbeddedTarget {
4458
4561
  setWhitelistError(error) {
4459
4562
  this.whitelistError = error;
4460
4563
  }
4461
- ensureWhitelistAccess(methodName) {
4462
- if (!this.whitelistError) {
4463
- return;
4564
+ async checkWhitelist(appId) {
4565
+ if (!appId) {
4566
+ if (isLocalFileHost() || isLocalLoopbackHost())
4567
+ return;
4568
+ throw new AlgeoError('正式接入演示模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
4569
+ }
4570
+ requireHttpHostForAppId(appId);
4571
+ const host = window.location.hostname;
4572
+ const url = new URL('https://open.dajiaoai.com/console/api/whitelist/check');
4573
+ url.searchParams.set('appId', appId);
4574
+ url.searchParams.set('host', host);
4575
+ try {
4576
+ const response = await fetch(url.toString(), { cache: 'no-store' });
4577
+ if (response.ok && (await response.json()).matched === true)
4578
+ return;
4579
+ }
4580
+ catch {
4581
+ // 网络或响应解析失败时按鉴权失败处理。
4582
+ }
4583
+ throw new AlgeoError(`白名单校验未通过,appId="${appId}",host="${host}"。请在开放平台将该主机加入应用白名单后重试。`, EMBED_ERROR_CODES.BAD_REQUEST);
4584
+ }
4585
+ async ensureWhitelistAccess(methodName) {
4586
+ try {
4587
+ if (this.whitelistError)
4588
+ throw this.whitelistError;
4589
+ this.whitelistCheck ?? (this.whitelistCheck = this.checkWhitelist(this.appId));
4590
+ await this.whitelistCheck;
4591
+ }
4592
+ catch (cause) {
4593
+ if (!(cause instanceof AlgeoError))
4594
+ throw cause;
4595
+ const error = new AlgeoError(`演示模式调用 ${methodName} 被白名单限制。${cause.message}`, cause.code, cause.details);
4596
+ console.error(error);
4597
+ throw error;
4464
4598
  }
4465
- const error = new AlgeoError(`演示模式调用 ${methodName} 被白名单限制。${this.whitelistError.message}`, this.whitelistError.code, this.whitelistError.details);
4466
- console.error(error);
4467
- throw error;
4468
4599
  }
4469
4600
  async loadShareById(id) {
4470
- this.ensureWhitelistAccess('loadShareById');
4601
+ await this.ensureWhitelistAccess('loadShareById');
4471
4602
  const result = await this.post('loadShareById', {
4472
4603
  id,
4473
4604
  });
@@ -4477,7 +4608,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4477
4608
  return result;
4478
4609
  }
4479
4610
  async loadFile(content) {
4480
- this.ensureWhitelistAccess('loadFile');
4611
+ await this.ensureWhitelistAccess('loadFile');
4481
4612
  const latestContent = convertToLatest(content);
4482
4613
  const result = await this.post('loadContent', {
4483
4614
  content: latestContent,
@@ -4488,19 +4619,19 @@ class EmbeddedPresentation extends EmbeddedTarget {
4488
4619
  return result;
4489
4620
  }
4490
4621
  async switchSlide(index) {
4491
- this.ensureWhitelistAccess('switchSlide');
4622
+ await this.ensureWhitelistAccess('switchSlide');
4492
4623
  const result = await this.post('switchSlide', { index });
4493
4624
  this.currentSlideIndex = index;
4494
4625
  return result;
4495
4626
  }
4496
4627
  async getSlideCount() {
4497
- this.ensureWhitelistAccess('getSlideCount');
4628
+ await this.ensureWhitelistAccess('getSlideCount');
4498
4629
  const result = await this.post('getSlideCount', {});
4499
4630
  this.slideCount = result.count;
4500
4631
  return result;
4501
4632
  }
4502
4633
  async repl(command) {
4503
- this.ensureWhitelistAccess('repl');
4634
+ await this.ensureWhitelistAccess('repl');
4504
4635
  return this.post('repl', { command });
4505
4636
  }
4506
4637
  }
@@ -4688,7 +4819,9 @@ class EmbeddedEditor extends EmbeddedTarget {
4688
4819
  this.historyCurrentIndex = state.historyCurrentIndex;
4689
4820
  }
4690
4821
  async initialize(options = {}, baseUrl) {
4691
- if (!options.auth?.appId?.trim()) {
4822
+ const appId = readEmbedAppId(options.auth);
4823
+ requireHttpHostForAppId(appId);
4824
+ if (!appId && !isLocalFileHost() && !isLocalLoopbackHost()) {
4692
4825
  throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
4693
4826
  }
4694
4827
  this.uiConfig = options.ui ? { ...options.ui } : {};
@@ -5198,33 +5331,6 @@ class EmbeddedEditor extends EmbeddedTarget {
5198
5331
  }
5199
5332
  }
5200
5333
 
5201
- const WHITELIST_CHECK_BASE_URL = 'https://open.dajiaoai.com/console';
5202
- const WHITELIST_CHECK_PATH = '/api/whitelist/check';
5203
- async function checkPresentationWhitelist(options) {
5204
- const appId = options.auth?.appId?.trim() ?? '';
5205
- const host = window.location.hostname.trim();
5206
- const url = new URL(`${normalizeBaseUrl(WHITELIST_CHECK_BASE_URL)}${WHITELIST_CHECK_PATH}`);
5207
- url.searchParams.set('appId', appId);
5208
- url.searchParams.set('host', host);
5209
- let matched = false;
5210
- try {
5211
- const response = await fetch(url.toString(), {
5212
- method: 'GET',
5213
- cache: 'no-store',
5214
- });
5215
- if (response.ok) {
5216
- const payload = (await response.json());
5217
- matched = payload.matched === true;
5218
- }
5219
- }
5220
- catch {
5221
- matched = false;
5222
- }
5223
- if (matched) {
5224
- return null;
5225
- }
5226
- return new AlgeoError(`白名单校验未通过,appId="${appId || '(empty)'}",host="${host || '(empty)'}"。`, EMBED_ERROR_CODES.BAD_REQUEST);
5227
- }
5228
5334
  async function createEditorInstance(container, options = {}, baseUrl) {
5229
5335
  const editor = new EmbeddedEditor(container);
5230
5336
  await editor.initialize(options, baseUrl);
@@ -5233,10 +5339,6 @@ async function createEditorInstance(container, options = {}, baseUrl) {
5233
5339
  async function createPresentationInstance(container, options = {}, baseUrl) {
5234
5340
  const presentation = new EmbeddedPresentation(container);
5235
5341
  await presentation.initialize(options, baseUrl);
5236
- const whitelistError = await checkPresentationWhitelist(options);
5237
- if (whitelistError) {
5238
- presentation.setWhitelistError(whitelistError);
5239
- }
5240
5342
  return presentation;
5241
5343
  }
5242
5344
  function createEmbeddedInstance(container, options) {