+ var editToolbar;
+ var previewInstance;
+ var inputInstance;
+ var linkNode;
+ var doingUndoRedo;
+ var doingUndoRedoTimer;
+ var $ = window.jQuery;
+
+ function getSelectedLink() {
+ var href, html,
+ node = editor.selection.getNode(),
+ link = editor.dom.getParent( node, 'a[href]' );
+
+ if ( ! link ) {
+ html = editor.selection.getContent({ format: 'raw' });
+
+ if ( html && html.indexOf( '</a>' ) !== -1 ) {
+ href = html.match( /href="([^">]+)"/ );
+
+ if ( href && href[1] ) {
+ link = editor.$( 'a[href="' + href[1] + '"]', node )[0];
+ }
+
+ if ( link ) {
+ editor.selection.select( link );
+ }
+ }
+ }
+
+ return link;
+ }
+
+ function removePlaceholders() {
+ editor.$( 'a' ).each( function( i, element ) {
+ var $element = editor.$( element );
+
+ if ( $element.attr( 'href' ) === '_wp_link_placeholder' ) {
+ editor.dom.remove( element, true );
+ } else if ( $element.attr( 'data-wplink-edit' ) ) {
+ $element.attr( 'data-wplink-edit', null );
+ }
+ });
+ }
+
+ function removePlaceholderStrings( content, dataAttr ) {
+ if ( dataAttr ) {
+ content = content.replace( / data-wplink-edit="true"/g, '' );
+ }
+
+ return content.replace( /<a [^>]*?href="_wp_link_placeholder"[^>]*>([\s\S]+)<\/a>/g, '$1' );
+ }
+
+ editor.on( 'preinit', function() {
+ if ( editor.wp && editor.wp._createToolbar ) {
+ toolbar = editor.wp._createToolbar( [
+ 'wp_link_preview',
+ 'wp_link_edit',
+ 'wp_link_remove'
+ ], true );
+
+ var editButtons = [
+ 'wp_link_input',
+ 'wp_link_apply'
+ ];
+
+ if ( typeof window.wpLink !== 'undefined' ) {
+ editButtons.push( 'wp_link_advanced' );
+ }
+
+ editToolbar = editor.wp._createToolbar( editButtons, true );
+
+ editToolbar.on( 'show', function() {
+ if ( ! tinymce.$( document.body ).hasClass( 'modal-open' ) ) {
+ window.setTimeout( function() {
+ var element = editToolbar.$el.find( 'input.ui-autocomplete-input' )[0],
+ selection = linkNode && ( linkNode.textContent || linkNode.innerText );
+
+ if ( element ) {
+ if ( ! element.value && selection && typeof window.wpLink !== 'undefined' ) {
+ element.value = window.wpLink.getUrlFromSelection( selection );
+ }
+
+ if ( ! doingUndoRedo ) {
+ element.focus();
+ element.select();
+ }
+ }
+ } );
+ }
+ } );
+
+ editToolbar.on( 'hide', function() {
+ if ( ! editToolbar.scrolling ) {
+ editor.execCommand( 'wp_link_cancel' );
+ }
+ } );
+ }
+ } );