Explorar el Código

Initial version of auto-embeding media.

Kamil Piechaczek hace 7 años
padre
commit
838830850c

+ 1 - 0
packages/ckeditor5-media-embed/package.json

@@ -10,6 +10,7 @@
   ],
   "dependencies": {
     "@ckeditor/ckeditor5-core": "^11.0.0",
+    "@ckeditor/ckeditor5-clipboard": "^10.0.2",
     "@ckeditor/ckeditor5-engine": "^10.2.0",
     "@ckeditor/ckeditor5-ui": "^11.0.0",
     "@ckeditor/ckeditor5-utils": "^10.2.0",

+ 25 - 1
packages/ckeditor5-media-embed/src/mediaembed.js

@@ -12,6 +12,9 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import MediaEmbedEditing from './mediaembedediting';
 import MediaEmbedUI from './mediaembedui';
 import Widget from '@ckeditor/ckeditor5-widget/src/widget';
+import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
+
+const URL_REGEXP = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/;
 
 /**
  * The media embed plugin.
@@ -28,7 +31,7 @@ export default class MediaEmbed extends Plugin {
 	 * @inheritDoc
 	 */
 	static get requires() {
-		return [ MediaEmbedEditing, MediaEmbedUI, Widget ];
+		return [ MediaEmbedEditing, MediaEmbedUI, Widget, Clipboard ];
 	}
 
 	/**
@@ -37,6 +40,27 @@ export default class MediaEmbed extends Plugin {
 	static get pluginName() {
 		return 'MediaEmbed';
 	}
+
+	/**
+	 * @inheritDoc
+	 */
+	init() {
+		const editor = this.editor;
+		const mediaRegistry = editor.plugins.get( MediaEmbedEditing ).mediaRegistry;
+
+		this.listenTo( editor.plugins.get( Clipboard ), 'inputTransformation', ( evt, data ) => {
+			const url = data.content.getChild( 0 ).data;
+
+			if ( !url.match( URL_REGEXP ) ) {
+				return;
+			}
+
+			if ( mediaRegistry.hasMedia( url ) ) {
+				this.editor.commands.execute( 'mediaEmbed', url );
+				evt.stop();
+			}
+		} );
+	}
 }
 
 /**