All files / web/src/app/api/create/calendar/utils typstGenerator.ts

0% Statements 0/195
0% Branches 0/1
0% Functions 0/1
0% Lines 0/195

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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196                                                                                                                                                                                                                                                                                                                                                                                                       
interface TypstMonthlyConfig {
  month: number
  year: number
  paperSize: 'us-letter' | 'a4' | 'a3' | 'tabloid'
  daysInMonth: number
}

interface TypstDailyConfig {
  month: number
  year: number
  paperSize: 'us-letter' | 'a4' | 'a3' | 'tabloid'
  daysInMonth: number
}

const MONTH_NAMES = [
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December',
]

export function getDaysInMonth(year: number, month: number): number {
  return new Date(year, month, 0).getDate()
}

function getFirstDayOfWeek(year: number, month: number): number {
  return new Date(year, month - 1, 1).getDay() // 0 = Sunday
}

function getDayOfWeek(year: number, month: number, day: number): string {
  const date = new Date(year, month - 1, day)
  return date.toLocaleDateString('en-US', { weekday: 'long' })
}

type PaperSize = 'us-letter' | 'a4' | 'a3' | 'tabloid'

interface PaperConfig {
  typstName: string
  marginX: string
  marginY: string
}

function getPaperConfig(size: string): PaperConfig {
  const configs: Record<PaperSize, PaperConfig> = {
    // Tight margins to maximize space for calendar grid
    'us-letter': { typstName: 'us-letter', marginX: '0.5in', marginY: '0.5in' },
    // A4 is slightly taller/narrower than US Letter - adjust margins proportionally
    a4: { typstName: 'a4', marginX: '1.3cm', marginY: '1.3cm' },
    // A3 is 2x area of A4 - can use same margins but will scale content larger
    a3: { typstName: 'a3', marginX: '1.5cm', marginY: '1.5cm' },
    // Tabloid (11" × 17") is larger - can use more margin
    tabloid: { typstName: 'us-tabloid', marginX: '0.75in', marginY: '0.75in' },
  }
  return configs[size as PaperSize] || configs['us-letter']
}

export function generateMonthlyTypst(config: TypstMonthlyConfig): string {
  const { paperSize } = config
  const paperConfig = getPaperConfig(paperSize)

  // Single-page design: use one composite SVG that scales to fit
  // This prevents overflow - Typst will scale the image to fit available space
  return `#set page(
  paper: "${paperConfig.typstName}",
  margin: (x: ${paperConfig.marginX}, y: ${paperConfig.marginY}),
)

// Composite calendar SVG - scales to fit page (prevents multi-page overflow)
#align(center + horizon)[
  #image("calendar.svg", width: 100%, fit: "contain")
]
`
}

export function generateDailyTypst(config: TypstDailyConfig): string {
  const { month, year, paperSize, daysInMonth } = config
  const paperConfig = getPaperConfig(paperSize)
  const monthName = MONTH_NAMES[month - 1]

  let pages = ''

  for (let day = 1; day <= daysInMonth; day++) {
    const dayOfWeek = getDayOfWeek(year, month, day)

    pages += `
#page(
  paper: "${paperConfig.typstName}",
  margin: (x: ${paperConfig.marginX}, y: ${paperConfig.marginY}),
)[
  #set text(font: "Georgia")

  // Decorative borders
  #rect(
    width: 100%,
    height: 100%,
    stroke: (paint: rgb("#2563eb"), thickness: 3pt),
    radius: 8pt,
    inset: 0pt,
  )[
    #rect(
      width: 100%,
      height: 100%,
      stroke: (paint: rgb("#2563eb"), thickness: 1pt),
      radius: 4pt,
      inset: 10pt,
    )[
      #v(10pt)

      // Header section with background
      #rect(
        width: 100%,
        height: 90pt,
        fill: rgb("#eff6ff"),
        stroke: (paint: rgb("#2563eb"), thickness: 2pt),
        radius: 6pt,
      )[
        #align(center)[
          #v(15pt)
          #text(size: 32pt, weight: "bold", fill: rgb("#1e40af"), tracking: 2pt)[
            ${monthName.toUpperCase()}
          ]
          #v(5pt)
          #image("year.svg", width: 15%)
        ]
      ]

      #v(15pt)

      // Day of week (large and prominent)
      #align(center)[
        #text(size: 28pt, weight: "bold", fill: rgb("#1e3a8a"))[
          ${dayOfWeek}
        ]
      ]

      #v(10pt)

      // Day abacus (main focus, large)
      #align(center)[
        #image("day-${day}.svg", width: 45%)
      ]

      #v(10pt)

      // Full date
      #align(center)[
        #text(size: 18pt, weight: 500, fill: rgb("#475569"))[
          ${monthName} ${day}, ${year}
        ]
      ]

      #v(1fr)

      // Notes section with decorative box
      #rect(
        width: 100%,
        height: 90pt,
        fill: rgb("#fefce8"),
        stroke: (paint: rgb("#ca8a04"), thickness: 2pt),
        radius: 4pt,
      )[
        #v(8pt)
        #text(size: 14pt, weight: "bold", fill: rgb("#854d0e"))[
          #h(10pt) Notes:
        ]
        #v(8pt)
        #line(length: 95%, stroke: (paint: rgb("#ca8a04"), thickness: 1pt))
        #v(8pt)
        #line(length: 95%, stroke: (paint: rgb("#ca8a04"), thickness: 1pt))
        #v(8pt)
        #line(length: 95%, stroke: (paint: rgb("#ca8a04"), thickness: 1pt))
      ]

      #v(10pt)
    ]
  ]
]

${day < daysInMonth ? '' : ''}`

    if (day < daysInMonth) {
      pages += '\n'
    }
  }

  return pages
}