Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
watermark.ts
Go to the documentation of this file.
1
90import * as vscode from 'vscode';
91import { logger } from "./logger";
92import { getMessage } from "./messageProvider";
93import { LazyFileLoader } from "./lazyFileLoad";
94import { authorLogo } from '../constants';
95
104export interface watermark {
106 watermark: string[],
108 fontName: string
109}
110
144export class Watermark {
145
147 private fileInstance: LazyFileLoader = new LazyFileLoader();
148
159 constructor(filePath: string | undefined = undefined, cwd: string | undefined = undefined, alternateFilePath: string | undefined = undefined) {
160 logger.debug(getMessage("inFunction", "constructor", "Watermark"));
161 if (filePath) {
162 this.fileInstance.updateFilePath(filePath);
163 }
164 if (alternateFilePath) {
165 this.fileInstance.updateAlternateFilePath(alternateFilePath);
166 }
167 if (cwd) {
168 this.fileInstance.updateCurrentWorkingDirectory(cwd);
169 }
170 }
171
181 async updateFilePath(filePath: string): Promise<boolean> {
182 logger.debug(getMessage("inFunction", "updateFilePath", "Watermark"));
183 return await this.fileInstance.updateFilePath(filePath);
184 }
185 async updateAlternateFilePath(alternateFilePath: string): Promise<boolean> {
186 logger.debug(getMessage("inFunction", "updateAlternateFilePath", "Watermark"));
187 return await this.fileInstance.updateAlternateFilePath(alternateFilePath);
188 }
189
199 async updateCurrentWorkingDirectory(cwd: string): Promise<boolean> {
200 logger.debug(getMessage("inFunction", "updateCurrentWorkingDirectory", "Watermark"));
201 return await this.fileInstance.updateCurrentWorkingDirectory(cwd);
202 }
203
213 private getRandomNumber(maxValue: number): number {
214 logger.debug(getMessage("inFunction", "getRandomNumber", "Watermark"));
215 return Math.floor(Math.random() * maxValue);
216 }
217
244 async getRandomWatermark(): Promise<watermark> {
245 logger.debug(getMessage("inFunction", "getRandomWatermark", "Watermark"));
246 const fileContent = await this.fileInstance.get();
247 if (!Array.isArray(fileContent) || fileContent.length === 0) {
248 const err: string = getMessage("watermarkJsonFileInvalid");
249 logger.Gui.error(err);
250 throw new Error(err);
251 }
252 const chosenIndex: number = this.getRandomNumber(fileContent.length);
253 const raw = fileContent[chosenIndex];
254 const watermark: watermark = {
255 watermark: raw.Logo,
256 fontName: raw.fontName
257 };
258 return watermark;
259 }
260
280 private copyButtonScript(): string {
281 logger.debug(getMessage("inFunction", "copyButtonScript", "Watermark"));
282 return `
283<script>
284 const vscode = acquireVsCodeApi();
285 console.log(\`vscode = \${vscode}\`);
286 document.getElementById('copyBtn').addEventListener('click', () => {
287 const content = document.getElementById('ascii').innerText;
288 navigator.clipboard.writeText(content).then(() => {
289 vscode.postMessage({ type: 'copied' });
290 });
291 });
292</script>
293 `;
294 }
295
316 private zoomScript(): string {
317 logger.debug(getMessage("inFunction", "zoomScript", "Watermark"));
318 return `
319<script>
320 let currentSize = 20;
321 function updateFontSize(sizeDifference) {
322 console.log(\`sizeDifference = \${sizeDifference}\`);
323 const asciiPre = document.getElementById('ascii');
324 console.log(\`asciiPre = \${JSON.stringify(asciiPre)}\`);
325 console.log(\`currentSize = \${currentSize}\`);
326 if (currentSize + sizeDifference >= 2) {
327 currentSize += sizeDifference;
328 console.log(\`currentSize (after update) = \${currentSize}\`);
329 } else {
330 console.log(\`currentSize (no update) = \${currentSize}\`);
331 }
332 asciiPre.style.fontSize = currentSize + "px";
333 asciiPre.style.lineHeight = currentSize + "px";
334 console.log(\`newSize = \${asciiPre.style.fontSize}\`);
335 }
336
337 document.getElementById('zoomInBtn').addEventListener('click', () => {
338 updateFontSize((2));
339 });
340
341 document.getElementById('zoomOutBtn').addEventListener('click', () => {
342 updateFontSize((-2));
343 });
344
345 // init
346 updateFontSize();
347</script>
348 `;
349 }
350
366 private pageStyle(): string {
367 logger.debug(getMessage("inFunction", "pageStyle", "Watermark"));
368 return `
369 <style>
370 body { font-family: sans-serif; padding: 20px; }
371 h1 { font-size: 20px; margin-bottom: 0.2em; }
372 h2 { margin-top: 1.2em; }
373 pre { font-size: 10px; line-height: 10px; white-space: pre; }
374 button { margin: 10px 0; padding: 5px 12px; font-size: 14px; }
375 .logo { width:150px; height:150px }
376 </style>
377 `;
378 }
379
414 async displayRandomAuthorWatermarkInWindow() {
415 logger.debug(getMessage("inFunction", "displayRandomAuthorWatermarkInWindow", "Watermark"));
416 const randomwatermark: watermark = await this.getRandomWatermark();
417
418 const panel = vscode.window.createWebviewPanel(
419 getMessage("watermarkView"),
420 randomwatermark.fontName,
421 vscode.ViewColumn.One,
422 { enableScripts: true }
423 );
424
425 const watermark = randomwatermark.watermark;
426 logger.info(getMessage("watermarkChosen", watermark));
427 let asciiArt = "";
428 if (typeof watermark === "string") {
429 asciiArt = watermark;
430 } else if (Array.isArray(watermark)) {
431 asciiArt = watermark.join("\n");
432 } else if (watermark === undefined) {
433 asciiArt = getMessage("watermarkNotFound");
434 }
435
436 const copyButton: string = this.copyButtonScript();
437 const pageStyle: string = this.pageStyle();
438 const zoomScript: string = this.zoomScript();
439
440 panel.webview.html = `
441<!DOCTYPE html>
442<html lang="en">
443<head>
444 <meta charset="UTF-8">
445 ${pageStyle}
446</head>
447<body>
448 <p>${getMessage("watermarkAuthorName")}: Henry Letellier</p>
449 <img class="logo" src="${authorLogo}"/>
450 <div>
451 <button id="copyBtn">${getMessage('watermarkCopyAscii')}</button>
452 <button id="zoomInBtn">${getMessage('watermarkZoomIn')}</button>
453 <button id="zoomOutBtn">${getMessage('watermarkZoomOut')}</button>
454 </div>
455 <h1>${getMessage('watermarkName')}: ${randomwatermark.fontName}</h1>
456 <pre id="ascii">${asciiArt}</pre>
457 ${copyButton}
458 ${zoomScript}
459</body>
460</html>
461`;
462
463 panel.webview.onDidReceiveMessage(message => {
464 if (message.type === "copied") {
465 logger.Gui.info(getMessage("watermarkCopied", randomwatermark.fontName));
466 }
467 });
468
469 logger.Gui.info(getMessage("watermarkPersonDisplayed", randomwatermark.fontName));
470 }
471}
Generic lazy file loader with caching and type safety @template T The expected type of the loaded fil...
export const authorLogo
the base64 logo of the author's icon
Definition constants.ts:251
Structure representing a loaded ASCII art watermark with font metadata.
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
import *as vscode from vscode
Definition watermark.ts:90