Skip to content

Commit

Permalink
Extended allowedActions property to allow registering non-copyable ma…
Browse files Browse the repository at this point in the history
…rkers.
  • Loading branch information
DawidKossowski committed Dec 31, 2024
1 parent d31e728 commit 2b97493
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
21 changes: 11 additions & 10 deletions packages/ckeditor5-clipboard/src/clipboardmarkersutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class ClipboardMarkersUtils extends Plugin {
*
* @internal
*/
private _markersToCopy: Map<string, ClipboardMarkerConfiguration> = new Map();
private _markersToCopy: Map<string, ClipboardMarkerConfiguration | null> = new Map();

/**
* @inheritDoc
Expand Down Expand Up @@ -228,11 +228,7 @@ export default class ClipboardMarkersUtils extends Plugin {
): void {
const before = this._markersToCopy.get( markerName );

if ( config ) {
this._markersToCopy.set( markerName, config );
} else {
this._markersToCopy.delete( markerName );
}
this._markersToCopy.set( markerName, config );

executor();

Expand All @@ -257,13 +253,18 @@ export default class ClipboardMarkersUtils extends Plugin {
return false;
}

// If there is no action provided then only presence of marker is checked.
const { allowedActions } = config;

// If `allowedActions` is set to `null` then markers should not be copied.
if ( !allowedActions ) {
return false;
}

// If there is no action provided then presence of marker is checked.
if ( !action ) {
return true;
}

const { allowedActions } = config;

return allowedActions === 'all' || allowedActions.includes( action );
}

Expand Down Expand Up @@ -649,7 +650,7 @@ export type ClipboardMarkerRestrictedAction = 'copy' | 'cut' | 'dragstart';
* @internal
*/
export type ClipboardMarkerConfiguration = {
allowedActions: NonEmptyArray<ClipboardMarkerRestrictedAction> | 'all';
allowedActions: NonEmptyArray<ClipboardMarkerRestrictedAction> | 'all' | null;

// If `false`, do not copy marker when only part of its content is selected.
copyPartiallySelected?: boolean;
Expand Down
18 changes: 17 additions & 1 deletion packages/ckeditor5-clipboard/tests/clipboardmarkersutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,11 +765,27 @@ describe( 'Clipboard Markers Utils', () => {
} );

describe( '_isMarkerCopyable', () => {
it( 'returns false on non existing clipboard markers', () => {
it( 'returns `false` on non existing clipboard markers', () => {
const result = clipboardMarkersUtils._isMarkerCopyable( 'Hello', 'cut' );

expect( result ).to.false;
} );

it( 'returns `true` when action is not defined', () => {
clipboardMarkersUtils._registerMarkerToCopy( 'comment', { allowedActions: [ 'copy' ] } );

const result = clipboardMarkersUtils._isMarkerCopyable( 'comment' );

expect( result ).to.true;
} );

it( 'returns `false` when action is not defined and allowed actions are set to `null`', () => {
clipboardMarkersUtils._registerMarkerToCopy( 'comment', { allowedActions: null } );

const result = clipboardMarkersUtils._isMarkerCopyable( 'comment' );

expect( result ).to.false;
} );
} );

describe( '_getCopyableMarkersFromSelection', () => {
Expand Down

0 comments on commit 2b97493

Please sign in to comment.