Refactor/fix
[fnpeditor.git] / modules / documentCanvas / canvas / documentElement.js
index 4c09b75..b7e8352 100644 (file)
@@ -101,6 +101,20 @@ $.extend(DocumentElement.prototype, {
         return toret;
     },
 
         return toret;
     },
 
+    getPreviousTextElement: function(includeInvisible) {
+        return this.getNearestTextElement('above', includeInvisible);
+    },
+
+    getNextTextElement: function(includeInvisible) {
+        return this.getNearestTextElement('below', includeInvisible);
+    },
+
+    getNearestTextElement: function(direction, includeInvisible) {
+        includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
+        var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
+        return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
+    },
+
     isVisible: function() {
         return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
     },
     isVisible: function() {
         return this instanceof DocumentTextElement || this.getWlxmlTag() !== 'metadata';
     },
@@ -148,8 +162,10 @@ $.extend(DocumentNodeElement, {
         }
         element.data('other-attrs', params.others);
 
         }
         element.data('other-attrs', params.others);
 
-        if(params.rawChildren) {
+        if(params.rawChildren && params.rawChildren.length) {
             container.append(params.rawChildren);
             container.append(params.rawChildren);
+        } else if(params.prepopulateOnEmpty) {
+            element.append(DocumentTextElement.create({text: ''}));
         }
         return dom;
     },
         }
         return dom;
     },
@@ -528,8 +544,9 @@ $.extend(DocumentTextElement.prototype, {
                     prev.setText(prev.getText() + this.getText() + next.getText());
                     next.detach();
                 } else if (prev || next) {
                     prev.setText(prev.getText() + this.getText() + next.getText());
                     next.detach();
                 } else if (prev || next) {
-                    var target = prev ? prev : next;
-                    target.setText(target.getText() + this.getText());
+                    var target = prev ? prev : next,
+                        newText = prev ? target.getText() + this.getText() : this.getText() + target.getText();
+                    target.setText(newText);
                 } else {
                     parent.after(this);
                 }
                 } else {
                     parent.after(this);
                 }