Fixing element highlighting synchronization issue
[fnpeditor.git] / modules / documentCanvas / commands.js
index b08348e..8152ccd 100644 (file)
@@ -1,6 +1,6 @@
 define([
 define([
-
-], function() {
+'modules/documentCanvas/canvas/documentElement'
+], function(documentElement) {
     
 'use strict';
 
     
 'use strict';
 
@@ -76,35 +76,55 @@ commands.register('newNodeRequested', function(canvas, params) {
         selectionStart = cursor.getSelectionStart(),
         selectionEnd = cursor.getSelectionEnd();
 
         selectionStart = cursor.getSelectionStart(),
         selectionEnd = cursor.getSelectionEnd();
 
-    if(cursor.isSelecting() && cursor.isSelectingSiblings()) {
-        if(cursor.isSelectingWithinElement()) {
-            var newElement = selectionStart.element.wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass, start: selectionStart.offset, end: selectionEnd.offset}),
-                caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
-            canvas.setCurrentElement(newElement.children()[0], {caretTo: caretTo});
-        }
-        else {
-            var parent = selectionStart.element.parent(),
-                caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
-
-            var wrapper = canvas.wrapText({
-                inside: parent,
-                _with: {tag: params.wlxmlTag, klass: params.wlxmlClass},
-                offsetStart: selectionStart.offset,
-                offsetEnd: selectionEnd.offset,
-                textNodeIdx: [parent.childIndex(selectionStart.element), parent.childIndex(selectionEnd.element)]
-            });
-
-            canvas.setCurrentElement(wrapper.children()[caretTo === 0 ? 0 : wrapper.children().length - 1], {caretTo: caretTo});
+    if(cursor.isSelecting()) {
+        if(cursor.isSelectingSiblings()) {
+            if(cursor.isSelectingWithinElement()) {
+                var newElement = selectionStart.element.wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass, start: selectionStart.offset, end: selectionEnd.offset}),
+                    caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
+                canvas.setCurrentElement(newElement.children()[0], {caretTo: caretTo});
+            }
+            else {
+                var parent = selectionStart.element.parent(),
+                    caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
+
+                var wrapper = canvas.wrapText({
+                    inside: parent,
+                    _with: {tag: params.wlxmlTag, klass: params.wlxmlClass},
+                    offsetStart: selectionStart.offset,
+                    offsetEnd: selectionEnd.offset,
+                    textNodeIdx: [parent.childIndex(selectionStart.element), parent.childIndex(selectionEnd.element)]
+                });
+
+                canvas.setCurrentElement(wrapper.children()[caretTo === 0 ? 0 : wrapper.children().length - 1], {caretTo: caretTo});
+            }
+        } else {
+            var siblingParents = canvas.getSiblingParents({element1: selectionStart.element, element2: selectionEnd.element})
+            if(siblingParents) {
+                canvas.wrapElements({
+                    element1: siblingParents.element1,
+                    element2: siblingParents.element2,
+                    _with: {tag: params.wlxmlTag, klass: params.wlxmlClass}
+                });
+            }
         }
     }
         }
     }
+
+
 });
 
 commands.register('footnote', function(canvas, params) {
 });
 
 commands.register('footnote', function(canvas, params) {
-    var position = canvas.getCursor().getPosition();
+    var cursor = canvas.getCursor(),
+        position = cursor.getPosition(),
+        asideElement;
+        
 
 
-    var asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
+    if(cursor.isSelectingWithinElement()) {
+        asideElement = position.element.wrapWithNodeElement({tag: 'aside', klass: 'footnote', start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
+    } else {
+        asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
+        asideElement.append({text: ''});
+    }
 
 
-    asideElement.append({text: ''});
     asideElement.toggle(true);
     canvas.setCurrentElement(asideElement);
 });
     asideElement.toggle(true);
     canvas.setCurrentElement(asideElement);
 });