<commit_before>/**
 * Visual editor for HTML editing. Use TinyMCE.
 *
 * @see https://github.com/santakani/santakani.com/wiki/Visual-Editor:-TinyMCE
 */

var Backbone = require('backbone');

module.exports = Backbone.View.extend({

    initialize: function (options) {
        _.extend(this, _.pick(options, 'imageManager'));

        if (!this.$el.attr('id')) {
            this.$el.attr('id', 'content-editor-' + Math.floor(Math.random() * 10000));
        }

        this.selector = '#' + this.$el.attr('id');

        var that = this;

        // Initialize TinyMCE
        tinymce.init({
            selector: this.selector,
            plugins: ['link', 'autolink', 'image', 'paste', 'table', 'fullscreen'],
            menubar: false,
            statusbar: false,
            toolbar: 'undo redo | formatselect bold italic | blockquote bullist numlist table | link unlink customimage | removeformat | fullscreen',

            language: app.locale,

            height: 400,
            min_height: 300,
            max_height: 600,

            content_css: [$('#app-css').attr('href')],

            convert_urls: false, // Keep relative URLs for images and links

            paste_postprocess: function(plugin, args) {
                var $root = $(args.node);

                // Remove <br> DOM
                $root.find('br').remove();

                // Remove empty <p>, <div> DOM
                $root.find('p, div').each(function () {
                    if (!$(this).html().trim()) {
                        $(this).remove();
                    }
                });

                // Remove all data and style attributes
                $root.find('*').each(function () {
                    $(this).removeAttr('style');
                    $(this).removeAttrs(/^data-/);
                    $(this).removeAttrs(/^data-/); // For unknown reason, we need do it twice to remove all data- attributes
                });

                $root.find('table').removeAttr('border').removeAttr('cellpadding').removeAttr('cellspacing');
            },

            setup: function (editor) {
                editor.on('change', function () {
                    editor.save();
                });
                editor.addButton('customimage', {
                    tooltip: 'Insert/edit image',
                    icon: 'image',
                    onclick: function () {
                        that.imageManager.call({
                            multiple: true,
                            done: function (images) {
                                for (var i = 0; i < images.length; i++) {
                                    var image = images[i];
                                    var src = image.fileUrl('medium');
                                    var largeUrl = image.fileUrl('large');
                                    var size = image.size('medium');
                                    var html = '<p><a href="' + largeUrl + '"><img src="'
                                        + src + '" width="' + size.width + '" height="'
                                        + size.height + '"></a></p>';
                                    editor.insertContent(html);
                                }
                            }
                        });
                    }
                });
            }
        });
    }
});
<commit_msg>Remove span tag when copy<commit_after>/**
 * Visual editor for HTML editing. Use TinyMCE.
 *
 * @see https://github.com/santakani/santakani.com/wiki/Visual-Editor:-TinyMCE
 */

var Backbone = require('backbone');

module.exports = Backbone.View.extend({

    initialize: function (options) {
        _.extend(this, _.pick(options, 'imageManager'));

        if (!this.$el.attr('id')) {
            this.$el.attr('id', 'content-editor-' + Math.floor(Math.random() * 10000));
        }

        this.selector = '#' + this.$el.attr('id');

        var that = this;

        // Initialize TinyMCE
        tinymce.init({
            selector: this.selector,
            plugins: ['link', 'autolink', 'image', 'paste', 'table', 'fullscreen'],
            menubar: false,
            statusbar: false,
            toolbar: 'undo redo | formatselect bold italic | blockquote bullist numlist table | link unlink customimage | removeformat | fullscreen',

            language: app.locale,

            height: 400,
            min_height: 300,
            max_height: 600,

            content_css: [$('#app-css').attr('href')],

            convert_urls: false, // Keep relative URLs for images and links

            paste_postprocess: function(plugin, args) {
                var $root = $(args.node);

                // Remove <br> DOM
                $root.find('br').remove();

                // Remove empty <p>, <div> DOM
                $root.find('p, div').each(function () {
                    if (!$(this).html().trim()) {
                        $(this).remove();
                    }
                });

                // Remove <span>
                $root.find('span').each(function () {
                    $(this).before($(this).contents());
                    $(this).remove();
                });

                // Remove all data and style attributes
                $root.find('*').each(function () {
                    $(this).removeAttr('style');
                    $(this).removeAttrs(/^data-/);
                    $(this).removeAttrs(/^data-/); // For unknown reason, we need do it twice to remove all data- attributes
                });

                $root.find('table').removeAttr('border').removeAttr('cellpadding').removeAttr('cellspacing');
            },

            setup: function (editor) {
                editor.on('change', function () {
                    editor.save();
                });
                editor.addButton('customimage', {
                    tooltip: 'Insert/edit image',
                    icon: 'image',
                    onclick: function () {
                        that.imageManager.call({
                            multiple: true,
                            done: function (images) {
                                for (var i = 0; i < images.length; i++) {
                                    var image = images[i];
                                    var src = image.fileUrl('medium');
                                    var largeUrl = image.fileUrl('large');
                                    var size = image.size('medium');
                                    var html = '<p><a href="' + largeUrl + '"><img src="'
                                        + src + '" width="' + size.width + '" height="'
                                        + size.height + '"></a></p>';
                                    editor.insertContent(html);
                                }
                            }
                        });
                    }
                });
            }
        });
    }
});

{'fraction_non_alphanumeric': 0.13962155963302753, 'fraction_numerical': 0.0043004587155963305, 'mean_word_length': 1.1573902288188003, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3943692', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1623, 'n_words': 367}