2021-07-13 20:13:13 +02:00
|
|
|
/* eslint-disable import/prefer-default-export */
|
|
|
|
|
|
|
|
// This contains the CodeMirror instance, which needs to be built into a bundle
|
2024-01-26 22:19:28 +02:00
|
|
|
// using `yarn buildInjectedJs`. This bundle is then loaded from
|
2021-07-13 20:13:13 +02:00
|
|
|
// NoteEditor.tsx into the webview.
|
|
|
|
//
|
|
|
|
// In general, since this file is harder to debug due to the intermediate built
|
2024-02-26 12:16:23 +02:00
|
|
|
// step, it's better to keep it as light as possible - it should just be a light
|
2021-07-13 20:13:13 +02:00
|
|
|
// wrapper to access CodeMirror functionalities. Anything else should be done
|
|
|
|
// from NoteEditor.tsx.
|
|
|
|
|
2023-09-21 10:12:40 +02:00
|
|
|
import { EditorSettings } from '@joplin/editor/types';
|
|
|
|
import createEditor from '@joplin/editor/CodeMirror/createEditor';
|
|
|
|
import CodeMirrorControl from '@joplin/editor/CodeMirror/CodeMirrorControl';
|
2024-03-11 17:02:15 +02:00
|
|
|
import WebViewToRNMessenger from '../../../utils/ipc/WebViewToRNMessenger';
|
|
|
|
import { WebViewToEditorApi } from '../types';
|
2022-12-08 01:19:55 +02:00
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
export const initCodeMirror = (
|
|
|
|
parentElement: HTMLElement,
|
|
|
|
initialText: string,
|
|
|
|
settings: EditorSettings,
|
|
|
|
): CodeMirrorControl => {
|
|
|
|
const messenger = new WebViewToRNMessenger<CodeMirrorControl, WebViewToEditorApi>('editor', null);
|
|
|
|
|
|
|
|
const control = createEditor(parentElement, {
|
2023-09-21 10:12:40 +02:00
|
|
|
initialText,
|
|
|
|
settings,
|
|
|
|
|
|
|
|
onLogMessage: message => {
|
2024-03-11 17:02:15 +02:00
|
|
|
void messenger.remoteApi.logMessage(message);
|
2022-06-26 19:21:38 +02:00
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
onEvent: (event): void => {
|
2024-03-11 17:02:15 +02:00
|
|
|
void messenger.remoteApi.onEditorEvent(event);
|
2022-04-11 12:56:45 +02:00
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
});
|
2022-08-08 17:00:14 +02:00
|
|
|
|
2024-03-05 18:57:18 +02:00
|
|
|
// Works around https://github.com/laurent22/joplin/issues/10047 by handling
|
|
|
|
// the text/uri-list MIME type when pasting, rather than sending the paste event
|
|
|
|
// to CodeMirror.
|
|
|
|
//
|
|
|
|
// TODO: Remove this workaround when the issue has been fixed upstream.
|
2024-03-18 12:17:39 +02:00
|
|
|
control.on('paste', (_editor, event: ClipboardEvent) => {
|
2024-03-05 18:57:18 +02:00
|
|
|
const clipboardData = event.clipboardData;
|
|
|
|
if (clipboardData.types.length === 1 && clipboardData.types[0] === 'text/uri-list') {
|
|
|
|
event.preventDefault();
|
2024-03-18 12:17:39 +02:00
|
|
|
control.insertText(clipboardData.getData('text/uri-list'));
|
2024-03-05 18:57:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
messenger.setLocalInterface(control);
|
|
|
|
return control;
|
|
|
|
};
|