<commit_before>angular.module("umbraco")
    .controller("Umbraco.PropertyEditors.GridController",
    function ($scope, $http, assetsService, $rootScope, dialogService, gridService, mediaResource, imageHelper, $timeout, umbRequestHelper) {

        // Grid status variables
        $scope.currentRow = null;
        $scope.currentCell = null;
        $scope.currentToolsControl = null;
        $scope.currentControl = null;
        $scope.openRTEToolbarId = null;
        $scope.hasSettings = false;

        // *********************************************
        // Sortable options
        // *********************************************

        $scope.sortableOptions = {
            distance: 10,
            cursor: "move",
            placeholder: 'ui-sortable-placeholder',
            handle: '.cell-tools-move',
            forcePlaceholderSize: true,
            tolerance: "pointer",
            zIndex: 999999999999999999,
            scrollSensitivity: 100,
            cursorAt: {
                top: 45,
                left: 90
            },

            sort: function (event, ui) {
                /* prevent vertical scroll out of the screen */
                var max = $(".usky-grid").width() - 150;
                if (parseInt(ui.helper.css('left')) > max) {
                    ui.helper.css({ 'left': max + "px" })
                }
                if (parseInt(ui.helper.css('left')) < 20) {
                    ui.helper.css({ 'left': 20 })
                }
            },

            start: function (e, ui) {
                ui.item.find('.mceNoEditor').each(function () {
                    tinyMCE.execCommand('mceRemoveEditor', false, $(this).attr('id'));
                });
            },

            stop: function (e, ui) {
                ui.item.parents(".usky-column").find('.mceNoEditor').each(function () {
                    tinyMCE.execCommand('mceRemoveEditor', false, $(this).attr('id'));
                    tinyMCE.execCommand('mceAddEditor', false, $(this).attr('id'));
                });
            }
        };

        var notIncludedRte = [];
        var cancelMove = false;

        $scope.sortableOptionsCell = {
            distance: 10,
            cursor: "move",
            placeholder: "ui-sortable-placeholder",
            handle: '.cell-tools-move',
            connectWith: ".usky-cell",
            forcePlaceholderSize: true,
            tolerance: "pointer",
            zIndex: 999999999999999999,
            scrollSensitivity: 100,
            cursorAt: {
                top: 45,
                left: 90
            },

            sort: function (event, ui) {
                /* prevent vertical scroll out of the screen */
                var position = parseInt(ui.item.parent().offset().left) + parseInt(ui.helper.css('left')) - parseInt($(".usky-grid").offset().left);
                var max = $(".usky-grid").width() - 220;
                if (position > max) {
                    ui.helper.css({ 'left': max - parseInt(ui.item.parent().offset().left) + parseInt($(".usky-grid").offset().left) + "px" })
                }
                if (position < 0) {
                    ui.helper.css({ 'left': 0 - parseInt(ui.item.parent().offset().left) + parseInt($(".usky-grid").offset().left) + "px" })
                }
            },

            over: function (event, ui) {
                allowedEditors = $(event.target).scope().area.allowed;

                if ($.inArray(ui.item.scope().control.editor.alias, allowedEditors) < 0 && allowedEditors) {
                    ui.placeholder.hide();
                    cancelMove = true;
                }
                else {
                    ui.placeholder.show();
                    cancelMove = false;
                }

            },

            update: function (event, ui) {
                if (!ui.sender) {
                    if (cancelMove) {
                        ui.item.sortable.cancel();
                    }
                    ui.item.parents(".usky-cell").find('.mceNoEditor').each(function () {
                        if ($.inArray($(this).attr('id'), notIncludedRte) < 0) {
                            notIncludedRte.splice(0, 0, $(this).attr('id'));
                        }
                    });
                }
                else {
                    $(event.target).find('.mceNoEditor').each(function () {
                        if ($.inArray($(this).attr('id'), notIncludedRte) < 0) {
                            notIncludedRte.splice(0, 0, $(this).attr('id'));
                        }
                    });
                }

            },

            start: function (e, ui) {
                ui.item.find('.mceNoEditor').each(function () {
                    notIncludedRte = [];
                    tinyMCE.execCommand('mceRemoveEditor', false, $(this).attr('id'));
                });
            },

            stop: function (e, ui) {
                ui.item.parents(".usky-cell").find('.mceNoEditor').each(function () {
                    if ($.inArray($(this).attr('id'), notIncludedRte) < 0) {
                        notIncludedRte.splice(0, 0, $(this).attr('id'));
                    }
                });
                $timeout(function () {
                    _.forEach(notIncludedRte, function (id) {
                        tinyMCE.execCommand('mceRemoveEditor', false, id);
                        tinyMCE.execCommand('mceAddEditor', false, id);
                        console.info("stop " + id);
                    });<commit_msg>U4-6593 - After sort, reset the TinyMCE editors with previous settings<commit_after>angular.module("umbraco")
    .controller("Umbraco.PropertyEditors.GridController",
    function ($scope, $http, assetsService, $rootScope, dialogService, gridService, mediaResource, imageHelper, $timeout, umbRequestHelper) {

        // Grid status variables
        $scope.currentRow = null;
        $scope.currentCell = null;
        $scope.currentToolsControl = null;
        $scope.currentControl = null;
        $scope.openRTEToolbarId = null;
        $scope.hasSettings = false;

        // *********************************************
        // Sortable options
        // *********************************************

        var draggedRteSettings;

        $scope.sortableOptions = {
            distance: 10,
            cursor: "move",
            placeholder: 'ui-sortable-placeholder',
            handle: '.cell-tools-move',
            forcePlaceholderSize: true,
            tolerance: "pointer",
            zIndex: 999999999999999999,
            scrollSensitivity: 100,
            cursorAt: {
                top: 45,
                left: 90
            },

            sort: function (event, ui) {
                /* prevent vertical scroll out of the screen */
                var max = $(".usky-grid").width() - 150;
                if (parseInt(ui.helper.css('left')) > max) {
                    ui.helper.css({ 'left': max + "px" })
                }
                if (parseInt(ui.helper.css('left')) < 20) {
                    ui.helper.css({ 'left': 20 })
                }
            },

            start: function (e, ui) {
                draggedRteSettings = {};
                ui.item.find('.mceNoEditor').each(function () {
                    // remove all RTEs in the dragged row and save their settings
                    var id = $(this).attr('id');
                    draggedRteSettings[id] = _.findWhere(tinyMCE.editors, { id: id }).settings;
                    tinyMCE.execCommand('mceRemoveEditor', false, id);
                });
            },

            stop: function (e, ui) {
                // reset all RTEs affected by the dragging
                ui.item.parents(".usky-column").find('.mceNoEditor').each(function () {
                    var id = $(this).attr('id');
                    draggedRteSettings[id] = draggedRteSettings[id] || _.findWhere(tinyMCE.editors, { id: id }).settings;
                    tinyMCE.execCommand('mceRemoveEditor', false, id);
                    tinyMCE.init(draggedRteSettings[id]);
                });
            }
        };

        var notIncludedRte = [];
        var cancelMove = false;

        $scope.sortableOptionsCell = {
            distance: 10,
            cursor: "move",
            placeholder: "ui-sortable-placeholder",
            handle: '.cell-tools-move',
            connectWith: ".usky-cell",
            forcePlaceholderSize: true,
            tolerance: "pointer",
            zIndex: 999999999999999999,
            scrollSensitivity: 100,
            cursorAt: {
                top: 45,
                left: 90
            },

            sort: function (event, ui) {
                /* prevent vertical scroll out of the screen */
                var position = parseInt(ui.item.parent().offset().left) + parseInt(ui.helper.css('left')) - parseInt($(".usky-grid").offset().left);
                var max = $(".usky-grid").width() - 220;
                if (position > max) {
                    ui.helper.css({ 'left': max - parseInt(ui.item.parent().offset().left) + parseInt($(".usky-grid").offset().left) + "px" })
                }
                if (position < 0) {
                    ui.helper.css({ 'left': 0 - parseInt(ui.item.parent().offset().left) + parseInt($(".usky-grid").offset().left) + "px" })
                }
            },

            over: function (event, ui) {
                allowedEditors = $(event.target).scope().area.allowed;

                if ($.inArray(ui.item.scope().control.editor.alias, allowedEditors) < 0 && allowedEditors) {
                    ui.placeholder.hide();
                    cancelMove = true;
                }
                else {
                    ui.placeholder.show();
                    cancelMove = false;
                }

            },

            update: function (event, ui) {
                // add all RTEs which are affected by the dragging
                if (!ui.sender) {
                    if (cancelMove) {
                        ui.item.sortable.cancel();
                    }
                    ui.item.parents(".usky-cell").find('.mceNoEditor').each(function () {
                        if ($.inArray($(this).attr('id'), notIncludedRte) < 0) {
                            notIncludedRte.splice(0, 0, $(this).attr('id'));
                        }
                    });
                }
                else {
                    $(event.target).find('.mceNoEditor').each(function () {
                        if ($.inArray($(this).attr('id'), notIncludedRte) < 0) {
                            notIncludedRte.splice(0, 0, $(this).attr('id'));
                        }
                    });
                }

            },

            start: function (e, ui) {
                ui.item.find('.mceNoEditor').each(function () {
                    notIncludedRte = [];

                    // save the dragged RTE settings
                    draggedRteSettings = _.findWhere(tinyMCE.editors, { id: $(this).attr('id') }).settings;

                    // remove the dragged RTE
                    tinyMCE.execCommand('mceRemoveEditor', false, $(this).attr('id'));
                });
            },

            stop: function (e, ui) {
                ui.item.parents(".usky-cell").find('.mceNoEditor').each(function () {
                    if ($.inArray($(this).attr('id'), notIncludedRte) < 0) {
                        // add all dragged's neighbouring RTEs in the new cell
                        notIncludedRte.splice(0, 0, $(this).attr('id'));
                    }
                });
                $timeout(function () {
                    // reconstruct the dragged RTE
                    tinyMCE.init(draggedRteSettings);

                    _.forEach(notIncludedRte, function (id) {
                        // reset all the other RTEs
                        if (id != draggedRteSettings.id) {
                            var rteSettings = _.findWhere(tinyMCE.editors, { id: id }).settings;
                            tinyMCE.execCommand('mceRemoveEditor', false, id);
                            tinyMCE.init(rteSettings);
                        }
                    });
{'fraction_non_alphanumeric': 0.14978799739073712, 'fraction_numerical': 0.012801696020874102, 'mean_word_length': 1.3128417876673582, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '261149', 'n_tokens_mistral': 3379, 'n_tokens_neox': 3065, 'n_words': 578}