дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-27 13:06:07 +02:00
fix(viewers): update the SVG preview's size when the typed source changes it (#6314)
WebKit keeps the natural size of an <img> that has a CSS size when its src changes to another SVG, so editing the root's width or height left the live preview and its dimension readout at the old size. The preview now measures each typed source with a detached image.
Цей коміт міститься в:
1 джерело
9ed0fd9fa7
коміт
2cdcb888cf
2 files changed
+44
-2
No files matched your search
@@ -157,9 +157,18 @@ export function ImageViewer({ input, source }: { input: EditorInput; source?: st
|
||||
useForwardFocus(live ? NO_FOCUS_TARGET : rootRef);
|
||||
|
||||
const onLoad = (img: HTMLImageElement) => {
|
||||
if (!live) return applyNatural(img.naturalWidth, img.naturalHeight);
|
||||
// WebKit keeps the old natural size when a sized <img> gets a new SVG src; measure a detached copy.
|
||||
const loaded = img.src;
|
||||
const probe = new Image();
|
||||
probe.onload = () => img.src === loaded && applyNatural(probe.naturalWidth, probe.naturalHeight);
|
||||
probe.src = loaded;
|
||||
};
|
||||
|
||||
const applyNatural = (naturalW: number, naturalH: number) => {
|
||||
// SVGs without intrinsic size report 0×0; show them at a usable size.
|
||||
const w = img.naturalWidth || 300;
|
||||
const h = img.naturalHeight || 150;
|
||||
const w = naturalW || 300;
|
||||
const h = naturalH || 150;
|
||||
setNatural({ w, h });
|
||||
const a = available();
|
||||
const el = canvasRef.current;
|
||||
|
||||
@@ -72,6 +72,39 @@ describe("SVG editor", () => {
|
||||
expect(screen.queryByText("The SVG source is not valid.")).toBeNull();
|
||||
});
|
||||
|
||||
it("measures a retyped SVG's size from a fresh image, not the sized <img>", () => {
|
||||
// WebKit reports the previous natural size on the shown <img>; a detached Image reports the new one.
|
||||
const probes: { onload: (() => void) | null; src: string; naturalWidth: number; naturalHeight: number }[] = [];
|
||||
vi.stubGlobal(
|
||||
"Image",
|
||||
class {
|
||||
onload: (() => void) | null = null;
|
||||
naturalWidth = 360;
|
||||
naturalHeight = 300;
|
||||
set src(value: string) {
|
||||
this._src = value;
|
||||
probes.push(this);
|
||||
}
|
||||
get src() {
|
||||
return this._src;
|
||||
}
|
||||
_src = "";
|
||||
},
|
||||
);
|
||||
try {
|
||||
const source = '<svg xmlns="http://www.w3.org/2000/svg" width="360" height="300"/>';
|
||||
render(<ImageViewer input={imageInput(PATH)} source={source} />);
|
||||
Object.defineProperty(image(), "naturalWidth", { value: 400 });
|
||||
Object.defineProperty(image(), "naturalHeight", { value: 300 });
|
||||
fireEvent.load(image());
|
||||
expect(probes.map((p) => p.src)).toEqual([svgDataUrl(source)]);
|
||||
act(() => probes[0]?.onload?.());
|
||||
expect(screen.getByText("360 × 300")).toBeInTheDocument();
|
||||
} finally {
|
||||
vi.unstubAllGlobals();
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the SVG layout apart from Markdown's, and the layout actions set the tab's own", async () => {
|
||||
const off = activateViewers();
|
||||
setContext("textEditorActive", true);
|
||||
|
||||
Reference in new issue
Заблокувати користувача