Skip to content

Commit

Permalink
Add isExternal flag to clipboard input events.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Jan 2, 2025
1 parent 4896a62 commit ea06ead
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/ckeditor5-clipboard/src/clipboardpipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ import ClipboardMarkersUtils from './clipboardmarkersutils.js';
* Read more about the clipboard integration in the {@glink framework/deep-dive/clipboard clipboard deep-dive} guide.
*/
export default class ClipboardPipeline extends Plugin {
/**
* The last known content copied to the clipboard.
*/
private _lastKnownCopiedHTMLContent: string | null = null;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -236,10 +241,12 @@ export default class ClipboardPipeline extends Plugin {
}

const eventInfo = new EventInfo( this, 'inputTransformation' );
const isExternal = this._lastKnownCopiedHTMLContent !== dataTransfer.getData( 'text/html' );

this.fire<ClipboardInputTransformationEvent>( eventInfo, {
content,
dataTransfer,
isExternal,
targetRanges: data.targetRanges,
method: data.method as 'paste' | 'drop'
} );
Expand Down Expand Up @@ -278,6 +285,7 @@ export default class ClipboardPipeline extends Plugin {
this.fire<ClipboardContentInsertionEvent>( 'contentInsertion', {
content: modelFragment,
method: data.method,
isExternal: data.isExternal,
dataTransfer: data.dataTransfer,
targetRanges: data.targetRanges
} );
Expand Down Expand Up @@ -329,8 +337,14 @@ export default class ClipboardPipeline extends Plugin {

this.listenTo<ViewDocumentClipboardOutputEvent>( viewDocument, 'clipboardOutput', ( evt, data ) => {
if ( !data.content.isEmpty ) {
data.dataTransfer.setData( 'text/html', this.editor.data.htmlProcessor.toData( data.content ) );
const html = this.editor.data.htmlProcessor.toData( data.content );

this._lastKnownCopiedHTMLContent = html;

data.dataTransfer.setData( 'text/html', html );
data.dataTransfer.setData( 'text/plain', viewToPlainText( data.content ) );
} else {
this._lastKnownCopiedHTMLContent = null;
}

if ( data.method == 'cut' ) {
Expand Down Expand Up @@ -389,6 +403,11 @@ export interface ClipboardInputTransformationData {
* Whether the event was triggered by a paste or a drop operation.
*/
method: 'paste' | 'drop';

/**
* Whether the content being pasted is external (from outside the editor).
*/
isExternal: boolean;
}

/**
Expand Down Expand Up @@ -433,6 +452,11 @@ export interface ClipboardContentInsertionData {
*/
method: 'paste' | 'drop';

/**
* Whether the content being pasted is external (from outside the editor).
*/
isExternal: boolean;

/**
* The data transfer instance.
*/
Expand Down

0 comments on commit ea06ead

Please sign in to comment.