258 lines
6.5 KiB
Vue
258 lines
6.5 KiB
Vue
<script setup lang="ts">
|
|
import { useStorage } from '@vueuse/core';
|
|
import { useThemeVars } from 'naive-ui';
|
|
import { RouterLink, useRoute, useRouter } from 'vue-router';
|
|
import MenuIconItem from './MenuIconItem.vue';
|
|
import type { Tool, ToolCategory } from '@/tools/tools.types';
|
|
import { PROTECTED_TOOLS } from '@/tools/license/license';
|
|
|
|
const props = withDefaults(defineProps<{ toolsByCategory?: ToolCategory[]; favoriteCategory?: ToolCategory | null }>(), { toolsByCategory: () => [], favoriteCategory: null });
|
|
const { toolsByCategory } = toRefs(props);
|
|
const route = useRoute();
|
|
const router = useRouter();
|
|
|
|
/** 点击菜单项任意位置(图标/内边距/箭头)都跳转,避免只有点到文字才生效 */
|
|
function goTo(key: string | number) {
|
|
const path = String(key);
|
|
if (path && path !== route.path) {
|
|
router.push(path);
|
|
}
|
|
}
|
|
|
|
const makeLabel = (tool: Tool) => () => h(RouterLink, { to: tool.path }, { default: () => tool.name });
|
|
const makeIcon = (tool: Tool) => () => h(MenuIconItem, { tool });
|
|
|
|
const collapsedCategories = useStorage<Record<string, boolean>>(
|
|
'menu-tool-option:collapsed-categories',
|
|
{},
|
|
undefined,
|
|
{
|
|
deep: true,
|
|
serializer: {
|
|
read: v => (v ? JSON.parse(v) : null),
|
|
write: v => JSON.stringify(v),
|
|
},
|
|
},
|
|
);
|
|
|
|
function toggleCategoryCollapse({ name }: { name: string }) {
|
|
collapsedCategories.value[name] = !collapsedCategories.value[name];
|
|
}
|
|
|
|
function expandAll() {
|
|
const m: Record<string, boolean> = {};
|
|
for (const { name } of toolsByCategory.value) {
|
|
m[name] = false;
|
|
}
|
|
collapsedCategories.value = m;
|
|
}
|
|
|
|
function collapseAll() {
|
|
const m: Record<string, boolean> = {};
|
|
for (const { name } of toolsByCategory.value) {
|
|
m[name] = true;
|
|
}
|
|
collapsedCategories.value = m;
|
|
}
|
|
|
|
const PAID_GROUP_NAME = '授权应用';
|
|
|
|
const menuOptions = computed(() => {
|
|
// pull licensed tools out of their original categories (favorites stay intact)
|
|
const paidTools: Tool[] = [];
|
|
const paidPaths = new Set<string>();
|
|
const remaining = toolsByCategory.value
|
|
.map(({ name, components }) => ({
|
|
name,
|
|
components: components.filter((tool) => {
|
|
if (tool.path in PROTECTED_TOOLS) {
|
|
if (!paidPaths.has(tool.path)) {
|
|
paidPaths.add(tool.path);
|
|
paidTools.push(tool);
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}),
|
|
}))
|
|
.filter(({ components }) => components.length > 0);
|
|
|
|
const toOption = (name: string, components: Tool[]) => ({
|
|
name,
|
|
isCollapsed: collapsedCategories.value[name],
|
|
tools: components.map(tool => ({
|
|
label: makeLabel(tool),
|
|
icon: makeIcon(tool),
|
|
key: tool.path,
|
|
})),
|
|
});
|
|
|
|
const options = [];
|
|
// favorites first, shown as-is (user shortcuts must stay visible)
|
|
if (props.favoriteCategory && props.favoriteCategory.components.length > 0) {
|
|
options.push(toOption(props.favoriteCategory.name, props.favoriteCategory.components));
|
|
}
|
|
if (paidTools.length > 0) {
|
|
options.push(toOption(PAID_GROUP_NAME, paidTools));
|
|
}
|
|
options.push(...remaining.map(({ name, components }) => toOption(name, components)));
|
|
return options;
|
|
});
|
|
|
|
const themeVars = useThemeVars();
|
|
</script>
|
|
|
|
<template>
|
|
<div class="menu-actions">
|
|
<a href="javascript:;" class="menu-action" title="展开所有分类" @click="expandAll">⊕ 展开全部</a>
|
|
<span class="menu-action-sep" />
|
|
<a href="javascript:;" class="menu-action" title="收起所有分类" @click="collapseAll">⊖ 收起全部</a>
|
|
</div>
|
|
|
|
<div v-for="{ name, tools, isCollapsed } of menuOptions" :key="name">
|
|
<div ml-6px mt-12px flex cursor-pointer items-center op-60 @click="toggleCategoryCollapse({ name })">
|
|
<span :class="{ 'rotate-0': isCollapsed, 'rotate-90': !isCollapsed }" text-16px lh-1 op-50 transition-transform>
|
|
<icon-mdi-chevron-right />
|
|
</span>
|
|
|
|
<span ml-8px text-14.5px font-600>
|
|
{{ name }}
|
|
</span>
|
|
|
|
<span class="menu-count">{{ tools.length }}</span>
|
|
</div>
|
|
|
|
<n-collapse-transition :show="!isCollapsed">
|
|
<div class="menu-wrapper">
|
|
<div class="toggle-bar" @click="toggleCategoryCollapse({ name })" />
|
|
|
|
<n-menu
|
|
class="menu"
|
|
size="large"
|
|
:value="route.path"
|
|
:collapsed-width="64"
|
|
:collapsed-icon-size="26"
|
|
:options="tools"
|
|
:indent="10"
|
|
:default-expand-all="true"
|
|
@update:value="goTo"
|
|
/>
|
|
</div>
|
|
</n-collapse-transition>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped lang="less">
|
|
// 菜单项放大,适配屏幕
|
|
.menu {
|
|
--n-item-text-color: rgba(0, 0, 0, 0.82);
|
|
font-size: 15px;
|
|
|
|
:deep(.n-menu-item-content) {
|
|
font-size: 15px;
|
|
padding-top: 11px;
|
|
padding-bottom: 11px;
|
|
|
|
.n-menu-item-content__icon {
|
|
font-size: 21px !important;
|
|
width: 26px !important;
|
|
}
|
|
|
|
.n-menu-item-content__label {
|
|
font-size: 15px;
|
|
font-weight: 500;
|
|
}
|
|
}
|
|
|
|
:deep(.n-menu-item-content__arrow) {
|
|
font-size: 15px;
|
|
}
|
|
}
|
|
|
|
.menu-count {
|
|
margin-left: auto;
|
|
margin-right: 8px;
|
|
font-size: 10.5px;
|
|
line-height: 1;
|
|
padding: 3px 7px;
|
|
border-radius: 999px;
|
|
background: rgba(128, 128, 128, 0.12);
|
|
opacity: 0.75;
|
|
font-weight: 600;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.menu-actions {
|
|
// 滚动侧栏时吸顶固定,展开/收起全部始终可见。
|
|
// top 对齐 hero 固定区高度(160px,与 .sider-content padding-top 配套),避免被 z-index:10 的 hero 盖住
|
|
position: sticky;
|
|
top: 160px;
|
|
z-index: 5;
|
|
background: var(--n-color, #fff);
|
|
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
padding: 2px 6px 0 6px;
|
|
|
|
.menu-action {
|
|
font-size: 12px;
|
|
color: inherit;
|
|
opacity: 0.55;
|
|
text-decoration: none;
|
|
padding: 2px 4px;
|
|
border-radius: 4px;
|
|
transition: opacity 0.15s, color 0.15s;
|
|
|
|
&:hover {
|
|
opacity: 1;
|
|
color: #18a058;
|
|
}
|
|
}
|
|
|
|
.menu-action-sep {
|
|
width: 1px;
|
|
height: 11px;
|
|
background: currentColor;
|
|
opacity: 0.2;
|
|
}
|
|
}
|
|
|
|
.menu-wrapper {
|
|
display: flex;
|
|
flex-direction: row;
|
|
.menu {
|
|
flex: 1;
|
|
margin-bottom: 5px;
|
|
|
|
::v-deep(.n-menu-item-content::before) {
|
|
left: 0;
|
|
right: 13px;
|
|
}
|
|
}
|
|
|
|
.toggle-bar {
|
|
width: 24px;
|
|
opacity: 0.1;
|
|
transition: opacity ease 0.2s;
|
|
position: relative;
|
|
cursor: pointer;
|
|
|
|
&::before {
|
|
width: 2px;
|
|
height: 100%;
|
|
content: ' ';
|
|
background-color: v-bind('themeVars.textColor3');
|
|
border-radius: 2px;
|
|
position: absolute;
|
|
top: 0;
|
|
left: 14px;
|
|
}
|
|
|
|
&:hover {
|
|
opacity: 0.5;
|
|
}
|
|
}
|
|
}
|
|
</style>
|