| 1 |
/** |
| 2 |
* Handles the Insert and Cancel events on TinyMCE and QuickTag Modals |
| 3 |
* |
| 4 |
* @since 1.9.6 |
| 5 |
* |
| 6 |
* @package ConvertKit |
| 7 |
* @author ConvertKit |
| 8 |
*/ |
| 9 |
|
| 10 |
jQuery( document ).ready( |
| 11 |
function ( $ ) { |
| 12 |
|
| 13 |
// Cancel. |
| 14 |
$( 'body' ).on( |
| 15 |
'click', |
| 16 |
'#convertkit-modal-body div.mce-cancel button, #convertkit-quicktags-modal .media-toolbar .media-toolbar-secondary button.cancel', |
| 17 |
function ( e ) { |
| 18 |
|
| 19 |
// TinyMCE. |
| 20 |
if ( typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) { |
| 21 |
tinymce.activeEditor.windowManager.close(); |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
// Text Editor. |
| 26 |
// Close the QuickTags modal. |
| 27 |
convertKitQuickTagsModal.close(); |
| 28 |
|
| 29 |
} |
| 30 |
); |
| 31 |
|
| 32 |
// Insert. |
| 33 |
$( 'body' ).on( |
| 34 |
'click', |
| 35 |
'#convertkit-modal-body div.mce-insert button, #convertkit-quicktags-modal .media-toolbar .media-toolbar-primary button.button-primary', |
| 36 |
function ( e ) { |
| 37 |
|
| 38 |
// Prevent default action. |
| 39 |
e.preventDefault(); |
| 40 |
|
| 41 |
// Get containing form. |
| 42 |
let form = $( 'form.convertkit-tinymce-popup' ); |
| 43 |
|
| 44 |
// Build Shortcode. |
| 45 |
let shortcode = '[' + $( 'input[name="shortcode"]', $( form ) ).val(), |
| 46 |
shortcodeClose = ( $( 'input[name="close_shortcode"]', $( form ) ).val() === '1' ? true : false ); |
| 47 |
|
| 48 |
$( 'input, select', $( form ) ).each( |
| 49 |
function ( i ) { |
| 50 |
// Skip if no data-shortcode attribute. |
| 51 |
if ( typeof $( this ).data( 'shortcode' ) === 'undefined' ) { |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
// Skip if the value is empty. |
| 56 |
if ( ! $( this ).val() ) { |
| 57 |
return true; |
| 58 |
} |
| 59 |
if ( $( this ).val().length === 0 ) { |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
// Get shortcode attribute. |
| 64 |
let key = $( this ).data( 'shortcode' ), |
| 65 |
trim = ( $( this ).data( 'trim' ) === '0' ? false : true ), |
| 66 |
val = $( this ).val(); |
| 67 |
|
| 68 |
// Skip if the shortcode is empty. |
| 69 |
if ( ! key.length ) { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
// Trim the value, unless the shortcode attribute disables string trimming. |
| 74 |
if ( trim ) { |
| 75 |
val = val.trim(); |
| 76 |
} |
| 77 |
|
| 78 |
// Append attribute and value to shortcode string. |
| 79 |
shortcode += ' ' + key.trim() + '="' + val + '"'; |
| 80 |
} |
| 81 |
); |
| 82 |
|
| 83 |
// Close Shortcode. |
| 84 |
shortcode += ']'; |
| 85 |
|
| 86 |
// If the shortcode includes a closing element, append it now. |
| 87 |
if ( shortcodeClose ) { |
| 88 |
shortcode += '[/' + $( 'input[name="shortcode"]', $( form ) ).val() + ']'; |
| 89 |
} |
| 90 |
|
| 91 |
// Depending on the editor type, insert the shortcode. |
| 92 |
switch ( $( 'input[name="editor_type"]', $( form ) ).val() ) { |
| 93 |
case 'tinymce': |
| 94 |
// Sanity check that a Visual editor exists and is active. |
| 95 |
if ( typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) { |
| 96 |
// Insert into editor. |
| 97 |
tinyMCE.activeEditor.execCommand( 'mceReplaceContent', false, shortcode ); |
| 98 |
|
| 99 |
// Close modal. |
| 100 |
tinyMCE.activeEditor.windowManager.close(); |
| 101 |
} |
| 102 |
break; |
| 103 |
|
| 104 |
case 'quicktags': |
| 105 |
// Insert into editor. |
| 106 |
QTags.insertContent( shortcode ); |
| 107 |
|
| 108 |
// Close modal. |
| 109 |
convertKitQuickTagsModal.close(); |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
} |
| 116 |
); |
| 117 |
|
| 118 |
|
| 119 |
// QuickTags: Setup Backbone Modal and Template. |
| 120 |
if ( typeof wp !== 'undefined' && typeof wp.media !== 'undefined' ) { |
| 121 |
|
| 122 |
// Declared globally, as used in this file and quicktags.js. |
| 123 |
var convertKitQuickTagsModal = new wp.media.view.Modal( |
| 124 |
{ |
| 125 |
controller: { trigger: function () {} }, |
| 126 |
className: 'convertkit-quicktags-modal' |
| 127 |
} |
| 128 |
); |
| 129 |
const convertKitQuickTagsModalContent = wp.Backbone.View.extend( |
| 130 |
{ |
| 131 |
template: wp.template( 'convertkit-quicktags-modal' ) |
| 132 |
} |
| 133 |
); |
| 134 |
convertKitQuickTagsModal.content( new convertKitQuickTagsModalContent() ); |
| 135 |
|
| 136 |
/** |
| 137 |
* Resets the content of the convertKitQuickTagsModal when closing. |
| 138 |
* |
| 139 |
* If this isn't performed, switching from Text to Visual Editor for the same shortcode results |
| 140 |
* code picking up data from the QuickTags modal, not the TinyMCE one, due to this 'stale' |
| 141 |
* modal remaining in the DOM, resulting in e.g. the tabbed UI not loading correctly. |
| 142 |
*/ |
| 143 |
convertKitQuickTagsModal.on( |
| 144 |
'close', |
| 145 |
function ( e ) { |
| 146 |
this.content( new convertKitQuickTagsModalContent() ); |
| 147 |
} |
| 148 |
); |
| 149 |
|
| 150 |
} |
| 151 |
|