2022-11-26 17:43:28 +01:00
|
|
|
import {provide as provideShortcuts} from "./shortcuts";
|
|
|
|
import {debounce} from "../services/util";
|
|
|
|
import Clipboard from "../services/clipboard";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 16:01:44 +02:00
|
|
|
/**
|
|
|
|
* @param {ViewUpdate} v
|
|
|
|
*/
|
|
|
|
function onViewUpdate(v) {
|
|
|
|
if (v.docChanged) {
|
|
|
|
editor.actions.updateAndRender();
|
|
|
|
}
|
|
|
|
}
|
2022-11-26 17:43:28 +01:00
|
|
|
|
2022-11-26 22:33:39 +01:00
|
|
|
const onScrollDebounced = debounce(editor.actions.syncDisplayPosition.bind(editor.actions), 100, false);
|
2022-11-28 13:12:36 +01:00
|
|
|
let syncActive = editor.settings.get('scrollSync');
|
|
|
|
editor.settings.onChange('scrollSync', val => syncActive = val);
|
2022-11-26 17:43:28 +01:00
|
|
|
|
2023-04-10 16:01:44 +02:00
|
|
|
const domEventHandlers = {
|
|
|
|
// Handle scroll to sync display view
|
|
|
|
scroll: (event) => syncActive && onScrollDebounced(event)
|
|
|
|
}
|
2022-11-26 17:43:28 +01:00
|
|
|
|
2023-04-10 16:01:44 +02:00
|
|
|
const cm = Code.markdownEditor(editor.config.inputEl, onViewUpdate, domEventHandlers);
|
|
|
|
window.cm = cm;
|
2022-11-26 17:43:28 +01:00
|
|
|
|
2023-04-10 16:01:44 +02:00
|
|
|
// Will force to remain as ltr for now due to issues when HTML is in editor.
|
|
|
|
// TODO
|
|
|
|
// cm.setOption('direction', 'ltr');
|
|
|
|
// Register shortcuts
|
|
|
|
// TODO
|
|
|
|
// cm.setOption('extraKeys', provideShortcuts(editor, Code.getMetaKey()));
|
2022-11-26 17:43:28 +01:00
|
|
|
|
|
|
|
|
2023-04-10 16:01:44 +02:00
|
|
|
// Handle image paste
|
|
|
|
// TODO
|
|
|
|
// cm.on('paste', (cm, event) => {
|
|
|
|
// 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);
|
|
|
|
// }
|
|
|
|
// });
|
2022-11-26 17:43:28 +01:00
|
|
|
|
2023-04-10 16:01:44 +02:00
|
|
|
// Handle image & content drag n drop
|
|
|
|
// TODO
|
|
|
|
// cm.on('drop', (cm, event) => {
|
|
|
|
//
|
|
|
|
// 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);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// });
|
2022-11-26 17:43:28 +01:00
|
|
|
|
|
|
|
return cm;
|
|
|
|
}
|