Skip to content

Commit

Permalink
Fix insertion into blank section
Browse files Browse the repository at this point in the history
Fixes #462
  • Loading branch information
bantic committed Aug 24, 2016
1 parent 843f381 commit 98623c8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
64 changes: 36 additions & 28 deletions src/js/editor/post/post-inserter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/unit/editor/post/insert-post-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 98623c8

Please sign in to comment.