补充部分注释

This commit is contained in:
ray_wuhao 2023-04-07 13:44:32 +08:00
parent 6fcbf918f1
commit 2d360f392e
3 changed files with 64 additions and 12 deletions

View File

@ -288,16 +288,21 @@ const RayChart = defineComponent({
* 使, `legend` * 使, `legend`
*/ */
const renderChart = (theme: ChartTheme) => { const renderChart = (theme: ChartTheme) => {
/** 获取 dom 容器 */
const element = rayChartRef.value as HTMLElement const element = rayChartRef.value as HTMLElement
/** 获取配置项 */
const options = useMergeOptions() const options = useMergeOptions()
/** 获取 dom 容器实际宽高 */
const { height, width } = element.getBoundingClientRect() const { height, width } = element.getBoundingClientRect()
/** 如果高度为 0, 则以 200px 填充 */
if (height === 0) { if (height === 0) {
addStyle(element, { addStyle(element, {
height: '200px', height: '200px',
}) })
} }
/** 如果款度为 0, 则以 200px 填充 */
if (width === 0) { if (width === 0) {
addStyle(element, { addStyle(element, {
width: '200px', width: '200px',
@ -305,13 +310,17 @@ const RayChart = defineComponent({
} }
try { try {
/** 注册 chart */
echartInstance = echarts.init(element, theme) echartInstance = echarts.init(element, theme)
echartInstanceRef.value = echartInstance echartInstanceRef.value = echartInstance
/** 设置 options 配置项 */
options && echartInstance.setOption(options) options && echartInstance.setOption(options)
/** 渲染成功回调 */
props.success?.(echartInstance) props.success?.(echartInstance)
} catch (e) { } catch (e) {
/** 渲染失败回调 */
props.error?.() props.error?.()
console.error(e) console.error(e)
@ -321,7 +330,6 @@ const RayChart = defineComponent({
/** /**
* *
* @param bool * @param bool
* @returns void 0
* *
* *
*/ */
@ -348,15 +356,23 @@ const RayChart = defineComponent({
} }
} }
/** 重置 echarts 尺寸 */
const resizeChart = () => { const resizeChart = () => {
if (echartInstance) { if (echartInstance) {
echartInstance.resize() echartInstance.resize()
} }
} }
/** 监听全局主题变化, 然后重新渲染对应主题 echarts */
watch( watch(
() => [themeValue.value], () => [themeValue.value],
([theme]) => { ([theme]) => {
/**
*
* Q: 为什么需要重新卸载再渲染
* A: 因为 echarts
* A: 虽然原型上有 setTheme , ECharts 访
*/
if (props.autoChangeTheme) { if (props.autoChangeTheme) {
destroyChart() destroyChart()
@ -384,29 +400,35 @@ const RayChart = defineComponent({
}, },
) )
/** 监听 options 变化 */
if (props.watchOptions) { if (props.watchOptions) {
watch( watch(
() => props.watchOptions, () => props.watchOptions,
() => { () => {
/** 重新组合 options */
const options = useMergeOptions() const options = useMergeOptions()
/** 如果 options 发生变动更新 echarts */
echartInstance?.setOption(options) echartInstance?.setOption(options)
}, },
) )
} }
onBeforeMount(async () => { onBeforeMount(async () => {
/** 注册 echarts 组件与渲染器 */
await registerChartCore() await registerChartCore()
}) })
onMounted(() => { onMounted(() => {
nextTick(() => { nextTick(() => {
/** 注册 echarts */
if (props.autoChangeTheme) { if (props.autoChangeTheme) {
renderThemeChart(themeValue.value) renderThemeChart(themeValue.value)
} else { } else {
props.theme ? renderChart('dark') : renderChart('') props.theme ? renderChart('dark') : renderChart('')
} }
/** 注册事件 */
if (props.autoResize) { if (props.autoResize) {
resizeDebounce = debounce(resizeChart, 500) resizeDebounce = debounce(resizeChart, 500)
@ -416,7 +438,9 @@ const RayChart = defineComponent({
}) })
onBeforeUnmount(() => { onBeforeUnmount(() => {
/** 卸载 echarts */
destroyChart() destroyChart()
/** 卸载事件柄 */
off(window, 'resize', resizeDebounce) off(window, 'resize', resizeDebounce)
}) })

View File

@ -110,3 +110,20 @@ export const matchMenuOption = (
} }
} }
} }
/**
*
* @param option menu option
*
* @remark
* @remark sideBarLogo.title
*/
export const updateDocumentTitle = (option: IMenuOptions) => {
const { breadcrumbLabel } = option
const {
layout: { sideBarLogo },
} = __APP_CFG__
const spliceTitle = sideBarLogo ? sideBarLogo.title : ''
document.title = breadcrumbLabel + ' - ' + spliceTitle
}

View File

@ -16,6 +16,10 @@
* : * :
* - BreadcrumbMenuTagMenuMenu * - BreadcrumbMenuTagMenuMenu
* - BreadcrumbMenuTagMenuMenu vue-router routers, * - BreadcrumbMenuTagMenuMenu vue-router routers,
*
* (sessionStorage):
* - breadcrumbOptions
* - menuKey
*/ */
import { NEllipsis } from 'naive-ui' import { NEllipsis } from 'naive-ui'
@ -23,7 +27,7 @@ import RayIcon from '@/components/RayIcon/index'
import { getCache, setCache } from '@/utils/cache' import { getCache, setCache } from '@/utils/cache'
import { validRole } from '@/router/basic' import { validRole } from '@/router/basic'
import { parse, matchMenuOption } from './helper' import { parse, matchMenuOption, updateDocumentTitle } from './helper'
import type { MenuOption } from 'naive-ui' import type { MenuOption } from 'naive-ui'
import type { RouteMeta } from 'vue-router' import type { RouteMeta } from 'vue-router'
@ -87,21 +91,26 @@ export const useMenu = defineStore(
menuState.menuKey, menuState.menuKey,
menuState.menuTagOptions, menuState.menuTagOptions,
) )
updateDocumentTitle(item as unknown as IMenuOptions)
menuState.breadcrumbOptions = parse(menuState.options, 'key', key) // 获取面包屑 menuState.breadcrumbOptions = parse(menuState.options, 'key', key) // 获取面包屑
/** 是否为根路由 */
if (key[0] !== '/') { if (key[0] !== '/') {
/** 如果不是根路由, 则拼接完整路由并跳转 */
const path = getCompleteRoutePath(menuState.options, key) const path = getCompleteRoutePath(menuState.options, key)
.map((curr) => curr.key) .map((curr) => curr.key)
.join('/') .join('/')
router.push(path) router.push(path)
} else { } else {
/** 根路由直接跳转 */
router.push(item.path as string) router.push(item.path as string)
} }
menuState.menuKey = key menuState.menuKey = key
/** 缓存菜单 key(sessionStorage) */
setCache('menuKey', key) setCache('menuKey', key)
} }
} }
@ -111,7 +120,8 @@ export const useMenu = defineStore(
* *
* @param path * @param path
* *
* * @remark
* @remark
*/ */
const updateMenuKeyWhenRouteUpdate = (path: string) => { const updateMenuKeyWhenRouteUpdate = (path: string) => {
const matchMenuItem = (options: MenuOption[]) => { const matchMenuItem = (options: MenuOption[]) => {
@ -141,16 +151,11 @@ export const useMenu = defineStore(
isAppend = true, isAppend = true,
) => { ) => {
const isArray = Array.isArray(optins) const isArray = Array.isArray(optins)
const arr = isArray ? [...optins] : [optins]
if (isAppend) { isAppend
isArray ? menuState.menuTagOptions.push(...arr)
? menuState.menuTagOptions.push(...optins) : (menuState.menuTagOptions = arr)
: menuState.menuTagOptions.push(optins)
} else {
isArray
? (menuState.menuTagOptions = optins)
: (menuState.menuTagOptions = [optins])
}
} }
/** /**
@ -159,6 +164,7 @@ export const useMenu = defineStore(
* @remark , * @remark ,
*/ */
const setupAppRoutes = () => { const setupAppRoutes = () => {
/** 取出所有 layout 下子路由 */
const layout = router.getRoutes().find((route) => route.name === 'layout') const layout = router.getRoutes().find((route) => route.name === 'layout')
const resolveRoutes = (routes: IMenuOptions[], index: number) => { const resolveRoutes = (routes: IMenuOptions[], index: number) => {
@ -168,6 +174,7 @@ export const useMenu = defineStore(
} }
const { meta } = curr const { meta } = curr
/** 设置 label, i18nKey 优先级最高 */
const label = computed(() => const label = computed(() =>
meta?.i18nKey meta?.i18nKey
? t(`GlobalMenuOptions.${meta!.i18nKey}`) ? t(`GlobalMenuOptions.${meta!.i18nKey}`)
@ -203,7 +210,10 @@ export const useMenu = defineStore(
: route : route
if (curr.path === cacheMenuKey) { if (curr.path === cacheMenuKey) {
/** 设置菜单标签 */
setMenuTagOptions(attr) setMenuTagOptions(attr)
/** 设置浏览器标题 */
updateDocumentTitle(attr)
} }
attr.show = validRole(curr) attr.show = validRole(curr)
@ -212,6 +222,7 @@ export const useMenu = defineStore(
}) })
} }
/** 缓存菜单列表 */
menuState.options = resolveRoutes(layout?.children as IMenuOptions[], 0) menuState.options = resolveRoutes(layout?.children as IMenuOptions[], 0)
/** 初始化后渲染面包屑 */ /** 初始化后渲染面包屑 */