Files
dsh-worktable/04_test/edge-panel.test.mjs
wangheng 8255ad604c feat(ui): 停靠态改为左缘细条 + hover 滑出面板 (v0.5.3-local.11)
停靠态原先把整个工作台面板常驻挂在 footer 槽位,占用侧栏纵向空间。
改为 12px 左缘细条 + 悬停滑出 250px 面板:

- 自动隐藏(默认):悬停细条滑出,移开 180ms 自动收回
- 固定常开:面板常显,可点右侧 « 手柄收成细条,之后仅点击展开
- 任一弹窗打开期间保持展开,避免误收回
- measureFloatGeo 新增细条左缘测量:优先 footerActions/footArea 左缘 −8,
  链断裂回退侧栏左缘,避免旧兜底 64 的残带
- footer 槽位只留零尺寸锚点,不再占位
- 浮动态(dock=float)保持原浮动卡片,不渲染细条

新增 04_test/edge-panel.test.mjs 并纳入 pack 兼容回归,覆盖默认自动隐藏、
设置开关持久化、固定常开 + « 收起、浮动态不受影响。

验证:npm run pack 全绿,双资产同源 sha256 faed3889…。
2026-12-03 15:56:42 +08:00

200 lines
9.7 KiB
JavaScript

// 边缘面板(左缘细条 + hover 滑出)行为验证:针对实际构建产物。
import { readFileSync } from 'node:fs'
import { createContext, runInNewContext } from 'node:vm'
import { fileURLToPath } from 'node:url'
import { dirname, join, resolve } from 'node:path'
const BUNDLE = resolve(process.argv[2] ?? join(dirname(fileURLToPath(import.meta.url)), '..', '01_content', 'lib', 'client.js'))
const source = readFileSync(BUNDLE, 'utf8')
const assert = (c, m) => { if (!c) throw new Error(m) }
const Fragment = Symbol('test.fragment')
const jsx = (type, props, key) => ({ type, props: props ?? {}, key: key ?? props?.key ?? null })
function makeHooks() {
const slots = []
let cursor = 0
const useState = (initial) => {
const i = cursor++
if (!(i in slots)) slots[i] = typeof initial === 'function' ? initial() : initial
return [slots[i], (next) => { slots[i] = typeof next === 'function' ? next(slots[i]) : next }]
}
const useRef = (initial) => {
const i = cursor++
if (!(i in slots)) slots[i] = { current: initial }
return slots[i]
}
const pass = (a, b) => b ?? undefined
const React = {
createElement: (type, props, ...children) => ({ type, props: { ...(props ?? {}), children: children.length > 1 ? children : children[0] }, key: props?.key ?? null }),
Fragment, useState, useRef,
useMemo: (fn) => fn(),
useCallback: (fn) => fn,
useEffect: () => {},
useLayoutEffect: () => {},
useContext: () => undefined,
useReducer: (r, initial) => [initial, () => {}],
memo: (c) => c,
forwardRef: (c) => c,
createContext: () => ({ Provider: Fragment, Consumer: Fragment }),
version: 'test',
}
return { React, render: (component, props) => { cursor = 0; return component(props) } }
}
function fakeElement(tag = 'div') {
const el = {
tagName: String(tag).toUpperCase(),
className: '', style: {}, children: [], dataset: {},
classList: { add() {}, remove() {}, contains: () => false },
getBoundingClientRect: () => ({ left: 0, right: 1280, top: 0, bottom: 0, width: 1280, height: 0 }),
appendChild(c) { el.children.push(c); return c },
removeChild() {}, remove() {},
setAttribute() {}, getAttribute: () => null, removeAttribute() {},
addEventListener() {}, removeEventListener() {},
focus() {}, blur() {}, click() {},
querySelector: () => null, querySelectorAll: () => [],
parentElement: null, parentNode: null,
scrollIntoView() {},
getContext: () => ({ globalCompositeOperation: '', createLinearGradient() { return { addColorStop() {} } }, fillRect() {}, getImageData() { return { data: [0, 0, 0, 255] } } }),
}
return el
}
function loadComponent({ stored = {} } = {}) {
const values = new Map([['dsh.worktable.updateCheck.v1', '0']])
for (const [key, value] of Object.entries(stored)) values.set(key, String(value))
const localStorage = {
getItem: (key) => values.has(key) ? values.get(key) : null,
setItem: (key, value) => values.set(key, String(value)),
removeItem: (key) => values.delete(key),
}
const body = fakeElement('body')
const head = fakeElement('head')
const document = {
body, head, documentElement: fakeElement('html'),
querySelector: () => null, querySelectorAll: () => [],
createElement: (tag) => fakeElement(tag),
addEventListener() {}, removeEventListener() {},
}
const window = {
innerWidth: 1280, innerHeight: 900, localStorage, document,
__ModuleLoader__: { load: (definition) => { window.captured = definition } },
addEventListener() {}, removeEventListener() {}, dispatchEvent() {},
matchMedia: () => ({ matches: false, addEventListener() {}, removeEventListener() {} }),
setTimeout: () => 1, clearTimeout() {}, setInterval: () => 1, clearInterval() {},
requestAnimationFrame: () => 1, cancelAnimationFrame() {}, open() {},
}
const hooks = makeHooks()
const jsxRuntime = { Fragment, jsx, jsxs: jsx }
const sandbox = {
window, document, localStorage, self: window,
location: { href: 'http://localhost/', protocol: 'http:' },
navigator: { userAgent: 'node edge regression', platform: 'Win32', language: 'en-US' },
console, atob, btoa,
fetch: async () => ({ ok: false, status: 404, json: async () => ({}) }),
setTimeout: window.setTimeout, clearTimeout: window.clearTimeout,
setInterval: window.setInterval, clearInterval: window.clearInterval,
requestAnimationFrame: window.requestAnimationFrame, cancelAnimationFrame: window.cancelAnimationFrame,
MutationObserver: class { observe() {} disconnect() {} },
ResizeObserver: class { observe() {} unobserve() {} disconnect() {} },
IntersectionObserver: class { observe() {} unobserve() {} disconnect() {} },
CustomEvent: class { constructor(type, init) { this.type = type; this.detail = init?.detail } },
URL, Blob, AbortController, indexedDB: undefined,
}
createContext(sandbox)
runInNewContext(source, sandbox, { filename: BUNDLE, timeout: 20000 })
const requireExternal = (id) => {
if (id === 'react') return hooks.React
if (id === 'react/jsx-runtime') return jsxRuntime
if (id === 'react-dom') return { createPortal: (child) => child }
if (id === 'scheduler') return {}
throw new Error('unexpected external: ' + id)
}
const exports = window.captured.factory(requireExternal)
assert(typeof exports.WorktableSection === 'function', 'no WorktableSection export')
return { component: exports.WorktableSection, hooks, values }
}
function walk(node, pred, out = []) {
if (Array.isArray(node)) { for (const c of node) walk(c, pred, out); return out }
if (!node || typeof node !== 'object') return out
if (typeof node.type !== 'undefined' && pred(node)) out.push(node)
walk(node.props?.children, pred, out)
return out
}
const classHas = (node, name) => String(node.props?.className ?? '').split(/\s+/).includes(name)
const byClass = (tree, name) => walk(tree, (n) => classHas(n, name))
function run() {
// 1) 默认(edgeAutoHide 未设置 = 自动隐藏):停靠态渲染细条,面板默认收起
{
const { component, hooks } = loadComponent()
const render = () => hooks.render(component, { wide: true, t: (k) => k })
let tree = render()
assert(byClass(tree, 'dsh-wt_edgeStrip').length === 1, '未渲染左缘细条')
assert(byClass(tree, 'dsh-wt_anchor' + 'x').length === 0, 'sanity')
assert(byClass(tree, 'dsh-wt_edgeAnchor').length === 1, '未渲染零尺寸锚点')
assert(byClass(tree, 'dsh-wt_edgePanelWrap').length === 1, '未渲染面板包裹层')
assert(byClass(tree, 'dsh-wt_edgePanelWrapOn').length === 0, '自动隐藏模式下默认应是收起')
// 细条上有 hover 打开
const strip = byClass(tree, 'dsh-wt_edgeStrip')[0]
assert(typeof strip.props.onMouseEnter === 'function', '细条缺少 hover 打开')
strip.props.onMouseEnter({})
tree = render()
assert(byClass(tree, 'dsh-wt_edgePanelWrapOn').length === 1, 'hover 后应展开')
// 移开 → 调度关闭(定时器在夹具里不执行,验证 handler 存在)
const wrap = byClass(tree, 'dsh-wt_edgePanelWrap')[0]
assert(typeof wrap.props.onMouseLeave === 'function', '面板缺少移开收回')
// 自动隐藏模式不显示 « 手柄
assert(byClass(tree, 'dsh-wt_edgeHandle').length === 0, '自动隐藏模式不应出现收起手柄')
console.log('PASS 1: 默认自动隐藏 — 细条常驻、默认收起、hover 展开、无手柄')
}
// 2) 设置开关存在且可切换
{
const { component, hooks, values } = loadComponent()
const render = () => hooks.render(component, { wide: true, t: (k) => k })
let tree = render()
assert(byClass(tree, 'dsh-wt_edgeSettings').length === 1, '设置里缺少侧边栏控制项')
const toggle = byClass(tree, 'dsh-wt_edgeSettingsToggle')[0]
assert(toggle?.props?.role === 'switch', '开关 role 不是 switch')
assert(toggle.props['aria-checked'] === true, '默认应为开启')
toggle.props.onClick({})
tree = render()
const raw = values.get('dsh.worktable.view.v1')
assert(raw && JSON.parse(raw).edgeAutoHide === false, '切换后未持久化 edgeAutoHide=false')
console.log('PASS 2: 设置开关存在、默认开启、点击持久化为 false')
}
// 3) 固定常开(edgeAutoHide=false):面板常显 + 出现 « 手柄;细条无 hover 展开
{
const { component, hooks } = loadComponent({ stored: { 'dsh.worktable.view.v1': JSON.stringify({ edgeAutoHide: false }) } })
const render = () => hooks.render(component, { wide: true, t: (k) => k })
let tree = render()
assert(byClass(tree, 'dsh-wt_edgePanelWrapOn').length === 1, '固定常开模式下应直接展开')
assert(byClass(tree, 'dsh-wt_edgeHandle').length === 1, '固定常开模式应出现 « 手柄')
const strip = byClass(tree, 'dsh-wt_edgeStrip')[0]
assert(strip.props.onMouseEnter === undefined, '固定常开模式细条不应 hover 展开')
// 点 « 收起
const handle = byClass(tree, 'dsh-wt_edgeHandle')[0]
handle.props.onClick({})
tree = render()
assert(byClass(tree, 'dsh-wt_edgePanelWrapOn').length === 0, '点 « 后应收起')
console.log('PASS 3: 固定常开 — 常显、有 « 手柄、细条不 hover、点 « 可收起')
}
// 4) 浮动态不受影响
{
const { component, hooks } = loadComponent({ stored: { 'dsh.worktable.view.v1': JSON.stringify({ dock: 'float', floatTop: 100 }) } })
const tree = hooks.render(component, { wide: true, t: (k) => k })
assert(byClass(tree, 'dsh-wt_float').length >= 1, '浮动态应保留浮动卡片')
assert(byClass(tree, 'dsh-wt_edgeStrip').length === 0, '浮动态不应渲染细条')
console.log('PASS 4: 浮动态保持原行为,不渲染细条')
}
console.log('\nALL EDGE PANEL CHECKS PASSED')
}
run()