diff --git a/packages/webui/src/components/pages/dashboard-grid.tsx b/packages/webui/src/components/pages/dashboard-grid.tsx index 9a94261a..5edb5cc9 100644 --- a/packages/webui/src/components/pages/dashboard-grid.tsx +++ b/packages/webui/src/components/pages/dashboard-grid.tsx @@ -1,7 +1,7 @@ import { useLayoutEffect, useRef, useState, type ReactNode } from 'react'; import { createPortal } from 'react-dom'; import { GridStack, type GridStackWidget } from 'gridstack'; -import { Eye, EyeOff, GripVertical, Settings2 } from 'lucide-react'; +import { GripVertical, Settings2 } from 'lucide-react'; import 'gridstack/dist/gridstack.css'; import { GRID_CELL_HEIGHT, GRID_MARGIN, minSizeOf } from '@/lib/dashboard-layout'; import { cn } from '@/lib/utils'; @@ -9,21 +9,31 @@ import type { UiLayoutItem } from '@/types'; export interface GridCoord { id: string; x: number; y: number; w: number; h: number } +/** dataTransfer key carrying the dragged gallery widget id (HTML5 add). */ +export const WIDGET_DRAG_TYPE = 'text/plain'; + interface DashboardGridProps { - /** Blocks to render — visible-only in view mode, ALL (incl. hidden) in edit - * mode so hidden widgets show as re-enableable ghosts. Each has x/y/w/h. */ + /** Visible widgets only — each carries x/y/w/h. Hidden widgets live in the + * gallery, not the grid (Apple-widgets model: presence = shown). */ blocks: UiLayoutItem[]; editing: boolean; /** Column count — 12 on desktop, 1 on narrow screens (auto single-column). */ cols: number; - /** Fired (debounced upstream) after a drag/resize settles. */ + /** + * Fired (debounced upstream) after a drag/resize/remove settles, with the + * grid's current visible set. The consumer derives removal from absence: a + * block that was visible but is no longer in `coords` was dragged out. + */ onChange: (coords: GridCoord[]) => void; renderWidget: (block: UiLayoutItem) => ReactNode; - // ── edit overlay ── - labelFor: (id: string) => string; configurableIds: ReadonlySet; - onToggleVisible: (id: string) => void; onConfigOpen: (id: string) => void; + /** CSS selector for the gallery drop-zone; dragging a card onto it removes + * the widget from the grid (gridstack `removable`). Edit mode only. */ + removableSelector?: string; + /** Add a widget dropped from the gallery at the computed grid cell (HTML5 + * drag-and-drop, driven entirely through React state). */ + onAdd?: (id: string, x: number, y: number) => void; } /** @@ -33,10 +43,15 @@ interface DashboardGridProps { * when the visible widget SET or the edit mode changes — never on a pure * coord change (gridstack already moved the item; the coord round-trips back * through `onChange` → state and matches what gridstack has). + * + * Add/remove are NOT done through gridstack's external drag-in: instead the + * gallery drives React state (add = HTML5 drop here; remove = drag onto the + * gallery via `removable`). Both funnel through the single `onChange` writer, + * which reads the post-mutation grid snapshot — so there's no state race. */ export function DashboardGrid({ blocks, editing, cols, onChange, renderWidget, - labelFor, configurableIds, onToggleVisible, onConfigOpen, + configurableIds, onConfigOpen, removableSelector, onAdd, }: DashboardGridProps) { const elRef = useRef(null); const gridRef = useRef(null); @@ -63,11 +78,9 @@ export function DashboardGrid({ float: true, // free placement — items stay where dropped (no auto-compact) disableDrag: !editing, disableResize: !editing, - // Default handle (.grid-stack-item-content) = the whole card is a drag - // target in edit mode — forgiving + discoverable. The card body is - // pointer-events-none in edit mode so widget controls don't fire, and - // the eye/gear are )} - - - )} - {hidden ? ( -
- {labelFor(b.id)} · 已隐藏 -
- ) : ( -
- {renderWidget(b)} -
+ )} +
+ {renderWidget(b)} +
, target, ); diff --git a/packages/webui/src/components/pages/overview-page.tsx b/packages/webui/src/components/pages/overview-page.tsx index 0b09269b..586d2042 100644 --- a/packages/webui/src/components/pages/overview-page.tsx +++ b/packages/webui/src/components/pages/overview-page.tsx @@ -3,7 +3,8 @@ import { motion, Reorder } from 'motion/react'; import { Link } from '@tanstack/react-router'; import { Activity, ArrowRight, Bell, Cable, Check, Cpu, ExternalLink, Eye, EyeOff, GripVertical, - MemoryStick, MonitorCog, Pencil, PlugZap, RefreshCw, RotateCcw, Server, Settings2, Users, + LayoutGrid, MemoryStick, MonitorCog, Pencil, Plus, PlugZap, RefreshCw, RotateCcw, Server, + Settings2, Users, } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; @@ -21,14 +22,14 @@ import { useTheme } from '@/contexts/ThemeContext'; import { reconcileLayoutItems, useLayout } from '@/contexts/LayoutContext'; import { useMediaQuery } from '@/hooks/use-media-query'; import { - CONFIGURABLE_WIDGETS, GRID_COLS, MOBILE_WIDGET_IDS, STATIC_WIDGET_IDS, mobileHeightOf, + CONFIGURABLE_WIDGETS, GRID_COLS, GRID_WIDGETS, MOBILE_WIDGET_IDS, STATIC_WIDGET_IDS, mobileHeightOf, parseAccountConfig, parseAlertsConfig, parseConnectionsConfig, parseHostConfig, parseLinkConfig, parseNoteConfig, parseSessionsConfig, widgetLabel, type AccountConfig, type AlertsConfig, type ConnectionsConfig, type HostConfig, type LinkConfig, type NoteConfig, type SessionsConfig, } from '@/lib/dashboard-layout'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; -import { DashboardGrid, type GridCoord } from '@/components/pages/dashboard-grid'; +import { DashboardGrid, WIDGET_DRAG_TYPE, type GridCoord } from '@/components/pages/dashboard-grid'; import { AccountConfigForm, AlertsConfigForm, ConnectionsConfigForm, HostConfigForm, LinkConfigForm, LINK_ICON_COMPONENTS, NoteConfigForm, SessionsConfigForm, @@ -62,12 +63,12 @@ export function OverviewPage() { const isWide = useMediaQuery('(min-width: 768px)'); // Free-grid 2D editing is desktop-only; on a phone, editing surfaces a // single-column drag-sort panel (overviewMobile) instead. - const gridEditing = isWide && editing; const [configId, setConfigId] = useState(null); + // The grid shows only visible widgets (Apple-widgets model); hidden ones live + // in the gallery and are dragged in/out to add/remove. const visibleBlocks = useMemo(() => overviewBlocks.filter((b) => b.visible), [overviewBlocks]); - // In edit mode show ALL blocks (hidden ones as re-enableable ghosts); else visible-only. - const gridBlocks = gridEditing ? overviewBlocks : visibleBlocks; + const hiddenBlocks = useMemo(() => overviewBlocks.filter((b) => !b.visible), [overviewBlocks]); // Per-widget config lives on the desktop blocks; mobile widgets reuse it. const configById = useMemo( @@ -81,18 +82,28 @@ export function OverviewPage() { [overviewMobile], ); + // Single writer for the grid: move/resize updates coords; a visible block no + // longer present in the snapshot was dragged out to the gallery → hide it. const onGridChange = (coords: GridCoord[]) => { const cmap = new Map(coords.map((c) => [c.id, c])); setOverviewBlocks( overviewBlocks.map((b) => { + if (!b.visible) return b; // hidden (in the gallery) — leave untouched const c = cmap.get(b.id); - return c ? { ...b, x: c.x, y: c.y, w: c.w, h: c.h } : b; + if (c) return { ...b, x: c.x, y: c.y, w: c.w, h: c.h }; + return { ...b, visible: false }; // was in the grid, now gone → removed }), ); }; - const toggleBlock = (id: string) => - setOverviewBlocks(overviewBlocks.map((b) => (b.id === id ? { ...b, visible: !b.visible } : b))); + // Add a widget dragged from the gallery at the dropped cell (catalogue size). + const addWidget = (id: string, x: number, y: number) => { + const spec = GRID_WIDGETS.find((w) => w.id === id); + setOverviewBlocks(overviewBlocks.map((b) => ( + b.id === id ? { ...b, visible: true, x, y, w: spec?.def.w ?? 3, h: spec?.def.h ?? 2 } : b + ))); + }; + const toggleMobile = (id: string) => setOverviewMobile(mobileItems.map((b) => (b.id === id ? { ...b, visible: !b.visible } : b))); const reorderMobile = (ids: string[]) => { @@ -126,7 +137,7 @@ export function OverviewPage() { {editing && (

{isWide - ? '拖动卡片顶部的「拖动」手柄移动位置、拖右下角缩放;眼睛图标显隐、齿轮改设置。左侧导航也可在编辑态拖动排序。' + ? '拖动卡片移动位置、拖右下角缩放,右上齿轮改设置;把卡片拖回右侧「部件库」即可移除,从部件库拖到画布即可添加。左侧导航也可在编辑态拖动排序。' : '拖动手柄调整卡片顺序,眼睛图标显隐,齿轮改设置。手机为单列布局,与桌面网格各自独立。'}

)} @@ -155,21 +166,30 @@ export function OverviewPage() { )} {isWide ? ( - visibleBlocks.length === 0 && !gridEditing ? ( - - ) : ( - - ) +
+
+ {visibleBlocks.length === 0 ? ( + editing ? ( + + ) : ( + + ) + ) : ( + + )} +
+ {editing &&
) : ( { /* no drag on mobile — order edited via the panel */ }} renderWidget={renderWidget} - labelFor={widgetLabel} configurableIds={CONFIGURABLE_WIDGETS} - onToggleVisible={() => { /* unused in view mode */ }} onConfigOpen={onConfigOpen} /> ); } +// ─────────────── widget gallery (desktop edit mode) ─────────────── +// The right-hand "store": hidden widgets shown as draggable tiles. Dragging a +// tile onto the canvas adds it (HTML5 DnD); dragging a canvas card back onto +// this panel removes it (gridstack `removable`, matched by the .widget-gallery +// class). Edit-mode only, desktop only. +function WidgetGallery({ hidden }: { hidden: UiLayoutItem[] }) { + return ( +
+
+ + 部件库 +
+

+ 拖到左侧画布添加;把卡片拖回此处即可移除。 +

+ {hidden.length === 0 ? ( +
+ 全部部件已添加 +
+ ) : ( +
+ {hidden.map((b) => ( +
{ + e.dataTransfer.setData(WIDGET_DRAG_TYPE, b.id); + e.dataTransfer.effectAllowed = 'copy'; + }} + className="flex cursor-grab items-center gap-2 rounded-lg border bg-card/70 px-2.5 py-2 text-sm transition-colors active:cursor-grabbing hover:border-primary/40 hover:bg-accent/30" + > + + {widgetLabel(b.id)} + +
+ ))} +
+ )} +
+ ); +} + +// Empty-canvas drop target shown in edit mode when no widget is on the grid — +// keeps an HTML5 drop zone so the first widget can still be dragged in. +function DropPlaceholder({ onAdd }: { onAdd: (id: string, x: number, y: number) => void }) { + return ( +
e.preventDefault()} + onDrop={(e) => { + e.preventDefault(); + const id = e.dataTransfer.getData(WIDGET_DRAG_TYPE); + if (id) onAdd(id, 0, 0); + }} + className="flex min-h-60 flex-col items-center justify-center gap-3 rounded-xl border-2 border-dashed py-16 text-center text-muted-foreground" + > + +

从右侧「部件库」拖动部件到这里

+
+ ); +} + // ─────────────── stat tiles (one widget each) ─────────────── function StatTile({