Skip to content

Commit

Permalink
feat: cache container for all adapters to avoid refetching it
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLMilner committed Apr 2, 2023
1 parent 4ebf3c8 commit d0e3332
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/adapters/leaflet.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ export class TerraDrawLeafletAdapter extends TerraDrawAdapterBase {

this._lib = config.lib;
this._map = config.map;
this._container = this._map.getContainer();
}

private _lib: typeof L;
private _map: L.Map;
private _layer: L.Layer | undefined;
private _panes: Record<string, HTMLStyleElement | undefined> = {};
private _container: HTMLElement;

/**
* Creates a pane and its associated style sheet
Expand Down Expand Up @@ -71,7 +73,7 @@ export class TerraDrawLeafletAdapter extends TerraDrawAdapterBase {
* @returns The HTMLElement representing the map container.
*/
public getMapContainer() {
return this._map.getContainer();
return this._container;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/adapters/mapbox-gl.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ export class TerraDrawMapboxGLAdapter extends TerraDrawAdapterBase {
super(config);

this._map = config.map;
this._container = this._map.getContainer();
}

private _map: mapboxgl.Map;
private _container: HTMLElement;
private _rendered: Record<string, boolean> = {};

private _addGeoJSONSource(id: string, features: Feature[]) {
Expand Down Expand Up @@ -180,7 +182,7 @@ export class TerraDrawMapboxGLAdapter extends TerraDrawAdapterBase {
* @returns The HTMLElement representing the map container.
*/
public getMapContainer() {
return this._map.getContainer();
return this._container;
}

/**
Expand Down Expand Up @@ -330,6 +332,7 @@ export class TerraDrawMapboxGLAdapter extends TerraDrawAdapterBase {
"LineString",
linestrings as Feature<LineString>[]
);

this._setGeoJSONLayerData<Polygon>(
mode,
"Polygon",
Expand Down
7 changes: 5 additions & 2 deletions src/adapters/openlayers.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ export class TerraDrawOpenLayersAdapter extends TerraDrawAdapterBase {
this._map = config.map;
this._lib = config.lib;

this._container = this._map.getViewport();

// TODO: Is this the best way to recieve keyboard events
this.getMapContainer().setAttribute("tabindex", "0");
this._container.setAttribute("tabindex", "0");
}

private _lib: InjectableOL;
private _map: Map;
private _container: HTMLElement;
private _projection = "EPSG:3857" as const;
private _vectorSource: undefined | VectorSource<Geometry>;
private _geoJSONReader: undefined | GeoJSON;
Expand Down Expand Up @@ -175,7 +178,7 @@ export class TerraDrawOpenLayersAdapter extends TerraDrawAdapterBase {
* @returns The HTMLElement representing the map container.
*/
public getMapContainer() {
return this._map.getViewport();
return this._container;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/modes/polygon/polygon.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class TerraDrawPolygonMode extends TerraDrawBaseDrawMode<PolygonStyling>
updatedCoordinates = [
currentPolygonCoordinates[0],
[event.lng, event.lat],
[event.lng, event.lat + offset],
[event.lng, event.lat - offset],
currentPolygonCoordinates[0],
];
} else if (this.currentCoordinate === 2) {
Expand Down

0 comments on commit d0e3332

Please sign in to comment.