Skip to content package
draft-js-code v0.1.0 Collection of utilities to make code blocks edition easy in DraftJS
node any 5 deps cjs open source
overview readme
license Apache-2.0
deps 5
unpacked Unknown
files Unknown
node any $ npm install draft-js-code@0.1.0n npmREADME GitHub-flavored package documentation.
downloads Across all versions.
Download trend unavailable.
compatibility Runtime metadata.
versions Latest published Oct 23, 2017
maintainers Registry package owners.
contents 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
Installation
$ npm install draft-js-code --save copy
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
import React from 'react' ;
import Draft from 'draft-js' ;
import CodeUtils from 'draft-js-code' ;
class Editor extends React .Component {
constructor (props) {
super (props);
this .state = {
editorState: Draft.EditorState. createEmpty ()
};
}
onChange = (editorState) => {
this . setState ({
editorState
})
}
handleKeyCommand = (command) => {
install size Estimated unpacked size across 5 packages.
734.2 kB
Includes dependencies.
dependencies 4 direct · 4 outdated
samypesse
samypesse@gmail.com
const
{ editorState }
=
this
.state;
let newState;
if (CodeUtils. hasSelectionInBlock (editorState)) {
newState = CodeUtils. handleKeyCommand (editorState, command);
}
if ( ! newState) {
newState = RichUtils. handleKeyCommand (editorState, command);
}
if (newState) {
this . onChange (newState);
return 'handled' ;
}
return 'not-handled' ;
}
keyBindingFn = (evt) => {
const { editorState } = this .state;
if ( ! CodeUtils. hasSelectionInBlock (editorState)) return Draft. getDefaultKeyBinding (evt);
const command = CodeUtils. getKeyBinding (evt);
return command || Draft. getDefaultKeyBinding (evt);
}
handleReturn = (evt) => {
const { editorState } = this .state;
if ( ! CodeUtils. hasSelectionInBlock (editorState)) return 'not-handled' ;
this . onChange (CodeUtils. handleReturn (evt, editorState));
return 'handled' ;
}
onTab = (evt) => {
const { editorState } = this .state;
if ( ! CodeUtils. hasSelectionInBlock (editorState)) return 'not-handled' ;
this . onChange (CodeUtils. onTab (evt, editorState));
return 'handled' ;
}
render () {
return (
< Draft.Editor
editorState={this .state.editorState }
onChange={this .onChange }
keyBindingFn={this .keyBindingFn }
handleKeyCommand={this .handleKeyCommand }
handleReturn={this .handleReturn }
onTab={this .onTab }
/>
);
}
}
copy