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