Skip to content

Commit 8d6442a

Browse files
Fix SVG size calulation, only use style attribute (#36133) (#36134)
Backport of #36133, only the bugfix part. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 3d66e75 commit 8d6442a

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

web_src/css/base.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
--gap-inline: 0.25rem; /* gap for inline texts and elements, for example: the spaces for sentence with labels, button text, etc */
4141
--gap-block: 0.5rem; /* gap for element blocks, for example: spaces between buttons, menu image & title, header icon & title etc */
42+
43+
--background-view-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAG0lEQVQYlWN4+vTpf3SMDTAMBYXYBLFpHgoKAeiOf0SGE9kbAAAAAElFTkSuQmCC") right bottom var(--color-primary-light-7);
4244
}
4345

4446
@media (min-width: 768px) and (max-width: 1200px) {

web_src/css/features/imagediff.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
.image-diff-container img {
1515
border: 1px solid var(--color-primary-light-7);
16-
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAG0lEQVQYlWN4+vTpf3SMDTAMBYXYBLFpHgoKAeiOf0SGE9kbAAAAAElFTkSuQmCC") right bottom var(--color-primary-light-7);
16+
background: var(--background-view-image);
1717
}
1818

1919
.image-diff-container .before-container {

web_src/css/repo/file-view.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
.view-raw img[src$=".svg" i] {
8282
max-height: 600px !important;
8383
max-width: 600px !important;
84+
background: var(--background-view-image);
8485
}
8586

8687
.file-view-render-container {

web_src/js/features/imagediff.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ function getDefaultSvgBoundsIfUndefined(text: string, src: string) {
3838
return null;
3939
}
4040

41-
function createContext(imageAfter: HTMLImageElement, imageBefore: HTMLImageElement) {
41+
function createContext(imageAfter: HTMLImageElement, imageBefore: HTMLImageElement, svgBoundsInfo: any) {
4242
const sizeAfter = {
43-
width: imageAfter?.width || 0,
44-
height: imageAfter?.height || 0,
43+
width: svgBoundsInfo.after?.width || imageAfter?.width || 0,
44+
height: svgBoundsInfo.after?.height || imageAfter?.height || 0,
4545
};
4646
const sizeBefore = {
47-
width: imageBefore?.width || 0,
48-
height: imageBefore?.height || 0,
47+
width: svgBoundsInfo.before?.width || imageBefore?.width || 0,
48+
height: svgBoundsInfo.before?.height || imageBefore?.height || 0,
4949
};
5050
const maxSize = {
5151
width: Math.max(sizeBefore.width, sizeAfter.width),
@@ -92,7 +92,8 @@ class ImageDiff {
9292
boundsInfo: containerEl.querySelector('.bounds-info-before'),
9393
}];
9494

95-
await Promise.all(imageInfos.map(async (info) => {
95+
const svgBoundsInfo: any = {before: null, after: null};
96+
await Promise.all(imageInfos.map(async (info, index) => {
9697
const [success] = await Promise.all(Array.from(info.images, (img) => {
9798
return loadElem(img, info.path);
9899
}));
@@ -102,11 +103,8 @@ class ImageDiff {
102103
const resp = await GET(info.path);
103104
const text = await resp.text();
104105
const bounds = getDefaultSvgBoundsIfUndefined(text, info.path);
106+
svgBoundsInfo[index === 0 ? 'after' : 'before'] = bounds;
105107
if (bounds) {
106-
for (const el of info.images) {
107-
el.setAttribute('width', String(bounds.width));
108-
el.setAttribute('height', String(bounds.height));
109-
}
110108
hideElem(info.boundsInfo);
111109
}
112110
}
@@ -115,10 +113,10 @@ class ImageDiff {
115113
const imagesAfter = imageInfos[0].images;
116114
const imagesBefore = imageInfos[1].images;
117115

118-
this.initSideBySide(createContext(imagesAfter[0], imagesBefore[0]));
116+
this.initSideBySide(createContext(imagesAfter[0], imagesBefore[0], svgBoundsInfo));
119117
if (imagesAfter.length > 0 && imagesBefore.length > 0) {
120-
this.initSwipe(createContext(imagesAfter[1], imagesBefore[1]));
121-
this.initOverlay(createContext(imagesAfter[2], imagesBefore[2]));
118+
this.initSwipe(createContext(imagesAfter[1], imagesBefore[1], svgBoundsInfo));
119+
this.initOverlay(createContext(imagesAfter[2], imagesBefore[2], svgBoundsInfo));
122120
}
123121
queryElemChildren(containerEl, '.image-diff-tabs', (el) => el.classList.remove('is-loading'));
124122
}

0 commit comments

Comments
 (0)