feat(redis): support drag selecting keys

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-10-06 21:27:43 +08:00
1 parent 53606ffd63
commit 0503d3b9d1
2 files changed
+82 -2

No files matched your search

@@ -13,7 +13,7 @@ import RedisGroupHeader from "./RedisGroupHeader.vue";
import { createRedisKeyViewYield } from "@/lib/redis/redisKeyViewScheduler";
const props = defineProps<{ keys: readonly RedisKeyInfo[]; config: RedisKeyGrouping; separator: string; scope: string; structureRevision: number; selected: string | null; checked: ReadonlySet<string>; busy: boolean; metadataEpoch: number; treeAllowed?: boolean }>();
const emit = defineEmits<{ select: [RedisKeyInfo]; check: [RedisKeyInfo, MouseEvent]; delete: [RedisKeyInfo, MouseEvent]; copy: [RedisKeyInfo]; scroll: [Event]; resize: []; requestList: [] }>();
const emit = defineEmits<{ select: [RedisKeyInfo]; check: [RedisKeyInfo, MouseEvent]; pointerdown: [RedisKeyInfo, PointerEvent]; delete: [RedisKeyInfo, MouseEvent]; copy: [RedisKeyInfo]; scroll: [Event]; resize: []; requestList: [] }>();
const { t } = useI18n();
const expanded = shallowRef(new Set<string>());
const working = ref(false);
@@ -94,6 +94,7 @@ function firstUnobscuredRowIndex() {
}
defineExpose({
getScrollElement: () => scroller.value?.$el as HTMLElement | undefined,
getKeyRawsInDisplayOrder: () => source.filter((row): row is Extract<RedisGroupedRow, { kind: "key" }> => row.kind === "key").map((row) => row.key.key_raw),
getViewportAnchor: () => {
const rowIndex = firstUnobscuredRowIndex();
const row = source[rowIndex];
@@ -213,10 +214,12 @@ onDeactivated(() => {
/>
<CustomContextMenu v-else :items="[{ label: t('redis.copyKeyName'), icon: Copy, action: () => emit('copy', item.key) }]" v-slot="{ onContextMenu }">
<div
class="group flex h-[30px] cursor-pointer items-center gap-1 border-b px-2 text-[13px] hover:bg-accent/40"
:data-redis-key-raw="item.key.key_raw"
class="group flex h-[30px] cursor-pointer items-center gap-1 border-b px-2 text-[13px] hover:bg-accent/40 select-none"
:class="selected === item.key.key_raw ? 'bg-accent' : checked.has(item.key.key_raw) ? 'bg-primary/10' : ''"
:style="{ paddingLeft: `${8 + item.depth * 10}px` }"
@click="emit('select', item.key)"
@pointerdown="emit('pointerdown', item.key, $event)"
@contextmenu="onContextMenu"
>
<input type="checkbox" :checked="checked.has(item.key.key_raw)" :disabled="busy" :aria-label="item.key.key_display" @click.stop="emit('check', item.key, $event)" />
@@ -182,6 +182,10 @@ const keyMetadataEpoch = ref(0);
/** Bumped only when a TTL refresh changes membership in the no-expiry projection. */
const noExpiryProjectionEpoch = ref(0);
const selectionAnchorRowId = ref<string | null>(null);
const dragSelection = ref<{ startRaw: string; baseChecked: Set<string>; active: boolean; lastEndRaw?: string; startX: number; startY: number } | null>(null);
// A click fires after pointerup on the same gesture; an active rubber-band drag
// must not fall through to opening the row it started (or ended) on.
const suppressNextRowClick = ref(false);
const selectedGroupLeafCounts = shallowRef<Map<string, number>>(new Map());
const deletingKeys = ref(false);
const showBatchExpiryDialog = ref(false);
@@ -746,6 +750,66 @@ function setKeysChecked(keyRaws: Iterable<string>, checked: boolean) {
selectionEpoch.value++;
}
function dragSelectionRows(startRaw: string, endRaw: string, baseChecked: ReadonlySet<string>) {
const groupedDisplayKeys = customGrouping.value.enabled ? groupedKeyListRef.value?.getKeyRawsInDisplayOrder?.() : undefined;
const keys = groupedDisplayKeys?.length ? groupedDisplayKeys : visibleRows.value.flatMap((row) => (row.node.kind === "leaf" ? [row.node.keyRaw] : []));
const start = keys.indexOf(startRaw);
const end = keys.indexOf(endRaw);
if (start < 0 || end < 0) return;
const range = new Set(keys.slice(Math.min(start, end), Math.max(start, end) + 1));
// Keep earlier batches selected, while letting the current drag range shrink
// or expand as the pointer moves back and forth. Each row is toggled against
// the selection snapshot, so mixed ranges add unselected rows and remove
// selected rows in the same gesture.
const next = new Set(baseChecked);
for (const keyRaw of range) {
if (baseChecked.has(keyRaw)) next.delete(keyRaw);
else next.add(keyRaw);
}
checkedKeys.value = next;
selectedGroupLeafCounts.value = groupLeafCountsFromChecked(next);
selectionEpoch.value++;
}
function onKeyPointerDown(node: RedisKeyTreeNode, event: PointerEvent) {
if (event.button !== 0 || selectionBusy.value || node.kind !== "leaf" || (event.target as HTMLElement | null)?.closest("input,button")) return;
suppressNextRowClick.value = false;
dragSelection.value = { startRaw: node.keyRaw, baseChecked: new Set(checkedKeys.value), active: false, startX: event.clientX, startY: event.clientY };
}
function onKeyPointerMove(event: PointerEvent) {
const drag = dragSelection.value;
if (!drag || selectionBusy.value) return;
// A missed pointerup (or a touch/pen takeover) leaves the drag armed; hover
// moves with no button pressed must not keep toggling rows.
if ((event.buttons & 1) === 0) {
if (drag.active) suppressNextRowClick.value = true;
dragSelection.value = null;
return;
}
if (!drag.active && Math.hypot(event.clientX - drag.startX, event.clientY - drag.startY) < 4) return;
const row = (event.target as HTMLElement | null)?.closest<HTMLElement>("[data-redis-key-raw]");
const endRaw = row?.dataset.redisKeyRaw;
if (!endRaw) return;
drag.active = true;
event.preventDefault();
// Recomputing the range walks the full display order; skip while the pointer
// stays on the same row (visibleRows can hold 100k+ keys after Fetch All).
if (drag.lastEndRaw === endRaw) return;
drag.lastEndRaw = endRaw;
dragSelectionRows(drag.startRaw, endRaw, drag.baseChecked);
}
function onKeyPointerUp() {
if (dragSelection.value?.active) suppressNextRowClick.value = true;
dragSelection.value = null;
}
function onKeyPointerCancel() {
if (dragSelection.value?.active) suppressNextRowClick.value = true;
dragSelection.value = null;
}
function nodeKeyRaws(node: RedisKeyTreeNode): string[] {
return node.kind === "leaf" ? [node.keyRaw] : collectRedisGroupKeyRaws(node);
}
@@ -1558,6 +1622,10 @@ function toggleGroup(groupId: string) {
}
function onRowClick(node: RedisKeyTreeNode, event?: MouseEvent) {
if (suppressNextRowClick.value) {
suppressNextRowClick.value = false;
return;
}
if (event && !selectionBusy.value && (event.shiftKey || event.ctrlKey || event.metaKey)) {
toggleNodeCheck(node, event);
if (node.kind === "leaf") {
@@ -3059,6 +3127,9 @@ function onCommandInputKeydown(event: KeyboardEvent) {
}
onMounted(async () => {
window.addEventListener("pointermove", onKeyPointerMove);
window.addEventListener("pointerup", onKeyPointerUp);
window.addEventListener("pointercancel", onKeyPointerCancel);
resumeRedisBrowserBackgroundWork();
void autofocusSearchOnce();
try {
@@ -3092,6 +3163,9 @@ onActivated(async () => {
onDeactivated(pauseRedisBrowserBackgroundWork);
onUnmounted(() => {
window.removeEventListener("pointermove", onKeyPointerMove);
window.removeEventListener("pointerup", onKeyPointerUp);
window.removeEventListener("pointercancel", onKeyPointerCancel);
pauseRedisBrowserBackgroundWork();
if (redisInfiniteScrollFrame) cancelAnimationFrame(redisInfiniteScrollFrame);
redisInfiniteScrollFrame = 0;
@@ -3429,6 +3503,7 @@ defineExpose({ focusSearch, insertCommand, executeCommand: executeAiCommand });
@scroll="onRedisKeyScroll"
@resize="maybeAutoLoadMoreRedisKeys"
@select="(key) => onRowClick(redisKeyToFlatTreeRow(key, db).node)"
@pointerdown="(key, event) => onKeyPointerDown(redisKeyToFlatTreeRow(key, db).node, event)"
@check="(key, event) => toggleNodeCheck(redisKeyToFlatTreeRow(key, db).node, event)"
@copy="(key) => copyRedisKeyName(key.key_display)"
@delete="(key, event) => requestKeyDelete(redisKeyToFlatTreeRow(key, db).node, event)"
@@ -3457,6 +3532,7 @@ defineExpose({ focusSearch, insertCommand, executeCommand: executeAiCommand });
<template #default="{ item: row }">
<CustomContextMenu :items="redisKeyContextMenuItems(row.node)" v-slot="{ onContextMenu, isOpen }">
<div
:data-redis-key-raw="row.node.kind === 'leaf' ? row.node.keyRaw : undefined"
class="flex items-center gap-2 border-b px-1.5 text-[13px] cursor-pointer select-none group"
:class="[
isOpen || (row.node.kind === 'leaf' && selectedKeyRaw === row.node.keyRaw) ? 'bg-accent text-accent-foreground' : 'hover:bg-accent/40',
@@ -3464,6 +3540,7 @@ defineExpose({ focusSearch, insertCommand, executeCommand: executeAiCommand });
]"
:style="{ height: '30px' }"
@click="onRowClick(row.node, $event)"
@pointerdown="onKeyPointerDown(row.node, $event)"
@contextmenu="(event) => onRedisRowContextMenu(event, row.node, onContextMenu)"
>
<div class="min-w-0 flex flex-1 items-center gap-1 overflow-hidden" :style="{ paddingLeft: `${4 + row.depth * 10}px` }">