Initial migration to typescript (#845)

This migrates some utilities classes from javascript to typescript.
Nothing special about the migration, mainly added types and made sure
the code adheres to the strict mode.

I have picked some small files so git doesn't think they are the same
since they are "very different".
I hope that in later PRs, when migrating lager files this won't be an
issues.
This commit is contained in:
Harel M
2023-12-20 22:56:33 +02:00
committed by GitHub
parent 4d1e2e6893
commit e8d07fa694
17 changed files with 76 additions and 68 deletions

View File

@@ -1,7 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import { Collapse as ReactCollapse } from 'react-collapse'
import accessibility from '../../libs/accessibility'
import {reducedMotionEnabled} from '../../libs/accessibility'
export default class Collapse extends React.Component {
@@ -15,7 +15,7 @@ export default class Collapse extends React.Component {
}
render() {
if (accessibility.reducedMotionEnabled()) {
if (reducedMotionEnabled()) {
return (
<div style={{display: this.props.isActive ? "block" : "none"}}>
{this.props.children}

View File

@@ -1,6 +1,6 @@
import React from 'react'
import PropTypes from 'prop-types'
import { combiningFilterOps } from '../libs/filterops.js'
import {combiningFilterOps} from '../libs/filterops'
import {mdiTableRowPlusAfter} from '@mdi/js';
import {isEqual} from 'lodash';

View File

@@ -1,7 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import { otherFilterOps } from '../libs/filterops.js'
import {otherFilterOps} from '../libs/filterops'
import InputString from './InputString'
import InputAutocomplete from './InputAutocomplete'
import InputSelect from './InputSelect'

View File

@@ -1,12 +0,0 @@
import throttle from 'lodash.throttle'
// Throttle for 3 seconds so when a user enables it they don't have to refresh the page.
const reducedMotionEnabled = throttle(() => {
return window.matchMedia("(prefers-reduced-motion: reduce)").matches
}, 3000);
export default {
reducedMotionEnabled
}

View File

@@ -0,0 +1,8 @@
import throttle from 'lodash.throttle'
export default {
// Throttle for 3 seconds so when a user enables it they don't have to refresh the page.
reducedMotionEnabled: throttle(() => {
return window.matchMedia("(prefers-reduced-motion: reduce)").matches
}, 3000)
}

View File

@@ -1,13 +0,0 @@
import {diff} from '@maplibre/maplibre-gl-style-spec'
export function diffMessages(beforeStyle, afterStyle) {
const changes = diff(beforeStyle, afterStyle)
return changes.map(cmd => cmd.command + ' ' + cmd.args.join(' '))
}
export function undoMessages(beforeStyle, afterStyle) {
return diffMessages(beforeStyle, afterStyle).map(m => 'Undo ' + m)
}
export function redoMessages(beforeStyle, afterStyle) {
return diffMessages(beforeStyle, afterStyle).map(m => 'Redo ' + m)
}

13
src/libs/diffmessage.ts Normal file
View File

@@ -0,0 +1,13 @@
import {StyleSpecification, diff} from '@maplibre/maplibre-gl-style-spec'
function diffMessages(beforeStyle: StyleSpecification, afterStyle: StyleSpecification) {
const changes = diff(beforeStyle, afterStyle)
return changes.map(cmd => cmd.command + ' ' + cmd.args.join(' '))
}
export function undoMessages(beforeStyle: StyleSpecification, afterStyle: StyleSpecification) {
return diffMessages(beforeStyle, afterStyle).map(m => 'Undo ' + m)
}
export function redoMessages(beforeStyle: StyleSpecification, afterStyle: StyleSpecification) {
return diffMessages(beforeStyle, afterStyle).map(m => 'Redo ' + m)
}

View File

@@ -3,7 +3,7 @@
*/
let REF = 0;
export default function(prefix="") {
export default function generateUniqueId(prefix="") {
REF++;
return prefix+REF;
}

View File

@@ -1,6 +0,0 @@
import {latest} from '@maplibre/maplibre-gl-style-spec'
export const combiningFilterOps = ['all', 'any', 'none']
export const setFilterOps = ['in', '!in']
export const otherFilterOps = Object
.keys(latest.filter_operator.values)
.filter(op => combiningFilterOps.indexOf(op) < 0)

7
src/libs/filterops.ts Normal file
View File

@@ -0,0 +1,7 @@
import {latest} from '@maplibre/maplibre-gl-style-spec'
export const combiningFilterOps = ['all', 'any', 'none'];
export const setFilterOps = ['in', '!in'];
export const otherFilterOps = Object
.keys(latest.filter_operator.values)
.filter(op => combiningFilterOps.indexOf(op) < 0);

View File

@@ -1,12 +0,0 @@
function asBool(queryObj, key) {
if(queryObj.hasOwnProperty(key)) {
return !queryObj[key].match(/^false|0$/);
}
else {
return false;
}
}
module.exports = {
asBool
}

View File

@@ -1,14 +0,0 @@
export default function(a, b) {
a = parseFloat(a);
b = parseFloat(b);
if(a < b) {
return -1
}
else if(a > b) {
return 1
}
else {
return 0;
}
}

View File

@@ -0,0 +1,14 @@
export default function(num1: string | number, num2: string| number) {
const a = +num1;
const b = +num2;
if(a < b) {
return -1
}
else if(a > b) {
return 1
}
else {
return 0;
}
}

View File

View File

@@ -1,5 +1,11 @@
import {Map} from 'maplibre-gl';
export default class ZoomControl {
onAdd(map) {
_map: Map| undefined = undefined;
_container: HTMLDivElement | undefined = undefined;
_textEl: HTMLSpanElement | null = null;
onAdd(map: Map) {
this._map = map;
this._container = document.createElement('div');
this._container.className = 'maplibregl-ctrl maplibregl-ctrl-group maplibregl-ctrl-zoom';
@@ -15,17 +21,17 @@ export default class ZoomControl {
}
updateZoomLevel() {
this._textEl.innerHTML = this._map.getZoom().toFixed(2);
this._textEl!.innerHTML = this._map!.getZoom().toFixed(2);
}
addEventListeners (){
this._map.on('render', this.updateZoomLevel.bind(this) );
this._map.on('zoomIn', this.updateZoomLevel.bind(this) );
this._map.on('zoomOut', this.updateZoomLevel.bind(this) );
this._map!.on('render', () => this.updateZoomLevel());
this._map!.on('zoomIn', () => this.updateZoomLevel());
this._map!.on('zoomOut', () => this.updateZoomLevel());
}
onRemove() {
this._container.parentNode.removeChild(this._container);
this._container!.parentNode!.removeChild(this._container!);
this._map = undefined;
}
}