draft-js-code
draft-js-code
is a collection of low-level utilities to make code block editing in DraftJS editors nicer.
It works well with draft-js-prism
or draft-js-prism-plugin
.
Demo: samypesse.github.io/draft-js-code/
Features
- Indent with TAB
- Insert new line with correct indentation with ENTER
- Remove indentation with DELETE
- Remove indentation with SHIFT+TAB (#6)
- Handle input of pair characters like
()
,[]
,{}
,""
, etc. (#3)
Installation
$ npm install draft-js-code --save
API
CodeUtils.hasSelectionInBlock(editorState)
Returns true if user is editing a code block. You should call this method to encapsulate all other methods when limiting code edition behaviour to code-block
.
CodeUtils.handleKeyCommand(editorState, command)
Handle key command for code blocks, returns a new EditorState
or null
.
CodeUtils.onTab(e, editorState)
Handle user pressing tab, to insert indentation, it returns a new EditorState
.
CodeUtils.handleReturn(e, editorState)
Handle user pressing return, to insert a new line inside the code block, it returns a new EditorState
.
Usage
;;; Component { superprops; thisstate = editorState: DraftEditorState ; } { this } { const editorState = thisstate; let newState; if CodeUtils newState = CodeUtils; if !newState newState = RichUtils; if newState this; return 'handled'; return 'not-handled'; } { const editorState = thisstate; if !CodeUtils return Draft; const command = CodeUtils; return command || Draft; } { const editorState = thisstate; if !CodeUtils return 'not-handled'; this; return 'handled'; } { const editorState = thisstate; if !CodeUtils return 'not-handled'; this; return 'handled'; } { return <DraftEditor editorState=thisstateeditorState onChange=thisonChange keyBindingFn=thiskeyBindingFn handleKeyCommand=thishandleKeyCommand handleReturn=thishandleReturn onTab=thisonTab /> ; }