utils.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. export const isTargetEmit = (event, targetClasses): boolean => {
  2. // event.path usage is discouraged, use event.composedPath() as it's standard and is more future-proof
  3. // path is the event-triggered bubbling path, which stores each node through which bubbling passes.
  4. // path.length-4 is to remove elements above the root node, such as body, html, document, window
  5. const path = event?.composedPath();
  6. const isTarget = path?.slice(0, path.length - 4).some((node): boolean => {
  7. if (node.className && typeof node.className === "string") {
  8. return targetClasses.some(c => node.className.includes(c));
  9. }
  10. return false;
  11. });
  12. return isTarget;
  13. };
  14. export const downloadImage = (src: string, filename: string): void => {
  15. const image = new Image();
  16. image.src = src;
  17. image.crossOrigin = "anonymous";
  18. image.onload = (e): void => {
  19. const eleLink = document.createElement("a");
  20. eleLink.download = filename;
  21. eleLink.style.display = "none";
  22. eleLink.download = filename;
  23. eleLink.href = src;
  24. const canvas = document.createElement("canvas");
  25. canvas.width = image.width;
  26. canvas.height = image.height;
  27. const context = canvas.getContext("2d");
  28. context.drawImage(image, 0, 0, image.width, image.height);
  29. eleLink.href = canvas.toDataURL("image/jpeg");
  30. document.body.appendChild(eleLink);
  31. eleLink.click();
  32. document.body.removeChild(eleLink);
  33. };
  34. };
  35. export const crossMerge = (leftArr = [], rightArr = []) => {
  36. let newArr = [];
  37. const leftLen = leftArr.length;
  38. const rightLen = rightArr.length;
  39. const crossLength = leftLen <= rightLen ? leftLen : rightLen;
  40. (new Array(crossLength).fill(0)).forEach((item, index) => {
  41. newArr.push(rightArr[index]);
  42. newArr.push(leftArr[index]);
  43. });
  44. if (leftLen > rightLen) {
  45. newArr = newArr.concat(leftArr.slice(rightLen, leftLen));
  46. } else if (leftLen < rightLen) {
  47. newArr = newArr.concat(rightArr.slice(leftLen, rightLen));
  48. }
  49. return newArr;
  50. };
  51. export const getPreloadImagArr = (imgSrc: string[], currentIndex: number, preLoadGap: number, infinite: boolean) => {
  52. const beginIndex = currentIndex - preLoadGap;
  53. const endIndex = currentIndex + preLoadGap;
  54. const srcLength = imgSrc.length;
  55. let leftArr = [];
  56. let rightArr = [];
  57. if ( preLoadGap >= Math.floor(srcLength / 2)) {
  58. if (infinite) {
  59. leftArr = imgSrc.concat(imgSrc).slice(beginIndex + srcLength < 0 ? 0 : beginIndex + srcLength, currentIndex + srcLength);
  60. rightArr = imgSrc.concat(imgSrc).slice(currentIndex + 1, endIndex + 1 < 2 * srcLength ? endIndex + 1 : 2 * srcLength);
  61. } else {
  62. leftArr = imgSrc.slice(0, currentIndex);
  63. rightArr = imgSrc.slice(currentIndex + 1, srcLength);
  64. }
  65. } else {
  66. if (infinite) {
  67. leftArr = imgSrc.concat(imgSrc).slice(beginIndex + srcLength, currentIndex + srcLength);
  68. rightArr = imgSrc.concat(imgSrc).slice(currentIndex + 1, endIndex + 1);
  69. } else {
  70. if (beginIndex >= 0 && endIndex < srcLength) {
  71. leftArr = imgSrc.slice(beginIndex, currentIndex);
  72. rightArr = imgSrc.slice(currentIndex + 1, endIndex + 1);
  73. } else if (beginIndex < 0) {
  74. leftArr = imgSrc.slice(0, currentIndex);
  75. rightArr = imgSrc.slice(currentIndex + 1, 2 * preLoadGap + 1);
  76. } else {
  77. rightArr = imgSrc.slice(currentIndex + 1, srcLength);
  78. leftArr = imgSrc.slice(srcLength - 2 * preLoadGap - 1, currentIndex);
  79. }
  80. }
  81. }
  82. const result = crossMerge(leftArr.reverse(), rightArr);
  83. const duplicateResult = Array.from(new Set(result));
  84. return duplicateResult;
  85. };