From: Aleksander Ɓukasz Date: Tue, 3 Dec 2013 13:49:29 +0000 (+0100) Subject: wip: integrating lists, first extensions api approach X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/20b9b567165ee10532a441f809a2a718b5b5e570 wip: integrating lists, first extensions api approach --- diff --git a/src/editor/modules/data/data.js b/src/editor/modules/data/data.js index 7c9077f..556d9b5 100644 --- a/src/editor/modules/data/data.js +++ b/src/editor/modules/data/data.js @@ -1,7 +1,8 @@ define([ 'libs/jquery', './saveDialog', - 'wlxml/wlxml' + 'wlxml/wlxml', + 'wlxml/extensions/list/list' ], function($, saveDialog, wlxml) { diff --git a/src/editor/modules/documentCanvas/commands.js b/src/editor/modules/documentCanvas/commands.js index 8793b9c..df21cff 100644 --- a/src/editor/modules/documentCanvas/commands.js +++ b/src/editor/modules/documentCanvas/commands.js @@ -50,12 +50,17 @@ commands.register('unwrap-node', function(canvas) { parent1 = selectionStart.element.parent() || undefined, parent2 = selectionEnd.element.parent() || undefined; - if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) { - return; - // TODO - // var selectionAnchor = cursor.getSelectionAnchor(); - // canvas.list.extractItems({element1: parent1, element2: parent2}); - // canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset}); + var selectionAnchor = cursor.getSelectionAnchor(), + node1 = parent1.data('wlxmlNode'), + node2 = parent2.data('wlxmlNode'), + doc = node1.document; + if(doc.areItemsOfSameList({node1: node1, node2: node2})) { + + + doc.transform('extractItems', {item1: node1, item2: node2}); + + //canvas.list.extractItems({element1: parent1, element2: parent2}); + canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset}); } else if(!cursor.isSelecting()) { var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'), parentNode = nodeToUnwrap.unwrap(); @@ -65,17 +70,22 @@ commands.register('unwrap-node', function(canvas) { } }); -// commands.register('wrap-node', function(canvas) { -// var cursor = canvas.getCursor(), -// selectionStart = cursor.getSelectionStart(), -// selectionEnd = cursor.getSelectionEnd(), -// parent1 = selectionStart.element.parent() || undefined, -// parent2 = selectionEnd.element.parent() || undefined; +commands.register('wrap-node', function(canvas) { + var cursor = canvas.getCursor(), + selectionStart = cursor.getSelectionStart(), + selectionEnd = cursor.getSelectionEnd(), + parent1 = selectionStart.element.parent() || undefined, + parent2 = selectionEnd.element.parent() || undefined; -// if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) { -// canvas.list.create({element1: parent1, element2: parent2}); -// } -// }); + var node1 = parent1.data('wlxmlNode'), + node2 = parent2.data('wlxmlNode'), + doc = node1.document; + + if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) { + //canvas.list.create({element1: parent1, element2: parent2}); + doc.transform('createList', {node1: node1, node2: node2}); + } +}); commands.register('list', function(canvas, params) { var cursor = canvas.getCursor(), @@ -90,7 +100,11 @@ commands.register('list', function(canvas, params) { return; } - canvas.list.create({element1: parent1, element2: parent2}); + var node1 = parent1.data('wlxmlNode'), + node2 = parent2.data('wlxmlNode'), + doc = node1.document; + + doc.transform('createList', {node1: node1, node2: node2}); canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset}); }); diff --git a/src/editor/modules/documentCanvas/nodes.less b/src/editor/modules/documentCanvas/nodes.less index d02e872..ab26442 100644 --- a/src/editor/modules/documentCanvas/nodes.less +++ b/src/editor/modules/documentCanvas/nodes.less @@ -73,7 +73,7 @@ display:none; } -[wlxml-class="list-items"] { +[wlxml-class="list"] { [wlxml-class="item"] { display: list-item; @@ -83,13 +83,13 @@ } [wlxml-class="item"] { - [wlxml-class="list-items"] { + [wlxml-class="list"] { display: block; } } -[wlxml-class="list-items-enum"] { +[wlxml-class="list-enum"] { counter-reset: myitem; diff --git a/src/wlxml/extensions/list/list.js b/src/wlxml/extensions/list/list.js index 6cf875f..4291957 100644 --- a/src/wlxml/extensions/list/list.js +++ b/src/wlxml/extensions/list/list.js @@ -23,6 +23,12 @@ extension.classMethods['list'] = { } } +extension.documentMethods = { + areItemsOfSameList: function(params) { + return params.node1.parent().sameNode(params.node2.parent()) && params.node2.parent().is('list'); + } +} + extension.documentTransformations.push({ name: 'createList', impl: function(params) { @@ -30,7 +36,7 @@ extension.documentTransformations.push({ parentContents = parent.contents(), nodeIndexes = [params.node1.getIndex(), params.node2.getIndex()].sort(), nodesToWrap = [], - listNode = params.node1.document.createDocumentNode({tagName: 'div', attrs: {'class': 'list.items'}}), + listNode = params.node1.document.createDocumentNode({tagName: 'div', attrs: {'class': 'list'}}), node, i; for(i = nodeIndexes[0]; i <= nodeIndexes[1]; i++) { @@ -131,7 +137,6 @@ extension.documentTransformations.push({ reference.after(toAdd); } if(!params.merge && listIsNested) { - debugger; return this.transform('extractItems', {item1: extractedItems[0], item2: extractedItems[extractedItems.length-1]}); } return true; diff --git a/src/wlxml/extensions/list/list.test.js b/src/wlxml/extensions/list/list.test.js index f15d75b..1efa795 100644 --- a/src/wlxml/extensions/list/list.test.js +++ b/src/wlxml/extensions/list/list.test.js @@ -28,7 +28,7 @@ var removeEmptyTextNodes = function(xml) { }; -describe.only('Lists extension', function() { +describe('Lists extension', function() { describe('creating lists', function() { it('allows creation of a list from existing sibling DocumentElements', function() { diff --git a/src/wlxml/wlxml.js b/src/wlxml/wlxml.js index d2dbd30..dbc0cf0 100644 --- a/src/wlxml/wlxml.js +++ b/src/wlxml/wlxml.js @@ -274,6 +274,8 @@ return { }); + _.extend(WLXMLDocument.prototype, extension.documentMethods); + } };