| 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 |
document.addEventListener( |
| 11 |
'DOMContentLoaded', |
| 12 |
function () { |
| 13 |
|
| 14 |
// Cancel. |
| 15 |
document.body.addEventListener( |
| 16 |
'click', |
| 17 |
function ( e ) { |
| 18 |
|
| 19 |
// Check if a cancel button was clicked. |
| 20 |
if ( e.target.matches( '#convertkit-modal-body div.mce-cancel button, #convertkit-modal-body div.mce-cancel button *, #convertkit-quicktags-modal .media-toolbar .media-toolbar-secondary button.cancel' ) ) { |
| 21 |
|
| 22 |
// TinyMCE. |
| 23 |
if ( typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) { |
| 24 |
tinymce.activeEditor.windowManager.close(); |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
// Text Editor. |
| 29 |
// Close the QuickTags modal. |
| 30 |
convertKitQuickTagsModal.close(); |
| 31 |
} |
| 32 |
} |
| 33 |
); |
| 34 |
|
| 35 |
// Insert. |
| 36 |
document.body.addEventListener( |
| 37 |
'click', |
| 38 |
function ( e ) { |
| 39 |
|
| 40 |
// Check if an insert button was clicked. |
| 41 |
if ( e.target.matches( '#convertkit-modal-body div.mce-insert button, #convertkit-modal-body div.mce-insert button *, #convertkit-quicktags-modal .media-toolbar .media-toolbar-primary button.button-primary' ) || |
| 42 |
e.target.matches( 'button.wp-convertkit-refresh-resources, span.dashicons-update' ) ) { |
| 43 |
|
| 44 |
// Prevent default action. |
| 45 |
e.preventDefault(); |
| 46 |
|
| 47 |
// Don't close the modal if the refresh button was clicked. |
| 48 |
if ( e.target.matches( 'button.wp-convertkit-refresh-resources, span.dashicons-update' ) ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
// Get containing form. |
| 53 |
const form = document.querySelector( 'form.convertkit-tinymce-popup' ); |
| 54 |
|
| 55 |
// Build Shortcode. |
| 56 |
let shortcode = '[' + form.querySelector( 'input[name="shortcode"]' ).value; |
| 57 |
const shortcodeClose = form.querySelector( 'input[name="close_shortcode"]' ).value === '1'; |
| 58 |
|
| 59 |
// Iterate through form fields. |
| 60 |
form.querySelectorAll( 'input, select' ).forEach( |
| 61 |
function ( element ) { |
| 62 |
// Skip if no data-shortcode attribute. |
| 63 |
if ( ! element.dataset.shortcode ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
let val = ''; |
| 68 |
|
| 69 |
// If this is a color picker, #000000 will be submitted by the browser as the value |
| 70 |
// even if no value / color was selected. |
| 71 |
// Check the data-value attribute instead. |
| 72 |
switch ( element.type ) { |
| 73 |
case 'color': |
| 74 |
val = element.dataset.value; |
| 75 |
break; |
| 76 |
default: |
| 77 |
val = element.value; |
| 78 |
break; |
| 79 |
} |
| 80 |
|
| 81 |
// Skip if the value is empty. |
| 82 |
if ( ! val ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
if ( val.length === 0 ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
// Get shortcode attribute. |
| 90 |
const key = element.dataset.shortcode; |
| 91 |
const trim = element.dataset.trim !== '0'; |
| 92 |
|
| 93 |
// Skip if the shortcode is empty. |
| 94 |
if ( ! key.length ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
// Trim the value, unless the shortcode attribute disables string trimming. |
| 99 |
if ( trim ) { |
| 100 |
val = val.trim(); |
| 101 |
} |
| 102 |
|
| 103 |
// Append attribute and value to shortcode string. |
| 104 |
shortcode += ' ' + key.trim() + '="' + val + '"'; |
| 105 |
} |
| 106 |
); |
| 107 |
|
| 108 |
// Close Shortcode. |
| 109 |
shortcode += ']'; |
| 110 |
|
| 111 |
// If the shortcode includes a closing element, append it now. |
| 112 |
if ( shortcodeClose ) { |
| 113 |
shortcode += '[/' + form.querySelector( 'input[name="shortcode"]' ).value + ']'; |
| 114 |
} |
| 115 |
|
| 116 |
// Depending on the editor type, insert the shortcode. |
| 117 |
switch ( form.querySelector( 'input[name="editor_type"]' ).value ) { |
| 118 |
case 'tinymce': |
| 119 |
// Sanity check that a Visual editor exists and is active. |
| 120 |
if ( typeof tinyMCE !== 'undefined' && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() ) { |
| 121 |
// Insert into editor. |
| 122 |
tinyMCE.activeEditor.execCommand( 'mceReplaceContent', false, shortcode ); |
| 123 |
|
| 124 |
// Close modal. |
| 125 |
tinyMCE.activeEditor.windowManager.close(); |
| 126 |
} |
| 127 |
break; |
| 128 |
|
| 129 |
case 'quicktags': |
| 130 |
// Insert into editor. |
| 131 |
QTags.insertContent( shortcode ); |
| 132 |
|
| 133 |
// Close modal. |
| 134 |
convertKitQuickTagsModal.close(); |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
); |
| 140 |
|
| 141 |
} |
| 142 |
); |
| 143 |
|
| 144 |
// QuickTags: Setup Backbone Modal and Template. |
| 145 |
if ( typeof wp !== 'undefined' && typeof wp.media !== 'undefined' ) { |
| 146 |
|
| 147 |
// Declared globally, as used in this file and quicktags.js. |
| 148 |
var convertKitQuickTagsModal = new wp.media.view.Modal( |
| 149 |
{ |
| 150 |
controller: { trigger: function () {} }, |
| 151 |
className: 'convertkit-quicktags-modal' |
| 152 |
} |
| 153 |
); |
| 154 |
const convertKitQuickTagsModalContent = wp.Backbone.View.extend( |
| 155 |
{ |
| 156 |
template: wp.template( 'convertkit-quicktags-modal' ) |
| 157 |
} |
| 158 |
); |
| 159 |
convertKitQuickTagsModal.content( new convertKitQuickTagsModalContent() ); |
| 160 |
|
| 161 |
/** |
| 162 |
* Resets the content of the convertKitQuickTagsModal when closing. |
| 163 |
* |
| 164 |
* If this isn't performed, switching from Text to Visual Editor for the same shortcode results |
| 165 |
* code picking up data from the QuickTags modal, not the TinyMCE one, due to this 'stale' |
| 166 |
* modal remaining in the DOM, resulting in e.g. the tabbed UI not loading correctly. |
| 167 |
*/ |
| 168 |
convertKitQuickTagsModal.on( |
| 169 |
'close', |
| 170 |
function ( e ) { |
| 171 |
this.content( new convertKitQuickTagsModalContent() ); |
| 172 |
} |
| 173 |
); |
| 174 |
|
| 175 |
} |
| 176 |
|