Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions packages/base/src/mainview/mainView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1386,8 +1386,6 @@ export class MainView extends React.Component<IMainViewProps, IStates> {

this.addProjection(newMapLayer);
await this._waitForSourceReady(newMapLayer);

this._trackLayerViewState(id, newMapLayer);
}

this._loadingLayers.delete(id);
Expand Down Expand Up @@ -1448,6 +1446,9 @@ export class MainView extends React.Component<IMainViewProps, IStates> {
const safeIndex = Math.min(index, numLayers);
this._Map.getLayers().insertAt(safeIndex, newMapLayer);

const shouldZoom = this.state.initialLayersReady;
this._trackLayerViewState(id, newMapLayer as Layer, shouldZoom);

// doing +1 instead of calling method again
if (
!this.state.initialLayersReady &&
Expand Down Expand Up @@ -1956,10 +1957,26 @@ export class MainView extends React.Component<IMainViewProps, IStates> {
return zoom ?? view.getZoom() ?? 0;
}

private _getLayerCreatorId(layerId: string): number | undefined {
const states = this._model.sharedModel.awareness.getStates();

for (const state of states.values()) {
if (state?.lastAddedLayer?.layerId === layerId) {
return state.lastAddedLayer.clientId;
}
}

return undefined;
}

/**
* Track layer's extent and zoom in model's view state
*/
private _trackLayerViewState(layerId: string, olLayer: Layer): void {
private _trackLayerViewState(
layerId: string,
olLayer: Layer,
shouldZoom = false,
): void {
const source = olLayer.getSource();
const sourceId = source?.get?.('id');

Expand All @@ -1978,6 +1995,15 @@ export class MainView extends React.Component<IMainViewProps, IStates> {

const view: IViewState[string] = { extent, zoom };
this._model.updateLayerViewState(layerId, view);

if (shouldZoom) {
const creatorId = this._getLayerCreatorId(layerId);
const currentClientId = this._model.getClientId();

if (creatorId === currentClientId) {
this._model.centerOnPosition(layerId);
}
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/schema/src/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IJGISSource,
IJGISSources,
IJGISStoryMap,
IJGISViewState,
} from './_interface/project/jgis';
import { SCHEMA_VERSION } from './_interface/version';
import {
Expand Down Expand Up @@ -195,11 +196,11 @@ export class JupyterGISDoc
return JSONExt.deepCopy(this._stories.toJSON());
}

get viewState(): JSONObject {
get viewState(): IJGISViewState {
return JSONExt.deepCopy(this._viewState.toJSON());
}

set viewState(viewState: JSONObject) {
set viewState(viewState: IJGISViewState) {
Comment thread
martinRenou marked this conversation as resolved.
this.transact(() => {
for (const [key, value] of Object.entries(viewState)) {
this._viewState.set(key, value);
Expand Down
3 changes: 3 additions & 0 deletions packages/schema/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
IJGISSource,
IJGISSources,
IJGISStoryMap,
IJGISViewState,
LayerType,
SourceType,
} from './_interface/project/jgis';
Expand Down Expand Up @@ -127,6 +128,7 @@ export interface IJupyterGISDoc extends YDocument<IJupyterGISDocChange> {
sources: IJGISSources;
stories: IJGISStoryMaps;
layerTree: IJGISLayerTree;
viewState: IJGISViewState;
metadata: any;

readonly editable: boolean;
Expand Down Expand Up @@ -299,6 +301,7 @@ export interface IJupyterGISModel extends DocumentRegistry.IModel {
syncViewport(viewport?: IViewPortState, emitter?: string): void;
syncSelected(value: { [key: string]: ISelection }, emitter?: string): void;
selected: { [key: string]: ISelection } | undefined;
syncLastAddedLayer(layerId: string): void;
setEditingItem(type: SelectionType, itemId: string): void;
clearEditingItem(): void;
readonly editing: { type: SelectionType; itemId: string } | null;
Expand Down
7 changes: 7 additions & 0 deletions packages/schema/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ export class JupyterGISModel implements IJupyterGISModel {
): void {
if (!this.getLayer(id)) {
this.sharedModel.addLayer(id, layer);
this.syncLastAddedLayer(id);
}

this._addLayerTreeItem(id, groupName, position);
Expand Down Expand Up @@ -585,6 +586,12 @@ export class JupyterGISModel implements IJupyterGISModel {
});
}

syncLastAddedLayer(layerId: string): void {
this.sharedModel.awareness.setLocalStateField('lastAddedLayer', {
layerId,
clientId: this.getClientId(),
});
}
Comment on lines +582 to +593
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you double check why we don't need to inject clientId for syncSelected but we do for "lastAddedLayer"? Since the selection is bound to the current user I assume it's bound to the clientId somehow too. So you probably don't need to add clientId here. To be confirmed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK let me check this one 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we can get rid of manually adding clientId and getting it from awareness.

get selected(): { [key: string]: ISelection } | undefined {
return this.localState?.selected?.value;
}
Expand Down
Loading