diff --git a/packages/studio/src/components/editor/domEditingDom.ts b/packages/studio/src/components/editor/domEditingDom.ts index b6a9775cec..77663712cb 100644 --- a/packages/studio/src/components/editor/domEditingDom.ts +++ b/packages/studio/src/components/editor/domEditingDom.ts @@ -102,6 +102,18 @@ export function getCompositionSourceMapRevision(): number { return compositionSourceMapRevision; } +export function knownCompositionFile( + claimed: string | undefined, + activeCompositionPath: string | null, +): string | null { + const file = claimed || activeCompositionPath || "index.html"; + const known = + file === "index.html" || + file === activeCompositionPath || + [...compositionSourceMap.values()].includes(file); + return known ? file : null; +} + /** The source file `source` names (see `EditingElementSource`), with this project's composition map. */ export function resolveElementSourceFile( source: EditingElementSource, diff --git a/packages/studio/src/hooks/useClipboard.sealed.test.ts b/packages/studio/src/hooks/useClipboard.sealed.test.ts index f243dbb13a..4023cad5c5 100644 --- a/packages/studio/src/hooks/useClipboard.sealed.test.ts +++ b/packages/studio/src/hooks/useClipboard.sealed.test.ts @@ -7,6 +7,7 @@ import { useClipboard } from "./useClipboard"; import { usePlayerStore, type TimelineElement } from "../player"; import type { DomEditSelection } from "../components/editor/domEditing"; import { sealedPreviewFrame } from "../player/lib/sealedPreviewFrame.test-helpers"; +import { setCompositionSourceMap } from "../components/editor/domEditingDom"; (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; @@ -21,6 +22,7 @@ afterEach(() => { act(() => root?.unmount()); root = null; vi.unstubAllGlobals(); + setCompositionSourceMap(new Map()); usePlayerStore.getState().reset(); document.body.innerHTML = ""; }); @@ -186,7 +188,8 @@ it("checks a saved element read from another file before pasting it into this on selector: "#go", sourceFile: "compositions/recv.html", } as DomEditSelection; - // The frame shows one of the person's own clips under the received element's file and id. + // A received sub-composition of this project, and one of the person's own clips shown under its file and id. + setCompositionSourceMap(new Map([["recv", "compositions/recv.html"]])); const relabelled = '
'; const { clipboard, writes, toasts } = sealedClipboard(selection, relabelled, SAVED, { @@ -198,6 +201,31 @@ it("checks a saved element read from another file before pasting it into this on expect(toasts.at(-1)).toMatch(/^Couldn't paste/); }); +// fails without the fix: the file the frame named for a clip or a selection was read and written as given. +it("copies and duplicates nothing from a file Studio does not know as one of this project's compositions", async () => { + const elsewhere = { ...clipOf("go", "button", 1, 2), sourceFile: "compositions/elsewhere.html" }; + const shown = + ''; + usePlayerStore.setState({ elements: [elsewhere] }); + selectClip("go"); + const duplicated = sealedClipboard(null, shown); + expect(await duplicated.clipboard().handleDuplicate()).toBe(false); + expect(duplicated.writes).toEqual([]); + act(() => root?.unmount()); + root = null; + + usePlayerStore.setState({ selectedElementId: null, selectedElementIds: new Set() }); + const selection = { + id: "go", + selector: "#go", + sourceFile: "compositions/elsewhere.html", + } as DomEditSelection; + const copied = sealedClipboard(selection, shown); + copied.clipboard().handleCopy(); + await copied.clipboard().handlePaste(); + expect(copied.writes).toEqual([]); +}); + it("pastes and duplicates a saved element with its own handler, as saved", async () => { const saved = SAVED.replace( " @@ -43,9 +44,12 @@ const TITLE: TimelineElement = { }; let root: Root | null = null; +// What Studio reads from the project's index.html before any copy. +beforeEach(() => setCompositionSourceMap(new Map([["sub", "compositions/sub.html"]]))); afterEach(() => { act(() => root?.unmount()); root = null; + setCompositionSourceMap(new Map()); vi.unstubAllGlobals(); document.body.innerHTML = ""; }); diff --git a/packages/studio/src/hooks/useClipboard.ts b/packages/studio/src/hooks/useClipboard.ts index 82fc3a5d31..443d803ec2 100644 --- a/packages/studio/src/hooks/useClipboard.ts +++ b/packages/studio/src/hooks/useClipboard.ts @@ -29,7 +29,8 @@ import { savedMarkupElseLive, } from "../utils/authoredSource"; import { serializeStudioFileMutations } from "../utils/studioFileMutationCoordinator"; -import { refuseUnlistedMarkup } from "../utils/clipboardMarkup"; +import { copiedFile, refuseUnlistedMarkup } from "../utils/clipboardMarkup"; +import { knownCompositionFile } from "../components/editor/domEditingDom"; type InPlace = { inPlace?: boolean }; @@ -325,7 +326,7 @@ export function useClipboard({ targets: { elements: TimelineElement[]; lives: Element[] }, pid: string | null, ): Promise