/* global I18n hljs GLOBAL_CONSTANTS HelperModule SmartAnnotation */ import tinyMCE from 'tinymce/tinymce'; import 'tinymce/models/dom'; import 'tinymce/icons/default'; import 'tinymce/themes/silver'; import 'tinymce/plugins/table'; import 'tinymce/plugins/autosave'; import 'tinymce/plugins/autoresize'; import 'tinymce/plugins/link'; import 'tinymce/plugins/advlist'; import 'tinymce/plugins/codesample'; import 'tinymce/plugins/autolink'; import 'tinymce/plugins/lists'; import 'tinymce/plugins/charmap'; import 'tinymce/plugins/anchor'; import 'tinymce/plugins/searchreplace'; import 'tinymce/plugins/wordcount'; import 'tinymce/plugins/visualblocks'; import 'tinymce/plugins/visualchars'; import 'tinymce/plugins/insertdatetime'; import 'tinymce/plugins/nonbreaking'; import 'tinymce/plugins/save'; import 'tinymce/plugins/directionality'; import './tinymce/marvinjs/plugin'; import './tinymce/image_toolbar/plugin'; import './tinymce/placeholder/plugin'; window.TinyMCE = (() => { function initHighlightjs() { $('[class*=language]').each((i, block) => { hljs.highlightBlock(block); }); } function initHighlightjsIframe(iframe) { $('[class*=language]', iframe).each((i, block) => { hljs.highlightBlock(block); }); } function makeItDirty(editor) { const editorForm = $(editor.getContainer()).closest('form'); editorForm.find('.tinymce-status-badge').addClass('hidden'); $(editor.getContainer()).find('.tinymce-save-button').removeClass('hidden'); } // Get LocalStorage auto save path function getAutoSavePrefix(editor) { let prefix = editor.getParam('autosave_prefix', 'tinymce-autosave-{path}{query}{hash}-{id}-'); prefix = prefix.replace(/\{path\}/g, document.location.pathname); prefix = prefix.replace(/\{query\}/g, document.location.search); prefix = prefix.replace(/\{hash\}/g, document.location.hash); prefix = prefix.replace(/\{id\}/g, editor.id); return prefix; } // Handles autosave notification if draft is available in the local storage function restoreDraftNotification(selector, editor) { const prefix = getAutoSavePrefix(editor); const lastDraftTime = parseInt(tinyMCE.util.LocalStorage.getItem(`${prefix}time`), 10); const lastUpdated = $(selector).data('last-updated'); let notificationBar; const restoreBtn = $(''); const cancelBtn = $(''); // Check whether we have draft stored if (editor.plugins.autosave.hasDraft()) { notificationBar = $('
'); if (lastDraftTime < lastUpdated) { notificationBar.html(`${I18n.t('tiny_mce.older_version_available')}`); } else { notificationBar.html(`${I18n.t('tiny_mce.newer_version_available')}`); } // Add notification bar $(notificationBar).append(restoreBtn).append(cancelBtn); $(editor.contentAreaContainer).before(notificationBar); $(restoreBtn).click(() => { editor.plugins.autosave.restoreDraft(); makeItDirty(editor); notificationBar.remove(); }); $(cancelBtn).click(() => { notificationBar.remove(); }); } } function initImageToolBar(editor) { const editorIframe = $(`#${editor.id}`).next().find('.tox-edit-area iframe'); const primaryColor = '#104da9'; editorIframe.contents().find('head').append(``); } function draftLocation() { return `tinymce-drafts-${document.location.pathname}`; } function removeDraft(editor, textAreaObject) { const location = draftLocation(); const storedDrafts = JSON.parse(sessionStorage.getItem(location) || '[]'); const draftId = storedDrafts.indexOf(textAreaObject.data('tinymce-object')); if (draftId > -1) { storedDrafts.splice(draftId, 1); } if (storedDrafts.length) { sessionStorage.setItem(location, JSON.stringify(storedDrafts)); } else { sessionStorage.removeItem(location); } } // Update scroll position after exit function updateScrollPosition(editorForm) { if (editorForm.offset().top < $(window).scrollTop()) { $(window).scrollTop(editorForm.offset().top - 150); } } function saveAction(editor) { const editorForm = $(editor.getContainer()).closest('form'); editorForm.clearFormErrors(); editor.setProgressState(1); editor.save(); editorForm.submit(); updateScrollPosition(editorForm); } // returns a public API for TinyMCE editor return { init: (selector, options) => { const textAreaObject = $(selector); if (typeof tinyMCE !== 'undefined') { // Hide element containing HTML view of RTE field const tinyMceContainer = $(selector).closest('form').find('.tinymce-view'); const tinyMceInitSize = tinyMceContainer.height(); $(selector).closest('.form-group') .before(`
`); tinyMceContainer.addClass('hidden'); const plugins = 'table autosave autoresize link advlist codesample autolink lists charmap anchor searchreplace wordcount visualblocks visualchars insertdatetime nonbreaking save directionality marvinjs placeholder custom_image_toolbar'; // if (typeof (MarvinJsEditor) !== 'undefined') plugins += ' marvinjsplugin'; if (textAreaObject.data('objectType') === 'step' || textAreaObject.data('objectType') === 'result_text') { document.location.hash = `${textAreaObject.data('objectType')}_${textAreaObject.data('objectId')}`; } return tinyMCE.init({ cache_suffix: '?v=6.1.2', // This suffix should be changed any time library is updated selector, convert_urls: false, menubar: 'file edit view insert format table', toolbar: 'undo redo restoredraft | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table | link | forecolor backcolor | codesample | marvinjs', plugins, autoresize_bottom_margin: 20, // placeholder: options.placeholder, skin: false, content_css: false, content_style: "body { font-family: Lato, sans-serif; }", codesample_languages: [ { text: 'R', value: 'r' }, { text: 'MATLAB', value: 'matlab' }, { text: 'Python', value: 'python' }, { text: 'JSON', value: 'javascript' }, { text: 'HTML/XML', value: 'markup' }, { text: 'JavaScript', value: 'javascript' }, { text: 'CSS', value: 'css' }, { text: 'PHP', value: 'php' }, { text: 'Ruby', value: 'ruby' }, { text: 'Java', value: 'java' }, { text: 'C', value: 'c' }, { text: 'C#', value: 'csharp' }, { text: 'C++', value: 'cpp' } ], browser_spellcheck: true, branding: false, fixed_toolbar_container: '#mytoolbar', autosave_restore_when_empty: false, autosave_interval: '1s', autosave_retention: '1440m', removed_menuitems: 'newdocument', object_resizing: true, elementpath: false, forced_root_block: 'div', default_link_target: '_blank', target_list: [ { title: 'New page', value: '_blank' }, { title: 'Same page', value: '_self' } ], style_formats: [ { title: 'Headers', items: [ { title: 'Header 1', format: 'h1' }, { title: 'Header 2', format: 'h2' }, { title: 'Header 3', format: 'h3' }, { title: 'Header 4', format: 'h4' }, { title: 'Header 5', format: 'h5' }, { title: 'Header 6', format: 'h6' } ] }, { title: 'Inline', items: [ { title: 'Bold', icon: 'bold', format: 'bold' }, { title: 'Italic', icon: 'italic', format: 'italic' }, { title: 'Underline', icon: 'underline', format: 'underline' }, { title: 'Strikethrough', icon: 'strikethrough', format: 'strikethrough' }, { title: 'Superscript', icon: 'superscript', format: 'superscript' }, { title: 'Subscript', icon: 'subscript', format: 'subscript' }, { title: 'Code', icon: 'code', format: 'code' } ] }, { title: 'Blocks', items: [ { title: 'Paragraph', format: 'p' }, { title: 'Blockquote', format: 'blockquote' } ] }, { title: 'Alignment', items: [ { title: 'Left', icon: 'alignleft', format: 'alignleft' }, { title: 'Center', icon: 'aligncenter', format: 'aligncenter' }, { title: 'Right', icon: 'alignright', format: 'alignright' }, { title: 'Justify', icon: 'alignjustify', format: 'alignjustify' } ] } ], init_instance_callback: (editor) => { const editorForm = $(editor.getContainer()).closest('form'); const menuBar = editorForm.find('.tox-menubar'); const editorToolbar = editorForm.find('.mce-top-part'); let editorToolbaroffset; $('.tinymce-placeholder').css('height', `${$(editor.editorContainer).height()}px`); $(editor.editorContainer).css('max-height', '0').css('opacity', '0') setTimeout(() => { $(editor.editorContainer).css('max-height', '').css('opacity', '') $('.tinymce-placeholder').remove(); }, 400); // Init saved status label if (editor.getContent() !== '') { editorForm.find('.tinymce-status-badge').removeClass('hidden'); } if ($('.navbar-secondary').length) { editorToolbaroffset = $('.navbar-secondary').position().top + $('.navbar-secondary').height(); } else if ($('#main-nav').length) { editorToolbaroffset = $('#main-nav').height(); } else { editorToolbaroffset = 0; } if (GLOBAL_CONSTANTS.IS_SAFARI) { editorToolbar.css('position', '-webkit-sticky'); } else { editorToolbar.css('position', 'sticky'); } editorToolbar.css('top', `${editorToolbaroffset}px`).css('z-index', '100'); // Init image toolbar initImageToolBar(editor); // Init save/cancel button wrapper $('
').appendTo(menuBar); // Init Save button editorForm .find('.tinymce-save-button') .clone() .appendTo(menuBar.find('.tinymce-save-controls')) .on('click', (event) => { event.preventDefault(); saveAction(editor); }); // After save action editorForm .on('ajax:success', (_ev, data) => { editor.save(); editor.setProgressState(0); editorForm.find('.tinymce-status-badge').removeClass('hidden'); editor.remove(); editorForm.find('.tinymce-view').html(data.html).removeClass('hidden'); editor.plugins.autosave.removeDraft(); removeDraft(editor, textAreaObject); if (options.onSaveCallback) { options.onSaveCallback(data); } }).on('ajax:error', (_ev, data) => { const model = editor.getElement().dataset.objectType; $(this).renderFormErrors(model, data.responseJSON); editor.setProgressState(0); if (data.status === 403) { HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger'); } }); // Init Cancel button editorForm .find('.tinymce-cancel-button') .clone() .appendTo(menuBar.find('.tinymce-save-controls')) .on('click', (event) => { $(editorForm).find('.form-group').removeClass('has-error'); $(editorForm).find('.help-block').remove(); event.preventDefault(); if (editor.isDirty()) { editor.setContent($(selector).val()); } editorForm.find('.tinymce-status-badge').addClass('hidden'); editorForm.find('.tinymce-view').removeClass('hidden'); editor.remove(); updateScrollPosition(editorForm); if (options.onSaveCallback) { options.onSaveCallback(); } }) .removeClass('hidden'); editor.selection.select(editor.getBody(), true); editor.selection.collapse(false); SmartAnnotation.init($(editor.contentDocument.activeElement)); SmartAnnotation.preventPropagation('.atwho-user-popover'); initHighlightjsIframe($(editor.iframeElement).contents()); }, setup: (editor) => { editor.on('keydown', (e) => { if (e.key === 'Enter' && $(editor.contentDocument.activeElement).atwho('isSelecting')) { return false; } return true; }); editor.on('NodeChange', (e) => { const node = e.element; setTimeout(() => { if ($(node).is('pre') && !editor.isHidden()) { initHighlightjsIframe($(editor.iframeElement).contents()); } }, 200); }); editor.on('Dirty', () => { makeItDirty(editor); }); editor.on('StoreDraft', () => { const location = draftLocation(); const storedDrafts = JSON.parse(sessionStorage.getItem(location) || '[]'); const draftName = textAreaObject.data('tinymce-object'); if (storedDrafts.includes(draftName) || !draftName) return; storedDrafts.push(draftName); sessionStorage.setItem(location, JSON.stringify(storedDrafts)); }); editor.on('remove', () => { const menuBar = $(editor.getContainer()).find('.tox-menubar'); menuBar.find('.tinymce-save-button').remove(); menuBar.find('.tinymce-cancel-button').remove(); }); editor.on('blur', () => { if (editor.blurDisabled) return false; if ($('.atwho-view:visible').length || $('#MarvinJsModal:visible').length) return false; setTimeout(() => { if (editor.isNotDirty === false) { $(editor.container).find('.tinymce-save-button').click(); } else { $(editor.container).find('.tinymce-cancel-button').click(); } }, 0); return true; }); editor.on('init', () => { restoreDraftNotification(selector, editor); }); }, codesample_content_css: $(selector).data('highlightjs-path'), save_onsavecallback: (editor) => { saveAction(editor); } }); } return null; }, destroyAll: () => { $.each(tinyMCE.editors, (editor) => { if (editor) { editor.remove(); initHighlightjs(); } }); }, refresh: () => { this.destroyAll(); this.init(); }, getContent: () => tinyMCE.editors[0].getContent(), updateImages: (editor) => { const iframe = $(`#${editor.id}`).prev().find('.mce-edit-area iframe').contents(); const images = $.map($('img', iframe), e => e.dataset.mceToken); $(`#${editor.id}`).next()[0].value = JSON.stringify(images); return JSON.stringify(images); }, makeItDirty: (editor) => { makeItDirty(editor); }, highlight: initHighlightjs, initIfHasDraft: (viewObject) => { const storedDrafts = sessionStorage.getItem(draftLocation()); const draftName = viewObject.data('tinymce-init'); if (storedDrafts && JSON.parse(storedDrafts)[0] === draftName) { const top = viewObject.offset().top; setTimeout(() => { viewObject.click(); }, 0); setTimeout(() => { window.scrollTo(0, top - 150); }, 2000); } } }; })(); $(document).on('turbolinks:before-visit', (e) => { $.each(tinyMCE.editors, (editor) => { if (editor.isNotDirty === false) { // eslint-disable-next-line no-alert if (confirm(I18n.t('tiny_mce.leaving_warning'))) { return false; } e.preventDefault(); return false; } return false; }); });