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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.attr('width', bounds.width);
  94. info.$images.attr('height', bounds.height);
  95. hideElem(info.$boundsInfo);
  96. }
  97. }
  98. }));
  99. const $imagesAfter = imageInfos[0].$images;
  100. const $imagesBefore = imageInfos[1].$images;
  101. initSideBySide(createContext($imagesAfter[0], $imagesBefore[0]));
  102. if ($imagesAfter.length > 0 && $imagesBefore.length > 0) {
  103. initSwipe(createContext($imagesAfter[1], $imagesBefore[1]));
  104. initOverlay(createContext($imagesAfter[2], $imagesBefore[2]));
  105. }
  106. $container.find('> .image-diff-tabs').removeClass('is-loading');
  107. function initSideBySide(sizes) {
  108. let factor = 1;
  109. if (sizes.max.width > (diffContainerWidth - 24) / 2) {
  110. factor = (diffContainerWidth - 24) / 2 / sizes.max.width;
  111. }
  112. const widthChanged = sizes.image1.length !== 0 && sizes.image2.length !== 0 && sizes.image1[0].naturalWidth !== sizes.image2[0].naturalWidth;
  113. const heightChanged = sizes.image1.length !== 0 && sizes.image2.length !== 0 && sizes.image1[0].naturalHeight !== sizes.image2[0].naturalHeight;
  114. if (sizes.image1.length !== 0) {
  115. $container.find('.bounds-info-after .bounds-info-width').text(`${sizes.image1[0].naturalWidth}px`).addClass(widthChanged ? 'green' : '');
  116. $container.find('.bounds-info-after .bounds-info-height').text(`${sizes.image1[0].naturalHeight}px`).addClass(heightChanged ? 'green' : '');
  117. }
  118. if (sizes.image2.length !== 0) {
  119. $container.find('.bounds-info-before .bounds-info-width').text(`${sizes.image2[0].naturalWidth}px`).addClass(widthChanged ? 'red' : '');
  120. $container.find('.bounds-info-before .bounds-info-height').text(`${sizes.image2[0].naturalHeight}px`).addClass(heightChanged ? 'red' : '');
  121. }
  122. const image1 = sizes.image1[0];
  123. if (image1) {
  124. const container = image1.parentNode;
  125. image1.style.width = `${sizes.size1.width * factor}px`;
  126. image1.style.height = `${sizes.size1.height * factor}px`;
  127. container.style.margin = '10px auto';
  128. container.style.width = `${sizes.size1.width * factor + 2}px`;
  129. container.style.height = `${sizes.size1.height * factor + 2}px`;
  130. }
  131. const image2 = sizes.image2[0];
  132. if (image2) {
  133. const container = image2.parentNode;
  134. image2.style.width = `${sizes.size2.width * factor}px`;
  135. image2.style.height = `${sizes.size2.height * factor}px`;
  136. container.style.margin = '10px auto';
  137. container.style.width = `${sizes.size2.width * factor + 2}px`;
  138. container.style.height = `${sizes.size2.height * factor + 2}px`;
  139. }
  140. }
  141. function initSwipe(sizes) {
  142. let factor = 1;
  143. if (sizes.max.width > diffContainerWidth - 12) {
  144. factor = (diffContainerWidth - 12) / sizes.max.width;
  145. }
  146. const image1 = sizes.image1[0];
  147. if (image1) {
  148. const container = image1.parentNode;
  149. const swipeFrame = container.parentNode;
  150. image1.style.width = `${sizes.size1.width * factor}px`;
  151. image1.style.height = `${sizes.size1.height * factor}px`;
  152. container.style.margin = `0px ${sizes.ratio[0] * factor}px`;
  153. container.style.width = `${sizes.size1.width * factor + 2}px`;
  154. container.style.height = `${sizes.size1.height * factor + 2}px`;
  155. swipeFrame.style.padding = `${sizes.ratio[1] * factor}px 0 0 0`;
  156. swipeFrame.style.width = `${sizes.max.width * factor + 2}px`;
  157. }
  158. const image2 = sizes.image2[0];
  159. if (image2) {
  160. const container = image2.parentNode;
  161. const swipeFrame = container.parentNode;
  162. image2.style.width = `${sizes.size2.width * factor}px`;
  163. image2.style.height = `${sizes.size2.height * factor}px`;
  164. container.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
  165. container.style.width = `${sizes.size2.width * factor + 2}px`;
  166. container.style.height = `${sizes.size2.height * factor + 2}px`;
  167. swipeFrame.style.width = `${sizes.max.width * factor + 2}px`;
  168. swipeFrame.style.height = `${sizes.max.height * factor + 2}px`;
  169. }
  170. // extra height for inner "position: absolute" elements
  171. const swipe = $container.find('.diff-swipe')[0];
  172. if (swipe) {
  173. swipe.style.width = `${sizes.max.width * factor + 2}px`;
  174. swipe.style.height = `${sizes.max.height * factor + 30}px`;
  175. }
  176. $container.find('.swipe-bar').on('mousedown', function(e) {
  177. e.preventDefault();
  178. const $swipeBar = $(this);
  179. const $swipeFrame = $swipeBar.parent();
  180. const width = $swipeFrame.width() - $swipeBar.width() - 2;
  181. $(document).on('mousemove.diff-swipe', (e2) => {
  182. e2.preventDefault();
  183. const value = Math.max(0, Math.min(e2.clientX - $swipeFrame.offset().left, width));
  184. $swipeBar[0].style.left = `${value}px`;
  185. $container.find('.swipe-container')[0].style.width = `${$swipeFrame.width() - value}px`;
  186. $(document).on('mouseup.diff-swipe', () => {
  187. $(document).off('.diff-swipe');
  188. });
  189. });
  190. });
  191. }
  192. function initOverlay(sizes) {
  193. let factor = 1;
  194. if (sizes.max.width > diffContainerWidth - 12) {
  195. factor = (diffContainerWidth - 12) / sizes.max.width;
  196. }
  197. sizes.image1.css({
  198. width: sizes.size1.width * factor,
  199. height: sizes.size1.height * factor,
  200. });
  201. sizes.image2.css({
  202. width: sizes.size2.width * factor,
  203. height: sizes.size2.height * factor,
  204. });
  205. sizes.image1.parent().css({
  206. margin: `${sizes.ratio[1] * factor}px ${sizes.ratio[0] * factor}px`,
  207. width: sizes.size1.width * factor + 2,
  208. height: sizes.size1.height * factor + 2,
  209. });
  210. sizes.image2.parent().css({
  211. margin: `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`,
  212. width: sizes.size2.width * factor + 2,
  213. height: sizes.size2.height * factor + 2,
  214. });
  215. // some inner elements are `position: absolute`, so the container's height must be large enough
  216. // the "css(width, height)" is somewhat hacky and not easy to understand, it could be improved in the future
  217. sizes.image2.parent().parent().css({
  218. width: sizes.max.width * factor + 2,
  219. height: sizes.max.height * factor + 2,
  220. });
  221. const $range = $container.find("input[type='range']");
  222. const onInput = () => sizes.image1.parent().css({
  223. opacity: $range.val() / 100,
  224. });
  225. $range.on('input', onInput);
  226. onInput();
  227. }
  228. });
  229. }