-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7886 from ckeditor/i/7799
Feature: Introduced the `PastePlainText` feature that detects pasting with <kbd>ctrl/cmd</kbd> + <kbd>shift</kbd> + <kbd>ctrl/v</kbd> keystroke. Closes #7799.
- Loading branch information
Showing
4 changed files
with
268 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* @module clipboard/clipboard | ||
*/ | ||
|
||
import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; | ||
|
||
import ClipboardObserver from './clipboardobserver'; | ||
|
||
/** | ||
* The plugin detects user intentions for pasting plain text. | ||
* | ||
* For example, it detects <kbd>ctrl/cmd</kbd> + <kbd>shift</kbd> + <kbd>ctrl/v</kbd> keystroke. | ||
* | ||
* @extends module:core/plugin~Plugin | ||
*/ | ||
export default class PastePlainText extends Plugin { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
static get pluginName() { | ||
return 'PastePlainText'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
init() { | ||
const view = this.editor.editing.view; | ||
const viewDocument = view.document; | ||
let shiftPressed = false; | ||
|
||
view.addObserver( ClipboardObserver ); | ||
|
||
this.listenTo( viewDocument, 'keydown', ( evt, data ) => { | ||
shiftPressed = data.shiftKey; | ||
} ); | ||
|
||
this.listenTo( viewDocument, 'clipboardInput', ( evt, data ) => { | ||
if ( shiftPressed ) { | ||
data.asPlainText = true; | ||
} | ||
}, { priority: 'high' } ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor'; | ||
|
||
import PastePlainText from '../src/pasteplaintext'; | ||
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; | ||
|
||
import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard'; | ||
|
||
/* global document */ | ||
|
||
describe( 'PastePlainText', () => { | ||
let editor, viewDocument; | ||
|
||
beforeEach( () => { | ||
return VirtualTestEditor | ||
.create( { | ||
plugins: [ PastePlainText, Paragraph ] | ||
} ) | ||
.then( newEditor => { | ||
editor = newEditor; | ||
viewDocument = editor.editing.view.document; | ||
} ); | ||
} ); | ||
|
||
it( 'marks clipboard input as plain text with shift pressed', () => { | ||
const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } ); | ||
|
||
viewDocument.on( 'clipboardInput', ( event, data ) => { | ||
expect( data.asPlainText ).to.be.true; | ||
|
||
// No need for further execution. | ||
event.stop(); | ||
} ); | ||
|
||
viewDocument.fire( 'keydown', { | ||
keyCode: getCode( 'v' ), | ||
shiftKey: true, | ||
ctrlKey: true, | ||
preventDefault: () => {}, | ||
domTarget: document.body | ||
} ); | ||
|
||
viewDocument.fire( 'clipboardInput', { | ||
dataTransfer: dataTransferMock | ||
} ); | ||
} ); | ||
|
||
it( 'ignores clipboard input as plain text when shift was released', () => { | ||
const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } ); | ||
|
||
viewDocument.on( 'clipboardInput', ( event, data ) => { | ||
expect( data.asPlainText ).to.be.undefined; | ||
|
||
// No need for further execution. | ||
event.stop(); | ||
} ); | ||
|
||
viewDocument.fire( 'keydown', { | ||
keyCode: getCode( 'a' ), | ||
shiftKey: true, | ||
preventDefault: () => {}, | ||
domTarget: document.body | ||
} ); | ||
|
||
viewDocument.fire( 'keyup', { | ||
keyCode: getCode( 'a' ), | ||
shiftKey: true, | ||
preventDefault: () => {}, | ||
domTarget: document.body | ||
} ); | ||
|
||
viewDocument.fire( 'keydown', { | ||
keyCode: getCode( 'v' ), | ||
shiftKey: false, | ||
ctrlKey: true, | ||
preventDefault: () => {}, | ||
domTarget: document.body | ||
} ); | ||
|
||
viewDocument.fire( 'clipboardInput', { | ||
dataTransfer: dataTransferMock | ||
} ); | ||
} ); | ||
|
||
function createDataTransfer( data ) { | ||
return { | ||
getData( type ) { | ||
return data[ type ]; | ||
} | ||
}; | ||
} | ||
} ); |