Skip to content

Commit

Permalink
Merge 978efeb into 36da62f
Browse files Browse the repository at this point in the history
  • Loading branch information
supersonicclay committed May 10, 2019
2 parents 36da62f + 978efeb commit 186cb74
Show file tree
Hide file tree
Showing 45 changed files with 1,671 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -20,5 +20,5 @@ yarn-error.log

# copied as part of a build step
modules/main/README.md
modules/core/README.md
modules/edit-modes/README.md
modules/layers/README.md
16 changes: 13 additions & 3 deletions examples/deck/example.js
@@ -1,4 +1,5 @@
// @flow
/* eslint-env browser */

import window from 'global/window';
import React, { Component } from 'react';
Expand All @@ -10,6 +11,7 @@ import circle from '@turf/circle';

import {
EditableGeoJsonLayer,
EditableGeoJsonLayer_EDIT_MODE_POC as EditableGeoJsonLayerEditModePoc,
SelectionLayer,
CompositeModeHandler,
ModifyHandler,
Expand All @@ -32,6 +34,12 @@ import {
styles as ToolboxStyles
} from './toolbox';

// TODO: once we refactor EditableGeoJsonLayer to use new EditMode interface, this can go away
let EditableGeoJsonLayerImpl = EditableGeoJsonLayer;
if (window.location.search && window.location.search.indexOf('useEditModePoc') !== -1) {
EditableGeoJsonLayerImpl = EditableGeoJsonLayerEditModePoc;
}

const styles = {
mapContainer: {
alignItems: 'stretch',
Expand Down Expand Up @@ -98,7 +106,7 @@ const modeHandlers = Object.assign(
new ModifyHandler()
])
},
EditableGeoJsonLayer.defaultProps.modeHandlers
EditableGeoJsonLayerImpl.defaultProps.modeHandlers
);

function getEditHandleColor(handle: Object) {
Expand Down Expand Up @@ -548,7 +556,7 @@ export default class Example extends Component<
};
}

const editableGeoJsonLayer = new EditableGeoJsonLayer({
const editableGeoJsonLayer = new EditableGeoJsonLayerImpl({
id: 'geojson',
data: testFeatures,
selectedFeatureIndexes,
Expand All @@ -565,14 +573,16 @@ export default class Example extends Component<
) {
// Don't log edits that happen as the pointer moves since they're really chatty
// eslint-disable-next-line
console.log('onEdit', editType, featureIndexes, editContext);
console.log('onEdit', editType, editContext);
}
if (editType === 'removePosition' && !this.state.pointsRemovable) {
// This is a simple example of custom handling of edits
// reject the edit
return;
}
if (editType === 'addFeature' && mode !== 'duplicate') {
// TODO: once we refactor EditableGeoJsonLayer to use new EditMode interface, this check can go away
featureIndexes = featureIndexes || editContext.featureIndexes;
// Add the new feature to the selection
updatedSelectedFeatureIndexes = [...this.state.selectedFeatureIndexes, ...featureIndexes];
}
Expand Down
4 changes: 4 additions & 0 deletions examples/webpack.config.local.js
Expand Up @@ -11,6 +11,7 @@ const webpack = require('webpack');

const LIB_DIR = resolve(__dirname, '..');
const MAIN_SRC_DIR = resolve(LIB_DIR, './modules/main/src');
const EDIT_MODES_SRC_DIR = resolve(LIB_DIR, './modules/edit-modes/src');
const LAYERS_SRC_DIR = resolve(LIB_DIR, './modules/layers/src');
const OVERLAYS_SRC_DIR = resolve(LIB_DIR, './modules/overlays/src');
const REACT_EDITOR_LITE_SRC_DIR = resolve(LIB_DIR, './modules/react-map-gl-draw/src');
Expand All @@ -34,6 +35,9 @@ function makeLocalDevConfig(EXAMPLE_DIR = LIB_DIR) {
'nebula.gl/dist': MAIN_SRC_DIR,
'nebula.gl': MAIN_SRC_DIR,

'@nebula.gl/edit-modes/dist': EDIT_MODES_SRC_DIR,
'@nebula.gl/edit-modes': EDIT_MODES_SRC_DIR,

'@nebula.gl/layers/dist': LAYERS_SRC_DIR,
'@nebula.gl/layers': LAYERS_SRC_DIR,

Expand Down
49 changes: 49 additions & 0 deletions modules/edit-modes/package.json
@@ -0,0 +1,49 @@
{
"name": "@nebula.gl/edit-modes",
"description": "GeoJSON editing modes for nebula.gl",
"license": "MIT",
"version": "0.12.1",
"author": "Georgios Karnas <georgios@uber.com>",
"repository": {
"type": "git",
"url": "https://github.com/uber/nebula.gl"
},
"keywords": [
"webgl",
"visualization",
"overlay",
"layer"
],
"main": "dist/index.js",
"module": "dist-es6/index.js",
"files": [
"dist",
"dist-es6",
"src"
],
"scripts": {
"build-clean": "rm -fr dist dist-es6 && mkdir -p dist dist-es6",
"build-es6": "rm -fr dist-es6 && BABEL_ENV=es6 babel src --config-file ../../babel.config.js --out-dir dist-es6 --source-maps inline",
"build-es5": "rm -fr dist && BABEL_ENV=es5 babel src --config-file ../../babel.config.js --out-dir dist --source-maps inline",
"build": "yarn build-clean && yarn build-es6 && yarn build-es5 && cp ../../README.md .",
"flow": "(cd ../.. && yarn flow)",
"lint": "(cd ../.. && yarn lint)",
"prepublish": "yarn build",
"publish-prod": "yarn build && yarn test && yarn test-dist && npm publish",
"publish-beta": "yarn build && yarn test && yarn test-dist && npm publish --tag beta",
"test": "yarn lint && yarn build && yarn flow && yarn jest",
"test-r": "yarn lint && yarn build && yarn test-node && (cd test/rendering-test && node node-rendering.js)",
"test-fast": "yarn test-node",
"test-ci": "yarn lint && node test/node-ci.js",
"test-cover": "NODE_ENV=test tape -r babel-register test/node.js && nyc report",
"test-node": "(cd ../.. && node test/node.js)",
"test-dist": "(cd ../.. && node test/node-dist.js)",
"test-browser": "webpack-dev-server --env.test --progress --hot --open",
"jest": "(cd ../.. && jest src)",
"bench": "node test/bench/node.js",
"bench-browser": "webpack-dev-server --env.bench --progress --hot --open",
"test-rendering": "(cd test/rendering-test && webpack-dev-server --config webpack.config.test-rendering.js --progress --hot --open)"
},
"dependencies": {
}
}
File renamed without changes.
40 changes: 40 additions & 0 deletions modules/edit-modes/src/index.js
@@ -0,0 +1,40 @@
// @flow

export { EditMode, BaseEditMode } from './lib/edit-mode.js';
export { ImmutableFeatureCollection } from './lib/immutable-feature-collection.js';

export type { ModeState } from './lib/edit-mode.js';

export type {
ScreenCoordinates,
EditAction,
Pick,
ClickEvent,
PointerMoveEvent,
StartDraggingEvent,
StopDraggingEvent
} from './types.js';

export type {
Position,
PointCoordinates,
LineStringCoordinates,
PolygonCoordinates,
MultiPointCoordinates,
MultiLineStringCoordinates,
MultiPolygonCoordinates,
AnyCoordinates,
Point,
LineString,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
Geometry,
Polygonal,
BoundingBoxArray,
FeatureOf,
Feature,
FeatureCollection,
AnyGeoJson
} from './geojson-types.js';
121 changes: 121 additions & 0 deletions modules/edit-modes/src/lib/draw-polygon-mode.js
@@ -0,0 +1,121 @@
// @flow

import type { ClickEvent, PointerMoveEvent } from '../types.js';
import type { Polygon, Position } from '../geojson-types.js';
import type { GeoJsonEditAction, EditHandle } from './geojson-edit-mode.js';
import {
BaseGeoJsonEditMode,
getPickedEditHandle,
getEditHandlesForGeometry
} from './geojson-edit-mode.js';

export class DrawPolygonMode extends BaseGeoJsonEditMode {
getEditHandlesAdapter(picks?: Array<Object>, mapCoords?: Position): EditHandle[] {
let handles = super.getEditHandlesAdapter(picks, mapCoords);

const tentativeFeature = this.getTentativeFeature();
if (tentativeFeature) {
handles = handles.concat(getEditHandlesForGeometry(tentativeFeature.geometry, -1));
// Slice off the handles that are are next to the pointer
if (tentativeFeature && tentativeFeature.geometry.type === 'LineString') {
// Remove the last existing handle
handles = handles.slice(0, -1);
} else if (tentativeFeature && tentativeFeature.geometry.type === 'Polygon') {
// Remove the last existing handle
handles = handles.slice(0, -1);
}
}

return handles;
}

handleClickAdapter(event: ClickEvent): ?GeoJsonEditAction {
super.handleClickAdapter(event);

const { picks } = event;
const tentativeFeature = this.getTentativeFeature();

let editAction: ?GeoJsonEditAction = null;
const clickedEditHandle = getPickedEditHandle(picks);

if (clickedEditHandle) {
// User clicked an edit handle.
// Remove it from the click sequence, so it isn't added as a new point.
const clickSequence = this.getClickSequence();
clickSequence.splice(clickSequence.length - 1, 1);
}

if (tentativeFeature && tentativeFeature.geometry.type === 'Polygon') {
const polygon: Polygon = tentativeFeature.geometry;

if (
clickedEditHandle &&
clickedEditHandle.featureIndex === -1 &&
(clickedEditHandle.positionIndexes[1] === 0 ||
clickedEditHandle.positionIndexes[1] === polygon.coordinates[0].length - 3)
) {
// They clicked the first or last point (or double-clicked), so complete the polygon

// Remove the hovered position
const polygonToAdd: Polygon = {
type: 'Polygon',
coordinates: [[...polygon.coordinates[0].slice(0, -2), polygon.coordinates[0][0]]]
};

this.resetClickSequence();
this._setTentativeFeature(null);
editAction = this.getAddFeatureOrBooleanPolygonAction(polygonToAdd);
}
}

// Trigger pointer move right away in order for it to update edit handles (to support double-click)
const fakePointerMoveEvent = {
screenCoords: [-1, -1],
mapCoords: event.mapCoords,
picks: [],
isDragging: false,
pointerDownPicks: null,
pointerDownScreenCoords: null,
pointerDownMapCoords: null,
sourceEvent: null
};

this.handlePointerMoveAdapter(fakePointerMoveEvent);

return editAction;
}

handlePointerMoveAdapter({
mapCoords
}: PointerMoveEvent): { editAction: ?GeoJsonEditAction, cancelMapPan: boolean } {
const clickSequence = this.getClickSequence();
const result = { editAction: null, cancelMapPan: false };

if (clickSequence.length === 0) {
// nothing to do yet
return result;
}

if (clickSequence.length < 3) {
// Draw a LineString connecting all the clicked points with the hovered point
this._setTentativeFeature({
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [...clickSequence, mapCoords]
}
});
} else {
// Draw a Polygon connecting all the clicked points with the hovered point
this._setTentativeFeature({
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [[...clickSequence, mapCoords, clickSequence[0]]]
}
});
}

return result;
}
}

0 comments on commit 186cb74

Please sign in to comment.