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( "", diff --git a/packages/studio/src/hooks/useClipboard.source.test.ts b/packages/studio/src/hooks/useClipboard.source.test.ts index b67a5e5032..11ecc08447 100644 --- a/packages/studio/src/hooks/useClipboard.source.test.ts +++ b/packages/studio/src/hooks/useClipboard.source.test.ts @@ -2,11 +2,12 @@ import React, { act } from "react"; import { createRoot, type Root } from "react-dom/client"; import { ensureHfIds } from "@hyperframes/parsers/hf-ids"; -import { afterEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { useClipboard } from "./useClipboard"; import { usePlayerStore, type TimelineElement } from "../player"; import type { DomEditSelection } from "../components/editor/domEditing"; import { serializeStudioFileMutations } from "../utils/studioFileMutationCoordinator"; +import { setCompositionSourceMap } from "../components/editor/domEditingDom"; // Saved without hf-ids and with no element id: the preview stamps ids only in memory. const SAVED = ` @@ -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 => { - const paths = targets.elements.map((el) => el.sourceFile || activeCompPath || "index.html"); + const paths = targets.elements.map((el) => copiedFile(el.sourceFile, activeCompPath)); const markup = await readSavedMarkup((path) => readSaved(path, pid), paths, targets.lives); return targets.elements.map((element, index) => ({ ...(markup[index] as MarkedMarkup), @@ -348,13 +349,12 @@ export function useClipboard({ findSelectedClips(selected, doc), ); const copy = (targets: { elements: TimelineElement[]; lives: Element[] }) => { - const sourceFile = targets.elements[0]?.sourceFile || activeCompPath || "index.html"; return readClips(targets, pid).then((clips): ClipboardPayload => { showToast(clips.length > 1 ? `Copied ${clips.length} clips` : "Copied clip", "info"); return { kind: "timeline-clip", clips, - sourceFile, + sourceFile: copiedFile(targets.elements[0]?.sourceFile, activeCompPath), projectId: pid ?? undefined, copiedInComposition: activeCompPath || "index.html", }; @@ -370,17 +370,17 @@ export function useClipboard({ const live = doc && findElementForSelection(doc, domSelection, activeCompPath); return live ?? { refused: "Unable to copy this element." }; }); - const sourceFile = domSelection.sourceFile || activeCompPath || "index.html"; - const copy = (live: Element) => - readSaved(sourceFile, pid).then((content): ClipboardPayload => { - showToast("Copied element", "info"); - return { - kind: "dom-element", - ...savedMarkupElseLive(parseSavedSource(content), live, sourceFile), - sourceFile, - projectId: pid ?? undefined, - }; - }); + const copy = async (live: Element): Promise => { + const sourceFile = copiedFile(domSelection.sourceFile, activeCompPath); + const content = await readSaved(sourceFile, pid); + showToast("Copied element", "info"); + return { + kind: "dom-element", + ...savedMarkupElseLive(parseSavedSource(content), live, sourceFile), + sourceFile, + projectId: pid ?? undefined, + }; + }; return copyFound(found, copy, showToast); }, [activeCompPath, previewIframeRef, readSaved, showToast], @@ -492,8 +492,7 @@ export function useClipboard({ if (!targets || !pid || projectIdRef.current !== pid) return false; const { elements } = targets; - const pathOf = (el: TimelineElement) => el.sourceFile || activeCompPath || "index.html"; - const targetPath = pathOf(elements[0]!); + const pathOf = (el: TimelineElement) => knownCompositionFile(el.sourceFile, activeCompPath); // The copy lands in targetPath's own clock, so anchor and lane check are local to it. const anchorTime = toAuthoredStart( elements[0]!, @@ -504,6 +503,7 @@ export function useClipboard({ try { const clips = await readClips(targets, pid); + const targetPath = copiedFile(elements[0]!.sourceFile, activeCompPath); let ids: string[] = []; const duplicate = (originalContent: string) => { const liveElements = usePlayerStore diff --git a/packages/studio/src/utils/clipboardMarkup.ts b/packages/studio/src/utils/clipboardMarkup.ts index b22157ec4f..4513ce21d3 100644 --- a/packages/studio/src/utils/clipboardMarkup.ts +++ b/packages/studio/src/utils/clipboardMarkup.ts @@ -1,4 +1,14 @@ import { markupOutsideAllowlist } from "@hyperframes/core/html-attr-safety"; +import { knownCompositionFile } from "../components/editor/domEditingDom"; + +export function copiedFile( + claimed: string | undefined, + activeCompositionPath: string | null, +): string { + const file = knownCompositionFile(claimed, activeCompositionPath); + if (file) return file; + throw new Error("Couldn't use this element: its file isn't one of this project's compositions."); +} /** Every clipboard write: only markup Studio itself read from the file it writes to skips the allowlist. */ export function refuseUnlistedMarkup(