Skip to content

Commit

Permalink
Fix issue #256: Copy-pasting from Notepad does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoran Brondsema committed Dec 14, 2015
1 parent eaaed00 commit e251455
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/js/utils/paste-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export function parsePostFromPaste(pasteEvent, builder, plugins=[]) {

let html = pasteEvent.clipboardData.getData('text/html');

// Fallback to 'text/plain'
if (!html || html.length === 0) {
html = pasteEvent.clipboardData.getData('text/plain');
}

if (mobiledocRegex.test(html)) {
let mobiledocString = html.match(mobiledocRegex)[1];
mobiledoc = JSON.parse(mobiledocString);
Expand Down
18 changes: 18 additions & 0 deletions tests/acceptance/editor-copy-paste-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ test('simple copy-paste at end of section works', (assert) => {
assert.hasElement('#editor p:contains(abcabc)', 'pastes the text');
});

test('paste from external text source', (assert) => {
const mobiledoc = Helpers.mobiledoc.build(
({post, markupSection, marker}) => {
return post([markupSection('p', [marker('abc')])]);
});
editor = new Editor({mobiledoc});
editor.render(editorElement);

let textNode = $('#editor p')[0].childNodes[0];
assert.equal(textNode.textContent, 'abc'); //precond
Helpers.dom.moveCursorTo(textNode, textNode.length);

Helpers.dom.setCopyData('text/plain', 'abc');
Helpers.dom.triggerPasteEvent(editor);

assert.hasElement('#editor p:contains(abcabc)', 'pastes the text');
});

test('can cut and then paste content', (assert) => {
const mobiledoc = Helpers.mobiledoc.build(
({post, markupSection, marker}) => {
Expand Down
5 changes: 5 additions & 0 deletions tests/helpers/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ function getCopyData(type) {
return lastCopyData[type];
}

function setCopyData(type, value) {
lastCopyData[type] = value;
}

function fromHTML(html) {
html = $.trim(html);
let div = document.createElement('div');
Expand Down Expand Up @@ -319,6 +323,7 @@ const DOMHelper = {
triggerCutEvent,
triggerPasteEvent,
getCopyData,
setCopyData,
createMockEvent
};

Expand Down

0 comments on commit e251455

Please sign in to comment.