<commit_before>'use strict';
var ko = require('knockout');
var $ = require('jquery');
var $osf = require('js/osfHelpers');
var Raven = require('raven-js');
var Markdown = require('pagedown-ace-converter');
Markdown.getSanitizingConverter = require('pagedown-ace-sanitizer').getSanitizingConverter;
require('imports?Markdown=pagedown-ace-converter!pagedown-ace-editor');

var waterbutler = require('js/waterbutler');


/**
 * Binding handler that instantiates an ACE editor.
 * The value accessor must be a ko.observable.
 * Example: <div data-bind="ace: currentText" id="editor"></div>
 */
var editor;

ko.bindingHandlers.ace = {
    init: function (element, valueAccessor) {
        editor = ace.edit(element.id);  // jshint ignore: line
        editor.renderer.setShowGutter(true);
        editor.renderer.setOption('showLineNumbers', true);

        editor.getPosition = function(x, y) {

            var config = editor.renderer.$markerFront.config;
            var height = config.lineHeight;
            var width = config.characterWidth;
            var row = Math.floor(y/height) < editor.session.getScreenLength() ? Math.floor(y/height) : editor.session.getScreenLength() - 1;
            var column = Math.floor(x/width) < editor.session.getScreenLastRowColumn(row) ? Math.floor(x/width) : editor.session.getScreenLastRowColumn(row);
            return {row: row, column: column}

        };
        // Updates the view model based on changes to the editor
        editor.getSession().on('change', function () {
            valueAccessor()(editor.getValue());
        });

        editor.marker = {};
        editor.marker.cursor = {};
        editor.marker.active = false;
        editor.marker.update = function(html, markerLayer, session, config) {
            var height = config.lineHeight;
            var width = config.characterWidth;
            var top = markerLayer.$getTop(this.cursor.row, config);
            var left = markerLayer.$padding + this.cursor.column * width;
            html.push(
                '<div class=\'drag-drop-cursor\' style=\'',
                'height:', height, 'px;',
                'top:', top, 'px;',
                'left:', left, 'px; width:', width, 'px\'></div>'
            );
        };

        editor.marker.redraw = function() {
            this.session._signal("changeFrontMarker");
        };

        element.addEventListener('dragenter', function(event) {
            event.preventDefault();
            event.stopPropagation();
        }, false);

        element.addEventListener('dragover', function(event) {
            event.preventDefault();
            event.stopPropagation();
            if (!editor.marker.active) {
                editor.marker.active = true;
                editor.marker.session = editor.session;
                editor.marker.session.addDynamicMarker(editor.marker, true);
            }
            editor.marker.cursor = editor.getPosition(event.offsetX, event.offsetY);
            editor.marker.redraw();
            var effect;
                try {
                  effect = event.dataTransfer.effectAllowed;
                } catch (_error) {}
                event.dataTransfer.dropEffect = 'move' === effect || 'linkMove' === effect ? 'move' : 'copy';
        }, false);

        element.addEventListener('drop', function(event) {
            event.preventDefault();
            event.stopPropagation();
            var re = /(?:\.([^.]+))?$/;
            var extensions = ['jpg', 'png', 'gif', 'bmp'];
            var ext;
            var position = editor.session.screenToDocumentPosition(editor.marker.cursor.row, editor.marker.cursor.column);
            var url = event.dataTransfer.getData('text/html');
            if (!!url) {
                var getImage = /(src=")(.*?)(")/;
                var imgURL = getImage.exec(url)[2];
                if (imgURL.substring(0,10) === 'data:image') {
                    imgURL = event.dataTransfer.getData('URL');
                    var exp = /(imgurl=)(.*?)(&)/;
                    if (!!exp.exec(imgURL)) {
                        imgURL = exp.exec(imgURL)[2];
                    }
                    else {
                        //alert('cant do this');
                        $osf.growl('Error', 'Please find a prettier URL to give us or download to your computer and drop it in from there');
                        imgURL = undefined;
                    }

                }
                else {
                    ext = re.exec(imgURL)[1];
                    if (extensions.indexOf(ext) <= -1) {
                        $osf.growl('Error', 'File type not supported', 'danger');
                        imgURL = undefined;
                    }
                }
                if (!!imgURL) {
                    var refOut = editor.marker.addLinkDef('[999]: ' + imgURL);
                    editor.session.insert(position, '![enter image description here][' + refOut + ']');
                }
            }
            else {
                var file = event.dataTransfer.files[0];
                ext = re.exec(file.name)[1];
                if (extensions.indexOf(ext) <= -1) {
                    $osf.growl('Error', 'File type not supported', 'danger');
                }
                else {
                    var waterbutler_url = waterbutler.buildUploadUrl('/', 'osfstorage', window.contextVars.node.id, file);
                    $.ajax({
                        url: waterbutler_url,
                        type: 'PUT',
                        processData: false,
                        contentType: false,
                        beforeSend: $osf.setXHRAuthorization,
                        data: file
                    }).done(function(data) {
                        //url = waterbutler.buildDownloadUrl(data.path, data.provider, window.contextVars.node.id, {mode: 'render'});
                        url = window.contextVars.waterbutlerURL + 'v1/resources/' + window.contextVars.node.id + '/providers/' + data.provider + data.path + '?mode=render';
                        var refOut = editor.marker.addLinkDef('[999]: ' + url);
                        editor.session.insert(position, '![enter image description here][' + refOut + ']');
                    }).fail(function(data) {
                        $osf.growl('Error', 'File not uploaded. Please refresh the page and try ' +
                        'again or contact <a href="mailto: <EMAIL>"><EMAIL></a> ' +
                        'if the problem persists.', 'danger');
                    });
                }
            }
            editor.marker.session.removeMarker(editor.marker.id);
            editor.marker.redraw();
            editor.marker.active = false;
        }, false);<commit_msg>Added ability to upload multiple files and changed the error message for Data URI<commit_after>'use strict';
var ko = require('knockout');
var $ = require('jquery');
var $osf = require('js/osfHelpers');
var Raven = require('raven-js');
var Markdown = require('pagedown-ace-converter');
Markdown.getSanitizingConverter = require('pagedown-ace-sanitizer').getSanitizingConverter;
require('imports?Markdown=pagedown-ace-converter!pagedown-ace-editor');

var waterbutler = require('js/waterbutler');


/**
 * Binding handler that instantiates an ACE editor.
 * The value accessor must be a ko.observable.
 * Example: <div data-bind="ace: currentText" id="editor"></div>
 */
var editor;

ko.bindingHandlers.ace = {
    init: function (element, valueAccessor) {
        editor = ace.edit(element.id);  // jshint ignore: line
        editor.renderer.setShowGutter(true);
        editor.renderer.setOption('showLineNumbers', true);

        editor.getPosition = function(x, y) {

            var config = editor.renderer.$markerFront.config;
            var height = config.lineHeight;
            var width = config.characterWidth;
            var row = Math.floor(y/height) < editor.session.getScreenLength() ? Math.floor(y/height) : editor.session.getScreenLength() - 1;
            var column = Math.floor(x/width) < editor.session.getScreenLastRowColumn(row) ? Math.floor(x/width) : editor.session.getScreenLastRowColumn(row);
            return {row: row, column: column}

        };
        // Updates the view model based on changes to the editor
        editor.getSession().on('change', function () {
            valueAccessor()(editor.getValue());
        });

        editor.marker = {};
        editor.marker.cursor = {};
        editor.marker.active = false;
        editor.marker.update = function(html, markerLayer, session, config) {
            var height = config.lineHeight;
            var width = config.characterWidth;
            var top = markerLayer.$getTop(this.cursor.row, config);
            var left = markerLayer.$padding + this.cursor.column * width;
            html.push(
                '<div class=\'drag-drop-cursor\' style=\'',
                'height:', height, 'px;',
                'top:', top, 'px;',
                'left:', left, 'px; width:', width, 'px\'></div>'
            );
        };

        editor.marker.redraw = function() {
            this.session._signal("changeFrontMarker");
        };

        element.addEventListener('dragenter', function(event) {
            event.preventDefault();
            event.stopPropagation();
        }, false);

        element.addEventListener('dragover', function(event) {
            event.preventDefault();
            event.stopPropagation();
            if (!editor.marker.active) {
                editor.marker.active = true;
                editor.marker.session = editor.session;
                editor.marker.session.addDynamicMarker(editor.marker, true);
            }
            editor.marker.cursor = editor.getPosition(event.offsetX, event.offsetY);
            editor.marker.redraw();
            var effect;
                try {
                  effect = event.dataTransfer.effectAllowed;
                } catch (_error) {}
                event.dataTransfer.dropEffect = 'move' === effect || 'linkMove' === effect ? 'move' : 'copy';
        }, false);

        element.addEventListener('drop', function(event) {
            event.preventDefault();
            event.stopPropagation();
            var re = /(?:\.([^.]+))?$/;
            var extensions = ['jpg', 'png', 'gif', 'bmp'];
            var ext;
            var position = editor.session.screenToDocumentPosition(editor.marker.cursor.row, editor.marker.cursor.column);
            var url = event.dataTransfer.getData('text/html');
            if (!!url) {
                var getImage = /(src=")(.*?)(")/;
                var imgURL = getImage.exec(url)[2];
                if (imgURL.substring(0,10) === 'data:image') {
                    imgURL = event.dataTransfer.getData('URL');
                    var exp = /(imgurl=)(.*?)(&)/;
                    if (!!exp.exec(imgURL)) {
                        imgURL = exp.exec(imgURL)[2];
                    }
                    else {
                        //alert('cant do this');
                        $osf.growl('Error', 'Unable to handle this type of link.  Please either find another link or save the image to your computer and import it from there.');
                        imgURL = undefined;
                    }

                }
                else {
                    ext = re.exec(imgURL)[1];
                    if (extensions.indexOf(ext) <= -1) {
                        $osf.growl('Error', 'File type not supported', 'danger');
                        imgURL = undefined;
                    }
                }
                if (!!imgURL) {
                    var refOut = editor.marker.addLinkDef('[999]: ' + imgURL);
                    editor.session.insert(position, '![enter image description here][' + refOut + ']');
                }
            }
            else {
                for (var i = 0, file; file = event.dataTransfer.files[i]; i ++) {
                    ext = re.exec(file.name)[1];
                    if (extensions.indexOf(ext) <= -1) {
                        $osf.growl('Error', 'File type not supported', 'danger');
                    }
                    else {
                        var waterbutler_url = waterbutler.buildUploadUrl('/', 'osfstorage', window.contextVars.node.id, file);
                        $.ajax({
                            url: waterbutler_url,
                            type: 'PUT',
                            processData: false,
                            contentType: false,
                            beforeSend: $osf.setXHRAuthorization,
                            data: file
                        }).done(function(data) {
                            //url = waterbutler.buildDownloadUrl(data.path, data.provider, window.contextVars.node.id, {mode: 'render'});
                            url = window.contextVars.waterbutlerURL + 'v1/resources/' + window.contextVars.node.id + '/providers/' + data.provider + data.path + '?mode=render';
                            var refOut = editor.marker.addLinkDef('[999]: ' + url);
                            editor.session.insert(position, '![enter image description here][' + refOut + ']');
                        }).fail(function(data) {
                            $osf.growl('Error', 'File not uploaded. Please refresh the page and try ' +
                            'again or contact <a href="mailto: <EMAIL>"><EMAIL></a> ' +
                            'if the problem persists.', 'danger');
                        });
                    }
                }
            }
            editor.marker.session.removeMarker(editor.marker.id);
            editor.marker.redraw();
            editor.marker.active = false;
        }, false);
{'fraction_non_alphanumeric': 0.13447148565201597, 'fraction_numerical': 0.0026153287322920452, 'mean_word_length': 1.6366596437464087, 'pattern_counts': {'":': 0, '<': 27, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2966017', 'n_tokens_mistral': 3650, 'n_tokens_neox': 3523, 'n_words': 738}