Skip to content

Commit

Permalink
draft-js-code plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ngs committed Dec 8, 2016
1 parent 77386e4 commit 269a6d8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions demo/client/components/DemoEditor/index.js
Expand Up @@ -8,12 +8,15 @@ import Draft, {
ContentState,
EditorState,
} from 'draft-js';

import styles from './styles.css';
import prismPlugin from '../../plugins/prism';
import codePlugin from '../../plugins/code';

window.Draft = Draft;

const plugins = [
codePlugin,
prismPlugin,
createMarkdownShortcutsPlugin()
];
Expand Down
43 changes: 43 additions & 0 deletions demo/client/plugins/code.js
@@ -0,0 +1,43 @@
import CodeUtils from 'draft-js-code';

const codePlugin = {
handleKeyCommand(command, { getEditorState, setEditorState }) {
const editorState = getEditorState();
let newEditorState = editorState;
if (CodeUtils.hasSelectionInBlock(editorState)) {
newEditorState = CodeUtils.handleKeyCommand(editorState, command);
}
if (newEditorState && newEditorState !== editorState) {
setEditorState(newEditorState);
return 'handled';
}
return 'not-handled';
},
keyBindingFn(e, { getEditorState }) {
const editorState = getEditorState();
if (CodeUtils.hasSelectionInBlock(editorState)) {
return CodeUtils.getKeyBinding(e);
}
return null;
},
handleReturn(e, { getEditorState, setEditorState }) {
const editorState = getEditorState();
console.info(CodeUtils.hasSelectionInBlock(editorState));
// if (CodeUtils.hasSelectionInBlock(editorState)) {
// setEditorState(CodeUtils.handleReturn(e, editorState));
// return 'handled';
// }
return 'not-handled';
},
handleTab(e, { getEditorState, setEditorState }) {
const editorState = getEditorState();
console.info(CodeUtils.hasSelectionInBlock(editorState));
// if (CodeUtils.hasSelectionInBlock(editorState)) {
// setEditorState(CodeUtils.handleTab(e, editorState));
// return 'handled';
// }
return 'not-handled';
}
};

export default codePlugin;
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -61,6 +61,7 @@
"css-loader": "^0.26.0",
"css-modules-require-hook": "^4.0.5",
"dirty-chai": "^1.2.2",
"draft-js-code": "^0.2.0",
"draft-js-plugins-editor": "2.0.0-beta7",
"draft-js-prism": "ngs/draft-js-prism#6edb31c3805dd1de3fb897cc27fced6bac1bafbb",
"enzyme": "^2.6.0",
Expand Down

0 comments on commit 269a6d8

Please sign in to comment.