Skip to content

Commit

Permalink
Initial migration to typescript (#845)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
HarelM authored Dec 20, 2023
1 parent 4d1e2e6 commit e8d07fa
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 68 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"@storybook/react-vite": "^7.6.5",
"@storybook/theming": "^7.6.5",
"@types/cors": "^2.8.17",
"@types/lodash.throttle": "^4.1.9",
"@types/react": "^16.14.52",
"@types/react-dom": "^16.9.24",
"@types/uuid": "^9.0.7",
Expand Down
4 changes: 2 additions & 2 deletions src/components/Collapse.jsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion src/components/FilterEditor.jsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion src/components/SingleFilterEditor.jsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
12 changes: 0 additions & 12 deletions src/libs/accessibility.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/libs/accessibility.ts
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 0 additions & 13 deletions src/libs/diffmessage.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/libs/diffmessage.ts
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion src/libs/document-uid.js → src/libs/document-uid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
let REF = 0;

export default function(prefix="") {
export default function generateUniqueId(prefix="") {
REF++;
return prefix+REF;
}
6 changes: 0 additions & 6 deletions src/libs/filterops.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/libs/filterops.ts
Original file line number Diff line number Diff line change
@@ -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);
12 changes: 0 additions & 12 deletions src/libs/query-util.js

This file was deleted.

14 changes: 0 additions & 14 deletions src/libs/sort-numerically.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/libs/sort-numerically.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Empty file removed src/libs/stylegen.js
Empty file.
18 changes: 12 additions & 6 deletions src/libs/zoomcontrol.js → src/libs/zoomcontrol.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
}
}

0 comments on commit e8d07fa

Please sign in to comment.