-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add editor#editCard and editor#displayCard
Cards normally render into "display" mode by default. Calling `editor#editCard` before the card is rendered will change it so that the card gets rendered in edit mode initially. If the card has already been rendered, it switches it to edit mode.
- Loading branch information
Showing
10 changed files
with
267 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,33 @@ | ||
import Section from './_section'; | ||
import { CARD_TYPE } from './types'; | ||
import { shallowCopyObject } from '../utils/copy'; | ||
|
||
export const CARD_MODES = { | ||
DISPLAY: 'display', | ||
EDIT: 'edit' | ||
}; | ||
|
||
const DEFAULT_INITIAL_MODE = CARD_MODES.DISPLAY; | ||
|
||
export default class Card extends Section { | ||
constructor(name, payload) { | ||
super(CARD_TYPE); | ||
this.name = name; | ||
this.payload = payload; | ||
this.setInitialMode(DEFAULT_INITIAL_MODE); | ||
} | ||
|
||
clone() { | ||
return this.builder.createCardSection(this.name, this.payload); | ||
const payload = shallowCopyObject(this.payload); | ||
return this.builder.createCardSection(this.name, payload); | ||
} | ||
|
||
/** | ||
* set the mode that this will be rendered into initially | ||
* @private | ||
*/ | ||
setInitialMode(initialMode) { | ||
// TODO validate initialMode | ||
this._initialMode = initialMode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function shallowCopyObject(object) { | ||
let copy = {}; | ||
Object.keys(object).forEach(key => { | ||
copy[key] = object[key]; | ||
}); | ||
return copy; | ||
} | ||
|
||
export { | ||
shallowCopyObject | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const {module, test} = QUnit; | ||
|
||
import PostNodeBuilder from 'content-kit-editor/models/post-node-builder'; | ||
|
||
let builder; | ||
module('Unit: Card', { | ||
beforeEach() { | ||
builder = new PostNodeBuilder(); | ||
}, | ||
afterEach() { | ||
builder = null; | ||
} | ||
}); | ||
|
||
test('can create a card with payload', (assert) => { | ||
const payload = {}; | ||
const card = builder.createCardSection('card-name', payload); | ||
assert.ok(!!card, 'creates card'); | ||
assert.ok(card.payload === payload, 'has payload'); | ||
}); | ||
|
||
test('cloning a card copies payload', (assert) => { | ||
const payload = {foo:'bar'}; | ||
|
||
const card = builder.createCardSection('card-name', payload); | ||
const card2 = card.clone(); | ||
|
||
assert.ok(card !== card2, 'card !== cloned'); | ||
assert.ok(card.payload !== card2.payload, 'payload is copied'); | ||
|
||
card.payload.foo = 'other foo'; | ||
assert.equal(card2.payload.foo, 'bar', 'card2 payload not updated'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Helpers from '../../test-helpers'; | ||
import { shallowCopyObject } from 'content-kit-editor/utils/copy'; | ||
|
||
const {module, test} = Helpers; | ||
|
||
module('Unit: Utils: copy'); | ||
|
||
test('#shallowCopyObject breaks references', (assert) => { | ||
let obj = {a: 1, b:'b'}; | ||
let obj2 = shallowCopyObject(obj); | ||
obj.a = 2; | ||
obj.b = 'new b'; | ||
|
||
assert.ok(obj !== obj2, 'obj !== obj2'); | ||
assert.equal(obj2.a, 1, 'obj2 "a" preserved'); | ||
assert.equal(obj2.b, 'b', 'obj2 "b" preserved'); | ||
}); | ||
|