Skip to content

Commit 3d8325e

Browse files
fix(studio): a pasted element keeps the look its id gave the original (#4852)
* fix(studio): a pasted element keeps the look its id gave the original * fix(studio): drop a comment the parameter name already says
1 parent c20934a commit 3d8325e

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎packages/studio/src/hooks/useClipboard.source.test.ts‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,16 @@ afterEach(() => {
5252

5353
// The preview page: the saved markup with the preview's in-memory ids, a sub-composition
5454
// mounted inline, and the runtime's hide on both clips.
55-
function mountPreview(sub: string, bundle: (host: Element) => void): HTMLIFrameElement {
55+
function mountPreview(
56+
sub: string,
57+
bundle: (host: Element) => void,
58+
saved = SAVED,
59+
): HTMLIFrameElement {
5660
const iframe = document.createElement("iframe");
5761
document.body.appendChild(iframe);
5862
const doc = iframe.contentDocument as Document;
5963
doc.open();
60-
doc.write(ensureHfIds(SAVED));
64+
doc.write(ensureHfIds(saved));
6165
doc.close();
6266
const parsed = new DOMParser().parseFromString(ensureHfIds(sub), "text/html");
6367
const subRoot = (parsed.querySelector("template") as HTMLTemplateElement).content
@@ -93,14 +97,15 @@ function mountClipboard(
9397
domSelection: DomEditSelection | null = null,
9498
sub = SUB,
9599
bundle: (host: Element) => void = () => {},
100+
saved = SAVED,
96101
) {
97-
const files: Record<string, string> = { "index.html": SAVED, "compositions/sub.html": sub };
102+
const files: Record<string, string> = { "index.html": saved, "compositions/sub.html": sub };
98103
const fail = { on: false };
99104
const delayMs: Record<string, number> = {};
100105
const domEditSave = { pending: Promise.resolve() };
101106
const domSelectionRef = { current: domSelection };
102107
stubFiles(files, fail, delayMs);
103-
const iframe = mountPreview(sub, bundle);
108+
const iframe = mountPreview(sub, bundle, saved);
104109
const writes: string[] = [];
105110
const deleted: string[] = [];
106111
const writeProjectFile = async (_path: string, content: string) => {
@@ -248,6 +253,39 @@ const SUB_SELECTION = {
248253
sourceFile: "compositions/sub.html",
249254
} as DomEditSelection;
250255

256+
describe("paste of an element styled by its id", () => {
257+
it("gives the copy the look its original has", async () => {
258+
clearSelection();
259+
const saved = SAVED.replace(
260+
"<body>",
261+
"<body><style>#badge { color: rgb(200, 30, 40); letter-spacing: 3px; }</style>",
262+
).replace("</h1>", '</h1><p id="badge">New</p>');
263+
const selection = {
264+
hfId: stampedHfId(saved, "#badge"),
265+
selector: "#badge",
266+
selectorIndex: 0,
267+
sourceFile: "index.html",
268+
} as DomEditSelection;
269+
const { clipboard, writes } = mountClipboard(selection, SUB, () => {}, saved);
270+
clipboard().handleCopy();
271+
await clipboard().handlePaste();
272+
const page = document.createElement("iframe");
273+
document.body.appendChild(page);
274+
const doc = page.contentDocument as Document;
275+
doc.open();
276+
doc.write(writes[0] ?? "");
277+
doc.close();
278+
const look = (id: string) => {
279+
const style = (page.contentWindow as Window).getComputedStyle(
280+
doc.getElementById(id) as Element,
281+
);
282+
return { color: style.color, letterSpacing: style.letterSpacing };
283+
};
284+
expect(look("badge")).toEqual({ color: "rgb(200, 30, 40)", letterSpacing: "3px" });
285+
expect(look("badge-2")).toEqual(look("badge"));
286+
});
287+
});
288+
251289
describe("copy of a sub-composition clip", () => {
252290
it("rebases its relative asset paths to the project root, as the preview does", async () => {
253291
clearSelection();

‎packages/studio/src/hooks/useClipboard.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,17 @@ const hfIdsInFile = (content: string) =>
160160
export function pasteElementHtml(
161161
content: string,
162162
payload: { html: string; originSelector?: string; originSelectorIndex?: number },
163+
fromThisFile = false,
163164
): string {
164165
const reminted = remintHfIds(payload.html, new DOMParser(), hfIdsInFile(content));
165166
const deduped = deduplicateIds(reminted, collectHtmlIds(content));
166-
return insertAsSibling(content, deduped, payload.originSelector, payload.originSelectorIndex);
167+
const result = insertAsSibling(
168+
content,
169+
deduped,
170+
payload.originSelector,
171+
payload.originSelectorIndex,
172+
);
173+
return fromThisFile ? carryLook(result, renamedIds(reminted, deduped), 0) : result;
167174
}
168175

169176
/** Shared insertion path for paste and duplicate, anchored at the playhead or the selection's end. Returns the
@@ -324,6 +331,7 @@ export function useClipboard({
324331
kind: "dom-element",
325332
html: savedMarkupElseLive(parseSavedSource(content), live, sourceFile),
326333
sourceFile,
334+
projectId: projectIdRef.current ?? undefined,
327335
originSelector: domSelection.selector,
328336
originSelectorIndex: domSelection.selectorIndex,
329337
};
@@ -373,15 +381,17 @@ export function useClipboard({
373381
const targetPath = activeCompPath || "index.html";
374382
try {
375383
let pastedIds: string[] = [];
384+
const fromThisFile = payload.sourceFile === targetPath && payload.projectId === pid;
376385
const paste = (originalContent: string) => {
377-
if (payload.kind !== "timeline-clip") return pasteElementHtml(originalContent, payload);
386+
if (payload.kind !== "timeline-clip")
387+
return pasteElementHtml(originalContent, payload, fromThisFile);
378388
const { currentTime, elements } = usePlayerStore.getState();
379389
const pasted = pasteTimelineClips(
380390
originalContent,
381391
payload.clips,
382392
currentTime,
383393
elements,
384-
payload.sourceFile === targetPath && payload.projectId === pid,
394+
fromThisFile,
385395
);
386396
pastedIds = pasted.ids;
387397
// A clip pasted past the current composition end would exist in the

‎packages/studio/src/utils/clipboardPayload.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type ClipboardPayload =
2222
kind: "dom-element";
2323
html: string;
2424
sourceFile: string;
25+
projectId?: string;
2526
originSelector?: string;
2627
originSelectorIndex?: number;
2728
};
@@ -70,6 +71,7 @@ export function deserializeClipboardPayload(json: string): ClipboardPayload | nu
7071
kind: "dom-element",
7172
html: obj.html,
7273
sourceFile: obj.sourceFile,
74+
projectId: typeof obj.projectId === "string" ? obj.projectId : undefined,
7375
originSelector: typeof obj.originSelector === "string" ? obj.originSelector : undefined,
7476
originSelectorIndex:
7577
typeof obj.originSelectorIndex === "number" ? obj.originSelectorIndex : undefined,

0 commit comments

Comments
 (0)