Skip to content

Commit

Permalink
Sync center property.
Browse files Browse the repository at this point in the history
  • Loading branch information
francois2metz committed May 8, 2024
1 parent 1cceba4 commit f7b62d5
Show file tree
Hide file tree
Showing 7 changed files with 2,434 additions and 35 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ jobs:
- run: yarn install --immutable
- run: yarn build
- run: yarn lint
- run: yarn test
16 changes: 13 additions & 3 deletions lib/components/map.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
} from "@/lib/types";
import { defaults } from "@/lib/defaults";
import { MapLib } from "@/lib/lib/map.lib";
import { isLngLatEqual } from "@/lib/lib/lng_lat";
import { Position } from "@/lib/components/controls/position.enum";
import mitt from "mitt";
import { registerMap } from "@/lib/lib/mapRegistry";
Expand Down Expand Up @@ -384,8 +385,14 @@ export default defineComponent({
"map:pitch",
"map:pitchend",
"map:wheel",
// Zoom property updated
"update:zoom"
/**
* Center property updated
*/
"update:center",
/**
* Zoom property updated
*/
"update:zoom",
],
slots: Object as SlotsType<{ default: {} }>,
setup(props, ctx) {
Expand Down Expand Up @@ -430,7 +437,7 @@ export default defineComponent({
watch(
() => props.center,
(v) => {
if (v) {
if (v && !isLngLatEqual(v, map.value?.getCenter())) {

Check failure on line 440 in lib/components/map.component.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'LngLat | undefined' is not assignable to parameter of type 'LngLatLike'.

Check failure on line 440 in lib/components/map.component.ts

View workflow job for this annotation

GitHub Actions / build

Argument of type 'LngLat | undefined' is not assignable to parameter of type 'LngLatLike'.
map.value?.setCenter(v);
}
},
Expand Down Expand Up @@ -584,6 +591,9 @@ export default defineComponent({
);
map.value.once("styledata", onStyleReady);
map.value.on("load", boundMapEvents.get("__load") as any);
map.value.on("moveend", () => {
ctx.emit("update:center", map.value!.getCenter());
});
map.value.on("zoomend", () => {
ctx.emit("update:zoom", map.value!.getZoom());
});
Expand Down
11 changes: 11 additions & 0 deletions lib/lib/lng_lat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type LngLatLike, LngLat } from "maplibre-gl";

export function isLngLatEqual(one: LngLatLike, two: LngLatLike): Boolean {
const firstPosition = LngLat.convert(one);
const secondPosition = LngLat.convert(two);

return (
firstPosition.lng === secondPosition.lng &&
firstPosition.lat === secondPosition.lat
);
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,30 @@
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
"lint": "eslint lib",
"prettier": "prettier lib --write"
"prettier": "prettier lib --write",
"test": "jest"
},
"devDependencies": {
"@babel/types": "^7.23.9",
"@eslint/js": "^9.1.1",
"@indoorequal/vue-maplibre-gl": "workspace:^",
"@mdi/js": "^7.4.47",
"@types/geojson": "^7946.0.14",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.17",
"@vitejs/plugin-vue": "^5.0.4",
"eslint": "^9.1.1",
"eslint-plugin-vue": "^9.25.0",
"geojson": "^0.5.0",
"glob": "^10.3.12",
"globals": "^15.0.0",
"jest": "^29.7.0",
"maplibre-gl": "^3.0.0",
"mitt": "^3.0.1",
"modular-maptiler-sdk": "^1.0.16",
"prettier": "3.2.5",
"sass": "^1.71.0",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3",
"typescript-eslint": "^7.7.1",
"vite": "^5.1.3",
Expand Down
26 changes: 26 additions & 0 deletions test/lat_lon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
type LngLatLike,
LngLat,
} from "maplibre-gl";
import { isLngLatEqual } from '../lib/lib/lng_lat';

const tests: [LngLatLike, LngLatLike, Boolean][] = [
[[-122, 37], [-122, 37], true],
[[-122, 37], new LngLat(-122, 37), true],
[[1, 37], [-122, 37], false],
[[-122, 1], [-122, 37], false],
[new LngLat(-122, 37), new LngLat(-122, 37), true],
[new LngLat(1, 37), new LngLat(-122, 37), false],
[new LngLat(-122, 1), new LngLat(-122, 37), false],
[{lon: -122, lat: 37}, {lon: -122, lat: 37}, true],
[{lon: 1, lat: 37}, {lon: -122, lat: 37}, false],
[{lng: -122, lat: 37}, {lng: -122, lat: 37}, true],
[{lng: 1, lat: 37}, {lng: -122, lat: 37}, false]
];

tests.forEach(([one, two, expected]) => {
test(`isLngLatEqual: ${one}/${two}`, () => {
const result = isLngLatEqual(one, two);
expect(result).toBe(expected);
});
})
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"node",
"vue",
"geojson",
"maplibre-gl"
"maplibre-gl",
"jest"
]
},
"include" : [
Expand Down
Loading

0 comments on commit f7b62d5

Please sign in to comment.