@dajiaoai/algeo-sdk 1.0.1 → 1.1.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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -11,7 +11,7 @@
11
11
  npm install @dajiaoai/algeo-sdk
12
12
 
13
13
  # 安装指定版本(推荐生产环境锁定版本)
14
- npm install @dajiaoai/algeo-sdk@1.0.0
14
+ npm install @dajiaoai/algeo-sdk@1.1.0
15
15
  ```
16
16
 
17
17
  在 `package.json` 中:
@@ -19,7 +19,7 @@ npm install @dajiaoai/algeo-sdk@1.0.0
19
19
  ```json
20
20
  {
21
21
  "dependencies": {
22
- "@dajiaoai/algeo-sdk": "1.0.0" // 精确版本,生产推荐
22
+ "@dajiaoai/algeo-sdk": "1.1.0" // 精确版本,生产推荐
23
23
  }
24
24
  }
25
25
  ```
@@ -29,34 +29,24 @@ npm install @dajiaoai/algeo-sdk@1.0.0
29
29
  ```json
30
30
  {
31
31
  "dependencies": {
32
- "@dajiaoai/algeo-sdk": "^1.0.0" // 兼容 1.x 的更新
32
+ "@dajiaoai/algeo-sdk": "^1.1.0" // 兼容 1.x 的更新
33
33
  }
34
34
  }
35
35
  ```
36
36
 
37
37
  ### CDN
38
38
 
39
- **api.dajiaoai.com(推荐)**:
40
-
41
- ```html
42
- <!-- 引用具体版本(推荐生产环境) -->
43
- <script src="https://api.dajiaoai.com/js/algeo-sdk@1.0.0/algeo-sdk.umd.js"></script>
44
-
45
- <!-- 引用最新版本 -->
46
- <script src="https://api.dajiaoai.com/js/algeo-sdk@latest/algeo-sdk.umd.js"></script>
47
- ```
48
-
49
39
  **unpkg**:
50
40
 
51
41
  ```html
52
- <script src="https://unpkg.com/@dajiaoai/algeo-sdk@1.0.0/dist/algeo-sdk.umd.js"></script>
42
+ <script src="https://unpkg.com/@dajiaoai/algeo-sdk@1.1.0/dist/algeo-sdk.umd.js"></script>
53
43
  <script src="https://unpkg.com/@dajiaoai/algeo-sdk@latest/dist/algeo-sdk.umd.js"></script>
54
44
  ```
55
45
 
56
46
  **jsDelivr**:
57
47
 
58
48
  ```html
59
- <script src="https://cdn.jsdelivr.net/npm/@dajiaoai/algeo-sdk@1.0.0/dist/algeo-sdk.umd.js"></script>
49
+ <script src="https://cdn.jsdelivr.net/npm/@dajiaoai/algeo-sdk@1.1.0/dist/algeo-sdk.umd.js"></script>
60
50
  <script src="https://cdn.jsdelivr.net/npm/@dajiaoai/algeo-sdk@latest/dist/algeo-sdk.umd.js"></script>
61
51
  ```
62
52
 
@@ -70,7 +60,7 @@ npm install @dajiaoai/algeo-sdk@1.0.0
70
60
  import { AlgeoSdk } from '@dajiaoai/algeo-sdk';
71
61
 
72
62
  const container = document.getElementById('algeo-container');
73
- const sdk = new AlgeoSdk(container, {
63
+ const sdk = await AlgeoSdk.create(container, {
74
64
  baseUrl: 'https://dajiaoai.com',
75
65
  initialId: 'E8NHN7OP', // 可选,初始加载的分享 ID
76
66
  });
@@ -96,21 +86,22 @@ sdk.switchSlide(1).then(() => console.log('切换画板'));
96
86
  直接在 iframe 的 `src` 中指定分享 ID,无需引入 SDK:
97
87
 
98
88
  ```html
99
- <iframe id="algeo-embed" src="https://dajiaoai.com/e/E8NHN7OP" allow="fullscreen"></iframe>
100
- <script>
101
- window.addEventListener('message', (e) => {
102
- if (e.data?.type === 'ready') console.log('就绪', e.data.version);
103
- });
104
- </script>
89
+ <iframe
90
+ id="algeo-embed"
91
+ src="https://dajiaoai.com/e/E8NHN7OP"
92
+ allow="fullscreen"
93
+ ></iframe>
105
94
  ```
106
95
 
107
- 如需动态加载、切换画板等能力,请使用方式一(SDK)。详见 [postMessage 协议](#postmessage-协议)。
96
+ 如需动态加载、切换画板等能力,请使用方式一(SDK)。详见 [API](#API)。
108
97
 
109
98
  > **全屏支持**:建议在 iframe 上添加 `allow="fullscreen"`,以便内嵌画板使用全屏功能。SDK 方式创建的 iframe 已自动包含此属性。
110
99
 
111
100
  ## API
112
101
 
113
- ### `VERSION`
102
+ ### 常量
103
+
104
+ #### `VERSION`
114
105
 
115
106
  SDK 版本号字符串,构建时注入,可用于运行时校验:
116
107
 
@@ -119,49 +110,151 @@ import { VERSION } from '@dajiaoai/algeo-sdk';
119
110
  console.log('Algeo SDK version:', VERSION);
120
111
  ```
121
112
 
122
- ### `new AlgeoSdk(container, options?)`
113
+ ---
114
+
115
+ ### 类 `AlgeoSdk`
116
+
117
+ #### `AlgeoSdk.create(container, options?): Promise<AlgeoSdk>`
118
+
119
+ 异步创建并初始化 SDK 实例。在 iframe 加载完成并收到 ready 消息后 resolve。
120
+
121
+ | 参数 | 类型 | 说明 |
122
+ | ----------- | ----------------- | ----------------------- |
123
+ | `container` | `HTMLElement` | 挂载 iframe 的 DOM 容器 |
124
+ | `options` | `AlgeoSdkOptions` | 可选配置 |
125
+
126
+ **AlgeoSdkOptions:**
127
+
128
+ | 属性 | 类型 | 默认值 | 说明 |
129
+ | ----------- | -------- | ---------------------- | ------------------------------------- |
130
+ | `baseUrl` | `string` | `https://dajiaoai.com` | 内嵌页基础 URL |
131
+ | `initialId` | `string` | `''` | 初始加载的分享 ID,为空则加载空白画板 |
132
+
133
+ **示例:**
134
+
135
+ ```javascript
136
+ const sdk = await AlgeoSdk.create(container, {
137
+ baseUrl: 'https://dajiaoai.com',
138
+ initialId: 'E8NHN7OP',
139
+ });
140
+ ```
141
+
142
+ ---
143
+
144
+ #### 实例属性(只读)
145
+
146
+ | 属性 | 类型 | 说明 |
147
+ | --------- | ---------------- | ------------------------------------ |
148
+ | `ready` | `boolean` | 是否已就绪(收到 iframe ready 通知) |
149
+ | `version` | `string \| null` | 内嵌页协议版本 |
123
150
 
124
- - `container`: 挂载的 DOM 元素
125
- - `options.baseUrl`: 内嵌页基础 URL,默认 `https://dajiaoai.com`
126
- - `options.initialId`: 初始加载的分享 ID,可选
151
+ ---
127
152
 
128
- ### `sdk.loadShareById(id: string): Promise<LoadShareByIdResult>`
153
+ #### 实例方法
154
+
155
+ ##### `sdk.loadShareById(id: string): Promise<LoadShareByIdResult>`
129
156
 
130
157
  按分享 ID 加载内容。
131
158
 
132
- ### `sdk.loadFile(content: FileContent): Promise<LoadFileResult>`
159
+ | 参数 | 类型 | 说明 |
160
+ | ---- | -------- | ---------------------- |
161
+ | `id` | `string` | 分享 ID,如 `E8NHN7OP` |
133
162
 
134
- 加载完整文件数据(覆盖式)。
163
+ **返回值:** `{ success: true }`
135
164
 
136
- ### `sdk.getSlideCount(): Promise<GetSlideCountResult>`
165
+ ---
137
166
 
138
- 查询当前画板数量。返回 `{ count: number }`。
167
+ ##### `sdk.loadFile(content: FileContent): Promise<LoadFileResult>`
139
168
 
140
- ### `sdk.switchSlide(index: number): Promise<SwitchSlideResult>`
169
+ 加载完整文件数据(覆盖式),需符合 FileContentV10 格式。
141
170
 
142
- 切换到指定索引的画板。
171
+ | 参数 | 类型 | 说明 |
172
+ | --------- | ------------- | ------------ |
173
+ | `content` | `FileContent` | 文件内容对象 |
143
174
 
144
- ### `sdk.repl(command: string): Promise<ReplResult>`
175
+ **FileContent 结构:**
145
176
 
146
- 执行 REPL 指令,返回面向 AI 的文档/文本内容。`command` 为 REPL 可用的单个指令,如 `help`、`list`、`list_slides`、`eval` 等。返回 `{ output: string }`。
177
+ ```typescript
178
+ interface FileContent {
179
+ slides: unknown[]; // 画板数据数组
180
+ messages: unknown[]; // 消息数据
181
+ metadata: {
182
+ version: string; // 如 '10'
183
+ shareOptions?: unknown;
184
+ };
185
+ }
186
+ ```
147
187
 
148
- ### `sdk.destroy(): void`
188
+ **返回值:** `{ success: true }`
149
189
 
150
- 销毁实例,移除 iframe 与事件监听。
190
+ ---
151
191
 
152
- ## postMessage 协议
192
+ ##### `sdk.getSlideCount(): Promise<GetSlideCountResult>`
153
193
 
154
- | 方法 | 请求 payload | 说明 |
155
- | --------------- | ----------------------------------------------------------- | --------------------------------------- |
156
- | `loadShareById` | `{ type: 'loadShareById', id: string, requestId? }` | 按分享 ID 加载 |
157
- | `loadFile` | `{ type: 'loadFile', content: FileContentV10, requestId? }` | 加载文件数据 |
158
- | `getSlideCount` | `{ type: 'getSlideCount', requestId? }` | 查询画板数量 |
159
- | `switchSlide` | `{ type: 'switchSlide', index: number, requestId? }` | 切换画板 |
160
- | `repl` | `{ type: 'repl', command: string, requestId? }` | 执行 REPL 指令,返回面向 AI 的文档/文本 |
194
+ 查询当前画板数量。
161
195
 
162
- 响应格式:
196
+ **返回值:** `{ count: number }`
197
+
198
+ ---
199
+
200
+ ##### `sdk.switchSlide(index: number): Promise<SwitchSlideResult>`
201
+
202
+ 切换到指定索引的画板(索引从 0 开始)。
203
+
204
+ | 参数 | 类型 | 说明 |
205
+ | ------- | -------- | ---------------------- |
206
+ | `index` | `number` | 画板索引,0 表示第一页 |
207
+
208
+ **返回值:** `{ success: true }`
209
+
210
+ ---
211
+
212
+ ##### `sdk.repl(command: string): Promise<ReplResult>`
213
+
214
+ 执行 REPL 指令,返回面向 AI 的文档/文本内容。需先通过 `loadShareById` 或 `loadFile` 加载内容后,部分指令才有数据可查。
215
+
216
+ | 参数 | 类型 | 说明 |
217
+ | --------- | -------- | --------- |
218
+ | `command` | `string` | REPL 指令 |
219
+
220
+ **常用指令:**
221
+
222
+ | 指令 | 说明 |
223
+ | ------------- | ----------------------------- |
224
+ | `help` | 查看 REPL 帮助 |
225
+ | `list` | 列出当前画板中的对象 |
226
+ | `list_slides` | 列出所有画板 |
227
+ | `eval` | 执行表达式(具体用法见 help) |
228
+
229
+ **返回值:** `{ output: string }` — 指令输出的文本内容
230
+
231
+ ---
232
+
233
+ ##### `sdk.destroy(): void`
234
+
235
+ 销毁实例,移除 iframe 与事件监听,并拒绝所有未完成的 pending 请求。
236
+
237
+ ---
238
+
239
+ ### 错误处理
240
+
241
+ 所有异步方法在失败时会 reject `AlgeoSdkError`:
242
+
243
+ ```javascript
244
+ try {
245
+ await sdk.loadShareById('invalid');
246
+ } catch (e) {
247
+ if (e.name === 'AlgeoSdkError') {
248
+ console.error(e.code, e.message, e.details);
249
+ }
250
+ }
251
+ ```
163
252
 
164
- - 成功:`{ type: 'response', requestId, success: true, result? }`
165
- - 失败:`{ type: 'response', requestId, success: false, error: { code, message, details? } }`
253
+ **常见错误码:**
166
254
 
167
- 内嵌页就绪时向父页面发送:`{ type: 'ready', version }`
255
+ | 错误码 | 说明 |
256
+ | ------------------ | --------------------------- |
257
+ | `IFRAME_NOT_READY` | iframe 未加载完成即调用方法 |
258
+ | `TIMEOUT` | 请求超时(30 秒) |
259
+ | `DESTROYED` | SDK 已销毁 |
260
+ | `BAD_REQUEST` | 非法请求(如重复 init) |
@@ -5,8 +5,8 @@
5
5
  * 绑定 DOM 容器,自动创建 iframe 并封装 postMessage 通信
6
6
  */
7
7
  /** SDK 版本号,构建时由 rollup 注入 */
8
- const VERSION = '1.0.1';
9
- const DEFAULT_EMBED_BASE = 'http://10.23.0.251:8083';
8
+ const VERSION = '1.1.0';
9
+ const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
10
10
  let requestIdCounter = 0;
11
11
  function generateRequestId() {
12
12
  return `req-${Date.now()}-${++requestIdCounter}`;
@@ -44,57 +44,76 @@ class AlgeoSdk {
44
44
  get version() {
45
45
  return this._version;
46
46
  }
47
- constructor(container, options = {}) {
47
+ constructor(container) {
48
48
  this.pending = new Map();
49
49
  this._ready = false;
50
50
  this._version = null;
51
51
  this.container = container;
52
- this.baseUrl = options.baseUrl ?? DEFAULT_EMBED_BASE;
53
- const initialId = options.initialId ?? '';
54
- const src = initialId
55
- ? `${this.baseUrl}/e/${encodeURIComponent(initialId)}`
56
- : `${this.baseUrl}/e`;
57
- this.iframe = document.createElement('iframe');
58
- this.iframe.id = 'algeo-embed';
59
- this.iframe.src = src;
60
- this.iframe.allow = 'fullscreen';
61
- this.iframe.style.width = '100%';
62
- this.iframe.style.height = '100%';
63
- this.iframe.style.border = 'none';
64
- this.container.appendChild(this.iframe);
65
- this.messageHandler = (e) => {
66
- const { data } = e;
67
- if (isReadyMessage(data)) {
68
- this._ready = true;
69
- this._version = data.version;
70
- return;
71
- }
72
- if (isResponseMessage(data)) {
73
- const { requestId, success } = data;
74
- const pending = this.pending.get(requestId);
75
- if (pending) {
76
- this.pending.delete(requestId);
77
- if (success) {
78
- pending.resolve(data.result);
79
- }
80
- else {
81
- const err = data.error;
82
- pending.reject(new AlgeoSdkError(err.message, err.code, err.details));
52
+ }
53
+ static async create(container, options) {
54
+ const ret = new AlgeoSdk(container);
55
+ await ret.init(options);
56
+ return ret;
57
+ }
58
+ async init(options = {}) {
59
+ if (this.iframe) {
60
+ throw new AlgeoSdkError('请勿多次调用init方法。', 'BAD_REQUEST');
61
+ }
62
+ return new Promise((resolve, reject) => {
63
+ const baseUrl = options.baseUrl ?? DEFAULT_EMBED_BASE;
64
+ const initialId = options.initialId ?? '';
65
+ const src = initialId
66
+ ? `${baseUrl}/e/${encodeURIComponent(initialId)}`
67
+ : `${baseUrl}/e`;
68
+ const iframe = this.iframe = document.createElement('iframe');
69
+ iframe.id = 'algeo-embed';
70
+ iframe.src = src;
71
+ iframe.allow = 'fullscreen';
72
+ iframe.style.width = '100%';
73
+ iframe.style.height = '100%';
74
+ iframe.style.border = 'none';
75
+ iframe.addEventListener('error', reject);
76
+ this.container.appendChild(iframe);
77
+ this.messageHandler = (e) => {
78
+ if (e.source !== iframe.contentWindow)
79
+ return;
80
+ const { data } = e;
81
+ if (isReadyMessage(data)) {
82
+ this._ready = true;
83
+ this._version = data.version;
84
+ resolve();
85
+ return;
86
+ }
87
+ if (isResponseMessage(data)) {
88
+ const { requestId, success } = data;
89
+ const pending = this.pending.get(requestId);
90
+ if (pending) {
91
+ this.pending.delete(requestId);
92
+ if (success) {
93
+ pending.resolve(data.result);
94
+ }
95
+ else {
96
+ const err = data.error;
97
+ pending.reject(new AlgeoSdkError(err.message, err.code, err.details));
98
+ }
83
99
  }
84
100
  }
85
- }
86
- };
87
- window.addEventListener('message', this.messageHandler);
101
+ };
102
+ window.addEventListener('message', this.messageHandler);
103
+ });
88
104
  }
89
105
  post(type, payload) {
90
106
  const requestId = generateRequestId();
91
107
  const msg = { type, requestId, ...payload };
92
108
  return new Promise((resolve, reject) => {
109
+ if (!this._ready) {
110
+ reject(new AlgeoSdkError('iframe 未加载完成', 'IFRAME_NOT_READY'));
111
+ }
93
112
  this.pending.set(requestId, {
94
113
  resolve: resolve,
95
114
  reject,
96
115
  });
97
- const target = this.iframe.contentWindow;
116
+ const target = this.iframe?.contentWindow;
98
117
  if (!target) {
99
118
  this.pending.delete(requestId);
100
119
  reject(new AlgeoSdkError('iframe 未加载完成', 'IFRAME_NOT_READY'));
@@ -145,10 +164,13 @@ class AlgeoSdk {
145
164
  * 销毁实例,移除 iframe 与事件监听
146
165
  */
147
166
  destroy() {
148
- window.removeEventListener('message', this.messageHandler);
167
+ if (this.messageHandler) {
168
+ window.removeEventListener('message', this.messageHandler);
169
+ this.messageHandler = undefined;
170
+ }
149
171
  this.pending.forEach(({ reject }) => reject(new AlgeoSdkError('SDK 已销毁', 'DESTROYED')));
150
172
  this.pending.clear();
151
- if (this.iframe.parentNode) {
173
+ if (this.iframe?.parentNode) {
152
174
  this.iframe.parentNode.removeChild(this.iframe);
153
175
  }
154
176
  }
@@ -3,8 +3,8 @@
3
3
  * 绑定 DOM 容器,自动创建 iframe 并封装 postMessage 通信
4
4
  */
5
5
  /** SDK 版本号,构建时由 rollup 注入 */
6
- const VERSION = '1.0.1';
7
- const DEFAULT_EMBED_BASE = 'http://10.23.0.251:8083';
6
+ const VERSION = '1.1.0';
7
+ const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
8
8
  let requestIdCounter = 0;
9
9
  function generateRequestId() {
10
10
  return `req-${Date.now()}-${++requestIdCounter}`;
@@ -42,57 +42,76 @@ class AlgeoSdk {
42
42
  get version() {
43
43
  return this._version;
44
44
  }
45
- constructor(container, options = {}) {
45
+ constructor(container) {
46
46
  this.pending = new Map();
47
47
  this._ready = false;
48
48
  this._version = null;
49
49
  this.container = container;
50
- this.baseUrl = options.baseUrl ?? DEFAULT_EMBED_BASE;
51
- const initialId = options.initialId ?? '';
52
- const src = initialId
53
- ? `${this.baseUrl}/e/${encodeURIComponent(initialId)}`
54
- : `${this.baseUrl}/e`;
55
- this.iframe = document.createElement('iframe');
56
- this.iframe.id = 'algeo-embed';
57
- this.iframe.src = src;
58
- this.iframe.allow = 'fullscreen';
59
- this.iframe.style.width = '100%';
60
- this.iframe.style.height = '100%';
61
- this.iframe.style.border = 'none';
62
- this.container.appendChild(this.iframe);
63
- this.messageHandler = (e) => {
64
- const { data } = e;
65
- if (isReadyMessage(data)) {
66
- this._ready = true;
67
- this._version = data.version;
68
- return;
69
- }
70
- if (isResponseMessage(data)) {
71
- const { requestId, success } = data;
72
- const pending = this.pending.get(requestId);
73
- if (pending) {
74
- this.pending.delete(requestId);
75
- if (success) {
76
- pending.resolve(data.result);
77
- }
78
- else {
79
- const err = data.error;
80
- pending.reject(new AlgeoSdkError(err.message, err.code, err.details));
50
+ }
51
+ static async create(container, options) {
52
+ const ret = new AlgeoSdk(container);
53
+ await ret.init(options);
54
+ return ret;
55
+ }
56
+ async init(options = {}) {
57
+ if (this.iframe) {
58
+ throw new AlgeoSdkError('请勿多次调用init方法。', 'BAD_REQUEST');
59
+ }
60
+ return new Promise((resolve, reject) => {
61
+ const baseUrl = options.baseUrl ?? DEFAULT_EMBED_BASE;
62
+ const initialId = options.initialId ?? '';
63
+ const src = initialId
64
+ ? `${baseUrl}/e/${encodeURIComponent(initialId)}`
65
+ : `${baseUrl}/e`;
66
+ const iframe = this.iframe = document.createElement('iframe');
67
+ iframe.id = 'algeo-embed';
68
+ iframe.src = src;
69
+ iframe.allow = 'fullscreen';
70
+ iframe.style.width = '100%';
71
+ iframe.style.height = '100%';
72
+ iframe.style.border = 'none';
73
+ iframe.addEventListener('error', reject);
74
+ this.container.appendChild(iframe);
75
+ this.messageHandler = (e) => {
76
+ if (e.source !== iframe.contentWindow)
77
+ return;
78
+ const { data } = e;
79
+ if (isReadyMessage(data)) {
80
+ this._ready = true;
81
+ this._version = data.version;
82
+ resolve();
83
+ return;
84
+ }
85
+ if (isResponseMessage(data)) {
86
+ const { requestId, success } = data;
87
+ const pending = this.pending.get(requestId);
88
+ if (pending) {
89
+ this.pending.delete(requestId);
90
+ if (success) {
91
+ pending.resolve(data.result);
92
+ }
93
+ else {
94
+ const err = data.error;
95
+ pending.reject(new AlgeoSdkError(err.message, err.code, err.details));
96
+ }
81
97
  }
82
98
  }
83
- }
84
- };
85
- window.addEventListener('message', this.messageHandler);
99
+ };
100
+ window.addEventListener('message', this.messageHandler);
101
+ });
86
102
  }
87
103
  post(type, payload) {
88
104
  const requestId = generateRequestId();
89
105
  const msg = { type, requestId, ...payload };
90
106
  return new Promise((resolve, reject) => {
107
+ if (!this._ready) {
108
+ reject(new AlgeoSdkError('iframe 未加载完成', 'IFRAME_NOT_READY'));
109
+ }
91
110
  this.pending.set(requestId, {
92
111
  resolve: resolve,
93
112
  reject,
94
113
  });
95
- const target = this.iframe.contentWindow;
114
+ const target = this.iframe?.contentWindow;
96
115
  if (!target) {
97
116
  this.pending.delete(requestId);
98
117
  reject(new AlgeoSdkError('iframe 未加载完成', 'IFRAME_NOT_READY'));
@@ -143,10 +162,13 @@ class AlgeoSdk {
143
162
  * 销毁实例,移除 iframe 与事件监听
144
163
  */
145
164
  destroy() {
146
- window.removeEventListener('message', this.messageHandler);
165
+ if (this.messageHandler) {
166
+ window.removeEventListener('message', this.messageHandler);
167
+ this.messageHandler = undefined;
168
+ }
147
169
  this.pending.forEach(({ reject }) => reject(new AlgeoSdkError('SDK 已销毁', 'DESTROYED')));
148
170
  this.pending.clear();
149
- if (this.iframe.parentNode) {
171
+ if (this.iframe?.parentNode) {
150
172
  this.iframe.parentNode.removeChild(this.iframe);
151
173
  }
152
174
  }
@@ -9,8 +9,8 @@
9
9
  * 绑定 DOM 容器,自动创建 iframe 并封装 postMessage 通信
10
10
  */
11
11
  /** SDK 版本号,构建时由 rollup 注入 */
12
- const VERSION = '1.0.1';
13
- const DEFAULT_EMBED_BASE = 'http://10.23.0.251:8083';
12
+ const VERSION = '1.1.0';
13
+ const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
14
14
  let requestIdCounter = 0;
15
15
  function generateRequestId() {
16
16
  return `req-${Date.now()}-${++requestIdCounter}`;
@@ -48,57 +48,76 @@
48
48
  get version() {
49
49
  return this._version;
50
50
  }
51
- constructor(container, options = {}) {
51
+ constructor(container) {
52
52
  this.pending = new Map();
53
53
  this._ready = false;
54
54
  this._version = null;
55
55
  this.container = container;
56
- this.baseUrl = options.baseUrl ?? DEFAULT_EMBED_BASE;
57
- const initialId = options.initialId ?? '';
58
- const src = initialId
59
- ? `${this.baseUrl}/e/${encodeURIComponent(initialId)}`
60
- : `${this.baseUrl}/e`;
61
- this.iframe = document.createElement('iframe');
62
- this.iframe.id = 'algeo-embed';
63
- this.iframe.src = src;
64
- this.iframe.allow = 'fullscreen';
65
- this.iframe.style.width = '100%';
66
- this.iframe.style.height = '100%';
67
- this.iframe.style.border = 'none';
68
- this.container.appendChild(this.iframe);
69
- this.messageHandler = (e) => {
70
- const { data } = e;
71
- if (isReadyMessage(data)) {
72
- this._ready = true;
73
- this._version = data.version;
74
- return;
75
- }
76
- if (isResponseMessage(data)) {
77
- const { requestId, success } = data;
78
- const pending = this.pending.get(requestId);
79
- if (pending) {
80
- this.pending.delete(requestId);
81
- if (success) {
82
- pending.resolve(data.result);
83
- }
84
- else {
85
- const err = data.error;
86
- pending.reject(new AlgeoSdkError(err.message, err.code, err.details));
56
+ }
57
+ static async create(container, options) {
58
+ const ret = new AlgeoSdk(container);
59
+ await ret.init(options);
60
+ return ret;
61
+ }
62
+ async init(options = {}) {
63
+ if (this.iframe) {
64
+ throw new AlgeoSdkError('请勿多次调用init方法。', 'BAD_REQUEST');
65
+ }
66
+ return new Promise((resolve, reject) => {
67
+ const baseUrl = options.baseUrl ?? DEFAULT_EMBED_BASE;
68
+ const initialId = options.initialId ?? '';
69
+ const src = initialId
70
+ ? `${baseUrl}/e/${encodeURIComponent(initialId)}`
71
+ : `${baseUrl}/e`;
72
+ const iframe = this.iframe = document.createElement('iframe');
73
+ iframe.id = 'algeo-embed';
74
+ iframe.src = src;
75
+ iframe.allow = 'fullscreen';
76
+ iframe.style.width = '100%';
77
+ iframe.style.height = '100%';
78
+ iframe.style.border = 'none';
79
+ iframe.addEventListener('error', reject);
80
+ this.container.appendChild(iframe);
81
+ this.messageHandler = (e) => {
82
+ if (e.source !== iframe.contentWindow)
83
+ return;
84
+ const { data } = e;
85
+ if (isReadyMessage(data)) {
86
+ this._ready = true;
87
+ this._version = data.version;
88
+ resolve();
89
+ return;
90
+ }
91
+ if (isResponseMessage(data)) {
92
+ const { requestId, success } = data;
93
+ const pending = this.pending.get(requestId);
94
+ if (pending) {
95
+ this.pending.delete(requestId);
96
+ if (success) {
97
+ pending.resolve(data.result);
98
+ }
99
+ else {
100
+ const err = data.error;
101
+ pending.reject(new AlgeoSdkError(err.message, err.code, err.details));
102
+ }
87
103
  }
88
104
  }
89
- }
90
- };
91
- window.addEventListener('message', this.messageHandler);
105
+ };
106
+ window.addEventListener('message', this.messageHandler);
107
+ });
92
108
  }
93
109
  post(type, payload) {
94
110
  const requestId = generateRequestId();
95
111
  const msg = { type, requestId, ...payload };
96
112
  return new Promise((resolve, reject) => {
113
+ if (!this._ready) {
114
+ reject(new AlgeoSdkError('iframe 未加载完成', 'IFRAME_NOT_READY'));
115
+ }
97
116
  this.pending.set(requestId, {
98
117
  resolve: resolve,
99
118
  reject,
100
119
  });
101
- const target = this.iframe.contentWindow;
120
+ const target = this.iframe?.contentWindow;
102
121
  if (!target) {
103
122
  this.pending.delete(requestId);
104
123
  reject(new AlgeoSdkError('iframe 未加载完成', 'IFRAME_NOT_READY'));
@@ -149,10 +168,13 @@
149
168
  * 销毁实例,移除 iframe 与事件监听
150
169
  */
151
170
  destroy() {
152
- window.removeEventListener('message', this.messageHandler);
171
+ if (this.messageHandler) {
172
+ window.removeEventListener('message', this.messageHandler);
173
+ this.messageHandler = undefined;
174
+ }
153
175
  this.pending.forEach(({ reject }) => reject(new AlgeoSdkError('SDK 已销毁', 'DESTROYED')));
154
176
  this.pending.clear();
155
- if (this.iframe.parentNode) {
177
+ if (this.iframe?.parentNode) {
156
178
  this.iframe.parentNode.removeChild(this.iframe);
157
179
  }
158
180
  }
package/dist/index.d.ts CHANGED
@@ -49,17 +49,18 @@ export declare class AlgeoSdkError extends Error {
49
49
  */
50
50
  export declare class AlgeoSdk {
51
51
  private container;
52
- private iframe;
53
- private baseUrl;
52
+ private iframe?;
54
53
  private pending;
55
- private messageHandler;
54
+ private messageHandler?;
56
55
  private _ready;
57
56
  private _version;
58
57
  /** 是否已就绪(收到 iframe ready 通知) */
59
58
  get ready(): boolean;
60
59
  /** 内嵌页协议版本 */
61
60
  get version(): string | null;
62
- constructor(container: HTMLElement, options?: AlgeoSdkOptions);
61
+ constructor(container: HTMLElement);
62
+ static create(container: HTMLElement, options?: AlgeoSdkOptions): Promise<AlgeoSdk>;
63
+ init(options?: AlgeoSdkOptions): Promise<void>;
63
64
  private post;
64
65
  /**
65
66
  * 按分享 ID 加载内容
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dajiaoai/algeo-sdk",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "大角几何内嵌画板 SDK,支持 iframe 挂载与 postMessage 通信",
5
5
  "main": "dist/algeo-sdk.cjs.js",
6
6
  "module": "dist/algeo-sdk.esm.js",
@@ -23,6 +23,14 @@
23
23
  ],
24
24
  "author": "大角几何",
25
25
  "license": "Apache-2.0",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/dajiaoai/algeo-sdk.git"
29
+ },
30
+ "bugs": {
31
+ "url": "https://github.com/dajiaoai/algeo-sdk/issues"
32
+ },
33
+ "homepage": "https://github.com/dajiaoai/algeo-sdk#readme",
26
34
  "type": "module",
27
35
  "devDependencies": {
28
36
  "@rollup/plugin-replace": "^6.0.1",