Skip to content

Commit

Permalink
Store map position in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Jul 10, 2024
1 parent bf72850 commit 0cf7ea6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
39 changes: 38 additions & 1 deletion packages/base/src/mainview/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ export class MainView extends React.Component<IProps, IStates> {
container: this.divRef.current
});

this._Map.on('zoomend', () => {
if (!this._initializedPosition) {
return;
}

const zoom = this._Map.getZoom();
this._model.setOptions({ ...this._model.getOptions(), zoom });
});

this._Map.on('moveend', () => {
if (!this._initializedPosition) {
return;
}

const center = this._Map.getCenter();
this._model.setOptions({
...this._model.getOptions(),
latitude: center.lat,
longitude: center.lng
});
});

this.setState(old => ({ ...old, loading: false }));
}
};
Expand Down Expand Up @@ -386,7 +408,21 @@ export class MainView extends React.Component<IProps, IStates> {
sender: IJupyterGISDoc,
change: MapChange
): void {
// TODO SOMETHING
if (!this._initializedPosition) {
const options = this._model.getOptions();

// It is important to call setZoom first, otherwise maplibre does set the center properly
this._Map.setZoom(options.zoom || 0);
this._Map.setCenter(
(options.longitude &&
options.latitude && {
lng: options.longitude,
lat: options.latitude
}) || [0, 0]
);

this._initializedPosition = true;
}
}

private _onViewChanged(
Expand Down Expand Up @@ -486,6 +522,7 @@ export class MainView extends React.Component<IProps, IStates> {
);
}

private _initializedPosition = false;
private divRef = React.createRef<HTMLDivElement>(); // Reference of render div

private _Map: MapLibre.Map;
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ export interface IJupyterGISModel extends DocumentRegistry.IModel {
groupName?: string,
position?: number
): void;
getOptions(): IJGISOptions;
setOptions(value: IJGISOptions): void;

syncSelected(value: { [key: string]: ISelection }, emitter?: string): void;
setUserToFollow(userId?: number): void;
Expand Down
11 changes: 10 additions & 1 deletion packages/schema/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
IJGISLayerGroup,
IJGISLayerTree,
IJGISSource,
IJGISSources
IJGISSources,
IJGISOptions
} from './_interface/jgis';
import { JupyterGISDoc } from './doc';
import {
Expand Down Expand Up @@ -274,6 +275,14 @@ export class JupyterGISModel implements IJupyterGISModel {
this._addLayerTreeItem(id, groupName, position);
}

setOptions(value: IJGISOptions) {
this._sharedModel.options = value;
}

getOptions(): IJGISOptions {
return this._sharedModel.options;
}

syncSelected(value: { [key: string]: ISelection }, emitter?: string): void {
this.sharedModel.awareness.setLocalStateField('selected', {
value,
Expand Down
15 changes: 14 additions & 1 deletion packages/schema/src/schema/jgis.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,20 @@
"type": "object",
"default": {},
"additionalProperties": false,
"properties": {}
"properties": {
"latitude": {
"type": "number",
"default": 0
},
"longitude": {
"type": "number",
"default": 0
},
"zoom": {
"type": "number",
"default": 0
}
}
},
"jGISLayerItem": {
"title": "IJGISLayerItem",
Expand Down

0 comments on commit 0cf7ea6

Please sign in to comment.