Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
darling.ts
Go to the documentation of this file.
1
80import * as vscode from 'vscode';
81import { logger } from "./logger";
82import { getMessage } from "./messageProvider";
83import { LazyFileLoader } from "./lazyFileLoad";
84
92export interface Person {
94 id: number,
96 name: string,
98 romaji: string,
100 age: string,
102 quote: string,
104 description: string,
106 imageContent: string[],
108 height: string,
110 weight: string,
112 more_information: string,
114 type: string,
116 alias: string[] | null
117}
134export class Darling {
135
137 private fileInstance: LazyFileLoader = new LazyFileLoader();
138
147 constructor(filePath: string | undefined = undefined, cwd: string | undefined = undefined, alternateFilePath: string | undefined = undefined) {
148 logger.debug(getMessage("inFunction", "constructor", "Darling"));
149 if (filePath) {
150 this.fileInstance.updateFilePath(filePath);
151 }
152 if (alternateFilePath) {
153 this.fileInstance.updateAlternateFilePath(alternateFilePath);
154 }
155 if (cwd) {
156 this.fileInstance.updateCurrentWorkingDirectory(cwd);
157 }
158 }
159
168 async updateFilePath(filePath: string): Promise<boolean> {
169 logger.debug(getMessage("inFunction", "updateFilePath", "Darling"));
170 return await this.fileInstance.updateFilePath(filePath);
171 }
172 async updateAlternateFilePath(alternateFilePath: string): Promise<boolean> {
173 logger.debug(getMessage("inFunction", "updateAlternateFilePath", "Darling"));
174 return await this.fileInstance.updateAlternateFilePath(alternateFilePath);
175 }
176
185 async updateCurrentWorkingDirectory(cwd: string): Promise<boolean> {
186 logger.debug(getMessage("inFunction", "updateCurrentWorkingDirectory", "Darling"));
187 return await this.fileInstance.updateCurrentWorkingDirectory(cwd);
188 }
189
198 private getRandomNumber(maxValue: number): number {
199 logger.debug(getMessage("inFunction", "getRandomNumber", "Darling"));
200 return Math.floor(Math.random() * maxValue);
201 }
202
212 async getRandomPerson(): Promise<Person> {
213 logger.debug(getMessage("inFunction", "getRandomPerson", "Darling"));
214 const fileContent = await this.fileInstance.get();
215 if (!Array.isArray(fileContent) || fileContent.length === 0) {
216 const err: string = getMessage("darlingJsonFileInvalid");
217 logger.Gui.error(err);
218 throw new Error(err);
219 }
220 const chosenIndex: number = this.getRandomNumber(fileContent.length);
221 const raw = fileContent[chosenIndex];
222 const person: Person = {
223 id: raw.id,
224 name: raw.name,
225 romaji: raw.romaji,
226 age: raw.age,
227 quote: raw.quote,
228 description: raw.description,
229 imageContent: raw.image_link ?? [], // rename image_link → imageContent
230 height: raw.height,
231 weight: raw.weight,
232 more_information: raw.more_information,
233 type: raw.type,
234 alias: raw.alias
235 };
236 return person;
237 }
238
247 private copyButtonScript(): string {
248 logger.debug(getMessage("inFunction", "copyButtonScript", "Darling"));
249 return `
250<script>
251 const vscode = acquireVsCodeApi();
252 console.log(\`vscode = \${vscode}\`);
253 document.getElementById('copyBtn').addEventListener('click', () => {
254 const content = document.getElementById('ascii').innerText;
255 navigator.clipboard.writeText(content).then(() => {
256 vscode.postMessage({ type: 'copied' });
257 });
258 });
259</script>
260 `;
261 }
262
271 private zoomScript(): string {
272 logger.debug(getMessage("inFunction", "zoomScript", "Darling"));
273 return `
274<script>
275 let currentSize = 6;
276 function updateFontSize(sizeDifference) {
277 console.log(\`sizeDifference = \${sizeDifference}\`);
278 const asciiPre = document.getElementById('ascii');
279 console.log(\`asciiPre = \${JSON.stringify(asciiPre)}\`);
280 console.log(\`currentSize = \${currentSize}\`);
281 if (currentSize + sizeDifference >= 2) {
282 currentSize += sizeDifference;
283 console.log(\`currentSize (after update) = \${currentSize}\`);
284 } else {
285 console.log(\`currentSize (no update) = \${currentSize}\`);
286 }
287 asciiPre.style.fontSize = currentSize + "px";
288 asciiPre.style.lineHeight = currentSize + "px";
289 console.log(\`newSize = \${asciiPre.style.fontSize}\`);
290 }
291
292 document.getElementById('zoomInBtn').addEventListener('click', () => {
293 updateFontSize((2));
294 });
295
296 document.getElementById('zoomOutBtn').addEventListener('click', () => {
297 updateFontSize((-2));
298 });
299
300 // init
301 updateFontSize();
302</script>
303 `;
304 }
305
314 private pageStyle(): string {
315 logger.debug(getMessage("inFunction", "pageStyle", "Darling"));
316 return `
317 <style>
318 body { font-family: sans-serif; padding: 20px; }
319 h1 { font-size: 72px; margin-bottom: 0.2em; }
320 h2 { margin-top: 1.2em; }
321 pre { font-size: 10px; line-height: 10px; white-space: pre; }
322 button { margin: 10px 0; padding: 5px 12px; font-size: 14px; }
323 </style>
324 `;
325 }
326
340 async displayRandomPersonInWindow() {
341 logger.debug(getMessage("inFunction", "displayRandomPersonInWindow", "Darling"));
342 const randomPerson: Person = await this.getRandomPerson();
343
344 const panel = vscode.window.createWebviewPanel(
345 getMessage("darlingView"),
346 randomPerson.name,
347 vscode.ViewColumn.One,
348 { enableScripts: true }
349 );
350
351 const asciiArt = randomPerson.imageContent.join("\n");
352
353 const copyButton: string = this.copyButtonScript();
354 const pageStyle: string = this.pageStyle();
355 const zoomScript: string = this.zoomScript();
356
357 panel.webview.html = `
358<!DOCTYPE html>
359<html lang="en">
360<head>
361 <meta charset="UTF-8">
362 ${pageStyle}
363</head>
364<body>
365 <h1>${randomPerson.name} (${randomPerson.romaji})</h1>
366 <p><b>${getMessage('darlingType')}:</b> ${randomPerson.type}</p>
367 <p><b>${getMessage('darlingAge')}:</b> ${randomPerson.age}</p>
368 <p><b>${getMessage('darlingHeight')}:</b> ${randomPerson.height} | <b>Weight:</b> ${randomPerson.weight}</p>
369 <p><b>${getMessage('darlingAlias')}:</b> ${randomPerson.alias ? randomPerson.alias.join(", ") : "None"}</p>
370
371 <h2>${getMessage('darlingDescription')}</h2>
372 <p>${randomPerson.description}</p>
373
374 <h2>${getMessage('darlingQuote')}</h2>
375 <blockquote>${randomPerson.quote}</blockquote>
376
377 <h2>${getMessage('darlingMoreInfo')}</h2>
378 <p><a href="${randomPerson.more_information}" target="_blank">${randomPerson.more_information}</a></p>
379
380 <h2>${getMessage('darlingImage')}</h2>
381<div>
382 <button id="copyBtn">${getMessage('darlingCopyAscii')}</button>
383 <button id="zoomInBtn">${getMessage('darlingZoomIn')}</button>
384 <button id="zoomOutBtn">${getMessage('darlingZoomOut')}</button>
385</div>
386 <pre id="ascii">${asciiArt}</pre>
387 ${copyButton}
388 ${zoomScript}
389</body>
390</html>
391`;
392
393 panel.webview.onDidReceiveMessage(message => {
394 if (message.type === "copied") {
395 logger.Gui.info(getMessage("darlingCopied", randomPerson.name));
396 }
397 });
398
399 logger.Gui.info(getMessage("darlingPersonDisplayed", randomPerson.name));
400 }
401}
Generic lazy file loader with caching and type safety @template T The expected type of the loaded fil...
import *as vscode from vscode
Definition darling.ts:80
Represents a character from the Darling in the FranXX series.
import type
export const logger
Singleton logger instance providing unified logging interface for the entire extension.
Definition logger.ts:910
export const getMessage
Exported function for direct message retrieval.
export const Record< string,(...args:any[])=> string