Allow multiple sources.
[redakcja.git] / src / redakcja / static / js / wiki / view_editor_wysiwyg.js
index c8f84e1..decc733 100644 (file)
                                 $origin.html($(element).html());
                             }
                             $overlay.remove();
                                 $origin.html($(element).html());
                             }
                             $overlay.remove();
+                            $.wiki.activePerspective().flush();
                         },
                         error: function(text){
                             alert('Błąd! ' + text);
                         },
                         error: function(text){
                             alert('Błąd! ' + text);
 
     class VisualPerspective extends $.wiki.Perspective {
         constructor(options) {
 
     class VisualPerspective extends $.wiki.Perspective {
         constructor(options) {
-            var old_callback = options.callback;
-
-            options.callback = function(){
-                let self = this;
-                var element = $("#html-view");
-                var button = $('<button class="edit-button active-block-button">Edytuj</button>');
-                var uwagaButton = $('<button class="uwaga-button active-block-button">Uwaga</button>');
-
-                if (!CurrentDocument.readonly) {
-
-                    $('#html-view').bind('mousemove', function(event){
-                        var editable = $(event.target).closest('*[x-editable]');
-                        $('.active', element).not(editable).removeClass('active').children('.active-block-button').remove();
-
-                        if (!editable.hasClass('active')) {
-                            editable.addClass('active').append(button);
-                            if (!editable.is('[x-edit-attribute]') &&
-                                !editable.is('.annotation-inline-box') &&
-                                !editable.is('[x-edit-no-format]')
-                               ) {
-                                editable.append(uwagaButton);
-                            }
-                        }
-                        if (editable.is('.annotation-inline-box')) {
-                            $('*[x-annotation-box]', editable).css({
-                            }).show();
+            super(options);
+            let self = this;
+            var element = $("#html-view");
+            var button = $('<button class="edit-button active-block-button">Edytuj</button>');
+            var uwagaButton = $('<button class="uwaga-button active-block-button">Uwaga</button>');
+
+            if (!CurrentDocument.readonly) {
+
+                $('#html-view').bind('mousemove', function(event){
+                    var editable = $(event.target).closest('*[x-editable]');
+                    $('.active', element).not(editable).removeClass('active').children('.active-block-button').remove();
+
+                    if (!editable.hasClass('active')) {
+                        editable.addClass('active').append(button);
+                        if (!editable.is('[x-edit-attribute]') &&
+                            !editable.is('.annotation-inline-box') &&
+                            !editable.is('[x-edit-no-format]')
+                           ) {
+                            editable.append(uwagaButton);
                         }
                         }
-                    });
-
-                    self.caret = new Caret(element);
-
-                    $('#insert-reference-button').click(function(){
-                        self.addReference();
-                        return false;
-                    });
-
-                    $('#insert-annotation-button').click(function(){
-                        addAnnotation();
-                        return false;
-                    });
+                    }
+                    if (editable.is('.annotation-inline-box')) {
+                        $('*[x-annotation-box]', editable).css({
+                        }).show();
+                    }
+                });
 
 
-                    $('#insert-theme-button').click(function(){
-                        addTheme();
-                        return false;
-                    });
+                self.caret = new Caret(element);
 
 
+                $('#insert-reference-button').click(function(){
+                    self.flush();
+                    self.addReference();
+                    return false;
+                });
 
 
-                    $(".insert-inline-tag").click(function() {
-                        self.insertInlineTag($(this).attr('data-tag'));
-                        return false;
-                    });
+                $('#insert-annotation-button').click(function(){
+                    self.flush();
+                    addAnnotation();
+                    return false;
+                });
 
 
-                    $(".insert-char").click(function() {
-                        addSymbol(caret=self.caret);
-                        return false;
-                    });
+                $('#insert-theme-button').click(function(){
+                    self.flush();
+                    addTheme();
+                    return false;
+                });
 
 
-                    $(document).on('click', '.edit-button', function(event){
-                        event.preventDefault();
-                        openForEdit($(this).parent());
-                    });
+                $(".insert-inline-tag").click(function() {
+                    self.flush();
+                    self.insertInlineTag($(this).attr('data-tag'));
+                    return false;
+                });
 
 
-                    $(document).on('click', '.uwaga-button', function(event){
-                        event.preventDefault();
-                        createUwagaBefore($(this).parent());
-                    });
-                }
+                $(".insert-char").click(function() {
+                    self.flush();
+                    addSymbol(caret=self.caret);
+                    return false;
+                });
 
 
-                $(document).on('click', '[x-node="motyw"]', function(){
-                    selectTheme($(this).attr('theme-class'));
+                $(document).on('click', '.edit-button', function(event){
+                    self.flush();
+                    event.preventDefault();
+                    openForEdit($(this).parent());
                 });
 
                 });
 
-                element.on('click', '.annotation', function(event) {
+                $(document).on('click', '.uwaga-button', function(event){
+                    self.flush();
                     event.preventDefault();
                     event.preventDefault();
-                    event.redakcja_caret_ignore = true;
-                    $('[x-annotation-box]', $(this).parent()).toggleClass('editing');
-                    self.caret.detach();
+                    createUwagaBefore($(this).parent());
                 });
                 });
+            }
 
 
-                old_callback.call(this);
-            };
+            $(document).on('click', '[x-node="motyw"]', function(){
+                selectTheme($(this).attr('theme-class'));
+            });
 
 
-            super(options);
+            element.on('click', '.annotation', function(event) {
+                self.flush();
+                event.preventDefault();
+                event.redakcja_caret_ignore = true;
+                $('[x-annotation-box]', $(this).parent()).toggleClass('editing');
+                self.caret.detach();
+            });
         }
 
         onEnter(success, failure) {
         }
 
         onEnter(success, failure) {
 
                     var htmlView = $('#html-view');
                     htmlView.html(element);
 
                     var htmlView = $('#html-view');
                     htmlView.html(element);
+                    if ('PropertiesPerspective' in $.wiki.perspectives)
+                        $.wiki.perspectives.PropertiesPerspective.enable();
 
                     _finalize(success);
                 },
 
                     _finalize(success);
                 },
                     _finalize(failure);
                 }
             });
                     _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();
 
 
         onExit(success, failure) {
             var self = this;
 
             self.caret.detach();
 
-            $.wiki.exitTab('#PropertiesPerspective');
+            if ('PropertiesPerspective' in $.wiki.perspectives)
+                $.wiki.perspectives.PropertiesPerspective.disable();
 
 
-            $.blockUI({
-                message: 'Zapisywanie widoku...'
-            });
-
-            function _finalize(callback){
-                $.unblockUI();
-                if (callback)
-                    callback();
-            }
-
-            if ($('#html-view .error').length > 0)
-                return _finalize(failure);
-
-            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();
             });
         };
 
         insertInlineTag(tag) {
             this.caret.detach();
+            let self = this;
 
             let selection = window.getSelection();
             var n = selection.rangeCount;
 
             let selection = window.getSelection();
             var n = selection.rangeCount;
                         success: function(html) {
                             // What if no end?
                             node.insertBefore($(html)[0], end);
                         success: function(html) {
                             // What if no end?
                             node.insertBefore($(html)[0], end);
+                            self.flush();
                         }
                     });
                 },
                         }
                     });
                 },