ts for editor

This commit is contained in:
muerwre 2019-02-20 18:06:28 +07:00
parent 4aff0f7eb1
commit 2f6aec1b57
2 changed files with 37 additions and 39 deletions

View file

@ -76,7 +76,7 @@ export class Editor implements IEditor {
} = this; } = this;
this.poly = new Poly({ this.poly = new Poly({
map, routerMoveStart, lockMapClicks, setTotalDist: this.setDistance, triggerOnChange, editor: this, map, routerMoveStart, lockMapClicks, setDistance: this.setDistance, triggerOnChange, editor: this,
}); });
this.stickers = new Stickers({ map, lockMapClicks, triggerOnChange, editor: this }); this.stickers = new Stickers({ map, lockMapClicks, triggerOnChange, editor: this });

View file

@ -1,11 +1,12 @@
import L, { Map, LayerGroup, Polyline } from 'leaflet'; import { Map, LayerGroup } from 'leaflet';
import 'leaflet-geometryutil'; import 'leaflet-geometryutil';
import { EditablePolyline } from '$utils/EditablePolyline'; import { EditablePolyline } from '$utils/EditablePolyline';
import { simplify } from '$utils/simplify'; import { simplify } from '$utils/simplify';
import { findDistance, middleCoord } from '$utils/geom'; import { findDistance, middleCoord } from '$utils/geom';
import { CLIENT } from '$config/frontend'; import { CLIENT } from '$config/frontend';
import { MODES } from '$constants/modes'; import { MODES } from '$constants/modes';
import { Editor } from "$modules/Editor"; import { editor, Editor } from "$modules/Editor";
import { ILatLng } from "$modules/Stickers";
const polyStyle = { const polyStyle = {
color: 'url(#activePathGradient)', color: 'url(#activePathGradient)',
@ -13,24 +14,20 @@ const polyStyle = {
markerMid: 'url(#arrow)' markerMid: 'url(#arrow)'
}; };
interface IPoly { interface Props {
poly: EditablePolyline;
editor: Editor;
map: Map; map: Map;
routerMoveStart: () => void; editor: Editor;
setTotalDist: (dist: number) => void; routerMoveStart: typeof editor.routerMoveStart,
triggerOnChange: () => void; lockMapClicks: typeof editor.lockMapClicks,
lockMapClicks: (status: boolean) => void; setDistance: typeof editor.setDistance,
arrows: LayerGroup; triggerOnChange: typeof editor.triggerOnChange,
} }
export class Poly implements IPoly { export class Poly {
constructor({ constructor({
map, routerMoveStart, lockMapClicks, setTotalDist, triggerOnChange, editor, map, routerMoveStart, lockMapClicks, setDistance, triggerOnChange, editor,
}) { }: Props) {
const coordinates = []; this.poly = new EditablePolyline([], {
this.poly = new EditablePolyline(coordinates, {
...polyStyle, ...polyStyle,
maxMarkers: 100, maxMarkers: 100,
@ -51,14 +48,14 @@ export class Poly implements IPoly {
this.map = map; this.map = map;
this.routerMoveStart = routerMoveStart; this.routerMoveStart = routerMoveStart;
this.setTotalDist = setTotalDist; this.setDistance = setDistance;
this.triggerOnChange = triggerOnChange; this.triggerOnChange = triggerOnChange;
this.lockMapClicks = lockMapClicks; this.lockMapClicks = lockMapClicks;
this.arrows.addTo(map); this.arrows.addTo(map);
} }
setModeOnDrawing = () => { setModeOnDrawing = (): void => {
if (this.editor.getMode() !== MODES.POLY) this.editor.setMode(MODES.POLY); if (this.editor.getMode() !== MODES.POLY) this.editor.setMode(MODES.POLY);
}; };
@ -105,7 +102,7 @@ export class Poly implements IPoly {
// if (coords.length > 1) this.triggerOnChange(); // if (coords.length > 1) this.triggerOnChange();
}; };
preventMissClicks = e => { preventMissClicks = (e): void => {
const mode = this.editor.getMode(); const mode = this.editor.getMode();
if (mode === MODES.POLY) return; if (mode === MODES.POLY) return;
@ -115,28 +112,28 @@ export class Poly implements IPoly {
if (mode === MODES.NONE) this.editor.setMode(MODES.POLY); if (mode === MODES.NONE) this.editor.setMode(MODES.POLY);
}; };
continue = () => { continue = (): void => {
this.poly.editor.continueForward(); this.poly.editor.continueForward();
}; };
stop = () => { stop = (): void => {
if (this.poly) this.poly.editor.stopDrawing(); if (this.poly) this.poly.editor.stopDrawing();
}; };
continueForward = () => { continueForward = (): void => {
this.poly.continueForward(); this.poly.continueForward();
}; };
lockMap = () => { lockMap = (): void => {
this.lockMapClicks(true); this.lockMapClicks(true);
}; };
setPoints = latlngs => { setPoints = (latlngs: Array<ILatLng>): void => {
if (!latlngs || latlngs.length <= 1) return; if (!latlngs || latlngs.length <= 1) return;
this.poly.setPoints(latlngs); this.poly.setPoints(latlngs);
}; };
pushPoints = latlngs => { pushPoints = (latlngs: Array<ILatLng>): void => {
const { map } = this; const { map } = this;
const simplified = simplify({ map, latlngs }); const simplified = simplify({ map, latlngs });
const summary = [ const summary = [
@ -150,32 +147,33 @@ export class Poly implements IPoly {
this.updateMarks(); this.updateMarks();
}; };
clearAll = () => { clearAll = (): void => {
// this.poly.setLatLngs([]); // this.poly.setLatLngs([]);
this.poly.editor.clear(); this.poly.editor.clear();
this.updateMarks(); this.updateMarks();
}; };
clearArrows = () => this.arrows.clearLayers(); clearArrows = (): LayerGroup<any> => this.arrows.clearLayers();
dumpData = () => this.latlngs; dumpData = (): Array<ILatLng> => this.latlngs;
get latlngs() { get latlngs(): Array<ILatLng> {
return ( return (
this.poly && this.poly.getLatLngs().length this.poly && this.poly.getLatLngs().length
&& this.poly.getLatLngs().map(el => ({ ...el }))) || []; && this.poly.getLatLngs().map(el => ({ ...el }))) || [];
} }
get isEmpty() { get isEmpty(): boolean {
return (!this.latlngs || Object.values(this.latlngs).length <= 0); return (!this.latlngs || Object.values(this.latlngs).length <= 0);
} }
poly; poly: EditablePolyline;
editor; arrows: LayerGroup = new LayerGroup();
map;
routerMoveStart; editor: Props['editor'];
setTotalDist; map: Props['map'];
triggerOnChange; routerMoveStart: Props['routerMoveStart'];
lockMapClicks; setDistance: Props['setDistance'];
arrows = new LayerGroup(); triggerOnChange: Props['triggerOnChange'];
lockMapClicks: Props['lockMapClicks'];
} }