- Move the PDF Viewer plugin into src/lib/features/pdf/ with pdfjs-dist 6.4.299 (minified builds). - A static pdf document type marks PDF files and opens them as tabs. The first PDF tab loads the module. - The worker, the character maps, the fonts, and the decoders come from the app. One worker for each tab. - The toolbar uses IconButton and the shared tooltip, with the new zoom-in, zoom-out, fit-width, and rotate icons. - Add THIRD_PARTY_NOTICES.md for PDF.js and xterm. - Update the tests, the menu inventory, the docs, and UI-059.
108 lines
5.1 KiB
TypeScript
108 lines
5.1 KiB
TypeScript
// A feature in the workbench of one window. Its views, commands, and document types come from the static catalog, so the
|
|
// navigation buttons, the command palette, and the file tree show them without the feature code. The first mount,
|
|
// restore, or action command loads the feature module and starts one instance for this window.
|
|
// A launch rule (`launch.ts`) can also start the instance at once.
|
|
import { tr } from "../i18n/index.ts";
|
|
import type { Dispose, PluginCommand, PluginDocumentContext, PluginDocumentType, PluginView } from "../plugins/sdk.ts";
|
|
import type { FeatureEntry } from "./catalog.ts";
|
|
import { loadFeature } from "./loader.ts";
|
|
import type { FeatureContext, FeatureInstance, FeatureModule } from "./types.ts";
|
|
|
|
export class FeatureSession {
|
|
readonly controller = new AbortController();
|
|
readonly views = new Map<string, PluginView>();
|
|
readonly documentTypes = new Map<string, PluginDocumentType>();
|
|
readonly commands = new Map<string, PluginCommand>();
|
|
readonly entry: FeatureEntry;
|
|
private context: FeatureContext;
|
|
private load: () => Promise<FeatureModule>;
|
|
private instance?: Promise<FeatureInstance>;
|
|
private ready?: FeatureInstance;
|
|
|
|
constructor(entry: FeatureEntry, context: FeatureContext, load: () => Promise<FeatureModule> = () => loadFeature(entry.id)) {
|
|
this.entry = entry;
|
|
this.context = context;
|
|
this.load = load;
|
|
for (const info of entry.views) {
|
|
this.views.set(info.id, {
|
|
id: info.id, get title() { return info.title(); }, placement: info.placement, icon: info.icon,
|
|
navigation: !!info.navigation, launcher: false,
|
|
mount: element => this.mountView(info.id, element),
|
|
saveTabState: () => this.ready?.saveTabState?.(info.id),
|
|
restoreTabState: async state => { await (await this.running()).restoreTabState?.(info.id, state); },
|
|
});
|
|
}
|
|
for (const info of entry.documentTypes) {
|
|
this.documentTypes.set(info.id, {
|
|
id: info.id, get title() { return info.title(); }, icon: info.icon, extensions: [...info.extensions], content: info.content,
|
|
identify: (path, source) => info.identify(path, source),
|
|
mount: (element, document) => this.mountDocument(info.id, element, document),
|
|
});
|
|
}
|
|
for (const info of entry.commands) {
|
|
const view = info.view;
|
|
this.commands.set(info.id, { id: info.id, get title() { return info.title(); }, icon: info.icon,
|
|
run: view ? () => context.openView(view) : async () => { await (await this.running()).run?.(info.id); } });
|
|
}
|
|
}
|
|
|
|
/** The instance of this window. A failed load leaves no instance, so a later call tries again. */
|
|
private running(): Promise<FeatureInstance> {
|
|
if (this.controller.signal.aborted) return Promise.reject(new Error(tr("This feature is off.")));
|
|
this.instance ??= this.load().then(module => {
|
|
if (this.controller.signal.aborted) throw new Error(tr("This feature is off."));
|
|
return this.ready = module.start(this.context);
|
|
}).catch(reason => { this.instance = undefined; throw reason; });
|
|
return this.instance;
|
|
}
|
|
|
|
private async mountView(id: string, element: HTMLElement): Promise<Dispose> {
|
|
return (await this.running()).mount(id, element);
|
|
}
|
|
|
|
private async mountDocument(type: string, element: HTMLElement, document: PluginDocumentContext): Promise<Dispose> {
|
|
const instance = await this.running();
|
|
if (!instance.mountDocument) throw new Error(tr("This view is unavailable."));
|
|
return instance.mountDocument(type, element, document);
|
|
}
|
|
|
|
/** Mount a view, or with `document`, a document of the document type `id`. */
|
|
async mount(id: string, element: HTMLElement, document?: PluginDocumentContext): Promise<Dispose> {
|
|
const view = document ? this.documentTypes.get(id) : this.views.get(id);
|
|
if (!view) throw new Error(tr("This view is unavailable."));
|
|
const dispose = document ? await (view as PluginDocumentType).mount(element, document) : await (view as PluginView).mount(element, { surface: "view" });
|
|
return async () => {
|
|
if (typeof dispose === "function") await dispose();
|
|
element.replaceChildren();
|
|
};
|
|
}
|
|
|
|
async launch(id: string) { this.context.openView(id); }
|
|
|
|
/** Load the module and start the instance now, without a view. Later calls use the same instance. */
|
|
async start(): Promise<void> { await this.running(); }
|
|
|
|
/** The instance of this window, for a feature with a surface that is not a view, such as the terminal panel. */
|
|
loadInstance(): Promise<FeatureInstance> { return this.running(); }
|
|
|
|
/** True after the instance started. */
|
|
get started(): boolean { return this.instance !== undefined; }
|
|
|
|
async run(id: string) {
|
|
const command = this.commands.get(id);
|
|
if (!command) throw new Error(tr("This command is unavailable."));
|
|
await command.run();
|
|
}
|
|
|
|
async dropTab(_data: string): Promise<void> { throw new Error(tr("This feature cannot receive this tab.")); }
|
|
|
|
/** Stop the instance. The host closes the views of the feature first. */
|
|
async stop() {
|
|
this.controller.abort();
|
|
const instance = this.ready ?? await this.instance?.catch(() => undefined);
|
|
this.ready = undefined;
|
|
this.instance = undefined;
|
|
await instance?.dispose();
|
|
}
|
|
}
|