Skip to content

Commit

Permalink
Iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Jun 19, 2024
1 parent 9c4ca6e commit 1547b2b
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 90 deletions.
8 changes: 3 additions & 5 deletions packages/base/src/mainview/mainview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ export class MainView extends React.Component<IProps, IStates> {
container: this.divRef.current
});

console.log('created map', this._Map);

this.setState(old => ({ ...old, loading: false }));
}
};
Expand Down Expand Up @@ -138,9 +136,9 @@ export class MainView extends React.Component<IProps, IStates> {
case 'TileLayer':
const tileLayerParameters = layer.parameters as ITileLayer;
this._Map.addSource('raster-source', {
'type': 'raster',
'tiles': [tileLayerParameters.url],
'tileSize': 256,
type: 'raster',
tiles: [tileLayerParameters.url],
tileSize: 256,
});
this._Map.addLayer({
id: 'simple-tiles',
Expand Down
156 changes: 93 additions & 63 deletions packages/schema/src/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { JSONExt, JSONObject } from '@lumino/coreutils';
import { ISignal, Signal } from '@lumino/signaling';
import * as Y from 'yjs';

import { IJGISLayer, IJGISOptions } from './_interface/jgis';
import { IJGISLayer, IJGISOptions, IJGISSource } from './_interface/jgis';
import {
IDict,
IJGISLayerDocChange,
IJGISSourceDocChange,
IJupyterGISDoc,
IJupyterGISDocChange
} from './interfaces';
Expand All @@ -19,10 +20,13 @@ export class JupyterGISDoc
super();

this._options = this.ydoc.getMap<Y.Map<any>>('options');
this._layers = this.ydoc.getArray<Y.Map<any>>('layers');
this._layers = this.ydoc.getMap<Y.Map<any>>('layers');
this._sources = this.ydoc.getMap<Y.Map<any>>('sources');
this.undoManager.addToScope(this._layers);
this.undoManager.addToScope(this._sources);

this._layers.observeDeep(this._layersObserver);
this._sources.observeDeep(this._sourcesObserver);
this._options.observe(this._optionsObserver);
}

Expand All @@ -34,11 +38,12 @@ export class JupyterGISDoc
return '0.1.0';
}

get layers(): Array<IJGISLayer> {
const objs = this._layers.map(
obj => JSONExt.deepCopy(obj.toJSON()) as IJGISLayer
);
return objs;
getLayer(id: string): IJGISLayer {
return JSONExt.deepCopy(this._layers[id].toJSON()) as IJGISLayer
}

getSource(id: string): IJGISSource {
return JSONExt.deepCopy(this._sources[id].toJSON()) as IJGISSource
}

get options(): JSONObject {
Expand All @@ -49,60 +54,60 @@ export class JupyterGISDoc
return this._layersChanged;
}

get sourcesChanged(): ISignal<IJupyterGISDoc, IJGISSourceDocChange> {
return this._sourcesChanged;
}

get optionsChanged(): ISignal<IJupyterGISDoc, MapChange> {
return this._optionsChanged;
}

layerExists(name: string): boolean {
return Boolean(this._getLayertAsYMapByName(name));
layerExists(id: string): boolean {
return Boolean(this._getLayerAsYMap(id));
}

getLayerByName(name: string): IJGISLayer | undefined {
const obj = this._getLayertAsYMapByName(name);
if (obj) {
return JSONExt.deepCopy(obj.toJSON()) as IJGISLayer;
}
return undefined;
removeLayer(id: string): void {
this.transact(() => {
this._layers.delete(id);
});
}

removeLayerByName(name: string): void {
let index = 0;
for (const obj of this._layers) {
if (obj.get('name') === name) {
break;
}
index++;
}
addLayer(id: string, value: IJGISLayer): void {
this.transact(() => {
this._layers.set(id, value);
});
}

if (this._layers.length > index) {
this.transact(() => {
this._layers.delete(index);
});
updateLayer(id: string, value: IJGISLayer): void {
const obj = this._getLayerAsYMap(id);
if (!obj) {
return;
}
this.transact(() => obj.set(id, value));
}

addLayer(value: IJGISLayer): void {
this.addLayers([value]);
sourceExists(id: string): boolean {
return Boolean(this._getSourceAsYMap(id));
}

addLayers(value: Array<IJGISLayer>): void {
removeSource(id: string): void {
this.transact(() => {
value.map(obj => {
if (!this.layerExists(obj.name)) {
this._layers.push([new Y.Map(Object.entries(obj))]);
} else {
console.error('There is already a layer with the name:', obj.name);
}
});
this._sources.delete(id);
});
}

updateLayerByName(name: string, key: string, value: any): void {
const obj = this._getLayertAsYMapByName(name);
addSource(id: string, value: IJGISSource): void {
this.transact(() => {
this._sources.set(id, value);
});
}

updateSource(id: string, value: any): void {
const obj = this._getSourceAsYMap(id);
if (!obj) {
return;
}
this.transact(() => obj.set(key, value));
this.transact(() => obj.set(id, value));
}

getOption(key: keyof IJGISOptions): IDict | undefined {
Expand Down Expand Up @@ -131,53 +136,78 @@ export class JupyterGISDoc

editable = true;

private _getLayertAsYMapByName(name: string): Y.Map<any> | undefined {
for (const obj of this._layers) {
if (obj.get('name') === name) {
return obj;
}
private _getLayerAsYMap(id: string): Y.Map<any> | undefined {
if (this._layers.has(id)) {
return this._layers.get(id);
}
return undefined;
}

private _layersObserver = (events: Y.YEvent<any>[]): void => {
private _getSourceAsYMap(id: string): Y.Map<any> | undefined {
if (this._sources.has(id)) {
return this._sources.get(id);
}
return undefined;
}

private _layersObserver(events: Y.YEvent<any>[]): void {
const changes: Array<{
name: string;
key: keyof IJGISLayer;
id: string;
newValue: IJGISLayer;
}> = [];
let needEmit = false;
events.forEach(event => {
const name = event.target.get('name');

if (name) {
event.keys.forEach((change, key) => {
if (!needEmit) {
needEmit = true;
}
changes.push({
name,
key: key as any,
newValue: JSONExt.deepCopy(event.target.toJSON())
});
event.keys.forEach((change, key) => {
if (!needEmit) {
needEmit = true;
}
changes.push({
id: key as string,
newValue: JSONExt.deepCopy(event.target.toJSON())
});
}
});
});
needEmit = changes.length === 0 ? true : needEmit;
if (needEmit) {
this._layersChanged.emit({ layerChange: changes });
}
this._changed.emit({ layerChange: changes });
};

private _sourcesObserver(events: Y.YEvent<any>[]): void {
const changes: Array<{
id: string;
newValue: IJGISSource;
}> = [];
let needEmit = false;
events.forEach(event => {
event.keys.forEach((change, key) => {
if (!needEmit) {
needEmit = true;
}
changes.push({
id: key as string,
newValue: JSONExt.deepCopy(event.target.toJSON())
});
});
});
needEmit = changes.length === 0 ? true : needEmit;
if (needEmit) {
this._sourcesChanged.emit({ sourceChange: changes });
}
};

private _optionsObserver = (event: Y.YMapEvent<Y.Map<string>>): void => {
this._optionsChanged.emit(event.keys);
};

private _layers: Y.Array<Y.Map<any>>;
private _layers: Y.Map<any>;
private _sources: Y.Map<any>;
private _options: Y.Map<any>;
private _optionsChanged = new Signal<IJupyterGISDoc, MapChange>(this);
private _layersChanged = new Signal<IJupyterGISDoc, IJGISLayerDocChange>(
this
);
private _sourcesChanged = new Signal<IJupyterGISDoc, IJGISSourceDocChange>(
this
);
}
30 changes: 21 additions & 9 deletions packages/schema/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IJGISContent,
IJGISLayers,
IJGISLayer,
IJGISSource,
IJGISOptions
} from './_interface/jgis';

Expand All @@ -25,12 +26,18 @@ export interface IDict<T = any> {

export interface IJGISLayerDocChange {
layerChange?: Array<{
name: string;
key: keyof IJGISLayer;
id: string;
newValue: IJGISLayer | undefined;
}>;
}

export interface IJGISSourceDocChange {
sourceChange?: Array<{
id: string;
newValue: IJGISSource | undefined;
}>;
}

export interface IJupyterGISClientState {
selectedPropField?: {
id: string | null;
Expand All @@ -43,25 +50,30 @@ export interface IJupyterGISClientState {
}

export interface IJupyterGISDoc extends YDocument<IJupyterGISDocChange> {
layers: Array<IJGISLayer>;
options: JSONObject;

readonly editable: boolean;
readonly toJGISEndpoint?: string;

layerExists(name: string): boolean;
getLayerByName(name: string): IJGISLayer | undefined;
removeLayerByName(name: string): void;
addLayer(value: IJGISLayer): void;
addLayers(value: Array<IJGISLayer>): void;
updateLayerByName(name: string, key: string, value: any): void;
layerExists(id: string): boolean;
getLayer(id: string): IJGISLayer | undefined;
removeLayer(id: string): void;
addLayer(id: string, value: IJGISLayer): void;
updateLayer(id: string, value: IJGISLayer): void;

sourceExists(id: string): boolean;
getSource(id: string): IJGISSource | undefined;
removeSource(id: string): void;
addSource(id: string, value: IJGISSource): void;
updateSource(id: string, value: IJGISSource): void;

getOption(key: keyof IJGISOptions): IDict | undefined;
setOption(key: keyof IJGISOptions, value: IDict): void;
setOptions(options: IJGISOptions): void;

optionsChanged: ISignal<IJupyterGISDoc, MapChange>;
layersChanged: ISignal<IJupyterGISDoc, IJGISLayerDocChange>;
sourcesChanged: ISignal<IJupyterGISDoc, IJGISSourceDocChange>;
}

export interface IJupyterGISDocChange extends DocumentChange {
Expand Down
Loading

0 comments on commit 1547b2b

Please sign in to comment.