317 lines
14 KiB
TypeScript
317 lines
14 KiB
TypeScript
/**
|
||
* 列宽拖拽的自动化探针(只在开发模式用,不进产物)。
|
||
*
|
||
* 拖宽是像素级 + 指针事件的事,截图看不出来「拖完之后这一列到底几像素」,
|
||
* 这一台把真的 DataTable 挂在固定宽度里,代码替人按一下把手,再把量到的数写进 <title>。
|
||
* 开法:`pnpm dev` 之后(Chrome 走系统代理的话要 `--no-proxy-server`,
|
||
* 否则 localhost 被代理接走,只回一个 502 的错误页)
|
||
* ?drag=240 把标题列往右拖 240px 再报数(负数往左)
|
||
* ?reset=1 再双击把手回默认
|
||
* ?uncap=1 接着再拖到最宽,看那句「被切了」的提示会不会当场撤掉
|
||
* ?sticky=0 关掉固定列,只看列宽本身
|
||
* 浏览器里读不到结果时先看 title:探针出错会把异常写进 `<title>` 的 `probeError`。
|
||
* 异步一律走 `setTimeout`——无头 Chrome 的 `--virtual-time-budget` 推 `requestAnimationFrame`
|
||
* 不稳定,实测整段不跑(探针静默不报数,很容易被误判成"功能没生效")。
|
||
*/
|
||
import { createApp, h, onMounted } from 'vue'
|
||
import { createI18n } from 'vue-i18n'
|
||
import zh from '@/i18n/locales/zh'
|
||
import DataTable from '@/components/common/DataTable.vue'
|
||
import type { Column } from '@/components/common/types'
|
||
import '@/style.css'
|
||
|
||
const params = new URLSearchParams(window.location.search)
|
||
const drag = params.get('drag')
|
||
const shouldReset = params.get('reset') === '1'
|
||
const width = Number(params.get('width') || 900)
|
||
|
||
const COLUMNS: Column[] = [
|
||
{ key: 'title', label: '标题' },
|
||
{ key: 'activity_name', label: '环节' },
|
||
{ key: 'status', label: '状态' },
|
||
{ key: 'start_time', label: '发起时间' },
|
||
{ key: 'actions', label: '操作' }
|
||
]
|
||
const ROWS = [
|
||
{
|
||
id: '1',
|
||
title: '康文靖报销2026年10月定制开发项目费用流程-软件开发事业部-第三笔',
|
||
serial: 'FYBX202611030012',
|
||
activity_name: '补充确认信息',
|
||
status: '处理中',
|
||
start_time: '2026-11-12 09:12'
|
||
},
|
||
{
|
||
id: '2',
|
||
title: '备用金申请流程测试(软件开发事业部)-康文靖kangwenjing-2026-11-12',
|
||
serial: 'BYJ202611120002',
|
||
activity_name: '执行董事',
|
||
status: '已办结',
|
||
start_time: '2026-11-13 10:20'
|
||
}
|
||
]
|
||
|
||
/** 换行档位:?wrap=0 就先按那颗开关切到单行,再走后面的量法 */
|
||
const wantWrap = params.get('wrap') !== '0'
|
||
|
||
|
||
/** 悬浮层里那句原话(整表共用一只,挂在 body 上) */
|
||
const tipNode = () => document.body.querySelector('.table-tip') as HTMLElement | null
|
||
const tipShown = () => {
|
||
const tip = tipNode()
|
||
return !!tip && tip.style.display !== 'none'
|
||
}
|
||
|
||
/**
|
||
* 把光标停在这一格上,报两件事:
|
||
* - **多快开口**:从派发事件到浮层真的显形隔了几毫秒(原生 title 是半秒起,
|
||
* 这一档要治的就是"人已经扫完两行它才慢吞吞出来");
|
||
* - **说的是不是这格**:主句应当是列名,正文是这格的全文。
|
||
* 走的是 `mouseover` ——表体是 tbody 一层委托,逐格派发 mouseenter 打不到它。
|
||
*/
|
||
function hoverCell(cell: HTMLElement): Promise<Record<string, unknown>> {
|
||
const started = performance.now()
|
||
// 这一格"本来就没话说"?折行之后装得下、又没有现成的话要说 —— 不说话才是对的
|
||
const authored = cell.getAttribute('title') || cell.querySelector('[title]')?.getAttribute('title') || ''
|
||
const expectSpeak = !!authored || cell.scrollWidth > cell.clientWidth + 1
|
||
const inner = (cell.querySelector('p') || cell.firstElementChild || cell) as HTMLElement
|
||
inner.dispatchEvent(new MouseEvent('mouseover', { bubbles: true }))
|
||
// 浮层只有一只,读它之前先看它此刻开没开:收着的那只里还留着上一格那句
|
||
const read = () =>
|
||
tipShown()
|
||
? {
|
||
title: tipNode()!.querySelector('.table-tip__title')?.textContent?.trim() ?? null,
|
||
body: tipNode()!.querySelector('.table-tip__body')?.textContent?.trim().slice(0, 60) ?? null
|
||
}
|
||
: { title: null, body: null }
|
||
return new Promise((resolve) => {
|
||
const finish = (latency: number) =>
|
||
resolve({
|
||
该说话: expectSpeak as unknown,
|
||
出手: latency as unknown,
|
||
...read(),
|
||
原生title还挂着: !!cell.getAttribute('title')
|
||
})
|
||
const tick = () => {
|
||
if (tipShown()) return finish(Math.round(performance.now() - started))
|
||
if (performance.now() - started > 1500) return finish(expectSpeak ? -1 : (null as unknown as number))
|
||
window.setTimeout(tick, 10)
|
||
}
|
||
window.setTimeout(tick, 5)
|
||
})
|
||
}
|
||
|
||
/** 收回浮层:滚动这一路是组件里现成的"当场收" */
|
||
function dismissHover(wrapper: HTMLElement) {
|
||
wrapper.dispatchEvent(new Event('scroll'))
|
||
}
|
||
|
||
/** 这一格到底折没折行:拿内容自己的高度跟单行比,比"看起来对不对"可靠。 */
|
||
function cellWrapInfo(cell: HTMLElement | null) {
|
||
if (!cell) return null
|
||
const inner = (cell.querySelector('p') || cell.firstElementChild || cell) as HTMLElement
|
||
const range = document.createRange()
|
||
range.selectNodeContents(inner)
|
||
const textHeight = Math.round(range.getBoundingClientRect().height)
|
||
return {
|
||
cellWidth: Math.round(cell.getBoundingClientRect().width),
|
||
scrollWidth: cell.scrollWidth,
|
||
clipped: cell.scrollWidth > cell.clientWidth + 1,
|
||
textHeight,
|
||
wrappedLines: Math.max(1, Math.round(textHeight / 21)),
|
||
tip: cell.getAttribute('title')
|
||
}
|
||
}
|
||
|
||
/** 把一格的内容拆出来报数:有没有被切、悬浮说的是哪句。 */
|
||
function cellInfo(cell: HTMLElement | null) {
|
||
if (!cell) return null
|
||
return {
|
||
clipped: cell.scrollWidth > cell.clientWidth + 1,
|
||
tip: cell.getAttribute('title'),
|
||
autoTip: cell.dataset.autoTip ?? null
|
||
}
|
||
}
|
||
|
||
/** 按一下把手往右拖 delta 像素(pointerId 换着来,模拟两次独立拖动)。 */
|
||
function pointerDrag(handle: HTMLElement, delta: number, pointerId: number) {
|
||
const fire = (target: EventTarget, type: string, x: number) =>
|
||
target.dispatchEvent(
|
||
new PointerEvent(type, { bubbles: true, cancelable: true, clientX: x, button: 0, pointerId })
|
||
)
|
||
fire(handle, 'pointerdown', 100)
|
||
fire(document, 'pointermove', 100 + delta / 2)
|
||
fire(document, 'pointermove', 100 + delta)
|
||
fire(document, 'pointerup', 100 + delta)
|
||
}
|
||
|
||
const titleCell = () => document.querySelector('tbody tr:first-child td') as HTMLElement | null
|
||
|
||
/**
|
||
* 挨个停一遍:标题/环节这类该说话,操作列那一排按钮不该抢话。
|
||
* 每格之间收一次,免得"已经开着就立刻换内容"这条掩盖了真实的出手速度。
|
||
*/
|
||
async function hoverAll() {
|
||
const wrapper = document.querySelector('.table-wrapper') as HTMLElement
|
||
const cells = Array.from(document.querySelectorAll('tbody tr:first-child td')) as HTMLElement[]
|
||
const wait = (ms: number) => new Promise((resolve) => window.setTimeout(resolve, ms))
|
||
const out: Record<string, unknown> = {}
|
||
for (const [key, cell] of [
|
||
['title', cells[0]],
|
||
['activity', cells[1]],
|
||
['actions', cells[cells.length - 1]]
|
||
] as Array<[string, HTMLElement]>) {
|
||
out[key] = await hoverCell(cell)
|
||
dismissHover(wrapper)
|
||
await wait(30)
|
||
}
|
||
return out
|
||
}
|
||
|
||
function report() {
|
||
const table = document.querySelector('table') as HTMLTableElement
|
||
const wrapper = document.querySelector('.table-wrapper') as HTMLElement
|
||
const headerCells = Array.from(table.querySelectorAll('thead th')) as HTMLElement[]
|
||
const firstRow = Array.from(table.querySelectorAll('tbody tr'))[0]?.children ?? []
|
||
const cells = Array.from(firstRow) as HTMLElement[]
|
||
const titleCell = cells[0]
|
||
document.title = JSON.stringify({
|
||
tableFixed: table.classList.contains('table-fixed'),
|
||
minWMax: table.classList.contains('min-w-max'),
|
||
tableWidth: Math.round(table.getBoundingClientRect().width),
|
||
tableMinWidth: table.style.minWidth,
|
||
wrapperScroll: wrapper.scrollWidth,
|
||
wrapperClient: wrapper.clientWidth,
|
||
cols: headerCells.map((cell) => Math.round(cell.getBoundingClientRect().width)),
|
||
titleColTitleAttr: headerCells[0].getAttribute('title'),
|
||
cellTip: titleCell.getAttribute('title'),
|
||
cellClipped: titleCell.scrollWidth > titleCell.clientWidth + 1,
|
||
// 固定列那道阴影在不在:列宽接管后外侧伪元素该被关掉、改由内侧 box-shadow 顶上
|
||
wrapping: !!document.querySelector('.table-wrapper.is-wrapping'),
|
||
toggle: (() => {
|
||
const button = document.querySelector('[data-test="toggle-wrap"]') as HTMLElement | null
|
||
return button ? { pressed: button.getAttribute('aria-pressed'), text: button.textContent?.trim() } : null
|
||
})(),
|
||
stickyShadow: (() => {
|
||
const cell = document.querySelector('tbody tr:first-child td') as HTMLElement | null
|
||
if (!cell) return null
|
||
const cs = getComputedStyle(cell)
|
||
return {
|
||
sticky: cs.position,
|
||
cellOverflow: cs.overflow,
|
||
outerPseudoDisplay: getComputedStyle(cell, '::after').display,
|
||
insetShadow: cs.boxShadow
|
||
}
|
||
})(),
|
||
handleWidth: Math.round(
|
||
(document.querySelector('[data-test="resize-col-title"]') as HTMLElement)?.getBoundingClientRect().width ?? -1
|
||
)
|
||
})
|
||
}
|
||
|
||
const Harness = {
|
||
setup() {
|
||
onMounted(() => {
|
||
// 全用定时器:无头 Chrome 的虚拟时钟推 rAF 不稳定(实测整段不跑),setTimeout 才稳
|
||
window.setTimeout(async () => {
|
||
try {
|
||
const button = document.querySelector('[data-test="toggle-wrap"]') as HTMLElement | null
|
||
const wrapOn = button?.getAttribute('aria-pressed') === 'true'
|
||
if (button && wrapOn !== wantWrap) button.click()
|
||
await new Promise((resolve) => window.setTimeout(resolve, 40))
|
||
const handle = document.querySelector('[data-test="resize-col-title"]') as HTMLElement
|
||
if (handle && drag) pointerDrag(handle, Number(drag), 1)
|
||
if (handle && shouldReset) handle.dispatchEvent(new MouseEvent('dblclick', { bubbles: true }))
|
||
|
||
window.setTimeout(async () => {
|
||
report()
|
||
const cells = Array.from(document.querySelectorAll('tbody tr:first-child td')) as HTMLElement[]
|
||
const AUTHORED = 'FYBX202611030012 · 费用报销流程'
|
||
if (params.get('authored') === '1' && cells[0]) {
|
||
const line = cells[0].querySelector('p')
|
||
line?.setAttribute('title', AUTHORED)
|
||
}
|
||
const wrapInfo = { titleCell: cellWrapInfo(cells[0]) }
|
||
// ?hold=1:只停标题格且不收起,让浮层留在屏上好看一眼
|
||
if (params.get('hold') === '1') {
|
||
const spoke = await hoverCell(cells[0])
|
||
report()
|
||
document.title = document.title.replace(/\}$/, `,"hover":{"title":${JSON.stringify(spoke)}}}`)
|
||
return
|
||
}
|
||
const hovered = (await hoverAll()) as Record<string, unknown>
|
||
if (params.get('authored') === '1') {
|
||
const spoke = (hovered.title ?? {}) as Record<string, unknown>
|
||
// 借来的那句就是视图写的那一句(不是 innerText 凑的)
|
||
hovered['借的是视图那句'] = String(spoke.body ?? '').startsWith('FYBX')
|
||
// 展示期间原生 title 该被摘掉(不然两层提示叠着出)
|
||
hovered['展示期间原生已摘'] = spoke['原生title还挂着'] === false
|
||
// 收起来之后原生那句要原样回到格子上(借东西得还)
|
||
dismissHover(document.querySelector('.table-wrapper') as HTMLElement)
|
||
await new Promise((resolve) => window.setTimeout(resolve, 40))
|
||
hovered['还回来的原生title'] = cells[0]?.querySelector('p')?.getAttribute('title') ?? null
|
||
}
|
||
document.title = document.title.replace(
|
||
/\}$/,
|
||
`,"hover":${JSON.stringify(hovered)},"wrap":${JSON.stringify(wrapInfo)}}`
|
||
)
|
||
|
||
// uncap=1:先拖窄挂上提示,再拖到最宽,看那句提示会不会当场撤掉
|
||
if (params.get('uncap') === '1') {
|
||
const widest = document.querySelector('[data-test="resize-col-title"]') as HTMLElement
|
||
if (widest) pointerDrag(widest, 4000, 2)
|
||
window.setTimeout(() => {
|
||
const title = cellInfo(titleCell())
|
||
const cols = Array.from(document.querySelectorAll('thead th')).map(
|
||
(th) => Math.round((th as HTMLElement).getBoundingClientRect().width)
|
||
)
|
||
document.title = document.title.replace(
|
||
/\}$/,
|
||
`,"widened":{"cols":[${cols.join(',')}],"title":${JSON.stringify(title)}}}`
|
||
)
|
||
}, 80)
|
||
}
|
||
}, 80)
|
||
} catch (error) {
|
||
document.title = JSON.stringify({ probeError: String(error) })
|
||
}
|
||
}, 80)
|
||
})
|
||
return () =>
|
||
h('div', { style: `width:${width}px;overflow:auto;height:300px;display:flex` }, [
|
||
h('div', { style: 'flex:1;min-width:0;display:flex;flex-direction:column' }, [
|
||
h(
|
||
DataTable,
|
||
{
|
||
columns: COLUMNS,
|
||
data: ROWS,
|
||
rowKey: 'id',
|
||
columnWidthsKey: '/probe',
|
||
stickyFirstColumn: params.get('sticky') !== '0',
|
||
stickyActionsColumn: params.get('sticky') !== '0'
|
||
},
|
||
{
|
||
// 与真列表同形:标题那一格两行(标题 + 单号),第二列套徽章,操作列放按钮
|
||
'cell-title': ({ row }: any) =>
|
||
h('div', { class: 'min-w-[220px]' }, [
|
||
h('p', { class: 'font-medium' }, row.title),
|
||
h('p', { class: 'text-xs text-gray-400' }, row.serial)
|
||
]),
|
||
'cell-activity_name': ({ row }: any) =>
|
||
h('span', { class: 'badge badge-primary' }, row.activity_name),
|
||
// 意见那一格照视图的老写法:自己写了 truncate(这一档最容易跟折行打架)
|
||
'cell-status': ({ row }: any) =>
|
||
h('p', { class: 'truncate', title: row.status }, `审批意见:${row.status},金额与发票一致,可以付`),
|
||
'cell-actions': () =>
|
||
h('div', { class: 'flex items-center gap-1.5' }, [h('button', { class: 'btn btn-sm' }, '处理')])
|
||
}
|
||
)
|
||
])
|
||
])
|
||
}
|
||
}
|
||
|
||
const app = createApp(Harness)
|
||
app.use(createI18n({ legacy: false, locale: 'zh', messages: { zh } }))
|
||
app.mount('#app')
|