Skip to content

Commit

Permalink
Feat use layout (#223)
Browse files Browse the repository at this point in the history
* style: format code

* feat: 添加 useLayout 暴露 closeTab 的能力
  • Loading branch information
winixt authored Dec 18, 2023
1 parent a3e931b commit 3223a20
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 75 deletions.
77 changes: 39 additions & 38 deletions packages/fes-plugin-layout/package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
{
"name": "@fesjs/plugin-layout",
"version": "5.1.7",
"description": "@fesjs/plugin-layout",
"main": "lib/index.js",
"files": [
"lib",
"types.d.ts"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/WeBankFinTech/fes.js.git",
"directory": "packages/fes-plugin-layout"
},
"keywords": [
"fes"
],
"author": "harrywan",
"license": "MIT",
"bugs": {
"url": "https://github.com/WeBankFinTech/fes.js/issues"
},
"homepage": "https://github.com/WeBankFinTech/fes.js#readme",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@fesjs/utils": "^3.0.1"
},
"peerDependencies": {
"@fesjs/fes": "^3.1.4",
"@fesjs/fes-design": ">=0.7.0",
"vue": "^3.2.47",
"vue-router": "^4.0.1"
},
"typings": "./types.d.ts"
"name": "@fesjs/plugin-layout",
"version": "5.1.7",
"description": "@fesjs/plugin-layout",
"author": "harrywan",
"license": "MIT",
"homepage": "https://github.com/WeBankFinTech/fes.js#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/WeBankFinTech/fes.js.git",
"directory": "packages/fes-plugin-layout"
},
"bugs": {
"url": "https://github.com/WeBankFinTech/fes.js/issues"
},
"keywords": [
"fes"
],
"main": "lib/index.js",
"files": [
"lib",
"types.d.ts"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"publishConfig": {
"access": "public"
},
"peerDependencies": {
"@fesjs/fes": "^3.1.4",
"@fesjs/fes-design": ">=0.7.0",
"vue": "^3.2.47",
"vue-router": "^4.0.1"
},
"dependencies": {
"@fesjs/utils": "^3.0.1",
"@vueuse/core": "^10.7.0"
},
"typings": "./types.d.ts"
}
12 changes: 6 additions & 6 deletions packages/fes-plugin-layout/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { join } from 'path';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { winPath } from '@fesjs/utils';
import { name } from '../package.json';

Expand Down Expand Up @@ -40,7 +40,7 @@ export default (api) => {

const iconNames = helper.getIconNamesFromMenu(userConfig.menus);

const iconsString = iconNames.map((iconName) => `import { ${iconName} } from '@fesjs/fes-design/icon'`);
const iconsString = iconNames.map(iconName => `import { ${iconName} } from '@fesjs/fes-design/icon'`);
api.writeTmpFile({
path: join(namespace, 'icons.js'),
content: `
Expand Down Expand Up @@ -84,15 +84,15 @@ export default (api) => {

api.addPluginExports(() => [
{
specifiers: ['Page', 'useTabTitle'],
specifiers: ['Page', 'useTabTitle', 'useLayout'],
source: join(namespace, 'index.js'),
},
]);

// 把 BaseLayout插入到路由配置中,作为根路由
// 添加 403 和 404 路由
api.modifyRoutes((routes) => {
if (!routes.find((item) => item.path === '/403')) {
if (!routes.find(item => item.path === '/403')) {
routes.push({
path: '/403',
name: 'Exception403',
Expand All @@ -102,7 +102,7 @@ export default (api) => {
},
});
}
if (!routes.find((item) => item.path === '/404')) {
if (!routes.find(item => item.path === '/404')) {
routes.push({
path: '/404',
name: 'Exception404',
Expand Down
1 change: 1 addition & 0 deletions packages/fes-plugin-layout/src/runtime/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as Page } from './views/page.vue';
export { useTabTitle } from './useTitle';
export * from './useLayout';
12 changes: 12 additions & 0 deletions packages/fes-plugin-layout/src/runtime/useLayout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createSharedComposable } from '@vueuse/core';
import { shallowReactive } from 'vue';

function _useLayout() {
const state = shallowReactive({
closeTab: () => {},
});

return state;
}

export const useLayout = createSharedComposable(_useLayout);
8 changes: 4 additions & 4 deletions packages/fes-plugin-layout/src/runtime/useTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { useRoute } from '@@/core/coreExports';

const cache = reactive(new Map());

export const getTitle = (path) => cache.get(path);
export const getTitle = path => cache.get(path);

export const deleteTitle = (patch) => cache.delete(patch);
export const deleteTitle = patch => cache.delete(patch);

export const useTabTitle = (title) => {
export function useTabTitle(title) {
const route = useRoute();
const titleRef = ref(title);
const path = route.path;

cache.set(path, titleRef);

return titleRef;
};
}
46 changes: 27 additions & 19 deletions packages/fes-plugin-layout/src/runtime/views/MultiTabProvider.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<template>
<template v-if="multiTabs">
<FTabs
:modelValue="route.path"
:model-value="route.path"
closable
:tabsPadding="24"
:tabs-padding="24"
type="card"
class="layout-content-tabs"
@close="handleCloseTab"
Expand All @@ -21,17 +21,19 @@
</FDropdown>
</template>
</FTabs>
<Page ref="pageRef" :pageKey="getPageKey" isAllKeepAlive />
<Page ref="pageRef" :page-key="getPageKey" is-all-keep-alive />
</template>
<Page v-else />
</template>

<script>
import { computed, unref, ref } from 'vue';
import { FTabs, FTabPane, FDropdown } from '@fesjs/fes-design';
import { ReloadOutlined, MoreOutlined } from '@fesjs/fes-design/icon';
import { useRouter, useRoute } from '@@/core/coreExports';
import { computed, ref, unref } from 'vue';
import { FDropdown, FTabPane, FTabs } from '@fesjs/fes-design';
import { MoreOutlined, ReloadOutlined } from '@fesjs/fes-design/icon';
import { useRoute, useRouter } from '@@/core/coreExports';
import { transTitle } from '../helpers/pluginLocale';
import { getTitle, deleteTitle } from '../useTitle';
import { deleteTitle, getTitle } from '../useTitle';
import { useLayout } from '../useLayout';
import Page from './page.vue';
let i = 0;
Expand All @@ -52,6 +54,8 @@ export default {
const pageRef = ref();
const route = useRoute();
const router = useRouter();
const layoutState = useLayout();
const createPage = (_route) => {
const computedTitle = computed(() => {
const customTitle = unref(getTitle(_route.path));
Expand All @@ -78,15 +82,16 @@ export default {
},
];
const findPage = (path) => pageList.value.find((item) => unref(item.path) === unref(path));
const findPage = path => pageList.value.find(item => unref(item.path) === unref(path));
router.beforeEach((to) => {
const page = findPage(to.path);
if (!page) {
if (!page)
pageList.value = [...pageList.value, createPage(to)];
} else {
else
page.route = to;
}
return true;
});
Expand All @@ -102,28 +107,30 @@ export default {
}
};
const handleCloseTab = async (targetKey) => {
targetKey = targetKey || route.path;
const selectedPage = findPage(targetKey);
const list = [...pageList.value];
const index = list.indexOf(selectedPage);
if (route.path === selectedPage.path) {
if (list.length > 1) {
if (list.length - 1 === index) {
if (list.length - 1 === index)
await switchPage(list[index - 1].path);
} else {
else
await switchPage(list[index + 1].path);
}
}
}
list.splice(index, 1);
pageList.value = list;
pageRef.value.removeKeepAlive(selectedPage.name);
deleteTitle(selectedPage.path);
};
layoutState.closeTab = handleCloseTab;
const reloadPage = (path) => {
const selectedPage = findPage(path || unref(route.path));
if (selectedPage) {
if (selectedPage)
selectedPage.key = getKey();
}
};
const closeOtherPage = (path) => {
const selectedPage = findPage(path || unref(route.path));
Expand All @@ -132,9 +139,9 @@ export default {
};
const getPageKey = (_route) => {
const selectedPage = findPage(_route.path);
if (selectedPage) {
if (selectedPage)
return selectedPage.key;
}
return '';
};
const handlerMore = (key) => {
Expand Down Expand Up @@ -163,6 +170,7 @@ export default {
},
};
</script>
<style lang="less">
.layout-content-tabs {
background: rgb(255, 255, 255);
Expand Down
12 changes: 9 additions & 3 deletions packages/fes-template/src/pages/editor.vue
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
<template>
<monaco-editor ref="editorRef" v-model="json" :language="language" height="200px" check />
<MonacoEditor ref="editorRef" v-model="json" :language="language" height="200px" check />
{{ json }}
</template>

<config>
{
"name": "editor",
"title": "$editor"
}
</config>

<script>
import { onMounted, ref } from 'vue';
import { MonacoEditor } from '@fesjs/fes';
import { MonacoEditor, useLayout } from '@fesjs/fes';
export default {
components: {
MonacoEditor,
},
setup() {
console.log('editor.vue');
const { closeTab } = useLayout();
const editorRef = ref();
const json = ref('');
const language = ref('json');
onMounted(() => {
setTimeout(() => {
language.value = 'html';
}, 2000);
setTimeout(() => {
closeTab();
}, 3000);
});
return {
Expand Down
9 changes: 6 additions & 3 deletions packages/fes-template/src/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<template>
<div class="page">
home
<FButton class="m-2" @click="go">Button</FButton>
<FButton class="m-2" @click="go">
Button
</FButton>
</div>
</template>

Expand All @@ -13,12 +15,13 @@ defineRouteMeta({
name: 'index',
title: '$home',
});
console.log('123123'.replaceAll('123', '234'));
const router = useRouter();
const go = () => {
function go() {
router.push('/editor');
};
}
</script>

<style>
Expand Down
Loading

0 comments on commit 3223a20

Please sign in to comment.