From dc828e0e5afcf4ae45b0de6ee4e380b5783eec87 Mon Sep 17 00:00:00 2001
From: Steffen van Bergerem <svbergerem@omgsrsly.net>
Date: Wed, 9 Aug 2017 22:11:14 +0200
Subject: [PATCH] Prevent publisher from closing in preview mode

Fixes #7245
---
 .../javascripts/app/views/publisher_view.js   |  4 +--
 .../app/views/publisher_view_spec.js          | 25 +++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/app/assets/javascripts/app/views/publisher_view.js b/app/assets/javascripts/app/views/publisher_view.js
index 160a77a95d..211153198e 100644
--- a/app/assets/javascripts/app/views/publisher_view.js
+++ b/app/assets/javascripts/app/views/publisher_view.js
@@ -407,8 +407,8 @@ app.views.Publisher = Backbone.View.extend({
   },
 
   tryClose : function(){
-    // if it is not submittable, close it.
-    if( !this._submittable() ){
+    // if it is not submittable and not in preview mode, close it.
+    if (!this._submittable() && !this.markdownEditor.isPreviewMode()) {
       this.close();
     }
   },
diff --git a/spec/javascripts/app/views/publisher_view_spec.js b/spec/javascripts/app/views/publisher_view_spec.js
index 8d02015ea5..e286c3cf6d 100644
--- a/spec/javascripts/app/views/publisher_view_spec.js
+++ b/spec/javascripts/app/views/publisher_view_spec.js
@@ -252,6 +252,31 @@ describe("app.views.Publisher", function() {
       });
     });
 
+    describe("tryClose", function() {
+      it("doesn't close the publisher if it is submittable", function() {
+        spyOn(this.view, "_submittable").and.returnValue(true);
+        spyOn(this.view, "close");
+        this.view.tryClose();
+        expect(this.view.close).not.toHaveBeenCalled();
+      });
+
+      it("doesn't close the publisher if it is in preview mode", function() {
+        spyOn(this.view, "_submittable").and.returnValue(false);
+        spyOn(this.view.markdownEditor, "isPreviewMode").and.returnValue(true);
+        spyOn(this.view, "close");
+        this.view.tryClose();
+        expect(this.view.close).not.toHaveBeenCalled();
+      });
+
+      it("closes the publisher if it is neither submittable nor in preview mode", function() {
+        spyOn(this.view, "_submittable").and.returnValue(false);
+        spyOn(this.view.markdownEditor, "isPreviewMode").and.returnValue(false);
+        spyOn(this.view, "close");
+        this.view.tryClose();
+        expect(this.view.close).toHaveBeenCalled();
+      });
+    });
+
     describe("_beforeUnload", function(){
       it("calls _submittable", function(){
         spyOn(this.view, "_submittable");
-- 
GitLab