@histoire/plugin-kit 0.0.0-stage → 1.0.0-beta.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/LICENSE +21 -0
- package/README.md +182 -2
- package/dist/builtins.d.ts +13 -0
- package/dist/builtins.d.ts.map +1 -0
- package/dist/builtins.js +7 -0
- package/dist/define.d.ts +19 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +43 -0
- package/dist/errors.d.ts +11 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +14 -0
- package/dist/frame.d.ts +6 -0
- package/dist/frame.d.ts.map +1 -0
- package/dist/frame.js +3 -0
- package/dist/host/actions.d.ts +37 -0
- package/dist/host/actions.d.ts.map +1 -0
- package/dist/host/actions.js +47 -0
- package/dist/host/create-host.d.ts +9 -0
- package/dist/host/create-host.d.ts.map +1 -0
- package/dist/host/create-host.js +194 -0
- package/dist/host/diagnostics.d.ts +14 -0
- package/dist/host/diagnostics.d.ts.map +1 -0
- package/dist/host/diagnostics.js +26 -0
- package/dist/host/frames.d.ts +43 -0
- package/dist/host/frames.d.ts.map +1 -0
- package/dist/host/frames.js +63 -0
- package/dist/host/generation.d.ts +24 -0
- package/dist/host/generation.d.ts.map +1 -0
- package/dist/host/generation.js +38 -0
- package/dist/host/install.d.ts +43 -0
- package/dist/host/install.d.ts.map +1 -0
- package/dist/host/install.js +168 -0
- package/dist/host/manifest.d.ts +23 -0
- package/dist/host/manifest.d.ts.map +1 -0
- package/dist/host/manifest.js +37 -0
- package/dist/host/mount.d.ts +15 -0
- package/dist/host/mount.d.ts.map +1 -0
- package/dist/host/mount.js +57 -0
- package/dist/host/ordering.d.ts +20 -0
- package/dist/host/ordering.d.ts.map +1 -0
- package/dist/host/ordering.js +12 -0
- package/dist/host/plugin-api.d.ts +33 -0
- package/dist/host/plugin-api.d.ts.map +1 -0
- package/dist/host/plugin-api.js +113 -0
- package/dist/host/preferences.d.ts +57 -0
- package/dist/host/preferences.d.ts.map +1 -0
- package/dist/host/preferences.js +137 -0
- package/dist/host/registry.d.ts +43 -0
- package/dist/host/registry.d.ts.map +1 -0
- package/dist/host/registry.js +123 -0
- package/dist/host/resources.d.ts +20 -0
- package/dist/host/resources.d.ts.map +1 -0
- package/dist/host/resources.js +45 -0
- package/dist/ids.d.ts +32 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +51 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/react/index.d.ts +19 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +25 -0
- package/dist/render/keys.d.ts +7 -0
- package/dist/render/keys.d.ts.map +1 -0
- package/dist/render/keys.js +15 -0
- package/dist/render/mount-manager.d.ts +36 -0
- package/dist/render/mount-manager.d.ts.map +1 -0
- package/dist/render/mount-manager.js +128 -0
- package/dist/render/view-binding.d.ts +19 -0
- package/dist/render/view-binding.d.ts.map +1 -0
- package/dist/render/view-binding.js +7 -0
- package/dist/render/workbench-components.d.ts +39 -0
- package/dist/render/workbench-components.d.ts.map +1 -0
- package/dist/render/workbench-components.js +76 -0
- package/dist/session/context.d.ts +66 -0
- package/dist/session/context.d.ts.map +1 -0
- package/dist/session/context.js +173 -0
- package/dist/session/frames.d.ts +46 -0
- package/dist/session/frames.d.ts.map +1 -0
- package/dist/session/frames.js +68 -0
- package/dist/shortcuts.d.ts +94 -0
- package/dist/shortcuts.d.ts.map +1 -0
- package/dist/shortcuts.js +157 -0
- package/dist/styles.d.ts +3 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +25 -0
- package/dist/svelte4/index.d.ts +27 -0
- package/dist/svelte4/index.d.ts.map +1 -0
- package/dist/svelte4/index.js +16 -0
- package/dist/svelte5/index.d.ts +21 -0
- package/dist/svelte5/index.d.ts.map +1 -0
- package/dist/svelte5/index.js +40 -0
- package/dist/types/actions.d.ts +12 -0
- package/dist/types/actions.d.ts.map +1 -0
- package/dist/types/actions.js +1 -0
- package/dist/types/base.d.ts +12 -0
- package/dist/types/base.d.ts.map +1 -0
- package/dist/types/base.js +1 -0
- package/dist/types/canvas.d.ts +150 -0
- package/dist/types/canvas.d.ts.map +1 -0
- package/dist/types/canvas.js +1 -0
- package/dist/types/channel.d.ts +28 -0
- package/dist/types/channel.d.ts.map +1 -0
- package/dist/types/channel.js +1 -0
- package/dist/types/commands.d.ts +35 -0
- package/dist/types/commands.d.ts.map +1 -0
- package/dist/types/commands.js +1 -0
- package/dist/types/common.d.ts +7 -0
- package/dist/types/common.d.ts.map +1 -0
- package/dist/types/common.js +1 -0
- package/dist/types/context.d.ts +72 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/context.js +1 -0
- package/dist/types/contribution.d.ts +56 -0
- package/dist/types/contribution.d.ts.map +1 -0
- package/dist/types/contribution.js +1 -0
- package/dist/types/frame.d.ts +41 -0
- package/dist/types/frame.d.ts.map +1 -0
- package/dist/types/frame.js +1 -0
- package/dist/types/host.d.ts +123 -0
- package/dist/types/host.d.ts.map +1 -0
- package/dist/types/host.js +1 -0
- package/dist/types/index.d.ts +19 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/inspector.d.ts +38 -0
- package/dist/types/inspector.d.ts.map +1 -0
- package/dist/types/inspector.js +1 -0
- package/dist/types/menus.d.ts +71 -0
- package/dist/types/menus.d.ts.map +1 -0
- package/dist/types/menus.js +1 -0
- package/dist/types/plugin.d.ts +133 -0
- package/dist/types/plugin.d.ts.map +1 -0
- package/dist/types/plugin.js +1 -0
- package/dist/types/preferences.d.ts +31 -0
- package/dist/types/preferences.d.ts.map +1 -0
- package/dist/types/preferences.js +1 -0
- package/dist/types/services.d.ts +175 -0
- package/dist/types/services.d.ts.map +1 -0
- package/dist/types/services.js +1 -0
- package/dist/types/shell.d.ts +47 -0
- package/dist/types/shell.d.ts.map +1 -0
- package/dist/types/shell.js +1 -0
- package/dist/types/toolbar.d.ts +59 -0
- package/dist/types/toolbar.d.ts.map +1 -0
- package/dist/types/toolbar.js +1 -0
- package/dist/types/view.d.ts +82 -0
- package/dist/types/view.d.ts.map +1 -0
- package/dist/types/view.js +1 -0
- package/dist/vue/components.d.ts +122 -0
- package/dist/vue/components.d.ts.map +1 -0
- package/dist/vue/components.js +112 -0
- package/dist/vue/index.d.ts +21 -0
- package/dist/vue/index.d.ts.map +1 -0
- package/dist/vue/index.js +47 -0
- package/dist/vue/mount.d.ts +9 -0
- package/dist/vue/mount.d.ts.map +1 -0
- package/dist/vue/mount.js +32 -0
- package/dist/vue/panel-action.d.ts +60 -0
- package/dist/vue/panel-action.d.ts.map +1 -0
- package/dist/vue/panel-action.js +28 -0
- package/package.json +92 -3
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { PLUGIN_FRAMES_SERVICE } from '../host/frames.js';
|
|
2
|
+
import { createScope } from '../host/generation.js';
|
|
3
|
+
/**
|
|
4
|
+
* Navigation for hosts without workbench routes: stories are selected through the session;
|
|
5
|
+
* tabs, panes, settings and canvas views do not exist and are ignored with a warning, so a
|
|
6
|
+
* plugin never changes a host-owned URL.
|
|
7
|
+
* @param session Session selecting stories.
|
|
8
|
+
* @param warn Receives ignored destinations; `console.warn` by default.
|
|
9
|
+
*/
|
|
10
|
+
export function createSessionNavigation(session, warn = message => console.warn(message)) {
|
|
11
|
+
const ignore = (kind) => (id) => warn(`[histoire plugin] ${kind} "${id ?? ''}" is not available in this host`);
|
|
12
|
+
return Object.freeze({
|
|
13
|
+
selectStory: async (input) => {
|
|
14
|
+
if (!session.selection)
|
|
15
|
+
throw Object.assign(new Error('[histoire plugin] story selection is not available in this host'), { code: 'CAPABILITY_UNAVAILABLE' });
|
|
16
|
+
await session.selection.select(input);
|
|
17
|
+
},
|
|
18
|
+
openInspectorTab: ignore('inspector tab'),
|
|
19
|
+
openPane: ignore('pane'),
|
|
20
|
+
openSettings: ignore('settings section'),
|
|
21
|
+
openCanvasView: ignore('canvas view'),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Key of the source generation. Revisions (content publications) keep the generation:
|
|
26
|
+
* a story edit must not abort unrelated pending actions.
|
|
27
|
+
* @param session Session.
|
|
28
|
+
*/
|
|
29
|
+
function sourceKey(session) {
|
|
30
|
+
const source = session.getSnapshot().source;
|
|
31
|
+
return source ? JSON.stringify([source.url, source.sourceId, source.epoch]) : '';
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Key of the selected target.
|
|
35
|
+
* @param session Session.
|
|
36
|
+
*/
|
|
37
|
+
function selectionKey(session) {
|
|
38
|
+
const target = session.getSnapshot().selection;
|
|
39
|
+
return target ? JSON.stringify([target.storyId, target.variantId]) : '';
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Snapshot of the selection exposed on the context; same object until the target changes.
|
|
43
|
+
* @param session Session.
|
|
44
|
+
*/
|
|
45
|
+
function freezeSelection(session) {
|
|
46
|
+
const target = session.getSnapshot().selection;
|
|
47
|
+
return Object.freeze({ target: target ? Object.freeze({ storyId: target.storyId, variantId: target.variantId }) : null });
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Runs every listener, isolating failures: one throwing plugin listener must not skip the
|
|
51
|
+
* others. Failures are logged since the session context has no plugin attribution.
|
|
52
|
+
* @param listeners Listeners to run; copied so (un)subscribing during a run is safe.
|
|
53
|
+
* @param kind Listener kind, for the log.
|
|
54
|
+
*/
|
|
55
|
+
function notify(listeners, kind) {
|
|
56
|
+
for (const listener of [...listeners]) {
|
|
57
|
+
try {
|
|
58
|
+
listener();
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
console.error(`[histoire plugin] ${kind} listener failed`, error);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Creates a root-scoped context from public session methods (standalone workbench, native Vue,
|
|
67
|
+
* React and plain DOM hosts share it). Selection and source changes rotate generation scopes;
|
|
68
|
+
* actions capture the current scope when they start, so a later change aborts them.
|
|
69
|
+
* @param options Session, mode and services.
|
|
70
|
+
*/
|
|
71
|
+
export function createSessionContext(options) {
|
|
72
|
+
const { session } = options;
|
|
73
|
+
const lifetime = createScope();
|
|
74
|
+
const listeners = new Set();
|
|
75
|
+
const catalogListeners = new Set();
|
|
76
|
+
let catalog = session.getSnapshot().catalog;
|
|
77
|
+
let source = { key: sourceKey(session), scope: lifetime.child() };
|
|
78
|
+
let selection = { key: selectionKey(session), scope: source.scope.child(), value: freezeSelection(session) };
|
|
79
|
+
/**
|
|
80
|
+
* Rotates scopes whose key changed. Called on every publication and before handing out a
|
|
81
|
+
* signal, so a caller reading synchronously after a change never captures a stale scope.
|
|
82
|
+
* @returns Whether a context value changed.
|
|
83
|
+
*/
|
|
84
|
+
function synchronize() {
|
|
85
|
+
if (!lifetime.active)
|
|
86
|
+
return false;
|
|
87
|
+
const nextSource = sourceKey(session);
|
|
88
|
+
const nextSelection = selectionKey(session);
|
|
89
|
+
if (nextSource === source.key && nextSelection === selection.key)
|
|
90
|
+
return false;
|
|
91
|
+
if (nextSource !== source.key) {
|
|
92
|
+
// Retiring the source scope also retires its selection child.
|
|
93
|
+
source.scope.retire();
|
|
94
|
+
source = { key: nextSource, scope: lifetime.child() };
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
selection.scope.retire();
|
|
98
|
+
}
|
|
99
|
+
selection = { key: nextSelection, scope: source.scope.child(), value: freezeSelection(session) };
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
const stop = session.subscribe(() => {
|
|
103
|
+
const nextCatalog = session.getSnapshot().catalog;
|
|
104
|
+
const catalogChanged = lifetime.active && nextCatalog !== catalog;
|
|
105
|
+
catalog = nextCatalog;
|
|
106
|
+
// Scopes rotate before any listener runs, so listeners never capture a stale scope.
|
|
107
|
+
const changed = synchronize();
|
|
108
|
+
if (catalogChanged)
|
|
109
|
+
notify(catalogListeners, 'catalog');
|
|
110
|
+
if (changed)
|
|
111
|
+
notify(listeners, 'context');
|
|
112
|
+
});
|
|
113
|
+
const capabilities = new Set(['catalog', 'navigation', 'preferences']);
|
|
114
|
+
if (options.devEvents)
|
|
115
|
+
capabilities.add('devEvents');
|
|
116
|
+
if (options.frames)
|
|
117
|
+
capabilities.add('frames');
|
|
118
|
+
if (options.tests)
|
|
119
|
+
capabilities.add('tests');
|
|
120
|
+
const context = {
|
|
121
|
+
capabilities,
|
|
122
|
+
source: Object.freeze({ mode: options.mode ?? 'remote' }),
|
|
123
|
+
// Bound methods only: plugins never receive the session object.
|
|
124
|
+
catalog: Object.freeze({
|
|
125
|
+
list: () => session.catalog.list(),
|
|
126
|
+
getStory: (storyId) => session.catalog.getStory(storyId),
|
|
127
|
+
search: (query) => session.catalog.search(query),
|
|
128
|
+
subscribe: (listener) => {
|
|
129
|
+
if (!lifetime.active)
|
|
130
|
+
return () => { };
|
|
131
|
+
const entry = () => listener();
|
|
132
|
+
catalogListeners.add(entry);
|
|
133
|
+
return () => void catalogListeners.delete(entry);
|
|
134
|
+
},
|
|
135
|
+
}),
|
|
136
|
+
get selection() {
|
|
137
|
+
synchronize();
|
|
138
|
+
return selection.value;
|
|
139
|
+
},
|
|
140
|
+
navigation: options.navigation ?? createSessionNavigation(session),
|
|
141
|
+
...(options.devEvents ? { devEvents: options.devEvents } : {}),
|
|
142
|
+
...(options.tests ? { tests: options.tests } : {}),
|
|
143
|
+
subscribe(listener) {
|
|
144
|
+
if (!lifetime.active)
|
|
145
|
+
return () => { };
|
|
146
|
+
const entry = () => listener();
|
|
147
|
+
listeners.add(entry);
|
|
148
|
+
return () => void listeners.delete(entry);
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
// plugin-kit reads the frame service from the root context under its shared symbol.
|
|
152
|
+
if (options.frames)
|
|
153
|
+
Object.defineProperty(context, PLUGIN_FRAMES_SERVICE, { value: options.frames });
|
|
154
|
+
return {
|
|
155
|
+
context: Object.freeze(context),
|
|
156
|
+
selectionSignal() {
|
|
157
|
+
synchronize();
|
|
158
|
+
return selection.scope.signal;
|
|
159
|
+
},
|
|
160
|
+
sourceSignal() {
|
|
161
|
+
synchronize();
|
|
162
|
+
return source.scope.signal;
|
|
163
|
+
},
|
|
164
|
+
close() {
|
|
165
|
+
if (!lifetime.active)
|
|
166
|
+
return;
|
|
167
|
+
lifetime.retire();
|
|
168
|
+
stop();
|
|
169
|
+
listeners.clear();
|
|
170
|
+
catalogListeners.clear();
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { HistoireFramePluginInfo, HistoireTarget } from '@histoire/protocol';
|
|
2
|
+
import type { PluginFrameService } from '../host/frames.js';
|
|
3
|
+
import type { PluginChannel } from '../types/channel.js';
|
|
4
|
+
import type { Disposer } from '../types/common.js';
|
|
5
|
+
/** Frame plugin actor as `session.plugins.actors()` reports it. */
|
|
6
|
+
export interface SessionFrameActor {
|
|
7
|
+
/** Attached plugin. */
|
|
8
|
+
readonly pluginId: string;
|
|
9
|
+
/** Rendered mount identity; channels bind to it. */
|
|
10
|
+
readonly actorId: string;
|
|
11
|
+
/** Variant rendered by the actor. */
|
|
12
|
+
readonly target: HistoireTarget;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Frame plugin messaging of a remote session (`HistoireSession.plugins` from `@histoire/sdk`
|
|
16
|
+
* satisfies it structurally). Data only: the host never evaluates book plugin modules.
|
|
17
|
+
*/
|
|
18
|
+
export interface SessionFramePlugins {
|
|
19
|
+
/** Frame plugins the connected book advertises. */
|
|
20
|
+
frames: () => readonly HistoireFramePluginInfo[];
|
|
21
|
+
/** Attached actors of one plugin; throws when not connected or not installed. */
|
|
22
|
+
actors: (pluginId: string) => readonly SessionFrameActor[];
|
|
23
|
+
/** Observes actor attach and detach. */
|
|
24
|
+
subscribe: (listener: () => void) => Disposer;
|
|
25
|
+
/** Channel bound to exactly one actor. */
|
|
26
|
+
open: (pluginId: string, frame: {
|
|
27
|
+
readonly actorId: string;
|
|
28
|
+
}) => PluginChannel;
|
|
29
|
+
}
|
|
30
|
+
/** Options of {@link createSessionFrameService}. */
|
|
31
|
+
export interface SessionFrameServiceOptions {
|
|
32
|
+
/**
|
|
33
|
+
* Frame key of an actor's variant. Defaults to the target key, which is how native hosts
|
|
34
|
+
* key their single primary frame (`FrameRef.key` of their frame-scoped contexts).
|
|
35
|
+
*/
|
|
36
|
+
frameKey?: (target: HistoireTarget) => string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Frame service of a native host backed by a remote session's `plugins` API, for
|
|
40
|
+
* `createSessionContext({ frames })`. Lists and opens only actors of frames the session itself
|
|
41
|
+
* mounted; a frame failure inside the book is not observable remotely, so `onError` never fires.
|
|
42
|
+
* @param plugins `session.plugins`.
|
|
43
|
+
* @param options Frame key mapping.
|
|
44
|
+
*/
|
|
45
|
+
export declare function createSessionFrameService(plugins: SessionFramePlugins, options?: SessionFrameServiceOptions): PluginFrameService;
|
|
46
|
+
//# sourceMappingURL=frames.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frames.d.ts","sourceRoot":"","sources":["../../src/session/frames.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACjF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC3D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACxD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAIlD,mEAAmE;AACnE,MAAM,WAAW,iBAAiB;IAChC,uBAAuB;IACvB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,oDAAoD;IACpD,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,qCAAqC;IACrC,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAA;CAChC;AAED;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC,mDAAmD;IACnD,MAAM,EAAE,MAAM,SAAS,uBAAuB,EAAE,CAAA;IAChD,iFAAiF;IACjF,MAAM,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,SAAS,iBAAiB,EAAE,CAAA;IAC1D,wCAAwC;IACxC,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,QAAQ,CAAA;IAC7C,0CAA0C;IAC1C,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;KAAE,KAAK,aAAa,CAAA;CAC/E;AAED,oDAAoD;AACpD,MAAM,WAAW,0BAA0B;IACzC;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,MAAM,CAAA;CAC9C;AAkCD;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,mBAAmB,EAAE,OAAO,GAAE,0BAA+B,GAAG,kBAAkB,CA0BpI"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { getHistoireTargetKey } from '@histoire/protocol';
|
|
2
|
+
/** Coded failure matching SDK error codes without a runtime SDK dependency. */
|
|
3
|
+
function failure(code, message) {
|
|
4
|
+
return Object.assign(new Error(`[histoire plugin] ${message}`), { code });
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Wraps a session channel so the plugin's listeners are released when `signal` aborts, and
|
|
8
|
+
* later posts reject instead of reaching a frame the plugin no longer owns.
|
|
9
|
+
* @param channel Session channel bound to one actor.
|
|
10
|
+
* @param signal Plugin lifetime.
|
|
11
|
+
*/
|
|
12
|
+
function ownedChannel(channel, signal) {
|
|
13
|
+
const listeners = new Set();
|
|
14
|
+
signal.addEventListener('abort', () => {
|
|
15
|
+
for (const off of [...listeners])
|
|
16
|
+
off();
|
|
17
|
+
listeners.clear();
|
|
18
|
+
}, { once: true });
|
|
19
|
+
return Object.freeze({
|
|
20
|
+
post: (type, data) => signal.aborted ? Promise.reject(failure('RUNTIME_CHANGED', 'plugin was disposed')) : channel.post(type, data),
|
|
21
|
+
on(type, listener) {
|
|
22
|
+
if (signal.aborted)
|
|
23
|
+
return () => { };
|
|
24
|
+
const off = channel.on(type, listener);
|
|
25
|
+
const release = () => {
|
|
26
|
+
if (listeners.delete(release))
|
|
27
|
+
off();
|
|
28
|
+
};
|
|
29
|
+
listeners.add(release);
|
|
30
|
+
return release;
|
|
31
|
+
},
|
|
32
|
+
getDroppedCount: () => channel.getDroppedCount(),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Frame service of a native host backed by a remote session's `plugins` API, for
|
|
37
|
+
* `createSessionContext({ frames })`. Lists and opens only actors of frames the session itself
|
|
38
|
+
* mounted; a frame failure inside the book is not observable remotely, so `onError` never fires.
|
|
39
|
+
* @param plugins `session.plugins`.
|
|
40
|
+
* @param options Frame key mapping.
|
|
41
|
+
*/
|
|
42
|
+
export function createSessionFrameService(plugins, options = {}) {
|
|
43
|
+
const frameKey = options.frameKey ?? getHistoireTargetKey;
|
|
44
|
+
/** Attached actors, empty while disconnected or when the book lacks the plugin. */
|
|
45
|
+
function actors(pluginId) {
|
|
46
|
+
try {
|
|
47
|
+
return plugins.actors(pluginId);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return Object.freeze({
|
|
54
|
+
list: (pluginId) => actors(pluginId).map(actor => Object.freeze({ key: frameKey(actor.target), target: actor.target, actorId: actor.actorId })),
|
|
55
|
+
open(pluginId, frame, signal) {
|
|
56
|
+
if (signal.aborted)
|
|
57
|
+
throw failure('RUNTIME_CHANGED', `plugin "${pluginId}" was disposed`);
|
|
58
|
+
const explicit = frame.actorId;
|
|
59
|
+
// An explicit actor binds exactly; a plain frame binds to its current (latest) actor.
|
|
60
|
+
const actorId = explicit ?? actors(pluginId).filter(actor => frameKey(actor.target) === frame.key).at(-1)?.actorId;
|
|
61
|
+
if (!actorId)
|
|
62
|
+
throw failure('PREVIEW_NOT_READY', 'frame plugin is not attached to this frame');
|
|
63
|
+
return ownedChannel(plugins.open(pluginId, { actorId }), signal);
|
|
64
|
+
},
|
|
65
|
+
onError: () => () => { },
|
|
66
|
+
subscribe: listener => plugins.subscribe(listener),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { ShortcutScope } from './types/commands.js';
|
|
2
|
+
import type { Disposer } from './types/common.js';
|
|
3
|
+
import type { QualifiedContribution } from './types/contribution.js';
|
|
4
|
+
import type { ExtensionHost } from './types/host.js';
|
|
5
|
+
/** Built-in key binding; plugin shortcuts overlapping one are disabled in every host. */
|
|
6
|
+
export interface BuiltinShortcut {
|
|
7
|
+
/** Stable action identity. */
|
|
8
|
+
readonly id: string;
|
|
9
|
+
/** Human-readable action name. */
|
|
10
|
+
readonly label: string;
|
|
11
|
+
/** Alternatives, using `mod` for platform command/control. */
|
|
12
|
+
readonly keys: readonly string[];
|
|
13
|
+
/** Focus scope in which the action runs. */
|
|
14
|
+
readonly scope: ShortcutScope;
|
|
15
|
+
/** Hidden in static books. */
|
|
16
|
+
readonly devOnly?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Built-in bindings shared by the standalone workbench and native hosts (native hosts bind the
|
|
20
|
+
* global search only). Built-ins take precedence whether or not their surface is mounted.
|
|
21
|
+
*/
|
|
22
|
+
export declare const BUILTIN_SHORTCUTS: readonly BuiltinShortcut[];
|
|
23
|
+
/** Keys and scope of a binding. */
|
|
24
|
+
export type ShortcutBinding = Pick<BuiltinShortcut, 'keys' | 'scope'>;
|
|
25
|
+
/** Binding with its identity, as conflicts name the binding that wins. */
|
|
26
|
+
export type IdentifiedShortcutBinding = ShortcutBinding & Pick<BuiltinShortcut, 'id'>;
|
|
27
|
+
/** Registered plugin shortcut. */
|
|
28
|
+
type PluginShortcut = QualifiedContribution<'shortcut'>;
|
|
29
|
+
/** Keyboard event fields shortcut matching reads. */
|
|
30
|
+
export type ShortcutKeyEvent = Pick<KeyboardEvent, 'key' | 'code' | 'altKey' | 'shiftKey' | 'metaKey' | 'ctrlKey'>;
|
|
31
|
+
/**
|
|
32
|
+
* Whether two bindings share a scope and at least one concrete key combination.
|
|
33
|
+
* @param a First binding.
|
|
34
|
+
* @param b Second binding.
|
|
35
|
+
*/
|
|
36
|
+
export declare function shortcutBindingsOverlap(a: ShortcutBinding, b: ShortcutBinding): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Built-in shortcut overlapping `binding`, bound or not.
|
|
39
|
+
* @param binding Candidate keys and scope.
|
|
40
|
+
*/
|
|
41
|
+
export declare function findBuiltinShortcutConflict(binding: ShortcutBinding): BuiltinShortcut | undefined;
|
|
42
|
+
/** Inputs of {@link findPluginShortcutConflict}. */
|
|
43
|
+
export interface PluginShortcutConflictOptions {
|
|
44
|
+
/** Bindings already registered in the host; the earliest binding wins. */
|
|
45
|
+
bound?: Iterable<IdentifiedShortcutBinding>;
|
|
46
|
+
/** Focus scopes the host has; others cannot fire. All scopes by default. */
|
|
47
|
+
scopes?: readonly ShortcutScope[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Why a plugin shortcut cannot bind, checked once at registration in every host: its scope has
|
|
51
|
+
* no surface in the host, it overlaps a built-in (built-ins take precedence whether or not their
|
|
52
|
+
* surface is mounted), or it overlaps an already bound shortcut of the same scope. The returned
|
|
53
|
+
* error is recorded as a `report` diagnostic; the shortcut's command stays available.
|
|
54
|
+
* @param shortcut Candidate shortcut.
|
|
55
|
+
* @param options Bound shortcuts and host scopes.
|
|
56
|
+
* @returns The diagnostic error, or `undefined` when the shortcut can bind.
|
|
57
|
+
*/
|
|
58
|
+
export declare function findPluginShortcutConflict(shortcut: IdentifiedShortcutBinding, options?: PluginShortcutConflictOptions): Error | undefined;
|
|
59
|
+
/** Plugin shortcuts of one host that passed registration checks. */
|
|
60
|
+
export interface PluginShortcutBindings {
|
|
61
|
+
/**
|
|
62
|
+
* First bound shortcut matching `event`, trying `scopes` in order (focused areas first).
|
|
63
|
+
* @param event Keyboard event.
|
|
64
|
+
* @param scopes Focus scopes containing the event target, innermost first.
|
|
65
|
+
* @param accept Extra filter, e.g. availability in the current context.
|
|
66
|
+
*/
|
|
67
|
+
match: (event: ShortcutKeyEvent, scopes: readonly ShortcutScope[], accept?: (shortcut: PluginShortcut) => boolean) => PluginShortcut | undefined;
|
|
68
|
+
/** Stops observing the host; never disposes it. */
|
|
69
|
+
dispose: Disposer;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Tracks `shortcut` contributions of a host without a shortcut registry (native Vue, React or
|
|
73
|
+
* DOM hosts). Each contribution is checked once, when it is committed, with
|
|
74
|
+
* {@link findPluginShortcutConflict}; a rejected one gets one `report` diagnostic and never
|
|
75
|
+
* binds. A contribution leaving the registry frees its keys for later registrations.
|
|
76
|
+
* @param host Extension host whose registry is observed.
|
|
77
|
+
* @param scopes Focus scopes the host routes key events to; `global` only by default.
|
|
78
|
+
*/
|
|
79
|
+
export declare function createPluginShortcutBindings(host: ExtensionHost, scopes?: readonly ShortcutScope[]): PluginShortcutBindings;
|
|
80
|
+
/**
|
|
81
|
+
* Whether `event` matches one binding exactly (no extra modifiers; `mod` is command or control,
|
|
82
|
+
* digits match their physical key).
|
|
83
|
+
* @param binding Binding such as `mod+shift+k`.
|
|
84
|
+
* @param event Keyboard event.
|
|
85
|
+
*/
|
|
86
|
+
export declare function matchesShortcut(binding: string, event: ShortcutKeyEvent): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Whether a key event target edits text (inputs, selects, content-editable, textbox roles);
|
|
89
|
+
* shortcuts skip those unless they explicitly allow input. No global DOM realm required.
|
|
90
|
+
* @param target Event target.
|
|
91
|
+
*/
|
|
92
|
+
export declare function isEditableTarget(target: EventTarget | null): boolean;
|
|
93
|
+
export {};
|
|
94
|
+
//# sourceMappingURL=shortcuts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shortcuts.d.ts","sourceRoot":"","sources":["../src/shortcuts.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACxD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AACpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAEpD,yFAAyF;AACzF,MAAM,WAAW,eAAe;IAC9B,8BAA8B;IAC9B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,kCAAkC;IAClC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,8DAA8D;IAC9D,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAA;IAChC,4CAA4C;IAC5C,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAA;IAC7B,8BAA8B;IAC9B,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAC3B;AAED;;;GAGG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,eAAe,EAqB1B,CAAA;AAE9B,mCAAmC;AACnC,MAAM,MAAM,eAAe,GAAG,IAAI,CAAC,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,CAAA;AAErE,0EAA0E;AAC1E,MAAM,MAAM,yBAAyB,GAAG,eAAe,GAAG,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,CAAA;AAErF,kCAAkC;AAClC,KAAK,cAAc,GAAG,qBAAqB,CAAC,UAAU,CAAC,CAAA;AAEvD,qDAAqD;AACrD,MAAM,MAAM,gBAAgB,GAAG,IAAI,CAAC,aAAa,EAAE,KAAK,GAAG,MAAM,GAAG,QAAQ,GAAG,UAAU,GAAG,SAAS,GAAG,SAAS,CAAC,CAAA;AAelH;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,EAAE,eAAe,GAAG,OAAO,CAEvF;AAED;;;GAGG;AACH,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,eAAe,GAAG,eAAe,GAAG,SAAS,CAEjG;AAED,oDAAoD;AACpD,MAAM,WAAW,6BAA6B;IAC5C,0EAA0E;IAC1E,KAAK,CAAC,EAAE,QAAQ,CAAC,yBAAyB,CAAC,CAAA;IAC3C,4EAA4E;IAC5E,MAAM,CAAC,EAAE,SAAS,aAAa,EAAE,CAAA;CAClC;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,yBAAyB,EAAE,OAAO,GAAE,6BAAkC,GAAG,KAAK,GAAG,SAAS,CAQ9I;AAED,oEAAoE;AACpE,MAAM,WAAW,sBAAsB;IACrC;;;;;OAKG;IACH,KAAK,EAAE,CAAC,KAAK,EAAE,gBAAgB,EAAE,MAAM,EAAE,SAAS,aAAa,EAAE,EAAE,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,cAAc,KAAK,OAAO,KAAK,cAAc,GAAG,SAAS,CAAA;IAChJ,mDAAmD;IACnD,OAAO,EAAE,QAAQ,CAAA;CAClB;AAKD;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,aAAa,EAAE,MAAM,GAAE,SAAS,aAAa,EAAe,GAAG,sBAAsB,CA6CvI;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAQjF;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI,GAAG,OAAO,CAGpE"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in bindings shared by the standalone workbench and native hosts (native hosts bind the
|
|
3
|
+
* global search only). Built-ins take precedence whether or not their surface is mounted.
|
|
4
|
+
*/
|
|
5
|
+
export const BUILTIN_SHORTCUTS = Object.freeze([
|
|
6
|
+
{ id: 'global.search', label: 'Search', keys: ['mod+k'], scope: 'global' },
|
|
7
|
+
{ id: 'global.toggle-ui', label: 'Toggle UI', keys: ['mod+shift+h'], scope: 'global' },
|
|
8
|
+
{ id: 'global.show-ui', label: 'Show UI', keys: ['escape'], scope: 'global' },
|
|
9
|
+
{ id: 'global.theme', label: 'Toggle dark mode', keys: ['mod+shift+d'], scope: 'global' },
|
|
10
|
+
{ id: 'canvas.select', label: 'Select tool', keys: ['v'], scope: 'canvas' },
|
|
11
|
+
{ id: 'canvas.pan', label: 'Pan tool', keys: ['h'], scope: 'canvas' },
|
|
12
|
+
{ id: 'canvas.measure', label: 'Measure tool', keys: ['m'], scope: 'canvas' },
|
|
13
|
+
{ id: 'canvas.comment', label: 'Comment tool', keys: ['c'], scope: 'canvas', devOnly: true },
|
|
14
|
+
{ id: 'canvas.pan-hold', label: 'Hold to pan', keys: ['space'], scope: 'canvas' },
|
|
15
|
+
{ id: 'canvas.fit', label: 'Fit to canvas', keys: ['shift+1'], scope: 'canvas' },
|
|
16
|
+
{ id: 'canvas.selection', label: 'Zoom to selection', keys: ['shift+2'], scope: 'canvas' },
|
|
17
|
+
{ id: 'canvas.actual', label: 'Actual size', keys: ['shift+0'], scope: 'canvas' },
|
|
18
|
+
{ id: 'frame.isolated', label: 'Open isolated', keys: ['mod+enter'], scope: 'frame' },
|
|
19
|
+
{ id: 'frame.editor', label: 'Open in editor', keys: ['alt+e'], scope: 'frame', devOnly: true },
|
|
20
|
+
{ id: 'frame.source', label: 'Copy source', keys: ['mod+shift+c'], scope: 'frame' },
|
|
21
|
+
{ id: 'frame.tests', label: 'Run tests', keys: ['t'], scope: 'frame', devOnly: true },
|
|
22
|
+
{ id: 'frame.screenshot', label: 'Screenshot', keys: ['shift+s'], scope: 'frame', devOnly: true },
|
|
23
|
+
{ id: 'frame.comment', label: 'Comment for AI', keys: ['shift+c'], scope: 'frame', devOnly: true },
|
|
24
|
+
{ id: 'comment.screenshot', label: 'Capture screenshot for AI message', keys: ['mod+shift+s'], scope: 'comment', devOnly: true },
|
|
25
|
+
{ id: 'comment.attach', label: 'Attach files to AI message', keys: ['mod+u'], scope: 'comment', devOnly: true },
|
|
26
|
+
]);
|
|
27
|
+
/** Normalizes equivalent modifier spellings for duplicate detection. */
|
|
28
|
+
function normalize(binding) {
|
|
29
|
+
const parts = binding.toLowerCase().split('+').map(value => value.trim());
|
|
30
|
+
const key = parts.pop();
|
|
31
|
+
return [...parts.sort(), key].join('+');
|
|
32
|
+
}
|
|
33
|
+
/** Platform aliases overlap explicit Control/Command bindings on supported hosts. */
|
|
34
|
+
function concreteBindings(binding) {
|
|
35
|
+
const value = normalize(binding);
|
|
36
|
+
return value.split('+').includes('mod') ? ['ctrl', 'meta'].map(modifier => normalize(value.replace(/\bmod\b/, modifier))) : [value];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether two bindings share a scope and at least one concrete key combination.
|
|
40
|
+
* @param a First binding.
|
|
41
|
+
* @param b Second binding.
|
|
42
|
+
*/
|
|
43
|
+
export function shortcutBindingsOverlap(a, b) {
|
|
44
|
+
return a.scope === b.scope && a.keys.some(key => b.keys.some(value => concreteBindings(value).some(binding => concreteBindings(key).includes(binding))));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Built-in shortcut overlapping `binding`, bound or not.
|
|
48
|
+
* @param binding Candidate keys and scope.
|
|
49
|
+
*/
|
|
50
|
+
export function findBuiltinShortcutConflict(binding) {
|
|
51
|
+
return BUILTIN_SHORTCUTS.find(item => shortcutBindingsOverlap(item, binding));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Why a plugin shortcut cannot bind, checked once at registration in every host: its scope has
|
|
55
|
+
* no surface in the host, it overlaps a built-in (built-ins take precedence whether or not their
|
|
56
|
+
* surface is mounted), or it overlaps an already bound shortcut of the same scope. The returned
|
|
57
|
+
* error is recorded as a `report` diagnostic; the shortcut's command stays available.
|
|
58
|
+
* @param shortcut Candidate shortcut.
|
|
59
|
+
* @param options Bound shortcuts and host scopes.
|
|
60
|
+
* @returns The diagnostic error, or `undefined` when the shortcut can bind.
|
|
61
|
+
*/
|
|
62
|
+
export function findPluginShortcutConflict(shortcut, options = {}) {
|
|
63
|
+
const prefix = `[histoire] shortcut "${shortcut.id}" (${shortcut.keys.join(', ')})`;
|
|
64
|
+
if (options.scopes && !options.scopes.includes(shortcut.scope))
|
|
65
|
+
return new Error(`${prefix} uses scope "${shortcut.scope}", which this host does not have, and is disabled`);
|
|
66
|
+
const builtin = findBuiltinShortcutConflict(shortcut);
|
|
67
|
+
if (builtin)
|
|
68
|
+
return new Error(`${prefix} conflicts with built-in "${builtin.id}" and is disabled`);
|
|
69
|
+
for (const existing of options.bound ?? []) {
|
|
70
|
+
if (shortcutBindingsOverlap(existing, shortcut))
|
|
71
|
+
return new Error(`${prefix} conflicts with "${existing.id}" and is disabled`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Shortcut contributions already rejected (and reported) per host. */
|
|
75
|
+
const rejectedByHost = new WeakMap();
|
|
76
|
+
/**
|
|
77
|
+
* Tracks `shortcut` contributions of a host without a shortcut registry (native Vue, React or
|
|
78
|
+
* DOM hosts). Each contribution is checked once, when it is committed, with
|
|
79
|
+
* {@link findPluginShortcutConflict}; a rejected one gets one `report` diagnostic and never
|
|
80
|
+
* binds. A contribution leaving the registry frees its keys for later registrations.
|
|
81
|
+
* @param host Extension host whose registry is observed.
|
|
82
|
+
* @param scopes Focus scopes the host routes key events to; `global` only by default.
|
|
83
|
+
*/
|
|
84
|
+
export function createPluginShortcutBindings(host, scopes = ['global']) {
|
|
85
|
+
// Bound contributions in registration order. Rejections are remembered per host, so a
|
|
86
|
+
// contribution reports once even when several bindings (providers) observe the same host.
|
|
87
|
+
const bound = new Set();
|
|
88
|
+
const rejected = rejectedByHost.get(host) ?? new WeakSet();
|
|
89
|
+
rejectedByHost.set(host, rejected);
|
|
90
|
+
let last;
|
|
91
|
+
/** Applies a registry snapshot; the same frozen snapshot means no shortcut changed. */
|
|
92
|
+
function sync() {
|
|
93
|
+
const snapshot = host.contributions('shortcut');
|
|
94
|
+
if (snapshot === last)
|
|
95
|
+
return;
|
|
96
|
+
// Set before reporting: diagnostics notify subscribers, re-entering with the same snapshot.
|
|
97
|
+
last = snapshot;
|
|
98
|
+
const current = new Set(snapshot);
|
|
99
|
+
for (const shortcut of bound) {
|
|
100
|
+
if (!current.has(shortcut))
|
|
101
|
+
bound.delete(shortcut);
|
|
102
|
+
}
|
|
103
|
+
for (const shortcut of snapshot) {
|
|
104
|
+
if (bound.has(shortcut) || rejected.has(shortcut))
|
|
105
|
+
continue;
|
|
106
|
+
const conflict = findPluginShortcutConflict(shortcut, { bound, scopes });
|
|
107
|
+
if (!conflict) {
|
|
108
|
+
bound.add(shortcut);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
rejected.add(shortcut);
|
|
112
|
+
host.reportViewError(shortcut.id, conflict, 'report');
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
sync();
|
|
116
|
+
const stop = host.subscribe(sync);
|
|
117
|
+
return {
|
|
118
|
+
match(event, eventScopes, accept) {
|
|
119
|
+
for (const scope of eventScopes) {
|
|
120
|
+
for (const shortcut of bound) {
|
|
121
|
+
if (shortcut.scope === scope && shortcut.keys.some(keys => matchesShortcut(keys, event)) && accept?.(shortcut) !== false)
|
|
122
|
+
return shortcut;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
dispose() {
|
|
127
|
+
stop();
|
|
128
|
+
bound.clear();
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Whether `event` matches one binding exactly (no extra modifiers; `mod` is command or control,
|
|
134
|
+
* digits match their physical key).
|
|
135
|
+
* @param binding Binding such as `mod+shift+k`.
|
|
136
|
+
* @param event Keyboard event.
|
|
137
|
+
*/
|
|
138
|
+
export function matchesShortcut(binding, event) {
|
|
139
|
+
const parts = normalize(binding).split('+');
|
|
140
|
+
const key = parts.pop();
|
|
141
|
+
const mod = parts.includes('mod');
|
|
142
|
+
if (event.altKey !== parts.includes('alt') || event.shiftKey !== parts.includes('shift'))
|
|
143
|
+
return false;
|
|
144
|
+
if (mod ? !(event.metaKey || event.ctrlKey) : event.metaKey !== parts.includes('meta') || event.ctrlKey !== parts.includes('ctrl'))
|
|
145
|
+
return false;
|
|
146
|
+
const physicalDigit = /^\d$/.test(key) && event.code === `Digit${key}`;
|
|
147
|
+
return physicalDigit || (key === 'space' ? event.code === 'Space' || event.key === ' ' : event.key.toLowerCase() === key);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Whether a key event target edits text (inputs, selects, content-editable, textbox roles);
|
|
151
|
+
* shortcuts skip those unless they explicitly allow input. No global DOM realm required.
|
|
152
|
+
* @param target Event target.
|
|
153
|
+
*/
|
|
154
|
+
export function isEditableTarget(target) {
|
|
155
|
+
const element = target;
|
|
156
|
+
return Boolean(element?.closest?.('input, textarea, select, [contenteditable="true"], [role="textbox"]'));
|
|
157
|
+
}
|
package/dist/styles.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../src/styles.ts"],"names":[],"mappings":"AAGA,+EAA+E;AAC/E,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAmB1G"}
|
package/dist/styles.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Style ownership per document and plugin, shared by every extension host. */
|
|
2
|
+
const styles = new WeakMap();
|
|
3
|
+
/** Install compiled, plugin-scoped stylesheet until final host releases it. */
|
|
4
|
+
export function installPluginStyles(document, id, css, signal) {
|
|
5
|
+
if (signal.aborted)
|
|
6
|
+
return;
|
|
7
|
+
let installed = styles.get(document);
|
|
8
|
+
if (!installed)
|
|
9
|
+
styles.set(document, installed = new Map());
|
|
10
|
+
let entry = installed.get(id);
|
|
11
|
+
if (!entry) {
|
|
12
|
+
const element = document.createElement('style');
|
|
13
|
+
element.dataset.histoirePlugin = id;
|
|
14
|
+
element.textContent = css;
|
|
15
|
+
document.head.append(element);
|
|
16
|
+
installed.set(id, entry = { element, users: 0 });
|
|
17
|
+
}
|
|
18
|
+
entry.users++;
|
|
19
|
+
signal.addEventListener('abort', () => {
|
|
20
|
+
if (!--entry.users) {
|
|
21
|
+
entry.element.remove();
|
|
22
|
+
installed.delete(id);
|
|
23
|
+
}
|
|
24
|
+
}, { once: true });
|
|
25
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { MountView, ViewHost } from '../types/view.js';
|
|
2
|
+
/** Props a Svelte 4 view component receives. */
|
|
3
|
+
export interface Svelte4ViewProps<C> {
|
|
4
|
+
/** Current context snapshot. */
|
|
5
|
+
context: C;
|
|
6
|
+
/** Host services; place popovers in `host.overlayRoot`. */
|
|
7
|
+
host: ViewHost;
|
|
8
|
+
}
|
|
9
|
+
/** Instance API of a Svelte 4 component used by the adapter. */
|
|
10
|
+
export interface Svelte4ViewInstance<C> {
|
|
11
|
+
/** Updates props. */
|
|
12
|
+
$set: (props: Partial<Svelte4ViewProps<C>>) => void;
|
|
13
|
+
/** Destroys the component. */
|
|
14
|
+
$destroy: () => void;
|
|
15
|
+
}
|
|
16
|
+
/** Svelte 4 component class rendered as a workbench view. */
|
|
17
|
+
export type Svelte4ViewComponent<C> = new (options: {
|
|
18
|
+
target: Element;
|
|
19
|
+
props?: Svelte4ViewProps<C>;
|
|
20
|
+
}) => Svelte4ViewInstance<C>;
|
|
21
|
+
/**
|
|
22
|
+
* Wraps a Svelte 4 component as a DOM `mount` view: `new Component({ target, props })`,
|
|
23
|
+
* `$set` on context updates, `$destroy` on disposal.
|
|
24
|
+
* @param Component Component rendered with the surface context.
|
|
25
|
+
*/
|
|
26
|
+
export declare function svelte4View<C>(Component: Svelte4ViewComponent<C>): MountView<C>;
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/svelte4/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAE3D,gDAAgD;AAChD,MAAM,WAAW,gBAAgB,CAAC,CAAC;IACjC,gCAAgC;IAChC,OAAO,EAAE,CAAC,CAAA;IACV,2DAA2D;IAC3D,IAAI,EAAE,QAAQ,CAAA;CACf;AAED,gEAAgE;AAChE,MAAM,WAAW,mBAAmB,CAAC,CAAC;IACpC,qBAAqB;IACrB,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IACnD,8BAA8B;IAC9B,QAAQ,EAAE,MAAM,IAAI,CAAA;CACrB;AAED,6DAA6D;AAC7D,MAAM,MAAM,oBAAoB,CAAC,CAAC,IAAI,KAAK,OAAO,EAAE;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAA;CAAE,KAAK,mBAAmB,CAAC,CAAC,CAAC,CAAA;AAE/H;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,SAAS,EAAE,oBAAoB,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAU/E"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a Svelte 4 component as a DOM `mount` view: `new Component({ target, props })`,
|
|
3
|
+
* `$set` on context updates, `$destroy` on disposal.
|
|
4
|
+
* @param Component Component rendered with the surface context.
|
|
5
|
+
*/
|
|
6
|
+
export function svelte4View(Component) {
|
|
7
|
+
return {
|
|
8
|
+
mount(container, context, host) {
|
|
9
|
+
const instance = new Component({ target: container, props: { context, host } });
|
|
10
|
+
return {
|
|
11
|
+
update: next => instance.$set({ context: next }),
|
|
12
|
+
dispose: () => instance.$destroy(),
|
|
13
|
+
};
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Component } from 'svelte';
|
|
2
|
+
import type { MountView, ViewHost } from '../types/view.js';
|
|
3
|
+
/** Props a Svelte 5 view component receives. */
|
|
4
|
+
export interface Svelte5ViewProps<C> {
|
|
5
|
+
/** Current context snapshot. */
|
|
6
|
+
readonly context: C;
|
|
7
|
+
/** Host services; place popovers in `host.overlayRoot`. */
|
|
8
|
+
readonly host: ViewHost;
|
|
9
|
+
}
|
|
10
|
+
/** Svelte 5 component rendered as a workbench view. */
|
|
11
|
+
export type Svelte5ViewComponent<C> = Component<Svelte5ViewProps<C>>;
|
|
12
|
+
/**
|
|
13
|
+
* Wraps a Svelte 5 component as a DOM `mount` view (Svelte 5.7+).
|
|
14
|
+
*
|
|
15
|
+
* Props are a plain object whose `context` getter reads a `createSubscriber` signal, so
|
|
16
|
+
* template effects reading `context` re-run when the host pushes a snapshot, without
|
|
17
|
+
* remounting and without compiling runes into this package.
|
|
18
|
+
* @param component Component rendered with the surface context.
|
|
19
|
+
*/
|
|
20
|
+
export declare function svelte5View<C>(component: Svelte5ViewComponent<C>): MountView<C>;
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/svelte5/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AACvC,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAI3D,gDAAgD;AAChD,MAAM,WAAW,gBAAgB,CAAC,CAAC;IACjC,gCAAgC;IAChC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAA;IACnB,2DAA2D;IAC3D,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAA;CACxB;AAED,uDAAuD;AACvD,MAAM,MAAM,oBAAoB,CAAC,CAAC,IAAI,SAAS,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAA;AAEpE;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,SAAS,EAAE,oBAAoB,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CA6B/E"}
|