Atlas91
Atlas91

Reputation: 5904

TinyMCE setContent does not clear undo history

I'm using angular tinyMCE directive in a form. After submit I use this command to clear the textarea:

tinyMCE.activeEditor.setContent('');

and also this: $scope.tinymceModel =''; to clear the model. My form is inside a modal. When I open the modal again the textarea seems clear but the "undo" button in TinyMCE is active and if I click on it I can see the old content. Is it possible reset and clear the TinyMCE undo history? This is the html part:

<textarea id="elm1" ui-tinymce="tinymceOptions" ng-model="tinymceModel" auto-focus id="elm1" rows="8" cols="100"></textarea>

and this is the options

$scope.tinymceModel = '';
        $scope.tinymceOptions = {
            height: 420,
            language:'it',
            theme: "modern",
            plugins: [
                "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
                "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
                "save table contextmenu directionality emoticons template paste textcolor"
            ],
            content_css: "css/partials/content.css",
            toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage",
            style_formats: [
                {title: 'Bold text', inline: 'b'},
                {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
                {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
                {title: 'Example 1', inline: 'span', classes: 'example1'},
                {title: 'Example 2', inline: 'span', classes: 'example2'},
                {title: 'Table styles'},
                {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
            ],
            setup : function(ed){
                ed.on('keyup', function(e) {
                    get_ed_content = tinymce.activeEditor.getContent();
                    if(get_ed_content != '' && get_ed_content != undefined && get_ed_content != null) {
                        $scope.tinyHasContent = true;
                    } else {
                        $scope.tinyHasContent = false;
                    }
                });
                ed.on('init', function()
                {
                    this.getDoc().body.style.fontSize = '1.2em';
                });

            }
        };

thanks

Upvotes: 2

Views: 2288

Answers (1)

Michael Fromin
Michael Fromin

Reputation: 13726

After you set your new content add a call to undoManager.clear(). Something like this:

tinymce.activeEditor.setContent('');
tinymce.activeEditor.undoManager.clear()

This will clear the undo history for the active editor so you can't go back to the prior content.

Upvotes: 9

Related Questions