Source fixes: avoid race and wait a minute after uploading to prevent unnecessary...
[redakcja.git] / src / redakcja / static / js / wiki / view_editor_wysiwyg.js
index f39f7a8..decc733 100644 (file)
                                 $origin.html($(element).html());
                             }
                             $overlay.remove();
+                            $.wiki.activePerspective().flush();
                         },
                         error: function(text){
                             alert('Błąd! ' + text);
                 self.caret = new Caret(element);
 
                 $('#insert-reference-button').click(function(){
+                    self.flush();
                     self.addReference();
                     return false;
                 });
 
                 $('#insert-annotation-button').click(function(){
+                    self.flush();
                     addAnnotation();
                     return false;
                 });
 
                 $('#insert-theme-button').click(function(){
+                    self.flush();
                     addTheme();
                     return false;
                 });
 
                 $(".insert-inline-tag").click(function() {
+                    self.flush();
                     self.insertInlineTag($(this).attr('data-tag'));
                     return false;
                 });
 
                 $(".insert-char").click(function() {
+                    self.flush();
                     addSymbol(caret=self.caret);
                     return false;
                 });
 
                 $(document).on('click', '.edit-button', function(event){
+                    self.flush();
                     event.preventDefault();
                     openForEdit($(this).parent());
                 });
 
                 $(document).on('click', '.uwaga-button', function(event){
+                    self.flush();
                     event.preventDefault();
                     createUwagaBefore($(this).parent());
                 });
             });
 
             element.on('click', '.annotation', function(event) {
+                self.flush();
                 event.preventDefault();
                 event.redakcja_caret_ignore = true;
                 $('[x-annotation-box]', $(this).parent()).toggleClass('editing');
 
                     var htmlView = $('#html-view');
                     htmlView.html(element);
+                    if ('PropertiesPerspective' in $.wiki.perspectives)
+                        $.wiki.perspectives.PropertiesPerspective.enable();
 
                     _finalize(success);
                 },
                     _finalize(failure);
                 }
             });
-        };
+        }
+
+        flush() {
+            let self = this;
+            return new Promise((resolve, reject) => {
+                if ($('#html-view .error').length > 0) {
+                    reject()
+                } else {
+                    //return _finalize(failure);
+                    html2text({
+                        element: $('#html-view').get(0),
+                        stripOuter: true,
+                        success: (text) => {
+                            self.doc.setText(text);
+                            resolve();
+                        },
+                        error: (text) => {
+                            reject(text);
+                            //$('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
+                        }
+                    });
+                }
+            });
+        }
 
         onExit(success, failure) {
             var self = this;
 
             self.caret.detach();
 
-            $.wiki.exitTab('#PropertiesPerspective');
-
-            $.blockUI({
-                message: 'Zapisywanie widoku...'
-            });
-
-            function _finalize(callback){
-                $.unblockUI();
-                if (callback)
-                    callback();
-            }
-
-            if ($('#html-view .error').length > 0)
-                return _finalize(failure);
+            if ('PropertiesPerspective' in $.wiki.perspectives)
+                $.wiki.perspectives.PropertiesPerspective.disable();
 
-            html2text({
-                element: $('#html-view').get(0),
-                stripOuter: true,
-                success: function(text){
-                    self.doc.setText(text);
-                    _finalize(success);
-                },
-                error: function(text){
-                    $('#source-editor').html('<p>Wystąpił błąd:</p><pre>' + text + '</pre>');
-                    _finalize(failure);
-                }
+            self.flush().then(() => {
+                success && success();
+            }).catch((e) => {
+                // TODO report
+                console.log('REJECTED!', e);
+                failure && failure();
             });
         };
 
         insertInlineTag(tag) {
             this.caret.detach();
+            let self = this;
 
             let selection = window.getSelection();
             var n = selection.rangeCount;
                         success: function(html) {
                             // What if no end?
                             node.insertBefore($(html)[0], end);
+                            self.flush();
                         }
                     });
                 },