From d94067e26c3b6eb465127ead914f67b2b60bbd24 Mon Sep 17 00:00:00 2001
From: Shweta Bansal <ershwetabansal@energyaspects.com>
Date: Sat, 17 Jun 2017 23:27:58 +0100
Subject: [PATCH 1/4] Add tests for ensuring that autosaving does not happen
 when save plugin is present

---
 test/tinymce.spec.js | 46 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/test/tinymce.spec.js b/test/tinymce.spec.js
index 5779cfd..4d1e75c 100644
--- a/test/tinymce.spec.js
+++ b/test/tinymce.spec.js
@@ -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();
+  })
 });

From 1ddfe492f9e2bc21cd780e677a90ccd9d3f8cd75 Mon Sep 17 00:00:00 2001
From: Shweta Bansal <ershwetabansal@energyaspects.com>
Date: Sat, 17 Jun 2017 23:28:13 +0100
Subject: [PATCH 2/4] Load save plugin in karma

---
 test/karma.conf.js | 1 +
 1 file changed, 1 insertion(+)

diff --git a/test/karma.conf.js b/test/karma.conf.js
index ca24edd..293101e 100644
--- a/test/karma.conf.js
+++ b/test/karma.conf.js
@@ -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},

From a2e6329f02ec8189d62213b95bfa6efd986817ca Mon Sep 17 00:00:00 2001
From: Shweta Bansal <ershwetabansal@energyaspects.com>
Date: Sat, 17 Jun 2017 23:28:27 +0100
Subject: [PATCH 3/4] Make the tests green

---
 src/tinymce.js | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/tinymce.js b/src/tinymce.js
index c0291af..88fb0b2 100644
--- a/src/tinymce.js
+++ b/src/tinymce.js
@@ -62,11 +62,16 @@ 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);

From 7855b1bc77f6a2aa9096c5cd9afb37c11df9cecd Mon Sep 17 00:00:00 2001
From: Shweta Bansal <ershwetabansal@energyaspects.com>
Date: Sat, 17 Jun 2017 23:56:03 +0100
Subject: [PATCH 4/4] Fix styling issues

---
 src/tinymce.js       | 4 +---
 test/tinymce.spec.js | 6 +++---
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/src/tinymce.js b/src/tinymce.js
index 88fb0b2..44e3293 100644
--- a/src/tinymce.js
+++ b/src/tinymce.js
@@ -62,9 +62,7 @@ angular.module('ui.tinymce', [])
         var debouncedUpdate = (function(debouncedUpdateDelay) {
           var debouncedUpdateTimer;
           return function(ed) {
-            var tinymcePlugins = tinyInstance.settings.plugins
-              ? tinyInstance.settings.plugins.split(' ')
-              : []
+            var tinymcePlugins = tinyInstance.settings.plugins ? tinyInstance.settings.plugins.split(' ') : [];
 	        $timeout.cancel(debouncedUpdateTimer);
 	         debouncedUpdateTimer = $timeout(function() {
               return (function(ed) {
diff --git a/test/tinymce.spec.js b/test/tinymce.spec.js
index 4d1e75c..f28e456 100644
--- a/test/tinymce.spec.js
+++ b/test/tinymce.spec.js
@@ -188,7 +188,7 @@ describe('uiTinymce', function () {
       plugins: 'save',
       setup: function (ed) {}
     };
-    spyOn(scope.options, 'setup').and.callThrough()
+    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();
@@ -211,7 +211,7 @@ describe('uiTinymce', function () {
       plugins: '',
       setup: function (ed) {}
     };
-    spyOn(scope.options, 'setup').and.callThrough()
+    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();
@@ -226,5 +226,5 @@ describe('uiTinymce', function () {
     // Then I see that content is automatically saved when content is dirty
     expect(editor.isDirty).toHaveBeenCalled();
     expect(editor.save).toHaveBeenCalled();
-  })
+  });
 });