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");
import {invokeActiveZooming} from "./modules/activeZooming";
import {applyPreset, drawBorders, drawRivers, drawStates} from "./modules/ui/layers";
import {applyMapSize, applyStoredOptions, randomizeOptions} from "./modules/ui/options";
import "./components";
import {ERROR, INFO, TIME, WARN} from "./config/logging";
import {UINT16_MAX} from "./constants";
import {invokeActiveZooming} from "./modules/activeZooming";
import {clearLegend} from "./modules/legend";
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 {restoreDefaultEvents} from "./scripts/events";
import {addGlobalListeners} from "./scripts/listeners";
import {locked} from "./scripts/options/lock";
import {clearMainTip, tip} from "./scripts/tooltips";
import {createTypedArray} from "./utils/arrayUtils";
import {parseError} from "./utils/errorUtils";
import {debounce} from "./utils/functionUtils";
import {
calculateVoronoi,
findCell,
@ -23,15 +27,12 @@ import {
isLand,
shouldRegenerateGrid
} 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 {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 "./components";
import {round} from "./utils/stringUtils";
addGlobalListeners();
@ -363,8 +364,6 @@ async function generate(options) {
applyMapSize();
randomizeOptions();
debugger;
if (shouldRegenerateGrid(grid)) grid = precreatedGraph || generateGrid();
else delete grid.cells.h;
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 {getRandomColor, getMixedColor} from "/src/utils/colorUtils";
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";
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 {rn} from "/src/utils/numberUtils";
import {round} from "/src/utils/stringUtils";
import {rw, each} from "/src/utils/probabilityUtils";
window.Rivers = (function () {
const generate = function (allowErosion = true) {

View file

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

View file

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

View file

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

View file

@ -1,7 +1,12 @@
import {ERROR} from "../config/logging";
import {rn} from "./numberUtils";
// round numbers in string to d decimals
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)));
}