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

Pass card options to card/atom components #122

Closed
Closed
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
6 changes: 5 additions & 1 deletion addon/components/mobiledoc-editor/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default Component.extend({
placeholder: this.get('placeholder'),
spellcheck: this.get('spellcheck'),
autofocus: this.get('autofocus'),
cardOptions: this.get('cardOptions'),
cards: this.get('cards') || [],
atoms: this.get('atoms') || []
}, options);
Expand Down Expand Up @@ -159,7 +160,7 @@ export default Component.extend({
// Create a new editor.
let editorOptions = this.get('editorOptions');
editorOptions.mobiledoc = mobiledoc;
editorOptions.cardOptions = {
let componentHooks = {
[ADD_CARD_HOOK]: ({env, options, payload}, isEditing=false) => {
let cardId = Ember.uuid();
let cardName = env.name;
Expand All @@ -178,6 +179,7 @@ export default Component.extend({
cardName,
payload,
env,
options,
editor,
postModel: env.postModel
});
Expand All @@ -202,6 +204,7 @@ export default Component.extend({
payload,
value,
callbacks: env,
options,
editor,
postModel: env.postModel
});
Expand All @@ -217,6 +220,7 @@ export default Component.extend({
this.get('componentAtoms').removeObject(atom);
}
};
editorOptions.cardOptions = assign(componentHooks, editorOptions.cardOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylenathan we're not convinced that merging user cardOptions over the editor built-in hooks is best. Really the built-in hooks should not need to be stomped, and if they are stomped we will end up with a mess.

At some point we should make the ADD_CARD_HOOK constant into a symbol, since we don't want the property name to be guessable by the outside world, and don't want it to conflict either.

This can just be reversed though, and the componentHooks can win. I'm going to make that change and land this 🎉

editor = new Editor(editorOptions);
editor.willRender(() => {
// The editor's render/rerender will happen after this `editor.willRender`,
Expand Down
2 changes: 2 additions & 0 deletions addon/components/mobiledoc-editor/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
payload=card.payload
data=card.payload
env=card.env
options=card.options
editCard=(action card.env.edit)
saveCard=(action card.env.save)
cancelCard=(action card.env.cancel)
Expand All @@ -48,6 +49,7 @@
atomName=atom.atomName
payload=atom.payload
value=atom.value
options=atom.options
saveAtom=(action atom.callbacks.save)
}}
{{/ember-wormhole}}
Expand Down
46 changes: 44 additions & 2 deletions tests/integration/components/mobiledoc-editor/component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import wait from 'ember-test-helpers/wait';

let { Component } = Ember;

const COMPONENT_CARD_EXPECTED_PROPS = ['env', 'editCard', 'saveCard', 'cancelCard', 'removeCard', 'postModel'];
const COMPONENT_CARD_EXPECTED_PROPS = ['env', 'editCard', 'saveCard', 'cancelCard', 'removeCard', 'postModel', 'options'];

const COMPONENT_ATOM_EXPECTED_PROPS = ['saveAtom'];
const COMPONENT_ATOM_EXPECTED_PROPS = ['saveAtom', 'options'];

moduleForComponent('mobiledoc-editor', 'Integration | Component | mobiledoc editor', {
integration: true,
Expand Down Expand Up @@ -579,6 +579,48 @@ test(`sets ${COMPONENT_CARD_EXPECTED_PROPS.join(',')} properties on card compone
`);
});

test(`passes options through to card components`, function(assert) {

let cardOptions = {
foo: 'bar'
};
let Component = Ember.Component.extend({
didInsertElement() {
assert.equal(this.get('options.foo'), 'bar', `options property has been passed`);
}
});
let card = this.registerCardComponent('demo-card', hbs`<div id='demo-card'></div>`, Component);
this.set('cards', [card]);
this.set('mobiledoc', mobiledocWithCard('demo-card'));
this.set('cardOptions', cardOptions);

this.render(hbs`
{{#mobiledoc-editor mobiledoc=mobiledoc cards=cards cardOptions=cardOptions as |editor|}}
{{/mobiledoc-editor}}
`);
});

test(`passes options through to atom components`, function(assert) {

let cardOptions = {
foo: 'bar'
};
let Component = Ember.Component.extend({
didInsertElement() {
assert.equal(this.get('options.foo'), 'bar', `options property has been passed`);
}
});
let atom = this.registerAtomComponent('demo-atom', hbs`I AM AN ATOM`, Component);
this.set('atoms', [atom]);
this.set('mobiledoc', mobiledocWithAtom('demo-atom'));
this.set('cardOptions', cardOptions);

this.render(hbs`
{{#mobiledoc-editor mobiledoc=mobiledoc atoms=atoms cardOptions=cardOptions as |editor|}}
{{/mobiledoc-editor}}
`);
});

test('component card `env` property exposes `isInEditor`', function(assert) {
assert.expect(1);

Expand Down