perf(short-drama-agent-front): 优化制作工作台交互与移动端布局
This commit is contained in:
1 parent
b5eaed3981
commit
54a381ebd3
37 files changed
+1217
-426
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import { nextTick, onScopeDispose, watch, type Ref } from 'vue'
|
||||
|
||||
/**
|
||||
* 为主体、分镜和生产目录共用选中项定位:桌面靠上、手机靠左,末尾遵守自然边界。
|
||||
* @param root 目录条目的根节点,位于目录自身的滚动容器内
|
||||
* @param getActiveId 读取当前选中项 ID
|
||||
* @param getItemIds 读取当前可见目录项 ID
|
||||
* @param stickyHeadingSelector 桌面粘性分组标题选择器
|
||||
*/
|
||||
export function useDirectorySelection(
|
||||
root: Ref<HTMLElement | null>,
|
||||
getActiveId: () => string,
|
||||
getItemIds: () => string[],
|
||||
stickyHeadingSelector?: string
|
||||
): void {
|
||||
/** 观察目录视口变化,兼容首次异步加载、屏幕缩放及手机横竖屏。 */
|
||||
let observer: ResizeObserver | undefined
|
||||
|
||||
/**
|
||||
* 只滚动目录自身,避免手机定位目录时改变整页阅读位置。
|
||||
* @param animate 是否为用户切换使用平滑滚动
|
||||
* @returns 目录渲染和定位完成
|
||||
*/
|
||||
async function locate(animate = false): Promise<void> {
|
||||
const activeId = getActiveId()
|
||||
await nextTick()
|
||||
const element = root.value
|
||||
const selected = element?.querySelector<HTMLElement>('[aria-current="true"]')
|
||||
const viewport = element?.closest<HTMLElement>('.n-scrollbar-container')
|
||||
if (!selected || !viewport || activeId !== getActiveId()) return
|
||||
const mobile = window.matchMedia('(max-width: 760px)').matches
|
||||
const behavior =
|
||||
animate && !window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'smooth' : 'instant'
|
||||
if (mobile) {
|
||||
viewport.scrollTo({
|
||||
left:
|
||||
viewport.scrollLeft + selected.getBoundingClientRect().left - viewport.getBoundingClientRect().left,
|
||||
behavior
|
||||
})
|
||||
return
|
||||
}
|
||||
const heading = stickyHeadingSelector
|
||||
? selected.parentElement?.parentElement?.querySelector<HTMLElement>(stickyHeadingSelector)
|
||||
: null
|
||||
viewport.scrollTo({
|
||||
top:
|
||||
viewport.scrollTop +
|
||||
selected.getBoundingClientRect().top -
|
||||
viewport.getBoundingClientRect().top -
|
||||
(heading?.offsetHeight ?? 0),
|
||||
behavior
|
||||
})
|
||||
}
|
||||
|
||||
/** 初次加载和可见项变化立即定位,切换选中项平滑定位,状态刷新不打断浏览。 */
|
||||
watch(
|
||||
[root, getActiveId, () => getItemIds().join(',')],
|
||||
(targets, previous) => void locate(!!previous?.[1] && previous[0] === targets[0] && previous[1] !== targets[1]),
|
||||
{ immediate: true, flush: 'post' }
|
||||
)
|
||||
/** 根节点可能在接口数据返回后才出现,出现时绑定尺寸监听,移除时释放旧监听。 */
|
||||
watch(
|
||||
root,
|
||||
element => {
|
||||
observer?.disconnect()
|
||||
const viewport = element?.closest('.n-scrollbar-container')
|
||||
if (!viewport) return
|
||||
observer = new ResizeObserver(() => void locate())
|
||||
observer.observe(viewport)
|
||||
},
|
||||
{ flush: 'post' }
|
||||
)
|
||||
/** 页面或目录卸载后释放监听。 */
|
||||
onScopeDispose(() => observer?.disconnect())
|
||||
}
|
||||
Reference in new issue
Block a user