@dajiaoai/algeo-sdk 1.2.0 → 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 +5 -4
package/dist/algeo-sdk.esm.js
CHANGED
|
@@ -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 = '
|
|
3898
|
+
const VERSION = '2.4.2';
|
|
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,210 @@ function isReadyMessage(msg) {
|
|
|
3917
3917
|
'type' in msg &&
|
|
3918
3918
|
msg.type === 'ready');
|
|
3919
3919
|
}
|
|
3920
|
-
|
|
3921
|
-
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
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
|
-
|
|
3933
|
-
|
|
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
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
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';
|
|
3957
4074
|
iframe.style.width = '100%';
|
|
3958
4075
|
iframe.style.height = '100%';
|
|
3959
4076
|
iframe.style.border = 'none';
|
|
3960
|
-
iframe.addEventListener('error',
|
|
4077
|
+
iframe.addEventListener('error', () => {
|
|
4078
|
+
finalizeFailure(new AlgeoError('iframe 初始化失败', EMBED_ERROR_CODES.IFRAME_NOT_READY));
|
|
4079
|
+
});
|
|
3961
4080
|
this.container.appendChild(iframe);
|
|
4081
|
+
initTimeoutId = setTimeout(() => {
|
|
4082
|
+
finalizeFailure(new AlgeoError('iframe 初始化超时', EMBED_ERROR_CODES.TIMEOUT));
|
|
4083
|
+
}, EMBED_TIMEOUT_MS);
|
|
3962
4084
|
this.messageHandler = (e) => {
|
|
3963
|
-
if (e.source !== iframe.contentWindow)
|
|
4085
|
+
if (e.source !== iframe.contentWindow) {
|
|
3964
4086
|
return;
|
|
4087
|
+
}
|
|
3965
4088
|
const { data } = e;
|
|
3966
4089
|
if (isReadyMessage(data)) {
|
|
4090
|
+
if (this._ready) {
|
|
4091
|
+
return;
|
|
4092
|
+
}
|
|
3967
4093
|
this._ready = true;
|
|
3968
4094
|
this._version = data.version;
|
|
3969
|
-
|
|
4095
|
+
this.emit('ready', {
|
|
4096
|
+
type: 'ready',
|
|
4097
|
+
mode: this.embedMode,
|
|
4098
|
+
version: this._version,
|
|
4099
|
+
});
|
|
4100
|
+
finalizeSuccess();
|
|
3970
4101
|
return;
|
|
3971
4102
|
}
|
|
3972
4103
|
if (isResponseMessage(data)) {
|
|
3973
|
-
const
|
|
3974
|
-
|
|
3975
|
-
|
|
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
|
-
}
|
|
4104
|
+
const pending = this.pending.get(data.requestId);
|
|
4105
|
+
if (!pending) {
|
|
4106
|
+
return;
|
|
3984
4107
|
}
|
|
4108
|
+
this.pending.delete(data.requestId);
|
|
4109
|
+
if (data.success) {
|
|
4110
|
+
pending.resolve(data.result);
|
|
4111
|
+
return;
|
|
4112
|
+
}
|
|
4113
|
+
const err = data.error;
|
|
4114
|
+
pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
|
|
4115
|
+
return;
|
|
4116
|
+
}
|
|
4117
|
+
if (isSaveRequestMessage(data) &&
|
|
4118
|
+
this.handleRequestMessage(data, iframe.contentWindow)) {
|
|
4119
|
+
return;
|
|
4120
|
+
}
|
|
4121
|
+
if (this.acceptsEventMessage() && isEmbedEventMessage(data)) {
|
|
4122
|
+
this.handleEventMessage(data);
|
|
4123
|
+
this.emit(data.type, data);
|
|
3985
4124
|
}
|
|
3986
4125
|
};
|
|
3987
4126
|
window.addEventListener('message', this.messageHandler);
|
|
@@ -3991,74 +4130,317 @@ class AlgeoSdk {
|
|
|
3991
4130
|
const requestId = generateRequestId();
|
|
3992
4131
|
const msg = { type, requestId, ...payload };
|
|
3993
4132
|
return new Promise((resolve, reject) => {
|
|
4133
|
+
if (this.destroyed) {
|
|
4134
|
+
reject(new AlgeoError('SDK 已销毁', EMBED_ERROR_CODES.DESTROYED));
|
|
4135
|
+
return;
|
|
4136
|
+
}
|
|
3994
4137
|
if (!this._ready) {
|
|
3995
4138
|
reject(new AlgeoError('iframe 未加载完成', EMBED_ERROR_CODES.IFRAME_NOT_READY));
|
|
4139
|
+
return;
|
|
3996
4140
|
}
|
|
3997
|
-
this.pending.set(requestId, {
|
|
3998
|
-
resolve: resolve,
|
|
3999
|
-
reject,
|
|
4000
|
-
});
|
|
4001
4141
|
const target = this.iframe?.contentWindow;
|
|
4002
4142
|
if (!target) {
|
|
4003
|
-
this.pending.delete(requestId);
|
|
4004
4143
|
reject(new AlgeoError('iframe 未加载完成', EMBED_ERROR_CODES.IFRAME_NOT_READY));
|
|
4005
4144
|
return;
|
|
4006
4145
|
}
|
|
4146
|
+
this.pending.set(requestId, {
|
|
4147
|
+
resolve: resolve,
|
|
4148
|
+
reject,
|
|
4149
|
+
});
|
|
4007
4150
|
target.postMessage(msg, '*');
|
|
4008
|
-
// 超时兜底
|
|
4009
4151
|
setTimeout(() => {
|
|
4010
4152
|
if (this.pending.has(requestId)) {
|
|
4011
4153
|
this.pending.delete(requestId);
|
|
4012
4154
|
reject(new AlgeoError('请求超时', EMBED_ERROR_CODES.TIMEOUT));
|
|
4013
4155
|
}
|
|
4014
|
-
},
|
|
4156
|
+
}, EMBED_TIMEOUT_MS);
|
|
4015
4157
|
});
|
|
4016
4158
|
}
|
|
4017
|
-
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4159
|
+
async destroy() {
|
|
4160
|
+
if (this.destroyed) {
|
|
4161
|
+
return;
|
|
4162
|
+
}
|
|
4163
|
+
this.destroyed = true;
|
|
4164
|
+
this.cleanupMessageHandler();
|
|
4165
|
+
this.resetRuntimeState();
|
|
4166
|
+
this.pending.forEach(({ reject }) => {
|
|
4167
|
+
reject(new AlgeoError('SDK 已销毁', EMBED_ERROR_CODES.DESTROYED));
|
|
4168
|
+
});
|
|
4169
|
+
this.pending.clear();
|
|
4170
|
+
this.cleanupIframe();
|
|
4022
4171
|
}
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4172
|
+
}
|
|
4173
|
+
|
|
4174
|
+
class EmbeddedPresentation extends EmbeddedTarget {
|
|
4175
|
+
constructor(container) {
|
|
4176
|
+
super(container, 'presentation');
|
|
4177
|
+
this.currentSlideIndex = 0;
|
|
4178
|
+
this.slideCount = 0;
|
|
4179
|
+
}
|
|
4180
|
+
async initialize(options = {}, baseUrl) {
|
|
4181
|
+
await this.init({
|
|
4182
|
+
baseUrl,
|
|
4183
|
+
initialId: options.shareId,
|
|
4184
|
+
});
|
|
4028
4185
|
}
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
*/
|
|
4032
|
-
switchSlide(index) {
|
|
4033
|
-
return this.post('switchSlide', { index });
|
|
4186
|
+
acceptsEventMessage() {
|
|
4187
|
+
return false;
|
|
4034
4188
|
}
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4189
|
+
async loadShareById(id) {
|
|
4190
|
+
const result = await this.post('loadShareById', {
|
|
4191
|
+
id,
|
|
4192
|
+
});
|
|
4193
|
+
this.currentContent = undefined;
|
|
4194
|
+
this.currentSlideIndex = 0;
|
|
4195
|
+
this.slideCount = 0;
|
|
4196
|
+
return result;
|
|
4040
4197
|
}
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
|
|
4045
|
-
|
|
4198
|
+
async loadFile(content) {
|
|
4199
|
+
const result = await this.post('loadContent', { content });
|
|
4200
|
+
this.currentContent = content;
|
|
4201
|
+
this.currentSlideIndex = 0;
|
|
4202
|
+
this.slideCount = content.slides.length;
|
|
4203
|
+
return result;
|
|
4204
|
+
}
|
|
4205
|
+
async switchSlide(index) {
|
|
4206
|
+
const result = await this.post('switchSlide', { index });
|
|
4207
|
+
this.currentSlideIndex = index;
|
|
4208
|
+
return result;
|
|
4209
|
+
}
|
|
4210
|
+
async getSlideCount() {
|
|
4211
|
+
const result = await this.post('getSlideCount', {});
|
|
4212
|
+
this.slideCount = result.count;
|
|
4213
|
+
return result;
|
|
4214
|
+
}
|
|
4215
|
+
async repl(command) {
|
|
4046
4216
|
return this.post('repl', { command });
|
|
4047
4217
|
}
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4218
|
+
}
|
|
4219
|
+
|
|
4220
|
+
class EmbeddedEditor extends EmbeddedTarget {
|
|
4221
|
+
constructor(container) {
|
|
4222
|
+
super(container, 'editor');
|
|
4223
|
+
this.currentSlideIndex = 0;
|
|
4224
|
+
this.slideCount = 0;
|
|
4225
|
+
this.historyCount = 0;
|
|
4226
|
+
this.historyCurrentIndex = -1;
|
|
4227
|
+
this.uiConfig = {};
|
|
4228
|
+
this.document = {
|
|
4229
|
+
loadContent: async (content) => {
|
|
4230
|
+
await this.loadContent(content, 'loadContent');
|
|
4231
|
+
},
|
|
4232
|
+
getContent: async () => {
|
|
4233
|
+
const result = await this.post('getContent', {});
|
|
4234
|
+
this.currentContent = result.content;
|
|
4235
|
+
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(result.content.slides.length - 1, 0));
|
|
4236
|
+
this.slideCount = result.content.slides.length;
|
|
4237
|
+
await this.refreshHistoryState();
|
|
4238
|
+
return result.content;
|
|
4239
|
+
},
|
|
4240
|
+
};
|
|
4241
|
+
this.slides = {
|
|
4242
|
+
getCount: () => this.slideCount,
|
|
4243
|
+
getCurrentIndex: () => this.currentSlideIndex,
|
|
4244
|
+
switchTo: async (index) => {
|
|
4245
|
+
await this.switchTo(index);
|
|
4246
|
+
},
|
|
4247
|
+
add: async () => this.addSlide('addSlide', {}),
|
|
4248
|
+
addAt: async (index) => this.addSlide('addSlideAt', { index }),
|
|
4249
|
+
remove: async (index) => {
|
|
4250
|
+
await this.post('removeSlide', { index });
|
|
4251
|
+
this.slideCount = Math.max(0, this.slideCount - 1);
|
|
4252
|
+
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
|
|
4253
|
+
await this.refreshHistoryState();
|
|
4254
|
+
},
|
|
4255
|
+
duplicate: async (index, targetIndex) => this.addSlide('duplicateSlide', { index, targetIndex }),
|
|
4256
|
+
reorder: async (fromIndex, toIndex) => {
|
|
4257
|
+
await this.post('reorderSlide', { fromIndex, toIndex });
|
|
4258
|
+
if (this.currentSlideIndex === fromIndex) {
|
|
4259
|
+
this.currentSlideIndex = toIndex;
|
|
4260
|
+
}
|
|
4261
|
+
await this.refreshHistoryState();
|
|
4262
|
+
},
|
|
4263
|
+
};
|
|
4264
|
+
this.history = {
|
|
4265
|
+
getCount: () => this.historyCount,
|
|
4266
|
+
getCurrentIndex: () => this.historyCurrentIndex,
|
|
4267
|
+
undo: async () => {
|
|
4268
|
+
await this.post('undo', {});
|
|
4269
|
+
await this.refreshHistoryState();
|
|
4270
|
+
},
|
|
4271
|
+
redo: async () => {
|
|
4272
|
+
await this.post('redo', {});
|
|
4273
|
+
await this.refreshHistoryState();
|
|
4274
|
+
},
|
|
4275
|
+
jumpTo: async (index) => {
|
|
4276
|
+
await this.post('jumpHistory', { index });
|
|
4277
|
+
await this.refreshHistoryState();
|
|
4278
|
+
},
|
|
4279
|
+
canUndo: () => this.historyCurrentIndex >= 0,
|
|
4280
|
+
canRedo: () => this.historyCurrentIndex < this.historyCount - 1,
|
|
4281
|
+
clear: async () => {
|
|
4282
|
+
await this.post('clearHistory', {});
|
|
4283
|
+
await this.refreshHistoryState();
|
|
4284
|
+
},
|
|
4285
|
+
};
|
|
4286
|
+
this.mode = {
|
|
4287
|
+
getUiConfig: () => ({ ...this.uiConfig }),
|
|
4288
|
+
setUiConfig: async (config) => {
|
|
4289
|
+
await this.post('setUiConfig', { config });
|
|
4290
|
+
this.uiConfig = {
|
|
4291
|
+
...this.uiConfig,
|
|
4292
|
+
...config,
|
|
4293
|
+
};
|
|
4294
|
+
},
|
|
4295
|
+
};
|
|
4296
|
+
}
|
|
4297
|
+
async initialize(options = {}, baseUrl) {
|
|
4298
|
+
if (!options.auth?.appId?.trim()) {
|
|
4299
|
+
throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
|
|
4055
4300
|
}
|
|
4056
|
-
this.
|
|
4057
|
-
this.
|
|
4058
|
-
|
|
4059
|
-
|
|
4301
|
+
this.uiConfig = options.ui || {};
|
|
4302
|
+
await this.init({
|
|
4303
|
+
baseUrl,
|
|
4304
|
+
auth: options.auth,
|
|
4305
|
+
initialId: options.shareId,
|
|
4306
|
+
});
|
|
4307
|
+
const content = await this.document.getContent();
|
|
4308
|
+
this.currentContent = content;
|
|
4309
|
+
this.slideCount = content.slides.length;
|
|
4310
|
+
this.currentSlideIndex = 0;
|
|
4311
|
+
await this.refreshHistoryState();
|
|
4312
|
+
if (options.initialContent) {
|
|
4313
|
+
await this.loadContent(options.initialContent, 'initialContent');
|
|
4314
|
+
}
|
|
4315
|
+
}
|
|
4316
|
+
handleEventMessage(event) {
|
|
4317
|
+
if (event.type === 'contentChange') {
|
|
4318
|
+
if (event.content) {
|
|
4319
|
+
this.currentContent = event.content;
|
|
4320
|
+
this.slideCount = event.content.slides.length;
|
|
4321
|
+
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
|
|
4322
|
+
}
|
|
4323
|
+
return;
|
|
4324
|
+
}
|
|
4325
|
+
if (event.type === 'slideChange') {
|
|
4326
|
+
this.currentSlideIndex = event.index;
|
|
4327
|
+
return;
|
|
4328
|
+
}
|
|
4329
|
+
this.currentContent = event.content;
|
|
4330
|
+
this.slideCount = event.content.slides.length;
|
|
4331
|
+
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
|
|
4332
|
+
}
|
|
4333
|
+
handleRequestMessage(message, sourceWindow) {
|
|
4334
|
+
if (message.type !== 'save') {
|
|
4335
|
+
return false;
|
|
4336
|
+
}
|
|
4337
|
+
void this.handleSaveRequest(message, sourceWindow);
|
|
4338
|
+
return true;
|
|
4339
|
+
}
|
|
4340
|
+
async loadContent(content, source) {
|
|
4341
|
+
await this.post('loadContent', { content });
|
|
4342
|
+
this.currentContent = content;
|
|
4343
|
+
this.currentSlideIndex = 0;
|
|
4344
|
+
this.slideCount = content.slides.length;
|
|
4345
|
+
await this.refreshHistoryState();
|
|
4346
|
+
}
|
|
4347
|
+
async switchTo(index) {
|
|
4348
|
+
await this.post('switchSlide', { index });
|
|
4349
|
+
this.currentSlideIndex = index;
|
|
4350
|
+
}
|
|
4351
|
+
async addSlide(command, payload) {
|
|
4352
|
+
const result = await this.post(command, payload);
|
|
4353
|
+
this.slideCount = Math.max(this.slideCount + 1, result.index + 1);
|
|
4354
|
+
this.currentSlideIndex = result.index;
|
|
4355
|
+
await this.refreshHistoryState();
|
|
4356
|
+
return result;
|
|
4357
|
+
}
|
|
4358
|
+
async refreshHistoryState() {
|
|
4359
|
+
const state = await this.post('getHistoryState', {});
|
|
4360
|
+
this.historyCount = state.count;
|
|
4361
|
+
this.historyCurrentIndex = state.currentIndex;
|
|
4362
|
+
return state;
|
|
4363
|
+
}
|
|
4364
|
+
async handleSaveRequest(message, sourceWindow) {
|
|
4365
|
+
const respond = (payload) => {
|
|
4366
|
+
sourceWindow.postMessage(payload, '*');
|
|
4367
|
+
};
|
|
4368
|
+
try {
|
|
4369
|
+
const result = await this.resolveSaveResult(message.content);
|
|
4370
|
+
respond({
|
|
4371
|
+
type: 'response',
|
|
4372
|
+
requestId: message.requestId,
|
|
4373
|
+
success: true,
|
|
4374
|
+
result,
|
|
4375
|
+
});
|
|
4376
|
+
}
|
|
4377
|
+
catch (error) {
|
|
4378
|
+
respond({
|
|
4379
|
+
type: 'response',
|
|
4380
|
+
requestId: message.requestId,
|
|
4381
|
+
success: false,
|
|
4382
|
+
error: {
|
|
4383
|
+
code: EMBED_ERROR_CODES.UNKNOWN_ERROR,
|
|
4384
|
+
message: error instanceof Error ? error.message : String(error),
|
|
4385
|
+
},
|
|
4386
|
+
});
|
|
4387
|
+
}
|
|
4388
|
+
}
|
|
4389
|
+
async resolveSaveResult(content) {
|
|
4390
|
+
const requestEvent = {
|
|
4391
|
+
type: 'save',
|
|
4392
|
+
stage: 'request',
|
|
4393
|
+
content,
|
|
4394
|
+
};
|
|
4395
|
+
for (const listener of this.getListeners('save')) {
|
|
4396
|
+
const result = await listener(requestEvent);
|
|
4397
|
+
if (result && typeof result === 'object' && 'status' in result) {
|
|
4398
|
+
return result;
|
|
4399
|
+
}
|
|
4060
4400
|
}
|
|
4401
|
+
return {
|
|
4402
|
+
status: 'error',
|
|
4403
|
+
message: '宿主未配置 save 事件处理器',
|
|
4404
|
+
};
|
|
4405
|
+
}
|
|
4406
|
+
}
|
|
4407
|
+
|
|
4408
|
+
async function createEditorInstance(container, options = {}, baseUrl) {
|
|
4409
|
+
const editor = new EmbeddedEditor(container);
|
|
4410
|
+
await editor.initialize(options, baseUrl);
|
|
4411
|
+
return editor;
|
|
4412
|
+
}
|
|
4413
|
+
async function createPresentationInstance(container, options = {}, baseUrl) {
|
|
4414
|
+
const presentation = new EmbeddedPresentation(container);
|
|
4415
|
+
await presentation.initialize(options, baseUrl);
|
|
4416
|
+
return presentation;
|
|
4417
|
+
}
|
|
4418
|
+
function createEmbeddedInstance(container, options) {
|
|
4419
|
+
if (options.mode === 'editor') {
|
|
4420
|
+
return createEditorInstance(container, options.editor, options.baseUrl);
|
|
4421
|
+
}
|
|
4422
|
+
return createPresentationInstance(container, options.presentation, options.baseUrl);
|
|
4423
|
+
}
|
|
4424
|
+
class AlgeoSdk {
|
|
4425
|
+
constructor() { }
|
|
4426
|
+
static async create(container, options) {
|
|
4427
|
+
return createEmbeddedInstance(container, options);
|
|
4061
4428
|
}
|
|
4429
|
+
static async createEditor(container, options = {}) {
|
|
4430
|
+
return createEditor(container, options);
|
|
4431
|
+
}
|
|
4432
|
+
static async createPresentation(container, options = {}) {
|
|
4433
|
+
return createPresentation(container, options);
|
|
4434
|
+
}
|
|
4435
|
+
}
|
|
4436
|
+
async function create(container, options) {
|
|
4437
|
+
return createEmbeddedInstance(container, options);
|
|
4438
|
+
}
|
|
4439
|
+
async function createEditor(container, options = {}) {
|
|
4440
|
+
return createEditorInstance(container, options);
|
|
4441
|
+
}
|
|
4442
|
+
async function createPresentation(container, options = {}) {
|
|
4443
|
+
return createPresentationInstance(container, options);
|
|
4062
4444
|
}
|
|
4063
4445
|
|
|
4064
|
-
export { AlgeoError, AlgeoSdk, EMBED_ERROR_CODES, VERSION };
|
|
4446
|
+
export { AlgeoError, AlgeoSdk, EMBED_ERROR_CODES, EmbeddedEditor, EmbeddedPresentation, VERSION, create, createEditor, createPresentation };
|