2024-07-07 09:32:30 -06:00
|
|
|
import {GET} from '../modules/fetch.ts';
|
|
|
|
import {hideElem, loadElem, queryElemChildren, queryElems} from '../utils/dom.ts';
|
|
|
|
import {parseDom} from '../utils.ts';
|
2024-11-07 23:04:24 -07:00
|
|
|
import {fomanticQuery} from '../modules/fomantic/base.ts';
|
2022-01-28 14:00:11 -07:00
|
|
|
|
2023-10-11 06:34:21 -06:00
|
|
|
function getDefaultSvgBoundsIfUndefined(text, src) {
|
2024-06-21 22:52:09 -06:00
|
|
|
const defaultSize = 300;
|
|
|
|
const maxSize = 99999;
|
2021-06-05 06:32:19 -06:00
|
|
|
|
2023-10-11 06:34:21 -06:00
|
|
|
const svgDoc = parseDom(text, 'image/svg+xml');
|
2024-11-07 23:04:24 -07:00
|
|
|
const svg = (svgDoc.documentElement as unknown) as SVGSVGElement;
|
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;
|
2024-06-21 22:52:09 -06:00
|
|
|
if (img.width > 1 && img.width < maxSize && img.height > 1 && img.height < maxSize) {
|
2021-06-05 06:32:19 -06:00
|
|
|
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 {
|
2024-06-21 22:52:09 -06:00
|
|
|
width: defaultSize,
|
|
|
|
height: defaultSize * viewBox.width / viewBox.height,
|
2021-06-05 06:32:19 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
2024-06-21 22:52:09 -06:00
|
|
|
width: defaultSize,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-04-25 07:01:38 -06:00
|
|
|
function createContext(imageAfter, imageBefore) {
|
|
|
|
const sizeAfter = {
|
|
|
|
width: imageAfter?.width || 0,
|
|
|
|
height: imageAfter?.height || 0,
|
|
|
|
};
|
|
|
|
const sizeBefore = {
|
|
|
|
width: imageBefore?.width || 0,
|
|
|
|
height: imageBefore?.height || 0,
|
|
|
|
};
|
|
|
|
const maxSize = {
|
|
|
|
width: Math.max(sizeBefore.width, sizeAfter.width),
|
|
|
|
height: Math.max(sizeBefore.height, sizeAfter.height),
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
imageAfter,
|
|
|
|
imageBefore,
|
|
|
|
sizeAfter,
|
|
|
|
sizeBefore,
|
|
|
|
maxSize,
|
|
|
|
ratio: [
|
|
|
|
Math.floor(maxSize.width - sizeAfter.width) / 2,
|
|
|
|
Math.floor(maxSize.height - sizeAfter.height) / 2,
|
|
|
|
Math.floor(maxSize.width - sizeBefore.width) / 2,
|
|
|
|
Math.floor(maxSize.height - sizeBefore.height) / 2,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
class ImageDiff {
|
2024-11-07 23:04:24 -07:00
|
|
|
containerEl: HTMLElement;
|
|
|
|
diffContainerWidth: number;
|
|
|
|
|
|
|
|
async init(containerEl: HTMLElement) {
|
2024-06-21 22:52:09 -06:00
|
|
|
this.containerEl = containerEl;
|
|
|
|
containerEl.setAttribute('data-image-diff-loaded', 'true');
|
|
|
|
|
2024-11-07 23:04:24 -07:00
|
|
|
fomanticQuery(containerEl).find('.ui.menu.tabular .item').tab({autoTabActivation: false});
|
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
|
2024-06-21 22:52:09 -06:00
|
|
|
this.diffContainerWidth = Math.max(containerEl.closest('.diff-file-box').clientWidth - 300, 100);
|
2021-02-27 10:25:00 -07:00
|
|
|
|
|
|
|
const imageInfos = [{
|
2024-06-21 22:52:09 -06:00
|
|
|
path: containerEl.getAttribute('data-path-after'),
|
|
|
|
mime: containerEl.getAttribute('data-mime-after'),
|
2024-11-07 23:04:24 -07:00
|
|
|
images: containerEl.querySelectorAll<HTMLImageElement>('img.image-after'), // matches 3 <img>
|
2024-06-21 22:52:09 -06:00
|
|
|
boundsInfo: containerEl.querySelector('.bounds-info-after'),
|
2021-02-27 10:25:00 -07:00
|
|
|
}, {
|
2024-06-21 22:52:09 -06:00
|
|
|
path: containerEl.getAttribute('data-path-before'),
|
|
|
|
mime: containerEl.getAttribute('data-mime-before'),
|
2024-11-07 23:04:24 -07:00
|
|
|
images: containerEl.querySelectorAll<HTMLImageElement>('img.image-before'), // matches 3 <img>
|
2024-06-21 22:52:09 -06:00
|
|
|
boundsInfo: containerEl.querySelector('.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) => {
|
2024-06-21 22:52:09 -06:00
|
|
|
const [success] = await Promise.all(Array.from(info.images, (img) => {
|
2023-10-11 06:34:21 -06:00
|
|
|
return loadElem(img, info.path);
|
|
|
|
}));
|
2024-06-10 04:12:31 -06:00
|
|
|
// only the first images is associated with boundsInfo
|
|
|
|
if (!success && info.boundsInfo) info.boundsInfo.textContent = '(image error)';
|
2023-10-11 06:34:21 -06:00
|
|
|
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-06-21 22:52:09 -06:00
|
|
|
for (const el of info.images) {
|
2024-11-07 23:04:24 -07:00
|
|
|
el.setAttribute('width', String(bounds.width));
|
|
|
|
el.setAttribute('height', String(bounds.height));
|
2024-06-21 22:52:09 -06:00
|
|
|
}
|
2024-06-10 04:12:31 -06:00
|
|
|
hideElem(info.boundsInfo);
|
2023-10-11 06:34:21 -06:00
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
}
|
2023-10-11 06:34:21 -06:00
|
|
|
}));
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
const imagesAfter = imageInfos[0].images;
|
|
|
|
const imagesBefore = imageInfos[1].images;
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
this.initSideBySide(createContext(imagesAfter[0], imagesBefore[0]));
|
|
|
|
if (imagesAfter.length > 0 && imagesBefore.length > 0) {
|
|
|
|
this.initSwipe(createContext(imagesAfter[1], imagesBefore[1]));
|
|
|
|
this.initOverlay(createContext(imagesAfter[2], imagesBefore[2]));
|
2021-02-27 10:25:00 -07:00
|
|
|
}
|
2024-06-21 22:52:09 -06:00
|
|
|
queryElemChildren(containerEl, '.image-diff-tabs', (el) => el.classList.remove('is-loading'));
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
initSideBySide(sizes) {
|
|
|
|
let factor = 1;
|
|
|
|
if (sizes.maxSize.width > (this.diffContainerWidth - 24) / 2) {
|
|
|
|
factor = (this.diffContainerWidth - 24) / 2 / sizes.maxSize.width;
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
const widthChanged = sizes.imageAfter && sizes.imageBefore && sizes.imageAfter.naturalWidth !== sizes.imageBefore.naturalWidth;
|
|
|
|
const heightChanged = sizes.imageAfter && sizes.imageBefore && sizes.imageAfter.naturalHeight !== sizes.imageBefore.naturalHeight;
|
|
|
|
if (sizes.imageAfter) {
|
|
|
|
const boundsInfoAfterWidth = this.containerEl.querySelector('.bounds-info-after .bounds-info-width');
|
|
|
|
if (boundsInfoAfterWidth) {
|
|
|
|
boundsInfoAfterWidth.textContent = `${sizes.imageAfter.naturalWidth}px`;
|
|
|
|
boundsInfoAfterWidth.classList.toggle('green', widthChanged);
|
2021-06-05 06:32:19 -06:00
|
|
|
}
|
2024-06-21 22:52:09 -06:00
|
|
|
const boundsInfoAfterHeight = this.containerEl.querySelector('.bounds-info-after .bounds-info-height');
|
|
|
|
if (boundsInfoAfterHeight) {
|
|
|
|
boundsInfoAfterHeight.textContent = `${sizes.imageAfter.naturalHeight}px`;
|
|
|
|
boundsInfoAfterHeight.classList.toggle('green', heightChanged);
|
2021-06-05 06:32:19 -06:00
|
|
|
}
|
2024-06-21 22:52:09 -06:00
|
|
|
}
|
2021-06-05 06:32:19 -06:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
if (sizes.imageBefore) {
|
|
|
|
const boundsInfoBeforeWidth = this.containerEl.querySelector('.bounds-info-before .bounds-info-width');
|
|
|
|
if (boundsInfoBeforeWidth) {
|
|
|
|
boundsInfoBeforeWidth.textContent = `${sizes.imageBefore.naturalWidth}px`;
|
|
|
|
boundsInfoBeforeWidth.classList.toggle('red', widthChanged);
|
2024-03-19 10:28:46 -06:00
|
|
|
}
|
2024-06-21 22:52:09 -06:00
|
|
|
const boundsInfoBeforeHeight = this.containerEl.querySelector('.bounds-info-before .bounds-info-height');
|
|
|
|
if (boundsInfoBeforeHeight) {
|
|
|
|
boundsInfoBeforeHeight.textContent = `${sizes.imageBefore.naturalHeight}px`;
|
2024-11-07 23:04:24 -07:00
|
|
|
boundsInfoBeforeHeight.classList.toggle('red', heightChanged);
|
2024-03-19 10:28:46 -06:00
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
}
|
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
if (sizes.imageAfter) {
|
|
|
|
const container = sizes.imageAfter.parentNode;
|
|
|
|
sizes.imageAfter.style.width = `${sizes.sizeAfter.width * factor}px`;
|
|
|
|
sizes.imageAfter.style.height = `${sizes.sizeAfter.height * factor}px`;
|
|
|
|
container.style.margin = '10px auto';
|
|
|
|
container.style.width = `${sizes.sizeAfter.width * factor + 2}px`;
|
|
|
|
container.style.height = `${sizes.sizeAfter.height * factor + 2}px`;
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
if (sizes.imageBefore) {
|
|
|
|
const container = sizes.imageBefore.parentNode;
|
|
|
|
sizes.imageBefore.style.width = `${sizes.sizeBefore.width * factor}px`;
|
|
|
|
sizes.imageBefore.style.height = `${sizes.sizeBefore.height * factor}px`;
|
|
|
|
container.style.margin = '10px auto';
|
|
|
|
container.style.width = `${sizes.sizeBefore.width * factor + 2}px`;
|
|
|
|
container.style.height = `${sizes.sizeBefore.height * factor + 2}px`;
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 10:28:46 -06:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
initSwipe(sizes) {
|
|
|
|
let factor = 1;
|
|
|
|
if (sizes.maxSize.width > this.diffContainerWidth - 12) {
|
|
|
|
factor = (this.diffContainerWidth - 12) / sizes.maxSize.width;
|
|
|
|
}
|
2024-03-19 10:28:46 -06:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
if (sizes.imageAfter) {
|
|
|
|
const imgParent = sizes.imageAfter.parentNode;
|
|
|
|
const swipeFrame = imgParent.parentNode;
|
|
|
|
sizes.imageAfter.style.width = `${sizes.sizeAfter.width * factor}px`;
|
|
|
|
sizes.imageAfter.style.height = `${sizes.sizeAfter.height * factor}px`;
|
|
|
|
imgParent.style.margin = `0px ${sizes.ratio[0] * factor}px`;
|
|
|
|
imgParent.style.width = `${sizes.sizeAfter.width * factor + 2}px`;
|
|
|
|
imgParent.style.height = `${sizes.sizeAfter.height * factor + 2}px`;
|
|
|
|
swipeFrame.style.padding = `${sizes.ratio[1] * factor}px 0 0 0`;
|
|
|
|
swipeFrame.style.width = `${sizes.maxSize.width * factor + 2}px`;
|
|
|
|
}
|
2024-03-19 10:28:46 -06:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
if (sizes.imageBefore) {
|
|
|
|
const imgParent = sizes.imageBefore.parentNode;
|
|
|
|
const swipeFrame = imgParent.parentNode;
|
|
|
|
sizes.imageBefore.style.width = `${sizes.sizeBefore.width * factor}px`;
|
|
|
|
sizes.imageBefore.style.height = `${sizes.sizeBefore.height * factor}px`;
|
|
|
|
imgParent.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
|
|
|
|
imgParent.style.width = `${sizes.sizeBefore.width * factor + 2}px`;
|
|
|
|
imgParent.style.height = `${sizes.sizeBefore.height * factor + 2}px`;
|
|
|
|
swipeFrame.style.width = `${sizes.maxSize.width * factor + 2}px`;
|
|
|
|
swipeFrame.style.height = `${sizes.maxSize.height * factor + 2}px`;
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
// extra height for inner "position: absolute" elements
|
2024-11-07 23:04:24 -07:00
|
|
|
const swipe = this.containerEl.querySelector<HTMLElement>('.diff-swipe');
|
2024-06-21 22:52:09 -06:00
|
|
|
if (swipe) {
|
|
|
|
swipe.style.width = `${sizes.maxSize.width * factor + 2}px`;
|
|
|
|
swipe.style.height = `${sizes.maxSize.height * factor + 30}px`;
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
this.containerEl.querySelector('.swipe-bar').addEventListener('mousedown', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.initSwipeEventListeners(e.currentTarget);
|
|
|
|
});
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
initSwipeEventListeners(swipeBar) {
|
|
|
|
const swipeFrame = swipeBar.parentNode;
|
|
|
|
const width = swipeFrame.clientWidth;
|
|
|
|
const onSwipeMouseMove = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const rect = swipeFrame.getBoundingClientRect();
|
|
|
|
const value = Math.max(0, Math.min(e.clientX - rect.left, width));
|
|
|
|
swipeBar.style.left = `${value}px`;
|
2024-11-07 23:04:24 -07:00
|
|
|
this.containerEl.querySelector<HTMLElement>('.swipe-container').style.width = `${swipeFrame.clientWidth - value}px`;
|
2024-06-21 22:52:09 -06:00
|
|
|
};
|
|
|
|
const removeEventListeners = () => {
|
|
|
|
document.removeEventListener('mousemove', onSwipeMouseMove);
|
|
|
|
document.removeEventListener('mouseup', removeEventListeners);
|
|
|
|
};
|
|
|
|
document.addEventListener('mousemove', onSwipeMouseMove);
|
|
|
|
document.addEventListener('mouseup', removeEventListeners);
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
initOverlay(sizes) {
|
|
|
|
let factor = 1;
|
|
|
|
if (sizes.maxSize.width > this.diffContainerWidth - 12) {
|
|
|
|
factor = (this.diffContainerWidth - 12) / sizes.maxSize.width;
|
2021-02-27 10:25:00 -07:00
|
|
|
}
|
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
if (sizes.imageAfter) {
|
|
|
|
const container = sizes.imageAfter.parentNode;
|
|
|
|
sizes.imageAfter.style.width = `${sizes.sizeAfter.width * factor}px`;
|
|
|
|
sizes.imageAfter.style.height = `${sizes.sizeAfter.height * factor}px`;
|
|
|
|
container.style.margin = `${sizes.ratio[1] * factor}px ${sizes.ratio[0] * factor}px`;
|
|
|
|
container.style.width = `${sizes.sizeAfter.width * factor + 2}px`;
|
|
|
|
container.style.height = `${sizes.sizeAfter.height * factor + 2}px`;
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
if (sizes.imageBefore) {
|
|
|
|
const container = sizes.imageBefore.parentNode;
|
|
|
|
const overlayFrame = container.parentNode;
|
|
|
|
sizes.imageBefore.style.width = `${sizes.sizeBefore.width * factor}px`;
|
|
|
|
sizes.imageBefore.style.height = `${sizes.sizeBefore.height * factor}px`;
|
|
|
|
container.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
|
|
|
|
container.style.width = `${sizes.sizeBefore.width * factor + 2}px`;
|
|
|
|
container.style.height = `${sizes.sizeBefore.height * factor + 2}px`;
|
2023-03-07 05:11:24 -07:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
// some inner elements are `position: absolute`, so the container's height must be large enough
|
|
|
|
overlayFrame.style.width = `${sizes.maxSize.width * factor + 2}px`;
|
|
|
|
overlayFrame.style.height = `${sizes.maxSize.height * factor + 2}px`;
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
|
2024-11-07 23:04:24 -07:00
|
|
|
const rangeInput = this.containerEl.querySelector<HTMLInputElement>('input[type="range"]');
|
2024-03-23 20:01:57 -06:00
|
|
|
|
2024-06-21 22:52:09 -06:00
|
|
|
function updateOpacity() {
|
|
|
|
if (sizes.imageAfter) {
|
2024-11-07 23:04:24 -07:00
|
|
|
sizes.imageAfter.parentNode.style.opacity = `${Number(rangeInput.value) / 100}`;
|
2024-03-23 20:01:57 -06:00
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
}
|
2024-06-21 22:52:09 -06:00
|
|
|
|
|
|
|
rangeInput?.addEventListener('input', updateOpacity);
|
|
|
|
updateOpacity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initImageDiff() {
|
2024-11-07 23:04:24 -07:00
|
|
|
for (const el of queryElems<HTMLImageElement>(document, '.image-diff:not([data-image-diff-loaded])')) {
|
2024-06-21 22:52:09 -06:00
|
|
|
(new ImageDiff()).init(el); // it is async, but we don't need to await for it
|
|
|
|
}
|
2021-02-27 10:25:00 -07:00
|
|
|
}
|