mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +01:00
refactor(es modules): modulize utils
This commit is contained in:
parent
11df349394
commit
12e1c9f334
45 changed files with 620 additions and 283 deletions
50
src/scripts/indexedDB.js
Normal file
50
src/scripts/indexedDB.js
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// indexedDB support: ldb object
|
||||
|
||||
// @ts-ignore unimplemented historical interfaces
|
||||
const indexedDBfactory = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
|
||||
if (!indexedDBfactory) console.error("indexedDB not supported");
|
||||
|
||||
let database;
|
||||
const databaseRequest = indexedDBfactory.open("d2", 1);
|
||||
|
||||
databaseRequest.onsuccess = function () {
|
||||
database = this.result;
|
||||
};
|
||||
|
||||
databaseRequest.onerror = function (e) {
|
||||
console.error("indexedDB request error", e);
|
||||
};
|
||||
|
||||
databaseRequest.onupgradeneeded = function (event) {
|
||||
database = null;
|
||||
const store = databaseRequest.result.createObjectStore("s", {keyPath: "k"});
|
||||
store.transaction.oncomplete = function (e) {
|
||||
database = e.target.db;
|
||||
};
|
||||
};
|
||||
|
||||
function getValue(key, callback) {
|
||||
if (!database) {
|
||||
setTimeout(() => getValue(key, callback), 100);
|
||||
return;
|
||||
}
|
||||
|
||||
database.transaction("s").objectStore("s").get(key).onsuccess = function (e) {
|
||||
const value = (e.target.result && e.target.result.v) || null;
|
||||
callback(value);
|
||||
};
|
||||
}
|
||||
|
||||
function setValue(key) {
|
||||
if (!database) {
|
||||
setTimeout(() => setValue(key, value), 100);
|
||||
return;
|
||||
}
|
||||
|
||||
database
|
||||
.transaction("s", "readwrite")
|
||||
.objectStore("s")
|
||||
.put({[key]: value});
|
||||
}
|
||||
|
||||
export const ldb = {get: getValue, set: setValue};
|
||||
44
src/scripts/prompt.ts
Normal file
44
src/scripts/prompt.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import {ERROR} from "@/config/logging";
|
||||
|
||||
// prompt replacer (prompt does not work in Electron)
|
||||
const $prompt: HTMLElement = document.getElementById("prompt")!;
|
||||
const $form: HTMLFormElement = $prompt.querySelector("#promptForm")!;
|
||||
const $input: HTMLInputElement = $prompt.querySelector("#promptInput")!;
|
||||
const $text: HTMLDivElement = $prompt.querySelector("#promptText")!;
|
||||
const $cancel: HTMLButtonElement = $prompt.querySelector("#promptCancel")!;
|
||||
|
||||
const defaultText = "Please provide an input";
|
||||
const defaultOptions = {default: 1, step: 0.01, min: 0, max: 100, required: true};
|
||||
|
||||
export function prompt(promptText = defaultText, options = defaultOptions, callback: (value: number | string) => void) {
|
||||
if (options.default === undefined)
|
||||
return ERROR && console.error("Prompt: options object does not have default value defined");
|
||||
|
||||
$text.innerHTML = promptText;
|
||||
$input.type = typeof options.default === "number" ? "number" : "text";
|
||||
|
||||
if (options.step !== undefined) $input.step = String(options.step);
|
||||
if (options.min !== undefined) $input.min = String(options.min);
|
||||
if (options.max !== undefined) $input.max = String(options.max);
|
||||
|
||||
$input.required = options.required === false ? false : true;
|
||||
$input.placeholder = "type a " + $input.type;
|
||||
$input.value = String(options.default);
|
||||
$prompt.style.display = "block";
|
||||
|
||||
$form.addEventListener(
|
||||
"submit",
|
||||
event => {
|
||||
event.preventDefault();
|
||||
$prompt.style.display = "none";
|
||||
|
||||
const value = $input.type === "number" ? Number($input.value) : $input.value;
|
||||
if (callback) callback(value);
|
||||
},
|
||||
{once: true}
|
||||
);
|
||||
}
|
||||
|
||||
$cancel.addEventListener("click", () => {
|
||||
$prompt.style.display = "none";
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue