gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import {GET} from '../modules/fetch.ts';
  2. import {hideElem, loadElem, queryElemChildren, queryElems} from '../utils/dom.ts';
  3. import {parseDom} from '../utils.ts';
  4. import {fomanticQuery} from '../modules/fomantic/base.ts';
  5. function getDefaultSvgBoundsIfUndefined(text: string, src: string) {
  6. const defaultSize = 300;
  7. const maxSize = 99999;
  8. const svgDoc = parseDom(text, 'image/svg+xml');
  9. const svg = (svgDoc.documentElement as unknown) as SVGSVGElement;
  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: HTMLImageElement, imageBefore: HTMLImageElement) {
  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. class ImageDiff {
  66. containerEl: HTMLElement;
  67. diffContainerWidth: number;
  68. async init(containerEl: HTMLElement) {
  69. this.containerEl = containerEl;
  70. containerEl.setAttribute('data-image-diff-loaded', 'true');
  71. fomanticQuery(containerEl).find('.ui.menu.tabular .item').tab();
  72. // the container may be hidden by "viewed" checkbox, so use the parent's width for reference
  73. this.diffContainerWidth = Math.max(containerEl.closest('.diff-file-box').clientWidth - 300, 100);
  74. const imageInfos = [{
  75. path: containerEl.getAttribute('data-path-after'),
  76. mime: containerEl.getAttribute('data-mime-after'),
  77. images: containerEl.querySelectorAll<HTMLImageElement>('img.image-after'), // matches 3 <img>
  78. boundsInfo: containerEl.querySelector('.bounds-info-after'),
  79. }, {
  80. path: containerEl.getAttribute('data-path-before'),
  81. mime: containerEl.getAttribute('data-mime-before'),
  82. images: containerEl.querySelectorAll<HTMLImageElement>('img.image-before'), // matches 3 <img>
  83. boundsInfo: containerEl.querySelector('.bounds-info-before'),
  84. }];
  85. await Promise.all(imageInfos.map(async (info) => {
  86. const [success] = await Promise.all(Array.from(info.images, (img) => {
  87. return loadElem(img, info.path);
  88. }));
  89. // only the first images is associated with boundsInfo
  90. if (!success && info.boundsInfo) info.boundsInfo.textContent = '(image error)';
  91. if (info.mime === 'image/svg+xml') {
  92. const resp = await GET(info.path);
  93. const text = await resp.text();
  94. const bounds = getDefaultSvgBoundsIfUndefined(text, info.path);
  95. if (bounds) {
  96. for (const el of info.images) {
  97. el.setAttribute('width', String(bounds.width));
  98. el.setAttribute('height', String(bounds.height));
  99. }
  100. hideElem(info.boundsInfo);
  101. }
  102. }
  103. }));
  104. const imagesAfter = imageInfos[0].images;
  105. const imagesBefore = imageInfos[1].images;
  106. this.initSideBySide(createContext(imagesAfter[0], imagesBefore[0]));
  107. if (imagesAfter.length > 0 && imagesBefore.length > 0) {
  108. this.initSwipe(createContext(imagesAfter[1], imagesBefore[1]));
  109. this.initOverlay(createContext(imagesAfter[2], imagesBefore[2]));
  110. }
  111. queryElemChildren(containerEl, '.image-diff-tabs', (el) => el.classList.remove('is-loading'));
  112. }
  113. initSideBySide(sizes: Record<string, any>) {
  114. let factor = 1;
  115. if (sizes.maxSize.width > (this.diffContainerWidth - 24) / 2) {
  116. factor = (this.diffContainerWidth - 24) / 2 / sizes.maxSize.width;
  117. }
  118. const widthChanged = sizes.imageAfter && sizes.imageBefore && sizes.imageAfter.naturalWidth !== sizes.imageBefore.naturalWidth;
  119. const heightChanged = sizes.imageAfter && sizes.imageBefore && sizes.imageAfter.naturalHeight !== sizes.imageBefore.naturalHeight;
  120. if (sizes.imageAfter) {
  121. const boundsInfoAfterWidth = this.containerEl.querySelector('.bounds-info-after .bounds-info-width');
  122. if (boundsInfoAfterWidth) {
  123. boundsInfoAfterWidth.textContent = `${sizes.imageAfter.naturalWidth}px`;
  124. boundsInfoAfterWidth.classList.toggle('green', widthChanged);
  125. }
  126. const boundsInfoAfterHeight = this.containerEl.querySelector('.bounds-info-after .bounds-info-height');
  127. if (boundsInfoAfterHeight) {
  128. boundsInfoAfterHeight.textContent = `${sizes.imageAfter.naturalHeight}px`;
  129. boundsInfoAfterHeight.classList.toggle('green', heightChanged);
  130. }
  131. }
  132. if (sizes.imageBefore) {
  133. const boundsInfoBeforeWidth = this.containerEl.querySelector('.bounds-info-before .bounds-info-width');
  134. if (boundsInfoBeforeWidth) {
  135. boundsInfoBeforeWidth.textContent = `${sizes.imageBefore.naturalWidth}px`;
  136. boundsInfoBeforeWidth.classList.toggle('red', widthChanged);
  137. }
  138. const boundsInfoBeforeHeight = this.containerEl.querySelector('.bounds-info-before .bounds-info-height');
  139. if (boundsInfoBeforeHeight) {
  140. boundsInfoBeforeHeight.textContent = `${sizes.imageBefore.naturalHeight}px`;
  141. boundsInfoBeforeHeight.classList.toggle('red', heightChanged);
  142. }
  143. }
  144. if (sizes.imageAfter) {
  145. const container = sizes.imageAfter.parentNode;
  146. sizes.imageAfter.style.width = `${sizes.sizeAfter.width * factor}px`;
  147. sizes.imageAfter.style.height = `${sizes.sizeAfter.height * factor}px`;
  148. container.style.margin = '10px auto';
  149. container.style.width = `${sizes.sizeAfter.width * factor + 2}px`;
  150. container.style.height = `${sizes.sizeAfter.height * factor + 2}px`;
  151. }
  152. if (sizes.imageBefore) {
  153. const container = sizes.imageBefore.parentNode;
  154. sizes.imageBefore.style.width = `${sizes.sizeBefore.width * factor}px`;
  155. sizes.imageBefore.style.height = `${sizes.sizeBefore.height * factor}px`;
  156. container.style.margin = '10px auto';
  157. container.style.width = `${sizes.sizeBefore.width * factor + 2}px`;
  158. container.style.height = `${sizes.sizeBefore.height * factor + 2}px`;
  159. }
  160. }
  161. initSwipe(sizes: Record<string, any>) {
  162. let factor = 1;
  163. if (sizes.maxSize.width > this.diffContainerWidth - 12) {
  164. factor = (this.diffContainerWidth - 12) / sizes.maxSize.width;
  165. }
  166. if (sizes.imageAfter) {
  167. const imgParent = sizes.imageAfter.parentNode;
  168. const swipeFrame = imgParent.parentNode;
  169. sizes.imageAfter.style.width = `${sizes.sizeAfter.width * factor}px`;
  170. sizes.imageAfter.style.height = `${sizes.sizeAfter.height * factor}px`;
  171. imgParent.style.margin = `0px ${sizes.ratio[0] * factor}px`;
  172. imgParent.style.width = `${sizes.sizeAfter.width * factor + 2}px`;
  173. imgParent.style.height = `${sizes.sizeAfter.height * factor + 2}px`;
  174. swipeFrame.style.padding = `${sizes.ratio[1] * factor}px 0 0 0`;
  175. swipeFrame.style.width = `${sizes.maxSize.width * factor + 2}px`;
  176. }
  177. if (sizes.imageBefore) {
  178. const imgParent = sizes.imageBefore.parentNode;
  179. const swipeFrame = imgParent.parentNode;
  180. sizes.imageBefore.style.width = `${sizes.sizeBefore.width * factor}px`;
  181. sizes.imageBefore.style.height = `${sizes.sizeBefore.height * factor}px`;
  182. imgParent.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
  183. imgParent.style.width = `${sizes.sizeBefore.width * factor + 2}px`;
  184. imgParent.style.height = `${sizes.sizeBefore.height * factor + 2}px`;
  185. swipeFrame.style.width = `${sizes.maxSize.width * factor + 2}px`;
  186. swipeFrame.style.height = `${sizes.maxSize.height * factor + 2}px`;
  187. }
  188. // extra height for inner "position: absolute" elements
  189. const swipe = this.containerEl.querySelector<HTMLElement>('.diff-swipe');
  190. if (swipe) {
  191. swipe.style.width = `${sizes.maxSize.width * factor + 2}px`;
  192. swipe.style.height = `${sizes.maxSize.height * factor + 30}px`;
  193. }
  194. this.containerEl.querySelector('.swipe-bar').addEventListener('mousedown', (e) => {
  195. e.preventDefault();
  196. this.initSwipeEventListeners(e.currentTarget as HTMLElement);
  197. });
  198. }
  199. initSwipeEventListeners(swipeBar: HTMLElement) {
  200. const swipeFrame = swipeBar.parentNode as HTMLElement;
  201. const width = swipeFrame.clientWidth;
  202. const onSwipeMouseMove = (e: MouseEvent) => {
  203. e.preventDefault();
  204. const rect = swipeFrame.getBoundingClientRect();
  205. const value = Math.max(0, Math.min(e.clientX - rect.left, width));
  206. swipeBar.style.left = `${value}px`;
  207. this.containerEl.querySelector<HTMLElement>('.swipe-container').style.width = `${swipeFrame.clientWidth - value}px`;
  208. };
  209. const removeEventListeners = () => {
  210. document.removeEventListener('mousemove', onSwipeMouseMove);
  211. document.removeEventListener('mouseup', removeEventListeners);
  212. };
  213. document.addEventListener('mousemove', onSwipeMouseMove);
  214. document.addEventListener('mouseup', removeEventListeners);
  215. }
  216. initOverlay(sizes: Record<string, any>) {
  217. let factor = 1;
  218. if (sizes.maxSize.width > this.diffContainerWidth - 12) {
  219. factor = (this.diffContainerWidth - 12) / sizes.maxSize.width;
  220. }
  221. if (sizes.imageAfter) {
  222. const container = sizes.imageAfter.parentNode;
  223. sizes.imageAfter.style.width = `${sizes.sizeAfter.width * factor}px`;
  224. sizes.imageAfter.style.height = `${sizes.sizeAfter.height * factor}px`;
  225. container.style.margin = `${sizes.ratio[1] * factor}px ${sizes.ratio[0] * factor}px`;
  226. container.style.width = `${sizes.sizeAfter.width * factor + 2}px`;
  227. container.style.height = `${sizes.sizeAfter.height * factor + 2}px`;
  228. }
  229. if (sizes.imageBefore) {
  230. const container = sizes.imageBefore.parentNode;
  231. const overlayFrame = container.parentNode;
  232. sizes.imageBefore.style.width = `${sizes.sizeBefore.width * factor}px`;
  233. sizes.imageBefore.style.height = `${sizes.sizeBefore.height * factor}px`;
  234. container.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
  235. container.style.width = `${sizes.sizeBefore.width * factor + 2}px`;
  236. container.style.height = `${sizes.sizeBefore.height * factor + 2}px`;
  237. // some inner elements are `position: absolute`, so the container's height must be large enough
  238. overlayFrame.style.width = `${sizes.maxSize.width * factor + 2}px`;
  239. overlayFrame.style.height = `${sizes.maxSize.height * factor + 2}px`;
  240. }
  241. const rangeInput = this.containerEl.querySelector<HTMLInputElement>('input[type="range"]');
  242. function updateOpacity() {
  243. if (sizes.imageAfter) {
  244. sizes.imageAfter.parentNode.style.opacity = `${Number(rangeInput.value) / 100}`;
  245. }
  246. }
  247. rangeInput?.addEventListener('input', updateOpacity);
  248. updateOpacity();
  249. }
  250. }
  251. export function initImageDiff() {
  252. for (const el of queryElems<HTMLImageElement>(document, '.image-diff:not([data-image-diff-loaded])')) {
  253. (new ImageDiff()).init(el); // it is async, but we don't need to await for it
  254. }
  255. }