Skip to content

Commit

Permalink
feat(utils): add workspace utils
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping committed Nov 23, 2023
1 parent d47df9d commit d74d949
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/designer/src/plugin/plugin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface ILowCodePluginContextPrivate {
set workspace(workspace: IPublicApiWorkspace);
set editorWindow(window: IPublicModelWindow);
set registerLevel(level: IPublicEnumPluginRegisterLevel);
set isPluginRegisteredInWorkspace(flag: boolean);
}
export interface ILowCodePluginContextApiAssembler {
assembleApis(
Expand Down
1 change: 1 addition & 0 deletions packages/engine/src/engine-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const pluginContextApiAssembler: ILowCodePluginContextApiAssembler = {
context.logger = new Logger({ level: 'warn', bizName: `plugin:${pluginName}` });
context.workspace = workspace;
context.registerLevel = IPublicEnumPluginRegisterLevel.Default;
context.isPluginRegisteredInWorkspace = false;
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export * from './is-plugin-event-name';
export * as css from './css-helper';
export { transactionManager } from './transaction-manager';
export * from './check-types';
export * from './workspace';
54 changes: 54 additions & 0 deletions packages/utils/src/workspace.tsx
Original file line number Diff line number Diff line change
@@ -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} />;
};
};
1 change: 1 addition & 0 deletions packages/workspace/src/context/base-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export class BasicContext implements IBasicContext {
context.editorWindow = new Window(editorWindow);
}
context.registerLevel = registerLevel;
context.isPluginRegisteredInWorkspace = registerLevel === IPublicEnumPluginRegisterLevel.Workspace;
},
};

Expand Down

0 comments on commit d74d949

Please sign in to comment.