Dashboard editor: copy and paste widgets across dashboards
Copy widget in the widget panel or mod+c keeps one widget in localStorage; a paste button in the dock and mod+v insert it below the other widgets with a fresh id. Placement is shared with adding a widget.
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
|
||||
import { useNavigate } from "@tanstack/react-router"
|
||||
import { Check, ExternalLink, Pencil, Plus, Settings2 } from "lucide-react"
|
||||
import {
|
||||
Check,
|
||||
ClipboardPaste,
|
||||
ExternalLink,
|
||||
Pencil,
|
||||
Plus,
|
||||
Settings2,
|
||||
} from "lucide-react"
|
||||
import { motion, Reorder } from "motion/react"
|
||||
import { type CSSProperties, useEffect, useRef, useState } from "react"
|
||||
import {
|
||||
@@ -38,6 +45,7 @@ import { appPath } from "@/lib/portal"
|
||||
import { useShortcuts } from "@/lib/shortcuts"
|
||||
import { cn } from "@/lib/utils"
|
||||
import { handleError } from "@/utils"
|
||||
import { copyWidget, useCopiedWidget } from "./clipboard"
|
||||
import { DashboardPalette } from "./DashboardPalette"
|
||||
import {
|
||||
areaOf,
|
||||
@@ -233,6 +241,29 @@ function nextId(dashboard: DashboardDef_Output, type: string): string {
|
||||
return candidate
|
||||
}
|
||||
|
||||
/**
|
||||
* A widget as it joins the dashboard, added or pasted: a fresh id, at the left
|
||||
* edge below everything already there, and no wider than the columns.
|
||||
*/
|
||||
export function appended(
|
||||
dashboard: Dashboard,
|
||||
widget: Omit<WidgetDef, "id" | "layout">,
|
||||
size: { w: number; h: number },
|
||||
): WidgetDef {
|
||||
const columns = columnsOf(dashboard)
|
||||
const bottom = layoutOf(widgetsOf(dashboard), columns).reduce(
|
||||
(lowest, item) => Math.max(lowest, item.y + item.h),
|
||||
0,
|
||||
)
|
||||
return {
|
||||
...widget,
|
||||
id: nextId(dashboard, widget.type),
|
||||
layout: {
|
||||
lg: { x: 0, y: bottom, w: Math.min(columns, size.w), h: size.h },
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Positions for a dashboard nobody has arranged yet.
|
||||
*
|
||||
@@ -376,7 +407,8 @@ export function DashboardEditor({
|
||||
const publish = usePublishDashboard(dashboard.name)
|
||||
const discard = useDiscardDashboardDraft(dashboard.name)
|
||||
const [discardOpen, setDiscardOpen] = useState(false)
|
||||
const { showErrorToast } = useCustomToast()
|
||||
const { showSuccessToast, showErrorToast } = useCustomToast()
|
||||
const copied = useCopiedWidget()
|
||||
const timer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
// The save that is already on its way, so a publish waits for it instead of
|
||||
// going out with the version it is about to replace.
|
||||
@@ -470,24 +502,42 @@ export function DashboardEditor({
|
||||
commit({ ...draft, widgets: next })
|
||||
}
|
||||
|
||||
const addWidget = (type: WidgetKind) => {
|
||||
const id = nextId(draft, type)
|
||||
const bottom = layoutOf(widgets, columns).reduce(
|
||||
(lowest, item) => Math.max(lowest, item.y + item.h),
|
||||
0,
|
||||
)
|
||||
updateWidgets([
|
||||
...widgets,
|
||||
{
|
||||
id,
|
||||
type,
|
||||
title: WIDGET_LABELS[type],
|
||||
layout: { lg: { x: 0, y: bottom, ...WIDGET_SIZES[type] } },
|
||||
config: type === "chart" ? { series: [{}] } : {},
|
||||
},
|
||||
])
|
||||
/** Put a widget on the dashboard and open its settings. */
|
||||
const place = (widget: WidgetDef) => {
|
||||
updateWidgets([...widgets, widget])
|
||||
setSettingsOpen(false)
|
||||
setSelected(id)
|
||||
setSelected(widget.id)
|
||||
}
|
||||
|
||||
const addWidget = (type: WidgetKind) =>
|
||||
place(
|
||||
appended(
|
||||
draft,
|
||||
{
|
||||
type,
|
||||
title: WIDGET_LABELS[type],
|
||||
config: type === "chart" ? { series: [{}] } : {},
|
||||
},
|
||||
WIDGET_SIZES[type],
|
||||
),
|
||||
)
|
||||
|
||||
const copy = (widget: WidgetDef) => {
|
||||
if (copyWidget(widget)) showSuccessToast("Widget copied")
|
||||
else showErrorToast("The browser would not store the copy.")
|
||||
}
|
||||
|
||||
/** The copied widget again, at the size it had where it was copied. */
|
||||
const paste = () => {
|
||||
if (!copied) return
|
||||
const { w = 3, h = 2 } = placement(copied)
|
||||
place(
|
||||
appended(
|
||||
draft,
|
||||
{ type: copied.type, title: copied.title, config: copied.config },
|
||||
{ w, h },
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
const openSettings = () => {
|
||||
@@ -503,6 +553,15 @@ export function DashboardEditor({
|
||||
"mod+z": () => (edit ? step(true) : false),
|
||||
"mod+shift+z": () => (edit ? step(false) : false),
|
||||
"mod+p": () => (edit ? setPaletteOpen((open) => !open) : false),
|
||||
// Selected text wins, so a native copy still works on the canvas.
|
||||
"mod+c": () => {
|
||||
const selection = document.getSelection()
|
||||
if (selection && !selection.isCollapsed) return false
|
||||
const widget = widgets.find((other) => other.id === selected)
|
||||
if (!edit || !widget) return false
|
||||
copy(widget)
|
||||
},
|
||||
"mod+v": () => (edit && copied ? paste() : false),
|
||||
// An edit still waiting for its autosave counts: publishing flushes it.
|
||||
"mod+s": () => {
|
||||
if (!edit) return false
|
||||
@@ -850,6 +909,26 @@ export function DashboardEditor({
|
||||
<TooltipContent>Add a widget (⌘P)</TooltipContent>
|
||||
</Tooltip>
|
||||
|
||||
{copied ? (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon-touch"
|
||||
className="text-muted-foreground"
|
||||
onClick={paste}
|
||||
aria-label={`Paste ${copied.title || WIDGET_LABELS[copied.type]}`}
|
||||
data-testid="paste-widget"
|
||||
>
|
||||
<ClipboardPaste />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
Paste {copied.title || WIDGET_LABELS[copied.type]} (⌘V)
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
) : null}
|
||||
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
@@ -946,6 +1025,7 @@ export function DashboardEditor({
|
||||
widget={active}
|
||||
round={round}
|
||||
onChange={(changes) => active && patch(active.id, changes)}
|
||||
onCopy={() => active && copy(active)}
|
||||
onDelete={() => {
|
||||
if (!active) return
|
||||
updateWidgets(widgets.filter((other) => other.id !== active.id))
|
||||
|
||||
Reference in New Issue
Block a user