Skip to content

Commit

Permalink
Add JSDoc to some scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Not-A-Normal-Robot committed May 24, 2024
1 parent a517179 commit 75c2bb6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 7 deletions.
24 changes: 18 additions & 6 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@
let height = bgCanvas.offsetHeight;

let lastTimestamp = performance.now();

/**
* @typedef Star
* @property {number} size
* @property {number} x
* @property {number} y
* @property {number} dx
* @property {number} dy
*/

/** @type {Star[]} */
let stars = [];

/** @param {number} min @param {number} max @returns {number} */
function random(min, max){ return Math.random() * (min - max) + max }

function resize(){
width = bgCanvas.offsetWidth;
height = bgCanvas.offsetHeight;
width = bgCanvas?.offsetWidth;
height = bgCanvas?.offsetHeight;

bgCanvas.width = width;
bgCanvas.height = height;
bgCanvas?.width = width;
bgCanvas?.height = height;

let starCount = Math.floor(width * height * 6e-4)
stars = new Array(starCount);
Expand Down Expand Up @@ -65,8 +77,8 @@
const star = stars[i];
star.x += (star.dx * dt)
star.y += (star.dy * dt)
star.x %= width + 10
star.y %= height + 10
star.x %= width
star.y %= height
}

// draw stars
Expand Down
38 changes: 37 additions & 1 deletion js/lang.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
/**
* Language entries.
* @typedef {Object} LangEntry
* @property {Object.<string, (string|LangEntry)>} [property] - An object where each property can be either a string or another LangEntry object.
*/
let langEntries = {};
let showdown;

export function getCurrentLang() {
return localStorage.getItem('lang') || 'en';
}

export function getLangFilePath(lang) {
lang = lang ?? getCurrentLang();
return `/data/lang/${lang}.js`;
}

/**
* Gets a language entry from the current language.
* The key is a dot-separated string representing the path to the entry.
* @param {string} key
* @param {string} fallback
* @returns {string}
*/
export function getLanguageEntry(key, fallback) {
if(typeof key !== 'string') throw new Error("Key must be a string!");
if(fallback === undefined) fallback = key;
Expand All @@ -21,17 +34,36 @@ export function getLanguageEntry(key, fallback) {
}
return scope;
}

/**
* Gets a mode's full name.
* Falls back to '[mode]' if the language entry is not found.
* @param {string} mode
* @returns {string}
*/
export function getModeFullName(mode) {
const langEntry = langEntries.modes[mode];
if(!langEntry) {
return `[${mode}]`;
}
return `${langEntry.title} ${langEntry.subtitle}`;
}

/**
* Gets the full language entry object.
* It can be useful if you value performance a lot.
* @returns {LangEntry}
*/
export function getLanguageEntries() {
return langEntries;
};

/**
* Get an article's HTML.
* @param {string} key
* @param {string} fallback
* @returns {string} The article HTML.
*/
export async function getArticle(key, fallback) {
if(typeof key !== 'string') throw new Error("Key must be a string!");

Expand All @@ -47,6 +79,10 @@ export async function getArticle(key, fallback) {
return await result.text();
}

/**
* Sets the current language.
* @param {string} lang
*/
export async function setLanguage(lang){
if(lang === localStorage.getItem('lang')) return;
if(typeof lang !== 'string') throw new Error("Language must be a string!");
Expand Down
47 changes: 47 additions & 0 deletions js/map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
"use strict";
import * as LANG from "./lang.js";

/**
* @typedef {{
* name: string,
* shape: number,
* unlock: string[],
* size: number,
* x: number, y: number,
* icon: string,
* source: string
* }} Mode
*
* @typedef {{
* modes: {[name: string]: Mode},
* min_x: number, max_x: number,
* min_y: number, max_y: number,
* starting_mode: string
* }} Map
*/
{
const INIT_TIME = performance.now();
const DEBUG_MODE = true;
Expand All @@ -23,6 +42,33 @@ import * as LANG from "./lang.js";

const MODE_INFO_ELEMENT = document.getElementById("mode-info");

/**
* @type {{
* outer: HTMLElement,
* title: HTMLElement,
* subtitle: HTMLElement,
* version: HTMLElement,
* description: HTMLElement,
* name: HTMLElement,
* rankReqs: HTMLElement,
* rankReqElements: HTMLCollectionOf<HTMLCollectionOf<HTMLElement>>,
* expandButton: HTMLElement,
* closeButton: HTMLElement,
* collapseButton: HTMLElement,
* article: HTMLElement,
* featuredVideo: HTMLVideoElement,
* featuredVideoText: HTMLElement,
* entries: {
* difficulty: HTMLElement,
* diffContent: HTMLElement,
* length: HTMLElement,
* lengthContent: HTMLElement,
* version: HTMLElement,
* versionContent: HTMLElement,
* sourceLink: HTMLAnchorElement
* }
* }} MODE_INFO_ELEMENTS
*/
const MODE_INFO_ELEMENTS = {
outer: MODE_INFO_ELEMENT,
title: MODE_INFO_ELEMENT.querySelector(".title"),
Expand Down Expand Up @@ -60,6 +106,7 @@ import * as LANG from "./lang.js";

let camX = 0; let camY = 0; let camZoom = 1;

/** @type {Map} */
let map = {};
let mapLoaded = false;
let heldKeyCodes = new Set();
Expand Down

0 comments on commit 75c2bb6

Please sign in to comment.