* Handle pressing tab in the editor * * @param {SyntheticKeyboardEvent} event * @param {Draft.EditorState} editorState * @return {Draft.EditorState}
(e, editorState)
| 11 | * @return {Draft.EditorState} |
| 12 | */ |
| 13 | function onTab(e, editorState) { |
| 14 | e.preventDefault(); |
| 15 | |
| 16 | var contentState = editorState.getCurrentContent(); |
| 17 | var selection = editorState.getSelection(); |
| 18 | var startKey = selection.getStartKey(); |
| 19 | var currentBlock = contentState.getBlockForKey(startKey); |
| 20 | |
| 21 | var indentation = getIndentation(currentBlock.getText()); |
| 22 | var newContentState; |
| 23 | |
| 24 | if (selection.isCollapsed()) { |
| 25 | newContentState = Draft.Modifier.insertText( |
| 26 | contentState, |
| 27 | selection, |
| 28 | indentation |
| 29 | ); |
| 30 | } else { |
| 31 | newContentState = Draft.Modifier.replaceText( |
| 32 | contentState, |
| 33 | selection, |
| 34 | indentation |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | return Draft.EditorState.push( |
| 39 | editorState, |
| 40 | newContentState, |
| 41 | 'insert-characters' |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | module.exports = onTab; |
no test coverage detected