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

Commit 142e66d

Browse files
authored
Merge pull request #22 from Rosey/fix_pasting_selected_text
Fix DraftJS error when pasting over non-collapsed selection
2 parents 2ad0547 + 32f18f8 commit 142e66d

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/__test__/plugin-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
314314
subject = () => plugin.handlePastedText(pastedText, html, store);
315315
});
316316
[
317-
'addText',
317+
'replaceText',
318318
'addEmptyBlock',
319319
'handleBlockType',
320320
'handleImage',
@@ -342,7 +342,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
342342
describe('pasted just text', () => {
343343
beforeEach(() => {
344344
pastedText = 'hello';
345-
createMarkdownShortcutsPlugin.__Rewire__('addText', modifierSpy); // eslint-disable-line no-underscore-dangle
345+
createMarkdownShortcutsPlugin.__Rewire__('replaceText', modifierSpy); // eslint-disable-line no-underscore-dangle
346346
});
347347
it('returns handled', () => {
348348
expect(subject()).to.equal('handled');

src/__test__/utils-test.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { expect } from 'chai';
2-
import Draft, { EditorState } from 'draft-js';
3-
import { addText, addEmptyBlock } from '../utils';
2+
import Draft, { EditorState, SelectionState } from 'draft-js';
3+
import { addText, replaceText, addEmptyBlock } from '../utils';
44

55
describe('utils test', () => {
66
it('is loaded', () => {
77
expect(addText).to.be.a('function');
8+
expect(replaceText).to.be.a('function');
89
expect(addEmptyBlock).to.be.a('function');
910
});
1011

@@ -45,4 +46,26 @@ describe('utils test', () => {
4546
const lastBlock = currentContent.getLastBlock();
4647
expect(lastBlock.getText()).to.equal(randomText);
4748
});
49+
50+
it('should replaceText', () => {
51+
let newEditorState = EditorState.createWithContent(Draft.convertFromRaw(newRawContentState));
52+
const randomText = Date.now().toString(32);
53+
let currentContent = newEditorState.getCurrentContent();
54+
let lastBlock = currentContent.getLastBlock();
55+
const newSelection = new SelectionState({
56+
anchorKey: lastBlock.getKey(),
57+
anchorOffset: 0,
58+
focusKey: lastBlock.getKey(),
59+
focusOffset: lastBlock.getText().length
60+
});
61+
newEditorState = EditorState.forceSelection(newEditorState, newSelection);
62+
63+
newEditorState = replaceText(newEditorState, randomText);
64+
currentContent = newEditorState.getCurrentContent();
65+
expect(currentContent.hasText()).to.equal(true);
66+
lastBlock = currentContent.getLastBlock();
67+
expect(lastBlock.getText()).to.equal(randomText);
68+
const firstBlock = currentContent.getFirstBlock();
69+
expect(firstBlock.getText()).to.equal(randomText);
70+
});
4871
});

src/index.js

Lines changed: 4 additions & 4 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 { addText, addEmptyBlock } from './utils';
19+
import { replaceText, addEmptyBlock } from './utils';
2020

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

@@ -140,16 +140,16 @@ const createMarkdownShortcutsPlugin = (config = {}) => {
140140
let buffer = [];
141141
for (let i = 0; i < text.length; i++) { // eslint-disable-line no-plusplus
142142
if (INLINE_STYLE_CHARACTERS.indexOf(text[i]) >= 0) {
143-
newEditorState = addText(newEditorState, buffer.join('') + text[i]);
143+
newEditorState = replaceText(newEditorState, buffer.join('') + text[i]);
144144
newEditorState = checkCharacterForState(newEditorState, text[i]);
145145
buffer = [];
146146
} else if (text[i].charCodeAt(0) === 10) {
147-
newEditorState = addText(newEditorState, buffer.join(''));
147+
newEditorState = replaceText(newEditorState, buffer.join(''));
148148
newEditorState = addEmptyBlock(newEditorState);
149149
newEditorState = checkReturnForState(newEditorState, {});
150150
buffer = [];
151151
} else if (i === text.length - 1) {
152-
newEditorState = addText(newEditorState, buffer.join('') + text[i]);
152+
newEditorState = replaceText(newEditorState, buffer.join('') + text[i]);
153153
buffer = [];
154154
} else {
155155
buffer.push(text[i]);

src/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export function addText(editorState, bufferText) {
1414
return EditorState.push(editorState, contentState, 'insert-characters');
1515
}
1616

17+
export function replaceText(editorState, bufferText) {
18+
const contentState = Modifier.replaceText(editorState.getCurrentContent(), editorState.getSelection(), bufferText);
19+
return EditorState.push(editorState, contentState, 'insert-characters');
20+
}
21+
1722
export function addEmptyBlock(editorState) {
1823
let contentState = editorState.getCurrentContent();
1924
const emptyBlock = getEmptyContentBlock();

0 commit comments

Comments
 (0)