utils.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 = async (src: string, filename: string, downloadErrorCb: (src: string) => void ) => {
  15. try {
  16. const response = await fetch(src);
  17. if (response.ok) {
  18. const blob = await response.blob();
  19. const url = URL.createObjectURL(blob);
  20. const link = document.createElement('a');
  21. link.href = url;
  22. link.download = filename;
  23. link.click();
  24. URL.revokeObjectURL(url);
  25. link.remove();
  26. } else {
  27. downloadErrorCb(src);
  28. }
  29. } catch (error) {
  30. downloadErrorCb(src);
  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. };