/* ========================================================== * editor.js * ========================================================== * Copyright 2021 Awesome Motive. * https://awesomemotive.com * ========================================================== */ import { getMonsterlink } from './Utils/monsterlink'; window.OMAPI_Editor = window.OMAPI_Editor || {}; /** * OptinMonster Classic Editor functionality. */ (function (window, document, $, app, undefined) { 'use strict'; // Make sure the OMAPI and OMAPI.monsterlink global is set. window.OMAPI = window.OMAPI || {}; OMAPI.monsterlink = app.monsterlink; /** * Get the currently active mce editor Id. * * @since 2.3.0 * * @returns {string|undefined} Tinymce editor instance Id if found. */ app.getActiveEditorId = function () { let { wpActiveEditor, tinymce } = window; if (wp.media.editor.activeEditor) { wpActiveEditor = wp.media.editor.activeEditor; } if (!wpActiveEditor && tinymce && tinymce.activeEditor) { wpActiveEditor = tinymce.activeEditor.id; } return wpActiveEditor; }; /** * Get the active WP tinymce editor instance. * * @since 2.3.0 * * @returns {Object|null} Tinymce editor instance or null if not found. */ app.getActiveEditor = function () { const editorId = app.getActiveEditorId(); // No luck... if (!editorId || !window.tinymce) { return null; } return window.tinymce.get(editorId); }; /** * Insert the selected campaign monsterlkink to the editor. * * @since 2.3.0 * * @returns {void} */ app.mceLinkifyText = function () { const id = app.$select.val(); if (id) { app.getActiveEditor().execCommand('mceInsertLink', false, { href: getMonsterlink(id), target: '_blank', rel: 'noopener noreferrer', }); } }; /** * Open campaign monsterlink modal * * @since 2.3.0 * * @returns {void} */ app.modalOpenLink = function () { // Show our modal. app.$toToggle.addClass('optin-monster-modal-monsterlink').removeClass('optin-monster-modal-inline'); app.$body.addClass('modal-open om-modal-open-monsterlink'); app.$modalWrap.show(); // When opening link modal, set "selected" option, if URL set. app.updateLinkSelectOptions(app.$select); // Trigger the original link link options button. // This is a hack... // We need this to be "open" (though we hide it with CSS) // In order for the mce selection to remain in place, otherwise focus shifts. const $optionsBtn = $('.wp-link-input').parent().find('.dashicons-admin-generic').parent(); $optionsBtn.click(); $(document).trigger('om-modal-open-monsterlink'); }; /** * Open campaign shortcode modal * * @since 2.3.0 * * @returns {void} */ app.modalOpenInline = function () { app.$toToggle.addClass('optin-monster-modal-inline').removeClass('optin-monster-modal-monsterlink').show(); app.$body.addClass('modal-open om-modal-open-inline'); app.updateInlineSelectOptions(); $(document).trigger('om-modal-open-inline'); }; /** * Close campaign shortcode modal * * @since 2.3.0 * * @returns {void} */ app.modalClose = function () { // When closing our modals, empty value for our campaign selects. ['$select', '$linkSelect', '$inlineSelect'].forEach((k) => { if (app[k] && app[k].length) { app[k].val(''); } }); app.$toToggle.hide(); const type = app.$body.hasClass('om-modal-open-monsterlink') ? 'monsterlink' : 'inline'; app.$body.removeClass('modal-open om-modal-open-monsterlink om-modal-open-inline'); $(document).trigger(`om-modal-close-${type}`); }; /** * Insert the selected campaign shortcode to the editor. * * @since 2.3.0 * * @returns {void} */ app.insertShortcode = function () { const id = app.$inlineSelect.val(); if (id) { wp.media.editor.insert(`[optin-monster slug="${id}" followrules="true"]`); } }; /** * If url already has value, check if it matches our monsterlink options. * * @since 2.3.0 * * @param {Object} $select jQuery object for campaign-select element. * * @returns {void} */ app.updateLinkSelectOptions = function ($select) { const $selector = $('#wp-link-wrap #link-selector'); const $search = $selector.find('#search-panel'); const searchBottom = $search.offset().top + $search.outerHeight(); const top = searchBottom - $selector.offset().top + 12; /* margin */ $('.has-text-field #wp-link .query-results').css({ top }); const url = $('.wp-link-input input.ui-autocomplete-input').val(); if (url) { $select.find('option').each(function () { const val = $(this).val(); if (val && url === getMonsterlink(val)) { $select.val(val); } }); } }; /** * Disable any options already in use. * * @since 2.3.0 * * @returns {void} */ app.updateInlineSelectOptions = function () { const editorId = app.getActiveEditorId(); // No luck... if (!editorId) { return; } const editor = app.getActiveEditor(); const editorText = editor && !editor.isHidden() ? editor.getContent() : document.getElementById(editorId).value; // Set options to disabled if they are already used. app.$inlineSelect.find('option').each(function () { const $option = $(this); const hasShortcode = editorText.indexOf(`optin-monster slug="${$option.val()}"`) >= 0; $option.attr('disabled', hasShortcode); }); }; /** * Add the monsterlink button to the wplink modal. * (which triggers the monsterlink-select modal) * * @since 2.3.0 * * @returns {void} */ app.initLinkButton = function () { $('.wp-link-input').each(function () { const $modal = $(this).parent(); if (!$modal.find('.optin-monster-insert-monsterlink').length) { const $div = $( '
' ); const $button = $( '' ); $button.append($('.wp-media-buttons-icon.optin-monster-menu-icon').first().clone()); $div.append($button); $modal.find('.mce-last').removeClass('mce-last'); $modal.append($div); } }); }; /** * Add the monsterlink select to the wplink advanced modal. * * @since 2.3.0 * * @returns {void} */ app.initAdvancedSettings = function () { const $advanced = $(`${app.i18n.or_monsterlink}
${ app.i18n.upgrade_monsterlink } ${app.i18n.upgrade}
` }