mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-23 23:10:17 +05:30
Add context.formats.registerExportFormat(id, opts) to PluginContext,
backed by a new FormatRegistry (mirrors PluginRegistry's Map-based
shape). Plugins register namespaced (${pluginId}:${id}) export
formats with a label/extension/handler; the writing-studio built-in
plugin registers a trivial "sprint-summary" .txt export as a
worked example.
The plugin system lives entirely in the renderer process while the
Export menu is built in main.js, so wiring formats into the menu
required a small IPC round-trip: renderer sends format metadata to
main after plugin load (main rebuilds the menu via the already-
idempotent createMenu()), and a menu click sends the resolved save
path back to the renderer, which is the only process holding the
plugin's handler function.
Amit Haridas
82 lines
2.1 KiB
JavaScript
82 lines
2.1 KiB
JavaScript
const { PluginContext } = require('./plugin-context');
|
|
const { PluginAPI } = require('./plugin-api');
|
|
|
|
class PluginRegistry {
|
|
constructor(deps) {
|
|
this.deps = deps;
|
|
this.plugins = new Map();
|
|
this.exportHooks = { preHooks: [], postHooks: [] };
|
|
}
|
|
|
|
/**
|
|
* Register a discovered plugin. Creates instance, builds context, calls init.
|
|
* If init throws, plugin is NOT registered.
|
|
*/
|
|
register(pluginInfo) {
|
|
const { id, name, version, description, manifest, PluginClass, dir } = pluginInfo;
|
|
|
|
let instance;
|
|
if (PluginClass) {
|
|
instance = new PluginClass();
|
|
} else {
|
|
instance = new PluginAPI();
|
|
}
|
|
instance._manifest = manifest;
|
|
|
|
const context = new PluginContext({
|
|
pluginId: id,
|
|
sidebar: this.deps.sidebar,
|
|
commands: this.deps.commands,
|
|
statusBar: this.deps.statusBar,
|
|
eventBus: this.deps.eventBus,
|
|
settings: this.deps.settings,
|
|
editor: this.deps.editor,
|
|
ipc: this.deps.ipc,
|
|
exportHooks: this.exportHooks,
|
|
formatRegistry: this.deps.formatRegistry,
|
|
});
|
|
|
|
try {
|
|
instance.init(context);
|
|
} catch (err) {
|
|
console.error(`[PluginRegistry] Plugin "${id}" init failed:`, err.message);
|
|
return;
|
|
}
|
|
|
|
this.plugins.set(id, { id, name, version, description, manifest, instance, dir, context });
|
|
console.log(`[PluginRegistry] Registered plugin: ${name} v${version}`);
|
|
}
|
|
|
|
getPlugin(id) {
|
|
return this.plugins.get(id);
|
|
}
|
|
|
|
getAll() {
|
|
return Array.from(this.plugins.values());
|
|
}
|
|
|
|
activate(id) {
|
|
const plugin = this.plugins.get(id);
|
|
if (plugin?.instance) {
|
|
try {
|
|
plugin.instance.activate();
|
|
} catch (err) {
|
|
console.error(`[PluginRegistry] Plugin "${id}" activate error:`, err.message);
|
|
}
|
|
}
|
|
}
|
|
|
|
deactivate(id) {
|
|
const plugin = this.plugins.get(id);
|
|
if (plugin?.instance) {
|
|
try {
|
|
plugin.instance.deactivate();
|
|
} catch (err) {
|
|
console.error(`[PluginRegistry] Plugin "${id}" deactivate error:`, err.message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = { PluginRegistry };
|