@eworkbase/sdk 0.8.5 → 0.8.6
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/dist/build.d.ts +54 -0
- package/dist/build.js +78 -0
- package/dist/components-DxL-99bM.js +131 -0
- package/dist/host-services-cIH3aYQD.js +5 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -1
- package/dist/markdown.d.ts +1 -1
- package/dist/{types-WIMkhmH4.d.ts → types-DDEHTBRv.d.ts} +11 -1
- package/dist/{types-CPObI29j.d.ts → types-b6G-gae3.d.ts} +10 -2
- package/dist/ui/components.js +1 -130
- package/dist/ui/hooks.d.ts +70 -0
- package/dist/ui/hooks.js +262 -0
- package/dist/ui/index.d.ts +22 -2
- package/dist/ui/index.js +3 -366
- package/dist/ui/markdown-links.d.ts +1 -1
- package/dist/ui/model.d.ts +1 -1
- package/dist/ui-DGLeDJDe.js +436 -0
- package/package.json +14 -2
- package/tsconfig.node.json +13 -0
- package/tsconfig.ui.json +18 -0
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import "./host-services-cIH3aYQD.js";
|
|
2
|
+
import { WindowMessenger, connect } from "penpal";
|
|
3
|
+
//#region src/ui/controller.ts
|
|
4
|
+
const MAX_CONTENT_HEIGHT = 1e4;
|
|
5
|
+
const MAX_FATAL_MESSAGE_LENGTH = 1e3;
|
|
6
|
+
const MIN_CONTENT_HEIGHT = 1;
|
|
7
|
+
const DEFAULT_VIEW_STATE_TOPIC = "default";
|
|
8
|
+
let PluginUiHostMethod = /* @__PURE__ */ function(PluginUiHostMethod) {
|
|
9
|
+
PluginUiHostMethod["InteractionDismiss"] = "interaction.dismiss";
|
|
10
|
+
PluginUiHostMethod["InteractionRespond"] = "interaction.respond";
|
|
11
|
+
PluginUiHostMethod["CommandSubscribe"] = "command.subscribe";
|
|
12
|
+
PluginUiHostMethod["CommandCancel"] = "command.cancel";
|
|
13
|
+
PluginUiHostMethod["CommandStart"] = "command.start";
|
|
14
|
+
PluginUiHostMethod["CommandQuery"] = "command.query";
|
|
15
|
+
PluginUiHostMethod["CommandReply"] = "command.reply";
|
|
16
|
+
PluginUiHostMethod["CommandClear"] = "command.clear";
|
|
17
|
+
PluginUiHostMethod["CommandExecute"] = "command.execute";
|
|
18
|
+
PluginUiHostMethod["DialogPrompt"] = "dialog.prompt";
|
|
19
|
+
PluginUiHostMethod["DesktopOpen"] = "desktop.open";
|
|
20
|
+
PluginUiHostMethod["RepositoryAuthorizePush"] = "repository.authorizePush";
|
|
21
|
+
PluginUiHostMethod["RepositoryPush"] = "repository.push";
|
|
22
|
+
PluginUiHostMethod["FileSelect"] = "file.select";
|
|
23
|
+
PluginUiHostMethod["Navigate"] = "navigate";
|
|
24
|
+
PluginUiHostMethod["OutputOpen"] = "output.open";
|
|
25
|
+
PluginUiHostMethod["StatusResolve"] = "status.resolve";
|
|
26
|
+
PluginUiHostMethod["ToastShow"] = "toast.show";
|
|
27
|
+
PluginUiHostMethod["ViewCall"] = "view.call";
|
|
28
|
+
PluginUiHostMethod["ViewModified"] = "view.modified";
|
|
29
|
+
return PluginUiHostMethod;
|
|
30
|
+
}({});
|
|
31
|
+
function createPluginUiController(definition, options) {
|
|
32
|
+
const onInit = definition?.onInit;
|
|
33
|
+
const connectToHost = options?.connectToHost;
|
|
34
|
+
const targetWindow = options?.targetWindow;
|
|
35
|
+
if (typeof onInit !== "function") throw new TypeError("defineWorkbasePluginUi requires an onInit callback.");
|
|
36
|
+
if (!targetWindow || typeof connectToHost !== "function") throw new TypeError("Plugin UI transport is unavailable.");
|
|
37
|
+
const state = {
|
|
38
|
+
fatalReportPromise: null,
|
|
39
|
+
isInitialized: false,
|
|
40
|
+
isFillLayout: false,
|
|
41
|
+
supportsRepositoryPush: false,
|
|
42
|
+
supportsRepositoryPushAuthorization: false,
|
|
43
|
+
contentHeight: null,
|
|
44
|
+
contextIdentity: null,
|
|
45
|
+
workspaceId: null,
|
|
46
|
+
recoverableHostErrors: /* @__PURE__ */ new WeakSet()
|
|
47
|
+
};
|
|
48
|
+
const executionListeners = /* @__PURE__ */ new Set();
|
|
49
|
+
const fileListeners = /* @__PURE__ */ new Set();
|
|
50
|
+
const stateListeners = /* @__PURE__ */ new Map();
|
|
51
|
+
const latestStates = /* @__PURE__ */ new Map();
|
|
52
|
+
const pendingStateChanges = /* @__PURE__ */ new Map();
|
|
53
|
+
let initialization = 0;
|
|
54
|
+
let refreshing = false;
|
|
55
|
+
let pending = false;
|
|
56
|
+
let lifecycle = null;
|
|
57
|
+
const deliverStateChange = (topic, nextState) => {
|
|
58
|
+
const listeners = stateListeners.get(topic);
|
|
59
|
+
if (!listeners) return;
|
|
60
|
+
[...listeners].forEach((listener) => listener(nextState));
|
|
61
|
+
};
|
|
62
|
+
const flushPendingStates = () => {
|
|
63
|
+
if (!pendingStateChanges.size) return;
|
|
64
|
+
const queued = [...pendingStateChanges.entries()];
|
|
65
|
+
pendingStateChanges.clear();
|
|
66
|
+
queued.forEach(([topic, nextState]) => deliverStateChange(topic, nextState));
|
|
67
|
+
};
|
|
68
|
+
const clientMethods = {
|
|
69
|
+
filesChanged: async () => {
|
|
70
|
+
pending = true;
|
|
71
|
+
if (refreshing || initialization || !state.isInitialized) return;
|
|
72
|
+
refreshing = true;
|
|
73
|
+
try {
|
|
74
|
+
while (pending) {
|
|
75
|
+
pending = false;
|
|
76
|
+
await Promise.all([...fileListeners].map((refresh) => refresh()));
|
|
77
|
+
}
|
|
78
|
+
} finally {
|
|
79
|
+
refreshing = false;
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
executionChanged: async (change) => {
|
|
83
|
+
if (state.isInitialized) [...executionListeners].forEach((listener) => listener(change));
|
|
84
|
+
},
|
|
85
|
+
stateChanged: async (change) => {
|
|
86
|
+
if (!isRecord(change) || typeof change.topic !== "string" || !change.topic) return;
|
|
87
|
+
latestStates.set(change.topic, change.state);
|
|
88
|
+
if (!state.isInitialized || initialization > 0) {
|
|
89
|
+
pendingStateChanges.set(change.topic, change.state);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
deliverStateChange(change.topic, change.state);
|
|
93
|
+
},
|
|
94
|
+
initialize: async (payload) => {
|
|
95
|
+
assertInitializationPayload(payload);
|
|
96
|
+
const nextIdentity = JSON.stringify([
|
|
97
|
+
payload.project.id,
|
|
98
|
+
payload.workspace.id,
|
|
99
|
+
payload.activationGeneration ?? 0
|
|
100
|
+
]);
|
|
101
|
+
if (state.contextIdentity !== null && state.contextIdentity !== nextIdentity) {
|
|
102
|
+
latestStates.clear();
|
|
103
|
+
pendingStateChanges.clear();
|
|
104
|
+
}
|
|
105
|
+
state.contextIdentity = nextIdentity;
|
|
106
|
+
state.isInitialized = true;
|
|
107
|
+
state.isFillLayout = payload.layout === "fill";
|
|
108
|
+
state.workspaceId = payload.workspace.id;
|
|
109
|
+
state.supportsRepositoryPush = payload.supportsRepositoryPush === true;
|
|
110
|
+
state.supportsRepositoryPushAuthorization = payload.supportsRepositoryPushAuthorization === true;
|
|
111
|
+
initialization++;
|
|
112
|
+
try {
|
|
113
|
+
await onInit(payload);
|
|
114
|
+
} finally {
|
|
115
|
+
initialization--;
|
|
116
|
+
}
|
|
117
|
+
flushPendingStates();
|
|
118
|
+
if (pending) await clientMethods.filesChanged();
|
|
119
|
+
if (state.isFillLayout) return;
|
|
120
|
+
lifecycle?.observeContentHeight();
|
|
121
|
+
await waitForPluginUiPaint(targetWindow);
|
|
122
|
+
await reportContentHeight(hostPromise, {
|
|
123
|
+
targetWindow,
|
|
124
|
+
state
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const connection = connectToHost(clientMethods);
|
|
129
|
+
const hostPromise = connection.promise;
|
|
130
|
+
const request = createHostRequest(hostPromise, state);
|
|
131
|
+
lifecycle = bindPluginUiLifecycle({
|
|
132
|
+
connection,
|
|
133
|
+
state,
|
|
134
|
+
targetWindow
|
|
135
|
+
});
|
|
136
|
+
const dispose = () => {
|
|
137
|
+
executionListeners.clear();
|
|
138
|
+
fileListeners.clear();
|
|
139
|
+
stateListeners.clear();
|
|
140
|
+
latestStates.clear();
|
|
141
|
+
pendingStateChanges.clear();
|
|
142
|
+
lifecycle?.dispose();
|
|
143
|
+
};
|
|
144
|
+
const subscribe = async (onChange) => {
|
|
145
|
+
const listener = (change) => onChange(change);
|
|
146
|
+
executionListeners.add(listener);
|
|
147
|
+
try {
|
|
148
|
+
await request("command.subscribe", { enabled: true });
|
|
149
|
+
} catch (error) {
|
|
150
|
+
executionListeners.delete(listener);
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
return async () => {
|
|
154
|
+
if (!executionListeners.delete(listener) || executionListeners.size) return;
|
|
155
|
+
await request("command.subscribe", { enabled: false });
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
const onFilesChanged = (refresh) => {
|
|
159
|
+
fileListeners.add(refresh);
|
|
160
|
+
return () => {
|
|
161
|
+
fileListeners.delete(refresh);
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
const onStateChanged = (listener, topic = DEFAULT_VIEW_STATE_TOPIC) => {
|
|
165
|
+
const wrapped = (nextState) => listener(nextState);
|
|
166
|
+
const bucket = stateListeners.get(topic) ?? /* @__PURE__ */ new Set();
|
|
167
|
+
bucket.add(wrapped);
|
|
168
|
+
stateListeners.set(topic, bucket);
|
|
169
|
+
let active = true;
|
|
170
|
+
if (latestStates.has(topic) && !pendingStateChanges.has(topic)) {
|
|
171
|
+
const cachedState = latestStates.get(topic);
|
|
172
|
+
queueMicrotask(() => {
|
|
173
|
+
if (active) wrapped(cachedState);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return () => {
|
|
177
|
+
active = false;
|
|
178
|
+
bucket.delete(wrapped);
|
|
179
|
+
if (bucket.size === 0) stateListeners.delete(topic);
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
return createPluginUiApi(request, {
|
|
183
|
+
targetWindow,
|
|
184
|
+
state,
|
|
185
|
+
dispose,
|
|
186
|
+
subscribe,
|
|
187
|
+
onFilesChanged,
|
|
188
|
+
onStateChanged
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
function createHostRequest(hostPromise, state) {
|
|
192
|
+
return async (method, params) => {
|
|
193
|
+
if (!state.isInitialized) throw new Error("插件界面尚未初始化完成。");
|
|
194
|
+
if (method === "repository.push" && !state.supportsRepositoryPush || method === "repository.authorizePush" && !state.supportsRepositoryPushAuthorization) {
|
|
195
|
+
const error = /* @__PURE__ */ new Error("当前 Workbase 版本无法在插件内推送,请在工作区同步仓库。");
|
|
196
|
+
state.recoverableHostErrors.add(error);
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
const response = await (await hostPromise).call(method, params);
|
|
200
|
+
if (isRecord(response) && response.ok === true) return response.result;
|
|
201
|
+
const error = createHostError(response);
|
|
202
|
+
state.recoverableHostErrors.add(error);
|
|
203
|
+
throw error;
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function createHostError(response) {
|
|
207
|
+
const responseError = isRecord(response) && isRecord(response.error) ? response.error : null;
|
|
208
|
+
const message = typeof responseError?.message === "string" ? responseError.message : "宿主操作未完成。";
|
|
209
|
+
const code = typeof responseError?.code === "string" ? responseError.code : "host_error";
|
|
210
|
+
return Object.assign(new Error(message), { code });
|
|
211
|
+
}
|
|
212
|
+
function createPluginUiApi(request, { targetWindow, state, dispose, subscribe, onFilesChanged, onStateChanged }) {
|
|
213
|
+
const commands = Object.freeze({
|
|
214
|
+
subscribe,
|
|
215
|
+
reply: (executionId) => request("command.reply", { executionId }),
|
|
216
|
+
clear: (executionId) => request("command.clear", executionId === void 0 ? {} : { executionId }),
|
|
217
|
+
start: (commandId, input, options = {}) => request("command.start", {
|
|
218
|
+
commandId,
|
|
219
|
+
input,
|
|
220
|
+
executionId: options.executionId ?? targetWindow.crypto.randomUUID()
|
|
221
|
+
}),
|
|
222
|
+
query: (executionId) => request("command.query", { executionId: executionId ?? null }),
|
|
223
|
+
cancel: (executionId) => request("command.cancel", { executionId }),
|
|
224
|
+
execute: (commandId, input, options = {}) => {
|
|
225
|
+
return request("command.execute", {
|
|
226
|
+
commandId,
|
|
227
|
+
executionId: options.executionId ?? targetWindow.crypto.randomUUID(),
|
|
228
|
+
input
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
const status = Object.freeze({ resolve: (statusId, input) => request("status.resolve", {
|
|
233
|
+
input,
|
|
234
|
+
statusId
|
|
235
|
+
}) });
|
|
236
|
+
const view = Object.freeze({
|
|
237
|
+
onFilesChanged,
|
|
238
|
+
onStateChanged,
|
|
239
|
+
setModified: (modified) => request("view.modified", modified),
|
|
240
|
+
call: (method, input) => request("view.call", {
|
|
241
|
+
input,
|
|
242
|
+
method
|
|
243
|
+
})
|
|
244
|
+
});
|
|
245
|
+
const navigate = (to, { replace = false } = {}) => request("navigate", {
|
|
246
|
+
replace,
|
|
247
|
+
to
|
|
248
|
+
});
|
|
249
|
+
const openPluginView = async ({ pluginId, viewId, workspaceId, route, params, replace = false }) => {
|
|
250
|
+
const resolvedWorkspaceId = workspaceId ?? state.workspaceId;
|
|
251
|
+
const search = new URLSearchParams({ viewId });
|
|
252
|
+
if (resolvedWorkspaceId) search.set("workspaceId", resolvedWorkspaceId);
|
|
253
|
+
if (route) search.set("route", route);
|
|
254
|
+
if (params && Object.keys(params).length > 0) search.set("params", JSON.stringify(params));
|
|
255
|
+
await navigate(`/plugins/${encodeURIComponent(pluginId)}?${search.toString()}`, { replace });
|
|
256
|
+
};
|
|
257
|
+
return Object.freeze({
|
|
258
|
+
dispose,
|
|
259
|
+
dismiss: () => request("interaction.dismiss", {}),
|
|
260
|
+
respond: (value) => request("interaction.respond", { value }),
|
|
261
|
+
commands,
|
|
262
|
+
desktop: Object.freeze({ open: (options) => request("desktop.open", options) }),
|
|
263
|
+
repositories: Object.freeze({
|
|
264
|
+
authorizePush: (repositoryIds) => request("repository.authorizePush", repositoryIds),
|
|
265
|
+
push: (targets, authorizationId) => request("repository.push", authorizationId === void 0 ? targets : {
|
|
266
|
+
targets,
|
|
267
|
+
authorizationId
|
|
268
|
+
})
|
|
269
|
+
}),
|
|
270
|
+
navigate,
|
|
271
|
+
openPluginView,
|
|
272
|
+
openOutput: (options) => request("output.open", options),
|
|
273
|
+
selectFiles: (options = {}) => request("file.select", options),
|
|
274
|
+
showPrompt: (options) => request("dialog.prompt", options),
|
|
275
|
+
showToast: (options) => request("toast.show", options),
|
|
276
|
+
status,
|
|
277
|
+
view
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
function bindPluginUiLifecycle({ connection, state, targetWindow }) {
|
|
281
|
+
let resizeFrame = null;
|
|
282
|
+
let resizeObserver = null;
|
|
283
|
+
let isDisposed = false;
|
|
284
|
+
const handleWindowError = (event) => {
|
|
285
|
+
if (!event.error && (event.message === "ResizeObserver loop completed with undelivered notifications." || event.message === "ResizeObserver loop limit exceeded")) return;
|
|
286
|
+
reportFirstFatal(connection.promise, state, event.error ?? event.message);
|
|
287
|
+
};
|
|
288
|
+
const handleUnhandledRejection = (event) => {
|
|
289
|
+
if (event.reason instanceof Error && state.recoverableHostErrors.has(event.reason)) return;
|
|
290
|
+
reportFirstFatal(connection.promise, state, event.reason);
|
|
291
|
+
};
|
|
292
|
+
const startObservingHeight = () => {
|
|
293
|
+
if (isDisposed || resizeObserver || state.isFillLayout) return;
|
|
294
|
+
resizeObserver = new targetWindow.ResizeObserver(() => {
|
|
295
|
+
if (resizeFrame !== null) return;
|
|
296
|
+
resizeFrame = targetWindow.requestAnimationFrame(() => {
|
|
297
|
+
resizeFrame = null;
|
|
298
|
+
if (state.isInitialized && !state.isFillLayout) reportContentHeight(connection.promise, {
|
|
299
|
+
targetWindow,
|
|
300
|
+
state
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
resizeObserver.observe(targetWindow.document.body ?? targetWindow.document.documentElement);
|
|
305
|
+
};
|
|
306
|
+
const cleanup = () => {
|
|
307
|
+
if (isDisposed) return;
|
|
308
|
+
isDisposed = true;
|
|
309
|
+
targetWindow.removeEventListener("error", handleWindowError, true);
|
|
310
|
+
targetWindow.removeEventListener("unhandledrejection", handleUnhandledRejection);
|
|
311
|
+
targetWindow.removeEventListener("beforeunload", cleanup);
|
|
312
|
+
resizeObserver?.disconnect();
|
|
313
|
+
resizeObserver = null;
|
|
314
|
+
if (resizeFrame !== null) targetWindow.cancelAnimationFrame(resizeFrame);
|
|
315
|
+
connection.destroy();
|
|
316
|
+
state.fatalReportPromise = null;
|
|
317
|
+
state.isInitialized = false;
|
|
318
|
+
};
|
|
319
|
+
ignoreConnectionFailure(connection.promise);
|
|
320
|
+
targetWindow.addEventListener("error", handleWindowError, true);
|
|
321
|
+
targetWindow.addEventListener("unhandledrejection", handleUnhandledRejection);
|
|
322
|
+
targetWindow.addEventListener("beforeunload", cleanup, { once: true });
|
|
323
|
+
return {
|
|
324
|
+
observeContentHeight: startObservingHeight,
|
|
325
|
+
dispose: cleanup
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
async function ignoreConnectionFailure(hostPromise) {
|
|
329
|
+
try {
|
|
330
|
+
await hostPromise;
|
|
331
|
+
} catch {}
|
|
332
|
+
}
|
|
333
|
+
async function waitForPluginUiPaint(targetWindow) {
|
|
334
|
+
await new Promise((resolve) => {
|
|
335
|
+
targetWindow.requestAnimationFrame(() => {
|
|
336
|
+
targetWindow.requestAnimationFrame(() => resolve());
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
async function reportFirstFatal(hostPromise, state, error) {
|
|
341
|
+
if (!state.fatalReportPromise) state.fatalReportPromise = sendFatalReport(hostPromise, getFatalMessage(error).slice(0, MAX_FATAL_MESSAGE_LENGTH));
|
|
342
|
+
await state.fatalReportPromise;
|
|
343
|
+
}
|
|
344
|
+
async function sendFatalReport(hostPromise, message) {
|
|
345
|
+
try {
|
|
346
|
+
await (await hostPromise).reportFatal(message);
|
|
347
|
+
} catch {}
|
|
348
|
+
}
|
|
349
|
+
async function reportContentHeight(hostPromise, { targetWindow, state }) {
|
|
350
|
+
const contentElement = targetWindow.document.body ?? targetWindow.document.documentElement;
|
|
351
|
+
const height = Math.max(contentElement.offsetHeight, contentElement.scrollHeight);
|
|
352
|
+
if (!Number.isFinite(height)) return;
|
|
353
|
+
try {
|
|
354
|
+
const host = await hostPromise;
|
|
355
|
+
const contentHeight = Math.min(Math.max(Math.ceil(height), MIN_CONTENT_HEIGHT), MAX_CONTENT_HEIGHT);
|
|
356
|
+
if (!state.isInitialized || state.contentHeight === contentHeight) return;
|
|
357
|
+
state.contentHeight = contentHeight;
|
|
358
|
+
await host.reportContentHeight(contentHeight);
|
|
359
|
+
} catch {}
|
|
360
|
+
}
|
|
361
|
+
function assertInitializationPayload(value) {
|
|
362
|
+
if (!isInitializationPayload(value)) throw new TypeError("插件界面初始化参数无效。");
|
|
363
|
+
}
|
|
364
|
+
function isInitializationPayload(value) {
|
|
365
|
+
if (!isRecord(value)) return false;
|
|
366
|
+
if (value.configurationRevision !== void 0 && typeof value.configurationRevision !== "string") return false;
|
|
367
|
+
if (value.activationGeneration !== void 0 && (!Number.isSafeInteger(value.activationGeneration) || Number(value.activationGeneration) < 0)) return false;
|
|
368
|
+
return isContextIdentity(value.project) && isContextIdentity(value.workspace) && isViewIdentity(value.view);
|
|
369
|
+
}
|
|
370
|
+
function isContextIdentity(value) {
|
|
371
|
+
return isRecord(value) && typeof value.id === "string" && value.id.length > 0;
|
|
372
|
+
}
|
|
373
|
+
function isViewIdentity(value) {
|
|
374
|
+
return isContextIdentity(value) && isRecord(value) && typeof value.pluginId === "string" && value.pluginId.length > 0 && typeof value.title === "string" && value.title.length > 0;
|
|
375
|
+
}
|
|
376
|
+
function isRecord(value) {
|
|
377
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
378
|
+
}
|
|
379
|
+
function getFatalMessage(error) {
|
|
380
|
+
if (error instanceof Error) return error.message;
|
|
381
|
+
if (typeof error === "string" && error.trim().length > 0) return error;
|
|
382
|
+
return "插件界面遇到异常。";
|
|
383
|
+
}
|
|
384
|
+
//#endregion
|
|
385
|
+
//#region src/ui/types.ts
|
|
386
|
+
let PluginDesktopTarget = /* @__PURE__ */ function(PluginDesktopTarget) {
|
|
387
|
+
PluginDesktopTarget["Editor"] = "editor";
|
|
388
|
+
PluginDesktopTarget["Terminal"] = "terminal";
|
|
389
|
+
PluginDesktopTarget["Folder"] = "folder";
|
|
390
|
+
PluginDesktopTarget["Chatgpt"] = "chatgpt";
|
|
391
|
+
return PluginDesktopTarget;
|
|
392
|
+
}({});
|
|
393
|
+
//#endregion
|
|
394
|
+
//#region src/ui/index.ts
|
|
395
|
+
/**
|
|
396
|
+
* Penpal browser entry for trusted Workbase plugin views.
|
|
397
|
+
*/
|
|
398
|
+
const BRIDGE_CHANNEL = "workbase-plugin-ui-v1";
|
|
399
|
+
const HOST_ORIGIN_PARAMETER = "__workbaseHostOrigin";
|
|
400
|
+
/** Creates an end-to-end typed wrapper over the Workbase plugin UI bridge with zero runtime schema overhead. */
|
|
401
|
+
function createPluginClient(host) {
|
|
402
|
+
return Object.freeze({
|
|
403
|
+
host,
|
|
404
|
+
view: (method, ...args) => host.view.call(method, args[0] ?? {}),
|
|
405
|
+
command: (commandId, ...args) => host.commands.execute(commandId, args[0] ?? {}, args[1]),
|
|
406
|
+
status: (statusId, ...args) => host.status.resolve(statusId, args[0] ?? {}),
|
|
407
|
+
onState: (topic, listener) => typeof host.view.onStateChanged === "function" ? host.view.onStateChanged(listener, topic || "default") : () => {}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
function defineWorkbasePluginUi(definition) {
|
|
411
|
+
const targetWindow = window;
|
|
412
|
+
return createPluginUiController(definition, {
|
|
413
|
+
connectToHost: (methods) => createPenpalConnection(targetWindow, methods),
|
|
414
|
+
targetWindow
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
function createPenpalConnection(targetWindow, methods) {
|
|
418
|
+
const connection = connect({
|
|
419
|
+
channel: BRIDGE_CHANNEL,
|
|
420
|
+
messenger: new WindowMessenger({
|
|
421
|
+
allowedOrigins: resolveParentOrigins(targetWindow),
|
|
422
|
+
remoteWindow: targetWindow.parent
|
|
423
|
+
}),
|
|
424
|
+
methods
|
|
425
|
+
});
|
|
426
|
+
return {
|
|
427
|
+
destroy: connection.destroy,
|
|
428
|
+
promise: connection.promise
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
function resolveParentOrigins(targetWindow) {
|
|
432
|
+
const hostOrigin = new URL(targetWindow.location.href).searchParams.get(HOST_ORIGIN_PARAMETER);
|
|
433
|
+
return hostOrigin && hostOrigin !== "null" ? [hostOrigin] : ["*"];
|
|
434
|
+
}
|
|
435
|
+
//#endregion
|
|
436
|
+
export { createPluginUiController as a, PluginUiHostMethod as i, defineWorkbasePluginUi as n, PluginDesktopTarget as r, createPluginClient as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eworkbase/sdk",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./markdown": {
|
|
@@ -15,11 +15,21 @@
|
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
16
|
"import": "./dist/index.js"
|
|
17
17
|
},
|
|
18
|
+
"./build": {
|
|
19
|
+
"types": "./dist/build.d.ts",
|
|
20
|
+
"import": "./dist/build.js"
|
|
21
|
+
},
|
|
22
|
+
"./tsconfig.node.json": "./tsconfig.node.json",
|
|
23
|
+
"./tsconfig.ui.json": "./tsconfig.ui.json",
|
|
18
24
|
"./ui": {
|
|
19
25
|
"types": "./dist/ui/index.d.ts",
|
|
20
26
|
"import": "./dist/ui/index.js",
|
|
21
27
|
"default": "./dist/ui/index.js"
|
|
22
28
|
},
|
|
29
|
+
"./ui/hooks": {
|
|
30
|
+
"types": "./dist/ui/hooks.d.ts",
|
|
31
|
+
"import": "./dist/ui/hooks.js"
|
|
32
|
+
},
|
|
23
33
|
"./ui/index.css": {
|
|
24
34
|
"types": "./dist/ui/index.css.d.ts",
|
|
25
35
|
"default": "./dist/ui/index.css"
|
|
@@ -66,7 +76,9 @@
|
|
|
66
76
|
"access": "public"
|
|
67
77
|
},
|
|
68
78
|
"files": [
|
|
69
|
-
"dist"
|
|
79
|
+
"dist",
|
|
80
|
+
"tsconfig.node.json",
|
|
81
|
+
"tsconfig.ui.json"
|
|
70
82
|
],
|
|
71
83
|
"devDependencies": {
|
|
72
84
|
"@tsdown/css": "0.23.0",
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"forceConsistentCasingInFileNames": true,
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"noEmit": true,
|
|
7
|
+
"noUnusedLocals": true,
|
|
8
|
+
"noUnusedParameters": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"target": "ES2024"
|
|
12
|
+
}
|
|
13
|
+
}
|
package/tsconfig.ui.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"forceConsistentCasingInFileNames": true,
|
|
4
|
+
"jsx": "react-jsx",
|
|
5
|
+
"lib": [
|
|
6
|
+
"DOM",
|
|
7
|
+
"ES2024"
|
|
8
|
+
],
|
|
9
|
+
"module": "ESNext",
|
|
10
|
+
"moduleResolution": "Bundler",
|
|
11
|
+
"noEmit": true,
|
|
12
|
+
"noUnusedLocals": true,
|
|
13
|
+
"noUnusedParameters": true,
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
"strict": true,
|
|
16
|
+
"target": "ES2022"
|
|
17
|
+
}
|
|
18
|
+
}
|