2023-02-28 23:22:35 +08:00
|
|
|
|
/* global I18n GLOBAL_CONSTANTS HelperModule SmartAnnotation TinyMCE */
|
2022-09-08 19:52:56 +08:00
|
|
|
|
|
2022-09-05 17:29:19 +08:00
|
|
|
|
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';
|
2023-02-21 18:23:52 +08:00
|
|
|
|
import 'tinymce/plugins/code';
|
2022-09-05 17:29:19 +08:00
|
|
|
|
import 'tinymce/plugins/autolink';
|
|
|
|
|
import 'tinymce/plugins/lists';
|
2023-02-13 22:33:56 +08:00
|
|
|
|
import 'tinymce/plugins/image';
|
2022-09-05 17:29:19 +08:00
|
|
|
|
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';
|
2022-09-21 18:01:08 +08:00
|
|
|
|
import 'tinymce/plugins/help';
|
2023-08-22 16:24:45 +08:00
|
|
|
|
import 'tinymce/plugins/help/js/i18n/keynav/en';
|
2022-09-21 18:01:08 +08:00
|
|
|
|
import 'tinymce/plugins/quickbars';
|
2022-09-05 17:29:19 +08:00
|
|
|
|
import 'tinymce/plugins/directionality';
|
2022-09-07 20:52:56 +08:00
|
|
|
|
import './tinymce/custom_image_uploader/plugin';
|
2022-09-06 19:02:50 +08:00
|
|
|
|
import './tinymce/marvinjs/plugin';
|
2022-09-07 17:24:54 +08:00
|
|
|
|
import './tinymce/image_toolbar/plugin';
|
2022-09-05 17:29:19 +08:00
|
|
|
|
|
2023-01-11 22:28:09 +08:00
|
|
|
|
// Content styles, including inline UI like fake cursors
|
|
|
|
|
// All the above CSS files are loaded on to the page but these two must
|
|
|
|
|
// be loaded into the editor iframe so they are loaded as strings and passed
|
|
|
|
|
// to the init function.
|
|
|
|
|
import 'raw-loader';
|
|
|
|
|
import contentCss from '!!raw-loader!tinymce/skins/content/default/content.min.css';
|
|
|
|
|
import contentUiCss from '!!raw-loader!tinymce/skins/ui/tinymce-5/content.min.css';
|
2023-01-19 18:39:14 +08:00
|
|
|
|
|
2023-02-07 20:00:14 +08:00
|
|
|
|
const contentPStyle = `p { margin: 0; padding: 0;}`;
|
2023-06-15 20:23:01 +08:00
|
|
|
|
const contentBodyStyle = `body { font-family: "SN Inter", "Open Sans", Arial, Helvetica, sans-serif }`;
|
2023-02-07 20:00:14 +08:00
|
|
|
|
const contentStyle = [contentCss, contentUiCss, contentBodyStyle, contentPStyle].map((s) => s.toString()).join('\n');
|
2023-01-11 22:28:09 +08:00
|
|
|
|
|
2023-02-06 23:05:13 +08:00
|
|
|
|
// Optional pre-initialization method
|
|
|
|
|
if (typeof(window.preTinyMceInit) === 'function') {
|
|
|
|
|
window.preTinyMceInit();
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
window.TinyMCE = (() => {
|
|
|
|
|
function makeItDirty(editor) {
|
|
|
|
|
const editorForm = $(editor.getContainer()).closest('form');
|
|
|
|
|
editorForm.find('.tinymce-status-badge').addClass('hidden');
|
|
|
|
|
$(editor.getContainer()).find('.tinymce-save-button').removeClass('hidden');
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-05 17:29:19 +08:00
|
|
|
|
// Get LocalStorage auto save path
|
|
|
|
|
function getAutoSavePrefix(editor) {
|
2022-09-08 19:52:56 +08:00
|
|
|
|
let prefix = editor.getParam('autosave_prefix', 'tinymce-autosave-{path}{query}{hash}-{id}-');
|
2022-09-05 17:29:19 +08:00
|
|
|
|
|
|
|
|
|
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) {
|
2022-09-08 19:52:56 +08:00
|
|
|
|
const prefix = getAutoSavePrefix(editor);
|
|
|
|
|
const lastDraftTime = parseInt(tinyMCE.util.LocalStorage.getItem(`${prefix}time`), 10);
|
|
|
|
|
const lastUpdated = $(selector).data('last-updated');
|
|
|
|
|
let notificationBar;
|
|
|
|
|
const restoreBtn = $('<button class="btn restore-draft-btn">Restore Draft</button>');
|
2023-06-08 23:33:50 +08:00
|
|
|
|
const cancelBtn = $('<span class="sn-icon sn-icon-close"></span>');
|
2022-09-05 17:29:19 +08:00
|
|
|
|
|
|
|
|
|
// Check whether we have draft stored
|
|
|
|
|
|
|
|
|
|
if (editor.plugins.autosave.hasDraft()) {
|
|
|
|
|
notificationBar = $('<div class="restore-draft-notification"></div>');
|
|
|
|
|
|
|
|
|
|
if (lastDraftTime < lastUpdated) {
|
|
|
|
|
notificationBar.html(`<span class="notification-text">${I18n.t('tiny_mce.older_version_available')}</span>`);
|
|
|
|
|
} else {
|
|
|
|
|
notificationBar.html(`<span class="notification-text">${I18n.t('tiny_mce.newer_version_available')}</span>`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add notification bar
|
|
|
|
|
$(notificationBar).append(restoreBtn).append(cancelBtn);
|
|
|
|
|
$(editor.contentAreaContainer).before(notificationBar);
|
|
|
|
|
|
2023-01-10 20:24:58 +08:00
|
|
|
|
// Prevents save on blur if clicking draft notification
|
|
|
|
|
$('.restore-draft-notification').on('mousedown', () => {
|
|
|
|
|
editor.isBlurTempDisabled = true;
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
editor.isBlurTempDisabled = false;
|
|
|
|
|
}, 500);
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
$(restoreBtn).click(() => {
|
2022-09-05 17:29:19 +08:00
|
|
|
|
editor.plugins.autosave.restoreDraft();
|
|
|
|
|
makeItDirty(editor);
|
|
|
|
|
notificationBar.remove();
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
$(cancelBtn).click(() => {
|
2022-09-05 17:29:19 +08:00
|
|
|
|
notificationBar.remove();
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-09-09 21:42:53 +08:00
|
|
|
|
|
|
|
|
|
setTimeout(() => { tinyMCE.activeEditor.execCommand('mceAutoResize') }, 500);
|
2022-09-05 17:29:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 21:06:47 +08:00
|
|
|
|
function initCssOverrides(editor) {
|
2022-09-08 19:52:56 +08:00
|
|
|
|
const editorIframe = $(`#${editor.id}`).next().find('.tox-edit-area iframe');
|
|
|
|
|
const primaryColor = '#104da9';
|
2022-09-05 17:29:19 +08:00
|
|
|
|
editorIframe.contents().find('head').append(`<style type="text/css">
|
|
|
|
|
img::-moz-selection{background:0 0}
|
|
|
|
|
img::selection{background:0 0}
|
|
|
|
|
.mce-content-body img[data-mce-selected]{outline:2px solid ${primaryColor}}
|
2022-09-07 17:24:54 +08:00
|
|
|
|
.mce-content-body div.mce-resizehandle{background:transparent;border-color:transparent;box-sizing:border-box;height:10px;width:10px; position:absolute}
|
2022-09-05 17:29:19 +08:00
|
|
|
|
.mce-content-body div.mce-resizehandle:hover{background:transparent}
|
|
|
|
|
.mce-content-body div#mceResizeHandlenw{border-left: 2px solid ${primaryColor}; border-top: 2px solid ${primaryColor}}
|
|
|
|
|
.mce-content-body div#mceResizeHandlene{border-right: 2px solid ${primaryColor}; border-top: 2px solid ${primaryColor}}
|
|
|
|
|
.mce-content-body div#mceResizeHandlesw{border-left: 2px solid ${primaryColor}; border-bottom: 2px solid ${primaryColor}}
|
|
|
|
|
.mce-content-body div#mceResizeHandlese{border-right: 2px solid ${primaryColor}; border-bottom: 2px solid ${primaryColor}}
|
2023-02-01 20:59:21 +08:00
|
|
|
|
h1 {font-size: 24px !important }
|
|
|
|
|
h2 {font-size: 18px !important }
|
|
|
|
|
h3 {font-size: 16px !important }
|
2022-09-05 17:29:19 +08:00
|
|
|
|
</style>`);
|
2023-02-14 21:06:47 +08:00
|
|
|
|
editorIframe.contents().find('head').append($('#font-css-pack').clone());
|
2022-09-05 17:29:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function draftLocation() {
|
2022-09-08 19:52:56 +08:00
|
|
|
|
return `tinymce-drafts-${document.location.pathname}`;
|
2022-09-05 17:29:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removeDraft(editor, textAreaObject) {
|
2022-09-08 19:52:56 +08:00
|
|
|
|
const location = draftLocation();
|
|
|
|
|
const storedDrafts = JSON.parse(sessionStorage.getItem(location) || '[]');
|
|
|
|
|
const draftId = storedDrafts.indexOf(textAreaObject.data('tinymce-object'));
|
2022-09-05 17:29:19 +08:00
|
|
|
|
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) {
|
2022-09-08 19:52:56 +08:00
|
|
|
|
const editorForm = $(editor.getContainer()).closest('form');
|
2022-09-05 17:29:19 +08:00
|
|
|
|
editorForm.clearFormErrors();
|
|
|
|
|
editor.setProgressState(1);
|
|
|
|
|
editor.save();
|
|
|
|
|
editorForm.submit();
|
|
|
|
|
updateScrollPosition(editorForm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns a public API for TinyMCE editor
|
2022-09-08 19:52:56 +08:00
|
|
|
|
return {
|
2022-09-09 21:42:53 +08:00
|
|
|
|
init: (selector, options = {}) => {
|
2022-09-08 19:52:56 +08:00
|
|
|
|
const textAreaObject = $(selector);
|
2022-09-09 21:42:53 +08:00
|
|
|
|
let editorToolbaroffset = 0;
|
|
|
|
|
|
2022-09-05 17:29:19 +08:00
|
|
|
|
if (typeof tinyMCE !== 'undefined') {
|
|
|
|
|
// Hide element containing HTML view of RTE field
|
2022-09-08 19:52:56 +08:00
|
|
|
|
const tinyMceContainer = $(selector).closest('form').find('.tinymce-view');
|
2023-10-17 22:49:19 +08:00
|
|
|
|
const editorForm = $(selector).closest('form');
|
2022-09-08 19:52:56 +08:00
|
|
|
|
const tinyMceInitSize = tinyMceContainer.height();
|
2023-10-17 22:49:19 +08:00
|
|
|
|
|
|
|
|
|
editorForm.parent().height(tinyMceInitSize);
|
2022-09-05 17:29:19 +08:00
|
|
|
|
tinyMceContainer.addClass('hidden');
|
2023-10-17 22:49:19 +08:00
|
|
|
|
|
2022-09-21 18:01:08 +08:00
|
|
|
|
const plugins = `
|
2023-02-21 18:23:52 +08:00
|
|
|
|
image table autosave autoresize link advlist codesample code autolink lists
|
2022-09-21 18:01:08 +08:00
|
|
|
|
charmap anchor searchreplace wordcount visualblocks visualchars
|
|
|
|
|
insertdatetime nonbreaking save directionality customimageuploader
|
2023-02-06 23:05:13 +08:00
|
|
|
|
marvinjs custom_image_toolbar help quickbars ${window.extraTinyMcePlugins ? window.extraTinyMcePlugins : ''}
|
2022-09-21 18:01:08 +08:00
|
|
|
|
`;
|
2022-09-05 17:29:19 +08:00
|
|
|
|
// if (typeof (MarvinJsEditor) !== 'undefined') plugins += ' marvinjsplugin';
|
|
|
|
|
|
|
|
|
|
if (textAreaObject.data('objectType') === 'step'
|
|
|
|
|
|| textAreaObject.data('objectType') === 'result_text') {
|
2022-09-08 19:52:56 +08:00
|
|
|
|
document.location.hash = `${textAreaObject.data('objectType')}_${textAreaObject.data('objectId')}`;
|
2022-09-05 17:29:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-05 18:06:48 +08:00
|
|
|
|
editorToolbaroffset = 0;
|
2023-08-24 20:21:30 +08:00
|
|
|
|
|
2023-10-06 19:41:04 +08:00
|
|
|
|
$.each($('.sticky-header-element, .sticky-header'), (_index, element) => {
|
2023-09-05 18:06:48 +08:00
|
|
|
|
editorToolbaroffset += $(element).outerHeight();
|
|
|
|
|
});
|
2022-09-09 21:42:53 +08:00
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
return tinyMCE.init({
|
2023-07-05 15:52:05 +08:00
|
|
|
|
cache_suffix: '?v=6.5.1-19', // This suffix should be changed any time library is updated
|
2022-09-08 19:52:56 +08:00
|
|
|
|
selector,
|
2023-01-11 22:28:09 +08:00
|
|
|
|
skin: false,
|
2023-10-30 21:54:35 +08:00
|
|
|
|
editimage_fetch_image: img => new Promise((resolve) => {
|
|
|
|
|
// Appending a timestamp to an image URL bypasses Chrome’s cache, resolving occasional CORS errors
|
|
|
|
|
fetch(img.src + '?t=' + new Date().getTime())
|
|
|
|
|
.then(response => response.blob())
|
|
|
|
|
.then(blob => resolve(blob));
|
|
|
|
|
}),
|
2023-01-11 22:28:09 +08:00
|
|
|
|
content_css: false,
|
|
|
|
|
content_style: contentStyle,
|
2022-09-05 17:29:19 +08:00
|
|
|
|
convert_urls: false,
|
2022-12-09 19:52:18 +08:00
|
|
|
|
promotion: false,
|
2022-09-07 20:52:56 +08:00
|
|
|
|
menu: {
|
|
|
|
|
insert: { title: 'Insert', items: 'link codesample inserttable | charmap hr | nonbreaking anchor | insertdatetime customimageuploader marvinjs' },
|
|
|
|
|
},
|
2023-02-01 20:59:21 +08:00
|
|
|
|
block_formats: 'Paragraph=p; Header 1=h1; Header 2=h2; Header 3=h3; Preformatted=pre',
|
2022-09-05 17:29:19 +08:00
|
|
|
|
menubar: 'file edit view insert format table',
|
2023-07-05 15:52:05 +08:00
|
|
|
|
toolbar: window.customTinyMceToolbar || 'undo redo restoredraft | insert | styleselect | bold italic | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table | customimageuploader | marvinjs link codesample | help',
|
2022-09-08 19:52:56 +08:00
|
|
|
|
plugins,
|
2023-02-07 18:40:18 +08:00
|
|
|
|
contextmenu: '',
|
2022-09-05 17:29:19 +08:00
|
|
|
|
autoresize_bottom_margin: 20,
|
2022-12-09 19:52:18 +08:00
|
|
|
|
placeholder: options.placeholder,
|
2022-09-09 21:42:53 +08:00
|
|
|
|
toolbar_sticky: true,
|
|
|
|
|
toolbar_sticky_offset: editorToolbaroffset,
|
2023-02-28 23:22:35 +08:00
|
|
|
|
codesample_global_prismjs: true,
|
2022-09-05 17:29:19 +08:00
|
|
|
|
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,
|
2022-09-21 18:01:08 +08:00
|
|
|
|
quickbars_insert_toolbar: false,
|
2023-06-14 16:38:17 +08:00
|
|
|
|
toolbar_mode: 'sliding',
|
|
|
|
|
color_default_background: 'yellow',
|
2023-08-22 20:59:34 +08:00
|
|
|
|
link_default_target: 'external',
|
2023-04-03 18:49:04 +08:00
|
|
|
|
mobile: {
|
|
|
|
|
menubar: 'file edit view insert format table'
|
|
|
|
|
},
|
2023-08-22 20:59:34 +08:00
|
|
|
|
link_target_list: [
|
|
|
|
|
{ title: 'New page', value: 'external' },
|
2022-09-05 17:29:19 +08:00
|
|
|
|
{ title: 'Same page', value: '_self' }
|
|
|
|
|
],
|
|
|
|
|
style_formats: [
|
|
|
|
|
{
|
|
|
|
|
title: 'Headers',
|
|
|
|
|
items: [
|
|
|
|
|
{ title: 'Header 1', format: 'h1' },
|
|
|
|
|
{ title: 'Header 2', format: 'h2' },
|
2023-02-01 20:59:21 +08:00
|
|
|
|
{ title: 'Header 3', format: 'h3' }
|
2022-09-05 17:29:19 +08:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Inline',
|
|
|
|
|
items: [
|
|
|
|
|
{ title: 'Bold', icon: 'bold', format: 'bold' },
|
|
|
|
|
{ title: 'Italic', icon: 'italic', format: 'italic' },
|
|
|
|
|
{ title: 'Underline', icon: 'underline', format: 'underline' },
|
2023-01-18 23:43:50 +08:00
|
|
|
|
{ title: 'Strikethrough', icon: 'strike-through', format: 'strikethrough' },
|
2022-09-05 17:29:19 +08:00
|
|
|
|
{ title: 'Superscript', icon: 'superscript', format: 'superscript' },
|
|
|
|
|
{ title: 'Subscript', icon: 'subscript', format: 'subscript' },
|
2023-01-18 23:43:50 +08:00
|
|
|
|
{ title: 'Code', icon: 'sourcecode', format: 'code' }
|
2022-09-05 17:29:19 +08:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Blocks',
|
|
|
|
|
items: [
|
|
|
|
|
{ title: 'Paragraph', format: 'p' },
|
|
|
|
|
{ title: 'Blockquote', format: 'blockquote' }
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'Alignment',
|
|
|
|
|
items: [
|
2023-01-18 23:43:50 +08:00
|
|
|
|
{ title: 'Left', icon: 'align-left', format: 'alignleft' },
|
|
|
|
|
{ title: 'Center', icon: 'align-center', format: 'aligncenter' },
|
|
|
|
|
{ title: 'Right', icon: 'align-right', format: 'alignright' },
|
|
|
|
|
{ title: 'Justify', icon: 'align-justify', format: 'alignjustify' }
|
2022-09-05 17:29:19 +08:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
2022-09-08 19:52:56 +08:00
|
|
|
|
init_instance_callback: (editor) => {
|
2022-12-09 19:52:18 +08:00
|
|
|
|
|
|
|
|
|
const editorContainer = $(editor.getContainer());
|
|
|
|
|
const editorForm = editorContainer.closest('form');
|
2022-09-08 19:52:56 +08:00
|
|
|
|
const menuBar = editorForm.find('.tox-menubar');
|
2022-09-05 17:29:19 +08:00
|
|
|
|
|
2023-10-17 22:49:19 +08:00
|
|
|
|
editorContainer.addClass('tox-tinymce--loaded');
|
|
|
|
|
const event = new CustomEvent('tinyMCEOpened', {
|
|
|
|
|
detail: {
|
|
|
|
|
target: editorForm.parent(),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
window.dispatchEvent(event);
|
|
|
|
|
editorForm.parent().css('height', '');
|
2022-09-05 17:29:19 +08:00
|
|
|
|
|
|
|
|
|
// Init saved status label
|
|
|
|
|
if (editor.getContent() !== '') {
|
|
|
|
|
editorForm.find('.tinymce-status-badge').removeClass('hidden');
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 15:15:43 +08:00
|
|
|
|
// Remove transition class
|
|
|
|
|
$('.tox-editor-header').removeClass('tox-editor-dock-fadeout');
|
|
|
|
|
|
2024-03-05 16:29:24 +08:00
|
|
|
|
// Fixes the overflowing vertical controls bar for inserted text
|
|
|
|
|
$('.tox-editor-header').css('display', 'contents');
|
|
|
|
|
|
2022-09-05 17:29:19 +08:00
|
|
|
|
// Init image toolbar
|
2023-02-14 21:06:47 +08:00
|
|
|
|
initCssOverrides(editor);
|
2022-09-05 17:29:19 +08:00
|
|
|
|
|
|
|
|
|
// Init save/cancel button wrapper
|
|
|
|
|
$('<div class="tinymce-save-controls"></div>').appendTo(menuBar);
|
|
|
|
|
|
|
|
|
|
// Init Save button
|
|
|
|
|
editorForm
|
|
|
|
|
.find('.tinymce-save-button')
|
|
|
|
|
.clone()
|
|
|
|
|
.appendTo(menuBar.find('.tinymce-save-controls'))
|
2022-09-08 19:52:56 +08:00
|
|
|
|
.on('click', (event) => {
|
2022-09-05 17:29:19 +08:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
saveAction(editor);
|
2023-05-25 22:01:36 +08:00
|
|
|
|
SmartAnnotation.closePopup();
|
2022-09-05 17:29:19 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// After save action
|
|
|
|
|
editorForm
|
2022-09-08 19:52:56 +08:00
|
|
|
|
.on('ajax:success', (_ev, data) => {
|
2022-09-05 17:29:19 +08:00
|
|
|
|
editor.save();
|
|
|
|
|
editor.setProgressState(0);
|
|
|
|
|
editorForm.find('.tinymce-status-badge').removeClass('hidden');
|
|
|
|
|
editor.remove();
|
|
|
|
|
editorForm.find('.tinymce-view').html(data.html).removeClass('hidden');
|
2023-01-19 19:56:15 +08:00
|
|
|
|
TinyMCE.wrapTables(editorForm.find('.tinymce-view'));
|
2022-09-05 17:29:19 +08:00
|
|
|
|
editor.plugins.autosave.removeDraft();
|
|
|
|
|
removeDraft(editor, textAreaObject);
|
|
|
|
|
if (options.onSaveCallback) { options.onSaveCallback(data); }
|
2022-09-08 19:52:56 +08:00
|
|
|
|
}).on('ajax:error', (_ev, data) => {
|
|
|
|
|
const model = editor.getElement().dataset.objectType;
|
2023-04-25 20:34:14 +08:00
|
|
|
|
let form = $(editor.getElement().closest('.form-group'));
|
|
|
|
|
form.renderFormErrors(model, data.responseJSON);
|
|
|
|
|
|
|
|
|
|
// to show bottom of the tinyMce editor instead at the top
|
|
|
|
|
form.find('.help-block').insertAfter(form.children().last());
|
2022-09-05 17:29:19 +08:00
|
|
|
|
editor.setProgressState(0);
|
|
|
|
|
if (data.status === 403) {
|
|
|
|
|
HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger');
|
2023-04-03 21:34:59 +08:00
|
|
|
|
} else if (data.status === 422) {
|
|
|
|
|
HelperModule.flashAlertMsg(data.responseJSON ? Object.values(data.responseJSON).join(', ') : I18n.t('errors.general'), 'danger');
|
2022-09-05 17:29:19 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Init Cancel button
|
|
|
|
|
editorForm
|
|
|
|
|
.find('.tinymce-cancel-button')
|
|
|
|
|
.clone()
|
2022-09-09 21:42:53 +08:00
|
|
|
|
.prependTo(menuBar.find('.tinymce-save-controls'))
|
2022-09-08 19:52:56 +08:00
|
|
|
|
.on('click', (event) => {
|
2022-09-05 17:29:19 +08:00
|
|
|
|
$(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);
|
2022-09-09 21:42:53 +08:00
|
|
|
|
if (options.onSaveCallback) { options.onSaveCallback($(selector).val()); }
|
2023-05-25 22:01:36 +08:00
|
|
|
|
|
|
|
|
|
SmartAnnotation.closePopup();
|
2022-09-05 17:29:19 +08:00
|
|
|
|
})
|
|
|
|
|
.removeClass('hidden');
|
|
|
|
|
|
|
|
|
|
editor.selection.select(editor.getBody(), true);
|
|
|
|
|
editor.selection.collapse(false);
|
|
|
|
|
|
2023-04-25 21:12:19 +08:00
|
|
|
|
SmartAnnotation.init($(editor.contentDocument.activeElement), false, options.assignableMyModuleId);
|
2022-09-05 17:29:19 +08:00
|
|
|
|
SmartAnnotation.preventPropagation('.atwho-user-popover');
|
2022-12-09 19:52:18 +08:00
|
|
|
|
|
2022-12-22 17:37:47 +08:00
|
|
|
|
if (options.afterInitCallback) { options.afterInitCallback(); }
|
2022-09-05 17:29:19 +08:00
|
|
|
|
},
|
2022-09-08 19:52:56 +08:00
|
|
|
|
setup: (editor) => {
|
2023-01-10 20:24:58 +08:00
|
|
|
|
editor.isBlurTempDisabled = false;
|
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
editor.on('keydown', (e) => {
|
|
|
|
|
if (e.key === 'Enter' && $(editor.contentDocument.activeElement).atwho('isSelecting')) {
|
2022-09-05 17:29:19 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
editor.on('Dirty', () => {
|
2022-09-05 17:29:19 +08:00
|
|
|
|
makeItDirty(editor);
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
editor.on('StoreDraft', () => {
|
|
|
|
|
const location = draftLocation();
|
|
|
|
|
const storedDrafts = JSON.parse(sessionStorage.getItem(location) || '[]');
|
|
|
|
|
const draftName = textAreaObject.data('tinymce-object');
|
2022-09-05 17:29:19 +08:00
|
|
|
|
if (storedDrafts.includes(draftName) || !draftName) return;
|
|
|
|
|
storedDrafts.push(draftName);
|
|
|
|
|
sessionStorage.setItem(location, JSON.stringify(storedDrafts));
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
editor.on('remove', () => {
|
|
|
|
|
const menuBar = $(editor.getContainer()).find('.tox-menubar');
|
2022-09-05 17:29:19 +08:00
|
|
|
|
menuBar.find('.tinymce-save-button').remove();
|
|
|
|
|
menuBar.find('.tinymce-cancel-button').remove();
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
editor.on('blur', () => {
|
2023-01-10 20:24:58 +08:00
|
|
|
|
if (editor.isBlurTempDisabled || editor.blurDisabled) return false;
|
2022-09-05 17:29:19 +08:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
editor.on('init', () => {
|
2022-09-05 17:29:19 +08:00
|
|
|
|
restoreDraftNotification(selector, editor);
|
|
|
|
|
});
|
2023-08-22 20:59:34 +08:00
|
|
|
|
|
|
|
|
|
editor.on('BeforeSetContent GetContent', function(e) {
|
|
|
|
|
if (e.content && e.content.includes('target="external"')) {
|
|
|
|
|
const div = document.createElement('div');
|
|
|
|
|
div.innerHTML = e.content;
|
|
|
|
|
const links = div.querySelectorAll('a[target="external"]');
|
|
|
|
|
links.forEach(link => {
|
|
|
|
|
link.removeAttribute('target');
|
|
|
|
|
link.setAttribute('rel', 'external');
|
|
|
|
|
});
|
|
|
|
|
e.content = div.innerHTML;
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-09-05 17:29:19 +08:00
|
|
|
|
},
|
2022-09-08 19:52:56 +08:00
|
|
|
|
save_onsavecallback: (editor) => { saveAction(editor); }
|
2022-09-05 17:29:19 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
return null;
|
2022-09-05 17:29:19 +08:00
|
|
|
|
},
|
2022-09-08 19:52:56 +08:00
|
|
|
|
destroyAll: () => {
|
2022-09-09 21:42:53 +08:00
|
|
|
|
if (tinyMCE.activeEditor) {
|
|
|
|
|
tinyMCE.activeEditor.remove();
|
|
|
|
|
}
|
2022-09-05 17:29:19 +08:00
|
|
|
|
},
|
2022-09-08 19:52:56 +08:00
|
|
|
|
refresh: () => {
|
2022-09-05 17:29:19 +08:00
|
|
|
|
this.destroyAll();
|
|
|
|
|
this.init();
|
|
|
|
|
},
|
2023-01-11 22:28:09 +08:00
|
|
|
|
getContent: () => tinyMCE.activeEditor && tinyMCE.activeEditor.getContent(),
|
2022-09-08 19:52:56 +08:00
|
|
|
|
updateImages: (editor) => {
|
2022-09-09 21:42:53 +08:00
|
|
|
|
const iframe = $(`#${editor.id}`).next().find('.tox-edit-area iframe').contents();
|
2022-09-08 19:52:56 +08:00
|
|
|
|
const images = $.map($('img', iframe), e => e.dataset.mceToken);
|
2022-09-09 21:42:53 +08:00
|
|
|
|
$(`#${editor.id}`).parent().find('input.tiny-mce-images').val(JSON.stringify(images));
|
2022-09-05 17:29:19 +08:00
|
|
|
|
return JSON.stringify(images);
|
|
|
|
|
},
|
2022-09-08 19:52:56 +08:00
|
|
|
|
makeItDirty: (editor) => {
|
2022-09-05 17:29:19 +08:00
|
|
|
|
makeItDirty(editor);
|
|
|
|
|
},
|
2023-01-19 19:56:15 +08:00
|
|
|
|
wrapTables: (container) => {
|
|
|
|
|
container.find('table').toArray().forEach((table) => {
|
|
|
|
|
if ($(table).parent().hasClass('table-wrapper')) return;
|
|
|
|
|
|
|
|
|
|
$(table).css('float', 'none').wrapAll(`
|
|
|
|
|
<div class="table-wrapper" style="overflow: auto; width: ${container.width()}px"></div>
|
|
|
|
|
`);
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-09-08 19:52:56 +08:00
|
|
|
|
};
|
|
|
|
|
})();
|
2022-09-05 17:29:19 +08:00
|
|
|
|
|
2022-09-08 19:52:56 +08:00
|
|
|
|
$(document).on('turbolinks:before-visit', (e) => {
|
2022-09-09 21:42:53 +08:00
|
|
|
|
const editor = tinyMCE.activeEditor;
|
|
|
|
|
|
|
|
|
|
if (editor === null) return true;
|
|
|
|
|
|
2023-01-18 18:00:08 +08:00
|
|
|
|
if (editor.isDirty()) {
|
2022-09-09 21:42:53 +08:00
|
|
|
|
// eslint-disable-next-line no-alert
|
|
|
|
|
if (confirm(I18n.t('tiny_mce.leaving_warning'))) {
|
2023-01-18 18:00:08 +08:00
|
|
|
|
$('.atwho-container').remove();
|
|
|
|
|
tinyMCE.activeEditor.remove();
|
|
|
|
|
return true;
|
2022-09-05 17:29:19 +08:00
|
|
|
|
}
|
2022-09-09 21:42:53 +08:00
|
|
|
|
e.preventDefault();
|
2022-09-05 17:29:19 +08:00
|
|
|
|
return false;
|
2022-09-09 21:42:53 +08:00
|
|
|
|
}
|
2023-01-12 19:09:44 +08:00
|
|
|
|
return true;
|
2022-09-05 17:29:19 +08:00
|
|
|
|
});
|