8
0
Просмотр исходного кода

Disabled cutting content when editor is readolny.

Oskar Wróbel 8 лет назад
Родитель
Сommit
74f123bf0d

+ 9 - 1
packages/ckeditor5-clipboard/src/clipboard.js

@@ -181,7 +181,15 @@ export default class Clipboard extends Plugin {
 		}
 		}
 
 
 		this.listenTo( editingView, 'copy', onCopyCut, { priority: 'low' } );
 		this.listenTo( editingView, 'copy', onCopyCut, { priority: 'low' } );
-		this.listenTo( editingView, 'cut', onCopyCut, { priority: 'low' } );
+		this.listenTo( editingView, 'cut', ( evt, data ) => {
+			// Cutting is disabled when editor is read-only.
+			// See: https://github.com/ckeditor/ckeditor5-clipboard/issues/26.
+			if ( editor.isReadOnly ) {
+				data.preventDefault();
+			} else {
+				onCopyCut( evt, data );
+			}
+		}, { priority: 'low' } );
 
 
 		this.listenTo( editingView, 'clipboardOutput', ( evt, data ) => {
 		this.listenTo( editingView, 'clipboardOutput', ( evt, data ) => {
 			if ( !data.content.isEmpty ) {
 			if ( !data.content.isEmpty ) {

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

@@ -283,6 +283,25 @@ describe( 'Clipboard feature', () => {
 			} );
 			} );
 		} );
 		} );
 
 
+		it( 'not fires clipboardOutput and preventDefault event for cut when editor is read-only', () => {
+			const dataTransferMock = createDataTransfer();
+			const preventDefaultSpy = sinon.spy();
+			const spy = sinon.spy();
+
+			setModelData( editor.document, '<paragraph>a[bc</paragraph><paragraph>de]f</paragraph>' );
+			editor.isReadOnly = true;
+
+			editingView.on( 'clipboardOutput', spy );
+
+			editingView.fire( 'cut', {
+				dataTransfer: dataTransferMock,
+				preventDefault: preventDefaultSpy
+			} );
+
+			sinon.assert.notCalled( spy );
+			sinon.assert.calledOnce( preventDefaultSpy );
+		} );
+
 		it( 'uses low priority observer for the copy event', () => {
 		it( 'uses low priority observer for the copy event', () => {
 			const dataTransferMock = createDataTransfer();
 			const dataTransferMock = createDataTransfer();
 			const spy = sinon.spy();
 			const spy = sinon.spy();