]> BookStack Code Mirror - bookstack/blob - resources/js/markdown/codemirror.js
Tests: Updated comment test to account for new editor usage
[bookstack] / resources / js / markdown / codemirror.js
1 import {provideKeyBindings} from './shortcuts';
2 import {debounce} from '../services/util.ts';
3 import {Clipboard} from '../services/clipboard.ts';
4
5 /**
6  * Initiate the codemirror instance for the markdown editor.
7  * @param {MarkdownEditor} editor
8  * @returns {Promise<void>}
9  */
10 export async function init(editor) {
11     const Code = await window.importVersioned('code');
12
13     /**
14      * @param {ViewUpdate} v
15      */
16     function onViewUpdate(v) {
17         if (v.docChanged) {
18             editor.actions.updateAndRender();
19         }
20     }
21
22     const onScrollDebounced = debounce(editor.actions.syncDisplayPosition.bind(editor.actions), 100, false);
23     let syncActive = editor.settings.get('scrollSync');
24     editor.settings.onChange('scrollSync', val => {
25         syncActive = val;
26     });
27
28     const domEventHandlers = {
29         // Handle scroll to sync display view
30         scroll: event => syncActive && onScrollDebounced(event),
31         // Handle image & content drag n drop
32         drop: event => {
33             const templateId = event.dataTransfer.getData('bookstack/template');
34             if (templateId) {
35                 event.preventDefault();
36                 editor.actions.insertTemplate(templateId, event.pageX, event.pageY);
37             }
38
39             const clipboard = new Clipboard(event.dataTransfer);
40             const clipboardImages = clipboard.getImages();
41             if (clipboardImages.length > 0) {
42                 event.stopPropagation();
43                 event.preventDefault();
44                 editor.actions.insertClipboardImages(clipboardImages, event.pageX, event.pageY);
45             }
46         },
47         // Handle dragover event to allow as drop-target in chrome
48         dragover: event => {
49             event.preventDefault();
50         },
51         // Handle image paste
52         paste: event => {
53             const clipboard = new Clipboard(event.clipboardData || event.dataTransfer);
54
55             // Don't handle the event ourselves if no items exist of contains table-looking data
56             if (!clipboard.hasItems() || clipboard.containsTabularData()) {
57                 return;
58             }
59
60             const images = clipboard.getImages();
61             for (const image of images) {
62                 editor.actions.uploadImage(image);
63             }
64         },
65     };
66
67     const cm = Code.markdownEditor(
68         editor.config.inputEl,
69         onViewUpdate,
70         domEventHandlers,
71         provideKeyBindings(editor),
72     );
73
74     // Add editor view to window for easy access/debugging.
75     // Not part of official API/Docs
76     window.mdEditorView = cm;
77
78     return cm;
79 }