8
0

automediaembed.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module media-embed/automediaembed
  7. */
  8. import MediaEmbedEditing from './mediaembedediting';
  9. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  10. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  11. import LiveRange from '@ckeditor/ckeditor5-engine/src/model/liverange';
  12. import LivePosition from '@ckeditor/ckeditor5-engine/src/model/liveposition';
  13. import TreeWalker from '@ckeditor/ckeditor5-engine/src/model/treewalker';
  14. import Undo from '@ckeditor/ckeditor5-undo/src/undo';
  15. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  16. const URL_REGEXP = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=]+$/;
  17. /**
  18. * The auto-media embed plugin. It recognizes media links in the pasted content and embeds
  19. * them shortly after they are injected into the document.
  20. *
  21. * @extends module:core/plugin~Plugin
  22. */
  23. export default class AutoMediaEmbed extends Plugin {
  24. /**
  25. * @inheritDoc
  26. */
  27. static get requires() {
  28. return [ Clipboard, Undo ];
  29. }
  30. /**
  31. * @inheritDoc
  32. */
  33. static get pluginName() {
  34. return 'AutoMediaEmbed';
  35. }
  36. /**
  37. * @inheritDoc
  38. */
  39. constructor( editor ) {
  40. super( editor );
  41. /**
  42. * The paste–to–embed `setTimeout` ID. Stored as a property to allow
  43. * cleaning of the timeout.
  44. *
  45. * @private
  46. * @member {Number} #_timeoutId
  47. */
  48. this._timeoutId = null;
  49. /**
  50. * The position where the `<media>` element will be inserted after the timeout,
  51. * determined each time the new content is pasted into the document.
  52. *
  53. * @private
  54. * @member {module:engine/model/liveposition~LivePosition} #_positionToInsert
  55. */
  56. this._positionToInsert = null;
  57. }
  58. /**
  59. * @inheritDoc
  60. */
  61. init() {
  62. const editor = this.editor;
  63. const modelDocument = editor.model.document;
  64. // We need to listen on `Clipboard#inputTransformation` because we need to save positions of selection.
  65. // After pasting, the content between those positions will be checked for a URL that could be transformed
  66. // into media.
  67. this.listenTo( editor.plugins.get( Clipboard ), 'inputTransformation', () => {
  68. const firstRange = modelDocument.selection.getFirstRange();
  69. const leftLivePosition = LivePosition.createFromPosition( firstRange.start );
  70. leftLivePosition.stickiness = 'toPrevious';
  71. const rightLivePosition = LivePosition.createFromPosition( firstRange.end );
  72. rightLivePosition.stickiness = 'toNext';
  73. modelDocument.once( 'change:data', () => {
  74. this._embedMediaBetweenPositions( leftLivePosition, rightLivePosition );
  75. leftLivePosition.detach();
  76. rightLivePosition.detach();
  77. }, { priority: 'high' } );
  78. } );
  79. editor.commands.get( 'undo' ).on( 'execute', () => {
  80. if ( this._timeoutId ) {
  81. global.window.clearTimeout( this._timeoutId );
  82. this._positionToInsert.detach();
  83. this._timeoutId = null;
  84. this._positionToInsert = null;
  85. }
  86. }, { priority: 'high' } );
  87. }
  88. /**
  89. * Analyzes the part of the document between provided positions in search for a URL representing media.
  90. * When the URL is found, it is automatically converted into media.
  91. *
  92. * @protected
  93. * @param {module:engine/model/liveposition~LivePosition} leftPosition Left position of the selection.
  94. * @param {module:engine/model/liveposition~LivePosition} rightPosition Right position of the selection.
  95. */
  96. _embedMediaBetweenPositions( leftPosition, rightPosition ) {
  97. const editor = this.editor;
  98. const mediaRegistry = editor.plugins.get( MediaEmbedEditing ).registry;
  99. const urlRange = new LiveRange( leftPosition, rightPosition );
  100. const walker = new TreeWalker( { boundaries: urlRange, ignoreElementEnd: true } );
  101. let url = '';
  102. for ( const node of walker ) {
  103. if ( node.item.is( 'textProxy' ) ) {
  104. url += node.item.data;
  105. }
  106. }
  107. url = url.trim();
  108. // If the URL does not match to universal URL regexp, let's skip that.
  109. if ( !url.match( URL_REGEXP ) ) {
  110. return;
  111. }
  112. // If the URL represents a media, let's use it.
  113. if ( !mediaRegistry.hasMedia( url ) ) {
  114. return;
  115. }
  116. const mediaEmbedCommand = editor.commands.get( 'mediaEmbed' );
  117. // Do not anything if media element cannot be inserted at the current position (#47).
  118. if ( !mediaEmbedCommand.isEnabled ) {
  119. return;
  120. }
  121. // Position won't be available in the `setTimeout` function so let's clone it.
  122. this._positionToInsert = LivePosition.createFromPosition( leftPosition );
  123. // This action mustn't be executed if undo was called between pasting and auto-embedding.
  124. this._timeoutId = global.window.setTimeout( () => {
  125. editor.model.change( writer => {
  126. this._timeoutId = null;
  127. writer.remove( urlRange );
  128. // Check if position where the media element should be inserted is still valid.
  129. if ( this._positionToInsert.root.rootName !== '$graveyard' ) {
  130. writer.setSelection( this._positionToInsert );
  131. }
  132. mediaEmbedCommand.execute( url );
  133. this._positionToInsert.detach();
  134. this._positionToInsert = null;
  135. } );
  136. }, 100 );
  137. }
  138. }