mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-28 07:23:34 +08:00
feat(menu-tag): add pin icon and drag preview
This commit is contained in:
parent
2a4a6182b0
commit
edf4794078
@ -1,5 +1,6 @@
|
||||
import MenuTag from '@/layout/components/MenuTag'
|
||||
import type { MenuTagOptions } from '@/types'
|
||||
import { RIcon } from '@ray-template/ui/icon'
|
||||
import { flushPromises, mount } from '@vue/test-utils'
|
||||
import type { VueWrapper } from '@vue/test-utils'
|
||||
import { NDropdown, NTag } from 'naive-ui'
|
||||
@ -131,7 +132,12 @@ const mouse = (element: Element, type: string, button: number) => {
|
||||
)
|
||||
}
|
||||
|
||||
const point = (element: Element, type: string, clientX: number) => {
|
||||
const point = (
|
||||
element: Element,
|
||||
type: string,
|
||||
clientX: number,
|
||||
clientY = 16,
|
||||
) => {
|
||||
const event = new PointerEvent(type, {
|
||||
bubbles: true,
|
||||
isPrimary: true,
|
||||
@ -139,16 +145,19 @@ const point = (element: Element, type: string, clientX: number) => {
|
||||
pointerType: 'mouse',
|
||||
buttons: type === 'pointerup' ? 0 : 1,
|
||||
clientX,
|
||||
clientY: 16,
|
||||
clientY,
|
||||
})
|
||||
|
||||
Object.defineProperties(event, {
|
||||
pageX: { value: clientX },
|
||||
pageY: { value: 16 },
|
||||
pageY: { value: clientY },
|
||||
})
|
||||
element.dispatchEvent(event)
|
||||
}
|
||||
|
||||
const dragPreview = () =>
|
||||
document.body.querySelector<HTMLElement>('.menu-tag__drag-preview')
|
||||
|
||||
const openContextMenu = async (index: number) => {
|
||||
const tab = wrapper.findAll('.menu-tag__tab')[index]!.element
|
||||
|
||||
@ -279,10 +288,12 @@ it('updates tab status and actions while preferences change', async () => {
|
||||
.props('options')
|
||||
?.map((option) => option.key)
|
||||
|
||||
expect(tab.find('.n-tag__close').exists()).toBe(false)
|
||||
expect(tab.find('.n-tag__icon').exists()).toBe(false)
|
||||
const pin = tab.get('.n-tag__icon').getComponent(RIcon)
|
||||
const label = tab.get('.menu-tag__tab')
|
||||
|
||||
expect(tab.find('.n-tag__close').exists()).toBe(false)
|
||||
expect(pin.props('name')).toBe('action/pin')
|
||||
expect(pin.attributes('aria-hidden')).toBe('true')
|
||||
expect(label.attributes('aria-label')).toBe(
|
||||
'Reports · menuTag.topped · menuTag.fixed',
|
||||
)
|
||||
@ -295,6 +306,7 @@ it('updates tab status and actions while preferences change', async () => {
|
||||
state.drag.value = false
|
||||
await nextTick()
|
||||
expect(tab.find('.n-tag__close').exists()).toBe(true)
|
||||
expect(tab.find('.n-tag__icon').exists()).toBe(false)
|
||||
expect(label.attributes('aria-label')).toBeUndefined()
|
||||
expect(label.attributes('aria-keyshortcuts')).toBeUndefined()
|
||||
expect(actions()).not.toContain('toggleTop')
|
||||
@ -308,7 +320,6 @@ it('reorders a focused protected tab with Shift and an arrow key', async () => {
|
||||
const button = tab.get('.menu-tag__tab')
|
||||
|
||||
expect(tab.find('.n-tag__close').exists()).toBe(false)
|
||||
expect(tab.find('.r-icon').exists()).toBe(false)
|
||||
expect(button.attributes('aria-keyshortcuts')).toBe(
|
||||
'Shift+ArrowLeft Shift+ArrowRight',
|
||||
)
|
||||
@ -358,7 +369,20 @@ it('reorders from the tab body using the release position and ignores the drag c
|
||||
const source = tags[0].element
|
||||
|
||||
point(source, 'pointerdown', 10)
|
||||
point(tags[1].element, 'pointermove', 180)
|
||||
expect(dragPreview()).toBeNull()
|
||||
point(tags[1].element, 'pointermove', 180, 22)
|
||||
const preview = dragPreview()!
|
||||
|
||||
expect(preview.parentElement).toBe(row.closest('.menu-tag'))
|
||||
expect(preview.textContent).toBe(source.textContent)
|
||||
expect(preview.dataset.menuTagPath).toBeUndefined()
|
||||
expect(preview.getAttribute('aria-hidden')).toBe('true')
|
||||
expect(preview.hasAttribute('inert')).toBe(true)
|
||||
expect(preview.style.transform).toBe('translate3d(170px, 6px, 0)')
|
||||
|
||||
point(tags[1].element, 'pointermove', 190, 24)
|
||||
expect(dragPreview()).toBe(preview)
|
||||
expect(preview.style.transform).toBe('translate3d(180px, 8px, 0)')
|
||||
expect(tags[1].attributes('data-menu-tag-drop')).toBe('after')
|
||||
expect(state.move).not.toHaveBeenCalled()
|
||||
|
||||
@ -371,6 +395,7 @@ it('reorders from the tab body using the release position and ignores the drag c
|
||||
expect(
|
||||
row.querySelector('[data-menu-tag-drop],[data-menu-tag-dragged]'),
|
||||
).toBeNull()
|
||||
expect(dragPreview()).toBeNull()
|
||||
|
||||
await tags[0].trigger('click', { detail: 1 })
|
||||
expect(state.navigate).not.toHaveBeenCalled()
|
||||
@ -414,6 +439,9 @@ it('refreshes the drop marker after a scroll with a stationary pointer', () => {
|
||||
|
||||
point(source, 'pointerdown', 30)
|
||||
point(tags[1].element, 'pointermove', 180)
|
||||
const position = dragPreview()?.style.transform
|
||||
|
||||
expect(position).toBe('translate3d(150px, 0px, 0)')
|
||||
expect(tags[1].attributes('data-menu-tag-drop')).toBe('after')
|
||||
expect(requestFrame).toHaveBeenCalledTimes(1)
|
||||
requestFrame.mock.calls[0][0](16)
|
||||
@ -423,6 +451,7 @@ it('refreshes the drop marker after a scroll with a stationary pointer', () => {
|
||||
expect(requestFrame).toHaveBeenCalledTimes(2)
|
||||
requestFrame.mock.calls[1][0](32)
|
||||
expect(tags[2].attributes('data-menu-tag-drop')).toBe('after')
|
||||
expect(dragPreview()?.style.transform).toBe(position)
|
||||
point(tags[2].element, 'pointerup', 180)
|
||||
})
|
||||
|
||||
@ -435,6 +464,7 @@ it('cancels dragging with Escape and preserves keyboard activation', async () =>
|
||||
point(button.element, 'pointermove', 60)
|
||||
expect(wrapper.find('[data-menu-tag-dragged]').exists()).toBe(true)
|
||||
await button.trigger('keydown', { key: 'Escape' })
|
||||
expect(dragPreview()).toBeNull()
|
||||
point(button.element, 'pointerup', 60)
|
||||
await button.trigger('click', { detail: 0 })
|
||||
expect(state.navigate).toHaveBeenCalledExactlyOnceWith(
|
||||
@ -447,6 +477,30 @@ it('cancels dragging with Escape and preserves keyboard activation', async () =>
|
||||
expect(state.navigate).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it.each(['pointercancel', 'disabled'])(
|
||||
'removes the drag preview when the drag is %s',
|
||||
async (reason) => {
|
||||
const { row, tags } = mockTagRects()
|
||||
const source = tags[0].element
|
||||
|
||||
point(source, 'pointerdown', 10)
|
||||
point(tags[1].element, 'pointermove', 180)
|
||||
expect(dragPreview()).not.toBeNull()
|
||||
|
||||
if (reason === 'pointercancel') point(source, 'pointercancel', 180)
|
||||
else {
|
||||
state.drag.value = false
|
||||
await nextTick()
|
||||
}
|
||||
|
||||
expect(dragPreview()).toBeNull()
|
||||
expect(
|
||||
row.querySelector('[data-menu-tag-drop],[data-menu-tag-dragged]'),
|
||||
).toBeNull()
|
||||
expect(state.move).not.toHaveBeenCalled()
|
||||
},
|
||||
)
|
||||
|
||||
it.each([
|
||||
{ hold: 100, claimed: false },
|
||||
{ hold: 400, claimed: true },
|
||||
@ -477,6 +531,7 @@ it.each([
|
||||
|
||||
expect(move.defaultPrevented).toBe(claimed)
|
||||
expect(wrapper.find('[data-menu-tag-dragged]').exists()).toBe(claimed)
|
||||
expect(Boolean(dragPreview())).toBe(claimed)
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@ -29,9 +29,21 @@
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
.menu-tag__item.menu-tag__drag-preview {
|
||||
position: fixed;
|
||||
inset: 0 auto auto 0;
|
||||
z-index: 4;
|
||||
background-color: var(--r-surface);
|
||||
background-image: linear-gradient(var(--n-color), var(--n-color));
|
||||
box-shadow: var(--r-shadow-soft);
|
||||
pointer-events: none;
|
||||
transition: none;
|
||||
}
|
||||
|
||||
.menu-tag__item[data-menu-tag-drop]::after {
|
||||
position: absolute;
|
||||
inset-block: 0;
|
||||
z-index: 5;
|
||||
inline-size: 2px;
|
||||
border-radius: var(--r-control-radius);
|
||||
background: var(--r-theme-primary-color);
|
||||
|
||||
@ -136,7 +136,9 @@ export default defineComponent({
|
||||
label: t(isMenuTagFixed(contextTag.value) ? 'unfix' : 'fix'),
|
||||
key: 'toggleFixed',
|
||||
disabled: !contextTag.value,
|
||||
icon: () => <RIcon name="action/lock" size="1em" />,
|
||||
icon: () => (
|
||||
<RIcon name="action/pin" size="1em" aria-hidden="true" />
|
||||
),
|
||||
},
|
||||
]
|
||||
: []),
|
||||
@ -573,7 +575,19 @@ export default defineComponent({
|
||||
contextMenu={(event) =>
|
||||
menuTagContextMenu(curr.fullPath, event)
|
||||
}
|
||||
/>
|
||||
>
|
||||
{{
|
||||
prefix: fixed
|
||||
? () => (
|
||||
<RIcon
|
||||
name="action/pin"
|
||||
size={14}
|
||||
aria-hidden="true"
|
||||
/>
|
||||
)
|
||||
: void 0,
|
||||
}}
|
||||
</ShellTab>
|
||||
)
|
||||
})}
|
||||
</TransitionGroup>
|
||||
|
||||
@ -7,6 +7,7 @@ import type { Ref } from 'vue'
|
||||
|
||||
type Interact = typeof import('interactjs').default
|
||||
type Placement = 'before' | 'after'
|
||||
type ClientPosition = Pick<PointerEvent, 'clientX' | 'clientY'>
|
||||
|
||||
const TAB_SELECTOR = '[data-menu-tag-path]'
|
||||
const CLOSE_SELECTOR = '.n-tag__close'
|
||||
@ -30,6 +31,14 @@ interface Press {
|
||||
tab: HTMLElement
|
||||
button: HTMLButtonElement
|
||||
path: string
|
||||
offsetX: number
|
||||
offsetY: number
|
||||
}
|
||||
|
||||
interface Drag {
|
||||
preview: HTMLElement
|
||||
restoreDropMode: () => void
|
||||
release?: PointerEvent
|
||||
}
|
||||
|
||||
let activeTagDrags = 0
|
||||
@ -59,7 +68,7 @@ export function useTagReorder(
|
||||
announceCancellation: () => void,
|
||||
) {
|
||||
let press: Press | undefined
|
||||
let drag: { restoreDropMode: () => void; release?: PointerEvent } | undefined
|
||||
let drag: Drag | undefined
|
||||
let marker: DropMarker | undefined
|
||||
let frame: number | undefined
|
||||
let suppressPointerClick = false
|
||||
@ -81,6 +90,7 @@ export function useTagReorder(
|
||||
if (frame !== void 0) cancelAnimationFrame(frame)
|
||||
|
||||
frame = void 0
|
||||
drag?.preview.remove()
|
||||
drag?.restoreDropMode()
|
||||
drag = void 0
|
||||
press = void 0
|
||||
@ -105,8 +115,18 @@ export function useTagReorder(
|
||||
})
|
||||
}
|
||||
|
||||
const start = ({ pointer, tab }: Press) => {
|
||||
const movePreview = ({ clientX, clientY }: ClientPosition) => {
|
||||
if (!press || !drag) return
|
||||
|
||||
const x = clientX - press.offsetX
|
||||
const y = clientY - press.offsetY
|
||||
|
||||
drag.preview.style.transform = `translate3d(${x}px, ${y}px, 0)`
|
||||
}
|
||||
|
||||
const start = ({ pointer, tab }: Press, position: ClientPosition) => {
|
||||
pointer.interaction.start({ name: 'drag' }, pointer.interactable, tab)
|
||||
movePreview(position)
|
||||
}
|
||||
|
||||
const startWithPointer = (event: PointerEvent) => {
|
||||
@ -123,7 +143,7 @@ export function useTagReorder(
|
||||
const x = Math.abs(event.clientX - current.down.clientX)
|
||||
const y = Math.abs(event.clientY - current.down.clientY)
|
||||
|
||||
if (Math.max(x, y) >= DRAG_SLOP) start(current)
|
||||
if (Math.max(x, y) >= DRAG_SLOP) start(current, event)
|
||||
}
|
||||
|
||||
const trackTouch = (event: TouchEvent) => {
|
||||
@ -153,7 +173,7 @@ export function useTagReorder(
|
||||
return
|
||||
}
|
||||
|
||||
start(current)
|
||||
start(current, touch)
|
||||
}
|
||||
|
||||
// 普通滑动保持原生滚动,长按启动拖动后才接管触摸。
|
||||
@ -165,6 +185,10 @@ export function useTagReorder(
|
||||
row: HTMLElement,
|
||||
viewport: HTMLElement,
|
||||
) => {
|
||||
const previewHost = row.closest('.menu-tag')
|
||||
|
||||
if (!previewHost) return
|
||||
|
||||
const zoneRect = (element: Element) => {
|
||||
const visible = interact.getElementRect(viewport)
|
||||
const origin = interact.getElementRect(row)?.left
|
||||
@ -224,12 +248,21 @@ export function useTagReorder(
|
||||
start: () => {
|
||||
if (!press) return
|
||||
|
||||
drag = { restoreDropMode: retainDynamicDrop(interact) }
|
||||
const preview = press.tab.cloneNode(true) as HTMLElement
|
||||
|
||||
preview.classList.add('menu-tag__drag-preview')
|
||||
preview.removeAttribute('data-menu-tag-path')
|
||||
preview.setAttribute('aria-hidden', 'true')
|
||||
preview.setAttribute('inert', '')
|
||||
// 副本浮在滚动区外,原标签继续占位并参与落点判定。
|
||||
previewHost.append(preview)
|
||||
drag = { preview, restoreDropMode: retainDynamicDrop(interact) }
|
||||
suppressPointerClick = true
|
||||
row.classList.add('menu-tag-wrapper--dragging')
|
||||
press.tab.setAttribute('data-menu-tag-dragged', '')
|
||||
refreshDrop()
|
||||
},
|
||||
move: movePreview,
|
||||
end: finish,
|
||||
},
|
||||
})
|
||||
@ -312,8 +345,20 @@ export function useTagReorder(
|
||||
const path = tab.dataset.menuTagPath
|
||||
const button = tab.querySelector<HTMLButtonElement>('.menu-tag__tab')
|
||||
|
||||
if (path && button)
|
||||
press = { down: event, pointer, row, tab, button, path }
|
||||
if (!path || !button) return
|
||||
|
||||
const { left, top } = tab.getBoundingClientRect()
|
||||
|
||||
press = {
|
||||
down: event,
|
||||
pointer,
|
||||
row,
|
||||
tab,
|
||||
button,
|
||||
path,
|
||||
offsetX: event.clientX - left,
|
||||
offsetY: event.clientY - top,
|
||||
}
|
||||
})
|
||||
|
||||
return () => {
|
||||
|
||||
@ -43,8 +43,8 @@ updateSettingState('layoutConfig', {
|
||||
|
||||
### 页面标签与面包屑
|
||||
|
||||
- 右键点击标签或打开右侧「标签页操作」菜单,可置顶或固定标签。置顶将标签移至最左侧,取消置顶时移到普通标签组开头。固定后,关闭按钮隐藏,标签受单个关闭和批量关闭保护。
|
||||
- 直接拖动标签,或聚焦标签后按 Shift + ← 或 Shift + → 即可排序。触屏上滑动标签栏可滚动,按住标签再拖动可排序。置顶标签和普通标签各自组内排序。顺序、置顶和固定状态随当前会话刷新恢复。
|
||||
- 右键点击标签或打开右侧「标签页操作」菜单,可置顶或固定标签。置顶将标签移至最左侧,取消置顶时移到普通标签组开头。固定后,标题左侧显示图钉,关闭按钮隐藏,标签受单个关闭和批量关闭保护。
|
||||
- 直接拖动标签,或聚焦标签后按 Shift + ← 或 Shift + → 即可排序。拖动时标签副本跟随指针,插入线指示落点。触屏上滑动标签栏可滚动,按住标签再拖动可排序。置顶标签和普通标签各自组内排序。顺序、置顶和固定状态随当前会话刷新恢复。
|
||||
- 面包屑复用菜单配置中的图标,支持 SVG 图标名和代码路由里的图片 VNode。缺少图标时使用线框窗口图标。窄屏显示当前路径项及其图标。
|
||||
|
||||
「偏好设置 → 外观 → 界面显示」提供五个独立开关,默认全部开启。关闭固定或置顶开关会暂停对应效果并保留标签标记,重新开启后恢复生效。关闭面包屑图标开关会一并隐藏占位图标,也可单独关闭占位图标。
|
||||
|
||||
@ -203,6 +203,7 @@
|
||||
"action/more",
|
||||
"action/mouse",
|
||||
"action/out",
|
||||
"action/pin",
|
||||
"action/print",
|
||||
"action/reload",
|
||||
"action/search",
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
操作、方向、导航、状态和系统图标来自 [Iconfont · 迈鼎icon](https://www.iconfont.cn/collections/detail?cid=51731) 的 IconPark 线性图形。原作者为 [ByteDance IconPark](https://github.com/bytedance/IconPark),Copyright 2019-present Bytedance Inc.;开源许可见 [Apache 2.0](LICENSE-ICONPARK)。表内 ID 对应该合辑的具体条目。入包时保留原始绘制路径,去掉透明画布路径和导出属性。按实际图形边界重设正方形视图框,让常规图标的主体约占八成半;省略号和展开箭头保留更多留白。轮廓补少量描边,使小尺寸下的线条更清楚,填充和描边均使用 `currentColor`。
|
||||
|
||||
The `action/close_all`, `action/table-striped`, and `action/table-bordered` icons are original artwork in this repository, covered by its MIT license.
|
||||
The `action/close_all`, `action/pin`, `action/table-striped`, and `action/table-bordered` icons are original artwork in this repository, covered by its MIT license.
|
||||
|
||||
品牌标识沿用仓库已有的 SVG。不要将品牌标识与这批通用线性图标的来源或许可混用。
|
||||
|
||||
|
||||
3
packages/icons/src/action/pin.svg
Normal file
3
packages/icons/src/action/pin.svg
Normal file
@ -0,0 +1,3 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path d="m14.2 3.2 6.6 6.6-3.7.6-4.2 4.2-.6 3.7-6.6-6.6 3.7-.6 4.2-4.2.6-3.7ZM9 15l-5.8 5.8" />
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 266 B |
@ -22,6 +22,7 @@ export const iconNames = [
|
||||
'action/more',
|
||||
'action/mouse',
|
||||
'action/out',
|
||||
'action/pin',
|
||||
'action/print',
|
||||
'action/reload',
|
||||
'action/search',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user