<commit_before>import {Component, SimpleChanges, Input, Output, EventEmitter, HostListener, NgZone,
    ChangeDetectorRef} from '@angular/core';
import L from 'leaflet';
import '@geoman-io/leaflet-geoman-free';
import {FieldDocument, FieldGeometry} from 'idai-components-2';
import {LayerMapComponent} from './layer-map.component';
import {GeometryHelper} from './geometry-helper';
import {FieldPolygon} from './field-polygon';
import {FieldPolyline} from './field-polyline';
import {FieldMarker} from './field-marker';
import {ProjectConfiguration} from '../../../../core/configuration/project-configuration';
import {LayerManager} from './layer-manager';
import {LayerImageProvider} from './layer-image-provider';
import {Messages} from '../../../messages/messages';
import {SettingsService} from '../../../../core/settings/settings-service';


const remote = typeof window !== 'undefined'
  ? window.require('electron').remote
  : require('electron').remote;

declare global { namespace L { namespace PM { namespace Draw { interface Line { _finishShape(): void
                     _layer: any } }
     interface Draw { Line: L.PM.Draw.Line } } }
}

type DrawMode = 'None'|'Line'|'Poly';


@Component({
    selector: 'editable-map',
    templateUrl: './editable-map.html'
})
/**
 * @author <NAME>
 */
export class EditableMapComponent extends LayerMapComponent {

    @Input() isEditing: boolean;

    @Output() onQuitEditing: EventEmitter<FieldGeometry> =
        new EventEmitter<FieldGeometry>();

    public mousePositionCoordinates: string[]|undefined;

    private editableMarkers: Array<L.CircleMarker>;
    public selectedMarker: L.CircleMarker;

    private editablePolylines: Array<L.Polyline>;
    public selectedPolyline: L.Polyline;

    private editablePolygons: Array<L.Polygon>;
    public selectedPolygon: L.Polygon;

    private drawMode: DrawMode = 'None';


    constructor(projectConfiguration: ProjectConfiguration,
                layerManager: LayerManager,
                layerImageProvider: LayerImageProvider,
                messages: Messages,
                settingsService: SettingsService,
                protected zone: NgZone,
                protected changeDetectorRef: ChangeDetectorRef) {

        super(projectConfiguration, layerManager, layerImageProvider, messages, settingsService, zone,
            changeDetectorRef);
    }


    public getLocale = () => remote.getGlobal('config').locale;

    public isInDragMode = () => this.map.pm.globalDragModeEnabled();


    @HostListener('document:keyup', ['$event'])
    public handleKeyEvent(event: KeyboardEvent) {

        if (event.key == 'Escape') this.finishDrawing();
    }


    public addPolygon() {

        this.zone.runOutsideAngular(() => {
            this.addPolyLayer('Poly');
        });
    }


    public addPolyline() {

        this.zone.runOutsideAngular(() => {
            this.addPolyLayer('Line');
        });
    }


    public abortEditing() {

        this.zone.runOutsideAngular(() => {
            this.fadeInMapElements();
            this.resetEditing();
        });

        this.onQuitEditing.emit(undefined as any);
    }


    public finishEditing() {

        let geometry: FieldGeometry | undefined | null = { type: '', coordinates: [] };

        this.zone.runOutsideAngular(() => {
            if (this.drawMode !== 'None') this.finishDrawing();

            if (this.editablePolygons.length === 1) {
                geometry.type = 'Polygon';
                geometry.coordinates = GeometryHelper.getCoordinatesFromPolygon(this.editablePolygons[0]);
            } else if (this.editablePolygons.length > 1) {
                geometry.type = 'MultiPolygon';
                geometry.coordinates = GeometryHelper.getCoordinatesFromPolygons(this.editablePolygons);
            } else if (this.editablePolylines.length === 1) {
                geometry.type = 'LineString';
                geometry.coordinates = GeometryHelper.getCoordinatesFromPolyline(this.editablePolylines[0]);
            } else if (this.editablePolylines.length > 1) {
                geometry.type = 'MultiLineString';
                geometry.coordinates = GeometryHelper.getCoordinatesFromPolylines(this.editablePolylines);
            } else if (this.editableMarkers.length === 1) {
                geometry.type = 'Point';
                geometry.coordinates = GeometryHelper.getCoordinatesFromMarker(this.editableMarkers[0]);
            } else if (this.editableMarkers.length > 1) {
                geometry.type = 'MultiPoint';
                geometry.coordinates = GeometryHelper.getCoordinatesFromMarkers(this.editableMarkers);
            } else {
                geometry = null;
            }

            this.fadeInMapElements();
            this.resetEditing();
        });

        this.onQuitEditing.emit(geometry as any);
    }


    public addMarker() {

        this.zone.runOutsideAngular(() => {
            const marker: L.CircleMarker = this.createEditableMarker(this.map.getCenter());
            this.setSelectedMarker(marker);
        });
    }


    public getEditorType(): string|undefined {

        if (!this.isEditing || !this.selectedDocument || !this.selectedDocument.resource
            || !this.selectedDocument.resource.geometry) {
            return 'none';
        }<commit_msg>Redraw geometries after creating new geometry<commit_after>import {Component, SimpleChanges, Input, Output, EventEmitter, HostListener, NgZone,
    ChangeDetectorRef} from '@angular/core';
import L from 'leaflet';
import '@geoman-io/leaflet-geoman-free';
import {FieldDocument, FieldGeometry} from 'idai-components-2';
import {LayerMapComponent} from './layer-map.component';
import {GeometryHelper} from './geometry-helper';
import {FieldPolygon} from './field-polygon';
import {FieldPolyline} from './field-polyline';
import {FieldMarker} from './field-marker';
import {ProjectConfiguration} from '../../../../core/configuration/project-configuration';
import {LayerManager} from './layer-manager';
import {LayerImageProvider} from './layer-image-provider';
import {Messages} from '../../../messages/messages';
import {SettingsService} from '../../../../core/settings/settings-service';


const remote = typeof window !== 'undefined'
  ? window.require('electron').remote
  : require('electron').remote;

declare global { namespace L { namespace PM { namespace Draw { interface Line { _finishShape(): void
                     _layer: any } }
     interface Draw { Line: L.PM.Draw.Line } } }
}

type DrawMode = 'None'|'Line'|'Poly';


@Component({
    selector: 'editable-map',
    templateUrl: './editable-map.html'
})
/**
 * @author <NAME>
 */
export class EditableMapComponent extends LayerMapComponent {

    @Input() isEditing: boolean;

    @Output() onQuitEditing: EventEmitter<FieldGeometry> =
        new EventEmitter<FieldGeometry>();

    public mousePositionCoordinates: string[]|undefined;

    private editableMarkers: Array<L.CircleMarker>;
    public selectedMarker: L.CircleMarker;

    private editablePolylines: Array<L.Polyline>;
    public selectedPolyline: L.Polyline;

    private editablePolygons: Array<L.Polygon>;
    public selectedPolygon: L.Polygon;

    private drawMode: DrawMode = 'None';


    constructor(projectConfiguration: ProjectConfiguration,
                layerManager: LayerManager,
                layerImageProvider: LayerImageProvider,
                messages: Messages,
                settingsService: SettingsService,
                protected zone: NgZone,
                protected changeDetectorRef: ChangeDetectorRef) {

        super(projectConfiguration, layerManager, layerImageProvider, messages, settingsService, zone,
            changeDetectorRef);
    }


    public getLocale = () => remote.getGlobal('config').locale;

    public isInDragMode = () => this.map.pm.globalDragModeEnabled();


    @HostListener('document:keyup', ['$event'])
    public handleKeyEvent(event: KeyboardEvent) {

        if (event.key == 'Escape') this.finishDrawing();
    }


    public addPolygon() {

        this.zone.runOutsideAngular(() => {
            this.addPolyLayer('Poly');
            this.redrawGeometries();
        });
    }


    public addPolyline() {

        this.zone.runOutsideAngular(() => {
            this.addPolyLayer('Line');
            this.redrawGeometries();
        });
    }


    public addMarker() {

        this.zone.runOutsideAngular(() => {
            const marker: L.CircleMarker = this.createEditableMarker(this.map.getCenter());
            this.setSelectedMarker(marker);
            this.redrawGeometries();
        });
    }


    public abortEditing() {

        this.zone.runOutsideAngular(() => {
            this.fadeInMapElements();
            this.resetEditing();
        });

        this.onQuitEditing.emit(undefined as any);
    }


    public finishEditing() {

        let geometry: FieldGeometry | undefined | null = { type: '', coordinates: [] };

        this.zone.runOutsideAngular(() => {
            if (this.drawMode !== 'None') this.finishDrawing();

            if (this.editablePolygons.length === 1) {
                geometry.type = 'Polygon';
                geometry.coordinates = GeometryHelper.getCoordinatesFromPolygon(this.editablePolygons[0]);
            } else if (this.editablePolygons.length > 1) {
                geometry.type = 'MultiPolygon';
                geometry.coordinates = GeometryHelper.getCoordinatesFromPolygons(this.editablePolygons);
            } else if (this.editablePolylines.length === 1) {
                geometry.type = 'LineString';
                geometry.coordinates = GeometryHelper.getCoordinatesFromPolyline(this.editablePolylines[0]);
            } else if (this.editablePolylines.length > 1) {
                geometry.type = 'MultiLineString';
                geometry.coordinates = GeometryHelper.getCoordinatesFromPolylines(this.editablePolylines);
            } else if (this.editableMarkers.length === 1) {
                geometry.type = 'Point';
                geometry.coordinates = GeometryHelper.getCoordinatesFromMarker(this.editableMarkers[0]);
            } else if (this.editableMarkers.length > 1) {
                geometry.type = 'MultiPoint';
                geometry.coordinates = GeometryHelper.getCoordinatesFromMarkers(this.editableMarkers);
            } else {
                geometry = null;
            }

            this.fadeInMapElements();
            this.resetEditing();
        });

        this.onQuitEditing.emit(geometry as any);
    }


    public getEditorType(): string|undefined {

        if (!this.isEditing || !this.selectedDocument || !this.selectedDocument.resource
            || !this.selectedDocument.resource.geometry) {
            return 'none';
        }
{'fraction_non_alphanumeric': 0.12466425859034917, 'fraction_numerical': 0.001852366398073539, 'mean_word_length': 2.6928864569083446, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5562444', 'n_tokens_mistral': 2993, 'n_tokens_neox': 2829, 'n_words': 560}