2023-04-18 17:20:02 -04:00
|
|
|
import {provideKeyBindings} from './shortcuts';
|
|
|
|
import {debounce} from '../services/util';
|
2023-04-19 05:46:13 -04:00
|
|
|
import {Clipboard} from '../services/clipboard';
|
2022-11-26 11:43:28 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate the codemirror instance for the markdown editor.
|
|
|
|
* @param {MarkdownEditor} editor
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
export async function init(editor) {
|
|
|
|
const Code = await window.importVersioned('code');
|
|
|
|
|
2023-04-10 10:01:44 -04:00
|
|
|
/**
|
|
|
|
* @param {ViewUpdate} v
|
|
|
|
*/
|
|
|
|
function onViewUpdate(v) {
|
|
|
|
if (v.docChanged) {
|
|
|
|
editor.actions.updateAndRender();
|
|
|
|
}
|
|
|
|
}
|
2022-11-26 11:43:28 -05:00
|
|
|
|
2022-11-26 16:33:39 -05:00
|
|
|
const onScrollDebounced = debounce(editor.actions.syncDisplayPosition.bind(editor.actions), 100, false);
|
2022-11-28 07:12:36 -05:00
|
|
|
let syncActive = editor.settings.get('scrollSync');
|
2023-04-19 10:20:04 -04:00
|
|
|
editor.settings.onChange('scrollSync', val => {
|
|
|
|
syncActive = val;
|
|
|
|
});
|
2022-11-26 11:43:28 -05:00
|
|
|
|
2023-04-10 10:01:44 -04:00
|
|
|
const domEventHandlers = {
|
|
|
|
// Handle scroll to sync display view
|
2023-04-18 17:20:02 -04:00
|
|
|
scroll: event => syncActive && onScrollDebounced(event),
|
2023-04-13 12:18:32 -04:00
|
|
|
// Handle image & content drag n drop
|
2023-04-18 17:20:02 -04:00
|
|
|
drop: event => {
|
2023-04-13 12:18:32 -04:00
|
|
|
const templateId = event.dataTransfer.getData('bookstack/template');
|
|
|
|
if (templateId) {
|
|
|
|
event.preventDefault();
|
|
|
|
editor.actions.insertTemplate(templateId, event.pageX, event.pageY);
|
|
|
|
}
|
|
|
|
|
|
|
|
const clipboard = new Clipboard(event.dataTransfer);
|
|
|
|
const clipboardImages = clipboard.getImages();
|
|
|
|
if (clipboardImages.length > 0) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
editor.actions.insertClipboardImages(clipboardImages, event.pageX, event.pageY);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Handle image paste
|
2023-04-18 17:20:02 -04:00
|
|
|
paste: event => {
|
2023-04-13 12:18:32 -04:00
|
|
|
const clipboard = new Clipboard(event.clipboardData || event.dataTransfer);
|
|
|
|
|
|
|
|
// Don't handle the event ourselves if no items exist of contains table-looking data
|
|
|
|
if (!clipboard.hasItems() || clipboard.containsTabularData()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const images = clipboard.getImages();
|
|
|
|
for (const image of images) {
|
|
|
|
editor.actions.uploadImage(image);
|
|
|
|
}
|
2023-04-18 17:20:02 -04:00
|
|
|
},
|
|
|
|
};
|
2022-11-26 11:43:28 -05:00
|
|
|
|
2023-04-11 06:48:58 -04:00
|
|
|
const cm = Code.markdownEditor(
|
|
|
|
editor.config.inputEl,
|
|
|
|
onViewUpdate,
|
|
|
|
domEventHandlers,
|
|
|
|
provideKeyBindings(editor),
|
|
|
|
);
|
2022-11-26 11:43:28 -05:00
|
|
|
|
2023-04-14 09:08:40 -04:00
|
|
|
// Add editor view to window for easy access/debugging.
|
|
|
|
// Not part of official API/Docs
|
|
|
|
window.mdEditorView = cm;
|
2022-11-26 11:43:28 -05:00
|
|
|
|
|
|
|
return cm;
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|