import type { ObjectDirective } from 'vue' import { resizeColumn } from '../utils/column-width' import '../assets/style/resizable-columns.css' const states = new WeakMap void; signature: () => boolean }>() function mount(table: HTMLTableElement) { const view = table.ownerDocument.defaultView! const headers = Array.from(table.querySelectorAll('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 = { 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) } }