Skip to content

Commit ad044f4

Browse files
liujupingJackLian
authored andcommitted
feat(utils): add workspace utils
1 parent 27e914c commit ad044f4

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

packages/designer/src/plugin/plugin-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface ILowCodePluginContextPrivate {
6060
set workspace(workspace: IPublicApiWorkspace);
6161
set editorWindow(window: IPublicModelWindow);
6262
set registerLevel(level: IPublicEnumPluginRegisterLevel);
63+
set isPluginRegisteredInWorkspace(flag: boolean);
6364
}
6465
export interface ILowCodePluginContextApiAssembler {
6566
assembleApis(

packages/engine/src/engine-core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const pluginContextApiAssembler: ILowCodePluginContextApiAssembler = {
139139
context.logger = new Logger({ level: 'warn', bizName: `plugin:${pluginName}` });
140140
context.workspace = workspace;
141141
context.registerLevel = IPublicEnumPluginRegisterLevel.Default;
142+
context.isPluginRegisteredInWorkspace = false;
142143
},
143144
};
144145

packages/types/src/shell/model/plugin-context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ export interface IPublicModelPluginContext {
108108
*/
109109
get registerLevel(): IPublicEnumPluginRegisterLevel;
110110

111+
get isPluginRegisteredInWorkspace(): boolean;
112+
111113
get editorWindow(): IPublicModelWindow;
112114
}
113115

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ export * from './is-plugin-event-name';
3030
export * as css from './css-helper';
3131
export { transactionManager } from './transaction-manager';
3232
export * from './check-types';
33+
export * from './workspace';

packages/utils/src/workspace.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React, { useEffect, useState, useCallback } from 'react';
2+
import { IPublicModelPluginContext, IPublicEnumPluginRegisterLevel, IPublicModelWindow, IPublicModelEditorView } from '@alilc/lowcode-types';
3+
4+
/**
5+
* 高阶组件(HOC):为组件提供 view 插件上下文。
6+
*
7+
* @param {React.ComponentType} Component - 需要被封装的组件。
8+
* @param {string|string[]} viewName - 视图名称或视图名称数组,用于过滤特定的视图插件上下文。
9+
* @returns {React.ComponentType} 返回封装后的组件。
10+
*
11+
* @example
12+
* // 用法示例(函数组件):
13+
* const EnhancedComponent = ProvideViewPluginContext(MyComponent, "viewName");
14+
*/
15+
export const ProvideViewPluginContext = (Component: any, viewName?: string | string[]) => {
16+
// 创建一个新的函数组件,以便在其中使用 Hooks
17+
return function WithPluginContext(props: {
18+
[key: string]: any;
19+
20+
pluginContext?: IPublicModelPluginContext;
21+
}) {
22+
const getPluginContextFun = useCallback((editorWindow?: IPublicModelWindow | null) => {
23+
if (!editorWindow?.currentEditorView) {
24+
return null;
25+
}
26+
if (viewName) {
27+
const items = editorWindow?.editorViews.filter(d => (d as any).viewName === viewName || (Array.isArray(viewName) && viewName.includes((d as any).viewName)));
28+
return items[0];
29+
} else {
30+
return editorWindow.currentEditorView;
31+
}
32+
}, []);
33+
34+
const { workspace } = props.pluginContext || {};
35+
const [pluginContext, setPluginContext] = useState<IPublicModelEditorView | null>(getPluginContextFun(workspace?.window));
36+
37+
useEffect(() => {
38+
if (workspace?.window) {
39+
const ctx = getPluginContextFun(workspace.window);
40+
ctx && setPluginContext(ctx);
41+
}
42+
return workspace?.onChangeActiveEditorView(() => {
43+
const ctx = getPluginContextFun(workspace.window);
44+
ctx && setPluginContext(ctx);
45+
});
46+
}, [workspace, getPluginContextFun]);
47+
48+
if (props.pluginContext?.registerLevel !== IPublicEnumPluginRegisterLevel.Workspace || !props.pluginContext) {
49+
return <Component {...props} />;
50+
}
51+
52+
return <Component {...props} pluginContext={pluginContext} />;
53+
};
54+
};

packages/workspace/src/context/base-context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ export class BasicContext implements IBasicContext {
170170
context.editorWindow = new Window(editorWindow);
171171
}
172172
context.registerLevel = registerLevel;
173+
context.isPluginRegisteredInWorkspace = registerLevel === IPublicEnumPluginRegisterLevel.Workspace;
173174
},
174175
};
175176

0 commit comments

Comments
 (0)