All files / client/src/lib font-scale-utils.ts

93.02% Statements 40/43
87.5% Branches 14/16
100% Functions 8/8
92.1% Lines 35/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82    1x               1x 1x     42x 42x 42x 40x 40x   1x         35x 35x 35x 35x                 12x 38x   12x   2x 1x 1x 1x         11x   2x     9x 9x       9x 26x   9x   4x 1x 1x 1x         8x   2x     6x 6x    
// font-scale-utils.ts - Utility functions for global font scale management
 
export const FONT_SCALES = [
  { label: "S", value: 0.875, px: 12 },
  { label: "M", value: 1.0, px: 14 },
  { label: "L", value: 1.125, px: 16 },
  { label: "XL", value: 1.25, px: 18 },
  { label: "XXL", value: 1.5, px: 20 },
] as const;
 
export const FONT_SCALE_KEY = "unoFontScale";
export const DEFAULT_FONT_SCALE = 1.0;
 
export function getCurrentFontScale(): number {
  try {
    const stored = window.localStorage.getItem(FONT_SCALE_KEY);
    if (!stored) return DEFAULT_FONT_SCALE;
    const parsed = parseFloat(stored);
    return isNaN(parsed) ? DEFAULT_FONT_SCALE : parsed;
  } catch {
    return DEFAULT_FONT_SCALE;
  }
}
 
export function setFontScale(scale: number): void {
  try {
    window.localStorage.setItem(FONT_SCALE_KEY, String(scale));
    document.documentElement.style.setProperty("--ui-font-scale", String(scale));
    document.dispatchEvent(
      new CustomEvent("uiFontScaleChange", { detail: { value: scale } })
    );
  } catch (e) {
    console.error("Failed to set font scale:", e);
  }
}
 
export function increaseFontScale(): boolean {
  const current = getCurrentFontScale();
  const currentIndex = FONT_SCALES.findIndex((s) => Math.abs(s.value - current) < 0.01);
  
  if (currentIndex === -1) {
    // If current scale is not in the list, find the next larger one
    const next = FONT_SCALES.find((s) => s.value > current);
    Eif (next) {
      setFontScale(next.value);
      return true;
    }
    return false;
  }
  
  if (currentIndex >= FONT_SCALES.length - 1) {
    // Already at maximum
    return false;
  }
  
  setFontScale(FONT_SCALES[currentIndex + 1].value);
  return true;
}
 
export function decreaseFontScale(): boolean {
  const current = getCurrentFontScale();
  const currentIndex = FONT_SCALES.findIndex((s) => Math.abs(s.value - current) < 0.01);
  
  if (currentIndex === -1) {
    // If current scale is not in the list, find the next smaller one
    const prev = [...FONT_SCALES].reverse().find((s) => s.value < current);
    Eif (prev) {
      setFontScale(prev.value);
      return true;
    }
    return false;
  }
  
  if (currentIndex <= 0) {
    // Already at minimum
    return false;
  }
  
  setFontScale(FONT_SCALES[currentIndex - 1].value);
  return true;
}