From: Aleksander Ɓukasz Date: Fri, 1 Aug 2014 09:21:01 +0000 (+0200) Subject: splitting text X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/9a99193e9c35b8688e2c9281edfd8b972107ff43?ds=sidebyside;hp=-c splitting text 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. --- 9a99193e9c35b8688e2c9281edfd8b972107ff43 diff --git a/src/editor/modules/documentCanvas/canvas/documentElement.js b/src/editor/modules/documentCanvas/canvas/documentElement.js index 5e653b8..7bbc5c1 100644 --- a/src/editor/modules/documentCanvas/canvas/documentElement.js +++ b/src/editor/modules/documentCanvas/canvas/documentElement.js @@ -78,6 +78,9 @@ $.extend(DocumentElement.prototype, { 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)); diff --git a/src/editor/modules/documentCanvas/canvas/keyboard.js b/src/editor/modules/documentCanvas/canvas/keyboard.js index b39a900..bd66c6e 100644 --- a/src/editor/modules/documentCanvas/canvas/keyboard.js +++ b/src/editor/modules/documentCanvas/canvas/keyboard.js @@ -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); + } } ]; diff --git a/src/editor/plugins/core/core.test.js b/src/editor/plugins/core/core.test.js index d315ca5..6a1bd32 100644 --- a/src/editor/plugins/core/core.test.js +++ b/src/editor/plugins/core/core.test.js @@ -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('
paragraph
'), + 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('
paragraph
'), + 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('
paragraph
'), + 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); + }); + }); + });