diff --git a/src/js/editor/post/post-inserter.js b/src/js/editor/post/post-inserter.js index 429408499..ead63f893 100644 --- a/src/js/editor/post/post-inserter.js +++ b/src/js/editor/post/post-inserter.js @@ -66,11 +66,7 @@ class Visitor { if (this.cursorSection.isBlank && !this._isNested) { // replace blank section with entire post let newSections = node.sections.map(s => s.clone()); - newSections.forEach(section => { - this._replaceSection(this.cursorSection, section); - }); - let lastNewSection = newSections[newSections.length - 1]; - this.cursorPosition = lastNewSection.tailPosition(); + this._replaceSection(this.cursorSection, newSections); } else { node.sections.forEach(section => this.visit(section)); } @@ -144,19 +140,17 @@ class Visitor { // break out of a nested cursor position _breakNestedAtCursor() { - assert('Cannot call _breakNestedAtCursor if not nested', - this._isNested); + assert('Cannot call _breakNestedAtCursor if not nested', this._isNested); - let parent = this.cursorSection.parent, - cursorAtEndOfList = this.cursorPosition.isEqual(parent.tailPosition()); + let parent = this.cursorSection.parent; + let cursorAtEndOfList = this.cursorPosition.isEqual(parent.tailPosition()); if (cursorAtEndOfList) { let blank = this.builder.createMarkupSection(); this._insertSectionAfter(blank, parent); - this.cursorPosition = blank.headPosition(); } else { let [pre, blank, post] = this._breakListAtCursor(); // jshint ignore:line - this.cursorPosition = blank.headPosition(); + this.cursorPosition = blank.tailPosition(); } } @@ -175,13 +169,6 @@ class Visitor { return [pre, blank, post]; } - _insertSectionAfter(section, parent) { - assert('Cannot _insertSectionAfter nested section', !parent.isNested); - let reference = parent.next; - let collection = this._post.sections; - this.postEditor.insertSectionBefore(collection, section, reference); - } - _wrapNestedSection(section) { let tagName = section.parent.tagName; let parent = this.builder.createListSection(tagName); @@ -226,17 +213,41 @@ class Visitor { _breakMarkerableAtCursor() { let [pre, post] = // jshint ignore:line this.postEditor.splitSection(this.cursorPosition); + this.cursorPosition = pre.tailPosition(); } - _replaceSection(section, newSection) { + _replaceSection(section, newSections) { assert('Cannot replace section that does not have parent.sections', section.parent && section.parent.sections); + assert('Must pass enumerable to _replaceSection', !!newSections.forEach); - let collection = section.parent.sections, - reference = section.next; + let collection = section.parent.sections; + let reference = section.next; this.postEditor.removeSection(section); - this.postEditor.insertSectionBefore(collection, newSection, reference); + newSections.forEach(section => { + this.postEditor.insertSectionBefore(collection, section, reference); + }); + let lastSection = newSections[newSections.length - 1]; + + this.cursorPosition = lastSection.tailPosition(); + } + + _insertSectionBefore(section, reference) { + let collection = this.cursorSection.parent.sections; + this.postEditor.insertSectionBefore(collection, section, reference); + + this.cursorPosition = section.tailPosition(); + } + + // Insert a section after the parent section. + // E.g., add a markup section after a list section + _insertSectionAfter(section, parent) { + assert('Cannot _insertSectionAfter nested section', !parent.isNested); + let reference = parent.next; + let collection = this._post.sections; + this.postEditor.insertSectionBefore(collection, section, reference); + this.cursorPosition = section.tailPosition(); } _insertLeafSection(section) { @@ -249,16 +260,13 @@ class Visitor { if (this.cursorSection.isBlank) { assert('Cannot insert leaf non-markerable section when cursor is nested', !(section.isMarkerable && this._isNested)); - this._replaceSection(this.cursorSection, section); + this._replaceSection(this.cursorSection, [section]); } else if (this.cursorSection.next && this.cursorSection.next.isBlank) { - this._replaceSection(this.cursorSection.next, section); + this._replaceSection(this.cursorSection.next, [section]); } else { let reference = this.cursorSection.next; - let collection = this.cursorSection.parent.sections; - this.postEditor.insertSectionBefore(collection, section, reference); + this._insertSectionBefore(section, reference); } - - this.cursorPosition = section.tailPosition(); } } diff --git a/tests/unit/editor/post/insert-post-test.js b/tests/unit/editor/post/insert-post-test.js index b935c2d6c..08c217daa 100644 --- a/tests/unit/editor/post/insert-post-test.js +++ b/tests/unit/editor/post/insert-post-test.js @@ -1027,3 +1027,21 @@ test('insert 2 markup sections + non-markup onto list', (assert) => { assert.postIsSimilar(editor.post, expected); assert.renderTreeIsEqual(editor._renderTree, expected); }); + +// See https://github.com/bustlelabs/mobiledoc-kit/issues/462 +test('insert 2 markup sections into blank section', (assert) => { + let {post: toInsert} = Helpers.postAbstract.buildFromText(['ghi','jkl']); + let expected = toInsert; + + editor = buildEditorWithMobiledoc(({post, markupSection, marker}) => { + return post([markupSection('p', [marker('')])]); + }); + + let position = editor.post.tailPosition(); + postEditor = new PostEditor(editor); + postEditor.insertPost(position, toInsert); + postEditor.complete(); + + assert.postIsSimilar(editor.post, expected); + assert.renderTreeIsEqual(editor._renderTree, expected); +});