|
|
@ -1,14 +1,14 @@ |
|
|
|
import $ from 'jquery'; |
|
|
|
|
|
|
|
import {GET} from '../modules/fetch.ts'; |
|
|
|
import {GET} from '../modules/fetch.ts'; |
|
|
|
import {hideElem, loadElem, queryElemChildren, queryElems} from '../utils/dom.ts'; |
|
|
|
import {hideElem, loadElem, queryElemChildren, queryElems} from '../utils/dom.ts'; |
|
|
|
import {parseDom} from '../utils.ts'; |
|
|
|
import {parseDom} from '../utils.ts'; |
|
|
|
|
|
|
|
import {fomanticQuery} from '../modules/fomantic/base.ts'; |
|
|
|
|
|
|
|
|
|
|
|
function getDefaultSvgBoundsIfUndefined(text, src) { |
|
|
|
function getDefaultSvgBoundsIfUndefined(text, src) { |
|
|
|
const defaultSize = 300; |
|
|
|
const defaultSize = 300; |
|
|
|
const maxSize = 99999; |
|
|
|
const maxSize = 99999; |
|
|
|
|
|
|
|
|
|
|
|
const svgDoc = parseDom(text, 'image/svg+xml'); |
|
|
|
const svgDoc = parseDom(text, 'image/svg+xml'); |
|
|
|
const svg = svgDoc.documentElement; |
|
|
|
const svg = (svgDoc.documentElement as unknown) as SVGSVGElement; |
|
|
|
const width = svg?.width?.baseVal; |
|
|
|
const width = svg?.width?.baseVal; |
|
|
|
const height = svg?.height?.baseVal; |
|
|
|
const height = svg?.height?.baseVal; |
|
|
|
if (width === undefined || height === undefined) { |
|
|
|
if (width === undefined || height === undefined) { |
|
|
@ -68,12 +68,14 @@ function createContext(imageAfter, imageBefore) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class ImageDiff { |
|
|
|
class ImageDiff { |
|
|
|
async init(containerEl) { |
|
|
|
containerEl: HTMLElement; |
|
|
|
|
|
|
|
diffContainerWidth: number; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async init(containerEl: HTMLElement) { |
|
|
|
this.containerEl = containerEl; |
|
|
|
this.containerEl = containerEl; |
|
|
|
containerEl.setAttribute('data-image-diff-loaded', 'true'); |
|
|
|
containerEl.setAttribute('data-image-diff-loaded', 'true'); |
|
|
|
|
|
|
|
|
|
|
|
// the only jQuery usage in this file
|
|
|
|
fomanticQuery(containerEl).find('.ui.menu.tabular .item').tab({autoTabActivation: false}); |
|
|
|
$(containerEl).find('.ui.menu.tabular .item').tab({autoTabActivation: false}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the container may be hidden by "viewed" checkbox, so use the parent's width for reference
|
|
|
|
// the container may be hidden by "viewed" checkbox, so use the parent's width for reference
|
|
|
|
this.diffContainerWidth = Math.max(containerEl.closest('.diff-file-box').clientWidth - 300, 100); |
|
|
|
this.diffContainerWidth = Math.max(containerEl.closest('.diff-file-box').clientWidth - 300, 100); |
|
|
@ -81,12 +83,12 @@ class ImageDiff { |
|
|
|
const imageInfos = [{ |
|
|
|
const imageInfos = [{ |
|
|
|
path: containerEl.getAttribute('data-path-after'), |
|
|
|
path: containerEl.getAttribute('data-path-after'), |
|
|
|
mime: containerEl.getAttribute('data-mime-after'), |
|
|
|
mime: containerEl.getAttribute('data-mime-after'), |
|
|
|
images: containerEl.querySelectorAll('img.image-after'), // matches 3 <img>
|
|
|
|
images: containerEl.querySelectorAll<HTMLImageElement>('img.image-after'), // matches 3 <img>
|
|
|
|
boundsInfo: containerEl.querySelector('.bounds-info-after'), |
|
|
|
boundsInfo: containerEl.querySelector('.bounds-info-after'), |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
path: containerEl.getAttribute('data-path-before'), |
|
|
|
path: containerEl.getAttribute('data-path-before'), |
|
|
|
mime: containerEl.getAttribute('data-mime-before'), |
|
|
|
mime: containerEl.getAttribute('data-mime-before'), |
|
|
|
images: containerEl.querySelectorAll('img.image-before'), // matches 3 <img>
|
|
|
|
images: containerEl.querySelectorAll<HTMLImageElement>('img.image-before'), // matches 3 <img>
|
|
|
|
boundsInfo: containerEl.querySelector('.bounds-info-before'), |
|
|
|
boundsInfo: containerEl.querySelector('.bounds-info-before'), |
|
|
|
}]; |
|
|
|
}]; |
|
|
|
|
|
|
|
|
|
|
@ -102,8 +104,8 @@ class ImageDiff { |
|
|
|
const bounds = getDefaultSvgBoundsIfUndefined(text, info.path); |
|
|
|
const bounds = getDefaultSvgBoundsIfUndefined(text, info.path); |
|
|
|
if (bounds) { |
|
|
|
if (bounds) { |
|
|
|
for (const el of info.images) { |
|
|
|
for (const el of info.images) { |
|
|
|
el.setAttribute('width', bounds.width); |
|
|
|
el.setAttribute('width', String(bounds.width)); |
|
|
|
el.setAttribute('height', bounds.height); |
|
|
|
el.setAttribute('height', String(bounds.height)); |
|
|
|
} |
|
|
|
} |
|
|
|
hideElem(info.boundsInfo); |
|
|
|
hideElem(info.boundsInfo); |
|
|
|
} |
|
|
|
} |
|
|
@ -151,7 +153,7 @@ class ImageDiff { |
|
|
|
const boundsInfoBeforeHeight = this.containerEl.querySelector('.bounds-info-before .bounds-info-height'); |
|
|
|
const boundsInfoBeforeHeight = this.containerEl.querySelector('.bounds-info-before .bounds-info-height'); |
|
|
|
if (boundsInfoBeforeHeight) { |
|
|
|
if (boundsInfoBeforeHeight) { |
|
|
|
boundsInfoBeforeHeight.textContent = `${sizes.imageBefore.naturalHeight}px`; |
|
|
|
boundsInfoBeforeHeight.textContent = `${sizes.imageBefore.naturalHeight}px`; |
|
|
|
boundsInfoBeforeHeight.classList.add('red', heightChanged); |
|
|
|
boundsInfoBeforeHeight.classList.toggle('red', heightChanged); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -205,7 +207,7 @@ class ImageDiff { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// extra height for inner "position: absolute" elements
|
|
|
|
// extra height for inner "position: absolute" elements
|
|
|
|
const swipe = this.containerEl.querySelector('.diff-swipe'); |
|
|
|
const swipe = this.containerEl.querySelector<HTMLElement>('.diff-swipe'); |
|
|
|
if (swipe) { |
|
|
|
if (swipe) { |
|
|
|
swipe.style.width = `${sizes.maxSize.width * factor + 2}px`; |
|
|
|
swipe.style.width = `${sizes.maxSize.width * factor + 2}px`; |
|
|
|
swipe.style.height = `${sizes.maxSize.height * factor + 30}px`; |
|
|
|
swipe.style.height = `${sizes.maxSize.height * factor + 30}px`; |
|
|
@ -225,7 +227,7 @@ class ImageDiff { |
|
|
|
const rect = swipeFrame.getBoundingClientRect(); |
|
|
|
const rect = swipeFrame.getBoundingClientRect(); |
|
|
|
const value = Math.max(0, Math.min(e.clientX - rect.left, width)); |
|
|
|
const value = Math.max(0, Math.min(e.clientX - rect.left, width)); |
|
|
|
swipeBar.style.left = `${value}px`; |
|
|
|
swipeBar.style.left = `${value}px`; |
|
|
|
this.containerEl.querySelector('.swipe-container').style.width = `${swipeFrame.clientWidth - value}px`; |
|
|
|
this.containerEl.querySelector<HTMLElement>('.swipe-container').style.width = `${swipeFrame.clientWidth - value}px`; |
|
|
|
}; |
|
|
|
}; |
|
|
|
const removeEventListeners = () => { |
|
|
|
const removeEventListeners = () => { |
|
|
|
document.removeEventListener('mousemove', onSwipeMouseMove); |
|
|
|
document.removeEventListener('mousemove', onSwipeMouseMove); |
|
|
@ -264,11 +266,11 @@ class ImageDiff { |
|
|
|
overlayFrame.style.height = `${sizes.maxSize.height * factor + 2}px`; |
|
|
|
overlayFrame.style.height = `${sizes.maxSize.height * factor + 2}px`; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const rangeInput = this.containerEl.querySelector('input[type="range"]'); |
|
|
|
const rangeInput = this.containerEl.querySelector<HTMLInputElement>('input[type="range"]'); |
|
|
|
|
|
|
|
|
|
|
|
function updateOpacity() { |
|
|
|
function updateOpacity() { |
|
|
|
if (sizes.imageAfter) { |
|
|
|
if (sizes.imageAfter) { |
|
|
|
sizes.imageAfter.parentNode.style.opacity = `${rangeInput.value / 100}`; |
|
|
|
sizes.imageAfter.parentNode.style.opacity = `${Number(rangeInput.value) / 100}`; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -278,7 +280,7 @@ class ImageDiff { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function initImageDiff() { |
|
|
|
export function initImageDiff() { |
|
|
|
for (const el of queryElems('.image-diff:not([data-image-diff-loaded])')) { |
|
|
|
for (const el of queryElems<HTMLImageElement>(document, '.image-diff:not([data-image-diff-loaded])')) { |
|
|
|
(new ImageDiff()).init(el); // it is async, but we don't need to await for it
|
|
|
|
(new ImageDiff()).init(el); // it is async, but we don't need to await for it
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|