splitting text
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 1 Aug 2014 09:21:01 +0000 (11:21 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 14 Aug 2014 14:01:45 +0000 (16:01 +0200)
This covers 1/3 of cases handled in the old implementation. Things
that are not covered:

- enter + ctrl
- enter on selection type 'node'

Both are most likely not necessary.

src/editor/modules/documentCanvas/canvas/documentElement.js
src/editor/modules/documentCanvas/canvas/keyboard.js
src/editor/plugins/core/core.test.js

index 5e653b8..7bbc5c1 100644 (file)
@@ -78,6 +78,9 @@ $.extend(DocumentElement.prototype, {
     sameNode: function(other) {
         return other && (typeof other === typeof this) && other.dom[0] === this.dom[0];
     },
     sameNode: function(other) {
         return other && (typeof other === typeof this) && other.dom[0] === this.dom[0];
     },
+    isRootElement: function() {
+        return this.sameNode(this.canvas.rootElement);
+    },
 
     trigger: function() {
         this.canvas.eventBus.trigger.apply(this.canvas.eventBus, Array.prototype.slice.call(arguments, 0));
 
     trigger: function() {
         this.canvas.eventBus.trigger.apply(this.canvas.eventBus, Array.prototype.slice.call(arguments, 0));
index b39a900..bd66c6e 100644 (file)
@@ -504,6 +504,34 @@ var keyEventHandlers = [
             });
 
         }
             });
 
         }
+    },
+    {
+        applies: function(e, s) {
+            return s.type === 'caret' && e.key === KEYS.ENTER && !s.element.parent().isRootElement();
+        },
+        run: function(e, s) {
+            var result, goto, gotoOptions;
+            void(e);
+            e.preventDefault();
+            s.canvas.wlxmlDocument.transaction(function() {
+                result = s.element.wlxmlNode.breakContent({offset: s.offset});
+            }, {
+                metadata: {
+                    description: gettext('Splitting text'),
+                    fragment: s.toDocumentFragment()
+                }
+            });
+
+            if(result.emptyText) {
+                goto = result.emptyText;
+                gotoOptions = {};
+            } else {
+                goto = result.second;
+                gotoOptions = {caretTo: 'start'};
+            }
+
+            s.canvas.setCurrentElement(utils.getElementForNode(goto), gotoOptions);
+        }
     }
 ];
 
     }
 ];
 
index d315ca5..6a1bd32 100644 (file)
@@ -406,6 +406,65 @@ describe('Keyboard interactions', function() {
         });
     });
 
         });
     });
 
+    describe('splitting with enter', function() {
+        afterEach(removeCanvas);
+
+        it('splits paragraph into two in the middle', function() {
+            var c = getCanvasFromXML('<section><div class="p">paragraph</div></section>'),
+                k = new Keyboard(c);
+
+            k.withCaret('para|graph').press(K.ENTER);
+
+            var rootContents = c.wlxmlDocument.root.contents();
+            expect(rootContents.length).to.equal(2);
+            expect(rootContents[0].is({tagName: 'div', klass: 'p'})).to.equal(true);
+            expect(rootContents[0].contents()[0].getText()).to.equal('para');
+            expect(rootContents[1].is({tagName: 'div', klass: 'p'})).to.equal(true);
+            expect(rootContents[1].contents()[0].getText()).to.equal('graph');
+
+            var selection = c.getSelection();
+            expect(selection.type).to.equal('caret');
+            expect(selection.element.sameNode(getTextElement('graph', c))).to.equal(true);
+            expect(selection.offset).to.equal(0);
+        });
+        it('splits paragraph into two at the beginning', function() {
+            var c = getCanvasFromXML('<section><div class="p">paragraph</div></section>'),
+                k = new Keyboard(c);
+
+            k.withCaret('|paragraph').press(K.ENTER);
+
+            var rootContents = c.wlxmlDocument.root.contents();
+            expect(rootContents.length).to.equal(2);
+            expect(rootContents[0].is({tagName: 'div', klass: 'p'})).to.equal(true);
+            expect(rootContents[0].contents()[0].getText()).to.equal('');
+            expect(rootContents[1].is({tagName: 'div', klass: 'p'})).to.equal(true);
+            expect(rootContents[1].contents()[0].getText()).to.equal('paragraph');
+
+            var selection = c.getSelection();
+            expect(selection.type).to.equal('caret');
+            expect(selection.element.sameNode(getTextElement('', c))).to.equal(true);
+            expect(selection.offset).to.equal(0);
+        });
+        it('splits paragraph into two at the end', function() {
+            var c = getCanvasFromXML('<section><div class="p">paragraph</div></section>'),
+                k = new Keyboard(c);
+
+            k.withCaret('paragraph|').press(K.ENTER);
+
+            var rootContents = c.wlxmlDocument.root.contents();
+            expect(rootContents.length).to.equal(2);
+            expect(rootContents[0].is({tagName: 'div', klass: 'p'})).to.equal(true);
+            expect(rootContents[0].contents()[0].getText()).to.equal('paragraph');
+            expect(rootContents[1].is({tagName: 'div', klass: 'p'})).to.equal(true);
+            expect(rootContents[1].contents()[0].getText()).to.equal('');
+
+            var selection = c.getSelection();
+            expect(selection.type).to.equal('caret');
+            expect(selection.element.sameNode(getTextElement('', c))).to.equal(true);
+            expect(selection.offset).to.equal(0);
+        });
+    });
+
 
 });
 
 
 });