Files
FileCodeBoxFronted2026/src/directives/resizable-columns.ts
T
orion 4fefcd0f0a ✨ feat: 完善文件状态、取件记录与表格交互
* 支持当前页下载状态的手动和定时检测,仅显示标签与检测时间
* 区分查件时间和下载点击时间,保留重复查件前的取件时间
* 将查件事件按时间并入现有取件记录,共用展开与收起
* 统一表头居中,支持拖拽和键盘调整列宽并限制最小宽度
* 修复筛选面板收起结束时顶部间距突然消失的顿挫
* 补充单元、集成与浏览器验证,覆盖查询下载、列宽和动画行为
2026-09-19 00:35:55 +08:00

115 lines
4.5 KiB
TypeScript

import type { ObjectDirective } from 'vue'
import { resizeColumn } from '../utils/column-width'
import '../assets/style/resizable-columns.css'
const states = new WeakMap<HTMLTableElement, { cleanup: () => void; signature: () => boolean }>()
function mount(table: HTMLTableElement) {
const view = table.ownerDocument.defaultView!
const headers = Array.from(table.querySelectorAll<HTMLTableCellElement>('thead th'))
const labels = headers.map(header => header.textContent?.trim() || '')
const handles: HTMLElement[] = []
let stopDrag = () => {}
table.classList.add('resizable-columns')
function measure() {
return headers.map(header => header.getBoundingClientRect().width)
}
function minimum(header: HTMLTableCellElement) {
const range = table.ownerDocument.createRange()
range.selectNodeContents(header)
// Keep the label and padding visible even when the data column is narrowed.
const labelWidth = Array.from(range.getClientRects()).reduce((max, rect) => Math.max(max, rect.width), 0)
const style = view.getComputedStyle(header)
return Math.ceil(Math.max(72, labelWidth + parseFloat(style.paddingLeft) + parseFloat(style.paddingRight)))
}
function apply(widths: number[]) {
table.style.tableLayout = 'fixed'
table.style.minWidth = '0'
table.style.width = `${widths.reduce((sum, width) => sum + width, 0)}px`
table.classList.add('columns-sized')
headers.forEach((header, index) => {
header.style.width = `${widths[index]}px`
handles[index]!.setAttribute('aria-valuenow', String(Math.round(widths[index]!)))
})
}
headers.forEach((header, index) => {
const handle = table.ownerDocument.createElement('span')
handle.className = 'column-resize-handle'
handle.tabIndex = 0
handle.setAttribute('role', 'separator')
handle.setAttribute('aria-orientation', 'vertical')
header.setAttribute('aria-label', labels[index]!)
handle.setAttribute('aria-label', `${labels[index]} — ↔`)
handle.setAttribute('aria-valuenow', String(Math.round(header.getBoundingClientRect().width)))
header.append(handle)
handles.push(handle)
handle.onpointerdown = event => {
if (event.button !== 0) return
event.preventDefault()
event.stopPropagation()
stopDrag()
const widths = measure()
const min = minimum(header)
const startX = event.clientX
const pointerId = event.pointerId
handle.setPointerCapture(pointerId)
table.classList.add('columns-resizing')
const move = (event: PointerEvent) => {
if (event.pointerId === pointerId) apply(resizeColumn(widths, index, event.clientX - startX, min))
}
const finish = (event: PointerEvent) => {
if (event.pointerId === pointerId) stopDrag()
}
stopDrag = () => {
view.removeEventListener('pointermove', move)
view.removeEventListener('pointerup', finish)
view.removeEventListener('pointercancel', finish)
view.removeEventListener('blur', stopDrag)
handle.removeEventListener('lostpointercapture', finish)
if (handle.hasPointerCapture(pointerId)) handle.releasePointerCapture(pointerId)
table.classList.remove('columns-resizing')
stopDrag = () => {}
}
view.addEventListener('pointermove', move)
view.addEventListener('pointerup', finish)
view.addEventListener('pointercancel', finish)
view.addEventListener('blur', stopDrag)
handle.addEventListener('lostpointercapture', finish)
}
handle.onkeydown = event => {
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return
event.preventDefault()
apply(resizeColumn(measure(), index, event.key === 'ArrowRight' ? 16 : -16, minimum(header)))
}
})
states.set(table, { signature: () => {
const current = Array.from(table.querySelectorAll('thead th'))
return current.length === headers.length && current.every((header, index) =>
header === headers[index] && header.textContent?.trim() === labels[index] && handles[index]?.isConnected)
}, cleanup: () => {
stopDrag()
handles.forEach(handle => handle.remove())
headers.forEach(header => header.removeAttribute('aria-label'))
} })
}
export const vResizableColumns: ObjectDirective<HTMLTableElement> = {
mounted: mount,
updated(table) {
const state = states.get(table)
if (!state?.signature()) {
state?.cleanup()
mount(table)
}
},
beforeUnmount(table) {
states.get(table)?.cleanup()
states.delete(table)
}
}