-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Replace ajax with fetch, improve image diff #27267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
aeb15d9
0290d7d
71d0e18
a4717dd
18c0b39
b4217d4
8039365
9ae1c4a
69d41a2
34a862c
eb064c4
94a6485
c83a48e
e967cb4
f5f72f4
e42dfb8
c6ac19e
f996bc5
acacbca
4c0212a
cc8721f
866f982
31a7de8
126c027
6c3544c
44646eb
12b49b5
06c5496
d34ba3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import $ from 'jquery'; | ||
import {GET} from '../modules/fetch.js'; | ||
import {hideElem} from '../utils/dom.js'; | ||
import {parseUrl, parseXml} from '../utils.js'; | ||
|
||
function getDefaultSvgBoundsIfUndefined(svgXml, src) { | ||
function getDefaultSvgBoundsIfUndefined(text, src) { | ||
const DefaultSize = 300; | ||
const MaxSize = 99999; | ||
|
||
const svg = svgXml.documentElement; | ||
const svgDoc = parseXml(text, 'image/svg+xml'); | ||
const svg = svgDoc.documentElement; | ||
const width = svg?.width?.baseVal; | ||
const height = svg?.height?.baseVal; | ||
if (width === undefined || height === undefined) { | ||
|
@@ -65,7 +68,7 @@ export function initImageDiff() { | |
}; | ||
} | ||
|
||
$('.image-diff:not([data-image-diff-loaded])').each(function() { | ||
$('.image-diff:not([data-image-diff-loaded])').each(async function() { | ||
const $container = $(this); | ||
$container.attr('data-image-diff-loaded', 'true'); | ||
|
||
|
@@ -88,29 +91,27 @@ export function initImageDiff() { | |
|
||
for (const info of imageInfos) { | ||
if (info.$image.length > 0) { | ||
$.ajax({ | ||
url: info.path, | ||
success: (data, _, jqXHR) => { | ||
info.$image.on('load', () => { | ||
info.loaded = true; | ||
setReadyIfLoaded(); | ||
}).on('error', () => { | ||
info.loaded = true; | ||
setReadyIfLoaded(); | ||
info.$boundsInfo.text('(image error)'); | ||
}); | ||
info.$image.attr('src', info.path); | ||
info.$image.on('load', () => { | ||
info.loaded = true; | ||
setReadyIfLoaded(); | ||
}).on('error', () => { | ||
info.loaded = true; | ||
setReadyIfLoaded(); | ||
info.$boundsInfo.text('(image error)'); | ||
}); | ||
info.$image.attr('src', info.path); | ||
|
||
if (jqXHR.getResponseHeader('Content-Type') === 'image/svg+xml') { | ||
const bounds = getDefaultSvgBoundsIfUndefined(data, info.path); | ||
if (bounds) { | ||
info.$image.attr('width', bounds.width); | ||
info.$image.attr('height', bounds.height); | ||
hideElem(info.$boundsInfo); | ||
} | ||
} | ||
// this may be dead code as we currently do not render SVGs images in image diffs | ||
if (parseUrl(info.path).pathname.toLowerCase().endsWith('.svg')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if image doesnot ends with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I seems possible for this to occur because of the typesniffer module. Likely need to echo the detected content-type into HTML. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Content-type was good alternative. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const resp = await GET(info.path); | ||
const text = await resp.text(); | ||
const bounds = getDefaultSvgBoundsIfUndefined(text, info.path); | ||
if (bounds) { | ||
info.$image.attr('width', bounds.width); | ||
info.$image.attr('height', bounds.height); | ||
hideElem(info.$boundsInfo); | ||
} | ||
}); | ||
} | ||
} else { | ||
info.loaded = true; | ||
setReadyIfLoaded(); | ||
|
Uh oh!
There was an error while loading. Please reload this page.