Refactoring: aliasing requirejs module names
[fnpeditor.git] / modules / documentCanvas / commands.js
index b0fb093..043d6b7 100644 (file)
@@ -1,6 +1,6 @@
 define([
-
-], function() {
+'modules/documentCanvas/canvas/documentElement'
+], function(documentElement) {
     
 'use strict';
 
@@ -48,22 +48,22 @@ commands.register('wrap-node', function(canvas) {
     }
 });
 
-commands.register('toggle-list', function(canvas, params) {
+commands.register('list', function(canvas, params) {
     var cursor = canvas.getCursor(),
         selectionStart = cursor.getSelectionStart(),
         selectionEnd = cursor.getSelectionEnd(),
         parent1 = selectionStart.element.parent() || undefined,
         parent2 = selectionEnd.element.parent() || undefined;
 
-    if(params.toggle) {
-        var selectionFocus = cursor.getSelectionFocus();
-        canvas.list.create({element1: parent1, element2: parent2});
-        canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
-    } else {
-        if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
-            canvas.list.extractItems({element1: parent1, element2: parent2, merge: false});
-        } 
+    var selectionFocus = cursor.getSelectionFocus();
+
+    if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
+        return;
     }
+
+    canvas.list.create({element1: parent1, element2: parent2});
+
+    canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
 });
 
 commands.register('toggle-grid', function(canvas, params) {
@@ -76,27 +76,88 @@ commands.register('newNodeRequested', function(canvas, params) {
         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});
+    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}
+                });
+            }
         }
-        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 if(canvas.getCurrentNodeElement()) {
+        var el = canvas.getCurrentNodeElement().wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass});
+        canvas.setCurrentElement(el);
+    }
+
+
+});
+
+commands.register('footnote', function(canvas, params) {
+    var cursor = canvas.getCursor(),
+        position = cursor.getPosition(),
+        asideElement;
+        
+
+    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.toggle(true);
+    canvas.setCurrentElement(asideElement);
+});
+
+commands.register('take-away-node', function(canvas) {
+    var position = canvas.getCursor().getPosition(),
+        element = position.element,
+        nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
+
+    if(!nodeElement || !(nodeElement.parent()))
+        return;
+
+
+    var range = nodeElement.unwrapContents();
+
+    if(element) {
+        var elementIsFirstChild = nodeElement.childIndex(element);
+        if(element.bound()) {
+            canvas.setCurrentElement(element, {caretTo: position.offset});
+        } else {
+            if(elementIsFirstChild) {
+                canvas.setCurrentElement(range.element1, {caretTo: 'end'});
+            } else {
+                canvas.setCurrentElement(range.element2, {caretTo: 'end'});
+            }
         }
+    } else {
+        canvas.setCurrentElement(range.element1, {caretTo: 'start'});
     }
+
 });