File size: 8,576 Bytes
fbcf5d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
let isDarkMode = false;
let fontData = [];

// Load available fonts when page loads
async function loadAvailableFonts() {
  try {
    const response = await fetch("/api/fonts");
    fontData = await response.json();

    // Populate both quote and author font family selects
    const quoteFontFamily = document.getElementById("quoteFontFamily");
    const authorFontFamily = document.getElementById("authorFontFamily");

    const fontOptions = fontData
      .map((font) => `<option value="${font.family}">${font.family}</option>`)
      .join("");

    quoteFontFamily.innerHTML = fontOptions;
    authorFontFamily.innerHTML = fontOptions;

    // Initialize variations for both
    if (fontData.length > 0) {
      updateFontVariations("quote", fontData[0].family);
      updateFontVariations("author", fontData[0].family);
      generateQuote();
    }
  } catch (error) {
    console.error("Error loading fonts:", error);
    showStatus("Error loading fonts", "error");
  }
}

// Update font variations based on selected font family
function updateFontVariations(type, family) {
  const font = fontData.find((f) => f.family === family);
  if (!font) return;

  const styleSelect = document.getElementById(`${type}FontStyle`);

  // Get unique styles
  const styles = new Set(font.variations.map((v) => v.style));

  // Update style options
  styleSelect.innerHTML = Array.from(styles)
    .map(
      (style) => `<option value="${style}">${formatFontStyle(style)}</option>`
    )
    .join("");
}

// Format font weight for display
function formatFontWeight(weight) {
  switch (weight) {
    case "normal":
      return "Regular";
    case "bold":
      return "Bold";
    case "light":
      return "Light";
    case "medium":
      return "Medium";
    default:
      return weight.charAt(0).toUpperCase() + weight.slice(1);
  }
}

// Format font style for display
function formatFontStyle(style) {
  switch (style) {
    case "normal":
      return "Normal";
    case "italic":
      return "Italic";
    default:
      return style.charAt(0).toUpperCase() + style.slice(1);
  }
}

// Setup color pickers with hex input sync
function setupColorPickers() {
  const colorInputs = document.querySelectorAll('input[type="color"]');
  colorInputs.forEach((input) => {
    const textInput = document.getElementById(input.id + "Text");

    // Update text input when color changes
    input.addEventListener("input", () => {
      textInput.value = input.value.toUpperCase();
    });

    // Update color input when valid hex is entered
    textInput.addEventListener("input", () => {
      let hex = textInput.value.trim();
      if (!hex.startsWith("#")) {
        hex = "#" + hex;
      }
      if (/^#[0-9A-F]{6}$/i.test(hex)) {
        input.value = hex;
      }
    });

    // Format hex on blur
    textInput.addEventListener("blur", () => {
      let hex = textInput.value.trim();
      if (!hex.startsWith("#")) {
        hex = "#" + hex;
      }
      if (/^#[0-9A-F]{6}$/i.test(hex)) {
        textInput.value = hex.toUpperCase();
        input.value = hex;
      } else {
        textInput.value = input.value.toUpperCase();
      }
    });
  });
}

async function generateQuote() {
  const generateBtn = document.querySelector(".btn-primary");
  const btnText = generateBtn.querySelector(".btn-text");
  const btnLoader = generateBtn.querySelector(".btn-loader");

  try {
    // Show loading state
    generateBtn.disabled = true;
    btnText.style.opacity = "0";
    btnLoader.style.display = "block";
    document.getElementById("loading").style.display = "flex";
    document.getElementById("quoteImage").style.opacity = "0.5";

    const data = {
      text: document.getElementById("quoteText").value,
      author: document.getElementById("author").value,
      bgColor: document.getElementById("bgColor").value,
      barColor: document.getElementById("barColor").value,
      textColor: document.getElementById("textColor").value,
      authorColor: document.getElementById("authorColor").value,
      quoteFontFamily: document.getElementById("quoteFontFamily").value,
      quoteFontWeight: "bold", // Force bold for quote
      quoteFontStyle: document.getElementById("quoteFontStyle").value,
      authorFontFamily: document.getElementById("authorFontFamily").value,
      authorFontWeight: "normal", // Force normal for author
      authorFontStyle: document.getElementById("authorFontStyle").value,
      barWidth: parseInt(document.getElementById("barWidth").value),
    };

    const response = await fetch("/api/generate-quote", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
    });

    const result = await response.json();

    if (result.success) {
      const img = document.getElementById("quoteImage");
      img.src = result.imageUrl;
      img.style.opacity = "1";
      showStatus("Quote generated successfully!", "success");
    } else {
      throw new Error(result.error);
    }
  } catch (error) {
    showStatus(error.message || "Error generating quote", "error");
    console.error(error);
  } finally {
    // Reset loading state
    generateBtn.disabled = false;
    btnText.style.opacity = "1";
    btnLoader.style.display = "none";
    document.getElementById("loading").style.display = "none";
  }
}

function downloadQuote() {
  const img = document.getElementById("quoteImage");
  if (!img.src || img.src === window.location.href) {
    showStatus("Generate a quote first!", "error");
    return;
  }

  const link = document.createElement("a");
  const timestamp = new Date().toISOString().split("T")[0];
  link.download = `quote-${timestamp}.png`;
  link.href = img.src;
  link.click();
}

async function viewRequests() {
  try {
    const response = await fetch("/api/requests-history");
    const requests = await response.json();

    const requestsList = document.getElementById("requestsList");
    requestsList.innerHTML = requests
      .map(
        (req) => `

            <div class="request-item">

                <div class="request-time">${new Date(

                  req.timestamp

                ).toLocaleString()}</div>

                <pre class="request-data">${JSON.stringify(

                  req.request,

                  null,

                  2

                )}</pre>

            </div>

        `
      )
      .join("");

    document.getElementById("requestsModal").style.display = "block";
  } catch (error) {
    showStatus("Error fetching requests", "error");
    console.error(error);
  }
}

function closeModal() {
  document.getElementById("requestsModal").style.display = "none";
}

function toggleTheme() {
  isDarkMode = !isDarkMode;
  document.documentElement.setAttribute(
    "data-theme",
    isDarkMode ? "dark" : "light"
  );
  document.querySelector(".theme-toggle").textContent = isDarkMode
    ? "☀️"
    : "🌙";
}

function showStatus(message, type) {
  const status = document.getElementById("status");
  status.textContent = message;
  status.style.color = type === "success" ? "var(--success)" : "#ef4444";
  status.style.display = "block";

  setTimeout(() => {
    status.style.display = "none";
  }, 3000);
}

// Event Listeners
document.addEventListener("DOMContentLoaded", () => {
  loadAvailableFonts();
  setupColorPickers();

  // Font family change listeners
  document.getElementById("quoteFontFamily").addEventListener("change", (e) => {
    updateFontVariations("quote", e.target.value);
  });

  document
    .getElementById("authorFontFamily")
    .addEventListener("change", (e) => {
      updateFontVariations("author", e.target.value);
    });

  // Check system theme preference
  if (
    window.matchMedia &&
    window.matchMedia("(prefers-color-scheme: dark)").matches
  ) {
    toggleTheme();
  }

  // Setup bar width value display
  const barWidthSlider = document.getElementById("barWidth");
  const barWidthValue = document.getElementById("barWidthValue");
  barWidthSlider.addEventListener("input", () => {
    barWidthValue.textContent = `${barWidthSlider.value}px`;
  });
});

// Close modal when clicking outside
window.onclick = function (event) {
  const modal = document.getElementById("requestsModal");
  if (event.target === modal) {
    closeModal();
  }
};