| 1 |
document.addEventListener('DOMContentLoaded', () => { |
| 2 |
const PhotonicEditor = () => { |
| 3 |
const wpEditor = document.getElementById('wp-content-wrap'); |
| 4 |
const wizardButton = document.querySelector('#photonic-add-gallery'); |
| 5 |
const mceTab = document.getElementById('content-tmce'), htmlTab = document.getElementById('content-html'); |
| 6 |
const nativeMediaLibrary = new PhotonicWPNativeUI(sendDataToWizard, getDataFromWizard); |
| 7 |
let photonicEditorSelection = ''; |
| 8 |
|
| 9 |
const isBlock = !!(wp.data && wp.data.select('core/block-editor') && document.body.classList.contains('block-editor-page')); |
| 10 |
let isTinyMCE = wpEditor ? wpEditor.classList.contains('tmce-active') : false; |
| 11 |
let lastClickedTab = isTinyMCE ? mceTab : htmlTab; |
| 12 |
|
| 13 |
if (!isBlock && !isTinyMCE) { |
| 14 |
nativeMediaLibrary.waitForIFrame(); |
| 15 |
} |
| 16 |
|
| 17 |
if (wizardButton) { |
| 18 |
wizardButton.addEventListener('click', e => { |
| 19 |
const textArea = document.querySelector('textarea#content'); |
| 20 |
const start = textArea.selectionStart; |
| 21 |
const end = textArea.selectionEnd; |
| 22 |
photonicEditorSelection = textArea.value.substring(start, end); |
| 23 |
}); |
| 24 |
} |
| 25 |
|
| 26 |
if (mceTab && htmlTab) { |
| 27 |
[mceTab, htmlTab].forEach(button => { |
| 28 |
button.addEventListener('click', e => { |
| 29 |
if (lastClickedTab.id !== button.id) { |
| 30 |
lastClickedTab = button; |
| 31 |
if (button.id === htmlTab.id) { |
| 32 |
nativeMediaLibrary.waitForIFrame(); |
| 33 |
} |
| 34 |
} |
| 35 |
}); |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
function parseEditorSelection(selection) { |
| 40 |
if (selection !== '' && top.wp !== undefined && top.wp.shortcode !== undefined) { |
| 41 |
let shortcode = top.wp.shortcode.next(Photonic_Editor_JS.shortcode, selection.trim()); |
| 42 |
let moreShortcode = top.wp.shortcode.next(Photonic_Editor_JS.shortcode, selection.trim(), 1); // Only one shortcode at a time |
| 43 |
|
| 44 |
if (shortcode !== undefined && moreShortcode === undefined && shortcode.content.length === selection.trim().length) { // Selection is a valid shortcode |
| 45 |
return { |
| 46 |
shortcode: shortcode |
| 47 |
} |
| 48 |
} |
| 49 |
else { // Selection is not a valid shortcode |
| 50 |
return { |
| 51 |
shortcode: null, |
| 52 |
error: 'Not a shortcode' |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
else if (selection === '') { // Selection is blank |
| 57 |
return { |
| 58 |
shortcode: '' |
| 59 |
} |
| 60 |
} |
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 64 |
function sendDataToWizard(messageChannel) { |
| 65 |
messageChannel.port1.postMessage({ |
| 66 |
type: 'photonicShortcode', |
| 67 |
object: parseEditorSelection(photonicEditorSelection), |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
function getDataFromWizard(data) { |
| 72 |
const win = window.dialogArguments || opener || parent || top; |
| 73 |
win.send_to_editor(data.html); |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
PhotonicEditor(); |
| 78 |
}); |
| 79 |
|