Skip to content

Commit a006366

Browse files
committed
[FEATURE] holding shift while pasting pastes plain text
fixes #334
1 parent 734b334 commit a006366

File tree

6 files changed

+67
-5
lines changed

6 files changed

+67
-5
lines changed

src/js/editor/event-manager.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default class EventManager {
2020
constructor(editor) {
2121
this.editor = editor;
2222
this._listeners = [];
23+
this.isShift = false;
2324
}
2425

2526
init() {
@@ -102,6 +103,11 @@ export default class EventManager {
102103
if (!editor.isEditable) {
103104
return;
104105
}
106+
let key = Key.fromEvent(event);
107+
if (key.isShiftKey()) {
108+
this.isShift = true;
109+
}
110+
105111
if (editor.handleKeyCommand(event)) {
106112
return;
107113
}
@@ -110,7 +116,6 @@ export default class EventManager {
110116
editor._insertEmptyMarkupSectionAtCursor();
111117
}
112118

113-
let key = Key.fromEvent(event);
114119
let range = editor.range;
115120

116121
switch(true) {
@@ -139,7 +144,12 @@ export default class EventManager {
139144
}
140145
}
141146

142-
keyup(/* event */) {
147+
keyup(event) {
148+
let key = Key.fromEvent(event);
149+
if (key.isShiftKey()) {
150+
this.isShift = false;
151+
}
152+
143153
setTimeout(() => this.editor._resetRange(), 0);
144154
}
145155

@@ -175,7 +185,8 @@ export default class EventManager {
175185
editor.handleDeletion();
176186
}
177187
let position = editor.range.head;
178-
let pastedPost = parsePostFromPaste(event, editor);
188+
let targetFormat = this.isShift ? 'text' : 'html';
189+
let pastedPost = parsePostFromPaste(event, editor, {targetFormat});
179190

180191
editor.run(postEditor => {
181192
let nextPosition = postEditor.insertPost(position, pastedPost);

src/js/utils/key.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,28 @@ const Key = class Key {
131131
return this.keyCode === Keycodes.ENTER;
132132
}
133133

134+
/**
135+
* If the shift key is depressed.
136+
* For example, while holding down meta+shift, pressing the "v"
137+
* key would result in an event whose `Key` had `isShift()` with a truthy value,
138+
* because the shift key is down when pressing the "v".
139+
* @see {isShiftKey} which checks if the key is actually the shift key itself.
140+
* @return {bool}
141+
*/
134142
isShift() {
135143
return this.shiftKey;
136144
}
137145

146+
/*
147+
* If the key is the actual shift key. This is false when the shift key
148+
* is held down and the source `event` is not the shift key.
149+
* @see {isShift}
150+
* @return {bool}
151+
*/
152+
isShiftKey() {
153+
return this.keyCode === Keycodes.SHIFT;
154+
}
155+
138156
hasModifier(modifier) {
139157
return modifier & this.modifierMask;
140158
}

src/js/utils/keycodes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export default {
22
BACKSPACE: 8,
33
SPACE: 32,
44
ENTER: 13,
5+
SHIFT: 16,
56
ESC: 27,
67
DELETE: 46,
78
'0': 48,

src/js/utils/parse-utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ export function setClipboardData(event, {mobiledoc, html, text}, window) {
106106
* @param {{builder: Builder, _parserPlugins: Array}} options
107107
* @return {Post}
108108
*/
109-
export function parsePostFromPaste(pasteEvent, {builder, _parserPlugins: plugins}) {
109+
export function parsePostFromPaste(pasteEvent, {builder, _parserPlugins: plugins}, {targetFormat}={targetFormat:'html'}) {
110110
let { html, text } = getContentFromPasteEvent(pasteEvent, window);
111111

112-
if (html && html.length) {
112+
if (targetFormat === 'html' && html && html.length) {
113113
return parsePostFromHTML(html, builder, plugins);
114114
} else if (text && text.length) {
115115
return parsePostFromText(text, builder, plugins);

tests/acceptance/editor-copy-paste-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
MIME_TEXT_PLAIN,
66
MIME_TEXT_HTML
77
} from 'mobiledoc-kit/utils/parse-utils';
8+
import Keycodes from 'mobiledoc-kit/utils/keycodes';
89

910
const { module, test } = Helpers;
1011

@@ -373,3 +374,28 @@ test('pasting when replacing a list item works', (assert) => {
373374
assert.hasElement('#editor li:contains(X)', 'replaces Y with X in li');
374375
assert.hasNoElement('#editor li:contains(Y)', 'li with Y is gone');
375376
});
377+
378+
test('paste with shift key pastes plain text', (assert) => {
379+
let expected;
380+
editor = Helpers.mobiledoc.renderInto(editorElement, ({post, markupSection, marker, markup}) => {
381+
expected = post([
382+
markupSection('p', [
383+
marker('a'), marker('b', [markup('b')]), marker('cabc')
384+
])
385+
]);
386+
return post([
387+
markupSection('p', [
388+
marker('a'), marker('b', [markup('b')]), marker('c')
389+
])
390+
]);
391+
});
392+
393+
editor.selectRange(new Range(editor.post.headPosition(), editor.post.tailPosition()));
394+
Helpers.dom.triggerCopyEvent(editor);
395+
editor.selectRange(new Range(editor.post.tailPosition()));
396+
397+
Helpers.dom.triggerKeyEvent(editor, 'keydown', { keyCode: Keycodes.SHIFT });
398+
Helpers.dom.triggerPasteEvent(editor);
399+
400+
assert.postIsSimilar(editor.post, expected);
401+
});

tests/helpers/dom.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ function insertText(editor, string) {
224224
});
225225
}
226226

227+
function triggerKeyEvent(editor, eventName, options={}) {
228+
let event = createMockEvent(eventName, editor.element, options);
229+
_triggerEditorEvent(editor, event);
230+
}
231+
227232
// triggers a key sequence like cmd-B on the editor, to test out
228233
// registered keyCommands
229234
function triggerKeyCommand(editor, string, modifiers=[]) {
@@ -376,6 +381,7 @@ const DOMHelper = {
376381
triggerForwardDelete,
377382
triggerEnter,
378383
insertText,
384+
triggerKeyEvent,
379385
triggerKeyCommand,
380386
triggerRightArrowKey,
381387
triggerLeftArrowKey,

0 commit comments

Comments
 (0)