<commit_before>import "./codemirror-asciidoc";

class jfMarkupEditorController {
	/* @ngInject */
    constructor($timeout,$scope, JFrogUIWebWorker) {

        this.$timeout = $timeout;
        this.$scope = $scope;

        this.JFrogUIWebWorker = JFrogUIWebWorker;
        this.checkPreviewers();
        if (this.previewersCount === 2) {
            this.init();
        }
        else {
            this.JFrogUIWebWorker.check().then(()=>{

                this.JFrogUIWebWorker.open();

                this.webworkerOk = true;
                this.init();
            }).catch(() => {
                console.error(`jf-code-mirror: Error: No preview render callback defined and WebWorker is not available.`)
            })
        }

        $scope.$on('$destroy',() => {
            if (this.webworkerOk) this.JFrogUIWebWorker.close();
        })

    }

    init() {
        this.mode = this.mode || 'Edit';
        this.markup = this.markup || '';
        this.language = this.language || 'Markdown';
        if (this.editable === undefined) this.editable = true;
        this.modeOptions = ['Edit', 'Preview'];

        this.updatePreviewButton();

        this.$scope.$watch('jfMarkup.markup',()=>{
            if (this.webworkerOk && (!this.previewRenderers || !this.previewRenderers[this.language.toLowerCase()])) this.renderPreview();
        })
    }

    setPreview(preview) {
        this.preview = preview;
    }

    renderPreview() {

        if (this.language.toLowerCase() === 'plain text') {
            this.setPreview(this.markup.replace(/\n/g,'<br>'));
        }
        else if (this.previewRenderers && this.previewRenderers[this.language.toLowerCase()]) {
            this.previewRenderers[this.language.toLowerCase()](this.markup, (preview)=>this.setPreview(preview));
        }
        else if (this.webworkerOk) {
            this.JFrogUIWebWorker.markupPreview(this.language.toLowerCase(), this.markup)
                .then(html => this.setPreview(html))
        }
    }

    updatePreviewButton() {
        this.modeOptions = ['Edit', 'Preview'];
    }
    onLanguageChange() {
        this.updatePreviewButton();
        this.preview = '';
        if (this.webworkerOk && (!this.previewRenderers || !this.previewRenderers[this.language.toLowerCase()])) this.renderPreview();
        this.$timeout(()=>this.switchController.updateOptionObjects());
    }
    onChangeModeInternal() {
        if (this.mode === 'Preview') this.renderPreview();
        this.onModeChange({mode:this.mode})
    }

    checkPreviewers() {
        if (!this.previewRenderers) {
            this.previewRenderers = {};<commit_msg>jf-markup-editor: Add safety check for when no onModeChange callback is specified<commit_after>import "./codemirror-asciidoc";

class jfMarkupEditorController {
	/* @ngInject */
    constructor($timeout,$scope, JFrogUIWebWorker) {

        this.$timeout = $timeout;
        this.$scope = $scope;

        this.JFrogUIWebWorker = JFrogUIWebWorker;
        this.checkPreviewers();
        if (this.previewersCount === 2) {
            this.init();
        }
        else {
            this.JFrogUIWebWorker.check().then(()=>{

                this.JFrogUIWebWorker.open();

                this.webworkerOk = true;
                this.init();
            }).catch(() => {
                console.error(`jf-code-mirror: Error: No preview render callback defined and WebWorker is not available.`)
            })
        }

        $scope.$on('$destroy',() => {
            if (this.webworkerOk) this.JFrogUIWebWorker.close();
        })

    }

    init() {
        this.mode = this.mode || 'Edit';
        this.markup = this.markup || '';
        this.language = this.language || 'Markdown';
        if (this.editable === undefined) this.editable = true;
        this.modeOptions = ['Edit', 'Preview'];

        this.updatePreviewButton();

        this.$scope.$watch('jfMarkup.markup',()=>{
            if (this.webworkerOk && (!this.previewRenderers || !this.previewRenderers[this.language.toLowerCase()])) this.renderPreview();
        })
    }

    setPreview(preview) {
        this.preview = preview;
    }

    renderPreview() {

        if (this.language.toLowerCase() === 'plain text') {
            this.setPreview(this.markup.replace(/\n/g,'<br>'));
        }
        else if (this.previewRenderers && this.previewRenderers[this.language.toLowerCase()]) {
            this.previewRenderers[this.language.toLowerCase()](this.markup, (preview)=>this.setPreview(preview));
        }
        else if (this.webworkerOk) {
            this.JFrogUIWebWorker.markupPreview(this.language.toLowerCase(), this.markup)
                .then(html => this.setPreview(html))
        }
    }

    updatePreviewButton() {
        this.modeOptions = ['Edit', 'Preview'];
    }
    onLanguageChange() {
        this.updatePreviewButton();
        this.preview = '';
        if (this.webworkerOk && (!this.previewRenderers || !this.previewRenderers[this.language.toLowerCase()])) this.renderPreview();
        this.$timeout(()=>this.switchController.updateOptionObjects());
    }
    onChangeModeInternal() {
        if (this.mode === 'Preview') this.renderPreview();
        if (this.onModeChange) this.onModeChange({mode:this.mode})
    }

    checkPreviewers() {
        if (!this.previewRenderers) {
            this.previewRenderers = {};
{'fraction_non_alphanumeric': 0.1522426949562628, 'fraction_numerical': 0.0003722315280104225, 'mean_word_length': 2.3091133004926108, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '974011', 'n_tokens_mistral': 1560, 'n_tokens_neox': 1567, 'n_words': 232}