editor: lists fix - do not touch context root on list creation
[fnpeditor.git] / src / editor / plugins / core / lists.js
index 3cf1192..c6de730 100644 (file)
@@ -12,6 +12,10 @@ var getBoundriesForAList = function(fragment) {
     }
     if(fragment instanceof fragment.NodeFragment) {
         node = fragment.node.getNearestElementNode();
+        if(node.isContextRoot()) {
+            node = fragment.node;
+        }
+
         return {
             node1: node,
             node2: node
@@ -32,32 +36,81 @@ var countItems = function(boundries) {
 var toggleListAction = function(type) {
     
     var execute = {
-        add: function(params) {
+        add: function(callback, params) {
             var boundries = getBoundriesForAList(params.fragment),
-                listParams = {klass: type === 'Bullet' ? 'list' : 'list.enum'};
+                listParams = {klass: type === 'Bullet' ? 'list' : 'list.enum'},
+                action = this;
+
             if(boundries && boundries.node1) {
                 listParams.node1 = boundries.node1;
                 listParams.node2 = boundries.node2;
-                boundries.node1.document.createList(listParams);
+                boundries.node1.document.transaction(function() {
+                    var list = boundries.node1.document.createList(listParams),
+                        item1 = list.object.getItem(0),
+                        text = item1 ? item1.contents()[0] : undefined, //
+                        doc = boundries.node1.document;
+                    if(text) {
+                        return doc.createFragment(doc.CaretFragment, {node: text, offset:0});
+                    }
+                }, {
+                    metadata: {
+                        description: action.getState().description,
+                        fragment: params.fragment
+                    },
+                    success: callback
+                });
             } else {
                 throw new Error('Invalid boundries');
             }
         },
-        remove: function(params) {
+        remove: function(callback, params) {
             /* globals Node */
-            var current = params.fragment.node;
+            var current = params.fragment.node,
+                action = this;
 
             var toSearch = current.nodeType === Node.ELEMENT_NODE ? [current] : [];
             toSearch = toSearch.concat(current.parents());
             toSearch.some(function(node) {
                 if(node.is('list')) {
-                    node.object.extractListItems();
+                    node.document.transaction(function() {
+                        var firstItem = node.object.extractListItems(),
+                            toret;
+                        if(params.fragment.isValid()) {
+                            toret = params.fragment;
+                        } else {
+                            toret = node.document.createFragment(node.document.NodeFragment, {node: firstItem});
+                        }
+                        return toret;
+                    }, {
+                        metadata: {
+                            description: action.getState().description,
+                            fragment: params.fragment
+                        },
+                        success: callback
+                    });
+                    
                     return true; // break
                 }
-            });
+            }.bind(this));
         },
-        changeType: function(params) {
-            params.fragment.node.getParent('list').setClass(type === 'Bullet' ? 'list' : 'list.enum');
+        changeType: function(callback, params) {
+            var node = params.fragment.node,
+                action = this;
+            node.document.transaction(function() {
+                var list = node.getParent('list');
+                list.setClass(type === 'Bullet' ? 'list' : 'list.enum');
+                if(params.fragment.isValid()) {
+                    return params.fragment;
+                } else {
+                    return node.document.createFragment(node.document.NodeFragment, {node: list.contents()[0]});
+                }
+            }, {
+                metadata: {
+                    description: action.getState().description,
+                    fragment: params.fragment
+                },
+                success: callback
+            });
         }
     };
 
@@ -69,6 +122,7 @@ var toggleListAction = function(type) {
         return false;
     };
 
+    var label = type === 'Bullet' ? gettext('bull. list') : gettext('num. list');
 
     return {
         name: 'toggle' + type + 'List',
@@ -77,7 +131,7 @@ var toggleListAction = function(type) {
             fragment: {type: 'context', name: 'fragment'}
         },
         stateDefaults: {
-            label: type === 'Bullet' ? gettext('bull. list') : gettext('num. list')
+            label: label
         },
         getState: function(params) {
             if(!params.fragment || !params.fragment.isValid()) {
@@ -89,7 +143,7 @@ var toggleListAction = function(type) {
                 if((list.getClass() === 'list' && type === 'Enum') || (list.getClass() === 'list.enum' && type === 'Bullet')) {
                     return {
                         allowed: true,
-                        description: interpolate(gettext('Change list type to %s'), [type]),
+                        description: interpolate(gettext('Change list type to %s'), [label]),
                         execute: execute.changeType
                     };
                 }