From e64e37a2fbe6d64b2513133d34370f0d1a63f7b9 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Aleksander=20=C5=81ukasz?=
 <aleksander.lukasz@nowoczesnapolska.org.pl>
Date: Mon, 24 Jun 2013 12:42:00 +0200
Subject: [PATCH] Adding/removing lists - first approach

---
 modules/documentCanvas/canvas.js           | 10 ++++++++++
 modules/documentCanvas/canvasManager.js    | 21 +++++++++++++++++++++
 modules/documentCanvas/documentCanvas.js   |  3 +++
 modules/documentCanvas/transformations.js  | 14 ++++++++++++++
 modules/documentToolbar/documentToolbar.js |  3 ++-
 modules/documentToolbar/template.html      |  3 +++
 modules/rng/rng.js                         |  3 +++
 7 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js
index 5db5e78..80d3915 100644
--- a/modules/documentCanvas/canvas.js
+++ b/modules/documentCanvas/canvas.js
@@ -175,6 +175,16 @@ Canvas.prototype.removeList = function(options) {
     
 }
 
+Canvas.prototype.insideList = function(options) {
+    if(options.pointer) {
+        if(options.pointer.klass === 'list-items' || options.pointer.klass === 'item')
+            return true;
+        var pointerElement = $(this.content.find('#' + options.pointer.id));
+        return pointerElement.parents('list-items').length > 0;
+    }
+    return false;
+}
+
 
 return {Canvas: Canvas, Node: Node};
 
diff --git a/modules/documentCanvas/canvasManager.js b/modules/documentCanvas/canvasManager.js
index 6a0fc5c..3ced49a 100644
--- a/modules/documentCanvas/canvasManager.js
+++ b/modules/documentCanvas/canvasManager.js
@@ -14,6 +14,7 @@ var getCursorPosition = function() {
         textNodeOffset: selection.anchorOffset,
         textNodeIndex: parent.contents().index(anchorNode),
         parentNode: parent,
+        focusNode: $(selection.focusNode).parent(),
         isAtEnd: selection.anchorOffset === anchorNode.text().length
     }
 };
@@ -181,6 +182,26 @@ Manager.prototype.onBackspaceKey = function(e) {
     }
 }
 
+Manager.prototype.command = function(command, meta) {
+    var pos = getCursorPosition();
+    
+    if(command === 'createList') {
+        var node = new wlxmlNode.Node(pos.parentNode);
+        if(window.getSelection().getRangeAt().collapsed && this.canvas.insideList({pointer: node})) {
+            this.canvas.removeList({pointer: node});
+            this.selectNode(node, {movecaret: 'end'});
+            this.sandbox.publish('contentChanged');
+        }
+        else {
+            if(!this.canvas.insideList({pointer: node})) {
+                this.canvas.createList({start: new wlxmlNode.Node(pos.parentNode), end: new wlxmlNode.Node(pos.focusNode)});
+                this.selectNode(new wlxmlNode.Node(pos.parentNode), {movecaret: 'end'});
+                this.sandbox.publish('contentChanged');
+            }
+        }
+    }
+
+}
 
 
 return Manager;
diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js
index fb67c0d..cc4964b 100644
--- a/modules/documentCanvas/documentCanvas.js
+++ b/modules/documentCanvas/documentCanvas.js
@@ -52,6 +52,9 @@ return function(sandbox) {
         },
         wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
             manager.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
+        },
+        command: function(command, meta) {
+            manager.command(command, meta);
         }
     }
     
diff --git a/modules/documentCanvas/transformations.js b/modules/documentCanvas/transformations.js
index d245818..4824849 100644
--- a/modules/documentCanvas/transformations.js
+++ b/modules/documentCanvas/transformations.js
@@ -37,6 +37,20 @@ define(['libs/jquery-1.9.1.min'], function($) {
             transform(toBlock, 'div');
             transform(toInline, 'span');
 
+            toret.find(":not(iframe)").addBack().contents().filter(function() {
+                return this.nodeType == 3} ).each(function() {
+                    var n = $(this); 
+                    var hasText = /\S/g.test(n.text());
+                    if(!hasText) {
+                        n.remove();
+                        return;
+                    }
+                    var startSpace = /\s/g.test(n.text().substr(0,1));
+                    var endSpace = /\s/g.test(n.text().substr(-1)) && n.text().length > 1;
+                    var trimmed = $.trim(n.text());
+                    n.get(0).data = (startSpace ? ' ' : '') + trimmed + (endSpace ? ' ' : '');
+            });
+            
             return toret.children();
         },
         getMetaData: function(xml) {
diff --git a/modules/documentToolbar/documentToolbar.js b/modules/documentToolbar/documentToolbar.js
index a2a1567..c459428 100644
--- a/modules/documentToolbar/documentToolbar.js
+++ b/modules/documentToolbar/documentToolbar.js
@@ -40,8 +40,9 @@ return function(sandbox) {
                             //this.wrapWithNodeRequest(wlxmlTag, wlxmlClass);
                         }
                         sandbox.publish('newNodeRequested', wlxmlTag, wlxmlClass);
+                    } else {
+                        sandbox.publish('command', btn.attr('data-btn'), btn.attr('data-meta'));
                     }
-                    //sandbox.publish('command', btn.attr('data-btn'), btn.attr('data-meta'));
                 }
             });
         },
diff --git a/modules/documentToolbar/template.html b/modules/documentToolbar/template.html
index 104f00a..112d93b 100644
--- a/modules/documentToolbar/template.html
+++ b/modules/documentToolbar/template.html
@@ -22,5 +22,8 @@
         <button data-btn="grid" data-btn-type="toggle" class="btn btn-mini"><i class="icon-th-large"></i></button>
         <button data-btn="tags" data-btn-type="toggle" class="btn btn-mini"><i class="icon-tag"></i></button>
     </div>
+    <div class="rng-module-documentToolbar-toolbarGroup">
+        <button data-btn="createList" data-btn-type="cmd" class="btn btn-mini"><i class="icon-list"></i></button>
+    </div>
     <div style="clear: both;"></div>
 </div>
\ No newline at end of file
diff --git a/modules/rng/rng.js b/modules/rng/rng.js
index 643b683..7c39d0f 100644
--- a/modules/rng/rng.js
+++ b/modules/rng/rng.js
@@ -259,6 +259,9 @@ return function(sandbox) {
             } else {
                 sandbox.getModule('documentCanvas').wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
             }
+        },
+        command: function(cmd, meta) {
+            sandbox.getModule('documentCanvas').command(cmd, meta);
         }
     };
     
-- 
2.20.1