leaflet-lasso
Advanced tools
Comparing version 1.1.3 to 2.0.0-alpha1
@@ -0,15 +1,15 @@ | ||
import L from 'leaflet'; | ||
import * as LassoHandler from './lasso-handler'; | ||
import * as LassoControl from './lasso-control'; | ||
declare module 'leaflet' { | ||
interface LassoOptions { | ||
polygon?: PolylineOptions; | ||
cursor?: string; | ||
let Lasso: typeof LassoHandler.LassoHandler; | ||
let lasso: (map: L.Map, options: LassoHandler.LassoHandlerOptions) => LassoHandler.LassoHandler; | ||
namespace Control { | ||
let Lasso: typeof LassoControl.LassoControl; | ||
} | ||
interface LassoFinishedEvent extends LeafletEvent { | ||
latLngs: LatLng[]; | ||
layers: Layer[]; | ||
namespace control { | ||
let lasso: (options: LassoControl.LassoControlOptions) => LassoControl.LassoControl; | ||
} | ||
class Lasso extends Handler { | ||
constructor(map: Map, options?: LassoOptions); | ||
} | ||
const lasso: (map: Map, options?: LassoOptions) => Lasso; | ||
} | ||
export {}; | ||
export * from './lasso-handler'; | ||
export * from './lasso-control'; |
{ | ||
"name": "leaflet-lasso", | ||
"version": "1.1.3", | ||
"version": "2.0.0-alpha1", | ||
"description": "Leaflet plugin for lasso selection", | ||
@@ -10,15 +10,29 @@ "keywords": [ | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"main": "dist/leaflet-lasso.cjs.js", | ||
"module": "dist/leaflet-lasso.esm.js", | ||
"browser": "dist/leaflet-lasso.umd.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc && webpack -p", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"build": "rm -rf dist && rollup -c", | ||
"test": "jest", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"devDependencies": { | ||
"@types/leaflet": "^1.2.7", | ||
"@types/leaflet.markercluster": "^1.0.3", | ||
"ts-loader": "^4.4.1", | ||
"typescript": "^2.9.2", | ||
"webpack": "^4.12.0", | ||
"webpack-cli": "^3.0.8" | ||
"@types/jest": "^24.0.13", | ||
"@types/leaflet": "^1.4.4", | ||
"@types/leaflet.markercluster": "^1.4.0", | ||
"autoprefixer": "^9.5.1", | ||
"jest": "^24.8.0", | ||
"leaflet": "^1.5.1", | ||
"leaflet.markercluster": "^1.4.1", | ||
"postcss-assets": "^5.0.0", | ||
"rollup": "^1.13.0", | ||
"rollup-plugin-commonjs": "^10.0.0", | ||
"rollup-plugin-node-resolve": "^5.0.1", | ||
"rollup-plugin-postcss": "^2.0.3", | ||
"rollup-plugin-terser": "^5.0.0", | ||
"rollup-plugin-typescript2": "^0.21.1", | ||
"rollup-plugin-visualizer": "^1.1.1", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.5.1" | ||
}, | ||
@@ -30,4 +44,5 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@turf/boolean-point-in-polygon": "^6.0.1" | ||
"circle-to-polygon": "^1.0.2", | ||
"terraformer": "^1.0.9" | ||
} | ||
} |
@@ -7,16 +7,51 @@ # leaflet-lasso | ||
Supports all Leaflet vector layers: | ||
- Marker | ||
- CircleMarker | ||
- Circle | ||
- Polyline | ||
- Polyline with multiple segments | ||
- Rectangle | ||
- Polygon | ||
- Polygon with hole | ||
- Polygon with multiple segments | ||
- Polygon with multiple segments and holes | ||
Selection modes: | ||
- contain - default, entire shape must be in lasso polygon | ||
- intersect - any part of shape can be in lasso polygon | ||
## Install | ||
- `npm install leaflet-lasso` | ||
- `import "leaflet-lasso"` | ||
``` | ||
npm install leaflet-lasso | ||
import "leaflet-lasso" | ||
``` | ||
or | ||
- `<script src="https://unpkg.com/leaflet-lasso@latest/dist/leaflet-lasso.min.js"></script>` | ||
``` | ||
<script src="https://unpkg.com/leaflet-lasso@latest/dist/leaflet-lasso.min.js"></script> | ||
``` | ||
## Usage | ||
### Control | ||
``` | ||
const lasso = L.control.lasso().addTo(map); | ||
map.on('lasso.finished', (event) => { | ||
console.log(event.layers); | ||
}); | ||
``` | ||
### Handler | ||
``` | ||
const lasso = L.lasso(map); | ||
lasso.enable(); | ||
yourCustomButton.addEventListener('click', () => { | ||
lasso.enable(); | ||
}); | ||
map.on('lasso.finished', (event) => { | ||
@@ -27,16 +62,12 @@ console.log(event.layers); | ||
## Methods | ||
## Options | ||
- `enable()` | ||
- `disable()` | ||
## Events | ||
- `lasso.finished` | ||
- `lasso.enabled` | ||
- `lasso.disabled` | ||
## API | ||
## TODO | ||
See TypeScript type definitions | ||
- support also other layer types, not just markers (with @turf/intersect) | ||
- add L.Control | ||
## Thanks | ||
Icon by @Falke-Design |
144
src/index.ts
@@ -1,137 +0,25 @@ | ||
import * as L from 'leaflet'; | ||
import { LeafletEvent, LeafletMouseEvent } from 'leaflet'; | ||
import booleanPointInPolygon from '@turf/boolean-point-in-polygon'; | ||
import L from 'leaflet'; | ||
import * as LassoHandler from './lasso-handler'; | ||
import * as LassoControl from './lasso-control'; | ||
declare module 'leaflet' { | ||
export interface LassoOptions { | ||
polygon?: PolylineOptions, | ||
cursor?: string; | ||
let Lasso: typeof LassoHandler.LassoHandler; | ||
let lasso: (map: L.Map, options: LassoHandler.LassoHandlerOptions) => LassoHandler.LassoHandler; | ||
namespace Control { | ||
let Lasso: typeof LassoControl.LassoControl; | ||
} | ||
export interface LassoFinishedEvent extends LeafletEvent { | ||
latLngs: LatLng[]; | ||
layers: Layer[]; | ||
namespace control { | ||
let lasso: (options: LassoControl.LassoControlOptions) => LassoControl.LassoControl; | ||
} | ||
export class Lasso extends Handler { | ||
constructor(map: Map, options?: LassoOptions); | ||
} | ||
export const lasso: (map: Map, options?: LassoOptions) => Lasso; | ||
} | ||
const Lasso = L.Handler.extend({ | ||
options: { | ||
polygon: { | ||
color: '#00C3FF', | ||
weight: 2, | ||
}, | ||
cursor: 'crosshair', | ||
} as L.LassoOptions, | ||
L.Lasso = LassoHandler.LassoHandler; | ||
L.lasso = (map: L.Map, options: LassoHandler.LassoHandlerOptions) => new LassoHandler.LassoHandler(map, options); | ||
map: undefined as L.Map | undefined, | ||
polygon: undefined as L.Polygon | undefined, | ||
L.Control.Lasso = LassoControl.LassoControl; | ||
L.control.lasso = (options: LassoControl.LassoControlOptions) => new LassoControl.LassoControl(options); | ||
initialize(map: L.Map, options?: L.LassoOptions) { | ||
this.map = map; | ||
this.onMouseUpBound = this.onMouseUp.bind(this); | ||
L.Util.setOptions(this, options); | ||
}, | ||
addHooks() { | ||
this.map.on('mousedown', this.onMouseDown, this); | ||
this.map.on('mouseup', this.onMouseUp, this); | ||
document.addEventListener('mouseup', this.onMouseUpBound, true); | ||
const mapContainer = this.map.getContainer(); | ||
mapContainer.style.cursor = this.options.cursor || ''; | ||
mapContainer.style.userSelect = 'none'; | ||
mapContainer.style.msUserSelect = 'none'; | ||
(mapContainer.style as any).mozUserSelect = 'none'; // missing typings | ||
mapContainer.style.webkitUserSelect = 'none'; | ||
this.map.dragging.disable(); | ||
this.map.fire('lasso.enabled'); | ||
}, | ||
removeHooks() { | ||
this.map.off('mousedown', this.onMouseDown, this); | ||
this.map.off('mousemove', this.onMouseMove, this); | ||
this.map.off('mouseup', this.onMouseUp, this); | ||
document.removeEventListener('mouseup', this.onMouseUpBound); | ||
const mapContainer = this.map.getContainer(); | ||
mapContainer.style.cursor = ''; | ||
mapContainer.style.userSelect = ''; | ||
mapContainer.style.msUserSelect = ''; | ||
(mapContainer.style as any).mozUserSelect = ''; // missing typings | ||
mapContainer.style.webkitUserSelect = ''; | ||
this.map.dragging.enable(); | ||
this.map.fire('lasso.disabled'); | ||
}, | ||
onMouseDown(event: LeafletEvent) { | ||
const event2 = event as LeafletMouseEvent; | ||
this.polygon = L.polygon([event2.latlng], this.options.polygon).addTo(this.map); | ||
this.map.on('mousemove', this.onMouseMove, this); | ||
}, | ||
onMouseMove(event: LeafletEvent) { | ||
if (!this.polygon) { | ||
return; | ||
} | ||
const event2 = event as LeafletMouseEvent; | ||
this.polygon.addLatLng(event2.latlng); | ||
}, | ||
onMouseUp() { | ||
if (!this.polygon) { | ||
return; | ||
} | ||
const selectedFeatures = this.getSelectedLayers(this.polygon); | ||
this.map.fire('lasso.finished', { | ||
latLngs: this.polygon.getLatLngs()[0], | ||
layers: selectedFeatures, | ||
}); | ||
this.map.removeLayer(this.polygon); | ||
this.polygon = undefined; | ||
this.disable(); | ||
}, | ||
getSelectedLayers(polygon: L.Polygon) { | ||
const lassoPolygonGeometry = polygon.toGeoJSON().geometry; | ||
const layers: L.Layer[] = []; | ||
this.map.eachLayer((layer: L.Layer) => { | ||
if (layer === this.polygon) { | ||
return; | ||
} | ||
if (L.MarkerCluster && layer instanceof L.MarkerCluster) { | ||
layers.push(...layer.getAllChildMarkers()); | ||
} else { | ||
layers.push(layer); | ||
} | ||
}); | ||
const selectedLayers = layers.filter(layer => { | ||
if (layer instanceof L.Marker) { | ||
const layerGeometry = layer.toGeoJSON().geometry; | ||
return booleanPointInPolygon(layerGeometry, lassoPolygonGeometry); | ||
} | ||
return false; | ||
}); | ||
return selectedLayers; | ||
}, | ||
}); | ||
(L as any).Lasso = Lasso; | ||
(L as any).lasso = (map: L.Map, options: L.LassoOptions) => { | ||
return new Lasso(map, options); | ||
}; | ||
export * from './lasso-handler'; | ||
export * from './lasso-control'; |
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "commonjs", | ||
"module": "es2015", | ||
"lib": ["dom", "es2018"], | ||
"esModuleInterop": true, | ||
"moduleResolution": "node", | ||
"outDir": "dist", | ||
"declaration": true, | ||
"outDir": "./dist", | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true | ||
} | ||
"noUnusedParameters": true | ||
}, | ||
"include": [ | ||
"src/**/*.ts", | ||
"typings/**/*.d.ts" | ||
], | ||
"exclude": [ | ||
"**/*.test.ts", | ||
"node_modules" | ||
] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
2067408
30
2874
71
4
17
1
3
5
2
+ Addedcircle-to-polygon@^1.0.2
+ Addedterraformer@^1.0.9
+ Added@types/geojson@7946.0.15(transitive)
+ Addedcircle-to-polygon@1.0.2(transitive)
+ Addedterraformer@1.0.12(transitive)
- Removed@turf/boolean-point-in-polygon@6.5.0(transitive)
- Removed@turf/helpers@6.5.0(transitive)
- Removed@turf/invariant@6.5.0(transitive)