Skip to content

Commit

Permalink
fix: perfect hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
chansee97 committed Jun 4, 2024
1 parent 648a0ba commit 448f3ba
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 66 deletions.
6 changes: 3 additions & 3 deletions service.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/** 不同请求服务的环境配置 */
export const serviceConfig: Record<ServiceEnvType, Record<string, string>> = {
dev: {
url: 'https://mock.apifox.com/m1/4071143-0-default',
url: 'https://mock.apifox.cn/m1/4071143-0-default',
},
test: {
url: 'https://mock.apifox.com/m1/4071143-0-default',
url: 'https://mock.apifox.cn/m1/4071143-0-default',
},
prod: {
url: 'https://mock.apifox.com/m1/4071143-0-default',
url: 'https://mock.apifox.cn/m1/4071143-0-default',
},
}
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useBoolean'
export * from './useEcharts'
export * from './usePermission'
export * from './refForm'
15 changes: 15 additions & 0 deletions src/hooks/refForm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function refForm<T>(initValue: T) {
function jsonClone(value: T) {
return JSON.parse(JSON.stringify(value))
}
const target = ref<T>(initValue)

function setDefault() {
target.value = jsonClone(initValue)
}

return {
target,
setDefault,
}
}
53 changes: 17 additions & 36 deletions src/hooks/useEcharts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,73 +66,54 @@ echarts.use([

/**
* Echarts hooks函数
* @param options - 图表配置
* @description 按需引入图表组件,没注册的组件需要先引入
*/
export function useEcharts(options: Ref<ECOption>) {
export function useEcharts(el: Ref<HTMLElement | null>, chartOptions: Ref<ECOption>) {
const appStore = useAppStore()

const domRef = ref<HTMLElement>()

let chart: echarts.ECharts | null = null

const initialSize = { width: 0, height: 0 }
const { width, height } = useElementSize(domRef, initialSize)
const { width, height } = useElementSize(el)

function canRender() {
return initialSize.width > 0 && initialSize.height > 0
}
const isRendered = computed(() => Boolean(el && chart))

function isRendered() {
return Boolean(domRef.value && chart)
}
async function render() {
const chartTheme = appStore.colorMode ? 'dark' : 'light'
await nextTick()
if (domRef.value) {
chart = echarts.init(domRef.value, chartTheme)
update(options.value)
if (el) {
chart = echarts.init(el.value, chartTheme)
update(chartOptions.value)
}
}

function update(updateOptions: ECOption) {
if (isRendered())
if (isRendered.value)
chart!.setOption({ ...updateOptions, backgroundColor: 'transparent' })
}

function resize() {
chart?.resize()
}

function destroy() {
chart?.dispose()
chart = null
}
const sizeWatch = watch([width, height], async ([newWidth, newHeight]) => {
initialSize.width = newWidth
initialSize.height = newHeight
if (newWidth === 0 && newHeight === 0) {
// 节点被删除 将chart置为空
chart = null
}
if (!canRender())
return
if (isRendered())
resize()
else await render()

watch([width, height], async ([newWidth, newHeight]) => {
if (isRendered.value && newWidth && newHeight)
chart?.resize()
})

const OptionWatch = watch(options, (newValue) => {
watch(chartOptions, (newValue) => {
update(newValue)
})

onMounted(() => {
render()
})
onUnmounted(() => {
sizeWatch()
OptionWatch()
destroy()
})

return {
domRef,
destroy,
update,
}
}
4 changes: 3 additions & 1 deletion src/views/dashboard/monitor/components/chart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ const lineOptions = ref<ECOption>({
data: [20, 71, 8, 50, 57, 32],
}],
}) as Ref<ECOption>
const { domRef: lineRef } = useEcharts(lineOptions)
const lineRef = ref<HTMLElement | null>(null)
useEcharts(lineRef, lineOptions)
</script>

<template>
Expand Down
4 changes: 3 additions & 1 deletion src/views/dashboard/monitor/components/chart2.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ const option = ref<ECOption>({
},
}],
}) as Ref<ECOption>
const { domRef: lineRef } = useEcharts(option)
const lineRef = ref<HTMLElement | null>(null)
useEcharts(lineRef, option)
</script>

<template>
Expand Down
3 changes: 2 additions & 1 deletion src/views/dashboard/monitor/components/chart3.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const option = ref<ECOption>({
},
],
}) as Ref<ECOption>
const { domRef: lineRef } = useEcharts(option)
const lineRef = ref<HTMLElement | null>(null)
useEcharts(lineRef, option)
</script>

<template>
Expand Down
3 changes: 2 additions & 1 deletion src/views/dashboard/workbench/components/chart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ const lineOptions = ref<ECOption>({
},
],
}) as Ref<ECOption>
const { domRef: lineRef } = useEcharts(lineOptions)
const lineRef = ref<HTMLElement | null>(null)
useEcharts(lineRef, lineOptions)
</script>

<template>
Expand Down
13 changes: 9 additions & 4 deletions src/views/demo/echarts/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const pieOptions = ref<ECOption>({
},
],
}) as Ref<ECOption>
const { domRef: pieRef } = useEcharts(pieOptions)
const pieRef = ref<HTMLElement | null>(null)
useEcharts(pieRef, pieOptions)
// 折线图
const lineOptions = ref<ECOption>({
Expand Down Expand Up @@ -245,7 +246,8 @@ const lineOptions = ref<ECOption>({
},
],
}) as Ref<ECOption>
const { domRef: lineRef } = useEcharts(lineOptions)
const lineRef = ref<HTMLElement | null>(null)
useEcharts(lineRef, lineOptions)
// 柱状图
const barOptions = ref<ECOption>({
Expand Down Expand Up @@ -385,7 +387,9 @@ const barOptions = ref<ECOption>({
},
],
}) as Ref<ECOption>
const { domRef: barRef } = useEcharts(barOptions)
const barRef = ref<HTMLElement | null>(null)
useEcharts(barRef, barOptions)
// 雷达图
const radarOptions = ref<ECOption>({
title: {
Expand Down Expand Up @@ -477,7 +481,8 @@ const radarOptions = ref<ECOption>({
},
],
}) as Ref<ECOption>
const { domRef: radarRef } = useEcharts(radarOptions)
const radarRef = ref<HTMLElement | null>(null)
useEcharts(radarRef, radarOptions)
</script>

<template>
Expand Down
19 changes: 10 additions & 9 deletions src/views/setting/account/components/TableModal.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { useBoolean } from '@/hooks'
import { refForm, useBoolean } from '@/hooks'
import { fetchRoleList } from '@/service'
interface Props {
Expand All @@ -19,15 +19,14 @@ const { bool: modalVisible, setTrue: showModal, setFalse: hiddenModal } = useBoo
const { bool: submitLoading, setTrue: startLoading, setFalse: endLoading } = useBoolean(false)
const formModel = ref()
const defaultFormModal: Entity.User = {
const { target: formModel, setDefault } = refForm<Entity.User>({
userName: '',
gender: undefined,
email: '',
tel: '',
role: [],
status: 1,
}
})
type ModalType = 'add' | 'view' | 'edit'
const modalType = shallowRef<ModalType>('add')
Expand All @@ -47,15 +46,17 @@ async function openModal(type: ModalType = 'add', data: any) {
getRoleList()
const handlers = {
async add() {
formModel.value = { ...defaultFormModal }
setDefault()
},
async view() {
if (data)
formModel.value = { ...data }
if (!data)
return
formModel.value = { ...data }
},
async edit() {
if (data)
formModel.value = { ...data }
if (!data)
return
formModel.value = { ...data }
},
}
await handlers[type]()
Expand Down
2 changes: 1 addition & 1 deletion src/views/setting/account/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const treeData = ref([

<template>
<n-flex>
<n-card class="w-60">
<n-card class="w-70">
<n-tree
block-line
:data="treeData"
Expand Down
19 changes: 10 additions & 9 deletions src/views/setting/menu/components/TableModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
FormItemRule,
} from 'naive-ui'
import HelpInfo from '@/components/common/HelpInfo.vue'
import { useBoolean } from '@/hooks'
import { refForm, useBoolean } from '@/hooks'
import { Regex } from '@/constants'
import { fetchRoleList } from '@/service'
Expand All @@ -24,7 +24,7 @@ const emit = defineEmits<{
const { bool: modalVisible, setTrue: showModal, setFalse: hiddenModal } = useBoolean(false)
const { bool: submitLoading, setTrue: startLoading, setFalse: endLoading } = useBoolean(false)
const defaultFormModal: AppRoute.RowRoute = {
const { target: formModel, setDefault } = refForm<AppRoute.RowRoute>({
'name': '',
'path': '',
'id': -1,
Expand All @@ -41,8 +41,7 @@ const defaultFormModal: AppRoute.RowRoute = {
'meta.withoutTab': true,
'meta.pinTab': false,
'meta.menuType': 'page',
}
const formModel = ref()
})
type ModalType = 'add' | 'view' | 'edit'
const modalType = shallowRef<ModalType>('add')
Expand All @@ -62,15 +61,17 @@ async function openModal(type: ModalType = 'add', data: AppRoute.RowRoute) {
showModal()
const handlers = {
async add() {
formModel.value = { ...defaultFormModal }
setDefault()
},
async view() {
if (data)
formModel.value = { ...data }
if (!data)
return
formModel.value = { ...data }
},
async edit() {
if (data)
formModel.value = { ...data }
if (!data)
return
formModel.value = { ...data }
},
}
await handlers[type]()
Expand Down

0 comments on commit 448f3ba

Please sign in to comment.