This commit is contained in:
wuzilong committed 2026-10-05 12:57:51 +08:00
1 parent 59fafb92d5
commit 6abfb6e251
1512 files changed
+1565614 -275592

No files matched your search

@@ -16,7 +16,7 @@
</template>
<script setup lang="ts">
import { computed, nextTick, onMounted, ref, unref, useAttrs, watch } from 'vue'
import { computed, nextTick, onActivated, onDeactivated, onMounted, ref, unref, useAttrs, watch } from 'vue'
import {
isFixedNarrowListColumnKey,
resolveListColumnKey,
@@ -72,6 +72,7 @@
})
let applyTimer: ReturnType<typeof setTimeout> | null = null
let mountApplyTimer: ReturnType<typeof setTimeout> | null = null
const scheduleApply = () => {
if (!props.persistColumnWidth) return
@@ -84,6 +85,17 @@
}, 16)
}
const clearApplyTimers = () => {
if (applyTimer) {
clearTimeout(applyTimer)
applyTimer = null
}
if (mountApplyTimer) {
clearTimeout(mountApplyTimer)
mountApplyTimer = null
}
}
const handleHeaderDragEnd = (
newWidth: number,
oldWidth: number,
@@ -106,7 +118,15 @@
onMounted(() => {
scheduleApply()
// 首屏列尚未完全注册时再补一次
setTimeout(() => scheduleApply(), 100)
mountApplyTimer = setTimeout(() => scheduleApply(), 100)
})
onDeactivated(() => {
clearApplyTimers()
})
onActivated(() => {
scheduleApply()
})
watch(
@@ -0,0 +1,71 @@
<!-- 工具栏溢出项默认渲染:单按钮或带下级的下拉 -->
<template>
<el-dropdown v-if="item.children?.length" trigger="click" @command="handleChildCommand">
<el-button
:class="buttonClassList"
:type="item.type"
:disabled="item.disabled"
:loading="item.loading"
>
<el-icon v-if="item.icon"><component :is="item.icon" /></el-icon>
{{ item.label }}
<el-icon class="el-icon--right"><ArrowDown /></el-icon>
</el-button>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item
v-for="child in item.children"
:key="child.key"
:command="child.key"
:disabled="!!child.disabled"
:divided="!!child.divided"
>
{{ child.label }}
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
<el-button
v-else
:class="buttonClassList"
:type="item.type"
:disabled="item.disabled"
:loading="item.loading"
@click="item.onClick?.()"
>
<el-icon v-if="item.icon"><component :is="item.icon" /></el-icon>
{{ item.label }}
</el-button>
</template>
<script setup lang="ts">
import { computed } from 'vue'
import { ArrowDown } from '@element-plus/icons-vue'
import type { ListToolbarOverflowItem } from './types'
defineOptions({ name: 'ToolbarOverflowAction' })
const props = defineProps<{
item: ListToolbarOverflowItem
}>()
const buttonClassList = computed(() => {
const classes: string[] = []
if (props.item.ghost !== false && props.item.type !== 'primary') {
classes.push('btn-ghost')
}
if (props.item.type === 'primary') {
classes.push('btn-main')
}
if (props.item.buttonClass) {
classes.push(props.item.buttonClass)
}
return classes
})
function handleChildCommand(command: string) {
const child = props.item.children?.find((c) => c.key === command)
child?.onClick()
}
</script>
@@ -0,0 +1,263 @@
<!-- 列表工具栏溢出收纳:超出宽度的操作收入「更多」下拉 -->
<template>
<div ref="rootRef" class="list-toolbar-overflow">
<!-- 离屏测量:与可见按钮同结构,不参与布局 -->
<div ref="measureRef" class="list-toolbar-overflow__measure" aria-hidden="true">
<div
v-for="item in activeItems"
:key="`m-${item.key}`"
class="list-toolbar-overflow__measure-item"
>
<slot :name="item.key" :item="item" mode="measure">
<ToolbarOverflowAction :item="item" />
</slot>
</div>
<div class="list-toolbar-overflow__measure-more">
<el-button class="btn-ghost">
<el-icon><MoreFilled /></el-icon>
更多
<el-icon class="el-icon--right"><ArrowDown /></el-icon>
</el-button>
</div>
</div>
<div class="list-toolbar-overflow__bar">
<div v-for="item in visibleItems" :key="item.key" class="list-toolbar-overflow__item">
<slot :name="item.key" :item="item" mode="button">
<ToolbarOverflowAction :item="item" />
</slot>
</div>
<el-dropdown
v-if="overflowItems.length"
trigger="click"
popper-class="list-toolbar-overflow-more-popper"
@command="handleMoreCommand"
>
<el-button class="btn-ghost list-toolbar-overflow__more-btn">
<el-icon><MoreFilled /></el-icon>
更多
<el-icon class="el-icon--right"><ArrowDown /></el-icon>
</el-button>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item
v-for="item in overflowMenuNodes"
:key="item.nodeKey"
:command="item.kind === 'action' ? item.command : undefined"
:disabled="item.kind === 'group' || item.disabled"
:divided="item.divided"
>
<span
v-if="item.kind === 'action' && item.icon"
class="list-toolbar-overflow__menu-item"
>
<el-icon><component :is="item.icon" /></el-icon>
{{ item.label }}
</span>
<template v-else>{{ item.label }}</template>
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
</div>
</template>
<script setup lang="ts">
import { computed, nextTick, ref, watch } from 'vue'
import { useResizeObserver } from '@vueuse/core'
import { ArrowDown, MoreFilled } from '@element-plus/icons-vue'
import {
calcListToolbarOverflowVisibleCount,
LIST_TOOLBAR_OVERFLOW_GAP
} from '@/utils/list-toolbar-overflow'
import ToolbarOverflowAction from './ToolbarOverflowAction.vue'
import type { ListToolbarOverflowItem } from './types'
import type { Component } from 'vue'
defineOptions({ name: 'ListToolbarOverflow' })
type OverflowMenuNode =
| { kind: 'group'; nodeKey: string; label: string; divided: boolean }
| {
kind: 'action'
nodeKey: string
command: string
label: string
disabled: boolean
divided: boolean
icon?: Component
}
const props = withDefaults(
defineProps<{
items: ListToolbarOverflowItem[]
gap?: number
}>(),
{
gap: LIST_TOOLBAR_OVERFLOW_GAP
}
)
const rootRef = ref<HTMLElement | null>(null)
const measureRef = ref<HTMLElement | null>(null)
const visibleCount = ref(0)
const activeItems = computed(() => props.items.filter((item) => !item.hidden))
const visibleItems = computed(() => activeItems.value.slice(0, visibleCount.value))
const overflowItems = computed(() => activeItems.value.slice(visibleCount.value))
const overflowMenuNodes = computed<OverflowMenuNode[]>(() => {
const nodes: OverflowMenuNode[] = []
overflowItems.value.forEach((item, index) => {
if (item.children?.length) {
nodes.push({
kind: 'group',
nodeKey: `g-${item.key}`,
label: item.label,
divided: index > 0
})
item.children.forEach((child) => {
nodes.push({
kind: 'action',
nodeKey: `${item.key}::${child.key}`,
command: `${item.key}::${child.key}`,
label: child.label,
disabled: !!child.disabled,
divided: !!child.divided
})
})
return
}
nodes.push({
kind: 'action',
nodeKey: item.key,
command: item.key,
label: item.label,
disabled: !!item.disabled || !!item.loading,
divided: index > 0 && !!item.divided,
icon: item.icon
})
})
return nodes
})
const itemsSignature = computed(() =>
activeItems.value
.map(
(item) =>
`${item.key}:${item.label}:${item.disabled ? 1 : 0}:${item.loading ? 1 : 0}:${item.children?.map((c) => `${c.key}:${c.disabled ? 1 : 0}:${c.label}`).join(',') || ''}`
)
.join('|')
)
function recomputeVisibleCount() {
const root = rootRef.value
const measure = measureRef.value
const items = activeItems.value
if (!root || !measure || !items.length) {
visibleCount.value = items.length ? items.length : 0
return
}
const availableWidth = root.clientWidth
const itemEls = measure.querySelectorAll<HTMLElement>('.list-toolbar-overflow__measure-item')
const moreEl = measure.querySelector<HTMLElement>('.list-toolbar-overflow__measure-more')
if (!itemEls.length || !moreEl) {
visibleCount.value = items.length
return
}
const itemWidths = Array.from(itemEls).map((el) => el.getBoundingClientRect().width)
const moreWidth = moreEl.getBoundingClientRect().width
visibleCount.value = calcListToolbarOverflowVisibleCount(
itemWidths,
moreWidth,
availableWidth,
props.gap
)
}
function handleMoreCommand(command: string | number | undefined) {
if (command === undefined || command === null || command === '') return
const cmd = String(command)
if (cmd.includes('::')) {
const [parentKey, childKey] = cmd.split('::')
const parent = activeItems.value.find((item) => item.key === parentKey)
const child = parent?.children?.find((c) => c.key === childKey)
child?.onClick()
return
}
const item = activeItems.value.find((i) => i.key === cmd)
if (!item || item.disabled || item.loading) return
item.onClick?.()
}
watch(
itemsSignature,
async () => {
await nextTick()
recomputeVisibleCount()
},
{ flush: 'post', immediate: true }
)
useResizeObserver(rootRef, () => {
recomputeVisibleCount()
})
useResizeObserver(measureRef, () => {
recomputeVisibleCount()
})
</script>
<style scoped lang="scss">
.list-toolbar-overflow {
position: relative;
display: flex;
flex: 1 1 auto;
align-items: center;
min-width: 0;
}
.list-toolbar-overflow__bar {
display: flex;
flex-wrap: nowrap;
align-items: center;
gap: 8px;
min-width: 0;
max-width: 100%;
overflow: hidden;
}
.list-toolbar-overflow__measure {
position: absolute;
top: 0;
left: 0;
z-index: -1;
display: flex;
flex-wrap: nowrap;
gap: 8px;
visibility: hidden;
pointer-events: none;
}
.list-toolbar-overflow__item,
.list-toolbar-overflow__measure-item,
.list-toolbar-overflow__measure-more {
flex-shrink: 0;
}
.list-toolbar-overflow__menu-item {
display: inline-flex;
align-items: center;
gap: 6px;
}
.list-toolbar-overflow__more-btn.el-button {
flex-shrink: 0;
}
</style>
@@ -0,0 +1,28 @@
import type { Component } from 'vue'
export type ListToolbarOverflowChild = {
key: string
label: string
disabled?: boolean
divided?: boolean
onClick: () => void
}
export type ListToolbarOverflowItem = {
key: string
label: string
icon?: Component
disabled?: boolean
loading?: boolean
/** 为 true 时不参与布局与测量 */
hidden?: boolean
ghost?: boolean
type?: 'default' | 'primary' | 'success' | 'warning' | 'danger' | 'info'
buttonClass?: string
/** 「更多」菜单中是否与前一项分隔 */
divided?: boolean
onClick?: () => void
children?: ListToolbarOverflowChild[]
}
export type ListToolbarOverflowSlotMode = 'button' | 'measure' | 'menu'