Skip to content

Commit

Permalink
fix(paste): Handle paste card at start of middle list item (#468)
Browse files Browse the repository at this point in the history
Change postEditor#_splitListAtPosition to ensure that it partitions the
list items correctly if the cursor position is at the start of a
middle-list item.

Fixes #467
  • Loading branch information
bantic authored Aug 24, 2016
1 parent a3d274d commit 939a541
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
45 changes: 19 additions & 26 deletions src/js/editor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,33 +875,26 @@ class PostEditor {
position = pre.tailPosition();
}

let positionIsStart = position.isEqual(list.headPosition()),
positionIsEnd = position.isEqual(list.tailPosition());

if (positionIsStart || positionIsEnd) {
let blank = this.builder.createListSection(list.tagName);
let reference = position.isEqual(list.headPosition()) ? list :
list.next;
let collection = this.editor.post.sections;
this.insertSectionBefore(collection, blank, reference);

let lists = positionIsStart ? [blank, list] : [list, blank];
return lists;
} else {
let preList = this.builder.createListSection(list.tagName),
postList = this.builder.createListSection(list.tagName);
let preItem = position.section;
let currentList = preList;
list.items.forEach(item => {
currentList.items.append(item.clone());
if (item === preItem) {
currentList = postList;
}
});
let preList = this.builder.createListSection(list.tagName);
let postList = this.builder.createListSection(list.tagName);

let preItem = position.section;
let currentList = preList;
list.items.forEach(item => {
// If this item matches the start item and the position is at its start,
// it should be appended to the postList instead of the preList
if (item === preItem && position.isEqual(item.headPosition())) {
currentList = postList;
}
currentList.items.append(item.clone());
// If we just appended the preItem, append the remaining items to the postList
if (item === preItem) {
currentList = postList;
}
});

this._replaceSection(list, [preList, postList]);
return [preList, postList];
}
this._replaceSection(list, [preList, postList]);
return [preList, postList];
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/editor/post/insert-post-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ let expectationGroups = [{
[['* abc|','* def'], ['[my-card]'], ['* abc','[my-card]|','* def']],
[['* ab|c','* def'], ['[my-card]'], ['* ab','[my-card]|','* c','* def']],
[['* abc|','* def'], ['[my-card]'], ['* abc','[my-card]|','* def']],
// See https://github.com/bustlelabs/mobiledoc-kit/issues/467
[['* abc','* |def'], ['[my-card]'], ['* abc','[my-card]|','* def']],

// insert markup section between list items
[['* abc|','* def'], ['123'], ['* abc123|','* def']],
Expand Down

0 comments on commit 939a541

Please sign in to comment.