Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Do not save dirty changes automatically when save plugin is present #337

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/tinymce.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ angular.module('ui.tinymce', [])
var debouncedUpdate = (function(debouncedUpdateDelay) {
var debouncedUpdateTimer;
return function(ed) {
var tinymcePlugins = tinyInstance.settings.plugins ? tinyInstance.settings.plugins.split(' ') : [];
$timeout.cancel(debouncedUpdateTimer);
debouncedUpdateTimer = $timeout(function() {
return (function(ed) {
if (ed.isDirty()) {
ed.save();
if (tinymcePlugins.indexOf('save') === -1) {
ed.save();
}
updateView(ed);
}
})(ed);
Expand Down
1 change: 1 addition & 0 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function (config) {
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/tinymce/tinymce.min.js',
'bower_components/tinymce/plugins/save/plugin.min.js',
'src/tinymce.js',
'test/*.spec.js',
{pattern: 'bower_components/tinymce/themes/**', included: false},
Expand Down
46 changes: 46 additions & 0 deletions test/tinymce.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,50 @@ describe('uiTinymce', function () {
expect(id2).not.toEqual(id3);
expect(id3).not.toEqual(id4);
});

it('does not automatically save the dirty changes when save plugin is present', function () {
// Given a tinymce editor is present on a page with a plugin to allow saving externally
scope.options = {
plugins: 'save',
setup: function (ed) {}
};
spyOn(scope.options, 'setup').and.callThrough();
element = $compile('<form><textarea ui-tinymce="options" data-ng-model="foo_1"</textarea></form>')(scope);
angular.element(document.getElementsByTagName('body')[0]).append(element);
scope.$apply();
$timeout.flush();

// When there is a change event on editor
var editor = scope.options.setup.calls.allArgs()[0][0];
spyOn(editor, 'isDirty').and.returnValue(true);
spyOn(editor, 'save');
$timeout.flush(); // This will ensure that debouncedUpdateTimer timer function is executed which was setup on 'change' event

// Then I see that content is not automatically saved when content is dirty
expect(editor.isDirty).toHaveBeenCalled();
expect(editor.save).not.toHaveBeenCalled();
});

it('automatically saves the dirty changes when save plugin is not present', function () {
// Given a tinymce editor is present on a page without a 'save' plugin
scope.options = {
plugins: '',
setup: function (ed) {}
};
spyOn(scope.options, 'setup').and.callThrough();
element = $compile('<form><textarea ui-tinymce="options" data-ng-model="foo_1"</textarea></form>')(scope);
angular.element(document.getElementsByTagName('body')[0]).append(element);
scope.$apply();
$timeout.flush();

// When there is a change event on editor
var editor = scope.options.setup.calls.allArgs()[0][0];
spyOn(editor, 'isDirty').and.returnValue(true);
spyOn(editor, 'save');
$timeout.flush(); // This will ensure that debouncedUpdateTimer timer function is executed which was setup on 'change' event

// Then I see that content is automatically saved when content is dirty
expect(editor.isDirty).toHaveBeenCalled();
expect(editor.save).toHaveBeenCalled();
});
});