/* global tinymce */
tinymce.PluginManager.add( 'wplink', function( editor ) {
- var linkButton;
-
- // Register a command so that it can be invoked by using tinyMCE.activeEditor.execCommand( 'WP_Link' );
editor.addCommand( 'WP_Link', function() {
- if ( ( ! linkButton || ! linkButton.disabled() ) && typeof window.wpLink !== 'undefined' ) {
- window.wpLink.open( editor.id );
- }
+ window.wpLink && window.wpLink.open( editor.id );
});
// WP default shortcut
- editor.addShortcut( 'alt+shift+a', '', 'WP_Link' );
+ editor.addShortcut( 'Alt+Shift+A', '', 'WP_Link' );
// The "de-facto standard" shortcut, see #27305
- editor.addShortcut( 'ctrl+k', '', 'WP_Link' );
-
- function setState( button, node ) {
- var parent = editor.dom.getParent( node, 'a' ),
- getView = editor.plugins.wpview ? editor.plugins.wpview.getView : function() { return false; };
-
- button.disabled( ( editor.selection.isCollapsed() && ! parent ) || ( parent && ! parent.href ) || getView( node ) );
- button.active( parent && parent.href );
- }
+ editor.addShortcut( 'Meta+K', '', 'WP_Link' );
editor.addButton( 'link', {
icon: 'link',
tooltip: 'Insert/edit link',
- shortcut: 'Alt+Shift+A',
cmd: 'WP_Link',
-
- onPostRender: function() {
- linkButton = this;
-
- editor.on( 'nodechange', function( event ) {
- setState( linkButton, event.element );
- });
- }
+ stateSelector: 'a[href]'
});
editor.addButton( 'unlink', {
icon: 'unlink',
tooltip: 'Remove link',
- cmd: 'unlink',
-
- onPostRender: function() {
- var unlinkButton = this;
-
- editor.on( 'nodechange', function( event ) {
- setState( unlinkButton, event.element );
- });
- }
+ cmd: 'unlink'
});
editor.addMenuItem( 'link', {
icon: 'link',
- text: 'Insert link',
- shortcut: 'Alt+Shift+A',
+ text: 'Insert/edit link',
cmd: 'WP_Link',
stateSelector: 'a[href]',
context: 'insert',
prependToContext: true
});
+
+ editor.on( 'pastepreprocess', function( event ) {
+ var pastedStr = event.content;
+
+ if ( ! editor.selection.isCollapsed() ) {
+ pastedStr = pastedStr.replace( /<[^>]+>/g, '' );
+ pastedStr = tinymce.trim( pastedStr );
+
+ if ( /^(?:https?:)?\/\/\S+$/i.test( pastedStr ) ) {
+ editor.execCommand( 'mceInsertLink', false, {
+ href: editor.dom.decode( pastedStr )
+ } );
+
+ event.preventDefault();
+ }
+ }
+ } );
});