| 1 |
/** |
| 2 |
* Handles registration of TinyMCE buttons. |
| 3 |
* |
| 4 |
* @since 1.9.6 |
| 5 |
* |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/* eslint-disable no-unused-vars */ |
| 10 |
/** |
| 11 |
* Registers the given block as a TinyMCE Plugin, with a button in |
| 12 |
* the Visual Editor toolbar. |
| 13 |
* |
| 14 |
* @since 1.9.6 |
| 15 |
* |
| 16 |
* @param {Object} block Block |
| 17 |
*/ |
| 18 |
function convertKitTinyMCERegisterPlugin(block) { |
| 19 |
tinymce.PluginManager.add( |
| 20 |
'convertkit_' + block.name, |
| 21 |
function (editor, url) { |
| 22 |
// Add Button to Visual Editor Toolbar. |
| 23 |
editor.addButton('convertkit_' + block.name, { |
| 24 |
title: block.title, |
| 25 |
image: url + '../../../../' + block.icon, |
| 26 |
cmd: 'convertkit_' + block.name, |
| 27 |
}); |
| 28 |
|
| 29 |
// Load View when button clicked. |
| 30 |
editor.addCommand('convertkit_' + block.name, function () { |
| 31 |
// Close any existing QuickTags modal. |
| 32 |
convertKitQuickTagsModal.close(); |
| 33 |
|
| 34 |
// Open the TinyMCE Modal. |
| 35 |
editor.windowManager.open({ |
| 36 |
id: 'convertkit-modal-body', |
| 37 |
title: block.title, |
| 38 |
width: block.modal.width, |
| 39 |
|
| 40 |
// Set modal height up to a maximum of 580px. |
| 41 |
// Content will overflow-y to show a scrollbar where necessary. |
| 42 |
height: block.modal.height < 580 ? block.modal.height : 580, |
| 43 |
inline: 1, |
| 44 |
buttons: [ |
| 45 |
{ |
| 46 |
text: 'Cancel', |
| 47 |
classes: 'cancel', |
| 48 |
}, |
| 49 |
{ |
| 50 |
text: 'Insert', |
| 51 |
subtype: 'primary', |
| 52 |
classes: 'insert', |
| 53 |
}, |
| 54 |
], |
| 55 |
}); |
| 56 |
|
| 57 |
// Perform an AJAX call to load the modal's view. |
| 58 |
fetch( |
| 59 |
convertkit_admin_tinymce.ajaxurl + |
| 60 |
'/' + |
| 61 |
block.name + |
| 62 |
'/' + |
| 63 |
'tinymce', |
| 64 |
{ |
| 65 |
method: 'GET', |
| 66 |
headers: { |
| 67 |
'X-WP-Nonce': convertkit_admin_tinymce.nonce, |
| 68 |
}, |
| 69 |
} |
| 70 |
) |
| 71 |
.then(function (response) { |
| 72 |
return response.json(); |
| 73 |
}) |
| 74 |
.then(function (result) { |
| 75 |
// Inject HTML into modal. |
| 76 |
document.querySelector( |
| 77 |
'#convertkit-modal-body-body' |
| 78 |
).innerHTML = result; |
| 79 |
|
| 80 |
// Initialize tabbed interface. |
| 81 |
convertKitTabsInit(); |
| 82 |
|
| 83 |
// Listen for color input changes. |
| 84 |
convertKitColorInputInit(); |
| 85 |
|
| 86 |
// Initialize conditional fields. |
| 87 |
convertKitConditionallyDisplayTinyMCEModalFields(); |
| 88 |
|
| 89 |
// Listen for field changes. |
| 90 |
convertKitConditionalFieldsInit(); |
| 91 |
|
| 92 |
// Bind refresh resource event listeners. |
| 93 |
convertKitRefreshResourcesInitEventListeners(); |
| 94 |
}) |
| 95 |
.catch(function (error) { |
| 96 |
console.error(error); |
| 97 |
}); |
| 98 |
}); |
| 99 |
} |
| 100 |
); |
| 101 |
} |
| 102 |
/* eslint-enable no-unused-vars */ |
| 103 |
|
| 104 |
/** |
| 105 |
* Listens for changes to input[type=color] inputs within a TinyMCE or QuickTags modal, |
| 106 |
* assigning the value to the input's data-value attribute. |
| 107 |
* |
| 108 |
* The modal.js will check the data-value for the 'true' value, as any color input will always |
| 109 |
* send #000000 as the value, even when none is supplied. |
| 110 |
* |
| 111 |
* @since 2.4.3 |
| 112 |
*/ |
| 113 |
function convertKitColorInputInit() { |
| 114 |
document |
| 115 |
.querySelectorAll('.convertkit-option input[type=color]') |
| 116 |
.forEach(function (colorPicker) { |
| 117 |
colorPicker.addEventListener('change', function (event) { |
| 118 |
event.target.dataset.value = event.target.value; |
| 119 |
}); |
| 120 |
}); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Initializes the conditional fields functionality. |
| 125 |
* |
| 126 |
* @since 3.0.6 |
| 127 |
*/ |
| 128 |
function convertKitConditionalFieldsInit() { |
| 129 |
document |
| 130 |
.querySelectorAll( |
| 131 |
'form.convertkit-tinymce-popup select, form.convertkit-tinymce-popup input' |
| 132 |
) |
| 133 |
.forEach(function (field) { |
| 134 |
field.addEventListener('change', function () { |
| 135 |
convertKitConditionallyDisplayTinyMCEModalFields(); |
| 136 |
}); |
| 137 |
}); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Conditionally display the TinyMCE modal fields. |
| 142 |
* |
| 143 |
* @since 3.0.6 |
| 144 |
*/ |
| 145 |
function convertKitConditionallyDisplayTinyMCEModalFields() { |
| 146 |
document |
| 147 |
.querySelectorAll('form.convertkit-tinymce-popup [data-display-if]') |
| 148 |
.forEach(function (field) { |
| 149 |
// Get field that controls whether this field should be displayed. |
| 150 |
const controllingField = document.querySelector( |
| 151 |
`form.convertkit-tinymce-popup select[name="${field.dataset.displayIf}"]` |
| 152 |
); |
| 153 |
|
| 154 |
// If the value of the field that should be displayed is the same as the value of the controlling field, show/hide the containing div. |
| 155 |
const container = field.closest('.convertkit-option'); |
| 156 |
container.style.display = |
| 157 |
field.dataset.displayIfValue === controllingField.value |
| 158 |
? 'grid' |
| 159 |
: 'none'; |
| 160 |
}); |
| 161 |
} |
| 162 |
|