Skip to content

Commit fdc3389

Browse files
authored
Merge pull request #5 from ngs/fix-prism
Fix code block handling return
2 parents 0d0abbb + 798b9a7 commit fdc3389

File tree

6 files changed

+73
-46
lines changed

6 files changed

+73
-46
lines changed

demo/client/components/DemoEditor/index.js

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
import React, { Component } from 'react';
22
import Editor from 'draft-js-plugins-editor';
3-
import PrismDecorator from 'draft-js-prism';
4-
import Prism from 'prismjs';
5-
import 'prismjs/components/prism-java';
6-
import 'prismjs/components/prism-scala';
7-
import 'prismjs/components/prism-go';
8-
import 'prismjs/components/prism-sql';
9-
import 'prismjs/components/prism-bash';
10-
import 'prismjs/components/prism-c';
11-
import 'prismjs/components/prism-cpp';
12-
import 'prismjs/components/prism-kotlin';
13-
import 'prismjs/components/prism-perl';
14-
import 'prismjs/components/prism-ruby';
15-
import 'prismjs/components/prism-swift';
163

174
import createMarkdownShortcutsPlugin from 'draft-js-markdown-shortcuts-plugin'; // eslint-disable-line
185
import Draft, {
@@ -22,29 +9,15 @@ import Draft, {
229
EditorState,
2310
} from 'draft-js';
2411
import styles from './styles.css';
25-
// import initialState from './initialState';
12+
import prismPlugin from '../../plugins/prism';
2613

2714
window.Draft = Draft;
2815

29-
const plugins = [createMarkdownShortcutsPlugin()];
30-
31-
const decorators = [
32-
new PrismDecorator({
33-
prism: Prism,
34-
getSyntax(block) {
35-
const language = block.getData().get('language');
36-
if (typeof Prism.languages[language] === 'object') {
37-
return language;
38-
}
39-
return null;
40-
},
41-
render({ type, children }) {
42-
return <span className={`prism-token token ${type}`}>{children}</span>;
43-
}
44-
})
16+
const plugins = [
17+
prismPlugin,
18+
createMarkdownShortcutsPlugin()
4519
];
4620

47-
// const contentState = ContentState.createFromBlockArray(convertFromRaw(initialState));
4821
const contentState = ContentState.createFromText('');
4922
const initialEditorState = EditorState.createWithContent(contentState);
5023

@@ -78,7 +51,6 @@ export default class DemoEditor extends Component {
7851
{placeholder}
7952
<div className={styles.editor} onClick={this.focus}>
8053
<Editor
81-
decorators={decorators}
8254
editorState={editorState}
8355
onChange={this.onChange}
8456
plugins={plugins}

demo/client/plugins/prism.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import Prism from 'prismjs';
3+
import PrismDecorator from 'draft-js-prism';
4+
import 'prismjs/components/prism-java';
5+
import 'prismjs/components/prism-scala';
6+
import 'prismjs/components/prism-go';
7+
import 'prismjs/components/prism-sql';
8+
import 'prismjs/components/prism-bash';
9+
import 'prismjs/components/prism-c';
10+
import 'prismjs/components/prism-cpp';
11+
import 'prismjs/components/prism-kotlin';
12+
import 'prismjs/components/prism-perl';
13+
import 'prismjs/components/prism-ruby';
14+
import 'prismjs/components/prism-swift';
15+
16+
const prismPlugin = {
17+
decorators: [
18+
new PrismDecorator({
19+
prism: Prism,
20+
getSyntax(block) {
21+
const language = block.getData().get('language');
22+
if (typeof Prism.languages[language] === 'object') {
23+
return language;
24+
}
25+
return null;
26+
},
27+
render({ type, children }) {
28+
return <span className={`prism-token token ${type}`}>{children}</span>;
29+
}
30+
})
31+
]
32+
};
33+
34+
export default prismPlugin;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"decorate-component-with-props": "^1.0.2",
107107
"draft-js": "^0.9.1",
108108
"draft-js-checkable-list-item": "^2.0.2",
109+
"immutable": "^3.8.1",
109110
"react": "^15.4.1",
110111
"react-dom": "^15.4.1"
111112
}

src/__test__/plugin-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
167167
entityMap: {},
168168
blocks: [{
169169
key: 'item1',
170-
text: '',
171-
type: '```',
170+
text: '```',
171+
type: 'unstyled',
172172
depth: 0,
173173
inlineStyleRanges: [],
174174
entityRanges: [],

src/modifiers/__test__/handleNewCodeBlock-test.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,17 @@ describe('handleNewCodeBlock', () => {
9696
}]
9797
};
9898
const contentState = Draft.convertFromRaw(beforeRawContentState);
99-
const selection = new SelectionState({
100-
anchorKey: 'item1',
101-
anchorOffset: 21,
102-
focusKey: 'item1',
103-
focusOffset: 21,
104-
isBackward: false,
105-
hasFocus: true
106-
});
107-
const editorState = EditorState.forceSelection(
108-
EditorState.createWithContent(contentState), selection);
10999
it('adds new line inside code block', () => {
100+
const selection = new SelectionState({
101+
anchorKey: 'item1',
102+
anchorOffset: 21,
103+
focusKey: 'item1',
104+
focusOffset: 21,
105+
isBackward: false,
106+
hasFocus: true
107+
});
108+
const editorState = EditorState.forceSelection(
109+
EditorState.createWithContent(contentState), selection);
110110
const newEditorState = handleNewCodeBlock(editorState);
111111
expect(newEditorState).not.to.equal(editorState);
112112
expect(
@@ -115,6 +115,25 @@ describe('handleNewCodeBlock', () => {
115115
afterRawContentState
116116
);
117117
});
118+
it('does not add new line even inside code block', () => {
119+
const selection = new SelectionState({
120+
anchorKey: 'item1',
121+
anchorOffset: 10,
122+
focusKey: 'item1',
123+
focusOffset: 10,
124+
isBackward: false,
125+
hasFocus: true
126+
});
127+
const editorState = EditorState.forceSelection(
128+
EditorState.createWithContent(contentState), selection);
129+
const newEditorState = handleNewCodeBlock(editorState);
130+
expect(newEditorState).to.equal(editorState);
131+
expect(
132+
Draft.convertToRaw(newEditorState.getCurrentContent())
133+
).to.deep.equal(
134+
beforeRawContentState
135+
);
136+
});
118137
});
119138

120139
describe('in unstyled block without three backquotes', () => {

src/modifiers/handleNewCodeBlock.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const handleNewCodeBlock = (editorState) => {
77
const key = selection.getStartKey();
88
const currentBlock = contentState.getBlockForKey(key);
99
const matchData = /^```([\w-]+)?$/.exec(currentBlock.getText());
10-
if (matchData && selection.getEndOffset() === currentBlock.getLength()) {
10+
const isLast = selection.getEndOffset() === currentBlock.getLength();
11+
if (matchData && isLast) {
1112
const data = {};
1213
const language = matchData[1];
1314
if (language) {
@@ -16,7 +17,7 @@ const handleNewCodeBlock = (editorState) => {
1617
return changeCurrentBlockType(editorState, 'code-block', '', data);
1718
}
1819
const type = currentBlock.getType();
19-
if (type === 'code-block') {
20+
if (type === 'code-block' && isLast) {
2021
return insertEmptyBlock(editorState, 'code-block', currentBlock.getData());
2122
}
2223
return editorState;

0 commit comments

Comments
 (0)