Skip to content

Commit

Permalink
simplify & focus
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Feb 6, 2025
1 parent 79c363c commit b0b2d06
Show file tree
Hide file tree
Showing 56 changed files with 12,411 additions and 15,828 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: npm ci && npm run build
run: |
echo "@wq:registry=https://npm.pkg.github.com/wq" > .npmrc
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> .npmrc
npm ci && npm run build
- name: Install native dependencies
if: matrix.package == 'map-gl-native'
run: cd packages/$PACKAGE && npm ci
run: |
cd packages/$PACKAGE
echo "@wq:registry=https://npm.pkg.github.com/wq" > .npmrc
echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> .npmrc
npm ci
- name: Lint with ESLint
run: npm run lint
- name: Start test server
Expand Down
9,687 changes: 4,211 additions & 5,476 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions packages/map-gl-native/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export default {
preset: "jest-expo",
setupFiles: [
"./node_modules/@maplibre/maplibre-react-native/setup-jest.js",
"./setup-jest.js",
],
setupFiles: ["./setup-jest.js"],
testMatch: ["**/__tests__/**/*.js?(x)"],
transformIgnorePatterns: [],
moduleNameMapper: { "@wq/material": "<rootDir>/setup-jest.js" },
};
15,134 changes: 7,090 additions & 8,044 deletions packages/map-gl-native/package-lock.json

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions packages/map-gl-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@
},
"homepage": "https://wq.io/@wq/map-gl",
"dependencies": {
"@turf/circle": "^6.5.0",
"@wq/map": "^2.0.0"
"@wq/react": "^3.0.0-a1-dev1.g760eb56"
},
"peerDependencies": {
"@maplibre/maplibre-react-native": "*",
"expo-location": "*",
"react-native-paper": "*"
"expo-location": "*"
},
"devDependencies": {
"@maplibre/maplibre-react-native": "github:maplibre/maplibre-react-native#a2b72b6",
"expo-location": "^16.1.0",
"jest-expo": "^49.0.0",
"react-native-paper": "^5.10.3"
"@maplibre/maplibre-react-native": "^10.0.1",
"expo-location": "^18.0.5",
"jest-expo": "^52.0.3"
}
}
8 changes: 4 additions & 4 deletions packages/map-gl-native/setup-jest.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NativeModules } from "react-native";

if (!NativeModules.MGLLogging) {
NativeModules.MGLLogging = {};
}
for (const name of ["MGLLogging", "MGLOfflineModule", "MGLLocationModule"]) {
for (const name of ["MLRNModule", "MLRNLocationModule", "MLRNOfflineModule"]) {
if (!NativeModules[name]) {
NativeModules[name] = {};
}
const module = NativeModules[name];
if (!module.addListener) {
module.addListener = jest.fn();
Expand Down
4 changes: 2 additions & 2 deletions packages/map-gl-native/src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mapgl from "../index.js";
import { MapProvider } from "../index.js";

test("it loads", () => {
expect(mapgl.name).toBe("map-gl");
expect(MapProvider.displayName).toBe("MapProvider:wq");
});
34 changes: 0 additions & 34 deletions packages/map-gl-native/src/basemaps/Tile.js

This file was deleted.

10 changes: 0 additions & 10 deletions packages/map-gl-native/src/basemaps/VectorTile.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/map-gl-native/src/basemaps/index.js

This file was deleted.

45 changes: 0 additions & 45 deletions packages/map-gl-native/src/components/GeoHelpIcon.js

This file was deleted.

161 changes: 106 additions & 55 deletions packages/map-gl-native/src/components/Map.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,134 @@
import React, { useEffect, useMemo, useContext } from "react";
import PropTypes from "prop-types";
import MapLibreGL from "@maplibre/maplibre-react-native";
import { findBasemapStyle } from "../util.js";
import React, { useMemo, useContext, useRef, useCallback } from "react";
import { MapView, Camera } from "@maplibre/maplibre-react-native";
import { MapContext } from "./MapProvider.js";
import { withWQ } from "@wq/react";
import { useBasemapStyle } from "../hooks.js";
import PropTypes from "prop-types";

export default function Map({
function Map({
name,
initBounds,
children,
mapProps,
containerStyle,
basemap,
...mapProps
}) {
const {
accessToken = null,
dragRotate: rotateEnabled,
pitchWithRotate: pitchEnabled = mapProps && mapProps.dragRotate,
} = mapProps || {};
useEffect(() => {
MapLibreGL.setAccessToken(accessToken);
}, [accessToken]);

const { setInstance } = useContext(MapContext),
fitBounds = useMemo(() => {
const [[xmin, ymin], [xmax, ymax]] = initBounds;
return { sw: [xmin, ymin], ne: [xmax, ymax] };
}, [initBounds]),
style = findBasemapStyle(children),
styleURL = typeof style === "string" ? style : null,
styleJSON =
!style || typeof style === "string" ? null : JSON.stringify(style);
const mapRef = React.useRef(),
setMapRef = React.useCallback((ref) => {
mapRef.current = ref;
if (ref && cameraRef.current) {
ref.camera = cameraRef.current;
}
if (ref) {
setInstance(ref, name);
}
}, []),
cameraRef = React.useRef(),
setCameraRef = React.useCallback((ref) => {
cameraRef.current = ref;
if (ref && mapRef.current) {
mapRef.current.camera = ref;
}
}, []);
mapStyle = useBasemapStyle(basemap),
style = useMemo(() => {
return {
flex: 1,
minHeight: 200,
...containerStyle,
};
}, [containerStyle]);

containerStyle = {
flex: 1,
minHeight: 200,
...containerStyle,
};
const mapRef = useRef(),
cameraRef = useRef(),
mapInstance = useMemo(() => createMapInstance(mapRef, cameraRef), []),
onInit = useCallback(
() => setInstance(mapInstance, name),
[setInstance, mapInstance]
),
onPress = useCallback(
(e) => {
if (mapInstance.handlers.click) {
mapInstance.handlers.click(e);
}
},
[mapInstance]
),
onMove = useCallback(
(e) => {
if (mapInstance.handlers.move) {
mapInstance.handlers.move(e);
}
},
[mapInstance]
);

let rotateEnabled, pitchEnabled;
if (mapProps.rotateEnabled !== undefined) {
rotateEnabled = mapProps.rotateEnabled;
} else if (mapProps.dragRotate !== undefined) {
rotateEnabled = mapProps.dragRotate;
} else {
rotateEnabled = false;
}
if (mapProps.pitchEnabled !== undefined) {
pitchEnabled = mapProps.pitchEnabled;
} else if (mapProps.pitchWithRotate !== undefined) {
pitchEnabled = mapProps.pitchWithRotate;
} else {
pitchEnabled = rotateEnabled;
}

return (
<MapLibreGL.MapView
styleURL={styleURL}
styleJSON={styleJSON}
ref={setMapRef}
<MapView
ref={mapRef}
mapStyle={mapStyle}
rotateEnabled={rotateEnabled}
pitchEnabled={pitchEnabled}
attributionEnabled={!!accessToken}
logoEnabled={!!accessToken}
style={containerStyle}
attributionEnabled={false}
logoEnabled={false}
style={style}
onPress={onPress}
onWillStartLoadingMap={onInit}
onRegionDidChange={onMove}
{...mapProps}
>
<MapLibreGL.Camera
bounds={fitBounds}
ref={setCameraRef}
animationDuration={0}
/>
<Camera ref={cameraRef} bounds={fitBounds} animationDuration={0} />
{children}
</MapLibreGL.MapView>
</MapView>
);
}

Map.propTypes = {
name: PropTypes.string,
initBounds: PropTypes.array,
children: PropTypes.node,
mapProps: PropTypes.object,
containerStyle: PropTypes.object,
basemap: PropTypes.object,
};

export default withWQ(Map);

// Mimic some functions from maplibre-gl-js & react-map-gl
export function createMapInstance(mapRef, cameraRef) {
return {
handlers: {},
on(event, handler) {
if (!["click", "move"].includes(event)) {
console.warn("Unsupported event: " + event);
}
this.handlers[event] = handler;
},
off(event) {
delete this.handlers[event];
},
flyTo({ center, zoom }) {
this.getCamera()?.flyTo(center, zoom);
},
getBounds() {
return this.getMap()?.getVisibleBounds();
},
fitBounds(bounds, { padding = 0, duration = 300 }) {
this.getCamera()?.fitBounds(
bounds[0],
bounds[1],
padding,
duration
);
},
getMap() {
return mapRef.current;
},
getCamera() {
return cameraRef.current;
},
};
}
3 changes: 2 additions & 1 deletion packages/map-gl-native/src/components/MapLayers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Fragment } from "react";
import { withWQ } from "@wq/react";

export default Fragment;
export default withWQ(Fragment, "MapLayers");
Loading

0 comments on commit b0b2d06

Please sign in to comment.