]> BookStack Code Mirror - bookstack/blob - resources/js/markdown/codemirror.js
Got md shortcuts working, marked actions for update
[bookstack] / resources / js / markdown / codemirror.js
1 import {provideKeyBindings} from "./shortcuts";
2 import {debounce} from "../services/util";
3 import Clipboard from "../services/clipboard";
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 => syncActive = val);
25
26     const domEventHandlers = {
27         // Handle scroll to sync display view
28         scroll: (event) => syncActive && onScrollDebounced(event)
29     }
30
31     const cm = Code.markdownEditor(
32         editor.config.inputEl,
33         onViewUpdate,
34         domEventHandlers,
35         provideKeyBindings(editor),
36     );
37     window.cm = cm;
38
39     // Will force to remain as ltr for now due to issues when HTML is in editor.
40     // TODO
41     // cm.setOption('direction', 'ltr');
42
43
44     // Handle image paste
45     // TODO
46     // cm.on('paste', (cm, event) => {
47     //     const clipboard = new Clipboard(event.clipboardData || event.dataTransfer);
48     //
49     //     // Don't handle the event ourselves if no items exist of contains table-looking data
50     //     if (!clipboard.hasItems() || clipboard.containsTabularData()) {
51     //         return;
52     //     }
53     //
54     //     const images = clipboard.getImages();
55     //     for (const image of images) {
56     //         editor.actions.uploadImage(image);
57     //     }
58     // });
59
60     // Handle image & content drag n drop
61     // TODO
62     // cm.on('drop', (cm, event) => {
63     //
64     //     const templateId = event.dataTransfer.getData('bookstack/template');
65     //     if (templateId) {
66     //         event.preventDefault();
67     //         editor.actions.insertTemplate(templateId, event.pageX, event.pageY);
68     //     }
69     //
70     //     const clipboard = new Clipboard(event.dataTransfer);
71     //     const clipboardImages = clipboard.getImages();
72     //     if (clipboardImages.length > 0) {
73     //         event.stopPropagation();
74     //         event.preventDefault();
75     //         editor.actions.insertClipboardImages(clipboardImages);
76     //     }
77     //
78     // });
79
80     return cm;
81 }