You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

imagediff.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import $ from 'jquery';
  2. import {GET} from '../modules/fetch.js';
  3. import {hideElem, loadElem, queryElemChildren} from '../utils/dom.js';
  4. import {parseDom} from '../utils.js';
  5. function getDefaultSvgBoundsIfUndefined(text, src) {
  6. const DefaultSize = 300;
  7. const MaxSize = 99999;
  8. const svgDoc = parseDom(text, 'image/svg+xml');
  9. const svg = svgDoc.documentElement;
  10. const width = svg?.width?.baseVal;
  11. const height = svg?.height?.baseVal;
  12. if (width === undefined || height === undefined) {
  13. return null; // in case some svg is invalid or doesn't have the width/height
  14. }
  15. if (width.unitType === SVGLength.SVG_LENGTHTYPE_PERCENTAGE || height.unitType === SVGLength.SVG_LENGTHTYPE_PERCENTAGE) {
  16. const img = new Image();
  17. img.src = src;
  18. if (img.width > 1 && img.width < MaxSize && img.height > 1 && img.height < MaxSize) {
  19. return {
  20. width: img.width,
  21. height: img.height,
  22. };
  23. }
  24. if (svg.hasAttribute('viewBox')) {
  25. const viewBox = svg.viewBox.baseVal;
  26. return {
  27. width: DefaultSize,
  28. height: DefaultSize * viewBox.width / viewBox.height,
  29. };
  30. }
  31. return {
  32. width: DefaultSize,
  33. height: DefaultSize,
  34. };
  35. }
  36. return null;
  37. }
  38. function createContext(imageAfter, imageBefore) {
  39. const sizeAfter = {
  40. width: imageAfter?.width || 0,
  41. height: imageAfter?.height || 0,
  42. };
  43. const sizeBefore = {
  44. width: imageBefore?.width || 0,
  45. height: imageBefore?.height || 0,
  46. };
  47. const maxSize = {
  48. width: Math.max(sizeBefore.width, sizeAfter.width),
  49. height: Math.max(sizeBefore.height, sizeAfter.height),
  50. };
  51. return {
  52. imageAfter,
  53. imageBefore,
  54. sizeAfter,
  55. sizeBefore,
  56. maxSize,
  57. ratio: [
  58. Math.floor(maxSize.width - sizeAfter.width) / 2,
  59. Math.floor(maxSize.height - sizeAfter.height) / 2,
  60. Math.floor(maxSize.width - sizeBefore.width) / 2,
  61. Math.floor(maxSize.height - sizeBefore.height) / 2,
  62. ],
  63. };
  64. }
  65. export function initImageDiff() {
  66. $('.image-diff:not([data-image-diff-loaded])').each(async function() {
  67. const $container = $(this);
  68. this.setAttribute('data-image-diff-loaded', 'true');
  69. // the container may be hidden by "viewed" checkbox, so use the parent's width for reference
  70. const diffContainerWidth = Math.max($container.closest('.diff-file-box').width() - 300, 100);
  71. const imageInfos = [{
  72. path: this.getAttribute('data-path-after'),
  73. mime: this.getAttribute('data-mime-after'),
  74. $images: $container.find('img.image-after'), // matches 3 <img>
  75. $boundsInfo: $container.find('.bounds-info-after'),
  76. }, {
  77. path: this.getAttribute('data-path-before'),
  78. mime: this.getAttribute('data-mime-before'),
  79. $images: $container.find('img.image-before'), // matches 3 <img>
  80. $boundsInfo: $container.find('.bounds-info-before'),
  81. }];
  82. await Promise.all(imageInfos.map(async (info) => {
  83. const [success] = await Promise.all(Array.from(info.$images, (img) => {
  84. return loadElem(img, info.path);
  85. }));
  86. // only the first images is associated with $boundsInfo
  87. if (!success) info.$boundsInfo.text('(image error)');
  88. if (info.mime === 'image/svg+xml') {
  89. const resp = await GET(info.path);
  90. const text = await resp.text();
  91. const bounds = getDefaultSvgBoundsIfUndefined(text, info.path);
  92. if (bounds) {
  93. info.$images.each(function() {
  94. this.setAttribute('width', bounds.width);
  95. this.setAttribute('height', bounds.height);
  96. });
  97. hideElem(info.$boundsInfo);
  98. }
  99. }
  100. }));
  101. const $imagesAfter = imageInfos[0].$images;
  102. const $imagesBefore = imageInfos[1].$images;
  103. initSideBySide(this, createContext($imagesAfter[0], $imagesBefore[0]));
  104. if ($imagesAfter.length > 0 && $imagesBefore.length > 0) {
  105. initSwipe(createContext($imagesAfter[1], $imagesBefore[1]));
  106. initOverlay(createContext($imagesAfter[2], $imagesBefore[2]));
  107. }
  108. queryElemChildren(this, '.image-diff-tabs', (el) => el.classList.remove('is-loading'));
  109. function initSideBySide(container, sizes) {
  110. let factor = 1;
  111. if (sizes.maxSize.width > (diffContainerWidth - 24) / 2) {
  112. factor = (diffContainerWidth - 24) / 2 / sizes.maxSize.width;
  113. }
  114. const widthChanged = sizes.imageAfter && sizes.imageBefore && sizes.imageAfter.naturalWidth !== sizes.imageBefore.naturalWidth;
  115. const heightChanged = sizes.imageAfter && sizes.imageBefore && sizes.imageAfter.naturalHeight !== sizes.imageBefore.naturalHeight;
  116. if (sizes.imageAfter) {
  117. const boundsInfoAfterWidth = container.querySelector('.bounds-info-after .bounds-info-width');
  118. if (boundsInfoAfterWidth) {
  119. boundsInfoAfterWidth.textContent = `${sizes.imageAfter.naturalWidth}px`;
  120. boundsInfoAfterWidth.classList.toggle('green', widthChanged);
  121. }
  122. const boundsInfoAfterHeight = container.querySelector('.bounds-info-after .bounds-info-height');
  123. if (boundsInfoAfterHeight) {
  124. boundsInfoAfterHeight.textContent = `${sizes.imageAfter.naturalHeight}px`;
  125. boundsInfoAfterHeight.classList.toggle('green', heightChanged);
  126. }
  127. }
  128. if (sizes.imageBefore) {
  129. const boundsInfoBeforeWidth = container.querySelector('.bounds-info-before .bounds-info-width');
  130. if (boundsInfoBeforeWidth) {
  131. boundsInfoBeforeWidth.textContent = `${sizes.imageBefore.naturalWidth}px`;
  132. boundsInfoBeforeWidth.classList.toggle('red', widthChanged);
  133. }
  134. const boundsInfoBeforeHeight = container.querySelector('.bounds-info-before .bounds-info-height');
  135. if (boundsInfoBeforeHeight) {
  136. boundsInfoBeforeHeight.textContent = `${sizes.imageBefore.naturalHeight}px`;
  137. boundsInfoBeforeHeight.classList.add('red', heightChanged);
  138. }
  139. }
  140. if (sizes.imageAfter) {
  141. const container = sizes.imageAfter.parentNode;
  142. sizes.imageAfter.style.width = `${sizes.sizeAfter.width * factor}px`;
  143. sizes.imageAfter.style.height = `${sizes.sizeAfter.height * factor}px`;
  144. container.style.margin = '10px auto';
  145. container.style.width = `${sizes.sizeAfter.width * factor + 2}px`;
  146. container.style.height = `${sizes.sizeAfter.height * factor + 2}px`;
  147. }
  148. if (sizes.imageBefore) {
  149. const container = sizes.imageBefore.parentNode;
  150. sizes.imageBefore.style.width = `${sizes.sizeBefore.width * factor}px`;
  151. sizes.imageBefore.style.height = `${sizes.sizeBefore.height * factor}px`;
  152. container.style.margin = '10px auto';
  153. container.style.width = `${sizes.sizeBefore.width * factor + 2}px`;
  154. container.style.height = `${sizes.sizeBefore.height * factor + 2}px`;
  155. }
  156. }
  157. function initSwipe(sizes) {
  158. let factor = 1;
  159. if (sizes.maxSize.width > diffContainerWidth - 12) {
  160. factor = (diffContainerWidth - 12) / sizes.maxSize.width;
  161. }
  162. if (sizes.imageAfter) {
  163. const container = sizes.imageAfter.parentNode;
  164. const swipeFrame = container.parentNode;
  165. sizes.imageAfter.style.width = `${sizes.sizeAfter.width * factor}px`;
  166. sizes.imageAfter.style.height = `${sizes.sizeAfter.height * factor}px`;
  167. container.style.margin = `0px ${sizes.ratio[0] * factor}px`;
  168. container.style.width = `${sizes.sizeAfter.width * factor + 2}px`;
  169. container.style.height = `${sizes.sizeAfter.height * factor + 2}px`;
  170. swipeFrame.style.padding = `${sizes.ratio[1] * factor}px 0 0 0`;
  171. swipeFrame.style.width = `${sizes.maxSize.width * factor + 2}px`;
  172. }
  173. if (sizes.imageBefore) {
  174. const container = sizes.imageBefore.parentNode;
  175. const swipeFrame = container.parentNode;
  176. sizes.imageBefore.style.width = `${sizes.sizeBefore.width * factor}px`;
  177. sizes.imageBefore.style.height = `${sizes.sizeBefore.height * factor}px`;
  178. container.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
  179. container.style.width = `${sizes.sizeBefore.width * factor + 2}px`;
  180. container.style.height = `${sizes.sizeBefore.height * factor + 2}px`;
  181. swipeFrame.style.width = `${sizes.maxSize.width * factor + 2}px`;
  182. swipeFrame.style.height = `${sizes.maxSize.height * factor + 2}px`;
  183. }
  184. // extra height for inner "position: absolute" elements
  185. const swipe = $container.find('.diff-swipe')[0];
  186. if (swipe) {
  187. swipe.style.width = `${sizes.maxSize.width * factor + 2}px`;
  188. swipe.style.height = `${sizes.maxSize.height * factor + 30}px`;
  189. }
  190. $container.find('.swipe-bar').on('mousedown', function(e) {
  191. e.preventDefault();
  192. const $swipeBar = $(this);
  193. const $swipeFrame = $swipeBar.parent();
  194. const width = $swipeFrame.width() - $swipeBar.width() - 2;
  195. $(document).on('mousemove.diff-swipe', (e2) => {
  196. e2.preventDefault();
  197. const value = Math.max(0, Math.min(e2.clientX - $swipeFrame.offset().left, width));
  198. $swipeBar[0].style.left = `${value}px`;
  199. $container.find('.swipe-container')[0].style.width = `${$swipeFrame.width() - value}px`;
  200. $(document).on('mouseup.diff-swipe', () => {
  201. $(document).off('.diff-swipe');
  202. });
  203. });
  204. });
  205. }
  206. function initOverlay(sizes) {
  207. let factor = 1;
  208. if (sizes.maxSize.width > diffContainerWidth - 12) {
  209. factor = (diffContainerWidth - 12) / sizes.maxSize.width;
  210. }
  211. if (sizes.imageAfter) {
  212. const container = sizes.imageAfter.parentNode;
  213. sizes.imageAfter.style.width = `${sizes.sizeAfter.width * factor}px`;
  214. sizes.imageAfter.style.height = `${sizes.sizeAfter.height * factor}px`;
  215. container.style.margin = `${sizes.ratio[1] * factor}px ${sizes.ratio[0] * factor}px`;
  216. container.style.width = `${sizes.sizeAfter.width * factor + 2}px`;
  217. container.style.height = `${sizes.sizeAfter.height * factor + 2}px`;
  218. }
  219. if (sizes.imageBefore) {
  220. const container = sizes.imageBefore.parentNode;
  221. const overlayFrame = container.parentNode;
  222. sizes.imageBefore.style.width = `${sizes.sizeBefore.width * factor}px`;
  223. sizes.imageBefore.style.height = `${sizes.sizeBefore.height * factor}px`;
  224. container.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
  225. container.style.width = `${sizes.sizeBefore.width * factor + 2}px`;
  226. container.style.height = `${sizes.sizeBefore.height * factor + 2}px`;
  227. // some inner elements are `position: absolute`, so the container's height must be large enough
  228. overlayFrame.style.width = `${sizes.maxSize.width * factor + 2}px`;
  229. overlayFrame.style.height = `${sizes.maxSize.height * factor + 2}px`;
  230. }
  231. const rangeInput = $container[0].querySelector('input[type="range"]');
  232. function updateOpacity() {
  233. if (sizes.imageAfter) {
  234. sizes.imageAfter.parentNode.style.opacity = `${rangeInput.value / 100}`;
  235. }
  236. }
  237. rangeInput?.addEventListener('input', updateOpacity);
  238. updateOpacity();
  239. }
  240. });
  241. }