#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
feat(pageEditor): add per-page duplicate action and Ctrl+D shortcut (#8559)
brios2fe512a994fd
diff --git a/frontend/editor/public/locales/en-US/translation.toml b/frontend/editor/public/locales/en-US/translation.tomlindex 8967130fa0..b8acd8e86d 100644--- a/frontend/editor/public/locales/en-US/translation.toml+++ b/frontend/editor/public/locales/en-US/translation.toml@@ -6852,6 +6852,9 @@ unsaved = "These files have unsaved changes."[pageTracks.delete]page = "Delete page"+[pageTracks.duplicate]+page = "Duplicate page"+[pageTracks.empty]body = "Add PDFs to expand them into editable page tracks"title = "No PDF files loaded"diff --git a/frontend/editor/src/core/components/pageTracks/PageTracks.module.css b/frontend/editor/src/core/components/pageTracks/PageTracks.module.cssindex 923a8da56b..4c5755374b 100644--- a/frontend/editor/src/core/components/pageTracks/PageTracks.module.css+++ b/frontend/editor/src/core/components/pageTracks/PageTracks.module.css@@ -161,6 +161,16 @@0 0 0 5px color-mix(in srgb, var(--c-primary) 22%, transparent);}+/* While the action pill (or the checkbox/shift chips) is up, the tile must+ outrank the invisible gap handles (z-index 5): the pill is wider than the+ tile and overhangs into the gaps, and the z-index inside it cannot climb out+ of the thumb box's hover-transform stacking context. */+.tile:hover,+.tile:focus-visible,+.tile:has(:focus-visible) {+ z-index: 6;+}+.tileDragging {opacity: 0.35;}@@ -224,6 +234,14 @@box-shadow: var(--pt-selection-ring);}+/* Selection owns the ring states, so keyboard focus needs its own cue on the+ tile: without it a tabbed-to tile and an unfocused one look identical. */+.tile:focus-visible {+ outline: 2px solid var(--c-primary);+ outline-offset: 2px;+ border-radius: 0.5rem;+}+/** A quarter turn is applied after layout, so the pre-rotation box has to be* constrained by the OPPOSITE axis or the rotated page overflows and clips.@@ -322,7 +340,8 @@}.tile:hover .tileCheckbox,-.tile:focus-within .tileCheckbox,+.tile:focus-visible .tileCheckbox,+.tile:has(:focus-visible) .tileCheckbox,.tileSelected .tileCheckbox {opacity: 1;}@@ -354,7 +373,8 @@}.tile:hover .shiftButton,-.tile:focus-within .shiftButton,+.tile:focus-visible .shiftButton,+.tile:has(:focus-visible) .shiftButton,.shiftButton[data-force-visible="true"] {opacity: 1;pointer-events: auto;@@ -427,6 +447,9 @@* own transform, which centres it) so the controls keep their proportion to the* tile at every zoom instead of swamping a shrunken page. Anchored bottom-centre* so it stays put over the page while it scales.+ *+ * Wider than the tile, it overhangs the gaps on both sides; see the hovered+ * tile's z-index for how those clicks stay reachable.*/.tile .pagePill {gap: 0.25rem;@@ -436,7 +459,10 @@transform-origin: center bottom;}-/* CSS-hover driven visibility, matching the file cards in Active Files. */+/* CSS-hover driven visibility, matching the file cards in Active Files. The+ rows must not stay pinned after a pointer click: mouse clicks set focus but+ not :focus-visible, so a clicked action hides again once the pointer leaves,+ while keyboard focus keeps the row (and its buttons) reachable. */.tile [data-hover-action-menu-mode="cssHover"][data-force-visible="false"] {opacity: 0;pointer-events: none;@@ -444,7 +470,9 @@.tile:hover[data-hover-action-menu-mode="cssHover"][data-force-visible="false"],-.tile:focus-within+.tile:focus-visible+ [data-hover-action-menu-mode="cssHover"][data-force-visible="false"],+.tile:has(:focus-visible)[data-hover-action-menu-mode="cssHover"][data-force-visible="false"] {opacity: 1;pointer-events: auto;diff --git a/frontend/editor/src/core/components/pageTracks/PageTracks.tsx b/frontend/editor/src/core/components/pageTracks/PageTracks.tsxindex 610e328ee3..0a904bb921 100644--- a/frontend/editor/src/core/components/pageTracks/PageTracks.tsx+++ b/frontend/editor/src/core/components/pageTracks/PageTracks.tsx@@ -426,13 +426,14 @@ export default function PageTracks() {[dispatch, selection.selectedIds],);+ const duplicatePages = useCallback(+ (pageIds: string[]) => dispatch({ type: "duplicate", pageIds }),+ [dispatch],+ );+const duplicateSelection = useCallback(- () =>- dispatch({- type: "duplicate",- pageIds: Array.from(selection.selectedIds),- }),- [dispatch, selection.selectedIds],+ () => duplicatePages(Array.from(selection.selectedIds)),+ [duplicatePages, selection.selectedIds],);const splitAfterSelection = useCallback(@@ -1064,6 +1065,7 @@ export default function PageTracks() {onInsertBlank={insertBlank}onRotate={rotatePages}onDelete={deletePages}+ onDuplicate={duplicatePages}onShiftPage={shiftPage}onClose={setCloseRequest}/>diff --git a/frontend/editor/src/core/components/pageTracks/TrackPageTile.tsx b/frontend/editor/src/core/components/pageTracks/TrackPageTile.tsxindex 7616ae379c..7c2659d431 100644--- a/frontend/editor/src/core/components/pageTracks/TrackPageTile.tsx+++ b/frontend/editor/src/core/components/pageTracks/TrackPageTile.tsx@@ -48,6 +48,7 @@ export interface TrackPageTileProps {onViewPage: (pageId: string) => void;onRotate: (pageIds: string[], delta: number) => void;onDelete: (pageIds: string[]) => void;+ onDuplicate: (pageIds: string[]) => void;/** Swaps the page with its neighbour: -1 left, 1 right. */onShift: (pageId: string, by: -1 | 1) => void;}@@ -107,6 +108,7 @@ function TrackPageTileImpl({onViewPage,onRotate,onDelete,+ onDuplicate,onShift,}: TrackPageTileProps) {const { t } = useTranslation();@@ -172,6 +174,15 @@ function TrackPageTileImpl({onRotate([page.id], 90);},},+ {+ id: "duplicate",+ icon: <Icon name="copy-plus" size={16} />,+ label: t("pageTracks.duplicate.page", "Duplicate page"),+ onClick: (event) => {+ event.stopPropagation();+ onDuplicate([page.id]);+ },+ },{id: "delete",icon: <Icon name="trash" size={16} />,@@ -183,7 +194,7 @@ function TrackPageTileImpl({},},],- [t, onViewPage, onRotate, onDelete, page.id],+ [t, onViewPage, onRotate, onDelete, onDuplicate, page.id],);return (diff --git a/frontend/editor/src/core/components/pageTracks/TrackRow.tsx b/frontend/editor/src/core/components/pageTracks/TrackRow.tsxindex 12c42f1dab..5698609d4d 100644--- a/frontend/editor/src/core/components/pageTracks/TrackRow.tsx+++ b/frontend/editor/src/core/components/pageTracks/TrackRow.tsx@@ -138,6 +138,7 @@ export interface TrackRowProps {onInsertBlank: (fileId: FileId, beforePageId: string | null) => void;onRotate: (pageIds: string[], delta: number) => void;onDelete: (pageIds: string[]) => void;+ onDuplicate: (pageIds: string[]) => void;onShiftPage: (pageId: string, by: -1 | 1) => void;/** Asks to close the file; the parent confirms before anything closes. */onClose: (fileId: FileId) => void;@@ -168,6 +169,7 @@ function TrackRowImpl({onInsertBlank,onRotate,onDelete,+ onDuplicate,onShiftPage,onClose,}: TrackRowProps) {@@ -566,6 +568,7 @@ function TrackRowImpl({onViewPage={handleViewPage}onRotate={onRotate}onDelete={onDelete}+ onDuplicate={onDuplicate}onShift={onShiftPage}/><GapHandle@@ -634,6 +637,7 @@ function TrackRowImpl({onViewPage={handleViewPage}onRotate={onRotate}onDelete={onDelete}+ onDuplicate={onDuplicate}onShift={onShiftPage}/><GapHandlediff --git a/frontend/editor/src/core/components/pageTracks/hooks/usePageTracksShortcuts.test.ts b/frontend/editor/src/core/components/pageTracks/hooks/usePageTracksShortcuts.test.tsindex 85069fde2b..bc4016f3fd 100644--- a/frontend/editor/src/core/components/pageTracks/hooks/usePageTracksShortcuts.test.ts+++ b/frontend/editor/src/core/components/pageTracks/hooks/usePageTracksShortcuts.test.ts@@ -51,6 +51,31 @@ describe("usePageTracksShortcuts", () => {});});+ it("duplicates the selection with Ctrl+D", () => {+ const { dispatch } = setup(["a1", "a2"]);+ press("d", { ctrlKey: true });+ expect(dispatch).toHaveBeenCalledWith({+ type: "duplicate",+ pageIds: ["a1", "a2"],+ });+ });++ it("duplicates the selection with Meta+D", () => {+ const { dispatch } = setup(["a1", "a2"]);+ press("d", { metaKey: true });+ expect(dispatch).toHaveBeenCalledWith({+ type: "duplicate",+ pageIds: ["a1", "a2"],+ });+ });++ it("leaves Ctrl+D to the browser when nothing is selected", () => {+ const { dispatch } = setup([]);+ const event = press("d", { ctrlKey: true });+ expect(dispatch).not.toHaveBeenCalled();+ expect(event.defaultPrevented).toBe(false);+ });+it("leaves keys typed into a field or a dialog alone", () => {const { dispatch } = setup(["a1"]);const input = document.createElement("input");diff --git a/frontend/editor/src/core/components/pageTracks/hooks/usePageTracksShortcuts.ts b/frontend/editor/src/core/components/pageTracks/hooks/usePageTracksShortcuts.tsindex a3eed07b48..d79d866f5a 100644--- a/frontend/editor/src/core/components/pageTracks/hooks/usePageTracksShortcuts.ts+++ b/frontend/editor/src/core/components/pageTracks/hooks/usePageTracksShortcuts.ts@@ -45,6 +45,14 @@ export function usePageTracksShortcuts(params: PageTracksShortcutsParams) {dispatch({ type: "redo" });return;}+ if (command && key === "d") {+ // Without a selection there is nothing to duplicate, so the browser+ // keeps its own Ctrl+D.+ if (selectedIds.size === 0) return;+ event.preventDefault();+ dispatch({ type: "duplicate", pageIds: [...selectedIds] });+ return;+ }if (!command && (event.key === "Delete" || event.key === "Backspace")) {if (selectedIds.size === 0) return;event.preventDefault();diff --git a/frontend/editor/src/core/components/pageTracks/trackWorkspaceReducer.test.ts b/frontend/editor/src/core/components/pageTracks/trackWorkspaceReducer.test.tsindex 8319d4d332..3a2f2981eb 100644--- a/frontend/editor/src/core/components/pageTracks/trackWorkspaceReducer.test.ts+++ b/frontend/editor/src/core/components/pageTracks/trackWorkspaceReducer.test.ts@@ -553,6 +553,24 @@ describe("trackEditorReducer insertBlank", () => {expect(ids(state, B)).toEqual([`${B}:1`, `${B}:2`]);});+ it("keeps a bulk duplicate in selection order after the block", () => {+ let state = twoTracks();+ const selected = pagesOf(state, A).slice(0, 3);+ state = trackEditorReducer(state, {+ type: "duplicate",+ pageIds: selected.map((page) => page.id),+ });++ expect(ids(state, A)).toEqual([+ `${A}:1`,+ `${A}:2`,+ `${A}:3`,+ `${A}:1`,+ `${A}:2`,+ `${A}:3`,+ ]);+ });+it("puts a copy after each given page, across tracks, as one undo step", () => {let state = twoTracks();const [a1, a2] = pagesOf(state, A);@@ -570,8 +588,8 @@ describe("trackEditorReducer insertBlank", () => {expect(ids(state, A)).toEqual([`${A}:1`,"blank",- "blank",`${A}:2`,+ "blank",`${A}:2`,`${A}:3`,]);diff --git a/frontend/editor/src/core/components/pageTracks/trackWorkspaceReducer.ts b/frontend/editor/src/core/components/pageTracks/trackWorkspaceReducer.tsindex 17eef0118a..88a5e2d60e 100644--- a/frontend/editor/src/core/components/pageTracks/trackWorkspaceReducer.ts+++ b/frontend/editor/src/core/components/pageTracks/trackWorkspaceReducer.ts@@ -688,10 +688,21 @@ export function trackEditorReducer(const duplicated = new Set(action.pageIds);let seq = state.seq;const next = mapTracks(state.present, (pages) => {- if (!pages.some((p) => duplicated.has(p.id))) return pages;- return pages.flatMap((p) =>- duplicated.has(p.id) ? [p, { ...p, id: `tp-${seq++}` }] : [p],- );+ const copies = pages+ .filter((p) => duplicated.has(p.id))+ .map((p) => ({ ...p, id: `tp-${seq++}` }));+ if (copies.length === 0) return pages;++ // A bulk duplicate keeps the selected pages in document order and+ // inserts the whole block after the last selected page; a single page+ // still lands immediately after its source.+ let lastSelected = -1;+ pages.forEach((page, index) => {+ if (duplicated.has(page.id)) lastSelected = index;+ });+ const result = [...pages];+ result.splice(lastSelected + 1, 0, ...copies);+ return result;});if (next === state.present) return state;return { ...withEdit(state, next), seq };diff --git a/frontend/editor/src/core/tests/stubbed/page-editor-duplicate.spec.ts b/frontend/editor/src/core/tests/stubbed/page-editor-duplicate.spec.tsnew file mode 100644index 0000000000..0e5408d4a9--- /dev/null+++ b/frontend/editor/src/core/tests/stubbed/page-editor-duplicate.spec.ts@@ -0,0 +1,161 @@+import path from "node:path";++import { test, expect } from "@app/tests/helpers/stub-test-base";+import { uploadFiles, dismissTourTooltip } from "@app/tests/helpers/ui-helpers";++/**+ * Extended coverage for the Page Editor's duplicate affordances (issue #5443):+ * the per-tile hover action, the toolbar action for a multi-selection, the+ * Ctrl+D shortcut, track isolation, and that a save persists duplicates.+ */++const ROTATED_PDF = path.join(+ import.meta.dirname,+ "../test-fixtures/rotated-pages.pdf",+);+const SAMPLE_PDF = path.join(+ import.meta.dirname,+ "../test-fixtures/sample.pdf",+);++test.use({ autoGoto: false, viewport: { width: 1600, height: 900 } });++function track(page: import("@playwright/test").Page, name: string) {+ return page.locator(`section[aria-label="${name}"]`);+}++function tile(+ page: import("@playwright/test").Page,+ name: string,+ index: number,+) {+ return track(page, name).locator("[data-page-id]").nth(index);+}++async function switchView(+ page: import("@playwright/test").Page,+ label: string,+) {+ await page+ .locator('[data-tour="view-switcher"]')+ .first()+ .locator(".mantine-SegmentedControl-label", { hasText: label })+ .click();+}++/** Intrinsic rotations as the editor shows them, in track order. */+async function readRotations(+ lane: import("@playwright/test").Locator,+ expectedCount: number,+) {+ const imgs = lane.locator("[data-page-id] img[data-original-rotation]");+ await expect(imgs).toHaveCount(expectedCount, { timeout: 60_000 });+ return imgs.evaluateAll((nodes) =>+ nodes.map((node) => Number(node.getAttribute("data-original-rotation"))),+ );+}++async function openPageEditor(page: import("@playwright/test").Page) {+ await page.goto("/editor", {+ waitUntil: "domcontentloaded",+ timeout: 120_000,+ });+ await uploadFiles(page, [ROTATED_PDF, SAMPLE_PDF]);+ await dismissTourTooltip(page);+ await switchView(page, "Page Editor");+ await expect(page.getByTestId("page-tracks")).toBeVisible({+ timeout: 30_000,+ });+ await expect(+ track(page, "rotated-pages.pdf").locator("[data-page-id]"),+ ).toHaveCount(4, { timeout: 30_000 });+}++test.describe("Page editor duplicate", () => {+ test("the tile hover action duplicates one page right after itself", async ({+ page,+ }) => {+ await openPageEditor(page);+ const rotated = track(page, "rotated-pages.pdf");+ const sample = track(page, "sample.pdf");+ // Rotations: 0, 90, 270, 180 — position proof for the insert.+ expect(await readRotations(rotated, 4)).toEqual([0, 90, 270, 180]);++ const third = tile(page, "rotated-pages.pdf", 2);+ await third.hover();+ await third.getByRole("button", { name: "Duplicate page" }).click();++ await expect(rotated.locator("[data-page-id]")).toHaveCount(5);+ // The copy lands after page 3 and carries its rotation; page 4 follows.+ expect(await readRotations(rotated, 5)).toEqual([0, 90, 270, 270, 180]);+ await expect(rotated).toContainText("edited");+ // The other track is untouched.+ await expect(sample.locator("[data-page-id]")).toHaveCount(1);++ await page.getByRole("button", { name: "Undo" }).click();+ await expect(rotated.locator("[data-page-id]")).toHaveCount(4);+ await page.getByRole("button", { name: "Redo" }).click();+ await expect(rotated.locator("[data-page-id]")).toHaveCount(5);+ });++ test("the toolbar action duplicates a multi-selection and Ctrl+D the selection", async ({+ page,+ }) => {+ await openPageEditor(page);+ const rotated = track(page, "rotated-pages.pdf");++ await tile(page, "rotated-pages.pdf", 0).click();+ await tile(page, "rotated-pages.pdf", 1).click({ modifiers: ["Shift"] });+ await page+ .getByRole("button", { name: "Duplicate selected pages" })+ .click();+ await expect(rotated.locator("[data-page-id]")).toHaveCount(6);+ // The selected block is copied after its last page: [0,90,0,90,270,180].+ expect(await readRotations(rotated, 6)).toEqual([0, 90, 0, 90, 270, 180]);++ await page.getByRole("button", { name: "Undo" }).click();+ await expect(rotated.locator("[data-page-id]")).toHaveCount(4);++ await tile(page, "rotated-pages.pdf", 1).click();+ await page.keyboard.press("Control+d");+ await expect(rotated.locator("[data-page-id]")).toHaveCount(5);+ });++ test("a save persists a duplicated page", async ({ page }) => {+ await openPageEditor(page);+ const rotated = track(page, "rotated-pages.pdf");++ const first = tile(page, "rotated-pages.pdf", 0);+ await first.hover();+ await first.getByRole("button", { name: "Duplicate page" }).click();+ await expect(rotated.locator("[data-page-id]")).toHaveCount(5);++ await page+ .getByRole("button", { name: "Save changes to all files" })+ .click();++ const saved = track(page, "rotated-pages.pdf");+ await expect(saved).toContainText("v2", { timeout: 90_000 });+ await expect(saved).not.toContainText("edited");+ await expect(saved.locator("[data-page-id]")).toHaveCount(5);+ // The duplicate of page 1 keeps its content on the way through the save.+ expect(await readRotations(saved, 5)).toEqual([0, 0, 90, 270, 180]);+ });++ test("duplicating a blank page keeps it blank", async ({ page }) => {+ await openPageEditor(page);+ const rotated = track(page, "rotated-pages.pdf");++ await tile(page, "rotated-pages.pdf", 0).click();+ await page+ .getByRole("button", { name: "Insert blank pages after selected pages" })+ .click();+ await expect(rotated.locator("[data-page-id]")).toHaveCount(5);++ const blank = tile(page, "rotated-pages.pdf", 1);+ await blank.hover();+ await blank.getByRole("button", { name: "Duplicate page" }).click();+ await expect(rotated.locator("[data-page-id]")).toHaveCount(6);+ await expect(blank.locator("canvas, svg, img").first()).toBeVisible();+ });+});