From: Aleksander Ɓukasz Date: Mon, 24 Jun 2013 10:42:00 +0000 (+0200) Subject: Adding/removing lists - first approach X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/e64e37a2fbe6d64b2513133d34370f0d1a63f7b9 Adding/removing lists - first approach --- 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 @@ +
+ +
\ 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); } };