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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import $ from 'jquery';
  2. import {GET} from '../modules/fetch.js';
  3. import {hideElem, loadElem} 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. export function initImageDiff() {
  39. function createContext(image1, image2) {
  40. const size1 = {
  41. width: image1 && image1.width || 0,
  42. height: image1 && image1.height || 0,
  43. };
  44. const size2 = {
  45. width: image2 && image2.width || 0,
  46. height: image2 && image2.height || 0,
  47. };
  48. const max = {
  49. width: Math.max(size2.width, size1.width),
  50. height: Math.max(size2.height, size1.height),
  51. };
  52. return {
  53. $image1: $(image1),
  54. $image2: $(image2),
  55. size1,
  56. size2,
  57. max,
  58. ratio: [
  59. Math.floor(max.width - size1.width) / 2,
  60. Math.floor(max.height - size1.height) / 2,
  61. Math.floor(max.width - size2.width) / 2,
  62. Math.floor(max.height - size2.height) / 2,
  63. ],
  64. };
  65. }
  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(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. $container.find('> .image-diff-tabs').removeClass('is-loading');
  109. function initSideBySide(sizes) {
  110. let factor = 1;
  111. if (sizes.max.width > (diffContainerWidth - 24) / 2) {
  112. factor = (diffContainerWidth - 24) / 2 / sizes.max.width;
  113. }
  114. const widthChanged = sizes.$image1.length !== 0 && sizes.$image2.length !== 0 && sizes.$image1[0].naturalWidth !== sizes.$image2[0].naturalWidth;
  115. const heightChanged = sizes.$image1.length !== 0 && sizes.$image2.length !== 0 && sizes.$image1[0].naturalHeight !== sizes.$image2[0].naturalHeight;
  116. if (sizes.$image1.length !== 0) {
  117. $container.find('.bounds-info-after .bounds-info-width').text(`${sizes.$image1[0].naturalWidth}px`).addClass(widthChanged ? 'green' : '');
  118. $container.find('.bounds-info-after .bounds-info-height').text(`${sizes.$image1[0].naturalHeight}px`).addClass(heightChanged ? 'green' : '');
  119. }
  120. if (sizes.$image2.length !== 0) {
  121. $container.find('.bounds-info-before .bounds-info-width').text(`${sizes.$image2[0].naturalWidth}px`).addClass(widthChanged ? 'red' : '');
  122. $container.find('.bounds-info-before .bounds-info-height').text(`${sizes.$image2[0].naturalHeight}px`).addClass(heightChanged ? 'red' : '');
  123. }
  124. const image1 = sizes.$image1[0];
  125. if (image1) {
  126. const container = image1.parentNode;
  127. image1.style.width = `${sizes.size1.width * factor}px`;
  128. image1.style.height = `${sizes.size1.height * factor}px`;
  129. container.style.margin = '10px auto';
  130. container.style.width = `${sizes.size1.width * factor + 2}px`;
  131. container.style.height = `${sizes.size1.height * factor + 2}px`;
  132. }
  133. const image2 = sizes.$image2[0];
  134. if (image2) {
  135. const container = image2.parentNode;
  136. image2.style.width = `${sizes.size2.width * factor}px`;
  137. image2.style.height = `${sizes.size2.height * factor}px`;
  138. container.style.margin = '10px auto';
  139. container.style.width = `${sizes.size2.width * factor + 2}px`;
  140. container.style.height = `${sizes.size2.height * factor + 2}px`;
  141. }
  142. }
  143. function initSwipe(sizes) {
  144. let factor = 1;
  145. if (sizes.max.width > diffContainerWidth - 12) {
  146. factor = (diffContainerWidth - 12) / sizes.max.width;
  147. }
  148. const image1 = sizes.$image1[0];
  149. if (image1) {
  150. const container = image1.parentNode;
  151. const swipeFrame = container.parentNode;
  152. image1.style.width = `${sizes.size1.width * factor}px`;
  153. image1.style.height = `${sizes.size1.height * factor}px`;
  154. container.style.margin = `0px ${sizes.ratio[0] * factor}px`;
  155. container.style.width = `${sizes.size1.width * factor + 2}px`;
  156. container.style.height = `${sizes.size1.height * factor + 2}px`;
  157. swipeFrame.style.padding = `${sizes.ratio[1] * factor}px 0 0 0`;
  158. swipeFrame.style.width = `${sizes.max.width * factor + 2}px`;
  159. }
  160. const image2 = sizes.$image2[0];
  161. if (image2) {
  162. const container = image2.parentNode;
  163. const swipeFrame = container.parentNode;
  164. image2.style.width = `${sizes.size2.width * factor}px`;
  165. image2.style.height = `${sizes.size2.height * factor}px`;
  166. container.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
  167. container.style.width = `${sizes.size2.width * factor + 2}px`;
  168. container.style.height = `${sizes.size2.height * factor + 2}px`;
  169. swipeFrame.style.width = `${sizes.max.width * factor + 2}px`;
  170. swipeFrame.style.height = `${sizes.max.height * factor + 2}px`;
  171. }
  172. // extra height for inner "position: absolute" elements
  173. const swipe = $container.find('.diff-swipe')[0];
  174. if (swipe) {
  175. swipe.style.width = `${sizes.max.width * factor + 2}px`;
  176. swipe.style.height = `${sizes.max.height * factor + 30}px`;
  177. }
  178. $container.find('.swipe-bar').on('mousedown', function(e) {
  179. e.preventDefault();
  180. const $swipeBar = $(this);
  181. const $swipeFrame = $swipeBar.parent();
  182. const width = $swipeFrame.width() - $swipeBar.width() - 2;
  183. $(document).on('mousemove.diff-swipe', (e2) => {
  184. e2.preventDefault();
  185. const value = Math.max(0, Math.min(e2.clientX - $swipeFrame.offset().left, width));
  186. $swipeBar[0].style.left = `${value}px`;
  187. $container.find('.swipe-container')[0].style.width = `${$swipeFrame.width() - value}px`;
  188. $(document).on('mouseup.diff-swipe', () => {
  189. $(document).off('.diff-swipe');
  190. });
  191. });
  192. });
  193. }
  194. function initOverlay(sizes) {
  195. let factor = 1;
  196. if (sizes.max.width > diffContainerWidth - 12) {
  197. factor = (diffContainerWidth - 12) / sizes.max.width;
  198. }
  199. const image1 = sizes.$image1[0];
  200. if (image1) {
  201. const container = image1.parentNode;
  202. image1.style.width = `${sizes.size1.width * factor}px`;
  203. image1.style.height = `${sizes.size1.height * factor}px`;
  204. container.style.margin = `${sizes.ratio[1] * factor}px ${sizes.ratio[0] * factor}px`;
  205. container.style.width = `${sizes.size1.width * factor + 2}px`;
  206. container.style.height = `${sizes.size1.height * factor + 2}px`;
  207. }
  208. const image2 = sizes.$image2[0];
  209. if (image2) {
  210. const container = image2.parentNode;
  211. const overlayFrame = container.parentNode;
  212. image2.style.width = `${sizes.size2.width * factor}px`;
  213. image2.style.height = `${sizes.size2.height * factor}px`;
  214. container.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
  215. container.style.width = `${sizes.size2.width * factor + 2}px`;
  216. container.style.height = `${sizes.size2.height * factor + 2}px`;
  217. // some inner elements are `position: absolute`, so the container's height must be large enough
  218. overlayFrame.style.width = `${sizes.max.width * factor + 2}px`;
  219. overlayFrame.style.height = `${sizes.max.height * factor + 2}px`;
  220. }
  221. const rangeInput = $container[0].querySelector('input[type="range"]');
  222. function updateOpacity() {
  223. if (sizes?.$image1?.[0]) {
  224. sizes.$image1[0].parentNode.style.opacity = `${rangeInput.value / 100}`;
  225. }
  226. }
  227. rangeInput?.addEventListener('input', updateOpacity);
  228. updateOpacity();
  229. }
  230. });
  231. }