From 4a18823cc7eb4b6b3f00a92129fce3268cc42f59 Mon Sep 17 00:00:00 2001 From: Dominic Alie Date: Tue, 13 Oct 2015 16:03:07 -0400 Subject: [PATCH 1/3] Leaflet.Editable type definitions --- leaflet-editable/leaflet-editable-tests.ts | 1 + leaflet-editable/leaflet-editable.d.ts | 256 +++++++++++++++++++++ 2 files changed, 257 insertions(+) create mode 100644 leaflet-editable/leaflet-editable-tests.ts create mode 100644 leaflet-editable/leaflet-editable.d.ts diff --git a/leaflet-editable/leaflet-editable-tests.ts b/leaflet-editable/leaflet-editable-tests.ts new file mode 100644 index 000000000..ebb90cc97 --- /dev/null +++ b/leaflet-editable/leaflet-editable-tests.ts @@ -0,0 +1 @@ +/// \ No newline at end of file diff --git a/leaflet-editable/leaflet-editable.d.ts b/leaflet-editable/leaflet-editable.d.ts new file mode 100644 index 000000000..663cd7573 --- /dev/null +++ b/leaflet-editable/leaflet-editable.d.ts @@ -0,0 +1,256 @@ +// Type definitions for Leaflet.Editable 0.7 +// Project: https://github.com/yohanboniface/Leaflet.Editable +// Definitions by: Dominic Alie +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module L { + /** + * Make geometries editable in Leaflet. + * + * This is not a plug and play UI, and will not. This is a minimal, lightweight, and fully extendable API to + * control editing of geometries. So you can easily build your own UI with your own needs and choices. + */ + export interface EditableStatic { + new (map: Map, options: EditOptions): Editable; + } + + /** + * Options to pass to L.Editable when instanciating. + */ + export interface EditOptions { + /** + * Class to be used when creating a new Polyline. + */ + polylineClass?: Object; + + /** + * Class to be used when creating a new Polygon. + */ + polygonClass?: Object; + + /** + * Class to be used when creating a new Marker. + */ + markerClass?: Object; + + /** + * CSS class to be added to the map container while drawing. + */ + drawingCSSClass?: string; + + /** + * Layer used to store edit tools (vertex, line guide…). + */ + editLayer?: L.LayerGroup; + + /** + * Default layer used to store drawn features (marker, polyline…). + */ + featuresLayer?: L.LayerGroup; + + /** + * Class to be used as vertex, for path editing. + */ + vertexMarkerClass?: Object; + + /** + * Class to be used as middle vertex, pulled by the user to create a new point in the middle of a path. + */ + middleMarkerClass?: Object; + + /** + * Class to be used as Polyline editor. + */ + polylineEditorClass?: Object; + + /** + * Class to be used as Polygon editor. + */ + polygonEditorClass?: Object; + + /** + * Class to be used as Marker editor. + */ + markerEditorClass?: Object; + + /** + * Options to be passed to the line guides. + */ + lineGuideOptions?: Object; + + /** + * Set this to true if you don't want middle markers. + */ + skipMiddleMarkers?: boolean; + } + + /** + * Make geometries editable in Leaflet. + * + * This is not a plug and play UI, and will not. This is a minimal, lightweight, and fully extendable API to + * control editing of geometries. So you can easily build your own UI with your own needs and choices. + */ + export interface Editable extends Mixin.LeafletMixinEvents { + /** + * Options to pass to L.Editable when instanciating. + */ + options: EditOptions; + + currentPolygon: Polyline|Polygon|Marker; + + /** + * Start drawing a polyline. If latlng is given, a first point will be added. In any case, continuing on user + * click. If options is given, it will be passed to the polyline class constructor. + */ + startPolyline(latLng?: LatLng, options?: L.PolylineOptions): L.Polyline; + + /** + * Start drawing a polygon. If latlng is given, a first point will be added. In any case, continuing on user + * click. If options is given, it will be passed to the polygon class constructor. + */ + startPolygon(latLng?: LatLng, options?: L.PolylineOptions): L.Polygon; + + /** + * Start adding a marker. If latlng is given, the marker will be shown first at this point. In any case, it + * will follow the user mouse, and will have a final latlng on next click (or touch). If options is given, + * it will be passed to the marker class constructor. + */ + startMarker(latLng?: LatLng, options?: L.MarkerOptions): L.Marker; + + /** + * When you need to stop any ongoing drawing, without needing to know which editor is active. + */ + stopDrawing(): void; + } + + export var Editable: EditableStatic; + + /** + * EditableMixin is included to L.Polyline, L.Polygon and L.Marker. It adds the following methods to them. + * + * When editing is enabled, the editor is accessible on the instance with the editor property. + */ + export interface EditableMixin { + /** + * Enable editing, by creating an editor if not existing, and then calling enable on it. + */ + enableEdit(): any; + + /** + * Disable editing, also remove the editor property reference. + */ + disableEdit(): void; + + /** + * Enable or disable editing, according to current status. + */ + toggleEdit(): void; + + /** + * Return true if current instance has an editor attached, and this editor is enabled. + */ + editEnabled(): boolean; + } + + export interface Map { + /** + * Whether to create a L.Editable instance at map init or not. + */ + editable: boolean; + + /** + * Options to pass to L.Editable when instanciating. + */ + editOptions: EditOptions; + + /** + * L.Editable plugin instance. + */ + editTools: Editable; + } + + export interface Polyline extends EditableMixin { + } + + export interface MapOptions { + /** + * Whether to create a L.Editable instance at map init or not. + */ + editable?: boolean; + + /** + * Options to pass to L.Editable when instanciating. + */ + editOptions?: EditOptions; + } + + /** + * When editing a feature (marker, polyline…), an editor is attached to it. This editor basically knows + * how to handle the edition. + */ + export interface BaseEditor { + /** + * Set up the drawing tools for the feature to be editable. + */ + enable(): MarkerEditor|PolylineEditor|PolygonEditor; + + /** + * Remove editing tools. + */ + disable(): MarkerEditor|PolylineEditor|PolygonEditor; + } + + /** + * Inherit from L.Editable.BaseEditor. + * Inherited by L.Editable.PolylineEditor and L.Editable.PolygonEditor. + */ + export interface PathEditor extends BaseEditor { + /** + * Rebuild edit elements (vertex, middlemarker, etc.). + */ + reset(): void; + } + + /** + * Inherit from L.Editable.PathEditor. + */ + export interface PolylineEditor extends PathEditor { + /** + * Set up drawing tools to continue the line forward. + */ + continueForward(): void; + + /** + * Set up drawing tools to continue the line backward. + */ + continueBackward(): void; + } + + /** + * Inherit from L.Editable.PathEditor. + */ + export interface PolygonEditor extends PathEditor { + /** + * Set up drawing tools for creating a new hole on the polygon. If the latlng param is given, a first + * point is created. + */ + newHole(latlng: LatLng): void; + } + + /** + * Inherit from L.Editable.BaseEditor. + */ + export interface MarkerEditor extends BaseEditor { + } + + export interface Marker extends EditableMixin, MarkerEditor { + } + + export interface Polyline extends EditableMixin, PolylineEditor { + } + + export interface Polygon extends EditableMixin, PolygonEditor { + } +} \ No newline at end of file From abd8bf27637b962f572cc5d6e622ed2f7ed21a6b Mon Sep 17 00:00:00 2001 From: Dominic Alie Date: Wed, 14 Oct 2015 11:05:27 -0400 Subject: [PATCH 2/3] Add tests --- leaflet-editable/leaflet-editable-tests.ts | 58 +++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/leaflet-editable/leaflet-editable-tests.ts b/leaflet-editable/leaflet-editable-tests.ts index ebb90cc97..fa904f671 100644 --- a/leaflet-editable/leaflet-editable-tests.ts +++ b/leaflet-editable/leaflet-editable-tests.ts @@ -1 +1,57 @@ -/// \ No newline at end of file +/// + +var map: L.Map = L.map('div', { + editable: true, + editOptions: { + drawingCSSClass: 'css-class', + editLayer: L.layerGroup(), + featuresLayer: L.layerGroup(), + lineGuideOptions: {}, + markerClass: MarkerClass, + markerEditorClass: MarkerEditorClass, + middleMarkerClass: MiddleMarkerClass, + polygonClass: PolygonClass, + polygonEditorClass: PolygonEditorClass, + polylineClass: PolylineClass, + polylineEditorClass: PolylineEditorClass, + skipMiddleMarkers: true, + vertexMarkerClass: VertexMarkerClass + } +}); + +var currentPoly: L.Polygon|L.Polyline| L.Marker = map.editTools.currentPolygon; +map.editTools.stopDrawing(); + +var marker: L.Marker = map.editTools.startMarker(L.latLng(0, 0), { draggable: true }); +marker.disable(); +marker.enable(); +marker.toggleEdit(); +var enabled: boolean = marker.editEnabled(); + +var polyline: L.Polyline = map.editTools.startPolyline(L.latLng(0, 0), { noClip: true }); +polyline.continueBackward(); +polyline.continueForward(); +polyline.disable(); +polyline.enable(); +enabled = polyline.editEnabled(); +polyline.reset(); +polyline.toggleEdit(); + +var polygon: L.Polygon = map.editTools.startPolygon(L.latLng(0, 0), { noClip: true }); +polygon.continueBackward(); +polygon.continueForward(); +polygon.disable(); +polygon.enable(); +enabled = polygon.editEnabled(); +polygon.newHole(L.latLng(0, 0)); +polygon.reset(); +polygon.toggleEdit(); + +class MarkerClass { } +class MarkerEditorClass { } +class MiddleMarkerClass { } +class PolygonClass { } +class PolygonEditorClass { } +class PolylineClass { } +class PolylineEditorClass { } +class VertexMarkerClass { } \ No newline at end of file From 950224d101703a48c1f0b3e062038fd5411fb4c3 Mon Sep 17 00:00:00 2001 From: Dominic Alie Date: Thu, 29 Oct 2015 13:36:55 -0400 Subject: [PATCH 3/3] Fix namespace issue --- leaflet-editable/leaflet-editable.d.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/leaflet-editable/leaflet-editable.d.ts b/leaflet-editable/leaflet-editable.d.ts index 663cd7573..4297a34cb 100644 --- a/leaflet-editable/leaflet-editable.d.ts +++ b/leaflet-editable/leaflet-editable.d.ts @@ -174,16 +174,18 @@ declare module L { export interface Polyline extends EditableMixin { } - export interface MapOptions { - /** - * Whether to create a L.Editable instance at map init or not. - */ - editable?: boolean; + module Map { + export interface MapOptions { + /** + * Whether to create a L.Editable instance at map init or not. + */ + editable?: boolean; - /** - * Options to pass to L.Editable when instanciating. - */ - editOptions?: EditOptions; + /** + * Options to pass to L.Editable when instanciating. + */ + editOptions?: EditOptions; + } } /**