Skip to content

Commit

Permalink
❄️ refactor: provider naive wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
pdsuwwz committed May 29, 2024
1 parent 6dccb80 commit 7220043
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 67 deletions.
6 changes: 5 additions & 1 deletion .eslintrc-auto-import.json
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@
"watchThrottled": true,
"watchTriggerable": true,
"watchWithFilter": true,
"whenever": true
"whenever": true,
"NotificationPlacement": true,
"reloadRouterViewInjectionKey": true,
"useApplyForceReload": true,
"useRegisterForceReload": true
}
}
12 changes: 12 additions & 0 deletions auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ declare global {
const refDefault: typeof import('@vueuse/core')['refDefault']
const refThrottled: typeof import('@vueuse/core')['refThrottled']
const refWithControl: typeof import('@vueuse/core')['refWithControl']
const reloadRouterViewInjectionKey: typeof import('./src/hooks/useForceReload')['reloadRouterViewInjectionKey']
const render: typeof import('vue')['render']
const resolveComponent: typeof import('vue')['resolveComponent']
const resolveRef: typeof import('@vueuse/core')['resolveRef']
Expand Down Expand Up @@ -128,6 +129,7 @@ declare global {
const until: typeof import('@vueuse/core')['until']
const useActiveElement: typeof import('@vueuse/core')['useActiveElement']
const useAnimate: typeof import('@vueuse/core')['useAnimate']
const useApplyForceReload: typeof import('./src/hooks/useForceReload')['useApplyForceReload']
const useArrayDifference: typeof import('@vueuse/core')['useArrayDifference']
const useArrayEvery: typeof import('@vueuse/core')['useArrayEvery']
const useArrayFilter: typeof import('@vueuse/core')['useArrayFilter']
Expand Down Expand Up @@ -241,6 +243,7 @@ declare global {
const usePrevious: typeof import('@vueuse/core')['usePrevious']
const useRafFn: typeof import('@vueuse/core')['useRafFn']
const useRefHistory: typeof import('@vueuse/core')['useRefHistory']
const useRegisterForceReload: typeof import('./src/hooks/useForceReload')['useRegisterForceReload']
const useResizeObserver: typeof import('@vueuse/core')['useResizeObserver']
const useRoute: typeof import('vue-router')['useRoute']
const useRouter: typeof import('vue-router')['useRouter']
Expand Down Expand Up @@ -319,6 +322,9 @@ declare global {
// @ts-ignore
export type { RouteRecordRaw, RouteLocationRaw, LocationQuery, RouteParams, RouteLocationNormalizedLoaded, RouteRecordName, NavigationGuard } from 'vue-router'
import('vue-router')
// @ts-ignore
export type { NotificationPlacement } from 'naive-ui'
import('naive-ui')
}
// for vue template auto import
import { UnwrapRef } from 'vue'
Expand Down Expand Up @@ -415,6 +421,7 @@ declare module 'vue' {
readonly refDefault: UnwrapRef<typeof import('@vueuse/core')['refDefault']>
readonly refThrottled: UnwrapRef<typeof import('@vueuse/core')['refThrottled']>
readonly refWithControl: UnwrapRef<typeof import('@vueuse/core')['refWithControl']>
readonly reloadRouterViewInjectionKey: UnwrapRef<typeof import('./src/hooks/useForceReload')['reloadRouterViewInjectionKey']>
readonly render: UnwrapRef<typeof import('vue')['render']>
readonly resolveComponent: UnwrapRef<typeof import('vue')['resolveComponent']>
readonly resolveRef: UnwrapRef<typeof import('@vueuse/core')['resolveRef']>
Expand Down Expand Up @@ -448,6 +455,7 @@ declare module 'vue' {
readonly until: UnwrapRef<typeof import('@vueuse/core')['until']>
readonly useActiveElement: UnwrapRef<typeof import('@vueuse/core')['useActiveElement']>
readonly useAnimate: UnwrapRef<typeof import('@vueuse/core')['useAnimate']>
readonly useApplyForceReload: UnwrapRef<typeof import('./src/hooks/useForceReload')['useApplyForceReload']>
readonly useArrayDifference: UnwrapRef<typeof import('@vueuse/core')['useArrayDifference']>
readonly useArrayEvery: UnwrapRef<typeof import('@vueuse/core')['useArrayEvery']>
readonly useArrayFilter: UnwrapRef<typeof import('@vueuse/core')['useArrayFilter']>
Expand Down Expand Up @@ -561,6 +569,7 @@ declare module 'vue' {
readonly usePrevious: UnwrapRef<typeof import('@vueuse/core')['usePrevious']>
readonly useRafFn: UnwrapRef<typeof import('@vueuse/core')['useRafFn']>
readonly useRefHistory: UnwrapRef<typeof import('@vueuse/core')['useRefHistory']>
readonly useRegisterForceReload: UnwrapRef<typeof import('./src/hooks/useForceReload')['useRegisterForceReload']>
readonly useResizeObserver: UnwrapRef<typeof import('@vueuse/core')['useResizeObserver']>
readonly useRoute: UnwrapRef<typeof import('vue-router')['useRoute']>
readonly useRouter: UnwrapRef<typeof import('vue-router')['useRouter']>
Expand Down Expand Up @@ -725,6 +734,7 @@ declare module '@vue/runtime-core' {
readonly refDefault: UnwrapRef<typeof import('@vueuse/core')['refDefault']>
readonly refThrottled: UnwrapRef<typeof import('@vueuse/core')['refThrottled']>
readonly refWithControl: UnwrapRef<typeof import('@vueuse/core')['refWithControl']>
readonly reloadRouterViewInjectionKey: UnwrapRef<typeof import('./src/hooks/useForceReload')['reloadRouterViewInjectionKey']>
readonly render: UnwrapRef<typeof import('vue')['render']>
readonly resolveComponent: UnwrapRef<typeof import('vue')['resolveComponent']>
readonly resolveRef: UnwrapRef<typeof import('@vueuse/core')['resolveRef']>
Expand Down Expand Up @@ -758,6 +768,7 @@ declare module '@vue/runtime-core' {
readonly until: UnwrapRef<typeof import('@vueuse/core')['until']>
readonly useActiveElement: UnwrapRef<typeof import('@vueuse/core')['useActiveElement']>
readonly useAnimate: UnwrapRef<typeof import('@vueuse/core')['useAnimate']>
readonly useApplyForceReload: UnwrapRef<typeof import('./src/hooks/useForceReload')['useApplyForceReload']>
readonly useArrayDifference: UnwrapRef<typeof import('@vueuse/core')['useArrayDifference']>
readonly useArrayEvery: UnwrapRef<typeof import('@vueuse/core')['useArrayEvery']>
readonly useArrayFilter: UnwrapRef<typeof import('@vueuse/core')['useArrayFilter']>
Expand Down Expand Up @@ -871,6 +882,7 @@ declare module '@vue/runtime-core' {
readonly usePrevious: UnwrapRef<typeof import('@vueuse/core')['usePrevious']>
readonly useRafFn: UnwrapRef<typeof import('@vueuse/core')['useRafFn']>
readonly useRefHistory: UnwrapRef<typeof import('@vueuse/core')['useRefHistory']>
readonly useRegisterForceReload: UnwrapRef<typeof import('./src/hooks/useForceReload')['useRegisterForceReload']>
readonly useResizeObserver: UnwrapRef<typeof import('@vueuse/core')['useResizeObserver']>
readonly useRoute: UnwrapRef<typeof import('vue-router')['useRoute']>
readonly useRouter: UnwrapRef<typeof import('vue-router')['useRouter']>
Expand Down
3 changes: 2 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
</template>

<script lang="ts" setup>
import NaiveProvider from './NaiveProvider.vue'
import NaiveProvider from './provider/NaiveProvider.vue'
import { dateZhCN, zhCN } from 'naive-ui'
const { theme, themeOverrides } = useTheme()
Expand Down
14 changes: 9 additions & 5 deletions src/components/Layout/LayoutArea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
<div
class="inner-content"
>
<slot name="content"></slot>
<slot
v-if="isRouterAlive"
name="content"
></slot>
</div>
</main>
</section>
Expand All @@ -32,14 +35,15 @@
</div>
</template>

<script lang="ts">
<script lang="ts" setup>
/**
* 上下左右布局,顶部导航 + (底部左侧侧边栏 + 底部右侧内容区域)
*/
export default defineComponent({
name: 'LayoutArea'
})
const { isRouterAlive } = useRegisterForceReload()
</script>

<style lang="scss" scoped>
Expand Down
18 changes: 2 additions & 16 deletions src/components/Layout/LayoutView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,16 @@
</LayoutSection>
</template>
<template #content>
<router-view />
<Layout />
</template>
</LayoutArea>
</template>

<script lang="ts">
<script lang="ts" setup>
import NavigationSideGoBack from '@/components/Navigation/Side/SideGoBack.vue'
import NavigationNavBar from '@/components/Navigation/NavBar.vue'
export default defineComponent({
name: 'LayoutView',
components: {
NavigationNavBar,
NavigationSideGoBack
},
setup () {
const router = useRouter()
return {
}
}
})
</script>

<style lang="scss" scoped>
Expand Down
38 changes: 22 additions & 16 deletions src/components/Navigation/NavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,42 @@
<div style="flex: 1;"></div>
<template v-if="auth">
<NavigationSideAction />
<n-tooltip>
强制刷新路由
<template #trigger>
<div
class="h-full text-1.5em i-material-symbols:refresh"
flex="~ items-center"
cursor-pointer
@click="reloadRouterView()"
>
</div>
</template>
</n-tooltip>
<NavigationAvatar />
</template>
<NavigationChangeTheme />
</div>
</header>
</template>

<script lang="ts">
<script lang="ts" setup>
import NavigationSideAction from '@/components/Navigation/Side/SideAction.vue'
import NavigationAvatar from '@/components/Navigation/Avatar.vue'
import NavigationChangeTheme from '@/components/Navigation/ChangeTheme.vue'
export default defineComponent({
name: 'NavigationNavBar',
components: {
NavigationSideAction,
NavigationAvatar,
NavigationChangeTheme
defineProps({
auth: {
type: Boolean,
default: true
},
props: {
auth: {
type: Boolean,
default: true
},
fixed: {
type: Boolean,
default: true
}
fixed: {
type: Boolean,
default: true
}
})
const { reloadRouterView } = useApplyForceReload()
</script>
<style lang="scss" scoped>
$headerHeight: 48px;
Expand Down
41 changes: 41 additions & 0 deletions src/hooks/useForceReload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useChangeNotifyPlacement } from '@/provider/context'

/**
* 强制刷新 Layout View 路由
*/
export type ReloadRouterViewApiInjection = () => void
export const reloadRouterViewInjectionKey: InjectionKey<ReloadRouterViewApiInjection> = Symbol()

export const useRegisterForceReload = () => {
const isRouterAlive = ref(true)

const { changeNotifyPlacement } = useChangeNotifyPlacement()
const reloadRouterView = () => {
isRouterAlive.value = false
nextTick(() => {
changeNotifyPlacement('bottom-right')
window.$ModalNotification.destroyAll()
window.$ModalNotification.success({
title: '页面刷新成功',
duration: 800,
closable: true
})
isRouterAlive.value = true
})
}

provide(reloadRouterViewInjectionKey, reloadRouterView)

return {
isRouterAlive,
reloadRouterView
}
}

export const useApplyForceReload = () => {
const reloadRouterView = inject(reloadRouterViewInjectionKey)!

return {
reloadRouterView
}
}
24 changes: 15 additions & 9 deletions src/NaiveProvider.vue → src/provider/NaiveProvider.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<script setup lang="ts">
import { DrawerProvider, useDrawer } from '@/components/Drawer'
import {
DrawerProvider,
useDrawer
} from '@/components/Drawer'
import { useNotifyPlacement } from './context'
function registerNaiveTools () {
window.$ModalMessage = useMessage()
Expand All @@ -12,23 +15,26 @@ function registerNaiveTools () {
const NaiveProviderWrapper = defineComponent({
name: 'NaiveProviderWrapper',
setup() {
setup(props, { slots }) {
registerNaiveTools()
},
render() {
return h('div')
return () => h(slots.default!)
}
})
const { notifyPlacement } = useNotifyPlacement()
</script>

<template>
<DrawerProvider>
<NLoadingBarProvider>
<NDialogProvider>
<NNotificationProvider>
<NNotificationProvider
:placement="notifyPlacement"
>
<NMessageProvider>
<slot></slot>
<NaiveProviderWrapper />
<NaiveProviderWrapper>
<slot></slot>
</NaiveProviderWrapper>
</NMessageProvider>
</NNotificationProvider>
</NDialogProvider>
Expand Down
25 changes: 25 additions & 0 deletions src/provider/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { InjectionKey } from 'vue'

type NotifyPlacementInjection = (placement: NotificationPlacement) => void

export const notifyPlacementInjectionKey: InjectionKey<NotifyPlacementInjection> = Symbol()

export const useNotifyPlacement = () => {
const placement = ref<NotificationPlacement>('top-right')

const emitChangeNotifyPlacement = (pt: NotificationPlacement) => {
placement.value = pt
}
provide(notifyPlacementInjectionKey, emitChangeNotifyPlacement)

return {
notifyPlacement: placement
}
}

export const useChangeNotifyPlacement = () => {
const changeNotifyPlacement = inject(notifyPlacementInjectionKey)!
return {
changeNotifyPlacement
}
}
32 changes: 17 additions & 15 deletions src/router/child-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,30 @@ const childrenRoutes: Array<RouteRecordRaw> = [
path: '/result',
redirect: {
name: 'ProjectList'
}
},
{
path: '/result/:projectId',
component: LayoutView,
name: 'result',
redirect: {
name: 'ResultOverview'
},
children: [
{
path: 'overview',
name: 'ResultOverview',
component: () => import('@/modules/Result/pages/overview.vue'),
meta: {
title: '总览'
path: ':projectId',
component: LayoutView,
name: 'result',
redirect: {
name: 'ResultOverview'
},
children: [
{
path: '',
path: 'overview',
name: 'ResultOverview',
component: () => import('@/modules/Result/pages/overview.vue')
component: () => import('@/modules/Result/pages/overview.vue'),
meta: {
title: '总览'
},
children: [
{
path: '',
name: 'ResultOverview',
component: () => import('@/modules/Result/pages/overview.vue')
}
]
}
]
}
Expand Down
Loading

0 comments on commit 7220043

Please sign in to comment.