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 #68 from ckeditor/t/36
Browse files Browse the repository at this point in the history
Fix: The `AutoMediaEmbed` feature should insert media in place of a pasted link. Closes #36. Closes #49.
  • Loading branch information
oleq authored Jan 4, 2019
2 parents f628629 + 2c1f17f commit 5f79878
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/automediaembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import LiveRange from '@ckeditor/ckeditor5-engine/src/model/liverange';
import LivePosition from '@ckeditor/ckeditor5-engine/src/model/liveposition';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import global from '@ckeditor/ckeditor5-utils/src/dom/global';
import { insertMedia } from './utils';

const URL_REGEXP = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=]+$/;

Expand Down Expand Up @@ -153,12 +154,15 @@ export default class AutoMediaEmbed extends Plugin {

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' ) {
writer.setSelection( this._positionToInsert );
insertionPosition = this._positionToInsert;
}

mediaEmbedCommand.execute( url );
insertMedia( editor.model, url, insertionPosition );

this._positionToInsert.detach();
this._positionToInsert = null;
Expand Down
10 changes: 2 additions & 8 deletions src/mediaembedcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import Command from '@ckeditor/ckeditor5-core/src/command';
import { findOptimalInsertionPosition } from '@ckeditor/ckeditor5-widget/src/utils';
import { getSelectedMediaElement } from './utils';
import { getSelectedMediaElement, insertMedia } from './utils';

/**
* The insert media command.
Expand Down Expand Up @@ -64,13 +64,7 @@ export default class MediaEmbedCommand extends Command {
} else {
const insertPosition = findOptimalInsertionPosition( selection, model );

model.change( writer => {
const mediaElement = writer.createElement( 'media', { url } );

model.insertContent( mediaElement, insertPosition );

writer.setSelection( mediaElement, 'on' );
} );
insertMedia( model, url, insertPosition );
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@ export function getSelectedMediaElement( selection ) {
return null;
}

/**
* Creates a media element and inserts it into the model.
*
* **Note**: This method will use {@link module:engine/model/model~Model#insertContent `model.insertContent()`} logic of inserting content
* if no `insertPosition` is passed.
*
* @param {module:engine/model/model~Model} model
* @param {String} url An URL of an embeddable media.
* @param {module:engine/model/position~Position} [insertPosition] Position to insert media. If not specified,
* the default behavior of {@link module:engine/model/model~Model#insertContent `model.insertContent()`} will
* be applied.
*/
export function insertMedia( model, url, insertPosition ) {
model.change( writer => {
const mediaElement = writer.createElement( 'media', { url } );

model.insertContent( mediaElement, insertPosition );

writer.setSelection( mediaElement, 'on' );
} );
}

function getFillerOffset() {
return null;
}
28 changes: 27 additions & 1 deletion tests/automediaembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,33 @@ describe( 'AutoMediaEmbed - integration', () => {
clock.tick( 100 );

expect( getData( editor.model ) ).to.equal(
'[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]<paragraph>For</paragraph>'
'<paragraph>Fo</paragraph>[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]<paragraph>r</paragraph>'
);
} );

it( 'inserts media in-place (collapsed selection)', () => {
setData( editor.model, '<paragraph>Foo []Bar</paragraph>' );
pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );

clock.tick( 100 );

expect( getData( editor.model ) ).to.equal(
'<paragraph>Foo </paragraph>' +
'[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]' +
'<paragraph>Bar</paragraph>'
);
} );

it( 'inserts media in-place (non-collapsed selection)', () => {
setData( editor.model, '<paragraph>Foo [Bar] Baz</paragraph>' );
pasteHtml( editor, 'https://www.youtube.com/watch?v=H08tGjXNHO4' );

clock.tick( 100 );

expect( getData( editor.model ) ).to.equal(
'<paragraph>Foo </paragraph>' +
'[<media url="https://www.youtube.com/watch?v=H08tGjXNHO4"></media>]' +
'<paragraph> Baz</paragraph>'
);
} );

Expand Down

0 comments on commit 5f79878

Please sign in to comment.