mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2025-12-14 21:12:53 +00:00
feat(utils): add workspace utils
This commit is contained in:
parent
32c5202767
commit
0222f31202
@ -60,6 +60,7 @@ export interface ILowCodePluginContextPrivate {
|
|||||||
set workspace(workspace: IPublicApiWorkspace);
|
set workspace(workspace: IPublicApiWorkspace);
|
||||||
set editorWindow(window: IPublicModelWindow);
|
set editorWindow(window: IPublicModelWindow);
|
||||||
set registerLevel(level: IPublicEnumPluginRegisterLevel);
|
set registerLevel(level: IPublicEnumPluginRegisterLevel);
|
||||||
|
set isPluginRegisteredInWorkspace(flag: boolean);
|
||||||
}
|
}
|
||||||
export interface ILowCodePluginContextApiAssembler {
|
export interface ILowCodePluginContextApiAssembler {
|
||||||
assembleApis(
|
assembleApis(
|
||||||
|
|||||||
@ -139,6 +139,7 @@ const pluginContextApiAssembler: ILowCodePluginContextApiAssembler = {
|
|||||||
context.logger = new Logger({ level: 'warn', bizName: `plugin:${pluginName}` });
|
context.logger = new Logger({ level: 'warn', bizName: `plugin:${pluginName}` });
|
||||||
context.workspace = workspace;
|
context.workspace = workspace;
|
||||||
context.registerLevel = IPublicEnumPluginRegisterLevel.Default;
|
context.registerLevel = IPublicEnumPluginRegisterLevel.Default;
|
||||||
|
context.isPluginRegisteredInWorkspace = false;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -108,6 +108,8 @@ export interface IPublicModelPluginContext {
|
|||||||
*/
|
*/
|
||||||
get registerLevel(): IPublicEnumPluginRegisterLevel;
|
get registerLevel(): IPublicEnumPluginRegisterLevel;
|
||||||
|
|
||||||
|
get isPluginRegisteredInWorkspace(): boolean;
|
||||||
|
|
||||||
get editorWindow(): IPublicModelWindow;
|
get editorWindow(): IPublicModelWindow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -30,3 +30,4 @@ export * from './is-plugin-event-name';
|
|||||||
export * as css from './css-helper';
|
export * as css from './css-helper';
|
||||||
export { transactionManager } from './transaction-manager';
|
export { transactionManager } from './transaction-manager';
|
||||||
export * from './check-types';
|
export * from './check-types';
|
||||||
|
export * from './workspace';
|
||||||
|
|||||||
54
packages/utils/src/workspace.tsx
Normal file
54
packages/utils/src/workspace.tsx
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import React, { useEffect, useState, useCallback } from 'react';
|
||||||
|
import { IPublicModelPluginContext, IPublicEnumPluginRegisterLevel, IPublicModelWindow, IPublicModelEditorView } from '@alilc/lowcode-types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 高阶组件(HOC):为组件提供 view 插件上下文。
|
||||||
|
*
|
||||||
|
* @param {React.ComponentType} Component - 需要被封装的组件。
|
||||||
|
* @param {string|string[]} viewName - 视图名称或视图名称数组,用于过滤特定的视图插件上下文。
|
||||||
|
* @returns {React.ComponentType} 返回封装后的组件。
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* // 用法示例(函数组件):
|
||||||
|
* const EnhancedComponent = ProvideViewPluginContext(MyComponent, "viewName");
|
||||||
|
*/
|
||||||
|
export const ProvideViewPluginContext = (Component: any, viewName?: string | string[]) => {
|
||||||
|
// 创建一个新的函数组件,以便在其中使用 Hooks
|
||||||
|
return function WithPluginContext(props: {
|
||||||
|
[key: string]: any;
|
||||||
|
|
||||||
|
pluginContext?: IPublicModelPluginContext;
|
||||||
|
}) {
|
||||||
|
const getPluginContextFun = useCallback((editorWindow?: IPublicModelWindow | null) => {
|
||||||
|
if (!editorWindow?.currentEditorView) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (viewName) {
|
||||||
|
const items = editorWindow?.editorViews.filter(d => (d as any).viewName === viewName || (Array.isArray(viewName) && viewName.includes((d as any).viewName)));
|
||||||
|
return items[0];
|
||||||
|
} else {
|
||||||
|
return editorWindow.currentEditorView;
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const { workspace } = props.pluginContext || {};
|
||||||
|
const [pluginContext, setPluginContext] = useState<IPublicModelEditorView | null>(getPluginContextFun(workspace?.window));
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (workspace?.window) {
|
||||||
|
const ctx = getPluginContextFun(workspace.window);
|
||||||
|
ctx && setPluginContext(ctx);
|
||||||
|
}
|
||||||
|
return workspace?.onChangeActiveEditorView(() => {
|
||||||
|
const ctx = getPluginContextFun(workspace.window);
|
||||||
|
ctx && setPluginContext(ctx);
|
||||||
|
});
|
||||||
|
}, [workspace, getPluginContextFun]);
|
||||||
|
|
||||||
|
if (props.pluginContext?.registerLevel !== IPublicEnumPluginRegisterLevel.Workspace || !props.pluginContext) {
|
||||||
|
return <Component {...props} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <Component {...props} pluginContext={pluginContext} />;
|
||||||
|
};
|
||||||
|
};
|
||||||
@ -170,6 +170,7 @@ export class BasicContext implements IBasicContext {
|
|||||||
context.editorWindow = new Window(editorWindow);
|
context.editorWindow = new Window(editorWindow);
|
||||||
}
|
}
|
||||||
context.registerLevel = registerLevel;
|
context.registerLevel = registerLevel;
|
||||||
|
context.isPluginRegisteredInWorkspace = registerLevel === IPublicEnumPluginRegisterLevel.Workspace;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user