typed editor

This commit is contained in:
muerwre 2019-02-21 09:18:51 +07:00
parent 2f6aec1b57
commit 9006bb375b
2 changed files with 35 additions and 37 deletions

View file

@ -61,7 +61,7 @@ interface IEditor {
user: IUser; user: IUser;
} }
export class Editor implements IEditor { export class Editor {
constructor() { constructor() {
this.logo = DEFAULT_LOGO; this.logo = DEFAULT_LOGO;
this.owner = null; this.owner = null;
@ -71,7 +71,7 @@ export class Editor implements IEditor {
this.provider = PROVIDERS[DEFAULT_PROVIDER]; this.provider = PROVIDERS[DEFAULT_PROVIDER];
const { const {
triggerOnChange, lockMapClicks, routerMoveStart, changeMode, pushPolyPoints, triggerOnChange, lockMapClicks, routerMoveStart, pushPolyPoints,
map: { map } map: { map }
} = this; } = this;
@ -175,35 +175,34 @@ export class Editor implements IEditor {
setPublic: typeof setPublic = value => store.dispatch(setPublic(value)); setPublic: typeof setPublic = value => store.dispatch(setPublic(value));
setIsEmpty: typeof setIsEmpty = value => store.dispatch(setIsEmpty(value)); setIsEmpty: typeof setIsEmpty = value => store.dispatch(setIsEmpty(value));
setMarkersShown = value => { setMarkersShown = (value: boolean): void => {
if (this.getState().markers_shown !== value) store.dispatch(setMarkersShown(value)); if (this.getState().markers_shown !== value) store.dispatch(setMarkersShown(value));
}; };
resetSaveDialog = () => store.dispatch(resetSaveDialog()); resetSaveDialog = (): void => { store.dispatch(resetSaveDialog()); };
setDistance = value => { setDistance = (value: number): void => {
if (this.getDistance() !== value) store.dispatch(setDistance(value)); if (this.getDistance() !== value) store.dispatch(setDistance(value));
}; };
setChanged = value => { setChanged = (value: boolean): void => {
if (this.getChanged() !== value) store.dispatch(setChanged(value)); if (this.getChanged() !== value) store.dispatch(setChanged(value));
}; };
clearMode = () => this.setMode(MODES.NONE); clearMode = (): void => { this.setMode(MODES.NONE); };
clearChanged = () => store.dispatch(setChanged(false)); clearChanged = (): void => { store.dispatch(setChanged(false)); };
startPoly = () => { startPoly = (): void => {
if (this.getRouterPoints()) this.router.clearAll(); if (this.getRouterPoints()) this.router.clearAll();
this.poly.continue(); this.poly.continue();
}; };
triggerOnChange = () => { triggerOnChange = (): void => {
if (this.isEmpty !== this.getIsEmpty()) this.setIsEmpty(this.isEmpty); if (this.isEmpty !== this.getIsEmpty()) this.setIsEmpty(this.isEmpty);
if (this.getEditing() && !this.getChanged()) this.setChanged(true); if (this.getEditing() && !this.getChanged()) this.setChanged(true);
}; };
createStickerOnClick = (e) => { createStickerOnClick = (e): void => {
if (!e || !e.latlng || !this.activeSticker) return; if (!e || !e.latlng || !this.activeSticker) return;
const { latlng }: { latlng: ILatLng } = e; const { latlng }: { latlng: ILatLng } = e;
@ -213,7 +212,7 @@ export class Editor implements IEditor {
this.setMode(MODES.STICKERS_SELECT); this.setMode(MODES.STICKERS_SELECT);
}; };
changeMode = mode => { changeMode = (mode: IRootState['mode']): void => {
if (this.mode === mode) { if (this.mode === mode) {
if (this.switches[mode] && this.switches[mode].toggle) { if (this.switches[mode] && this.switches[mode].toggle) {
// if we have special function on mode when it clicked again // if we have special function on mode when it clicked again
@ -231,20 +230,20 @@ export class Editor implements IEditor {
} }
}; };
enableMode = mode => { enableMode = (mode: IRootState['mode']): void => {
if (this.switches[mode] && this.switches[mode].start) this.switches[mode].start(); if (this.switches[mode] && this.switches[mode].start) this.switches[mode].start();
}; };
disableMode = mode => { disableMode = (mode: IRootState['mode']): void => {
if (this.switches[mode] && this.switches[mode].stop) this.switches[mode].stop(); if (this.switches[mode] && this.switches[mode].stop) this.switches[mode].stop();
}; };
onClick = e => { onClick = (e): void => {
if (e.originalEvent.which === 3) return; // skip right / middle click if (e.originalEvent.which === 3) return; // skip right / middle click
if (this.clickHandlers[this.mode]) this.clickHandlers[this.mode](e); if (this.clickHandlers[this.mode]) this.clickHandlers[this.mode](e);
}; };
lockMapClicks = lock => { lockMapClicks = (lock: boolean): void => {
if (lock) { if (lock) {
this.map.map.removeEventListener('mouseup', this.onClick); this.map.map.removeEventListener('mouseup', this.onClick);
this.map.map.addEventListener('mouseup', this.unlockMapClicks); this.map.map.addEventListener('mouseup', this.unlockMapClicks);
@ -254,11 +253,11 @@ export class Editor implements IEditor {
} }
}; };
unlockMapClicks = () => { unlockMapClicks = (): void => {
this.lockMapClicks(false); this.lockMapClicks(false);
}; };
routerSetStart = () => { routerSetStart = (): void => {
const { latlngs } = this.poly; const { latlngs } = this.poly;
if (!latlngs || !latlngs.length) return; if (!latlngs || !latlngs.length) return;
@ -266,17 +265,17 @@ export class Editor implements IEditor {
this.router.startFrom(latlngs[latlngs.length - 1]); this.router.startFrom(latlngs[latlngs.length - 1]);
}; };
routerMoveStart = () => { routerMoveStart = (): void => {
const { _latlngs } = this.poly.poly; const { _latlngs } = this.poly.poly;
if (_latlngs) this.router.moveStart(_latlngs[_latlngs.length - 1]); if (_latlngs) this.router.moveStart(_latlngs[_latlngs.length - 1]);
}; };
pushPolyPoints = latlngs => { pushPolyPoints = (latlngs: Array<ILatLng>): void => {
this.poly.pushPoints(latlngs); this.poly.pushPoints(latlngs);
}; };
clearSticker = () => { clearSticker = (): void => {
if (this.activeSticker) { if (this.activeSticker) {
this.setActiveSticker(null); this.setActiveSticker(null);
} else { } else {
@ -284,7 +283,7 @@ export class Editor implements IEditor {
} }
}; };
clearAll = () => { clearAll = (): void => {
this.poly.clearAll(); this.poly.clearAll();
this.router.clearAll(); this.router.clearAll();
this.stickers.clearAll(); this.stickers.clearAll();
@ -294,7 +293,7 @@ export class Editor implements IEditor {
setData = ({ setData = ({
route = [], stickers = [], owner, title, address, provider = DEFAULT_PROVIDER, logo = DEFAULT_LOGO, is_public, route = [], stickers = [], owner, title, address, provider = DEFAULT_PROVIDER, logo = DEFAULT_LOGO, is_public,
}: IEditor['initialData']) => { }: IEditor['initialData']): void => {
this.setTitle(title || ''); this.setTitle(title || '');
const { id } = this.getUser(); const { id } = this.getUser();
@ -305,6 +304,7 @@ export class Editor implements IEditor {
if (route) this.poly.setPoints(route); if (route) this.poly.setPoints(route);
this.stickers.clearAll(); this.stickers.clearAll();
if (stickers) { if (stickers) {
stickers.map(sticker => stickers.map(sticker =>
sticker.set && STICKERS[sticker.set].url && sticker.set && STICKERS[sticker.set].url &&
@ -325,14 +325,14 @@ export class Editor implements IEditor {
if (owner) this.owner = owner; if (owner) this.owner = owner;
}; };
fitDrawing = () => { fitDrawing = (): void => {
if (this.poly.isEmpty) return; if (this.poly.isEmpty) return;
const bounds = this.poly.poly.getBounds(); const bounds = this.poly.poly.getBounds();
if (bounds && Object.values(bounds)) this.map.map.fitBounds(bounds); if (bounds && Object.values(bounds)) this.map.map.fitBounds(bounds);
}; };
setInitialData = () => { setInitialData = (): void => {
const { path } = getUrlData(); const { path } = getUrlData();
const { id } = this.getUser(); const { id } = this.getUser();
const { is_public, logo } = this.getState(); const { is_public, logo } = this.getState();
@ -352,7 +352,7 @@ export class Editor implements IEditor {
}; };
}; };
startEditing = () => { startEditing = (): void => {
const { id } = this.getUser(); const { id } = this.getUser();
this.setInitialData(); this.setInitialData();
@ -362,13 +362,13 @@ export class Editor implements IEditor {
this.stickers.startEditing(); this.stickers.startEditing();
}; };
stopEditing = () => { stopEditing = (): void => {
this.poly.poly.editor.disable(); this.poly.poly.editor.disable();
this.stickers.stopEditing(); this.stickers.stopEditing();
this.router.clearAll(); this.router.clearAll();
}; };
cancelEditing = () => { cancelEditing = (): void => {
if (this.hasEmptyHistory) { if (this.hasEmptyHistory) {
this.clearAll(); this.clearAll();
this.startEditing(); this.startEditing();
@ -380,8 +380,6 @@ export class Editor implements IEditor {
this.stopEditing(); this.stopEditing();
this.clearChanged(); this.clearChanged();
return true;
}; };
dumpData = () => ({ dumpData = () => ({
@ -390,15 +388,15 @@ export class Editor implements IEditor {
provider: this.getProvider(), provider: this.getProvider(),
}); });
setProvider = provider => store.dispatch(changeProvider(provider)); setProvider: typeof changeProvider = provider => store.dispatch(changeProvider(provider));
get isEmpty() { get isEmpty(): boolean {
const { route, stickers } = this.dumpData(); const { route, stickers } = this.dumpData();
return (!route || route.length < 1) && (!stickers || stickers.length <= 0); return (!route || route.length < 1) && (!stickers || stickers.length <= 0);
} }
get hasEmptyHistory() { get hasEmptyHistory(): boolean {
const { route, stickers } = this.initialData; const { route, stickers } = this.initialData;
return (!route || route.length < 1) && (!stickers || stickers.length <= 0); return (!route || route.length < 1) && (!stickers || stickers.length <= 0);
@ -407,6 +405,6 @@ export class Editor implements IEditor {
export const editor = new Editor(); export const editor = new Editor();
// for debug purposes
declare let window: any; declare let window: any;
window.editor = editor; window.editor = editor;

View file

@ -20,8 +20,8 @@ const getX = e => (
export interface IStickerDump { export interface IStickerDump {
latlng: ILatLng, latlng: ILatLng,
sticker: string,
set: IRootState['activeSticker']['set'], set: IRootState['activeSticker']['set'],
sticker: IRootState['activeSticker']['sticker'],
angle?: number, angle?: number,
text?: string, text?: string,
} }