Skip to content

Commit 9eb0e1b

Browse files
committed
Test case for returning key at the end of line inside code block
1 parent 0d0abbb commit 9eb0e1b

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

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)