-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow customisation of block style function & block render map
- Loading branch information
1 parent
796f39e
commit 15a6bcb
Showing
6 changed files
with
134 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Map } from 'immutable'; | ||
import { DefaultDraftBlockRenderMap } from 'draft-js'; | ||
|
||
import { BLOCK_TYPE } from '../api/constants'; | ||
|
||
// Maximum level of nesting for unordered and ordered lists. | ||
export const MAX_LIST_NESTING = 3; | ||
// Frequency at which the save callback is triggered. | ||
export const STATE_SAVE_INTERVAL = 250; | ||
|
||
/** | ||
* Options / configuration methods for the editor. | ||
*/ | ||
export default { | ||
getBlockRenderMap(BLOCK_TYPES = []) { | ||
const renderMap = {}; | ||
|
||
renderMap[BLOCK_TYPE.BREAK] = { element: 'div' }; | ||
|
||
BLOCK_TYPES.filter(type => type.element) | ||
.forEach((type) => { | ||
renderMap[type.style] = { element: type.element }; | ||
}); | ||
|
||
return DefaultDraftBlockRenderMap.merge(Map(renderMap)); | ||
}, | ||
|
||
getBlockStyleFn(BLOCK_TYPES = []) { | ||
const blockClassNames = {}; | ||
|
||
BLOCK_TYPES.filter(type => type.className) | ||
.forEach((type) => { | ||
blockClassNames[type.style] = type.className; | ||
}); | ||
|
||
const blockStyleFn = (block) => { | ||
const type = block.getType(); | ||
|
||
return blockClassNames[type] || undefined; | ||
}; | ||
|
||
return blockStyleFn; | ||
}, | ||
}; |
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,76 @@ | ||
import { List, Repeat } from 'immutable'; | ||
import { | ||
CharacterMetadata, | ||
ContentBlock, | ||
} from 'draft-js'; | ||
|
||
import config, { MAX_LIST_NESTING, STATE_SAVE_INTERVAL } from '../api/config'; | ||
import { BLOCK_TYPE } from '../api/constants'; | ||
|
||
describe('config', () => { | ||
describe('#MAX_LIST_NESTING', () => { | ||
it('exists', () => { | ||
expect(MAX_LIST_NESTING).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('#STATE_SAVE_INTERVAL', () => { | ||
it('exists', () => { | ||
expect(STATE_SAVE_INTERVAL).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('#getBlockRenderMap', () => { | ||
it('exists', () => { | ||
expect(config.getBlockRenderMap).toBeDefined(); | ||
}); | ||
|
||
it('has break', () => { | ||
expect(config.getBlockRenderMap().get(BLOCK_TYPE.BREAK)).toEqual({ element: 'div' }); | ||
}); | ||
|
||
it('has custom block with element', () => { | ||
expect(config.getBlockRenderMap([ | ||
{ style: 'TEST', element: 'div' }, | ||
]).get('TEST')).toEqual({ element: 'div' }); | ||
}); | ||
|
||
it('no custom block without element', () => { | ||
expect(config.getBlockRenderMap([ | ||
{ style: 'TEST' }, | ||
]).get('TEST')).not.toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('#getBlockStyleFn', () => { | ||
it('exists', () => { | ||
expect(config.getBlockStyleFn).toBeDefined(); | ||
}); | ||
|
||
it('returns function', () => { | ||
expect(config.getBlockStyleFn()).toBeInstanceOf(Function); | ||
}); | ||
|
||
it('has custom block with className', () => { | ||
expect(config.getBlockStyleFn([ | ||
{ style: 'TEST', className: 'test-item' }, | ||
])(new ContentBlock({ | ||
key: 'test1234', | ||
type: 'TEST', | ||
text: 'This is test text', | ||
characterList: List(Repeat(CharacterMetadata.create({ entity: 'test1234' }), 'This is test text'.length)), | ||
}))).toEqual('test-item'); | ||
}); | ||
|
||
it('no custom block without className', () => { | ||
expect(config.getBlockStyleFn([ | ||
{ style: 'TEST' }, | ||
])(new ContentBlock({ | ||
key: 'test1234', | ||
type: 'TEST', | ||
text: 'This is test text', | ||
characterList: List(Repeat(CharacterMetadata.create({ entity: 'test1234' }), 'This is test text'.length)), | ||
}))).not.toBeDefined(); | ||
}); | ||
}); | ||
}); |
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 was deleted.
Oops, something went wrong.