From 3b7c21689abd4b4feb1070fe78e654f359b41a17 Mon Sep 17 00:00:00 2001 From: chencheng Date: Sun, 29 Sep 2019 15:32:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(block):=20=E6=A3=80=E6=B5=8B=E5=8C=BA?= =?UTF-8?q?=E5=9D=97=E7=89=B9=E6=80=A7=E5=92=8C=E6=9C=AC=E5=9C=B0=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E7=9A=84=E5=8C=B9=E9=85=8D=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/commands/block/ui/server/index.ts | 75 ++++++++++++++++++- .../block/ui/ui/BlockList/BlockItem.tsx | 4 + 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/packages/umi-build-dev/src/plugins/commands/block/ui/server/index.ts b/packages/umi-build-dev/src/plugins/commands/block/ui/server/index.ts index 7ab8dfb67116..36d2030d528d 100644 --- a/packages/umi-build-dev/src/plugins/commands/block/ui/server/index.ts +++ b/packages/umi-build-dev/src/plugins/commands/block/ui/server/index.ts @@ -148,6 +148,7 @@ export default (api: IApi) => { success: true, }); break; + // 取消任务 case 'org.umi.block.cancel': success({ @@ -179,16 +180,84 @@ export default (api: IApi) => { break; case 'org.umi.block.checkIfCanAdd': + function haveFeature(feature) { + return payload.item.features && payload.item.features.includes(feature); + } + if (!api.config.routes) { - failure({ + return failure({ message: lang === 'zh-CN' ? '区块添加不支持约定式路由,请转成配置式路由。' : 'The block adding does not support the conventional route, please convert to a configuration route.', }); - } else { - success({}); } + + const payloadType = payload.type === 'block' ? '区块' : '模板'; + const isBigfish = !!process.env.BIGFISH_COMPAT; + const reactPlugin = api.config.plugins.find(p => { + return p === 'umi-plugin-react' || p[0] === 'umi-plugin-react'; + }); + const reactPluginOpts = reactPlugin + ? typeof reactPlugin === 'string' + ? {} + : reactPlugin[1] + : {}; + + // antd 特性依赖 + // bigfish 默认开了 antd + if (haveFeature('antd') && !isBigfish) { + if (!reactPlugin || !reactPluginOpts.antd) { + return failure({ + message: + lang === 'zh-CN' + ? `${payloadType}依赖 antd,请安装 umi-plugin-react 插件并开启 antd 。` + : 'Block depends on antd, please install umi-plugin-react and enable antd.', + }); + } + } + + // dva 特性依赖 + if (haveFeature('dva')) { + if (isBigfish) { + if (!api.config.dva) { + return failure({ + message: `${payloadType}依赖 dva,请开启 dva 配置。`, + }); + } + } else { + if (!reactPlugin || !reactPluginOpts.locale) { + return failure({ + message: + lang === 'zh-CN' + ? `${payloadType}依赖 dva,请安装 umi-plugin-react 插件并开启 dva 。` + : 'Block depends on dva, please install umi-plugin-react and enable dva.', + }); + } + } + } + + // locale 特性依赖 + if (haveFeature('i18n')) { + if (isBigfish) { + if (!api.config.locale) { + return failure({ + message: `${payloadType}依赖 locale,请开启 locale 配置。`, + }); + } + } else { + if (!reactPlugin || !reactPluginOpts.locale) { + return failure({ + message: + lang === 'zh-CN' + ? `${payloadType}依赖国际化(i18n),请安装 umi-plugin-react 插件并开启 locale 。` + : 'Block depends on i18n, please install umi-plugin-react and enable locale.', + }); + } + } + } + + success({}); break; // 检查路由是否存在 diff --git a/packages/umi-build-dev/src/plugins/commands/block/ui/ui/BlockList/BlockItem.tsx b/packages/umi-build-dev/src/plugins/commands/block/ui/ui/BlockList/BlockItem.tsx index 9b171353a360..5e736af776a9 100644 --- a/packages/umi-build-dev/src/plugins/commands/block/ui/ui/BlockList/BlockItem.tsx +++ b/packages/umi-build-dev/src/plugins/commands/block/ui/ui/BlockList/BlockItem.tsx @@ -70,6 +70,10 @@ const onBeforeOpenModal = async (api, { item, type, onShowModal }) => { try { await api.callRemote({ type: 'org.umi.block.checkIfCanAdd', + payload: { + item, + type, + }, }); } catch (e) { message.error(e.message);