Plugin manager for dynamically loading and unloading plugins.

This class manages the lifecycle of plugins, including:

  • Loading plugins and calling their onLoad hooks
  • Unloading plugins and calling their onUnload hooks
  • Providing access to the application context and command manager

Plugins are responsible for cleaning up their own registered commands in the onUnload hook using commandManager.removeCmd().

const pluginManager = AcApDocManager.instance.pluginManager;

// Load a plugin
const myPlugin = new MyPlugin();
await pluginManager.loadPlugin(myPlugin);

// Unload a plugin
await pluginManager.unloadPlugin('MyPlugin');

// Check if a plugin is loaded
if (pluginManager.isPluginLoaded('MyPlugin')) {
console.log('Plugin is loaded');
}

// Get all loaded plugins
const loadedPlugins = pluginManager.getLoadedPlugins();

Constructors

Methods

  • Loads a plugin and calls its onLoad hook.

    If the plugin is already loaded, this method will throw an error. The plugin's onLoad method will be called with the context and command manager.

    Parameters

    Returns Promise<void>

    If a plugin with the same name is already loaded

    const plugin = new MyPlugin();
    await pluginManager.loadPlugin(plugin);
  • Loads multiple plugins from a configuration array.

    This method accepts an array of plugin instances or plugin factory functions. Factory functions are useful when you want to create plugin instances lazily.

    Parameters

    • plugins: (AcApPlugin | () => AcApPlugin)[]

      Array of plugin instances or factory functions that return plugin instances

    • Optionaloptions: { continueOnError?: boolean }

      Optional configuration for loading behavior

      • OptionalcontinueOnError?: boolean

        If true, continue loading other plugins even if one fails (default: false)

    Returns Promise<{ failed: { error: Error; name: string }[]; loaded: string[] }>

    Promise that resolves to an object containing successful and failed plugin loads

    // Load plugins from instances
    await pluginManager.loadPluginsFromConfig([
    new MyPlugin1(),
    new MyPlugin2()
    ]);

    // Load plugins from factory functions
    await pluginManager.loadPluginsFromConfig([
    () => new MyPlugin1(),
    () => new MyPlugin2()
    ]);

    // Continue loading even if some fail
    const result = await pluginManager.loadPluginsFromConfig(
    [new Plugin1(), new Plugin2()],
    { continueOnError: true }
    );
    console.log('Loaded:', result.loaded);
    console.log('Failed:', result.failed);
  • Loads plugins from a folder using dynamic imports.

    This method scans a folder for plugin files and dynamically imports them. It expects each plugin file to export a default export that is either:

    • A plugin instance
    • A plugin class (constructor function)
    • A factory function that returns a plugin instance

    Parameters

    • folderPath: string

      Path to the folder containing plugin files (relative to the base URL)

    • Optionaloptions: { continueOnError?: boolean; pluginList?: string[] }

      Optional configuration for loading behavior

      • OptionalcontinueOnError?: boolean

        If true, continue loading other plugins even if one fails (default: false)

      • OptionalpluginList?: string[]

        Optional array of specific plugin file names to load (if not provided, attempts to auto-discover)

    Returns Promise<{ failed: { error: Error; name: string }[]; loaded: string[] }>

    Promise that resolves to an object containing successful and failed plugin loads

    // Load all plugins from a folder (requires plugin list or manifest)
    await pluginManager.loadPluginsFromFolder('./plugins', {
    pluginList: ['MyPlugin1.js', 'MyPlugin2.js']
    });

    // Or with continue on error
    const result = await pluginManager.loadPluginsFromFolder('./plugins', {
    pluginList: ['Plugin1.js', 'Plugin2.js'],
    continueOnError: true
    });

    In browser environments, you typically need to provide a list of plugin files to load, as there's no direct way to list directory contents. You can:

    1. Provide a pluginList array with specific file names
    2. Create a manifest file that lists all plugins
    3. Use a build-time tool to generate the plugin list
  • Unloads a plugin and calls its onUnload hook.

    This method will:

    1. Call the plugin's onUnload hook (plugins should clean up their commands here)
    2. Remove the plugin from the loaded plugins map

    Parameters

    • pluginName: string

      The name of the plugin to unload

    Returns Promise<boolean>

    true if the plugin was successfully unloaded, false if it wasn't loaded

    const success = await pluginManager.unloadPlugin('MyPlugin');
    if (success) {
    console.log('Plugin unloaded successfully');
    }