Skip to content

Commit

Permalink
Delete selection before applying paste
Browse files Browse the repository at this point in the history
fixes #184
  • Loading branch information
bantic committed Oct 23, 2015
1 parent b1080ae commit ee22762
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/js/editor/editor.js
Expand Up @@ -211,13 +211,17 @@ class Editor {
}
}

handleDeletion(event) {
/**
* @param {KeyEvent} event optional
* @private
*/
handleDeletion(event=null) {
const range = this.cursor.offsets;

if (this.cursor.hasSelection()) {
this.run(postEditor => postEditor.deleteRange(range));
this.cursor.moveToPosition(range.head);
} else {
} else if (event) {
const key = Key.fromEvent(event);
const nextPosition = this.run(postEditor => {
return postEditor.deleteFrom(range.head, key.direction);
Expand Down Expand Up @@ -633,24 +637,31 @@ class Editor {
}

handleCut(event) {
event.preventDefault();

this.handleCopy(event);
this.handleDeletion(event);
this.handleDeletion();
}

handleCopy(event) {
event.preventDefault();

setClipboardCopyData(event, this);
}

handlePaste(event) {
event.preventDefault();

const { head: position } = this.cursor.offsets;
if (this.cursor.hasSelection()) {
this.handleDeletion();
}

let pastedPost = parsePostFromPaste(event, this.builder);

const range = this.cursor.offsets;
let nextPosition;
this.run(postEditor => {
nextPosition = postEditor.insertPost(range.head, pastedPost);
nextPosition = postEditor.insertPost(position, pastedPost);
});

this.cursor.moveToPosition(nextPosition);
Expand Down
21 changes: 21 additions & 0 deletions tests/acceptance/editor-copy-paste-test.js
Expand Up @@ -58,6 +58,27 @@ test('can cut and then paste content', (assert) => {
assert.hasElement('#editor p:contains(abc)', 'pastes the text');
});

test('paste when text is selected replaces that text', (assert) => {
const mobiledoc = Helpers.mobiledoc.build(
({post, markupSection, marker}) => {
return post([markupSection('p', [marker('abc')])]);
});
editor = new Editor({mobiledoc});
editor.render(editorElement);

assert.hasElement('#editor p:contains(abc)', 'precond - has p');

Helpers.dom.selectText('bc', editorElement);
Helpers.dom.triggerCopyEvent(editor);

Helpers.dom.selectText('a', editorElement);

Helpers.dom.triggerPasteEvent(editor);

assert.hasElement('#editor p:contains(bcbc)',
'pastes, replacing the selection');
});

test('simple copy-paste with markup at end of section works', (assert) => {
const mobiledoc = Helpers.mobiledoc.build(
({post, markupSection, marker, markup}) => {
Expand Down

0 comments on commit ee22762

Please sign in to comment.