Browse Source

Merge pull request #47 from ckeditor/t/ckeditor5-image/208

Fix: Disabled the entire clipboard input pipeline when the editor is read-only. Closes #48.
Piotrek Koszuliński 7 years ago
parent
commit
5c38882225

+ 6 - 4
packages/ckeditor5-clipboard/src/clipboard.js

@@ -134,13 +134,15 @@ export default class Clipboard extends Plugin {
 
 		// The clipboard paste pipeline.
 
-		this.listenTo( viewDocument, 'clipboardInput', ( evt, data ) => {
-			// Pasting and dropping is disabled when editor is read-only.
-			// See: https://github.com/ckeditor/ckeditor5-clipboard/issues/26.
+		// Pasting and dropping is disabled when editor is read-only.
+		// See: https://github.com/ckeditor/ckeditor5-clipboard/issues/26.
+		this.listenTo( viewDocument, 'clipboardInput', evt => {
 			if ( editor.isReadOnly ) {
-				return;
+				evt.stop();
 			}
+		}, { priority: 'highest' } );
 
+		this.listenTo( viewDocument, 'clipboardInput', ( evt, data ) => {
 			const dataTransfer = data.dataTransfer;
 			let content = '';
 

+ 23 - 0
packages/ckeditor5-clipboard/tests/clipboard.js

@@ -194,6 +194,29 @@ describe( 'Clipboard feature', () => {
 			sinon.assert.notCalled( spy );
 		} );
 
+		it( 'stops `clipboardInput` event on highest priority when editor is read-only', () => {
+			const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
+			const spy = sinon.spy();
+
+			viewDocument.on( 'clipboardInput', spy, { priority: 'high' } );
+
+			editor.isReadOnly = true;
+
+			viewDocument.fire( 'clipboardInput', {
+				dataTransfer: dataTransferMock
+			} );
+
+			sinon.assert.notCalled( spy );
+
+			editor.isReadOnly = false;
+
+			viewDocument.fire( 'clipboardInput', {
+				dataTransfer: dataTransferMock
+			} );
+
+			sinon.assert.calledOnce( spy );
+		} );
+
 		it( 'does not insert content if the whole content was invalid', () => {
 			// Whole content is invalid. Even though there is "view" content, the "model" content would be empty.
 			// Do not insert content in this case.