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
|
|
|
|
// using `npm run buildInjectedJs`. This bundle is then loaded from
|
|
|
|
// NoteEditor.tsx into the webview.
|
|
|
|
//
|
|
|
|
// In general, since this file is harder to debug due to the intermediate built
|
|
|
|
// step, it's better to keep it as light as possible - it shoud just be a light
|
|
|
|
// wrapper to access CodeMirror functionalities. Anything else should be done
|
|
|
|
// from NoteEditor.tsx.
|
|
|
|
|
2022-07-22 11:44:19 +02:00
|
|
|
import createTheme from './theme';
|
2022-07-28 18:01:34 +02:00
|
|
|
import decoratorExtension from './decoratorExtension';
|
2022-07-22 11:44:19 +02:00
|
|
|
|
|
|
|
import { EditorState } from '@codemirror/state';
|
2021-07-13 20:13:13 +02:00
|
|
|
import { markdown } from '@codemirror/lang-markdown';
|
2022-06-24 11:56:59 +02:00
|
|
|
import { highlightSelectionMatches, search } from '@codemirror/search';
|
2022-06-20 15:31:30 +02:00
|
|
|
import { EditorView, drawSelection, highlightSpecialChars, ViewUpdate } from '@codemirror/view';
|
|
|
|
import { undo, redo, history, undoDepth, redoDepth } from '@codemirror/commands';
|
|
|
|
|
|
|
|
import { keymap } from '@codemirror/view';
|
|
|
|
import { indentOnInput } from '@codemirror/language';
|
2022-06-24 11:56:59 +02:00
|
|
|
import { searchKeymap } from '@codemirror/search';
|
2022-06-20 15:31:30 +02:00
|
|
|
import { historyKeymap, defaultKeymap } from '@codemirror/commands';
|
2022-07-22 11:44:19 +02:00
|
|
|
import { MarkdownMathExtension } from './markdownMathParser';
|
|
|
|
import { GFM as GitHubFlavoredMarkdownExtension } from '@lezer/markdown';
|
2022-07-28 18:01:34 +02:00
|
|
|
import syntaxHighlightingLanguages from './syntaxHighlightingLanguages';
|
2021-07-13 20:13:13 +02:00
|
|
|
|
|
|
|
interface CodeMirrorResult {
|
|
|
|
editor: EditorView;
|
|
|
|
undo: Function;
|
|
|
|
redo: Function;
|
2022-06-26 19:21:38 +02:00
|
|
|
select(anchor: number, head: number): void;
|
|
|
|
scrollSelectionIntoView(): void;
|
|
|
|
insertText(text: string): void;
|
2021-07-13 20:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function postMessage(name: string, data: any) {
|
|
|
|
(window as any).ReactNativeWebView.postMessage(JSON.stringify({
|
|
|
|
data,
|
|
|
|
name,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function logMessage(...msg: any[]) {
|
|
|
|
postMessage('onLog', { value: msg });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initCodeMirror(parentElement: any, initialText: string, theme: any): CodeMirrorResult {
|
|
|
|
logMessage('Initializing CodeMirror...');
|
|
|
|
|
|
|
|
let schedulePostUndoRedoDepthChangeId_: any = 0;
|
|
|
|
function schedulePostUndoRedoDepthChange(editor: EditorView, doItNow: boolean = false) {
|
|
|
|
if (schedulePostUndoRedoDepthChangeId_) {
|
|
|
|
if (doItNow) {
|
|
|
|
clearTimeout(schedulePostUndoRedoDepthChangeId_);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
schedulePostUndoRedoDepthChangeId_ = setTimeout(() => {
|
|
|
|
schedulePostUndoRedoDepthChangeId_ = null;
|
|
|
|
postMessage('onUndoRedoDepthChange', {
|
|
|
|
undoDepth: undoDepth(editor.state),
|
|
|
|
redoDepth: redoDepth(editor.state),
|
|
|
|
});
|
|
|
|
}, doItNow ? 0 : 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
const editor = new EditorView({
|
|
|
|
state: EditorState.create({
|
2022-06-20 15:31:30 +02:00
|
|
|
// See https://github.com/codemirror/basic-setup/blob/main/src/codemirror.ts
|
|
|
|
// for a sample configuration.
|
2021-07-13 20:13:13 +02:00
|
|
|
extensions: [
|
2022-07-22 11:44:19 +02:00
|
|
|
markdown({
|
|
|
|
extensions: [
|
|
|
|
MarkdownMathExtension,
|
|
|
|
GitHubFlavoredMarkdownExtension,
|
|
|
|
],
|
2022-07-28 18:01:34 +02:00
|
|
|
codeLanguages: syntaxHighlightingLanguages,
|
2022-07-22 11:44:19 +02:00
|
|
|
}),
|
2022-06-20 15:31:30 +02:00
|
|
|
...createTheme(theme),
|
2021-07-13 20:13:13 +02:00
|
|
|
history(),
|
2022-06-24 11:56:59 +02:00
|
|
|
search(),
|
2021-07-13 20:13:13 +02:00
|
|
|
drawSelection(),
|
|
|
|
highlightSpecialChars(),
|
2022-06-24 11:56:59 +02:00
|
|
|
highlightSelectionMatches(),
|
2022-06-20 15:31:30 +02:00
|
|
|
indentOnInput(),
|
|
|
|
|
2022-07-28 18:01:34 +02:00
|
|
|
decoratorExtension,
|
2021-07-13 20:13:13 +02:00
|
|
|
EditorView.lineWrapping,
|
2021-10-31 11:32:08 +02:00
|
|
|
EditorView.contentAttributes.of({ autocapitalize: 'sentence' }),
|
2021-07-13 20:13:13 +02:00
|
|
|
EditorView.updateListener.of((viewUpdate: ViewUpdate) => {
|
|
|
|
if (viewUpdate.docChanged) {
|
|
|
|
postMessage('onChange', { value: editor.state.doc.toString() });
|
|
|
|
schedulePostUndoRedoDepthChange(editor);
|
|
|
|
}
|
2022-04-11 12:56:45 +02:00
|
|
|
|
|
|
|
if (!viewUpdate.state.selection.eq(viewUpdate.startState.selection)) {
|
|
|
|
const mainRange = viewUpdate.state.selection.main;
|
|
|
|
const selStart = mainRange.from;
|
|
|
|
const selEnd = mainRange.to;
|
|
|
|
postMessage('onSelectionChange', { selection: { start: selStart, end: selEnd } });
|
|
|
|
}
|
2021-07-13 20:13:13 +02:00
|
|
|
}),
|
2022-06-20 15:31:30 +02:00
|
|
|
keymap.of([
|
2022-06-24 11:56:59 +02:00
|
|
|
...defaultKeymap, ...historyKeymap, ...searchKeymap,
|
2022-06-20 15:31:30 +02:00
|
|
|
]),
|
2021-07-13 20:13:13 +02:00
|
|
|
],
|
|
|
|
doc: initialText,
|
|
|
|
}),
|
|
|
|
parent: parentElement,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
editor,
|
|
|
|
undo: () => {
|
|
|
|
undo(editor);
|
|
|
|
schedulePostUndoRedoDepthChange(editor, true);
|
|
|
|
},
|
|
|
|
redo: () => {
|
|
|
|
redo(editor);
|
|
|
|
schedulePostUndoRedoDepthChange(editor, true);
|
|
|
|
},
|
2022-04-11 12:56:45 +02:00
|
|
|
select: (anchor: number, head: number) => {
|
|
|
|
editor.dispatch(editor.state.update({
|
|
|
|
selection: { anchor, head },
|
|
|
|
scrollIntoView: true,
|
|
|
|
}));
|
|
|
|
},
|
2022-06-26 19:21:38 +02:00
|
|
|
scrollSelectionIntoView: () => {
|
|
|
|
editor.dispatch(editor.state.update({
|
|
|
|
scrollIntoView: true,
|
|
|
|
}));
|
|
|
|
},
|
2022-04-11 12:56:45 +02:00
|
|
|
insertText: (text: string) => {
|
|
|
|
editor.dispatch(editor.state.replaceSelection(text));
|
|
|
|
},
|
2021-07-13 20:13:13 +02:00
|
|
|
};
|
|
|
|
}
|