@lvce-editor/api 8.43.0 → 8.44.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/dist/index.d.ts CHANGED
@@ -13,7 +13,7 @@ export { executeSourceControlAcceptInput, executeSourceControlAdd, executeSource
13
13
  export { createViewInstance, dispatchViewEvent, disposeViewInstance, executeViewProvider, getViewRegistrySnapshot, renderViewInstance, registerView, resetViewRegistry, saveViewInstanceState, } from './parts/ViewRegistry/ViewRegistry.ts';
14
14
  export { createOutputChannel, getOutputChannelRegistrySnapshot, resetOutputChannelRegistry } from './parts/OutputChannel/OutputChannel.ts';
15
15
  export { getStatusBarItemProviderRegistrySnapshot, registerStatusBarItemProvider, resetStatusBarItemProviderRegistry, } from './parts/StatusBar/StatusBar.ts';
16
- export type { RegisteredView, View, ViewAction, ViewCommand, ViewContext, ViewEvent, ViewKind, MenuEntry, ViewRegistrySnapshot, ViewRenderResult, VirtualDomViewInstance, } from './parts/View/View.ts';
16
+ export type { RegisteredView, View, ViewAction, ViewCommand, ViewContext, ViewEvent, ViewKind, MenuEntry, ViewRegistrySnapshot, ViewRenderResult, ViewSelection, VirtualDomViewInstance, } from './parts/View/View.ts';
17
17
  export { handleExtensionManagementMessagePort } from './parts/HandleExtensionManagementMessagePort/HandleExtensionManagementMessagePort.ts';
18
18
  export { createNodeRpc, createRpc } from './parts/Rpc/Rpc.ts';
19
19
  export type { Command } from './parts/Command/Command.ts';
@@ -1,14 +1,20 @@
1
- import { LazyTransferMessagePortRpcParent, ModuleWorkerRpcParent } from '@lvce-editor/rpc';
1
+ import { LazyTransferMessagePortRpcParent } from '@lvce-editor/rpc';
2
2
  import { ExtensionManagementWorker } from '@lvce-editor/rpc-registry';
3
3
  const sendMessagePortToNode = async (port) => {
4
4
  await ExtensionManagementWorker.invokeAndTransfer('Extensions.sendMessagePortToElectron', port, 'HandleMessagePortForExtensionHostHelperProcess.handleMessagePortForExtensionHostHelperProcess');
5
5
  };
6
+ const sendMessagePortToWebWorker = async (port, name) => {
7
+ await ExtensionManagementWorker.invokeAndTransfer('Extensions.createWebViewWorkerRpc', { name }, port);
8
+ };
6
9
  export const createRpc = async ({ commandMap = {}, name = '', url }) => {
7
- return ModuleWorkerRpcParent.create({
10
+ const rpc = await LazyTransferMessagePortRpcParent.create({
8
11
  commandMap,
9
- name,
10
- url,
12
+ send(port) {
13
+ return sendMessagePortToWebWorker(port, name);
14
+ },
11
15
  });
16
+ await rpc.invoke('LoadFile.loadFile', url);
17
+ return rpc;
12
18
  };
13
19
  export const createNodeRpc = async ({ path }) => {
14
20
  const rpc = await LazyTransferMessagePortRpcParent.create({
@@ -26,6 +26,11 @@ export interface ViewAction {
26
26
  readonly icon: string;
27
27
  readonly title: string;
28
28
  }
29
+ export interface ViewSelection {
30
+ readonly end: number;
31
+ readonly name: string;
32
+ readonly start: number;
33
+ }
29
34
  export interface DomEventListener {
30
35
  readonly capture?: boolean;
31
36
  readonly name: string | number;
@@ -42,6 +47,7 @@ export interface VirtualDomViewInstance {
42
47
  readonly render: () => readonly VirtualDomNode[] | Promise<readonly VirtualDomNode[]>;
43
48
  readonly renderActions?: () => readonly ViewAction[] | Promise<readonly ViewAction[]>;
44
49
  readonly renderFocus?: (oldContext: Readonly<Record<string, boolean>>, newContext: Readonly<Record<string, boolean>>) => string | Promise<string>;
50
+ readonly renderSelections?: () => readonly ViewSelection[] | Promise<readonly ViewSelection[]>;
45
51
  readonly renderTitle?: () => string | Promise<string>;
46
52
  readonly saveState?: () => unknown;
47
53
  }
@@ -72,12 +78,14 @@ export interface ViewRegistrySnapshot {
72
78
  export interface ViewRenderResultDom {
73
79
  readonly dom: readonly VirtualDomNode[];
74
80
  readonly focusSelector?: string;
81
+ readonly selections?: readonly ViewSelection[];
75
82
  readonly title?: string;
76
83
  readonly type: 'setDom';
77
84
  }
78
85
  export interface ViewRenderResultPatches {
79
86
  readonly focusSelector?: string;
80
87
  readonly patches: readonly unknown[];
88
+ readonly selections?: readonly ViewSelection[];
81
89
  readonly title?: string;
82
90
  readonly type: 'setPatches';
83
91
  }
@@ -237,6 +237,26 @@ const normalizeViewActions = (actions) => {
237
237
  }
238
238
  return actions.map(normalizeViewAction);
239
239
  };
240
+ const normalizeViewSelection = (selection, index) => {
241
+ if (!selection || typeof selection !== 'object' || Array.isArray(selection)) {
242
+ throw new ExtensionApiError(`view selection ${index} must be an object`);
243
+ }
244
+ const viewSelection = selection;
245
+ assertString(viewSelection.name, `view selection ${index} is missing name`);
246
+ assertNumber(viewSelection.start, `view selection ${index} is missing start`);
247
+ assertNumber(viewSelection.end, `view selection ${index} is missing end`);
248
+ return {
249
+ end: viewSelection.end,
250
+ name: viewSelection.name,
251
+ start: viewSelection.start,
252
+ };
253
+ };
254
+ const normalizeViewSelections = (selections) => {
255
+ if (!Array.isArray(selections)) {
256
+ throw new ExtensionApiError('view selections must be an array');
257
+ }
258
+ return selections.map(normalizeViewSelection);
259
+ };
240
260
  const renderPatches = async (uid, instance) => {
241
261
  const oldDom = renderedDoms[uid] || [];
242
262
  const newDom = await renderDom(instance);
@@ -336,9 +356,23 @@ const withTitle = async (result, instance) => {
336
356
  title,
337
357
  };
338
358
  };
359
+ const withSelections = async (result, instance) => {
360
+ if (typeof instance.renderSelections !== 'function') {
361
+ return result;
362
+ }
363
+ const selections = normalizeViewSelections(await instance.renderSelections());
364
+ if (selections.length === 0) {
365
+ return result;
366
+ }
367
+ return {
368
+ ...result,
369
+ selections,
370
+ };
371
+ };
339
372
  const withRenderMetadata = async (result, instance, contextChange) => {
340
373
  const resultWithFocus = await withFocusSelector(result, instance, contextChange);
341
- return withTitle(resultWithFocus, instance);
374
+ const resultWithSelections = await withSelections(resultWithFocus, instance);
375
+ return withTitle(resultWithSelections, instance);
342
376
  };
343
377
  const maybeClearContext = async (uid, viewId) => {
344
378
  if (!contexts[uid]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/api",
3
- "version": "8.43.0",
3
+ "version": "8.44.0",
4
4
  "description": "Tree-shakeable extension API for Lvce Editor extensions.",
5
5
  "keywords": [
6
6
  "lvce-editor",