From 0cde2704d09ecb03936683ea0446be0650e6918d Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 5 Feb 2022 21:20:20 +0000 Subject: [PATCH] Made further tweaks to align with current editor - Ensured each of the core actions worked at a high level. - Handled some TinyMCE API changes. - Moved code block insert to its own button. --- resources/js/components/wysiwyg-editor.js | 65 ++++++++--------------- resources/sass/_tinymce.scss | 4 ++ 2 files changed, 26 insertions(+), 43 deletions(-) diff --git a/resources/js/components/wysiwyg-editor.js b/resources/js/components/wysiwyg-editor.js index 9d6f70e32..100a70ff4 100644 --- a/resources/js/components/wysiwyg-editor.js +++ b/resources/js/components/wysiwyg-editor.js @@ -189,9 +189,11 @@ function codePlugin() { const $ = editor.$; + editor.ui.registry.addIcon('codeblock', '') + editor.ui.registry.addButton('codeeditor', { - text: 'Code block', - icon: false, + title: 'Insert code block', + icon: 'codeblock', onAction() { editor.execCommand('codeeditor'); } @@ -455,7 +457,7 @@ class WysiwygEditor { this.serverUploadLimitText = this.$opts.serverUploadLimitText; this.isDarkMode = document.documentElement.classList.contains('dark-mode'); - this.plugins = "image imagetools table textcolor paste link autolink fullscreen code customhr autosave lists codeeditor media"; + this.plugins = "image imagetools table paste link autolink fullscreen code customhr autosave lists codeeditor media"; this.loadPlugins(); this.tinyMceConfig = this.getTinyMceConfig(); @@ -481,7 +483,7 @@ class WysiwygEditor { getToolBar() { const textDirPlugins = this.textDirection === 'rtl' ? 'ltr rtl' : ''; - return `undo redo | styleselect | bold italic underline strikethrough superscript subscript | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table image-insert link hr drawio media | removeformat code ${textDirPlugins} fullscreen` + return `undo redo | styleselect | bold italic underline strikethrough superscript subscript | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table image-insert link hr codeeditor drawio media | removeformat code ${textDirPlugins} fullscreen` } getTinyMceConfig() { @@ -512,6 +514,7 @@ class WysiwygEditor { valid_children: "-div[p|h1|h2|h3|h4|h5|h6|blockquote],+div[pre],+div[img]", plugins: this.plugins, imagetools_toolbar: 'imageoptions', + contextmenu: false, toolbar: this.getToolBar(), content_style: `html, body, html.dark-mode {background: ${this.isDarkMode ? '#222' : '#fff'};} body {padding-left: 15px !important; padding-right: 15px !important; margin:0!important; margin-left:auto!important;margin-right:auto!important;}`, style_formats: [ @@ -521,7 +524,6 @@ class WysiwygEditor { {title: "Header Tiny", format: "h5"}, {title: "Paragraph", format: "p", exact: true, classes: ''}, {title: "Blockquote", format: "blockquote"}, - {title: "", cmd: 'codeeditor', format: 'codeeditor'}, {title: "Inline Code", inline: "code"}, {title: "Callouts", items: [ {title: "Info", format: 'calloutinfo'}, @@ -543,60 +545,37 @@ class WysiwygEditor { calloutwarning: {block: 'p', exact: true, attributes: {class: 'callout warning'}}, calloutdanger: {block: 'p', exact: true, attributes: {class: 'callout danger'}} }, - file_browser_callback: function (field_name, url, type, win) { + file_picker_types: 'file image', + file_picker_callback(callback, value, meta) { - if (type === 'file') { - window.EntitySelectorPopup.show(function(entity) { - const originalField = win.document.getElementById(field_name); - originalField.value = entity.link; - const mceForm = originalField.closest('.mce-form'); - const inputs = mceForm.querySelectorAll('input'); - - // Set text to display if not empty - if (!inputs[1].value) { - inputs[1].value = entity.name; - } - - // Set title field - inputs[2].value = entity.name; + // field_name, url, type, win + if (meta.filetype === 'file') { + window.EntitySelectorPopup.show(entity => { + callback(entity.link, { + text: entity.name, + title: entity.name, + }); }); } - if (type === 'image') { + if (meta.filetype === 'image') { // Show image manager window.ImageManager.show(function (image) { - - // Set popover link input to image url then fire change event - // to ensure the new value sticks - win.document.getElementById(field_name).value = image.url; - if ("createEvent" in document) { - let evt = document.createEvent("HTMLEvents"); - evt.initEvent("change", false, true); - win.document.getElementById(field_name).dispatchEvent(evt); - } else { - win.document.getElementById(field_name).fireEvent("onchange"); - } - - // Replace the actively selected content with the linked image - const imageUrl = image.thumbs.display || image.url; - let html = ``; - html += `${image.name}`; - html += ''; - win.tinyMCE.activeEditor.execCommand('mceInsertContent', false, html); + callback(image.url, {alt: image.name}); }, 'gallery'); } }, - paste_preprocess: function (plugin, args) { + paste_preprocess(plugin, args) { let content = args.content; if (content.indexOf('