@dajiaoai/algeo-sdk 1.2.1 → 2.5.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/README.md +275 -47
- package/dist/algeo-sdk.cjs.js +518 -83
- package/dist/algeo-sdk.esm.js +514 -84
- package/dist/algeo-sdk.umd.js +518 -83
- package/dist/embedded-editor.d.ts +27 -0
- package/dist/embedded-presentation.d.ts +18 -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 +172 -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.5.0';
|
|
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,211 @@ 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';
|
|
4076
|
+
iframe.referrerPolicy = 'origin';
|
|
3959
4077
|
iframe.style.width = '100%';
|
|
3960
4078
|
iframe.style.height = '100%';
|
|
3961
4079
|
iframe.style.border = 'none';
|
|
3962
|
-
iframe.addEventListener('error',
|
|
4080
|
+
iframe.addEventListener('error', () => {
|
|
4081
|
+
finalizeFailure(new AlgeoError('iframe 初始化失败', EMBED_ERROR_CODES.IFRAME_NOT_READY));
|
|
4082
|
+
});
|
|
3963
4083
|
this.container.appendChild(iframe);
|
|
4084
|
+
initTimeoutId = setTimeout(() => {
|
|
4085
|
+
finalizeFailure(new AlgeoError('iframe 初始化超时', EMBED_ERROR_CODES.TIMEOUT));
|
|
4086
|
+
}, EMBED_TIMEOUT_MS);
|
|
3964
4087
|
this.messageHandler = (e) => {
|
|
3965
|
-
if (e.source !== iframe.contentWindow)
|
|
4088
|
+
if (e.source !== iframe.contentWindow) {
|
|
3966
4089
|
return;
|
|
4090
|
+
}
|
|
3967
4091
|
const { data } = e;
|
|
3968
4092
|
if (isReadyMessage(data)) {
|
|
4093
|
+
if (this._ready) {
|
|
4094
|
+
return;
|
|
4095
|
+
}
|
|
3969
4096
|
this._ready = true;
|
|
3970
4097
|
this._version = data.version;
|
|
3971
|
-
|
|
4098
|
+
this.emit('ready', {
|
|
4099
|
+
type: 'ready',
|
|
4100
|
+
mode: this.embedMode,
|
|
4101
|
+
version: this._version,
|
|
4102
|
+
});
|
|
4103
|
+
finalizeSuccess();
|
|
3972
4104
|
return;
|
|
3973
4105
|
}
|
|
3974
4106
|
if (isResponseMessage(data)) {
|
|
3975
|
-
const
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
const err = data.error;
|
|
3984
|
-
pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
|
|
3985
|
-
}
|
|
4107
|
+
const pending = this.pending.get(data.requestId);
|
|
4108
|
+
if (!pending) {
|
|
4109
|
+
return;
|
|
4110
|
+
}
|
|
4111
|
+
this.pending.delete(data.requestId);
|
|
4112
|
+
if (data.success) {
|
|
4113
|
+
pending.resolve(data.result);
|
|
4114
|
+
return;
|
|
3986
4115
|
}
|
|
4116
|
+
const err = data.error;
|
|
4117
|
+
pending.reject(new AlgeoError(err?.message ?? '未知错误', err?.code ?? EMBED_ERROR_CODES.UNKNOWN_ERROR, err?.details));
|
|
4118
|
+
return;
|
|
4119
|
+
}
|
|
4120
|
+
if (isSaveRequestMessage(data) &&
|
|
4121
|
+
this.handleRequestMessage(data, iframe.contentWindow)) {
|
|
4122
|
+
return;
|
|
4123
|
+
}
|
|
4124
|
+
if (this.acceptsEventMessage() && isEmbedEventMessage(data)) {
|
|
4125
|
+
this.handleEventMessage(data);
|
|
4126
|
+
this.emit(data.type, data);
|
|
3987
4127
|
}
|
|
3988
4128
|
};
|
|
3989
4129
|
window.addEventListener('message', this.messageHandler);
|
|
@@ -3993,77 +4133,372 @@ class AlgeoSdk {
|
|
|
3993
4133
|
const requestId = generateRequestId();
|
|
3994
4134
|
const msg = { type, requestId, ...payload };
|
|
3995
4135
|
return new Promise((resolve, reject) => {
|
|
4136
|
+
if (this.destroyed) {
|
|
4137
|
+
reject(new AlgeoError('SDK 已销毁', EMBED_ERROR_CODES.DESTROYED));
|
|
4138
|
+
return;
|
|
4139
|
+
}
|
|
3996
4140
|
if (!this._ready) {
|
|
3997
4141
|
reject(new AlgeoError('iframe 未加载完成', EMBED_ERROR_CODES.IFRAME_NOT_READY));
|
|
4142
|
+
return;
|
|
3998
4143
|
}
|
|
3999
|
-
this.pending.set(requestId, {
|
|
4000
|
-
resolve: resolve,
|
|
4001
|
-
reject,
|
|
4002
|
-
});
|
|
4003
4144
|
const target = this.iframe?.contentWindow;
|
|
4004
4145
|
if (!target) {
|
|
4005
|
-
this.pending.delete(requestId);
|
|
4006
4146
|
reject(new AlgeoError('iframe 未加载完成', EMBED_ERROR_CODES.IFRAME_NOT_READY));
|
|
4007
4147
|
return;
|
|
4008
4148
|
}
|
|
4149
|
+
this.pending.set(requestId, {
|
|
4150
|
+
resolve: resolve,
|
|
4151
|
+
reject,
|
|
4152
|
+
});
|
|
4009
4153
|
target.postMessage(msg, '*');
|
|
4010
|
-
// 超时兜底
|
|
4011
4154
|
setTimeout(() => {
|
|
4012
4155
|
if (this.pending.has(requestId)) {
|
|
4013
4156
|
this.pending.delete(requestId);
|
|
4014
4157
|
reject(new AlgeoError('请求超时', EMBED_ERROR_CODES.TIMEOUT));
|
|
4015
4158
|
}
|
|
4016
|
-
},
|
|
4159
|
+
}, EMBED_TIMEOUT_MS);
|
|
4017
4160
|
});
|
|
4018
4161
|
}
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
|
|
4162
|
+
async destroy() {
|
|
4163
|
+
if (this.destroyed) {
|
|
4164
|
+
return;
|
|
4165
|
+
}
|
|
4166
|
+
this.destroyed = true;
|
|
4167
|
+
this.cleanupMessageHandler();
|
|
4168
|
+
this.resetRuntimeState();
|
|
4169
|
+
this.pending.forEach(({ reject }) => {
|
|
4170
|
+
reject(new AlgeoError('SDK 已销毁', EMBED_ERROR_CODES.DESTROYED));
|
|
4171
|
+
});
|
|
4172
|
+
this.pending.clear();
|
|
4173
|
+
this.cleanupIframe();
|
|
4024
4174
|
}
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4175
|
+
}
|
|
4176
|
+
|
|
4177
|
+
class EmbeddedPresentation extends EmbeddedTarget {
|
|
4178
|
+
constructor(container) {
|
|
4179
|
+
super(container, 'presentation');
|
|
4180
|
+
this.currentSlideIndex = 0;
|
|
4181
|
+
this.slideCount = 0;
|
|
4182
|
+
}
|
|
4183
|
+
async initialize(options = {}, baseUrl) {
|
|
4184
|
+
await this.init({
|
|
4185
|
+
baseUrl,
|
|
4186
|
+
initialId: options.shareId,
|
|
4187
|
+
});
|
|
4030
4188
|
}
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
*/
|
|
4034
|
-
switchSlide(index) {
|
|
4035
|
-
return this.post('switchSlide', { index });
|
|
4189
|
+
acceptsEventMessage() {
|
|
4190
|
+
return false;
|
|
4036
4191
|
}
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
*/
|
|
4040
|
-
getSlideCount() {
|
|
4041
|
-
return this.post('getSlideCount', {});
|
|
4192
|
+
setWhitelistError(error) {
|
|
4193
|
+
this.whitelistError = error;
|
|
4042
4194
|
}
|
|
4043
|
-
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
|
|
4195
|
+
ensureWhitelistAccess(methodName) {
|
|
4196
|
+
if (!this.whitelistError) {
|
|
4197
|
+
return;
|
|
4198
|
+
}
|
|
4199
|
+
const error = new AlgeoError(`演示模式调用 ${methodName} 被白名单限制。${this.whitelistError.message}`, this.whitelistError.code, this.whitelistError.details);
|
|
4200
|
+
console.error(error);
|
|
4201
|
+
throw error;
|
|
4202
|
+
}
|
|
4203
|
+
async loadShareById(id) {
|
|
4204
|
+
this.ensureWhitelistAccess('loadShareById');
|
|
4205
|
+
const result = await this.post('loadShareById', {
|
|
4206
|
+
id,
|
|
4207
|
+
});
|
|
4208
|
+
this.currentContent = undefined;
|
|
4209
|
+
this.currentSlideIndex = 0;
|
|
4210
|
+
this.slideCount = 0;
|
|
4211
|
+
return result;
|
|
4212
|
+
}
|
|
4213
|
+
async loadFile(content) {
|
|
4214
|
+
this.ensureWhitelistAccess('loadFile');
|
|
4215
|
+
const result = await this.post('loadContent', { content });
|
|
4216
|
+
this.currentContent = content;
|
|
4217
|
+
this.currentSlideIndex = 0;
|
|
4218
|
+
this.slideCount = content.slides.length;
|
|
4219
|
+
return result;
|
|
4220
|
+
}
|
|
4221
|
+
async switchSlide(index) {
|
|
4222
|
+
this.ensureWhitelistAccess('switchSlide');
|
|
4223
|
+
const result = await this.post('switchSlide', { index });
|
|
4224
|
+
this.currentSlideIndex = index;
|
|
4225
|
+
return result;
|
|
4226
|
+
}
|
|
4227
|
+
async getSlideCount() {
|
|
4228
|
+
this.ensureWhitelistAccess('getSlideCount');
|
|
4229
|
+
const result = await this.post('getSlideCount', {});
|
|
4230
|
+
this.slideCount = result.count;
|
|
4231
|
+
return result;
|
|
4232
|
+
}
|
|
4233
|
+
async repl(command) {
|
|
4234
|
+
this.ensureWhitelistAccess('repl');
|
|
4048
4235
|
return this.post('repl', { command });
|
|
4049
4236
|
}
|
|
4050
|
-
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
|
|
4237
|
+
}
|
|
4238
|
+
|
|
4239
|
+
class EmbeddedEditor extends EmbeddedTarget {
|
|
4240
|
+
constructor(container) {
|
|
4241
|
+
super(container, 'editor');
|
|
4242
|
+
this.currentSlideIndex = 0;
|
|
4243
|
+
this.slideCount = 0;
|
|
4244
|
+
this.historyCount = 0;
|
|
4245
|
+
this.historyCurrentIndex = -1;
|
|
4246
|
+
this.uiConfig = {};
|
|
4247
|
+
this.document = {
|
|
4248
|
+
loadContent: async (content) => {
|
|
4249
|
+
await this.loadContent(content, 'loadContent');
|
|
4250
|
+
},
|
|
4251
|
+
getContent: async () => {
|
|
4252
|
+
const result = await this.post('getContent', {});
|
|
4253
|
+
this.currentContent = result.content;
|
|
4254
|
+
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(result.content.slides.length - 1, 0));
|
|
4255
|
+
this.slideCount = result.content.slides.length;
|
|
4256
|
+
await this.refreshHistoryState();
|
|
4257
|
+
return result.content;
|
|
4258
|
+
},
|
|
4259
|
+
};
|
|
4260
|
+
this.slides = {
|
|
4261
|
+
getCount: () => this.slideCount,
|
|
4262
|
+
getCurrentIndex: () => this.currentSlideIndex,
|
|
4263
|
+
switchTo: async (index) => {
|
|
4264
|
+
await this.switchTo(index);
|
|
4265
|
+
},
|
|
4266
|
+
add: async () => this.addSlide('addSlide', {}),
|
|
4267
|
+
addAt: async (index) => this.addSlide('addSlideAt', { index }),
|
|
4268
|
+
remove: async (index) => {
|
|
4269
|
+
await this.post('removeSlide', { index });
|
|
4270
|
+
this.slideCount = Math.max(0, this.slideCount - 1);
|
|
4271
|
+
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
|
|
4272
|
+
await this.refreshHistoryState();
|
|
4273
|
+
},
|
|
4274
|
+
duplicate: async (index, targetIndex) => this.addSlide('duplicateSlide', { index, targetIndex }),
|
|
4275
|
+
reorder: async (fromIndex, toIndex) => {
|
|
4276
|
+
await this.post('reorderSlide', { fromIndex, toIndex });
|
|
4277
|
+
if (this.currentSlideIndex === fromIndex) {
|
|
4278
|
+
this.currentSlideIndex = toIndex;
|
|
4279
|
+
}
|
|
4280
|
+
await this.refreshHistoryState();
|
|
4281
|
+
},
|
|
4282
|
+
};
|
|
4283
|
+
this.history = {
|
|
4284
|
+
getCount: () => this.historyCount,
|
|
4285
|
+
getCurrentIndex: () => this.historyCurrentIndex,
|
|
4286
|
+
undo: async () => {
|
|
4287
|
+
await this.post('undo', {});
|
|
4288
|
+
await this.refreshHistoryState();
|
|
4289
|
+
},
|
|
4290
|
+
redo: async () => {
|
|
4291
|
+
await this.post('redo', {});
|
|
4292
|
+
await this.refreshHistoryState();
|
|
4293
|
+
},
|
|
4294
|
+
jumpTo: async (index) => {
|
|
4295
|
+
await this.post('jumpHistory', { index });
|
|
4296
|
+
await this.refreshHistoryState();
|
|
4297
|
+
},
|
|
4298
|
+
canUndo: () => this.historyCurrentIndex >= 0,
|
|
4299
|
+
canRedo: () => this.historyCurrentIndex < this.historyCount - 1,
|
|
4300
|
+
clear: async () => {
|
|
4301
|
+
await this.post('clearHistory', {});
|
|
4302
|
+
await this.refreshHistoryState();
|
|
4303
|
+
},
|
|
4304
|
+
};
|
|
4305
|
+
this.mode = {
|
|
4306
|
+
getUiConfig: () => ({ ...this.uiConfig }),
|
|
4307
|
+
setUiConfig: async (config) => {
|
|
4308
|
+
await this.post('setUiConfig', { config });
|
|
4309
|
+
this.uiConfig = {
|
|
4310
|
+
...this.uiConfig,
|
|
4311
|
+
...config,
|
|
4312
|
+
};
|
|
4313
|
+
},
|
|
4314
|
+
};
|
|
4315
|
+
}
|
|
4316
|
+
async initialize(options = {}, baseUrl) {
|
|
4317
|
+
if (!options.auth?.appId?.trim()) {
|
|
4318
|
+
throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
|
|
4057
4319
|
}
|
|
4058
|
-
this.
|
|
4059
|
-
this.
|
|
4060
|
-
|
|
4061
|
-
|
|
4320
|
+
this.uiConfig = options.ui || {};
|
|
4321
|
+
await this.init({
|
|
4322
|
+
baseUrl,
|
|
4323
|
+
auth: options.auth,
|
|
4324
|
+
initialId: options.shareId,
|
|
4325
|
+
});
|
|
4326
|
+
const content = await this.document.getContent();
|
|
4327
|
+
this.currentContent = content;
|
|
4328
|
+
this.slideCount = content.slides.length;
|
|
4329
|
+
this.currentSlideIndex = 0;
|
|
4330
|
+
await this.refreshHistoryState();
|
|
4331
|
+
if (options.initialContent) {
|
|
4332
|
+
await this.loadContent(options.initialContent, 'initialContent');
|
|
4062
4333
|
}
|
|
4063
4334
|
}
|
|
4335
|
+
handleEventMessage(event) {
|
|
4336
|
+
if (event.type === 'contentChange') {
|
|
4337
|
+
if (event.content) {
|
|
4338
|
+
this.currentContent = event.content;
|
|
4339
|
+
this.slideCount = event.content.slides.length;
|
|
4340
|
+
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
|
|
4341
|
+
}
|
|
4342
|
+
return;
|
|
4343
|
+
}
|
|
4344
|
+
if (event.type === 'slideChange') {
|
|
4345
|
+
this.currentSlideIndex = event.index;
|
|
4346
|
+
return;
|
|
4347
|
+
}
|
|
4348
|
+
this.currentContent = event.content;
|
|
4349
|
+
this.slideCount = event.content.slides.length;
|
|
4350
|
+
this.currentSlideIndex = Math.min(this.currentSlideIndex, Math.max(this.slideCount - 1, 0));
|
|
4351
|
+
}
|
|
4352
|
+
handleRequestMessage(message, sourceWindow) {
|
|
4353
|
+
if (message.type !== 'save') {
|
|
4354
|
+
return false;
|
|
4355
|
+
}
|
|
4356
|
+
void this.handleSaveRequest(message, sourceWindow);
|
|
4357
|
+
return true;
|
|
4358
|
+
}
|
|
4359
|
+
async loadContent(content, source) {
|
|
4360
|
+
await this.post('loadContent', { content });
|
|
4361
|
+
this.currentContent = content;
|
|
4362
|
+
this.currentSlideIndex = 0;
|
|
4363
|
+
this.slideCount = content.slides.length;
|
|
4364
|
+
await this.refreshHistoryState();
|
|
4365
|
+
}
|
|
4366
|
+
async switchTo(index) {
|
|
4367
|
+
await this.post('switchSlide', { index });
|
|
4368
|
+
this.currentSlideIndex = index;
|
|
4369
|
+
}
|
|
4370
|
+
async addSlide(command, payload) {
|
|
4371
|
+
const result = await this.post(command, payload);
|
|
4372
|
+
this.slideCount = Math.max(this.slideCount + 1, result.index + 1);
|
|
4373
|
+
this.currentSlideIndex = result.index;
|
|
4374
|
+
await this.refreshHistoryState();
|
|
4375
|
+
return result;
|
|
4376
|
+
}
|
|
4377
|
+
async refreshHistoryState() {
|
|
4378
|
+
const state = await this.post('getHistoryState', {});
|
|
4379
|
+
this.historyCount = state.count;
|
|
4380
|
+
this.historyCurrentIndex = state.currentIndex;
|
|
4381
|
+
return state;
|
|
4382
|
+
}
|
|
4383
|
+
async handleSaveRequest(message, sourceWindow) {
|
|
4384
|
+
const respond = (payload) => {
|
|
4385
|
+
sourceWindow.postMessage(payload, '*');
|
|
4386
|
+
};
|
|
4387
|
+
try {
|
|
4388
|
+
const result = await this.resolveSaveResult(message.content);
|
|
4389
|
+
respond({
|
|
4390
|
+
type: 'response',
|
|
4391
|
+
requestId: message.requestId,
|
|
4392
|
+
success: true,
|
|
4393
|
+
result,
|
|
4394
|
+
});
|
|
4395
|
+
}
|
|
4396
|
+
catch (error) {
|
|
4397
|
+
respond({
|
|
4398
|
+
type: 'response',
|
|
4399
|
+
requestId: message.requestId,
|
|
4400
|
+
success: false,
|
|
4401
|
+
error: {
|
|
4402
|
+
code: EMBED_ERROR_CODES.UNKNOWN_ERROR,
|
|
4403
|
+
message: error instanceof Error ? error.message : String(error),
|
|
4404
|
+
},
|
|
4405
|
+
});
|
|
4406
|
+
}
|
|
4407
|
+
}
|
|
4408
|
+
async resolveSaveResult(content) {
|
|
4409
|
+
const requestEvent = {
|
|
4410
|
+
type: 'save',
|
|
4411
|
+
stage: 'request',
|
|
4412
|
+
content,
|
|
4413
|
+
};
|
|
4414
|
+
for (const listener of this.getListeners('save')) {
|
|
4415
|
+
const result = await listener(requestEvent);
|
|
4416
|
+
if (result && typeof result === 'object' && 'status' in result) {
|
|
4417
|
+
return result;
|
|
4418
|
+
}
|
|
4419
|
+
}
|
|
4420
|
+
return {
|
|
4421
|
+
status: 'error',
|
|
4422
|
+
message: '宿主未配置 save 事件处理器',
|
|
4423
|
+
};
|
|
4424
|
+
}
|
|
4425
|
+
}
|
|
4426
|
+
|
|
4427
|
+
const WHITELIST_CHECK_BASE_URL = 'https://open.dajiaoai.com/console';
|
|
4428
|
+
const WHITELIST_CHECK_PATH = '/api/whitelist/check';
|
|
4429
|
+
async function checkPresentationWhitelist(options) {
|
|
4430
|
+
const appId = options.appId?.trim() ?? '';
|
|
4431
|
+
const host = window.location.host.trim();
|
|
4432
|
+
const url = new URL(`${normalizeBaseUrl(WHITELIST_CHECK_BASE_URL)}${WHITELIST_CHECK_PATH}`);
|
|
4433
|
+
url.searchParams.set('appId', appId);
|
|
4434
|
+
url.searchParams.set('host', host);
|
|
4435
|
+
let matched = false;
|
|
4436
|
+
try {
|
|
4437
|
+
const response = await fetch(url.toString(), {
|
|
4438
|
+
method: 'GET',
|
|
4439
|
+
cache: 'no-store',
|
|
4440
|
+
});
|
|
4441
|
+
if (response.ok) {
|
|
4442
|
+
const payload = (await response.json());
|
|
4443
|
+
matched = payload.matched === true;
|
|
4444
|
+
}
|
|
4445
|
+
}
|
|
4446
|
+
catch {
|
|
4447
|
+
matched = false;
|
|
4448
|
+
}
|
|
4449
|
+
if (matched) {
|
|
4450
|
+
return null;
|
|
4451
|
+
}
|
|
4452
|
+
return new AlgeoError(`白名单校验未通过,appId="${appId || '(empty)'}",host="${host || '(empty)'}"。`, EMBED_ERROR_CODES.BAD_REQUEST);
|
|
4453
|
+
}
|
|
4454
|
+
async function createEditorInstance(container, options = {}, baseUrl) {
|
|
4455
|
+
const editor = new EmbeddedEditor(container);
|
|
4456
|
+
await editor.initialize(options, baseUrl);
|
|
4457
|
+
return editor;
|
|
4458
|
+
}
|
|
4459
|
+
async function createPresentationInstance(container, options = {}, baseUrl) {
|
|
4460
|
+
const presentation = new EmbeddedPresentation(container);
|
|
4461
|
+
await presentation.initialize(options, baseUrl);
|
|
4462
|
+
const whitelistError = await checkPresentationWhitelist(options);
|
|
4463
|
+
if (whitelistError) {
|
|
4464
|
+
presentation.setWhitelistError(whitelistError);
|
|
4465
|
+
}
|
|
4466
|
+
return presentation;
|
|
4467
|
+
}
|
|
4468
|
+
function createEmbeddedInstance(container, options) {
|
|
4469
|
+
if (options.mode === 'editor') {
|
|
4470
|
+
return createEditorInstance(container, options.editor, options.baseUrl);
|
|
4471
|
+
}
|
|
4472
|
+
return createPresentationInstance(container, options.presentation, options.baseUrl);
|
|
4473
|
+
}
|
|
4474
|
+
class AlgeoSdk {
|
|
4475
|
+
constructor() { }
|
|
4476
|
+
static async create(container, options) {
|
|
4477
|
+
return createEmbeddedInstance(container, options);
|
|
4478
|
+
}
|
|
4479
|
+
static async createEditor(container, options = {}) {
|
|
4480
|
+
return createEditor(container, options);
|
|
4481
|
+
}
|
|
4482
|
+
static async createPresentation(container, options = {}) {
|
|
4483
|
+
return createPresentation(container, options);
|
|
4484
|
+
}
|
|
4485
|
+
}
|
|
4486
|
+
async function create(container, options) {
|
|
4487
|
+
return createEmbeddedInstance(container, options);
|
|
4488
|
+
}
|
|
4489
|
+
async function createEditor(container, options = {}) {
|
|
4490
|
+
return createEditorInstance(container, options);
|
|
4491
|
+
}
|
|
4492
|
+
async function createPresentation(container, options = {}) {
|
|
4493
|
+
return createPresentationInstance(container, options);
|
|
4064
4494
|
}
|
|
4065
4495
|
|
|
4066
4496
|
exports.AlgeoError = AlgeoError;
|
|
4067
4497
|
exports.AlgeoSdk = AlgeoSdk;
|
|
4068
4498
|
exports.EMBED_ERROR_CODES = EMBED_ERROR_CODES;
|
|
4499
|
+
exports.EmbeddedEditor = EmbeddedEditor;
|
|
4500
|
+
exports.EmbeddedPresentation = EmbeddedPresentation;
|
|
4069
4501
|
exports.VERSION = VERSION;
|
|
4502
|
+
exports.create = create;
|
|
4503
|
+
exports.createEditor = createEditor;
|
|
4504
|
+
exports.createPresentation = createPresentation;
|