Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #27 from ckeditor/t/26
Browse files Browse the repository at this point in the history
Feature: Disable pasting and cutting when an editor is read-only. Closes #26.
  • Loading branch information
Piotr Jasiun committed Jul 18, 2017
2 parents 5888743 + 7062474 commit 0ba74d5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand Down Expand Up @@ -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 ) {
Expand Down
33 changes: 33 additions & 0 deletions tests/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0ba74d5

Please sign in to comment.