2022-01-28 14:00:11 -07:00
|
|
|
import $ from 'jquery';
|
2023-10-11 06:34:21 -06:00
|
|
|
import {GET} from '../modules/fetch.js';
|
|
|
|
import {hideElem, loadElem} from '../utils/dom.js';
|
|
|
|
import {parseDom} from '../utils.js';
|
2022-01-28 14:00:11 -07:00
|
|
|
|
2023-10-11 06:34:21 -06:00
|
|
|
function getDefaultSvgBoundsIfUndefined(text, src) {
|
2021-06-05 06:32:19 -06:00
|
|
|
const DefaultSize = 300;
|
|
|
|
const MaxSize = 99999;
|
|
|
|
|
2023-10-11 06:34:21 -06:00
|
|
|
const svgDoc = parseDom(text, 'image/svg+xml');
|
|
|
|
const svg = svgDoc.documentElement;
|
2022-04-25 21:14:01 -06:00
|
|
|
const width = svg?.width?.baseVal;
|
|
|
|
const height = svg?.height?.baseVal;
|
|
|
|
if (width === undefined || height === undefined) {
|
|
|
|
return null; // in case some svg is invalid or doesn't have the width/height
|
|
|
|
}
|
2021-06-05 06:32:19 -06:00
|
|
|
if (width.unitType === SVGLength.SVG_LENGTHTYPE_PERCENTAGE || height.unitType === SVGLength.SVG_LENGTHTYPE_PERCENTAGE) {
|
|
|
|
const img = new Image();
|
|
|
|
img.src = src;
|
|
|
|
if (img.width > 1 && img.width < MaxSize && img.height > 1 && img.height < MaxSize) {
|
|
|
|
return {
|
|
|
|
width: img.width,
|
2024-03-22 08:06:53 -06:00
|
|
|
height: img.height,
|
2021-06-05 06:32:19 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (svg.hasAttribute('viewBox')) {
|
|
|
|
const viewBox = svg.viewBox.baseVal;
|
|
|
|
return {
|
|
|
|
width: DefaultSize,
|
2024-03-22 08:06:53 -06:00
|
|
|
height: DefaultSize * viewBox.width / viewBox.height,
|
2021-06-05 06:32:19 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
width: DefaultSize,
|
2024-03-22 08:06:53 -06:00
|
|
|
height: DefaultSize,
|
2021-06-05 06:32:19 -06:00
|
|
|
};
|
|
|
|
}
|
2022-04-25 21:14:01 -06:00
|
|
|
return null;
|
2021-06-05 06:32:19 -06:00
|
|
|
}
|
|
|
|
|
2022-12-23 09:03:11 -07:00
|
|
|
export function initImageDiff() {
|
2021-02-27 10:25:00 -07:00
|
|
|
function createContext(image1, image2) {
|
|
|
|
const size1 = {
|
|
|
|
width: image1 && image1.width || 0,
|
2024-03-22 08:06:53 -06:00
|
|
|
height: image1 && image1.height || 0,
|
2021-02-27 10:25:00 -07:00
|
|
|
};
|
|
|
|
const size2 = {
|
|
|
|
width: image2 && image2.width || 0,
|
2024-03-22 08:06:53 -06:00
|
|
|
height: image2 && image2.height || 0,
|
2021-02-27 10:25:00 -07:00
|
|
|
};
|
|
|
|
const max = {
|
|
|
|
width: Math.max(size2.width, size1.width),
|
2024-03-22 08:06:53 -06:00
|
|
|
height: Math.max(size2.height, size1.height),
|
2021-02-27 10:25:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2024-03-23 20:01:57 -06:00
|
|
|
$image1: $(image1),
|
|
|
|
$image2: $(image2),
|
2021-02-27 10:25:00 -07:00
|
|
|
size1,
|
|
|
|
size2,
|
|
|
|
max,
|
|
|
|
ratio: [
|
|
|
|
Math.floor(max.width - size1.width) / 2,
|
|
|
|
Math.floor(max.height - size1.height) / 2,
|
|
|
|
Math.floor(max.width - size2.width) / 2,
|
2024-03-22 08:06:53 -06:00
|
|
|
Math.floor(max.height - size2.height) / 2,
|
|
|
|
],
|
2021-02-27 10:25:00 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-10-11 06:34:21 -06:00
|
|
|
$('.image-diff:not([data-image-diff-loaded])').each(async function() {
|
2021-02-27 10:25:00 -07:00
|
|
|
const $container = $(this);
|
2024-03-19 17:49:15 -06:00
|
|
|
this.setAttribute('data-image-diff-loaded', 'true');
|
2021-06-05 06:32:19 -06:00
|
|
|
|
2022-06-08 11:19:06 -06:00
|
|
|
// the container may be hidden by "viewed" checkbox, so use the parent's width for reference
|
|
|
|
const diffContainerWidth = Math.max($container.closest('.diff-file-box').width() - 300, 100);
|
2021-02-27 10:25:00 -07:00
|
|
|
|
|
|
|
const imageInfos = [{
|
2023-10-11 06:34:21 -06:00
|
|
|
path: this.getAttribute('data-path-after'),
|
|
|
|
mime: this.getAttribute('data-mime-after'),
|
|
|
|
$images: $container.find('img.image-after'), // matches 3 <img>
|
2024-03-22 08:06:53 -06:00
|
|
|
$boundsInfo: $container.find('.bounds-info-after'),
|
2021-02-27 10:25:00 -07:00
|
|
|
}, {
|
2023-10-11 06:34:21 -06:00
|
|
|
path: this.getAttribute('data-path-before'),
|
|
|
|
mime: this.getAttribute('data-mime-before'),
|
|
|
|
$images: $container.find('img.image-before'), // matches 3 <img>
|
2024-03-22 08:06:53 -06:00
|
|
|
$boundsInfo: $container.find('.bounds-info-before'),
|
2021-02-27 10:25:00 -07:00
|
|
|
}];
|
|
|
|
|
2023-10-11 06:34:21 -06:00
|
|
|
await Promise.all(imageInfos.map(async (info) => {
|
|
|
|
const [success] = await Promise.all(Array.from(info.$images, (img) => {
|
|
|
|
return loadElem(img, info.path);
|
|
|
|
}));
|
|
|
|
// only the first images is associated with $boundsInfo
|
|
|
|
if (!success) info.$boundsInfo.text('(image error)');
|
|
|
|
if (info.mime === 'image/svg+xml') {
|
|
|
|
const resp = await GET(info.path);
|
|
|
|
const text = await resp.text();
|
|
|
|
const bounds = getDefaultSvgBoundsIfUndefined(text, info.path);
|
|
|
|
if (bounds) {
|
2024-03-23 05:51:34 -06:00
|
|
|
info.$images.each(function() {
|
|
|
|
this.setAttribute('width', bounds.width);
|
|
|
|
this.setAttribute('height', bounds.height);
|
|
|
|
});
|
2023-10-11 06:34:21 -06:00
|
|
|
hideElem(info.$boundsInfo);
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
}
|
2023-10-11 06:34:21 -06:00
|
|
|
}));
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2023-10-11 06:34:21 -06:00
|
|
|
const $imagesAfter = imageInfos[0].$images;
|
|
|
|
const $imagesBefore = imageInfos[1].$images;
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2023-10-11 06:34:21 -06:00
|
|
|
initSideBySide(createContext($imagesAfter[0], $imagesBefore[0]));
|
|
|
|
if ($imagesAfter.length > 0 && $imagesBefore.length > 0) {
|
|
|
|
initSwipe(createContext($imagesAfter[1], $imagesBefore[1]));
|
|
|
|
initOverlay(createContext($imagesAfter[2], $imagesBefore[2]));
|
2021-02-27 10:25:00 -07:00
|
|
|
}
|
|
|
|
|
2023-10-11 06:34:21 -06:00
|
|
|
$container.find('> .image-diff-tabs').removeClass('is-loading');
|
|
|
|
|
2021-02-27 10:25:00 -07:00
|
|
|
function initSideBySide(sizes) {
|
|
|
|
let factor = 1;
|
|
|
|
if (sizes.max.width > (diffContainerWidth - 24) / 2) {
|
|
|
|
factor = (diffContainerWidth - 24) / 2 / sizes.max.width;
|
|
|
|
}
|
|
|
|
|
2024-03-23 20:01:57 -06:00
|
|
|
const widthChanged = sizes.$image1.length !== 0 && sizes.$image2.length !== 0 && sizes.$image1[0].naturalWidth !== sizes.$image2[0].naturalWidth;
|
|
|
|
const heightChanged = sizes.$image1.length !== 0 && sizes.$image2.length !== 0 && sizes.$image1[0].naturalHeight !== sizes.$image2[0].naturalHeight;
|
|
|
|
if (sizes.$image1.length !== 0) {
|
|
|
|
$container.find('.bounds-info-after .bounds-info-width').text(`${sizes.$image1[0].naturalWidth}px`).addClass(widthChanged ? 'green' : '');
|
|
|
|
$container.find('.bounds-info-after .bounds-info-height').text(`${sizes.$image1[0].naturalHeight}px`).addClass(heightChanged ? 'green' : '');
|
2021-06-05 06:32:19 -06:00
|
|
|
}
|
2024-03-23 20:01:57 -06:00
|
|
|
if (sizes.$image2.length !== 0) {
|
|
|
|
$container.find('.bounds-info-before .bounds-info-width').text(`${sizes.$image2[0].naturalWidth}px`).addClass(widthChanged ? 'red' : '');
|
|
|
|
$container.find('.bounds-info-before .bounds-info-height').text(`${sizes.$image2[0].naturalHeight}px`).addClass(heightChanged ? 'red' : '');
|
2021-06-05 06:32:19 -06:00
|
|
|
}
|
|
|
|
|
2024-03-23 20:01:57 -06:00
|
|
|
const image1 = sizes.$image1[0];
|
2024-03-19 10:28:46 -06:00
|
|
|
if (image1) {
|
|
|
|
const container = image1.parentNode;
|
|
|
|
image1.style.width = `${sizes.size1.width * factor}px`;
|
|
|
|
image1.style.height = `${sizes.size1.height * factor}px`;
|
|
|
|
container.style.margin = '10px auto';
|
|
|
|
container.style.width = `${sizes.size1.width * factor + 2}px`;
|
|
|
|
container.style.height = `${sizes.size1.height * factor + 2}px`;
|
|
|
|
}
|
|
|
|
|
2024-03-23 20:01:57 -06:00
|
|
|
const image2 = sizes.$image2[0];
|
2024-03-19 10:28:46 -06:00
|
|
|
if (image2) {
|
|
|
|
const container = image2.parentNode;
|
|
|
|
image2.style.width = `${sizes.size2.width * factor}px`;
|
|
|
|
image2.style.height = `${sizes.size2.height * factor}px`;
|
|
|
|
container.style.margin = '10px auto';
|
|
|
|
container.style.width = `${sizes.size2.width * factor + 2}px`;
|
|
|
|
container.style.height = `${sizes.size2.height * factor + 2}px`;
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function initSwipe(sizes) {
|
|
|
|
let factor = 1;
|
|
|
|
if (sizes.max.width > diffContainerWidth - 12) {
|
|
|
|
factor = (diffContainerWidth - 12) / sizes.max.width;
|
|
|
|
}
|
|
|
|
|
2024-03-23 20:01:57 -06:00
|
|
|
const image1 = sizes.$image1[0];
|
2024-03-19 10:28:46 -06:00
|
|
|
if (image1) {
|
|
|
|
const container = image1.parentNode;
|
|
|
|
const swipeFrame = container.parentNode;
|
|
|
|
image1.style.width = `${sizes.size1.width * factor}px`;
|
|
|
|
image1.style.height = `${sizes.size1.height * factor}px`;
|
|
|
|
container.style.margin = `0px ${sizes.ratio[0] * factor}px`;
|
|
|
|
container.style.width = `${sizes.size1.width * factor + 2}px`;
|
|
|
|
container.style.height = `${sizes.size1.height * factor + 2}px`;
|
|
|
|
swipeFrame.style.padding = `${sizes.ratio[1] * factor}px 0 0 0`;
|
|
|
|
swipeFrame.style.width = `${sizes.max.width * factor + 2}px`;
|
|
|
|
}
|
|
|
|
|
2024-03-23 20:01:57 -06:00
|
|
|
const image2 = sizes.$image2[0];
|
2024-03-19 10:28:46 -06:00
|
|
|
if (image2) {
|
|
|
|
const container = image2.parentNode;
|
|
|
|
const swipeFrame = container.parentNode;
|
|
|
|
image2.style.width = `${sizes.size2.width * factor}px`;
|
|
|
|
image2.style.height = `${sizes.size2.height * factor}px`;
|
|
|
|
container.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
|
|
|
|
container.style.width = `${sizes.size2.width * factor + 2}px`;
|
|
|
|
container.style.height = `${sizes.size2.height * factor + 2}px`;
|
|
|
|
swipeFrame.style.width = `${sizes.max.width * factor + 2}px`;
|
|
|
|
swipeFrame.style.height = `${sizes.max.height * factor + 2}px`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// extra height for inner "position: absolute" elements
|
|
|
|
const swipe = $container.find('.diff-swipe')[0];
|
|
|
|
if (swipe) {
|
|
|
|
swipe.style.width = `${sizes.max.width * factor + 2}px`;
|
|
|
|
swipe.style.height = `${sizes.max.height * factor + 30}px`;
|
|
|
|
}
|
|
|
|
|
2021-02-27 10:25:00 -07:00
|
|
|
$container.find('.swipe-bar').on('mousedown', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const $swipeBar = $(this);
|
|
|
|
const $swipeFrame = $swipeBar.parent();
|
|
|
|
const width = $swipeFrame.width() - $swipeBar.width() - 2;
|
|
|
|
|
|
|
|
$(document).on('mousemove.diff-swipe', (e2) => {
|
|
|
|
e2.preventDefault();
|
|
|
|
|
|
|
|
const value = Math.max(0, Math.min(e2.clientX - $swipeFrame.offset().left, width));
|
2024-03-19 10:28:46 -06:00
|
|
|
$swipeBar[0].style.left = `${value}px`;
|
|
|
|
$container.find('.swipe-container')[0].style.width = `${$swipeFrame.width() - value}px`;
|
2021-02-27 10:25:00 -07:00
|
|
|
|
|
|
|
$(document).on('mouseup.diff-swipe', () => {
|
|
|
|
$(document).off('.diff-swipe');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initOverlay(sizes) {
|
|
|
|
let factor = 1;
|
|
|
|
if (sizes.max.width > diffContainerWidth - 12) {
|
|
|
|
factor = (diffContainerWidth - 12) / sizes.max.width;
|
|
|
|
}
|
|
|
|
|
2024-03-23 20:01:57 -06:00
|
|
|
const image1 = sizes.$image1[0];
|
|
|
|
if (image1) {
|
|
|
|
const container = image1.parentNode;
|
|
|
|
image1.style.width = `${sizes.size1.width * factor}px`;
|
|
|
|
image1.style.height = `${sizes.size1.height * factor}px`;
|
|
|
|
container.style.margin = `${sizes.ratio[1] * factor}px ${sizes.ratio[0] * factor}px`;
|
|
|
|
container.style.width = `${sizes.size1.width * factor + 2}px`;
|
|
|
|
container.style.height = `${sizes.size1.height * factor + 2}px`;
|
|
|
|
}
|
2023-03-07 05:11:24 -07:00
|
|
|
|
2024-03-23 20:01:57 -06:00
|
|
|
const image2 = sizes.$image2[0];
|
|
|
|
if (image2) {
|
|
|
|
const container = image2.parentNode;
|
|
|
|
const overlayFrame = container.parentNode;
|
|
|
|
image2.style.width = `${sizes.size2.width * factor}px`;
|
|
|
|
image2.style.height = `${sizes.size2.height * factor}px`;
|
|
|
|
container.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
|
|
|
|
container.style.width = `${sizes.size2.width * factor + 2}px`;
|
|
|
|
container.style.height = `${sizes.size2.height * factor + 2}px`;
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-03-23 20:01:57 -06:00
|
|
|
// some inner elements are `position: absolute`, so the container's height must be large enough
|
|
|
|
overlayFrame.style.width = `${sizes.max.width * factor + 2}px`;
|
|
|
|
overlayFrame.style.height = `${sizes.max.height * factor + 2}px`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rangeInput = $container[0].querySelector('input[type="range"]');
|
|
|
|
function updateOpacity() {
|
|
|
|
if (sizes?.$image1?.[0]) {
|
|
|
|
sizes.$image1[0].parentNode.style.opacity = `${rangeInput.value / 100}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rangeInput?.addEventListener('input', updateOpacity);
|
|
|
|
updateOpacity();
|
2021-02-27 10:25:00 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|