utils.ts 3.7 KB

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