| 1 |
/** |
| 2 |
* Handles registration of TinyMCE buttons. |
| 3 |
* |
| 4 |
* @since 1.9.6 |
| 5 |
* |
| 6 |
* @package ConvertKit |
| 7 |
* @author ConvertKit |
| 8 |
*/ |
| 9 |
|
| 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 |
|
| 20 |
tinymce.PluginManager.add( |
| 21 |
'convertkit_' + block.name, |
| 22 |
function ( editor, url ) { |
| 23 |
|
| 24 |
// Add Button to Visual Editor Toolbar. |
| 25 |
editor.addButton( |
| 26 |
'convertkit_' + block.name, |
| 27 |
{ |
| 28 |
title: block.title, |
| 29 |
image: url + '../../../../' + block.icon, |
| 30 |
cmd: 'convertkit_' + block.name, |
| 31 |
} |
| 32 |
); |
| 33 |
|
| 34 |
// Load View when button clicked. |
| 35 |
editor.addCommand( |
| 36 |
'convertkit_' + block.name, |
| 37 |
function () { |
| 38 |
|
| 39 |
// Close any existing QuickTags modal. |
| 40 |
convertKitQuickTagsModal.close(); |
| 41 |
|
| 42 |
// Open the TinyMCE Modal. |
| 43 |
editor.windowManager.open( |
| 44 |
{ |
| 45 |
id: 'convertkit-modal-body', |
| 46 |
title: block.title, |
| 47 |
width: block.modal.width, |
| 48 |
|
| 49 |
// Set modal height up to a maximum of 580px. |
| 50 |
// Content will overflow-y to show a scrollbar where necessary. |
| 51 |
height: ( block.modal.height < 580 ? block.modal.height : 580 ), |
| 52 |
inline: 1, |
| 53 |
buttons: [ |
| 54 |
{ |
| 55 |
text: 'Cancel', |
| 56 |
classes: 'cancel' |
| 57 |
}, |
| 58 |
{ |
| 59 |
text: 'Insert', |
| 60 |
subtype: 'primary', |
| 61 |
classes: 'insert' |
| 62 |
} |
| 63 |
] |
| 64 |
} |
| 65 |
); |
| 66 |
|
| 67 |
// Perform an AJAX call to load the modal's view. |
| 68 |
fetch( |
| 69 |
ajaxurl, |
| 70 |
{ |
| 71 |
method: 'POST', |
| 72 |
headers: { |
| 73 |
'Content-Type': 'application/x-www-form-urlencoded', |
| 74 |
}, |
| 75 |
body: new URLSearchParams( |
| 76 |
{ |
| 77 |
'action': 'convertkit_admin_tinymce_output_modal', |
| 78 |
'nonce': convertkit_admin_tinymce.nonce, |
| 79 |
'editor_type': 'tinymce', |
| 80 |
'shortcode': block.name |
| 81 |
} |
| 82 |
), |
| 83 |
} |
| 84 |
) |
| 85 |
.then( |
| 86 |
function ( response ) { |
| 87 |
return response.text(); |
| 88 |
} |
| 89 |
) |
| 90 |
.then( |
| 91 |
function ( result ) { |
| 92 |
// Inject HTML into modal. |
| 93 |
document.querySelector( '#convertkit-modal-body-body' ).innerHTML = result; |
| 94 |
|
| 95 |
// Initialize tabbed interface. |
| 96 |
convertKitTabsInit(); |
| 97 |
|
| 98 |
// Listen for color input changes. |
| 99 |
convertKitColorInputInit(); |
| 100 |
} |
| 101 |
) |
| 102 |
.catch( |
| 103 |
function ( error ) { |
| 104 |
console.error( error ); |
| 105 |
} |
| 106 |
); |
| 107 |
|
| 108 |
} |
| 109 |
); |
| 110 |
|
| 111 |
} |
| 112 |
); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Listens for changes to input[type=color] inputs within a TinyMCE or QuickTags modal, |
| 118 |
* assigning the value to the input's data-value attribute. |
| 119 |
* |
| 120 |
* The modal.js will check the data-value for the 'true' value, as any color input will always |
| 121 |
* send #000000 as the value, even when none is supplied. |
| 122 |
* |
| 123 |
* @since 2.4.3 |
| 124 |
*/ |
| 125 |
function convertKitColorInputInit() { |
| 126 |
|
| 127 |
document.querySelectorAll( '.convertkit-option input[type=color]' ).forEach( |
| 128 |
function ( colorPicker ) { |
| 129 |
colorPicker.addEventListener( |
| 130 |
'change', |
| 131 |
function ( event ) { |
| 132 |
event.target.dataset.value = event.target.value; |
| 133 |
} |
| 134 |
); |
| 135 |
} |
| 136 |
); |
| 137 |
|
| 138 |
} |
| 139 |
|