Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): 新增api.addUiMenu #11032

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/preset-umi/src/features/ui/ui.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isLocalDev } from '@umijs/utils';
import fs from 'fs';
import path from 'path';
import { IApi } from '../../types';
import { IApi, IUIModule } from '../../types';
// @ts-ignore
import sirv from '../../../compiled/sirv';

Expand Down Expand Up @@ -88,6 +88,20 @@ export default (api: IApi) => {
];
});

api.modifyAppData(async (memo) => {
const uiModules: IUIModule =
(await api.applyPlugins({
key: 'addUIModules',
initialValue: [],
})) ?? [];
Object.assign(memo, {
ui: {
modules: uiModules,
},
});
return memo;
Comment on lines +97 to +102
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥要这么用,直接 memo.ui 不就好了,这样会慢 40% ,参考 benchmark

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

学习了,我后面改掉

});

api.onGenerateFiles(({ isFirstTime }) => {
if (!isFirstTime) return;
api.writeTmpFile({
Expand All @@ -111,7 +125,6 @@ uiBtn.addEventListener('click', () => {
`,
});
});

api.addEntryImports(() => {
return [{ source: '@@/core/ui' }];
});
Expand Down
2 changes: 1 addition & 1 deletion packages/preset-umi/src/registerMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default (api: IApi) => {
'addHTMLScripts',
'addTmpGenerateWatcherPaths',
'addOnDemandDeps',
'addUIModules',
'chainWebpack',
'modifyEntry',
'modifyHTMLFavicon',
Expand Down Expand Up @@ -122,7 +123,6 @@ export default (api: IApi) => {
const transformIEAR = transformModule.default;
content = transformIEAR({ content, path: absPath }, api);
}

if (!existsSync(absPath)) {
writeFileSync(absPath, content!, 'utf-8');
} else {
Expand Down
12 changes: 12 additions & 0 deletions packages/preset-umi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ export interface IOnGenerateFiles {
files?: IFileInfo | null;
isFirstTime?: boolean;
}
export interface IUIMenu {
path: string;
url: string;
icon: string;
name: string;
}
export interface IUIModule {
name: string;
menus?: IUIMenu[];
[key: string]: any;
}
export type GenerateFilesFn = (opts: IOnGenerateFiles) => Promise<void>;
export type OnConfigChangeFn = (opts: {
generate: GenerateFilesFn;
Expand Down Expand Up @@ -111,6 +122,7 @@ export type IApi = PluginAPI &
addRuntimePlugin: IAdd<null, string>;
addRuntimePluginKey: IAdd<null, string>;
addTmpGenerateWatcherPaths: IAdd<null, string>;
addUIModules: IAdd<null, IUIModule[]>;
chainWebpack: {
(fn: {
(
Expand Down
20 changes: 20 additions & 0 deletions ui/app.ts → ui/app.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PluginContainer } from '@/components/PluginContainer';
import { IAppData } from '@/hooks/useAppData';
import { globalCSS } from '@/utils/globalCSS';
import { createGlobalStyle } from 'umi';

Expand Down Expand Up @@ -39,3 +41,21 @@ export const reactQuery = {
},
},
};

export async function patchClientRoutes({ routes }) {
const {
ui: { modules = [] },
} = (await fetch('/__umi/api/app-data').then(
(res) => res.json() as Promise<IAppData>,
)) ?? {
ui: {},
};
const uiMenusAdded = modules.map((module) => module.menus || []).flat();

routes[0].routes?.unshift(
...uiMenusAdded.map((menu: any) => ({
path: menu.path,
element: <PluginContainer url={menu.url} name={menu.name} />,
})),
);
}
8 changes: 7 additions & 1 deletion ui/components/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useAppData } from '@/hooks/useAppData';
import { state as globalState } from '@/models/global';
import { Icon, NavLink, styled, useSnapshot } from 'umi';

Expand All @@ -23,10 +24,15 @@ const Wrapper = styled.div`
`;

export function Menu() {
const {
ui: { modules = [] },
} = useAppData().data || { ui: {} };
const { menus } = useSnapshot(globalState);
const uiMenusAdded = modules.map((module) => module.menus || []).flat();

return (
<Wrapper>
{menus.map((menu) => (
{menus.concat(uiMenusAdded).map((menu) => (
<div key={menu.name}>
<NavLink
to={menu.path}
Expand Down
3 changes: 3 additions & 0 deletions ui/components/PluginContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const PluginContainer = (props: { url: string; name: string }) => {
return <iframe style={{ width: '100%', height: '100%' }} src={props.url} />;
};
12 changes: 12 additions & 0 deletions ui/hooks/useAppData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ export interface IAppData {
prepare: {
buildResult: BuildResult;
};
ui: {
modules: {
name: string;
menus?: {
path: string;
url: string;
icon: string;
name: string;
}[];
[key: string]: any;
}[];
};
}

export function useAppData() {
Expand Down