Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return cards and atoms from high-level insert methods #428

Merged
merged 1 commit into from
Jul 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,24 +908,28 @@ class Editor {
* @param {String} atomName
* @param {String} [atomText='']
* @param {Object} [atomPayload={}]
* @return {Atom} The inserted atom.
* @public
*/
insertAtom(atomName, atomText='', atomPayload={}) {
if (!this.hasCursor()) { return; }
if (this.post.isBlank) {
this._insertEmptyMarkupSectionAtCursor();
}

let atom;
let { range } = this;
this.run(postEditor => {
let position = range.head;

let atom = postEditor.builder.createAtom(atomName, atomText, atomPayload);
atom = postEditor.builder.createAtom(atomName, atomText, atomPayload);
if (!range.isCollapsed) {
position = postEditor.deleteRange(range);
}

postEditor.insertMarkers(position, [atom]);
});
return atom;
}

/**
Expand All @@ -937,6 +941,7 @@ class Editor {
* @param {String} cardName
* @param {Object} [cardPayload={}]
* @param {Boolean} [inEditMode=false] Whether the card should be inserted in edit mode.
* @return {Card} The inserted Card section.
* @public
*/
insertCard(cardName, cardPayload={}, inEditMode=false) {
Expand All @@ -945,10 +950,11 @@ class Editor {
this._insertEmptyMarkupSectionAtCursor();
}

let card;
let { range } = this;
this.run(postEditor => {
let position = range.tail;
let card = postEditor.builder.createCardSection(cardName, cardPayload);
card = postEditor.builder.createCardSection(cardName, cardPayload);
if (inEditMode) {
this.editCard(card);
}
Expand Down Expand Up @@ -977,6 +983,7 @@ class Editor {
// See: https://github.com/bustlelabs/mobiledoc-kit/issues/286
postEditor.setRange(new Range(card.tailPosition()));
});
return card;
}

/**
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/editor/editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,23 @@ test('#insertAtom when post is blank', (assert) => {
assert.postIsSimilar(editor.post, expected);
});

test('#insertAtom returns the inserted atom', (assert) => {
let atom = {
name: 'the-atom',
type: 'dom',
render() {
}
};

editor = Helpers.mobiledoc.renderInto(editorElement, ({post}) => {
return post();
}, {atoms: [atom]});

const insertedAtom = editor.insertAtom('the-atom', 'THEATOMTEXT');

assert.equal(insertedAtom.value, 'THEATOMTEXT', 'return value is the inserted atom');
});

test('#insertCard inserts card at section after cursor position, replacing range if non-collapsed', (assert) => {
let card = {
name: 'the-card',
Expand Down Expand Up @@ -612,3 +629,20 @@ test('#insertCard when post is blank', (assert) => {

assert.postIsSimilar(editor.post, expected, 'adds card section');
});

test('#insertCard returns card object', (assert) => {
let card = {
name: 'the-card',
type: 'dom',
render() {
}
};

editor = Helpers.mobiledoc.renderInto(editorElement, ({post}) => {
return post();
}, {cards: [card]});

const insertedCard = editor.insertCard('the-card');

assert.equal(editor.post.sections.tail, insertedCard, 'returned card is the inserted card');
});