Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .filesize-allowlist
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ packages/studio/src/components/editor/manualEdits.test.ts
packages/studio/src/player/hooks/useTimelinePlayer.test.ts
packages/studio/src/components/editor/manualEditsDom.ts
packages/studio/src/utils/sourcePatcher.ts
packages/studio/src/utils/sourcePatcher.test.ts
packages/studio/src/App.tsx
packages/studio/src/player/components/Timeline.tsx
packages/studio/src/player/components/timelineEditing.test.ts
80 changes: 7 additions & 73 deletions packages/studio/src/hooks/useTimelineEditing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { useCallback, useRef } from "react";
import type { TimelineElement } from "../player";
import { usePlayerStore } from "../player";
import { applyPatchByTarget, readAttributeByTarget } from "../utils/sourcePatcher";
import {
buildTrackZIndexMap,
formatTimelineAttributeNumber,
} from "../player/components/timelineEditing";
import { formatTimelineAttributeNumber } from "../player/components/timelineEditing";
import {
buildTimelineAssetId,
buildTimelineAssetInsertHtml,
Expand Down Expand Up @@ -101,16 +98,6 @@ export function useTimelineEditing({
throw new Error(`Timeline element ${element.id} is missing a patchable target`);
}

const resolvedTargetPath = targetPath || "index.html";
const relevantElements = timelineElements
.map((te) =>
(te.key ?? te.id) === (element.key ?? element.id)
? { ...te, start: updates.start, track: updates.track }
: te,
)
.filter((te) => (te.sourceFile || activeCompPath || "index.html") === resolvedTargetPath);
const trackZIndices = buildTrackZIndexMap(relevantElements.map((te) => te.track));

let patchedContent = applyPatchByTarget(originalContent, patchTarget, {
type: "attribute",
property: "start",
Expand All @@ -121,17 +108,6 @@ export function useTimelineEditing({
property: "track-index",
value: String(updates.track),
});
for (const te of relevantElements) {
const elementTarget = buildPatchTarget(te);
if (!elementTarget) continue;
const nextZIndex = trackZIndices.get(te.track);
if (nextZIndex == null) continue;
patchedContent = applyPatchByTarget(patchedContent, elementTarget, {
type: "inline-style",
property: "z-index",
value: String(nextZIndex),
});
}

if (patchedContent === originalContent) {
throw new Error(`Unable to patch timeline element ${element.id} in ${targetPath}`);
Expand All @@ -150,14 +126,7 @@ export function useTimelineEditing({

reloadPreview();
},
[
activeCompPath,
recordEdit,
timelineElements,
writeProjectFile,
domEditSaveTimestampRef,
reloadPreview,
],
[activeCompPath, recordEdit, writeProjectFile, domEditSaveTimestampRef, reloadPreview],
);

const handleTimelineElementResize = useCallback(
Expand Down Expand Up @@ -247,14 +216,6 @@ export function useTimelineEditing({
throw new Error(`Timeline element ${element.id} is missing a patchable target`);
}

const resolvedTargetPath = targetPath || "index.html";
const remainingElements = timelineElements.filter(
(te) =>
(te.key ?? te.id) !== (element.key ?? element.id) &&
(te.sourceFile || activeCompPath || "index.html") === resolvedTargetPath,
);
const trackZIndices = buildTrackZIndexMap(remainingElements.map((te) => te.track));

const removeResponse = await fetch(
`/api/projects/${pid}/file-mutations/remove-element/${encodeURIComponent(targetPath)}`,
{
Expand All @@ -271,19 +232,8 @@ export function useTimelineEditing({
changed?: boolean;
content?: string;
};
let patchedContent =
const patchedContent =
typeof removeData.content === "string" ? removeData.content : originalContent;
for (const te of remainingElements) {
const elementTarget = buildPatchTarget(te);
if (!elementTarget) continue;
const nextZIndex = trackZIndices.get(te.track);
if (nextZIndex == null) continue;
patchedContent = applyPatchByTarget(patchedContent, elementTarget, {
type: "inline-style",
property: "z-index",
value: String(nextZIndex),
});
}

domEditSaveTimestampRef.current = Date.now();
await saveProjectFilesWithHistory({
Expand Down Expand Up @@ -352,34 +302,18 @@ export function useTimelineEditing({
const relevantElements = timelineElements.filter(
(te) => (te.sourceFile || activeCompPath || "index.html") === resolvedTargetPath,
);
const trackZIndices = buildTrackZIndexMap([
...relevantElements.map((te) => te.track),
placement.track,
]);

let patchedContent = originalContent;
for (const te of relevantElements) {
const elementTarget = buildPatchTarget(te);
if (!elementTarget) continue;
const nextZIndex = trackZIndices.get(te.track);
if (nextZIndex == null) continue;
patchedContent = applyPatchByTarget(patchedContent, elementTarget, {
type: "inline-style",
property: "z-index",
value: String(nextZIndex),
});
}
const newElementZIndex = Math.max(1, relevantElements.length + 1);

patchedContent = insertTimelineAssetIntoSource(
patchedContent,
const patchedContent = insertTimelineAssetIntoSource(
originalContent,
buildTimelineAssetInsertHtml({
id: newId,
assetPath: resolvedAssetSrc,
kind,
start: normalizedStart,
duration: normalizedDuration,
track: placement.track,
zIndex: trackZIndices.get(placement.track) ?? 1,
zIndex: newElementZIndex,
geometry: resolveTimelineAssetInitialGeometry(originalContent),
}),
);
Expand Down
24 changes: 0 additions & 24 deletions packages/studio/src/player/components/timelineEditing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
buildPromptCopyText,
buildTimelineElementAgentPrompt,
buildTimelineAgentPrompt,
buildTrackZIndexMap,
canOffsetTrimClipStart,
getTimelineEditCapabilities,
hasPatchableTimelineTarget,
Expand Down Expand Up @@ -159,29 +158,6 @@ describe("resolveTimelineMove", () => {
});
});

describe("buildTrackZIndexMap", () => {
it("maps visually higher tracks onto higher z-index values", () => {
expect(buildTrackZIndexMap([-2, -1, 0, 3])).toEqual(
new Map([
[-2, 4],
[-1, 3],
[0, 2],
[3, 1],
]),
);
});

it("deduplicates tracks before assigning z-index values", () => {
expect(buildTrackZIndexMap([-1, 0, -1, 3, 3])).toEqual(
new Map([
[-1, 3],
[0, 2],
[3, 1],
]),
);
});
});

describe("canOffsetTrimClipStart", () => {
it("allows front trim for clips that carry playback offset metadata", () => {
expect(
Expand Down
6 changes: 0 additions & 6 deletions packages/studio/src/player/components/timelineEditing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,6 @@ export function resolveTimelineMove(
};
}

export function buildTrackZIndexMap(tracks: number[]): Map<number, number> {
const uniqueTracks = Array.from(new Set(tracks)).sort((a, b) => a - b);
const maxZIndex = uniqueTracks.length;
return new Map(uniqueTracks.map((track, index) => [track, maxZIndex - index]));
}

export function resolveTimelineResize(
input: TimelineResizeInput,
edge: "start" | "end",
Expand Down
8 changes: 2 additions & 6 deletions packages/studio/src/utils/blockInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import {
resolveTimelineAssetInitialGeometry,
} from "./timelineAssetDrop";
import { collectHtmlIds } from "./studioHelpers";
import {
buildTrackZIndexMap,
formatTimelineAttributeNumber,
} from "../player/components/timelineEditing";
import { formatTimelineAttributeNumber } from "../player/components/timelineEditing";
import { saveProjectFilesWithHistory } from "./studioFileHistory";
import type { EditHistoryKind } from "./editHistory";

Expand Down Expand Up @@ -127,8 +124,7 @@ export async function addBlockToProject(
? Math.max(...relevantElements.map((te) => te.track)) + 1
: 1);

const trackZIndices = buildTrackZIndexMap([...relevantElements.map((te) => te.track), track]);
const zIndex = trackZIndices.get(track) ?? 1;
const zIndex = Math.max(1, relevantElements.length + 1);

const width = isBlock
? (block as { dimensions: { width: number } }).dimensions.width
Expand Down
22 changes: 22 additions & 0 deletions packages/studio/src/utils/sourcePatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ describe("applyPatchByTarget", () => {
);
});

it("adds inline style to a self-closing void element without malforming it", () => {
const html = `<img id="gif-img" class="clip" data-start="1" src="earth.gif" alt="earth" />`;
const op: PatchOperation = { type: "inline-style", property: "z-index", value: "3" };

const result = applyPatch(html, "gif-img", op);
expect(result).toBe(
`<img id="gif-img" class="clip" data-start="1" src="earth.gif" alt="earth" style="z-index: 3" />`,
);
expect(result).not.toContain("/ style");
});

it("adds inline style to a self-closing void element matched by selector", () => {
const html = `<img class="clip hero" data-start="0" src="bg.png" alt="" />`;
const op: PatchOperation = { type: "inline-style", property: "opacity", value: "0.5" };

const result = applyPatchByTarget(html, { selector: ".hero" }, op);
expect(result).toBe(
`<img class="clip hero" data-start="0" src="bg.png" alt="" style="opacity: 0.5" />`,
);
expect(result).not.toContain("/ style");
});

it("patches inline move styles by target", () => {
const html = `<div id="card" style="position: absolute; left: 108px; top: 112px"></div>`;

Expand Down
6 changes: 3 additions & 3 deletions packages/studio/src/utils/sourcePatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@
} else {
// No existing style attribute
if (value === null) return html; // nothing to remove
// Add one
const newTag =
tag.replace(/>$/, "") + ` style="${prop}: ${escapeStyleAttributeValue(value, '"')}"`;
const selfClosing = /\s*\/$/.test(tag);

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of ' '.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of ' '.
const base = selfClosing ? tag.replace(/\s*\/$/, "") : tag;

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of ' '.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of ' '.
const newTag = `${base} style="${prop}: ${escapeStyleAttributeValue(value, '"')}"${selfClosing ? " /" : ""}`;
return html.replace(tag, newTag);
}
}
Expand Down
Loading
Loading