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