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