Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit ba70187

Browse files
authored
Merge pull request #24 from ngs/patch-1
Fix handlePasetedText behavior
2 parents 52049df + ea19227 commit ba70187

File tree

4 files changed

+19
-47
lines changed

4 files changed

+19
-47
lines changed

src/__test__/plugin-test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
315315
});
316316
[
317317
'replaceText',
318-
'addEmptyBlock',
318+
'insertEmptyBlock',
319319
'handleBlockType',
320320
'handleImage',
321321
'handleLink',
@@ -349,6 +349,15 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
349349
expect(modifierSpy).to.have.been.calledWith(currentEditorState, 'hello');
350350
});
351351
});
352+
describe('passed `html` argument', () => {
353+
beforeEach(() => {
354+
pastedText = '# hello';
355+
html = '<h1>hello</h1>';
356+
});
357+
it('returns not-handled', () => {
358+
expect(subject()).to.equal('not-handled');
359+
});
360+
});
352361
});
353362
});
354363
});

src/__test__/utils-test.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { expect } from 'chai';
22
import Draft, { EditorState, SelectionState } from 'draft-js';
3-
import { addText, replaceText, addEmptyBlock } from '../utils';
3+
import insertEmptyBlock from '../modifiers/insertEmptyBlock';
4+
import { addText, replaceText } from '../utils';
45

56
describe('utils test', () => {
67
it('is loaded', () => {
78
expect(addText).to.be.a('function');
89
expect(replaceText).to.be.a('function');
9-
expect(addEmptyBlock).to.be.a('function');
1010
});
1111

1212
const newRawContentState = {
@@ -21,25 +21,11 @@ describe('utils test', () => {
2121
data: {}
2222
}]
2323
};
24-
it('should add empty block', () => {
25-
let newEditorState = EditorState.createWithContent(Draft.convertFromRaw(newRawContentState));
26-
const initialBlockSize = newEditorState.getCurrentContent().getBlockMap().size;
27-
const randomBlockSize = Math.floor((Math.random() * 50) + 1); // random number bettween 1 to 50
28-
for (let i = 0; i < randomBlockSize; i++) { // eslint-disable-line no-plusplus
29-
newEditorState = addEmptyBlock(newEditorState);
30-
}
31-
const finalBlockSize = newEditorState.getCurrentContent().getBlockMap().size;
32-
expect(finalBlockSize - initialBlockSize).to.equal(randomBlockSize);
33-
34-
const lastBlock = newEditorState.getCurrentContent().getLastBlock();
35-
expect(lastBlock.getType()).to.equal('unstyled');
36-
expect(lastBlock.getText()).to.have.lengthOf(0);
37-
});
3824

3925
it('should addText', () => {
4026
let newEditorState = EditorState.createWithContent(Draft.convertFromRaw(newRawContentState));
4127
const randomText = Date.now().toString(32);
42-
newEditorState = addEmptyBlock(newEditorState);
28+
newEditorState = insertEmptyBlock(newEditorState);
4329
newEditorState = addText(newEditorState, randomText);
4430
const currentContent = newEditorState.getCurrentContent();
4531
expect(currentContent.hasText()).to.equal(true);

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import leaveList from './modifiers/leaveList';
1616
import insertText from './modifiers/insertText';
1717
import createLinkDecorator from './decorators/link';
1818
import createImageDecorator from './decorators/image';
19-
import { replaceText, addEmptyBlock } from './utils';
19+
import { replaceText } from './utils';
2020

2121
const INLINE_STYLE_CHARACTERS = [' ', '*', '_'];
2222

@@ -135,6 +135,9 @@ const createMarkdownShortcutsPlugin = (config = {}) => {
135135
return 'not-handled';
136136
},
137137
handlePastedText(text, html, { getEditorState, setEditorState }) {
138+
if (html) {
139+
return 'not-handled';
140+
}
138141
const editorState = getEditorState();
139142
let newEditorState = editorState;
140143
let buffer = [];
@@ -145,7 +148,7 @@ const createMarkdownShortcutsPlugin = (config = {}) => {
145148
buffer = [];
146149
} else if (text[i].charCodeAt(0) === 10) {
147150
newEditorState = replaceText(newEditorState, buffer.join(''));
148-
newEditorState = addEmptyBlock(newEditorState);
151+
newEditorState = insertEmptyBlock(newEditorState);
149152
newEditorState = checkReturnForState(newEditorState, {});
150153
buffer = [];
151154
} else if (i === text.length - 1) {

src/utils.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import { genKey, ContentBlock, Modifier, EditorState } from 'draft-js';
2-
import { List } from 'immutable';
3-
4-
function getEmptyContentBlock() {
5-
return new ContentBlock({
6-
key: genKey(),
7-
text: '',
8-
characterList: List(),
9-
});
10-
}
1+
import { Modifier, EditorState } from 'draft-js';
112

123
export function addText(editorState, bufferText) {
134
const contentState = Modifier.insertText(editorState.getCurrentContent(), editorState.getSelection(), bufferText);
@@ -18,20 +9,3 @@ export function replaceText(editorState, bufferText) {
189
const contentState = Modifier.replaceText(editorState.getCurrentContent(), editorState.getSelection(), bufferText);
1910
return EditorState.push(editorState, contentState, 'insert-characters');
2011
}
21-
22-
export function addEmptyBlock(editorState) {
23-
let contentState = editorState.getCurrentContent();
24-
const emptyBlock = getEmptyContentBlock();
25-
const blockMap = contentState.getBlockMap();
26-
const selectionState = editorState.getSelection();
27-
contentState = contentState.merge({
28-
blockMap: blockMap.set(emptyBlock.getKey(), emptyBlock),
29-
selectionAfter: selectionState.merge({
30-
anchorKey: emptyBlock.getKey(),
31-
focusKey: emptyBlock.getKey(),
32-
anchorOffset: 0,
33-
focusOffset: 0,
34-
}),
35-
});
36-
return EditorState.push(editorState, contentState, 'insert-characters');
37-
}

0 commit comments

Comments
 (0)