diff --git a/docs/content/1.guide/16.hub.md b/docs/content/1.guide/16.hub.md index bdaed873..a05464b5 100644 --- a/docs/content/1.guide/16.hub.md +++ b/docs/content/1.guide/16.hub.md @@ -272,11 +272,12 @@ The first time any member of a group opens in a session, the hub also loads the Declare `defaultChildId` when one member is the natural landing spot; leave it off when the members are peers. -### Pinned and recent tools +### Pinned, reordered, and recent tools The reference hub UI (`@devframes/hub-ui`) lets users reach a group member in one click. A devframe does not need to set anything for this. - **Pin.** Right-click a dock button (on the bar, in a group popover, or in the group sidebar) and pick **Pin**. Keyboard users press Shift+F10 or the Menu key on a focused button. A pinned entry moves to the front of the bar. A pinned group member also gets its own bar button, and it still leads inside its group. Opening it opens that member, and its group button shows as active. Pins are stored in the `docksPinned` user setting. +- **Reorder.** Drag a tool up or down in the group sidebar to change its place. Keyboard users press Alt+ArrowUp or Alt+ArrowDown on a focused tool. A tool moves only inside its own sub-category. The order is stored in the `docksCustomOrder` user setting, the same one **Settings > Docks** edits. - **Recent.** When a user opens a tool from a group popover or the overflow menu, the float bar keeps it in a slot after the bar items, newest first. The slots are added on top of the bar's capacity, so opening a tool never pushes another button into the overflow. A tool that is already on the bar (for example a pinned member) does not take a slot. The list is kept per browser tab, in the `recentDockIds` field of the session store. The `docksRecentLimit` user setting sets how many recent slots the bar keeps. The default is `3`. `0` turns the slots off. Users change it under **Settings > Appearance > Recent tools on the dock bar**. The edge-mode toolbar shows pinned entries but no recent slots. diff --git a/packages/hub-ui/src/client/components/dock/DockGroupSidebar.vue b/packages/hub-ui/src/client/components/dock/DockGroupSidebar.vue index ce0cdf5d..fa3a6fe2 100644 --- a/packages/hub-ui/src/client/components/dock/DockGroupSidebar.vue +++ b/packages/hub-ui/src/client/components/dock/DockGroupSidebar.vue @@ -2,9 +2,9 @@ import type { DevframeDockEntry, DevframeViewGroup } from '@devframes/hub' import type { DocksContext } from '@devframes/hub/client' import { watchDebounced } from '@vueuse/core' -import { computed, h, ref, useTemplateRef, watch } from 'vue' +import { computed, h, nextTick, ref, useTemplateRef, watch } from 'vue' import { t } from '../../i18n' -import { deriveSidebarCapacity, docksSplitGroupsWithCapacity, getGroupMembersGrouped } from '../../state/dock-settings' +import { applyCustomOrderPatch, deriveSidebarCapacity, docksSplitGroupsWithCapacity, getGroupMembersGrouped, resolveGroupMemberMove } from '../../state/dock-settings' import { getCurrentDockId } from '../../state/docks' import { dockTooltipContent, setDocksSidebarOverflowPanel, setFloatingTooltip, useDocksSidebarOverflowPanel } from '../../state/floating-tooltip' import { useSettings } from '../../state/settings-defaults' @@ -101,10 +101,112 @@ const currentInOverflow = computed(() => const selectedInOverflow = computed(() => !!props.selectedId && overflowGroups.value.some(([, items]) => items.some(m => m.id === props.selectedId))) +// Set when a drag ends, so the click that follows it does not open the member. +let suppressClick = false + function select(id: string) { + if (suppressClick) { + suppressClick = false + return + } props.context.docks.switchEntry(id) } +// --- Reorder --- +// Drag a member up or down within its sub-category, or press Alt+ArrowUp / +// Alt+ArrowDown on it. The order is saved to `docksCustomOrder`, the same +// setting the Docks settings page edits. Pointer capture keeps the drag alive +// while the pointer crosses the panel's iframe. +const DRAG_THRESHOLD = 4 +const draggingId = ref(null) +const dragOverId = ref(null) +let dragStart: { id: string, y: number } | null = null + +function moveMember(fromId: string, toId: string): boolean { + const patch = resolveGroupMemberMove(props.context.docks.entries, props.group.id, settings.value, fromId, toId) + if (!patch) + return false + props.context.docks.settings.mutate(state => applyCustomOrderPatch(state, patch)) + return true +} + +function findSubcategory(id: string) { + return visibleGroups.value.find(([, items]) => items.some(m => m.id === id)) +} + +// Where the drop line shows on the hovered member: the dragged member takes +// the hovered member's slot, so it lands before it when moving up and after +// it when moving down. +const dropSide = computed<'before' | 'after' | null>(() => { + if (!draggingId.value || !dragOverId.value) + return null + const items = findSubcategory(draggingId.value)?.[1] ?? [] + const from = items.findIndex(m => m.id === draggingId.value) + const to = items.findIndex(m => m.id === dragOverId.value) + return from > to + ? 'before' + : 'after' +}) + +function onMemberPointerDown(event: PointerEvent, id: string) { + hideTooltip() + suppressClick = false + if (event.button !== 0) + return + dragStart = { id, y: event.clientY } + ;(event.currentTarget as HTMLElement).setPointerCapture(event.pointerId) +} + +function onMemberPointerMove(event: PointerEvent) { + if (!dragStart) + return + if (!draggingId.value) { + if (Math.abs(event.clientY - dragStart.y) < DRAG_THRESHOLD) + return + draggingId.value = dragStart.id + } + const peers = findSubcategory(dragStart.id)?.[1] ?? [] + const target = peers.find((m) => { + const rect = sidebar.value?.querySelector(`[data-dock-id="${CSS.escape(m.id)}"]`)?.getBoundingClientRect() + return rect && event.clientY >= rect.top && event.clientY <= rect.bottom + }) + dragOverId.value = target && target.id !== dragStart.id + ? target.id + : null +} + +function onMemberPointerUp() { + if (draggingId.value) { + // A drag is not a click, even when it ends on the start button. + suppressClick = true + if (dragOverId.value) + moveMember(draggingId.value, dragOverId.value) + } + resetDrag() +} + +function resetDrag() { + dragStart = null + draggingId.value = null + dragOverId.value = null +} + +async function onMemberKeydown(event: KeyboardEvent, id: string) { + if (!event.altKey || (event.key !== 'ArrowUp' && event.key !== 'ArrowDown')) + return + event.preventDefault() + const items = findSubcategory(id)?.[1] ?? [] + const index = items.findIndex(m => m.id === id) + const neighbor = items[index + (event.key === 'ArrowUp' + ? -1 + : 1)] + if (!neighbor || !moveMember(id, neighbor.id)) + return + // Moving the focused button in the DOM can drop focus, so put it back. + await nextTick() + sidebar.value?.querySelector(`[data-dock-id="${CSS.escape(id)}"]`)?.focus() +} + function showTooltip(event: PointerEvent, entry: { title: string, description?: string }) { setFloatingTooltip({ content: dockTooltipContent(entry), @@ -212,19 +314,31 @@ const moreButtonActive = computed(() => selectedInOverflow.value || isOverflowPa