-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support Pasting markdown * Fix single line paste * Reset buffer using assignment * Move eslint rules to eslintrc * Revert eslint config * Added Test case for utils * Specify disable rule * Added test case for handlePastedText * remove redundant check
- Loading branch information
Showing
4 changed files
with
202 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { expect } from 'chai'; | ||
import Draft, { EditorState } from 'draft-js'; | ||
import { addText, addEmptyBlock } from '../utils'; | ||
|
||
describe('utils test', () => { | ||
it('is loaded', () => { | ||
expect(addText).to.be.a('function'); | ||
expect(addEmptyBlock).to.be.a('function'); | ||
}); | ||
|
||
const newRawContentState = { | ||
entityMap: {}, | ||
blocks: [{ | ||
key: 'item1', | ||
text: 'altered!!', | ||
type: 'unstyled', | ||
depth: 0, | ||
inlineStyleRanges: [], | ||
entityRanges: [], | ||
data: {} | ||
}] | ||
}; | ||
it('should add empty block', () => { | ||
let newEditorState = EditorState.createWithContent(Draft.convertFromRaw(newRawContentState)); | ||
const initialBlockSize = newEditorState.getCurrentContent().getBlockMap().size; | ||
const randomBlockSize = Math.floor((Math.random() * 50) + 1); // random number bettween 1 to 50 | ||
for (let i = 0; i < randomBlockSize; i++) { // eslint-disable-line no-plusplus | ||
newEditorState = addEmptyBlock(newEditorState); | ||
} | ||
const finalBlockSize = newEditorState.getCurrentContent().getBlockMap().size; | ||
expect(finalBlockSize - initialBlockSize).to.equal(randomBlockSize); | ||
|
||
const lastBlock = newEditorState.getCurrentContent().getLastBlock(); | ||
expect(lastBlock.getType()).to.equal('unstyled'); | ||
expect(lastBlock.getText()).to.have.lengthOf(0); | ||
}); | ||
|
||
it('should addText', () => { | ||
let newEditorState = EditorState.createWithContent(Draft.convertFromRaw(newRawContentState)); | ||
const randomText = Date.now().toString(32); | ||
newEditorState = addEmptyBlock(newEditorState); | ||
newEditorState = addText(newEditorState, randomText); | ||
const currentContent = newEditorState.getCurrentContent(); | ||
expect(currentContent.hasText()).to.equal(true); | ||
const lastBlock = currentContent.getLastBlock(); | ||
expect(lastBlock.getText()).to.equal(randomText); | ||
}); | ||
}); |
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,32 @@ | ||
import { genKey, ContentBlock, Modifier, EditorState } from 'draft-js'; | ||
import { List } from 'immutable'; | ||
|
||
function getEmptyContentBlock() { | ||
return new ContentBlock({ | ||
key: genKey(), | ||
text: '', | ||
characterList: List(), | ||
}); | ||
} | ||
|
||
export function addText(editorState, bufferText) { | ||
const contentState = Modifier.insertText(editorState.getCurrentContent(), editorState.getSelection(), bufferText); | ||
return EditorState.push(editorState, contentState, 'insert-characters'); | ||
} | ||
|
||
export function addEmptyBlock(editorState) { | ||
let contentState = editorState.getCurrentContent(); | ||
const emptyBlock = getEmptyContentBlock(); | ||
const blockMap = contentState.getBlockMap(); | ||
const selectionState = editorState.getSelection(); | ||
contentState = contentState.merge({ | ||
blockMap: blockMap.set(emptyBlock.getKey(), emptyBlock), | ||
selectionAfter: selectionState.merge({ | ||
anchorKey: emptyBlock.getKey(), | ||
focusKey: emptyBlock.getKey(), | ||
anchorOffset: 0, | ||
focusOffset: 0, | ||
}), | ||
}); | ||
return EditorState.push(editorState, contentState, 'insert-characters'); | ||
} |