mobility-toolbox-js 3.0.0 → 3.0.1-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import { MapLibreLayer } from '@geoblocks/ol-maplibre-layer/lib';
2
2
  import { EventsKey } from 'ol/events';
3
3
  import Map from 'ol/Map';
4
+ import MaplibreLayerRenderer from '../renderers/MaplibreLayerRenderer';
4
5
  import { MobilityLayerOptions } from './Layer';
5
6
  import type { MapLibreLayerOptions, MapLibreOptions } from '@geoblocks/ol-maplibre-layer/lib/MapLibreLayer';
6
7
  import type { QueryRenderedFeaturesOptions } from 'maplibre-gl';
@@ -11,7 +12,7 @@ export type MaplibreLayerOptions = {
11
12
  queryRenderedFeaturesOptions?: QueryRenderedFeaturesOptions | undefined;
12
13
  style?: maplibregl.StyleSpecification | null | string;
13
14
  url?: string;
14
- } & MapLibreLayerOptions & MobilityLayerOptions;
15
+ } & MobilityLayerOptions & Omit<MapLibreLayerOptions, 'mapLibreOptions'>;
15
16
  /**
16
17
  * An OpenLayers layer able to display data from the [geOps Maps API](https://developer.geops.io/apis/maps).
17
18
  *
@@ -83,6 +84,7 @@ declare class MaplibreLayer extends MapLibreLayer {
83
84
  * @public
84
85
  */
85
86
  clone(newOptions: MaplibreLayerOptions): MaplibreLayer;
87
+ createRenderer(): MaplibreLayerRenderer;
86
88
  detachFromMap(): void;
87
89
  getStyle(): any;
88
90
  setMapInternal(map: Map): void;
@@ -2,6 +2,7 @@ import { MapLibreLayer } from '@geoblocks/ol-maplibre-layer/lib';
2
2
  import debounce from 'lodash.debounce';
3
3
  import { unByKey } from 'ol/Observable';
4
4
  import { getUrlWithParams } from '../../common/utils';
5
+ import MaplibreLayerRenderer from '../renderers/MaplibreLayerRenderer';
5
6
  import defineDeprecatedProperties from '../utils/defineDeprecatedProperties';
6
7
  const buildStyleUrl = (url, style, apiKey, apiKeyName) => {
7
8
  return getUrlWithParams(`${url}/styles/${style}/style.json`, {
@@ -146,6 +147,9 @@ class MaplibreLayer extends MapLibreLayer {
146
147
  clone(newOptions) {
147
148
  return new MaplibreLayer(Object.assign(Object.assign({}, (this.get('options') || {})), (newOptions || {})));
148
149
  }
150
+ createRenderer() {
151
+ return new MaplibreLayerRenderer(this);
152
+ }
149
153
  detachFromMap() {
150
154
  unByKey(this.olEventsKeys);
151
155
  }
@@ -0,0 +1,9 @@
1
+ import { MapLibreLayerRenderer } from '@geoblocks/ol-maplibre-layer/lib';
2
+ import { FrameState } from 'ol/Map';
3
+ import { MaplibreLayer } from '../layers';
4
+ export default class MaplibreLayerRenderer extends MapLibreLayerRenderer {
5
+ ignoreNextRender: boolean;
6
+ constructor(layer: MaplibreLayer);
7
+ renderFrame(frameState: FrameState): HTMLElement;
8
+ setIsReady(): void;
9
+ }
@@ -1,142 +1,40 @@
1
- "use strict";
2
- // /* eslint-disable no-underscore-dangle */
3
- // import { FrameState } from 'ol/Map';
4
- // import { toDegrees } from 'ol/math';
5
- // import { toLonLat } from 'ol/proj';
6
- // import LayerRenderer from 'ol/renderer/Layer';
7
- // import GeoJSON from 'ol/format/GeoJSON';
8
- // import { Coordinate } from 'ol/coordinate';
9
- // import { FeatureCallback } from 'ol/renderer/vector';
10
- // import { Feature } from 'ol';
11
- // import { Geometry } from 'ol/geom';
12
- // import { Pixel } from 'ol/pixel';
13
- // import { Map } from 'maplibre-gl';
14
- // import { VECTOR_TILE_FEATURE_PROPERTY } from '../../common';
15
- // import type MaplibreLayer from '../layers/MaplibreLayer';
1
+ import { MapLibreLayerRenderer } from '@geoblocks/ol-maplibre-layer/lib';
16
2
  // /**
17
- // * @private
18
- // */
19
- // const formats: {
20
- // [key: string]: GeoJSON;
21
- // } = {
22
- // 'EPSG:3857': new GeoJSON({
23
- // featureProjection: 'EPSG:3857',
24
- // }),
25
- // };
26
- // /**
27
- // * This class is a renderer for Maplibre Layer to be able to use the native ol
3
+ // * This class is usea renderer for Maplibre Layer to be able to use the native ol
28
4
  // * functionnalities like map.getFeaturesAtPixel or map.hasFeatureAtPixel.
29
5
  // * @private
30
6
  // */
31
7
  // // @ts-expect-error
32
- // export default class MaplibreLayerRenderer extends LayerRenderer<MaplibreLayer> {
33
- // getFeaturesAtCoordinate(
34
- // coordinate: Coordinate | undefined,
35
- // hitTolerance: number = 5,
36
- // ): Feature<Geometry>[] {
37
- // if (!coordinate) {
38
- // return [];
39
- // }
40
- // const layer = this.getLayer();
41
- // const map = layer.getMapInternal();
42
- // const { mbMap } = layer;
43
- // const projection =
44
- // map?.getView()?.getProjection()?.getCode() || 'EPSG:3857';
45
- // let features: Feature[] = [];
46
- // if (!formats[projection]) {
47
- // formats[projection] = new GeoJSON({
48
- // featureProjection: projection,
49
- // });
50
- // }
51
- // if (mbMap?.isStyleLoaded()) {
52
- // const pixel =
53
- // coordinate && mbMap.project(toLonLat(coordinate) as [number, number]);
54
- // if (pixel?.x && pixel?.y) {
55
- // let pixels: [[number, number], [number, number]] | [number, number] = [
56
- // pixel.x,
57
- // pixel.y,
58
- // ];
59
- // if (hitTolerance) {
60
- // const [x, y] = pixels as [number, number];
61
- // pixels = [
62
- // [x - hitTolerance, y - hitTolerance],
63
- // [x + hitTolerance, y + hitTolerance],
64
- // ];
65
- // }
66
- // // At this point we get GeoJSON Maplibre feature, we transform it to an OpenLayers
67
- // // feature to be consistent with other layers.
68
- // features = (mbMap as Map)
69
- // .queryRenderedFeatures(
70
- // pixels,
71
- // layer.queryRenderedFeaturesOptions || {},
72
- // )
73
- // .map((feature) => {
74
- // const olFeature = formats[projection].readFeature(
75
- // feature,
76
- // ) as Feature;
77
- // if (olFeature) {
78
- // // We save the original Maplibre feature to avoid losing informations
79
- // // potentially needed for other functionnality like highlighting
80
- // // (id, layer id, source, sourceLayer ...)
81
- // // @ts-expect-error
82
- // olFeature.set(VECTOR_TILE_FEATURE_PROPERTY, feature);
83
- // }
84
- // return olFeature;
85
- // });
86
- // }
87
- // }
88
- // return features;
89
- // }
90
- // // eslint-disable-next-line class-methods-use-this
91
- // override prepareFrame() {
92
- // return true;
93
- // }
94
- // override renderFrame(frameState: FrameState) {
95
- // const layer = this.getLayer();
96
- // const { map, mbMap } = layer;
97
- // if (!layer || !map || !mbMap) {
98
- // return null;
99
- // }
100
- // const canvas = mbMap.getCanvas();
101
- // const { viewState } = frameState;
102
- // const opacity = layer.getOpacity() || 1;
103
- // canvas.style.opacity = `${opacity}`;
104
- // // adjust view parameters in Maplibre
105
- // mbMap.jumpTo({
106
- // center: toLonLat(viewState.center) as [number, number],
107
- // zoom: viewState.zoom - 1,
108
- // bearing: toDegrees(-viewState.rotation),
109
- // });
110
- // if (!canvas.isConnected) {
111
- // // The canvas is not connected to the DOM, request a map rendering at the next animation frame
112
- // // to set the canvas size.
113
- // map.render();
114
- // } else if (
115
- // canvas.width !== frameState.size[0] ||
116
- // canvas.height !== frameState.size[1]
117
- // ) {
118
- // mbMap.resize();
119
- // }
120
- // mbMap.redraw();
121
- // return mbMap.getContainer();
122
- // }
123
- // override getFeatures(pixel: Pixel) {
124
- // const coordinate = this.getLayer()
125
- // ?.getMapInternal()
126
- // ?.getCoordinateFromPixel(pixel);
127
- // return Promise.resolve(this.getFeaturesAtCoordinate(coordinate));
128
- // }
129
- // override forEachFeatureAtCoordinate<Feature>(
130
- // coordinate: Coordinate,
131
- // frameState: FrameState,
132
- // hitTolerance: number,
133
- // callback: FeatureCallback<Feature>,
134
- // ): Feature | undefined {
135
- // const features = this.getFeaturesAtCoordinate(coordinate, hitTolerance);
136
- // features.forEach((feature) => {
137
- // // @ts-expect-error
138
- // callback(feature, this.layer_, feature.getGeometry());
139
- // });
140
- // return features?.[0] as Feature;
141
- // }
142
- // }
8
+ export default class MaplibreLayerRenderer extends MapLibreLayerRenderer {
9
+ constructor(layer) {
10
+ super(layer);
11
+ this.ignoreNextRender = false;
12
+ this.setIsReady = this.setIsReady.bind(this);
13
+ this.ignoreNextRender = false;
14
+ }
15
+ renderFrame(frameState) {
16
+ var _a, _b, _c;
17
+ (_b = (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.mapLibreMap) === null || _b === void 0 ? void 0 : _b.off('idle', this.setIsReady);
18
+ const mapLibreMap = (_c = this.getLayer()) === null || _c === void 0 ? void 0 : _c.mapLibreMap;
19
+ mapLibreMap === null || mapLibreMap === void 0 ? void 0 : mapLibreMap.off('idle', this.setIsReady);
20
+ if (this.ignoreNextRender) {
21
+ this.ignoreNextRender = false;
22
+ const container = mapLibreMap === null || mapLibreMap === void 0 ? void 0 : mapLibreMap.getContainer();
23
+ if (container) {
24
+ return container;
25
+ }
26
+ }
27
+ this.ready = false;
28
+ const container = super.renderFrame(frameState);
29
+ // Mark the renderer as ready when the map is idle
30
+ mapLibreMap === null || mapLibreMap === void 0 ? void 0 : mapLibreMap.once('idle', this.setIsReady);
31
+ return container;
32
+ }
33
+ setIsReady() {
34
+ if (!this.ready) {
35
+ this.ready = true;
36
+ this.ignoreNextRender = true;
37
+ this.getLayer().changed();
38
+ }
39
+ }
40
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mobility-toolbox-js",
3
3
  "license": "MIT",
4
4
  "description": "Toolbox for JavaScript applications in the domains of mobility and logistics.",
5
- "version": "3.0.0",
5
+ "version": "3.0.1-beta.2",
6
6
  "homepage": "https://mobility-toolbox-js.geops.io/",
7
7
  "exports": {
8
8
  ".": "./index.js",
@@ -108,6 +108,7 @@
108
108
  "esbuild:iife:unminify": "yarn esbuild:iife:base --outfile=build/mbt.js",
109
109
  "format": "prettier --write 'src/**/*.js' 'src/**/*.test.js' && eslint src/**/*.js src/**/*.test.js src/**/*.ts --fix && stylelint 'src/**/*.css' 'src/**/*.scss' --fix --allow-empty-input",
110
110
  "lint": "eslint src/**/*.js src/**/*.ts && stylelint src/**/*.css src/**/*.scss --allow-empty-input",
111
+ "prepare": "is-ci || husky",
111
112
  "publish:beta": "HUSKY=0 yarn release -- --prerelease beta --skip.changelog && yarn run build && cd build && HUSKY=0 yarn publish --tag beta && git push origin HEAD && git push --tags ",
112
113
  "publish:beta:dryrun": "yarn release -- --prerelease beta --dry-run --skip.changelog",
113
114
  "publish:public": "yarn release && yarn run build && cd build && HUSKY=0 yarn publish && git push origin HEAD && git push --tags ",