@hyperframes/studio 0.9.43 → 0.9.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/{codec-DHChmhF2.js → codec-DYhkxayl.js} +1 -1
- package/dist/assets/{hyperframes-player-CdbweVDs.js → hyperframes-player-pPwFQY2W.js} +1 -1
- package/dist/assets/{index-4VGEAZyf.css → index-CU24FmKd.css} +1 -1
- package/dist/assets/{index-CjWDrWWG.js → index-Crhhpl7F.js} +120 -120
- package/dist/assets/{index-l-ti2dV8.js → index-Cv1BkoPn.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-xqOQ0ciK.js → pausedVideoDecoder-Cns-60-8.js} +1 -1
- package/dist/{chunk-4QQMX4BX.js → chunk-67DSVBEI.js} +1 -1
- package/dist/{chunk-52AJB4PN.js → chunk-ZNUWV4FN.js} +1 -1
- package/dist/index.html +2 -2
- package/dist/index.js +653 -627
- package/dist/player.js +2 -2
- package/dist/styles.css +1 -1
- package/dist/ui.js +1 -1
- package/package.json +8 -8
- package/src/components/StudioOverlays.tsx +15 -13
- package/src/components/StudioToast.test.tsx +41 -0
- package/src/components/StudioToast.tsx +23 -39
- package/src/components/feedback/StudioFeedbackCard.tsx +5 -2
- package/src/components/ui/Menu.test.tsx +30 -1
- package/src/components/ui/Menu.tsx +1 -1
- package/src/hooks/timelineEditingHelpers.test.ts +48 -0
- package/src/hooks/timelineEditingHelpers.ts +2 -0
- package/src/hooks/timelineElementsAttribute.test.ts +46 -0
- package/src/hooks/timelineElementsAttribute.ts +14 -16
- package/src/hooks/timelineTrackVisibility.test.ts +34 -0
- package/src/hooks/useElementPicker.test.ts +9 -2
- package/src/hooks/useElementPicker.ts +4 -2
- package/src/hooks/useToast.ts +6 -18
- package/src/player/lib/thumbnailFrameSink.test.ts +92 -0
- package/src/player/lib/thumbnailFrameSink.ts +83 -0
- package/src/player/lib/thumbnailVideoDecoder.test.ts +43 -16
- package/src/player/lib/thumbnailVideoDecoder.ts +6 -6
- package/src/styles/components.css +19 -1
- package/src/styles/theme.css +3 -0
- package/src/utils/previewSafeMargins.ts +6 -10
- package/src/utils/sourcePatcher.test.ts +21 -0
- package/src/utils/sourcePatcher.ts +16 -8
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { scaledFrames } from "./thumbnailFrameSink";
|
|
5
|
+
|
|
6
|
+
const drawImage = vi.fn();
|
|
7
|
+
const createImageBitmapMock = vi.fn();
|
|
8
|
+
const bitmap = { close: vi.fn() };
|
|
9
|
+
|
|
10
|
+
function sample(over: Partial<{ rotation: number; flipped: boolean }> = {}) {
|
|
11
|
+
const frame = { displayWidth: 3456, displayHeight: 2234, close: vi.fn() };
|
|
12
|
+
return {
|
|
13
|
+
frame,
|
|
14
|
+
rotation: 0,
|
|
15
|
+
flipped: false,
|
|
16
|
+
toVideoFrame: vi.fn(() => frame),
|
|
17
|
+
drawWithFit: vi.fn(),
|
|
18
|
+
close: vi.fn(),
|
|
19
|
+
...over,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function collect(samples: (ReturnType<typeof sample> | null)[], fit: "cover" | "contain") {
|
|
24
|
+
const sink = {
|
|
25
|
+
async *samplesAtTimestamps() {
|
|
26
|
+
yield* samples;
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
const out: ({ canvas: HTMLCanvasElement } | null)[] = [];
|
|
30
|
+
const times = (async function* () {})();
|
|
31
|
+
for await (const item of scaledFrames(sink, times, { width: 360, height: 240 }, fit))
|
|
32
|
+
out.push(item as { canvas: HTMLCanvasElement } | null);
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("scaledFrames", () => {
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
vi.stubGlobal("createImageBitmap", createImageBitmapMock.mockResolvedValue(bitmap));
|
|
39
|
+
vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue({
|
|
40
|
+
drawImage,
|
|
41
|
+
} as unknown as RenderingContext);
|
|
42
|
+
});
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
vi.unstubAllGlobals();
|
|
45
|
+
vi.restoreAllMocks();
|
|
46
|
+
vi.clearAllMocks();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("crops and scales a straight frame in createImageBitmap, centred for cover", async () => {
|
|
50
|
+
const s = sample();
|
|
51
|
+
const [out] = await collect([s], "cover");
|
|
52
|
+
|
|
53
|
+
expect(s.drawWithFit).not.toHaveBeenCalled();
|
|
54
|
+
const [frame, x, y, w, h, options] = createImageBitmapMock.mock.calls[0]!;
|
|
55
|
+
expect(frame).toBe(s.frame);
|
|
56
|
+
expect(options).toMatchObject({ resizeWidth: 360, resizeHeight: 240 });
|
|
57
|
+
expect(w / h).toBeCloseTo(360 / 240, 3);
|
|
58
|
+
expect(x + w / 2).toBeCloseTo(3456 / 2, 3);
|
|
59
|
+
expect(y + h / 2).toBeCloseTo(2234 / 2, 3);
|
|
60
|
+
expect(drawImage).toHaveBeenCalledWith(bitmap, 0, 0, 360, 240);
|
|
61
|
+
expect([out!.canvas.width, out!.canvas.height]).toEqual([360, 240]);
|
|
62
|
+
expect(s.frame.close).toHaveBeenCalled();
|
|
63
|
+
expect(bitmap.close).toHaveBeenCalled();
|
|
64
|
+
expect(s.close).toHaveBeenCalled();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("contain keeps the whole frame", async () => {
|
|
68
|
+
await collect([sample()], "contain");
|
|
69
|
+
const [, x, y, w, h] = createImageBitmapMock.mock.calls[0]!;
|
|
70
|
+
expect([x, y, w, h]).toEqual([0, 0, 3456, 2234]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("leaves turned and flipped frames to the sample's own draw, which applies the turn", async () => {
|
|
74
|
+
const turned = sample({ rotation: 90 });
|
|
75
|
+
const flipped = sample({ flipped: true });
|
|
76
|
+
await collect([turned, flipped], "cover");
|
|
77
|
+
|
|
78
|
+
expect(createImageBitmapMock).not.toHaveBeenCalled();
|
|
79
|
+
expect(turned.drawWithFit).toHaveBeenCalledWith(expect.anything(), { fit: "cover" });
|
|
80
|
+
expect(flipped.drawWithFit).toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("passes a missing frame through and closes the sample even when the scale fails", async () => {
|
|
84
|
+
expect(await collect([null], "cover")).toEqual([null]);
|
|
85
|
+
|
|
86
|
+
const s = sample();
|
|
87
|
+
createImageBitmapMock.mockRejectedValueOnce(new Error("decode failed"));
|
|
88
|
+
await expect(collect([s], "cover")).rejects.toThrow("decode failed");
|
|
89
|
+
expect(s.frame.close).toHaveBeenCalled();
|
|
90
|
+
expect(s.close).toHaveBeenCalled();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** The slice of a Mediabunny video sample the strip reads. */
|
|
2
|
+
interface FrameSample {
|
|
3
|
+
rotation: number;
|
|
4
|
+
flipped?: boolean;
|
|
5
|
+
toVideoFrame(): VideoFrame;
|
|
6
|
+
drawWithFit(
|
|
7
|
+
context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
|
|
8
|
+
options: { fit: "contain" | "cover" },
|
|
9
|
+
): void;
|
|
10
|
+
close(): void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface FrameSampleSink {
|
|
14
|
+
samplesAtTimestamps(timestamps: AsyncIterable<number>): AsyncIterable<FrameSample | null>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type FrameCanvas = HTMLCanvasElement | OffscreenCanvas;
|
|
18
|
+
type Box = { width: number; height: number };
|
|
19
|
+
|
|
20
|
+
function newCanvas({ width, height }: Box): FrameCanvas {
|
|
21
|
+
if (typeof document === "undefined") return new OffscreenCanvas(width, height);
|
|
22
|
+
const canvas = document.createElement("canvas");
|
|
23
|
+
canvas.width = width;
|
|
24
|
+
canvas.height = height;
|
|
25
|
+
return canvas;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The part of a source that fills a box of the target's shape, centred. */
|
|
29
|
+
function coverCrop(source: Box, target: Box) {
|
|
30
|
+
const scale = Math.max(target.width / source.width, target.height / source.height);
|
|
31
|
+
const width = Math.min(source.width, target.width / scale);
|
|
32
|
+
const height = Math.min(source.height, target.height / scale);
|
|
33
|
+
return { x: (source.width - width) / 2, y: (source.height - height) / 2, width, height };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function drawScaled(
|
|
37
|
+
sample: FrameSample,
|
|
38
|
+
context: CanvasRenderingContext2D,
|
|
39
|
+
target: Box,
|
|
40
|
+
fit: "contain" | "cover",
|
|
41
|
+
): Promise<void> {
|
|
42
|
+
const frame = sample.toVideoFrame();
|
|
43
|
+
const source = { width: frame.displayWidth, height: frame.displayHeight };
|
|
44
|
+
const crop = fit === "cover" ? coverCrop(source, target) : { x: 0, y: 0, ...source };
|
|
45
|
+
const bitmap = await createImageBitmap(frame, crop.x, crop.y, crop.width, crop.height, {
|
|
46
|
+
resizeWidth: target.width,
|
|
47
|
+
resizeHeight: target.height,
|
|
48
|
+
resizeQuality: "medium",
|
|
49
|
+
}).finally(() => frame.close());
|
|
50
|
+
try {
|
|
51
|
+
context.drawImage(bitmap, 0, 0, target.width, target.height);
|
|
52
|
+
} finally {
|
|
53
|
+
bitmap.close();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Strip frames cropped and scaled by the browser off the main thread: drawing a 4K frame onto a strip-sized canvas
|
|
59
|
+
* holds the main thread for 100 ms or more. Turned or flipped frames keep the sample's own draw, which turns them.
|
|
60
|
+
*/
|
|
61
|
+
export async function* scaledFrames(
|
|
62
|
+
sink: FrameSampleSink,
|
|
63
|
+
times: AsyncIterable<number>,
|
|
64
|
+
target: Box,
|
|
65
|
+
fit: "contain" | "cover",
|
|
66
|
+
): AsyncGenerator<{ canvas: FrameCanvas } | null> {
|
|
67
|
+
for await (const sample of sink.samplesAtTimestamps(times)) {
|
|
68
|
+
if (!sample) {
|
|
69
|
+
yield null;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const canvas = newCanvas(target);
|
|
74
|
+
const context = canvas.getContext("2d") as CanvasRenderingContext2D | null;
|
|
75
|
+
if (!context) throw new Error("Thumbnail canvas has no 2d context");
|
|
76
|
+
if (sample.rotation === 0 && !sample.flipped) await drawScaled(sample, context, target, fit);
|
|
77
|
+
else sample.drawWithFit(context, { fit });
|
|
78
|
+
yield { canvas };
|
|
79
|
+
} finally {
|
|
80
|
+
sample.close();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -11,6 +11,15 @@ const sourceRetryRules: string[] = [];
|
|
|
11
11
|
const cancelledReads: ReturnType<typeof vi.fn>[] = [];
|
|
12
12
|
const canvasesAtTimestamps = vi.fn();
|
|
13
13
|
const canvasSinks: unknown[] = [];
|
|
14
|
+
const drawImage = vi.fn();
|
|
15
|
+
const createImageBitmapMock = vi.fn();
|
|
16
|
+
const fourKSample = () => ({
|
|
17
|
+
rotation: 0,
|
|
18
|
+
flipped: false,
|
|
19
|
+
toVideoFrame: () => ({ displayWidth: 3456, displayHeight: 2234, close: vi.fn() }),
|
|
20
|
+
drawWithFit: vi.fn(),
|
|
21
|
+
close: vi.fn(),
|
|
22
|
+
});
|
|
14
23
|
const getKeyPacket = vi.fn(async (_time: number) => null as { timestamp: number } | null);
|
|
15
24
|
const input = {
|
|
16
25
|
getPrimaryVideoTrack: vi.fn(),
|
|
@@ -37,11 +46,16 @@ vi.mock("mediabunny", () => ({
|
|
|
37
46
|
getPrimaryVideoTrack = input.getPrimaryVideoTrack;
|
|
38
47
|
dispose = input.dispose;
|
|
39
48
|
},
|
|
40
|
-
|
|
49
|
+
// Each frame the canvas seam yields becomes one straight (unturned) sample of a 4K source.
|
|
50
|
+
VideoSampleSink: class {
|
|
41
51
|
constructor() {
|
|
42
52
|
canvasSinks.push(this);
|
|
43
53
|
}
|
|
44
|
-
|
|
54
|
+
async *samplesAtTimestamps(timestamps: AsyncIterable<number>) {
|
|
55
|
+
for await (const wrapped of canvasesAtTimestamps(timestamps)) {
|
|
56
|
+
yield wrapped === null ? null : fourKSample();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
45
59
|
},
|
|
46
60
|
EncodedPacketSink: class {
|
|
47
61
|
getKeyPacket = getKeyPacket;
|
|
@@ -89,6 +103,11 @@ beforeEach(() => {
|
|
|
89
103
|
HTMLCanvasElement.prototype.toBlob = function toBlob(callback) {
|
|
90
104
|
callback(new Blob(["frame"], { type: "image/jpeg" }));
|
|
91
105
|
};
|
|
106
|
+
HTMLCanvasElement.prototype.getContext = (() => ({ drawImage })) as never;
|
|
107
|
+
vi.stubGlobal(
|
|
108
|
+
"createImageBitmap",
|
|
109
|
+
createImageBitmapMock.mockImplementation(async () => ({ close: vi.fn() })),
|
|
110
|
+
);
|
|
92
111
|
input.getPrimaryVideoTrack.mockResolvedValue({
|
|
93
112
|
getDisplayWidth: vi.fn(async () => 1080),
|
|
94
113
|
getDisplayHeight: vi.fn(async () => 1920),
|
|
@@ -96,6 +115,28 @@ beforeEach(() => {
|
|
|
96
115
|
});
|
|
97
116
|
});
|
|
98
117
|
|
|
118
|
+
describe("decodeVideoThumbnail frame scaling", () => {
|
|
119
|
+
it("never hands a full-size frame to a canvas draw: the browser crops and scales it to the strip's size", async () => {
|
|
120
|
+
recordDecodes([]);
|
|
121
|
+
await decodeVideoThumbnail(
|
|
122
|
+
{ source, sourceStart: 0, sourceRangeDuration: 10, frameCount: 2 },
|
|
123
|
+
new AbortController().signal,
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
expect(createImageBitmapMock).toHaveBeenCalled();
|
|
127
|
+
for (const [, , , cropWidth, cropHeight, options] of createImageBitmapMock.mock.calls) {
|
|
128
|
+
expect(options.resizeWidth).toBeLessThan(cropWidth);
|
|
129
|
+
expect(options.resizeHeight).toBeLessThan(cropHeight);
|
|
130
|
+
}
|
|
131
|
+
// What lands on the canvas is the small bitmap, never a 4K frame.
|
|
132
|
+
for (const [image, , , width, height] of drawImage.mock.calls) {
|
|
133
|
+
expect(image).not.toHaveProperty("displayWidth");
|
|
134
|
+
expect(width).toBeLessThan(1000);
|
|
135
|
+
expect(height).toBeLessThan(1000);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
99
140
|
describe("videoThumbnailTimestamps", () => {
|
|
100
141
|
it("uses the midpoint for a poster, and each slice's left edge then the end for a strip", () => {
|
|
101
142
|
expect(videoThumbnailTimestamps(2, 6, 1)).toEqual([5]);
|
|
@@ -706,20 +747,6 @@ describe("decodeVideoThumbnail's shared source", () => {
|
|
|
706
747
|
expect(dispose).not.toHaveBeenCalled();
|
|
707
748
|
});
|
|
708
749
|
|
|
709
|
-
it("keeps the reader when an offscreen frame will not encode", async () => {
|
|
710
|
-
const offscreen = { width: 1, height: 1, convertToBlob: () => Promise.reject(new Error("no")) };
|
|
711
|
-
canvasesAtTimestamps.mockImplementationOnce(async function* (
|
|
712
|
-
timestamps: AsyncIterable<number>,
|
|
713
|
-
) {
|
|
714
|
-
for await (const _time of timestamps) yield { canvas: offscreen };
|
|
715
|
-
});
|
|
716
|
-
await expect(decodeVideoThumbnail(clip(0), signal)).rejects.toThrow("encode failed");
|
|
717
|
-
recordDecodes([]);
|
|
718
|
-
await decodeVideoThumbnail(clip(6), signal);
|
|
719
|
-
expect(input.getPrimaryVideoTrack).toHaveBeenCalledTimes(1);
|
|
720
|
-
expect(dispose).not.toHaveBeenCalled();
|
|
721
|
-
});
|
|
722
|
-
|
|
723
750
|
it("keeps the reader of a clip scrolled away from, for the next clip", async () => {
|
|
724
751
|
vi.useFakeTimers();
|
|
725
752
|
try {
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
type ThumbnailLoadedResult,
|
|
5
5
|
type ThumbnailValue,
|
|
6
6
|
} from "./thumbnailScheduler";
|
|
7
|
+
import { scaledFrames } from "./thumbnailFrameSink";
|
|
7
8
|
import { MediaFetchError, reportMediaFetchFailure } from "./mediaFetch";
|
|
8
9
|
import {
|
|
9
10
|
acquireThumbnailSource,
|
|
@@ -395,12 +396,11 @@ async function decodeMissingFrames(
|
|
|
395
396
|
}
|
|
396
397
|
}
|
|
397
398
|
const target = targetDimensions(info.aspect, budgets);
|
|
398
|
-
const
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
});
|
|
399
|
+
const frames = new mediabunny.VideoSampleSink(track);
|
|
400
|
+
const sink: ThumbnailCanvasSink = {
|
|
401
|
+
canvasesAtTimestamps: (timestamps) =>
|
|
402
|
+
scaledFrames(frames, timestamps, target, request.fit ?? "cover"),
|
|
403
|
+
};
|
|
404
404
|
const times = decodeTimesAtNearbyKeyframes();
|
|
405
405
|
await decodeFrames(sink, times, slots, signal, resources, source.progress);
|
|
406
406
|
}
|
|
@@ -605,7 +605,7 @@
|
|
|
605
605
|
}
|
|
606
606
|
}
|
|
607
607
|
|
|
608
|
-
/*
|
|
608
|
+
/* Feedback card enter/exit (StudioFeedbackCard) */
|
|
609
609
|
@keyframes hf-toast-in {
|
|
610
610
|
from {
|
|
611
611
|
opacity: 0;
|
|
@@ -646,6 +646,24 @@
|
|
|
646
646
|
}
|
|
647
647
|
}
|
|
648
648
|
|
|
649
|
+
/* A toast rises in and leaves at once (StudioToast). */
|
|
650
|
+
@keyframes hf-notice-in {
|
|
651
|
+
from {
|
|
652
|
+
opacity: 0;
|
|
653
|
+
transform: translateY(8px) scale(0.98);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
.hf-notice-in {
|
|
658
|
+
animation: hf-notice-in 200ms cubic-bezier(0.32, 0.72, 0, 1) backwards;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
@media (prefers-reduced-motion: reduce) {
|
|
662
|
+
.hf-notice-in {
|
|
663
|
+
animation: none;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
649
667
|
/* Overlay/backdrop entrance — the heaviest visual changes shouldn't pop in */
|
|
650
668
|
@keyframes hf-backdrop-in {
|
|
651
669
|
from {
|
package/src/styles/theme.css
CHANGED
|
@@ -285,6 +285,9 @@
|
|
|
285
285
|
0 16px 40px light-dark(rgb(0 0 0 / 0.14), rgb(0 0 0 / 0.55)),
|
|
286
286
|
0 0 0 1px light-dark(rgb(0 0 0 / 0.07), rgb(0 0 0 / 0.45));
|
|
287
287
|
|
|
288
|
+
/* The toast pill: a close contact shadow under a soft lift. */
|
|
289
|
+
--shadow-notice: 0 2px 4px rgb(0 0 0 / 0.12), 0 14px 36px -8px rgb(0 0 0 / 0.4);
|
|
290
|
+
|
|
288
291
|
/* The primary button reads as a raised key rather than a floating panel: a tight
|
|
289
292
|
* contact shadow, a soft ambient one, and an inner top highlight. */
|
|
290
293
|
--shadow-btn-primary:
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/** Inset from each frame edge, as a percent of the frame. */
|
|
8
|
-
export function safeBoxInsetPercent(boxPercent: number): number {
|
|
9
|
-
return (100 - boxPercent) / 2;
|
|
10
|
-
}
|
|
1
|
+
export {
|
|
2
|
+
ACTION_SAFE_PERCENT,
|
|
3
|
+
SAFE_BOX_PERCENTS,
|
|
4
|
+
TITLE_SAFE_PERCENT,
|
|
5
|
+
safeBoxInsetPercent,
|
|
6
|
+
} from "@hyperframes/core/safe-areas";
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
readTagSnippetByTarget,
|
|
8
8
|
type PatchOperation,
|
|
9
9
|
} from "./sourcePatcher";
|
|
10
|
+
import { ensureHfIds } from "@hyperframes/parsers/hf-ids";
|
|
10
11
|
|
|
11
12
|
describe("applyPatchByTarget", () => {
|
|
12
13
|
it("removes all duplicate boolean attributes so unmuting cannot reveal another", () => {
|
|
@@ -679,6 +680,26 @@ describe("T3 — hfId targeting (spec for R1)", () => {
|
|
|
679
680
|
});
|
|
680
681
|
});
|
|
681
682
|
|
|
683
|
+
describe("a file nothing has stamped yet", () => {
|
|
684
|
+
// Added outside Studio: no id, no class. The preview serves it with an hf-id the file does not hold.
|
|
685
|
+
const ON_DISK = `<div data-composition-id="main"><p data-start="1" data-volume="0.5">Title</p></div>`;
|
|
686
|
+
const hfId = /<p data-hf-id="([^"]+)"/.exec(ensureHfIds(ON_DISK))?.[1] ?? "";
|
|
687
|
+
const op: PatchOperation = { type: "attribute", property: "volume", value: "0.9" };
|
|
688
|
+
|
|
689
|
+
it("reads, finds and patches the element by its served hf-id", () => {
|
|
690
|
+
expect(hfId).toMatch(/^hf-/);
|
|
691
|
+
expect(readAttributeByTarget(ON_DISK, { hfId }, "volume")).toBe("0.5");
|
|
692
|
+
expect(readTagSnippetByTarget(ON_DISK, { hfId })).toContain(`data-hf-id="${hfId}"`);
|
|
693
|
+
expect(
|
|
694
|
+
readAttributeByTarget(applyPatchByTarget(ON_DISK, { hfId }, op), { hfId }, "volume"),
|
|
695
|
+
).toBe("0.9");
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
it("leaves the file as it was when no element has the id, ids and all", () => {
|
|
699
|
+
expect(applyPatchByTarget(ON_DISK, { hfId: "hf-gone" }, op)).toBe(ON_DISK);
|
|
700
|
+
});
|
|
701
|
+
});
|
|
702
|
+
|
|
682
703
|
describe("file names with special characters", () => {
|
|
683
704
|
const src = (value: string): PatchOperation => ({
|
|
684
705
|
type: "html-attribute",
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
decodeAuthoredAttribute,
|
|
4
4
|
HTML_ATTRIBUTE_ENTITIES,
|
|
5
5
|
} from "@hyperframes/parsers";
|
|
6
|
+
import { stampHfIdsInPlace } from "@hyperframes/parsers/hf-ids";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Source Patcher — Maps visual property edits back to source HTML files.
|
|
@@ -277,6 +278,12 @@ function findTagByClass(html: string, target: PatchTarget): TagMatch | null {
|
|
|
277
278
|
: null;
|
|
278
279
|
}
|
|
279
280
|
|
|
281
|
+
export function servedSource(html: string, target: PatchTarget): string {
|
|
282
|
+
if (!target.hfId || findTagByTarget(html, target)) return html;
|
|
283
|
+
const stamped = stampHfIdsInPlace(html);
|
|
284
|
+
return findTagByTarget(stamped, target) ? stamped : html;
|
|
285
|
+
}
|
|
286
|
+
|
|
280
287
|
export function findTagByTarget(html: string, target: PatchTarget): TagMatch | null {
|
|
281
288
|
if (target.hfId) {
|
|
282
289
|
const result = findTagByAttribute(html, "data-hf-id", target.hfId);
|
|
@@ -304,7 +311,7 @@ export function readAttributeByTarget(
|
|
|
304
311
|
target: PatchTarget,
|
|
305
312
|
attr: string,
|
|
306
313
|
): string | undefined {
|
|
307
|
-
const match = findTagByTarget(html, target);
|
|
314
|
+
const match = findTagByTarget(servedSource(html, target), target);
|
|
308
315
|
if (!match) return undefined;
|
|
309
316
|
|
|
310
317
|
return readTagAttribute(match.tag, attr.startsWith("data-") ? attr : `data-${attr}`);
|
|
@@ -316,7 +323,7 @@ export function readTagAttribute(tag: string, attr: string): string | undefined
|
|
|
316
323
|
}
|
|
317
324
|
|
|
318
325
|
export function readTagSnippetByTarget(html: string, target: PatchTarget): string | undefined {
|
|
319
|
-
const match = findTagByTarget(html, target);
|
|
326
|
+
const match = findTagByTarget(servedSource(html, target), target);
|
|
320
327
|
return match?.tag;
|
|
321
328
|
}
|
|
322
329
|
|
|
@@ -454,22 +461,23 @@ export function applyPatch(html: string, elementId: string, op: PatchOperation):
|
|
|
454
461
|
}
|
|
455
462
|
|
|
456
463
|
export function applyPatchByTarget(html: string, target: PatchTarget, op: PatchOperation): string {
|
|
464
|
+
const source = servedSource(html, target);
|
|
457
465
|
if (target.id) {
|
|
458
|
-
const patchedById = applyPatch(
|
|
459
|
-
if (patchedById !==
|
|
466
|
+
const patchedById = applyPatch(source, target.id, op);
|
|
467
|
+
if (patchedById !== source || (!target.selector && !target.hfId)) {
|
|
460
468
|
return patchedById;
|
|
461
469
|
}
|
|
462
470
|
}
|
|
463
471
|
|
|
464
472
|
switch (op.type) {
|
|
465
473
|
case "inline-style":
|
|
466
|
-
return patchInlineStyleByTarget(
|
|
474
|
+
return patchInlineStyleByTarget(source, target, op.property, op.value);
|
|
467
475
|
case "attribute":
|
|
468
|
-
return patchAttributeByTarget(
|
|
476
|
+
return patchAttributeByTarget(source, target, op.property, op.value);
|
|
469
477
|
case "html-attribute":
|
|
470
|
-
return patchHtmlAttributeByTarget(
|
|
478
|
+
return patchHtmlAttributeByTarget(source, target, op.property, op.value);
|
|
471
479
|
case "text-content":
|
|
472
|
-
return op.value !== null ? patchTextContentByTarget(
|
|
480
|
+
return op.value !== null ? patchTextContentByTarget(source, target, op.value) : html;
|
|
473
481
|
default:
|
|
474
482
|
return html;
|
|
475
483
|
}
|