浏览代码

Merge pull request #27 from ckeditor/t/26

Feature: Disable pasting and cutting when an editor is read-only. Closes #26.
Piotr Jasiun 8 年之前
父节点
当前提交
2d33f60086
共有 2 个文件被更改,包括 50 次插入3 次删除
  1. 17 3
      packages/ckeditor5-clipboard/src/clipboard.js
  2. 33 0
      packages/ckeditor5-clipboard/tests/clipboard.js

+ 17 - 3
packages/ckeditor5-clipboard/src/clipboard.js

@@ -134,6 +134,12 @@ export default class Clipboard extends Plugin {
 		// The clipboard paste pipeline.
 
 		this.listenTo( editingView, 'clipboardInput', ( evt, data ) => {
+			// Pasting and dropping is disabled when editor is read-only.
+			// See: https://github.com/ckeditor/ckeditor5-clipboard/issues/26.
+			if ( editor.isReadOnly ) {
+				return;
+			}
+
 			const dataTransfer = data.dataTransfer;
 			let content = '';
 
@@ -165,17 +171,25 @@ export default class Clipboard extends Plugin {
 
 		// The clipboard copy/cut pipeline.
 
-		const onCopyCut = ( evt, data ) => {
+		function onCopyCut( evt, data ) {
 			const dataTransfer = data.dataTransfer;
 			const content = editor.data.toView( editor.data.getSelectedContent( doc.selection ) );
 
 			data.preventDefault();
 
 			editingView.fire( 'clipboardOutput', { dataTransfer, content, method: evt.name } );
-		};
+		}
 
 		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 ) => {
 			if ( !data.content.isEmpty ) {

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

@@ -174,6 +174,20 @@ describe( 'Clipboard feature', () => {
 			expect( stringifyModel( spy.args[ 0 ][ 0 ] ) ).to.equal( '<paragraph>x</paragraph>' );
 		} );
 
+		it( 'do not insert content when editor is read-only', () => {
+			const dataTransferMock = createDataTransfer( { 'text/html': '<p>x</p>', 'text/plain': 'y' } );
+			const spy = sinon.stub( editor.data, 'insertContent' );
+
+			editor.isReadOnly = true;
+
+			editingView.fire( 'paste', {
+				dataTransfer: dataTransferMock,
+				preventDefault() {}
+			} );
+
+			sinon.assert.notCalled( spy );
+		} );
+
 		it( 'converts content in an "all allowed" context', () => {
 			// It's enough if we check this here with a text node and paragraph because if the conversion was made
 			// in a normal root, then text or paragraph wouldn't be allowed here.
@@ -269,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', () => {
 			const dataTransferMock = createDataTransfer();
 			const spy = sinon.spy();