refactor(es modules): restore old functions with TS

This commit is contained in:
Azgaar 2022-06-27 02:21:15 +03:00
parent aa214a7826
commit dea496018c
7 changed files with 55 additions and 47 deletions

View file

@ -3,18 +3,22 @@
console.log("Hello World"); console.log("Hello World");
import {invokeActiveZooming} from "./modules/activeZooming"; import "./components";
import {applyPreset, drawBorders, drawRivers, drawStates} from "./modules/ui/layers";
import {applyMapSize, applyStoredOptions, randomizeOptions} from "./modules/ui/options";
import {ERROR, INFO, TIME, WARN} from "./config/logging"; import {ERROR, INFO, TIME, WARN} from "./config/logging";
import {UINT16_MAX} from "./constants"; import {UINT16_MAX} from "./constants";
import {invokeActiveZooming} from "./modules/activeZooming";
import {clearLegend} from "./modules/legend"; import {clearLegend} from "./modules/legend";
import {drawScaleBar, Ruler, Rulers} from "./modules/measurers"; import {drawScaleBar, Ruler, Rulers} from "./modules/measurers";
import {applyPreset, drawBorders, drawRivers, drawStates} from "./modules/ui/layers";
import {applyMapSize, applyStoredOptions, randomizeOptions} from "./modules/ui/options";
import {applyStyleOnLoad} from "./modules/ui/stylePresets"; import {applyStyleOnLoad} from "./modules/ui/stylePresets";
import {restoreDefaultEvents} from "./scripts/events"; import {restoreDefaultEvents} from "./scripts/events";
import {addGlobalListeners} from "./scripts/listeners"; import {addGlobalListeners} from "./scripts/listeners";
import {locked} from "./scripts/options/lock"; import {locked} from "./scripts/options/lock";
import {clearMainTip, tip} from "./scripts/tooltips"; import {clearMainTip, tip} from "./scripts/tooltips";
import {createTypedArray} from "./utils/arrayUtils";
import {parseError} from "./utils/errorUtils";
import {debounce} from "./utils/functionUtils";
import { import {
calculateVoronoi, calculateVoronoi,
findCell, findCell,
@ -23,15 +27,12 @@ import {
isLand, isLand,
shouldRegenerateGrid shouldRegenerateGrid
} from "./utils/graphUtils"; } from "./utils/graphUtils";
import {parseError} from "./utils/errorUtils";
import {rn, minmax, normalize} from "./utils/numberUtils";
import {createTypedArray} from "./utils/arrayUtils";
import {clipPoly} from "./utils/lineUtils";
import {rand, P, gauss, ra, rw, generateSeed} from "./utils/probabilityUtils";
import {getAdjective} from "./utils/languageUtils"; import {getAdjective} from "./utils/languageUtils";
import {debounce} from "./utils/functionUtils"; import {clipPoly} from "./utils/lineUtils";
import {minmax, normalize, rn} from "./utils/numberUtils";
import {gauss, generateSeed, P, ra, rand, rw} from "./utils/probabilityUtils";
import {byId} from "./utils/shorthands"; import {byId} from "./utils/shorthands";
import "./components"; import {round} from "./utils/stringUtils";
addGlobalListeners(); addGlobalListeners();
@ -363,8 +364,6 @@ async function generate(options) {
applyMapSize(); applyMapSize();
randomizeOptions(); randomizeOptions();
debugger;
if (shouldRegenerateGrid(grid)) grid = precreatedGraph || generateGrid(); if (shouldRegenerateGrid(grid)) grid = precreatedGraph || generateGrid();
else delete grid.cells.h; else delete grid.cells.h;
grid.cells.h = await HeightmapGenerator.generate(grid); grid.cells.h = await HeightmapGenerator.generate(grid);

View file

@ -3,7 +3,7 @@ import {findAll} from "/src/utils/graphUtils";
import {unique} from "/src/utils/arrayUtils"; import {unique} from "/src/utils/arrayUtils";
import {getRandomColor, getMixedColor} from "/src/utils/colorUtils"; import {getRandomColor, getMixedColor} from "/src/utils/colorUtils";
import {rn} from "/src/utils/numberUtils"; import {rn} from "/src/utils/numberUtils";
import {rand, P, ra, rw, biased} from "/src/utils/probabilityUtils"; import {rand, P, ra, rw, biased, gauss} from "/src/utils/probabilityUtils";
import {trimVowels, getAdjective, abbreviate} from "/src/utils/languageUtils"; import {trimVowels, getAdjective, abbreviate} from "/src/utils/languageUtils";
window.Religions = (function () { window.Religions = (function () {

View file

@ -1,7 +1,8 @@
import {TIME} from "/src/config/logging"; import {TIME, WARN} from "/src/config/logging";
import {last} from "/src/utils/arrayUtils"; import {last} from "/src/utils/arrayUtils";
import {rn} from "/src/utils/numberUtils"; import {rn} from "/src/utils/numberUtils";
import {round} from "/src/utils/stringUtils"; import {round} from "/src/utils/stringUtils";
import {rw, each} from "/src/utils/probabilityUtils";
window.Rivers = (function () { window.Rivers = (function () {
const generate = function (allowErosion = true) { const generate = function (allowErosion = true) {

View file

@ -7,6 +7,7 @@ window.viewY = 0;
window.Zoom = (function () { window.Zoom = (function () {
function onZoom() { function onZoom() {
if (!d3.event?.transform) return;
const {k, x, y} = d3.event.transform; const {k, x, y} = d3.event.transform;
const isScaleChanged = Boolean(scale - k); const isScaleChanged = Boolean(scale - k);

View file

@ -32,41 +32,43 @@ export function rollups<TObject, TKey, TReduce>(
return nest(values, Array.from, reduce, keys); return nest(values, Array.from, reduce, keys);
} }
export function debounce<T extends (...args: any[]) => any>(func: T, waitFor: number) { export function debounce(func: Function, ms: number) {
let timeout: ReturnType<typeof setTimeout>; let isCooldown = false;
return (...args: Parameters<T>): ReturnType<T> => {
let result: any; return function (this: unknown, ...args: unknown[]) {
timeout && clearTimeout(timeout); if (isCooldown) return;
timeout = setTimeout(() => { func.apply(this, args);
result = func(...args); isCooldown = true;
}, waitFor); setTimeout(() => (isCooldown = false), ms);
return result;
}; };
} }
export function throttle(func: Function, waitFor: number = 300) { export function throttle(func: Function, ms: number) {
let inThrottle: boolean; let isThrottled = false;
let lastFn: ReturnType<typeof setTimeout>; let savedArgs: unknown[];
let lastTime: number; let savedThis: unknown;
return function (this: any) { function wrapper(this: unknown, ...args: unknown[]) {
const context = this; if (isThrottled) {
const args = arguments; savedArgs = args;
savedThis = this;
return;
}
if (!inThrottle) { func.apply(this, args);
func.apply(context, args); isThrottled = true;
lastTime = Date.now();
inThrottle = true; setTimeout(function () {
} else { isThrottled = false;
clearTimeout(lastFn); if (savedArgs) {
lastFn = setTimeout(() => { wrapper.apply(savedThis, savedArgs);
if (Date.now() - lastTime >= waitFor) { savedArgs = [];
func.apply(context, args); savedThis = null;
lastTime = Date.now();
} }
}, Math.max(waitFor - (Date.now() - lastTime), 0)); }, ms);
} }
};
return wrapper;
} }
export function getBase64(url: string, callback: (base64: string | ArrayBuffer | null) => void) { export function getBase64(url: string, callback: (base64: string | ArrayBuffer | null) => void) {

View file

@ -1,10 +1,9 @@
import {polygon} from "lineclip"; import {polygon} from "lineclip";
const {graphWidth, graphHeight, pack} = window;
// clip polygon by graph bbox // clip polygon by graph bbox
export function clipPoly(points: TPoints) { export function clipPoly(points: TPoints) {
return polygon(points, [0, 0, graphWidth, graphHeight]); // @ts-expect-error graphWidth/graphWidth are global variables
return polygon(points, [0, 0, graphWidth, graphWidth]);
} }
// get segment of any point on polyline // get segment of any point on polyline
@ -40,6 +39,7 @@ export function getSegmentId(points: TPoints, point: TPoint, step = 10) {
// return center point of common edge of 2 pack cells // return center point of common edge of 2 pack cells
export function getMiddlePoint(cell1: number, cell2: number) { export function getMiddlePoint(cell1: number, cell2: number) {
// @ts-expect-error pack is global variable
const {cells, vertices} = pack; const {cells, vertices} = pack;
const commonVertices = cells.v[cell1].filter((vertex: number) => const commonVertices = cells.v[cell1].filter((vertex: number) =>

View file

@ -1,7 +1,12 @@
import {ERROR} from "../config/logging";
import {rn} from "./numberUtils"; import {rn} from "./numberUtils";
// round numbers in string to d decimals // round numbers in string to d decimals
export function round(str: string, d = 1) { export function round(str: string, d = 1) {
if (!str) {
ERROR && console.error("Path is empty", str);
return "";
}
return str.replace(/[\d\.-][\d\.e-]*/g, n => String(rn(+n, d))); return str.replace(/[\d\.-][\d\.e-]*/g, n => String(rn(+n, d)));
} }