canvas api: wrapElements
[fnpeditor.git] / modules / documentCanvas / commands.js
index 52f03d8..b08348e 100644 (file)
@@ -26,7 +26,9 @@ commands.register('unwrap-node', function(canvas) {
         parent2 = selectionEnd.element.parent() || undefined;
 
     if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
         parent2 = selectionEnd.element.parent() || undefined;
 
     if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
+        var selectionAnchor = cursor.getSelectionAnchor();
         canvas.list.extractItems({element1: parent1, element2: parent2});
         canvas.list.extractItems({element1: parent1, element2: parent2});
+        canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
     } else if(!cursor.isSelecting()) {
         var toUnwrap = cursor.getPosition().element,
             parent = toUnwrap.unwrap();
     } else if(!cursor.isSelecting()) {
         var toUnwrap = cursor.getPosition().element,
             parent = toUnwrap.unwrap();
@@ -53,6 +55,7 @@ commands.register('toggle-list', function(canvas, params) {
         parent1 = selectionStart.element.parent() || undefined,
         parent2 = selectionEnd.element.parent() || undefined;
 
         parent1 = selectionStart.element.parent() || undefined,
         parent2 = selectionEnd.element.parent() || undefined;
 
+    var selectionFocus = cursor.getSelectionFocus();
     if(params.toggle) {
         canvas.list.create({element1: parent1, element2: parent2});
     } else {
     if(params.toggle) {
         canvas.list.create({element1: parent1, element2: parent2});
     } else {
@@ -60,6 +63,7 @@ commands.register('toggle-list', function(canvas, params) {
             canvas.list.extractItems({element1: parent1, element2: parent2, merge: false});
         } 
     }
             canvas.list.extractItems({element1: parent1, element2: parent2, merge: false});
         } 
     }
+    canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
 });
 
 commands.register('toggle-grid', function(canvas, params) {
 });
 
 commands.register('toggle-grid', function(canvas, params) {
@@ -74,21 +78,37 @@ commands.register('newNodeRequested', function(canvas, params) {
 
     if(cursor.isSelecting() && cursor.isSelectingSiblings()) {
         if(cursor.isSelectingWithinElement()) {
 
     if(cursor.isSelecting() && cursor.isSelectingSiblings()) {
         if(cursor.isSelectingWithinElement()) {
-            selectionStart.element.wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass, start: selectionStart.offset, end: selectionEnd.offset});
+            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 {
         }
         else {
-            var parent = selectionStart.element.parent();
-            canvas.wrapText({
+            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)]
             });
                 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});
         }
     }
 });
 
         }
     }
 });
 
+commands.register('footnote', function(canvas, params) {
+    var position = canvas.getCursor().getPosition();
+
+    var asideElement = position.element.divide({tag: 'aside', klass: 'footnote', offset: position.offset});
+
+    asideElement.append({text: ''});
+    asideElement.toggle(true);
+    canvas.setCurrentElement(asideElement);
+});
+
 
 return {
     run: function(name, params, canvas) {
 
 return {
     run: function(name, params, canvas) {