<commit_before>import { Component, ElementRef, OnInit, ViewEncapsulation, OnDestroy, OnChanges, Input } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { Observer } from 'rxjs/Observer';

import { buildEditor, getModeForMime, DEFAULT_TEXT } from '../util/CodeMirror';
import { PadModel } from '../services/PadModel';
import { PadEdit } from '../signaler/Protocol';

@Component({
    selector: 'editor',
    template: '',
    styleUrls: ['editor.component.scss'],
    encapsulation: ViewEncapsulation.None
})
export class EditorComponent implements OnInit, OnDestroy, OnChanges {

    @Input() pad: PadModel;

    private editor: CodeMirror.Editor;
    private applyingRemoteChanges = false;
    private isDemoMode = false;

    private remoteEditSub: Subscription;
    private localEdits: Observer<PadEdit[]>;
    private mimeSub: Subscription;

    constructor(
        private elementRef: ElementRef) { }

    ngOnInit() {
        this.editor = buildEditor(this.elementRef.nativeElement);
        this.editor.on('changes', this.onEditorChanges);
        this.editor.on('cursorActivity', this.onCursorActivity);
        this.editor.on('focus', this.onEditorFocus);
        this.editor.on('blur', this.onEditorBlur);

        this.ngOnChanges();
    }

    ngOnDestroy() {
        this.editor.off('changes', this.onEditorChanges);
        this.editor.off('cursorActivity', this.onCursorActivity);
        this.editor.off('focus', this.onEditorFocus);
        this.editor.off('blur', this.onEditorBlur);

        this.ngOnChanges();
    }

    ngOnChanges() {
        if (this.remoteEditSub) {
            this.remoteEditSub.unsubscribe();
            this.remoteEditSub = null;
            this.localEdits = null;
        }
        if (this.mimeSub) {
            this.mimeSub.unsubscribe();
            this.mimeSub = null;
        }
        const pad = this.pad;
        if (!pad || !this.editor) {
            if (this.editor) this.setDemoMode(true);
            return;
        }
        if (!pad.isStarted()) this.setDemoMode(true);

        this.remoteEditSub = pad.getRemoteEdits().subscribe(this.onRemoteEdits);
        this.localEdits = pad.getLocalEdits();
        // this.editor.setValue(pad.getContents());

        this.mimeSub = pad.getMimeType().subscribe(mime => {
            this.editor.setOption('mode', getModeForMime(mime).mime);
        });
    }

    private setDemoMode(demoMode: boolean) {
        if (demoMode === this.isDemoMode) return;
        if (demoMode) {
            this.isDemoMode = true;
            this.editor.setValue(DEFAULT_TEXT);
        } else {
            this.editor.setValue('');
            this.isDemoMode = false;
        }
    }

    private onRemoteEdits = (edits: PadEdit[]) => {
        this.setDemoMode(false);
        this.applyingRemoteChanges = true;
        const doc = this.editor.getDoc();
        this.editor.operation(() => {
            edits.forEach(edit => {
                const start = doc.posFromIndex(edit.index);<commit_msg>editor gets focus whenever the container element is clicked on<commit_after>import { Component, ElementRef, OnInit, ViewEncapsulation, OnDestroy, OnChanges, Input } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { Observer } from 'rxjs/Observer';

import { buildEditor, getModeForMime, DEFAULT_TEXT } from '../util/CodeMirror';
import { PadModel } from '../services/PadModel';
import { PadEdit } from '../signaler/Protocol';

@Component({
    selector: 'editor',
    template: '',
    styleUrls: ['editor.component.scss'],
    encapsulation: ViewEncapsulation.None,
    host: {
        '(click)': 'onEditorClick($event)'
    }
})
export class EditorComponent implements OnInit, OnDestroy, OnChanges {

    @Input() pad: PadModel;

    private editor: CodeMirror.Editor;
    private applyingRemoteChanges = false;
    private isDemoMode = false;

    private remoteEditSub: Subscription;
    private localEdits: Observer<PadEdit[]>;
    private mimeSub: Subscription;

    constructor(
        private elementRef: ElementRef) { }

    ngOnInit() {
        this.editor = buildEditor(this.elementRef.nativeElement);
        this.editor.on('changes', this.onEditorChanges);
        this.editor.on('cursorActivity', this.onCursorActivity);
        this.editor.on('focus', this.onEditorFocus);
        this.editor.on('blur', this.onEditorBlur);

        this.ngOnChanges();
    }

    ngOnDestroy() {
        this.editor.off('changes', this.onEditorChanges);
        this.editor.off('cursorActivity', this.onCursorActivity);
        this.editor.off('focus', this.onEditorFocus);
        this.editor.off('blur', this.onEditorBlur);

        this.ngOnChanges();
    }

    ngOnChanges() {
        if (this.remoteEditSub) {
            this.remoteEditSub.unsubscribe();
            this.remoteEditSub = null;
            this.localEdits = null;
        }
        if (this.mimeSub) {
            this.mimeSub.unsubscribe();
            this.mimeSub = null;
        }
        const pad = this.pad;
        if (!pad || !this.editor) {
            if (this.editor) this.setDemoMode(true);
            return;
        }
        if (!pad.isStarted()) this.setDemoMode(true);

        this.remoteEditSub = pad.getRemoteEdits().subscribe(this.onRemoteEdits);
        this.localEdits = pad.getLocalEdits();
        // this.editor.setValue(pad.getContents());

        this.mimeSub = pad.getMimeType().subscribe(mime => {
            this.editor.setOption('mode', getModeForMime(mime).mime);
        });
    }

    onEditorClick(event: MouseEvent) {
        this.editor.focus();
    }

    private setDemoMode(demoMode: boolean) {
        if (demoMode === this.isDemoMode) return;
        if (demoMode) {
            this.isDemoMode = true;
            this.editor.setValue(DEFAULT_TEXT);
        } else {
            this.editor.setValue('');
            this.isDemoMode = false;
        }
    }

    private onRemoteEdits = (edits: PadEdit[]) => {
        this.setDemoMode(false);
        this.applyingRemoteChanges = true;
        const doc = this.editor.getDoc();
        this.editor.operation(() => {
            edits.forEach(edit => {
                const start = doc.posFromIndex(edit.index);
{'fraction_non_alphanumeric': 0.13040692085869912, 'fraction_numerical': 0.0, 'mean_word_length': 2.545144804088586, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '6931108', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1784, 'n_words': 334}