1 import Clipboard from "../services/clipboard";
4 let draggedContentEditable;
6 function hasTextContent(node) {
7 return node && !!(node.textContent || node.innerText);
11 * Handle pasting images from clipboard.
12 * @param {Editor} editor
13 * @param {WysiwygConfigOptions} options
14 * @param {ClipboardEvent|DragEvent} event
16 function paste(editor, options, event) {
17 const clipboard = new Clipboard(event.clipboardData || event.dataTransfer);
19 // Don't handle the event ourselves if no items exist of contains table-looking data
20 if (!clipboard.hasItems() || clipboard.containsTabularData()) {
24 const images = clipboard.getImages();
25 for (const imageFile of images) {
27 const id = "image-" + Math.random().toString(16).slice(2);
28 const loadingImage = window.baseUrl('/loading.gif');
29 event.preventDefault();
32 editor.insertContent(`<p><img src="${loadingImage}" id="${id}"></p>`);
34 uploadImageFile(imageFile, options.pageId).then(resp => {
35 const safeName = resp.name.replace(/"/g, '');
36 const newImageHtml = `<img src="${resp.thumbs.display}" alt="${safeName}" />`;
38 const newEl = editor.dom.create('a', {
43 editor.dom.replace(newEl, id);
45 editor.dom.remove(id);
46 window.$events.emit('error', options.translations.imageUploadErrorText);
54 * Upload an image file to the server
58 async function uploadImageFile(file, pageId) {
59 if (file === null || file.type.indexOf('image') !== 0) {
60 throw new Error(`Not an image file`);
63 const remoteFilename = file.name || `image-${Date.now()}.png`;
64 const formData = new FormData();
65 formData.append('file', file, remoteFilename);
66 formData.append('uploaded_to', pageId);
68 const resp = await window.$http.post(window.baseUrl('/images/gallery'), formData);
73 * @param {Editor} editor
74 * @param {WysiwygConfigOptions} options
76 function dragStart(editor, options) {
77 let node = editor.selection.getNode();
79 if (node.nodeName === 'IMG') {
80 wrap = editor.dom.getParent(node, '.mceTemp');
82 if (!wrap && node.parentNode.nodeName === 'A' && !hasTextContent(node.parentNode)) {
83 wrap = node.parentNode;
87 // Track dragged contenteditable blocks
88 if (node.hasAttribute('contenteditable') && node.getAttribute('contenteditable') === 'false') {
89 draggedContentEditable = node;
94 * @param {Editor} editor
95 * @param {WysiwygConfigOptions} options
96 * @param {DragEvent} event
98 function drop(editor, options, event) {
100 rng = tinymce.dom.RangeUtils.getCaretRangeFromPoint(event.clientX, event.clientY, editor.getDoc());
102 // Template insertion
103 const templateId = event.dataTransfer && event.dataTransfer.getData('bookstack/template');
105 event.preventDefault();
106 window.$http.get(`/templates/${templateId}`).then(resp => {
107 editor.selection.setRng(rng);
108 editor.undoManager.transact(function () {
109 editor.execCommand('mceInsertContent', false, resp.data.html);
114 // Don't allow anything to be dropped in a captioned image.
115 if (dom.getParent(rng.startContainer, '.mceTemp')) {
116 event.preventDefault();
118 event.preventDefault();
120 editor.undoManager.transact(function () {
121 editor.selection.setRng(rng);
122 editor.selection.setNode(wrap);
127 // Handle contenteditable section drop
128 if (!event.isDefaultPrevented() && draggedContentEditable) {
129 event.preventDefault();
130 editor.undoManager.transact(function () {
131 const selectedNode = editor.selection.getNode();
132 const range = editor.selection.getRng();
133 const selectedNodeRoot = selectedNode.closest('body > *');
134 if (range.startOffset > (range.startContainer.length / 2)) {
135 selectedNodeRoot.after(draggedContentEditable);
137 selectedNodeRoot.before(draggedContentEditable);
142 // Handle image insert
143 if (!event.isDefaultPrevented()) {
144 paste(editor, options, event);
151 * @param {Editor} editor
152 * @param {WysiwygConfigOptions} options
154 export function listenForDragAndPaste(editor, options) {
155 editor.on('dragstart', () => dragStart(editor, options));
156 editor.on('drop', event => drop(editor, options, event));
157 editor.on('paste', event => paste(editor, options, event));