Guest User

Front

a guest
Aug 8th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. <!---------- Header ------------->
  2. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  3. <main>
  4. <div class="front-container">
  5. <!--------- Vocab card ---------->
  6. <div lang="ja" class="front-vocab" id="front-vocab-expression">{{Word}}</div>
  7.  
  8. <!-- Hint -->
  9. {{#Hint}}
  10. <div id="hint">{{Hint}}</div>
  11. {{/Hint}}
  12. </div>
  13. <header style="visibility: hidden"></header>
  14.  
  15. <script>
  16. // Adjust font size dynamically for vocab (front) and store it
  17. function adjustVocabFontSize() {
  18. try {
  19. console.log("adjustVocabFontSize running on front...");
  20. const vocabElement = document.getElementById('front-vocab-expression');
  21. if (!vocabElement) {
  22. console.log("front-vocab-expression not found");
  23. return;
  24. }
  25.  
  26. const containerWidth = window.innerWidth * 0.8; // 80vw consistently
  27. console.log("Front container width: " + containerWidth + "px");
  28.  
  29. if (!containerWidth) {
  30. console.log("Container width not available on front");
  31. return;
  32. }
  33.  
  34. let fontSize = 50; // Start with default font size
  35. vocabElement.style.fontSize = fontSize + 'px';
  36.  
  37. // Measure the width of the text content only (exclude furigana if present)
  38. let textWidth = vocabElement.scrollWidth;
  39. console.log("Front initial text width: " + textWidth + "px");
  40.  
  41. // Adjust font size until it fits
  42. let attempts = 0;
  43. while (textWidth > containerWidth && fontSize > 25 && attempts < 50) {
  44. fontSize -= 2;
  45. vocabElement.style.fontSize = fontSize + 'px';
  46. textWidth = vocabElement.scrollWidth;
  47. attempts++;
  48. console.log("Front adjusting font size: " + fontSize + "px, textWidth: " + textWidth + "px");
  49. }
  50.  
  51. // Ensure minimum readability
  52. if (fontSize <= 25) {
  53. vocabElement.style.fontSize = '25px';
  54. fontSize = 25;
  55. console.log("Front minimum adjusted font size reached: 25px");
  56. }
  57.  
  58. // Store the font size in a global variable for the back to use
  59. window.calculatedFontSize = fontSize;
  60. console.log("Front final font size stored: " + fontSize + "px");
  61. } catch (error) {
  62. console.error("Error in adjustVocabFontSize on front:", error);
  63. }
  64. }
  65.  
  66. // Run immediately and poll until the element is available
  67. function ensureFontSizeAdjustment() {
  68. adjustVocabFontSize();
  69. // Poll every 50ms until the element is found or 2 seconds pass
  70. let attempts = 0;
  71. const interval = setInterval(() => {
  72. const vocabElement = document.getElementById('front-vocab-expression');
  73. if (vocabElement && vocabElement.scrollWidth > 0) {
  74. adjustVocabFontSize();
  75. clearInterval(interval);
  76. console.log("Font size adjusted successfully on front after polling");
  77. }
  78. attempts++;
  79. if (attempts > 40) { // 40 * 50ms = 2 seconds
  80. clearInterval(interval);
  81. console.log("Stopped polling: front-vocab-expression not found or not measurable");
  82. }
  83. }, 50);
  84. }
  85.  
  86. // Run on load, resize, and as a fallback
  87. document.addEventListener('DOMContentLoaded', ensureFontSizeAdjustment);
  88. window.addEventListener('resize', adjustVocabFontSize);
  89. setTimeout(ensureFontSizeAdjustment, 0); // Immediate fallback
  90. </script>
  91. </main>
Advertisement
Add Comment
Please, Sign In to add comment