Skip to content

Commit

Permalink
Merge pull request #428 from joshfrench/return-cards
Browse files Browse the repository at this point in the history
Return cards and atoms from high-level insert methods
  • Loading branch information
bantic authored Jul 5, 2016
2 parents cb8e91f + fd3dcd2 commit 4f789d8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
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');
});

0 comments on commit 4f789d8

Please sign in to comment.