4 * Handle pasting images from clipboard.
6 * @param editor - editor instance
8 function editorPaste(e, editor) {
9 if (!e.clipboardData) return;
10 let items = e.clipboardData.items;
12 for (let i = 0; i < items.length; i++) {
13 if (items[i].type.indexOf("image") === -1) return;
15 let file = items[i].getAsFile();
16 let formData = new FormData();
18 let xhr = new XMLHttpRequest();
21 let fileNameMatches = file.name.match(/\.(.+)$/);
22 if (fileNameMatches) {
23 ext = fileNameMatches[1];
27 let id = "image-" + Math.random().toString(16).slice(2);
28 let loadingImage = window.baseUrl('/loading.gif');
29 editor.execCommand('mceInsertContent', false, `<img src="${loadingImage}" id="${id}">`);
31 let remoteFilename = "image-" + Date.now() + "." + ext;
32 formData.append('file', file, remoteFilename);
33 formData.append('_token', document.querySelector('meta[name="token"]').getAttribute('content'));
35 xhr.open('POST', window.baseUrl('/images/gallery/upload'));
36 xhr.onload = function () {
37 if (xhr.status === 200 || xhr.status === 201) {
38 let result = JSON.parse(xhr.responseText);
39 editor.dom.setAttrib(id, 'src', result.thumbs.display);
41 console.log('An error occurred uploading the image', xhr.responseText);
42 editor.dom.remove(id);
50 function registerEditorShortcuts(editor) {
52 for (let i = 1; i < 5; i++) {
53 editor.addShortcut('meta+' + i, '', ['FormatBlock', false, 'h' + i]);
56 // Other block shortcuts
57 editor.addShortcut('meta+q', '', ['FormatBlock', false, 'blockquote']);
58 editor.addShortcut('meta+d', '', ['FormatBlock', false, 'p']);
59 editor.addShortcut('meta+e', '', ['FormatBlock', false, 'pre']);
60 editor.addShortcut('meta+shift+E', '', ['FormatBlock', false, 'code']);
63 export default function() {
65 selector: '#html-editor',
67 window.baseUrl('/css/styles.css'),
68 window.baseUrl('/libs/material-design-iconic-font/css/material-design-iconic-font.min.css')
70 body_class: 'page-content',
72 remove_script_host: false,
73 document_base_url: window.baseUrl('/'),
76 paste_data_images: false,
77 extended_valid_elements: 'pre[*]',
78 automatic_uploads: false,
79 valid_children: "-div[p|pre|h1|h2|h3|h4|h5|h6|blockquote]",
80 plugins: "image table textcolor paste link autolink fullscreen imagetools code customhr autosave lists",
81 imagetools_toolbar: 'imageoptions',
82 toolbar: "undo redo | styleselect | bold italic underline strikethrough superscript subscript | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table image-insert link hr | removeformat code fullscreen",
83 content_style: "body {padding-left: 15px !important; padding-right: 15px !important; margin:0!important; margin-left:auto!important;margin-right:auto!important;}",
85 {title: "Header Large", format: "h2"},
86 {title: "Header Medium", format: "h3"},
87 {title: "Header Small", format: "h4"},
88 {title: "Header Tiny", format: "h5"},
89 {title: "Paragraph", format: "p", exact: true, classes: ''},
90 {title: "Blockquote", format: "blockquote"},
91 {title: "Code Block", icon: "code", format: "pre"},
92 {title: "Inline Code", icon: "code", inline: "code"},
93 {title: "Callouts", items: [
94 {title: "Success", block: 'p', exact: true, attributes : {'class' : 'callout success'}},
95 {title: "Info", block: 'p', exact: true, attributes : {'class' : 'callout info'}},
96 {title: "Warning", block: 'p', exact: true, attributes : {'class' : 'callout warning'}},
97 {title: "Danger", block: 'p', exact: true, attributes : {'class' : 'callout danger'}}
100 style_formats_merge: false,
102 alignleft: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-left'},
103 aligncenter: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-center'},
104 alignright: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-right'},
106 file_browser_callback: function (field_name, url, type, win) {
108 if (type === 'file') {
109 window.showEntityLinkSelector(function(entity) {
110 let originalField = win.document.getElementById(field_name);
111 originalField.value = entity.link;
112 $(originalField).closest('.mce-form').find('input').eq(2).val(entity.name);
116 if (type === 'image') {
117 // Show image manager
118 window.ImageManager.showExternal(function (image) {
120 // Set popover link input to image url then fire change event
121 // to ensure the new value sticks
122 win.document.getElementById(field_name).value = image.url;
123 if ("createEvent" in document) {
124 let evt = document.createEvent("HTMLEvents");
125 evt.initEvent("change", false, true);
126 win.document.getElementById(field_name).dispatchEvent(evt);
128 win.document.getElementById(field_name).fireEvent("onchange");
131 // Replace the actively selected content with the linked image
132 let html = `<a href="${image.url}" target="_blank">`;
133 html += `<img src="${image.thumbs.display}" alt="${image.name}">`;
135 win.tinyMCE.activeEditor.execCommand('mceInsertContent', false, html);
140 paste_preprocess: function (plugin, args) {
141 let content = args.content;
142 if (content.indexOf('<img src="file://') !== -1) {
147 setup: function (editor) {
149 // Run additional setup actions
150 // Used by the angular side of things
151 for (let i = 0; i < settings.extraSetups.length; i++) {
152 settings.extraSetups[i](editor);
155 registerEditorShortcuts(editor);
159 function hasTextContent(node) {
160 return node && !!( node.textContent || node.innerText );
163 editor.on('dragstart', function () {
164 let node = editor.selection.getNode();
166 if (node.nodeName !== 'IMG') return;
167 wrap = editor.dom.getParent(node, '.mceTemp');
169 if (!wrap && node.parentNode.nodeName === 'A' && !hasTextContent(node.parentNode)) {
170 wrap = node.parentNode;
174 editor.on('drop', function (event) {
175 let dom = editor.dom,
176 rng = tinymce.dom.RangeUtils.getCaretRangeFromPoint(event.clientX, event.clientY, editor.getDoc());
178 // Don't allow anything to be dropped in a captioned image.
179 if (dom.getParent(rng.startContainer, '.mceTemp')) {
180 event.preventDefault();
182 event.preventDefault();
184 editor.undoManager.transact(function () {
185 editor.selection.setRng(rng);
186 editor.selection.setNode(wrap);
194 // Custom Image picker button
195 editor.addButton('image-insert', {
198 tooltip: 'Insert an image',
199 onclick: function () {
200 window.ImageManager.showExternal(function (image) {
201 let html = `<a href="${image.url}" target="_blank">`;
202 html += `<img src="${image.thumbs.display}" alt="${image.name}">`;
204 editor.execCommand('mceInsertContent', false, html);
209 // Paste image-uploads
210 editor.on('paste', function(event) {
211 editorPaste(event, editor);