@atomm-developer/generator-sdk 1.0.4 → 1.0.6

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/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export declare type AuthChangeCallback = (status: AuthStatus) => void;
5
5
 
6
6
  /**
7
7
  * Auth 模块
8
- * 对外暴露:getStatus / login / logout / onChange / getToken
8
+ * 对外暴露:getStatus / login / logout / onChange / getToken / syncToken
9
9
  */
10
10
  declare class AuthModule {
11
11
  private readonly appKey;
@@ -23,6 +23,10 @@ declare class AuthModule {
23
23
  * 未登录时返回空字符串
24
24
  */
25
25
  getToken(): string;
26
+ /**
27
+ * 外部传入 token,同步到本地缓存并刷新登录状态
28
+ */
29
+ syncToken(token: string): Promise<AuthStatus>;
26
30
  /**
27
31
  * 弹出登录弹窗
28
32
  * 登录成功后 resolve UserInfo,用户取消则 reject
@@ -298,12 +302,35 @@ declare class CreditsModule {
298
302
  getCachedBalance(): number;
299
303
  }
300
304
 
305
+ /**
306
+ * 通用导出数据
307
+ * 当画布不是 HTMLCanvasElement 时,开发者通过 getExportData 返回此类型
308
+ */
309
+ export declare type ExportData = {
310
+ type: 'canvas';
311
+ canvas: HTMLCanvasElement;
312
+ } | {
313
+ type: 'blob';
314
+ blob: Blob;
315
+ } | {
316
+ type: 'dataUrl';
317
+ dataUrl: string;
318
+ } | {
319
+ type: 'url';
320
+ url: string;
321
+ } | {
322
+ type: 'svg';
323
+ svgString: string;
324
+ };
325
+
301
326
  /** export.download() 参数 */
302
327
  export declare interface ExportDownloadOptions {
303
- /** 文件名(可选,默认 'export-{timestamp}.png') */
328
+ /** 文件名(可选,默认 'export-{timestamp}.{format}') */
304
329
  fileName?: string;
305
330
  /** 导出格式(可选,默认 'png') */
306
- format?: 'png' | 'svg';
331
+ format?: ExportFormat;
332
+ /** JPEG/WebP 质量 0-1(可选,默认 0.92) */
333
+ quality?: number;
307
334
  }
308
335
 
309
336
  /** export.download() 返回值 */
@@ -312,6 +339,9 @@ export declare interface ExportDownloadResult {
312
339
  fileName: string;
313
340
  }
314
341
 
342
+ /** 支持的导出格式 */
343
+ export declare type ExportFormat = 'png' | 'jpeg' | 'webp' | 'svg';
344
+
315
345
  /**
316
346
  * Export 模块
317
347
  * 对外暴露:register / download / openInStudio
@@ -330,26 +360,51 @@ declare class ExportModule {
330
360
  register(provider: ExportProvider): void;
331
361
  /**
332
362
  * 下载图片到本地
333
- * 内部流程:getExportCanvas('download') toBlob 触发浏览器下载
363
+ * 支持 png / jpeg / webp / svg 格式
364
+ *
365
+ * 内部流程:
366
+ * 1. 优先调用 getExportData(purpose, format) 获取导出数据
367
+ * 2. 若未实现则回退到 getExportCanvas(purpose) → canvas.toBlob
368
+ * 3. 生成 Blob → 触发浏览器下载
334
369
  */
335
370
  download(options?: ExportDownloadOptions): Promise<ExportDownloadResult>;
336
371
  /**
337
372
  * 打开到 xTool Studio
338
- * 内部流程:
339
- * - 传入图片 URL:直接拉起 Studio
340
- * - 传入图片数据:上传 OSS 调用 xtool:// 协议
341
- * - 未传参数:getExportCanvas('studio') toDataURL 上传 OSS 调用 xtool:// 协议
373
+ *
374
+ * 支持两种调用方式(向后兼容):
375
+ * - openInStudio(source) — 直接传图片源(URL / data URL / Blob / File)
376
+ * - openInStudio({ source, format }) options 对象,可指定导入格式
377
+ * - openInStudio() — 无参数,通过 provider 获取数据
378
+ *
379
+ * 当未传 source 时,根据 format 参数通过 provider 获取对应格式的数据上传到 OSS
380
+ */
381
+ openInStudio(sourceOrOptions?: ExportOpenInStudioSource | ExportOpenInStudioOptions): Promise<ExportOpenInStudioResult>;
382
+ /**
383
+ * 兼容解析 openInStudio 参数:
384
+ * - 无参数 → { source: undefined, format: 'png' }
385
+ * - string / Blob / File → { source, format: 'png' }
386
+ * - { source?, format? } → 直接使用
342
387
  */
343
- openInStudio(source?: ExportOpenInStudioSource): Promise<ExportOpenInStudioResult>;
388
+ private _normalizeStudioArgs;
344
389
  /**
345
- * 获取导出 Canvas 并校验
390
+ * 获取导出 Blob,统一处理 getExportData / getExportCanvas 两种路径
346
391
  */
347
- private _getCanvas;
392
+ private _getBlob;
393
+ /** 尝试通过 getExportData 获取导出数据 */
394
+ private _tryGetExportData;
395
+ /** 尝试通过 getExportCanvas 获取画布 */
396
+ private _tryGetCanvas;
397
+ /** 确保 provider 已注册 */
398
+ private _ensureProvider;
399
+ /** 将 ExportData 转换为 Blob */
400
+ private _exportDataToBlob;
401
+ /** Canvas → Blob,正确处理各格式 */
402
+ private _canvasToBlob;
348
403
  /**
349
404
  * 解析 openInStudio 的输入来源:
350
405
  * - URL 直传给 Studio
351
406
  * - data URL / Blob / File 先上传 OSS
352
- * - 未传时回退到 provider canvas
407
+ * - 未传时通过 provider 按 format 获取数据
353
408
  */
354
409
  private _resolveStudioAsset;
355
410
  /** 获取或懒初始化 Uploader 实例 */
@@ -358,10 +413,16 @@ declare class ExportModule {
358
413
  private _uploadFile;
359
414
  /** 通过 xtool:// 协议打开 Studio */
360
415
  private _openStudioProtocol;
416
+ /** 导出格式 → MIME 类型(仅位图格式) */
417
+ private _formatToMimeType;
418
+ /** data URL → Blob */
419
+ private _dataUrlToBlob;
361
420
  /** data URL → File 对象 */
362
421
  private _dataUrlToFile;
363
422
  /** Blob → File 对象 */
364
423
  private _blobToFile;
424
+ /** 从 data URL 推断文件扩展名 */
425
+ private _detectDataUrlExtension;
365
426
  /** 判断是否为 data URL */
366
427
  private _isDataUrl;
367
428
  /** 判断是否为可直接交给 Studio 的远程 URL */
@@ -370,24 +431,48 @@ declare class ExportModule {
370
431
  private _mimeTypeToExtension;
371
432
  }
372
433
 
434
+ /**
435
+ * export.openInStudio() 参数
436
+ * 支持两种调用方式(向后兼容):
437
+ * - openInStudio(source) — 直接传图片源
438
+ * - openInStudio({ source, format }) — 传 options 对象
439
+ */
440
+ export declare interface ExportOpenInStudioOptions {
441
+ /** 图片来源:URL / data URL / Blob / File,不传时从 provider 获取 */
442
+ source?: ExportOpenInStudioSource;
443
+ /** 导入 Studio 的格式(不传 source 时生效),默认 'png' */
444
+ format?: ExportFormat;
445
+ }
446
+
373
447
  /** export.openInStudio() 返回值 */
374
448
  export declare interface ExportOpenInStudioResult {
375
449
  success: boolean;
376
450
  }
377
451
 
378
- /** export.openInStudio() 输入源 */
452
+ /** export.openInStudio() 直接传入的图片源(向后兼容) */
379
453
  export declare type ExportOpenInStudioSource = string | Blob | File;
380
454
 
381
455
  /**
382
456
  * 导出能力提供者
383
457
  * 开发者实现此接口并通过 sdk.export.register() 注册
458
+ *
459
+ * 提供两种方式(优先级:getExportData > getExportCanvas):
460
+ * - getExportData:灵活方式,适合非 canvas 场景(SVG/DOM/纯数据)
461
+ * - getExportCanvas:传统方式,直接返回 HTMLCanvasElement
462
+ *
463
+ * 至少实现其中一个,否则调用 download/openInStudio 时会抛出错误
384
464
  */
385
465
  export declare interface ExportProvider {
386
466
  /**
387
- * 获取导出用的 Canvas
388
- * @param purpose 用途:download=下载 studio=打开到Studio cover=封面
467
+ * 获取导出数据(灵活方式,优先级高于 getExportCanvas)
468
+ * 适用于非 canvas 场景:SVG 编辑器、DOM 截图、纯数据生成等
389
469
  */
390
- getExportCanvas: (purpose: ExportPurpose) => HTMLCanvasElement | Promise<HTMLCanvasElement | null> | null;
470
+ getExportData?: (purpose: ExportPurpose, format: ExportFormat) => ExportData | Promise<ExportData | null> | null;
471
+ /**
472
+ * 获取导出用的 Canvas(传统方式)
473
+ * 当 getExportData 未实现或返回 null 时回退到此方法
474
+ */
475
+ getExportCanvas?: (purpose: ExportPurpose) => HTMLCanvasElement | Promise<HTMLCanvasElement | null> | null;
391
476
  /** 自定义文件名生成器(可选) */
392
477
  getFileName?: (purpose: ExportPurpose) => string;
393
478
  }
@@ -396,6 +481,8 @@ export declare interface ExportProvider {
396
481
  export declare type ExportPurpose = 'download' | 'studio' | 'cover';
397
482
 
398
483
  export declare class GeneratorSDK {
484
+ /** 最近一次初始化使用的 appKey,供全局场景读取 */
485
+ private static lastInitializedAppKey;
399
486
  /** 登录/退出/用户信息 */
400
487
  readonly auth: AuthModule;
401
488
  /** 云保存/恢复/删除 */
@@ -413,6 +500,10 @@ export declare class GeneratorSDK {
413
500
  private readonly appKey;
414
501
  private readonly env;
415
502
  private constructor();
503
+ /**
504
+ * 返回当前 SDK 实例绑定的 appKey
505
+ */
506
+ getAppKey(): string;
416
507
  /**
417
508
  * 初始化 SDK,返回 SDK 实例
418
509
  *
@@ -424,6 +515,10 @@ export declare class GeneratorSDK {
424
515
  * ```
425
516
  */
426
517
  static init(options: SdkInitOptions): GeneratorSDK;
518
+ /**
519
+ * 返回最近一次初始化使用的 appKey
520
+ */
521
+ static getAppKey(): string | null;
427
522
  /**
428
523
  * 清除实例缓存(测试场景使用)
429
524
  */
package/dist/index.es.js CHANGED
@@ -1,4 +1,4 @@
1
- import { G, S, T, w } from "./index-qYdFih-w.js";
1
+ import { G, S, T, w } from "./index-DP-Q2Dkh.js";
2
2
  export {
3
3
  G as GeneratorSDK,
4
4
  S as SdkError,