2023-03-30 23:35:34 +02:00
|
|
|
<script setup lang="ts">
|
|
|
|
import { useStorage } from '@vueuse/core';
|
|
|
|
import { useThemeVars } from 'naive-ui';
|
|
|
|
import { RouterLink, useRoute } from 'vue-router';
|
|
|
|
import MenuIconItem from './MenuIconItem.vue';
|
2023-05-28 23:13:24 +02:00
|
|
|
import type { Tool, ToolCategory } from '@/tools/tools.types';
|
2023-03-30 23:35:34 +02:00
|
|
|
|
|
|
|
const props = withDefaults(defineProps<{ toolsByCategory?: ToolCategory[] }>(), { toolsByCategory: () => [] });
|
|
|
|
const { toolsByCategory } = toRefs(props);
|
|
|
|
const route = useRoute();
|
|
|
|
|
|
|
|
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: {
|
2023-05-28 23:13:24 +02:00
|
|
|
read: v => (v ? JSON.parse(v) : null),
|
|
|
|
write: v => JSON.stringify(v),
|
2023-03-30 23:35:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
function toggleCategoryCollapse({ name }: { name: string }) {
|
|
|
|
collapsedCategories.value[name] = !collapsedCategories.value[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
const menuOptions = computed(() =>
|
|
|
|
toolsByCategory.value.map(({ name, components }) => ({
|
2023-05-28 23:13:24 +02:00
|
|
|
name,
|
2023-03-30 23:35:34 +02:00
|
|
|
isCollapsed: collapsedCategories.value[name],
|
2023-05-28 23:13:24 +02:00
|
|
|
tools: components.map(tool => ({
|
2023-03-30 23:35:34 +02:00
|
|
|
label: makeLabel(tool),
|
|
|
|
icon: makeIcon(tool),
|
|
|
|
key: tool.name,
|
|
|
|
})),
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
|
|
|
|
const themeVars = useThemeVars();
|
|
|
|
</script>
|
|
|
|
|
2023-05-28 23:13:24 +02:00
|
|
|
<template>
|
|
|
|
<div v-for="{ name, tools, isCollapsed } of menuOptions" :key="name">
|
2023-06-25 15:49:43 +02:00
|
|
|
<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>
|
2023-05-28 23:13:24 +02:00
|
|
|
|
2023-06-25 15:49:43 +02:00
|
|
|
<span ml-8px text-13px>
|
2023-05-28 23:13:24 +02:00
|
|
|
{{ name }}
|
|
|
|
</span>
|
2023-06-25 15:49:43 +02:00
|
|
|
</div>
|
2023-05-28 23:13:24 +02:00
|
|
|
|
|
|
|
<n-collapse-transition :show="!isCollapsed">
|
|
|
|
<div class="menu-wrapper">
|
|
|
|
<div class="toggle-bar" @click="toggleCategoryCollapse({ name })" />
|
|
|
|
|
|
|
|
<n-menu
|
|
|
|
class="menu"
|
|
|
|
:value="route.name as string"
|
|
|
|
:collapsed-width="64"
|
|
|
|
:collapsed-icon-size="22"
|
|
|
|
:options="tools"
|
|
|
|
:indent="8"
|
|
|
|
:default-expand-all="true"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</n-collapse-transition>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2023-03-30 23:35:34 +02:00
|
|
|
<style scoped lang="less">
|
|
|
|
.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 {
|
2023-03-30 23:35:34 +02:00
|
|
|
width: 24px;
|
2023-03-30 23:35:34 +02:00
|
|
|
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;
|
2023-03-30 23:35:34 +02:00
|
|
|
left: 14px;
|
2023-03-30 23:35:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
opacity: 0.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|