Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
// If the URL represents a media, let's use it.
if ( !mediaRegistry.hasMedia( url ) ) {
return;
}
const mediaEmbedCommand = editor.commands.get( 'mediaEmbed' );
// Do not anything if media element cannot be inserted at the current position (#47).
if ( !mediaEmbedCommand.isEnabled ) {
return;
}
// Position won't be available in the `setTimeout` function so let's clone it.
this._positionToInsert = LivePosition.fromPosition( leftPosition );
// This action mustn't be executed if undo was called between pasting and auto-embedding.
this._timeoutId = global.window.setTimeout( () => {
editor.model.change( writer => {
this._timeoutId = null;
writer.remove( urlRange );
let insertionPosition;
// Check if position where the media element should be inserted is still valid.
// Otherwise leave it as undefined to use document.selection - default behavior of model.insertContent().
if ( this._positionToInsert.root.rootName !== '$graveyard' ) {
insertionPosition = this._positionToInsert;
}
this.listenTo( editor.plugins.get( Clipboard ), 'inputTransformation', () => {
const firstRange = modelDocument.selection.getFirstRange();
const leftLivePosition = LivePosition.fromPosition( firstRange.start );
leftLivePosition.stickiness = 'toPrevious';
const rightLivePosition = LivePosition.fromPosition( firstRange.end );
rightLivePosition.stickiness = 'toNext';
modelDocument.once( 'change:data', () => {
this._embedMediaBetweenPositions( leftLivePosition, rightLivePosition );
leftLivePosition.detach();
rightLivePosition.detach();
}, { priority: 'high' } );
} );
this.listenTo( editor.plugins.get( Clipboard ), 'inputTransformation', () => {
const firstRange = modelDocument.selection.getFirstRange();
const leftLivePosition = LivePosition.fromPosition( firstRange.start );
leftLivePosition.stickiness = 'toPrevious';
const rightLivePosition = LivePosition.fromPosition( firstRange.end );
rightLivePosition.stickiness = 'toNext';
modelDocument.once( 'change:data', () => {
this._embedMediaBetweenPositions( leftLivePosition, rightLivePosition );
leftLivePosition.detach();
rightLivePosition.detach();
}, { priority: 'high' } );
} );