util.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. export const presence = new Presence({
  2. clientId: "1133602327476047873",
  3. });
  4. export const slideshow = presence.createSlideshow();
  5. export const enum Assets {
  6. Logo = "https://cdn.rcd.gg/PreMiD/websites/F/Fire%20Emblem%20Heroes/assets/logo.png",
  7. }
  8. export function truncateText(text: string): string {
  9. if (text.length > 127) return `${text.slice(0, 124)}...`;
  10. return text;
  11. }
  12. function loadImage(image: HTMLImageElement, url: string): Promise<void> {
  13. return new Promise(resolve => {
  14. image.addEventListener("load", () => resolve());
  15. image.src = url;
  16. });
  17. }
  18. function getCanvasBlob(): Promise<Blob> {
  19. return new Promise((resolve, reject) => {
  20. canvas.toBlob(blob => {
  21. if (!blob) reject();
  22. resolve(blob);
  23. }, "image/png");
  24. });
  25. }
  26. const canvas = document.createElement("canvas"),
  27. ctx = canvas.getContext("2d");
  28. canvas.height = 512;
  29. canvas.width = 512;
  30. const squareImageCache: Record<string, string> = {};
  31. let isUploading = false;
  32. export async function squareImage(url: string): Promise<string> {
  33. if (squareImageCache[url]) return squareImageCache[url];
  34. if (isUploading) return Assets.Logo;
  35. isUploading = true;
  36. const image = document.createElement("img");
  37. image.crossOrigin = "anonymous";
  38. await loadImage(image, url);
  39. const canvasWidth = canvas.width,
  40. canvasHeight = canvas.height,
  41. imageWidth = image.naturalWidth,
  42. imageHeight = image.naturalHeight,
  43. scale = Math.min(canvasWidth / imageWidth, canvasHeight / imageHeight),
  44. scaledWidth = imageWidth * scale,
  45. scaledHeight = imageHeight * scale;
  46. ctx.clearRect(0, 0, 512, 512);
  47. ctx.drawImage(
  48. image,
  49. (canvasWidth - scaledWidth) / 2,
  50. (canvasHeight - scaledHeight) / 2,
  51. scaledWidth,
  52. scaledHeight
  53. );
  54. const file = await getCanvasBlob(),
  55. formData = new FormData();
  56. formData.append("file", file, "file");
  57. const resultURL = await fetch("https://pd.premid.app/create/image", {
  58. method: "POST",
  59. body: formData,
  60. }).then(r => r.text());
  61. isUploading = false;
  62. squareImageCache[url] = resultURL;
  63. return resultURL;
  64. }
  65. let section = "",
  66. intersectionObserversActivated = false;
  67. const observer = new IntersectionObserver(
  68. entries => {
  69. let visibleSection = section;
  70. for (const entry of entries) {
  71. const { id } = entry.target,
  72. ratio = entry.intersectionRatio;
  73. if (ratio > 0.05) {
  74. visibleSection = id;
  75. break;
  76. }
  77. if (visibleSection === id && ratio < 0.05) visibleSection = "";
  78. }
  79. if (visibleSection !== section) section = visibleSection;
  80. },
  81. {
  82. threshold: [0.0, 0.05],
  83. }
  84. );
  85. /**
  86. * Observers are used to show what the user is currently viewing on the website.
  87. * This can be used for pages when individual posts/items are displayed on one page without changing the URL.
  88. */
  89. export function activateIntersectionObservers(pathList: string[]): void {
  90. if (intersectionObserversActivated) return;
  91. switch (pathList[0] ?? "") {
  92. case "": {
  93. observer.observe(document.querySelector("#cont2"));
  94. observer.observe(document.querySelector("#cont4"));
  95. break;
  96. }
  97. case "topics": {
  98. const articles = [...document.querySelectorAll(".article")];
  99. for (const article of articles) observer.observe(article);
  100. break;
  101. }
  102. }
  103. intersectionObserversActivated = true;
  104. }
  105. export function areObserversActivated(): boolean {
  106. return intersectionObserversActivated;
  107. }
  108. export function resetObservers(): void {
  109. intersectionObserversActivated = false;
  110. section = "";
  111. observer.disconnect();
  112. }
  113. export function getSection(): string {
  114. return section;
  115. }