PluginManager API
大约 1 分钟
PluginManager API
PluginManager 管理插件的加载、卸载和生命周期。
引入
const { PluginManager } = vjmapext;通常通过 mapcadLayer.pluginManager 访问,或使用 mapcadLayer.loadPlugin(plugin) 快捷方法。
方法
load(plugin)
加载并安装插件。
| 参数 | 类型 | 说明 |
|---|---|---|
| plugin | Plugin | 插件对象 |
| 返回 | Promise<void> | 安装完成的 Promise |
await mapcadLayer.loadPlugin({
manifest: { name: 'my-plugin', version: '1.0.0' },
install(ctx) {
ctx.commandRegistry.register('MYCMD', MyCommand);
}
});unload(name)
按名称卸载插件。如果插件提供了 deactivate,会先调用。
| 参数 | 类型 | 说明 |
|---|---|---|
| name | string | 插件名称(manifest.name) |
getLoaded()
返回所有已加载的插件列表。
| 返回值 | 类型 | 说明 |
|---|---|---|
| — | Plugin[] | 已加载的插件数组 |
类型定义
Plugin
interface Plugin {
manifest: PluginManifest;
install(ctx: PluginContext): void | Promise<void>;
activate?(): void;
deactivate?(): void;
}PluginManifest
interface PluginManifest {
name: string;
version: string;
description?: string;
}PluginContext
插件安装时接收的上下文对象,提供对核心子系统的访问。
interface PluginContext {
commandRegistry: CommandRegistry;
entityRegistry: EntityRegistry;
eventBus: EventBus;
store: EntityStore;
on(event: string, handler: Function): void;
off(event: string, handler: Function): void;
}| 属性 | 说明 |
|---|---|
commandRegistry | 注册自定义命令 |
entityRegistry | 注册自定义实体类型 |
eventBus | 订阅/发布事件 |
store | 访问实体数据 |
on / off | EventBus 的快捷方法 |
完整示例
const measurePlugin = {
manifest: {
name: 'measure-tool',
version: '1.0.0',
description: '测量工具插件'
},
install(ctx) {
ctx.commandRegistry.register('MEASURE', {
async main(cmdCtx) {
const p1 = await cmdCtx.inputManager.getPoint({ prompt: '起点' });
if (p1.status !== 0) return;
const p2 = await cmdCtx.inputManager.getPoint({ prompt: '终点' });
if (p2.status !== 0) return;
const dx = p2.point.x - p1.point.x;
const dy = p2.point.y - p1.point.y;
const dist = Math.sqrt(dx * dx + dy * dy);
console.log(`距离: ${dist.toFixed(2)}`);
}
});
},
activate() {
console.log('测量插件已激活');
},
deactivate() {
console.log('测量插件已停用');
}
};
await mapcadLayer.loadPlugin(measurePlugin);
await mapcadLayer.executeCommand('MEASURE');