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.
|
|
|
|
|
|
|
|
import { EditorState, Extension } from '@codemirror/state';
|
|
|
|
import { EditorView, drawSelection, highlightSpecialChars, ViewUpdate } from '@codemirror/view';
|
|
|
|
import { markdown } from '@codemirror/lang-markdown';
|
|
|
|
import { defaultHighlightStyle, HighlightStyle, tags } from '@codemirror/highlight';
|
|
|
|
import { undo, redo, history, undoDepth, redoDepth } from '@codemirror/history';
|
|
|
|
|
|
|
|
interface CodeMirrorResult {
|
|
|
|
editor: EditorView;
|
|
|
|
undo: Function;
|
|
|
|
redo: Function;
|
2022-04-11 12:56:45 +02:00
|
|
|
select: (anchor: number, head: number)=> 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 });
|
|
|
|
}
|
|
|
|
|
2021-10-30 12:51:40 +02:00
|
|
|
// For an example on how to customize the theme, see:
|
|
|
|
//
|
|
|
|
// https://github.com/codemirror/theme-one-dark/blob/main/src/one-dark.ts
|
|
|
|
//
|
2022-04-10 11:58:11 +02:00
|
|
|
// For a tutorial, see:
|
|
|
|
//
|
|
|
|
// https://codemirror.net/6/examples/styling/#themes
|
|
|
|
//
|
2021-10-30 12:51:40 +02:00
|
|
|
// Use Safari developer tools to view the content of the CodeMirror iframe while
|
|
|
|
// the app is running. It seems that what appears as ".ͼ1" in the CSS is the
|
|
|
|
// equivalent of "&" in the theme object. So to target ".ͼ1.cm-focused", you'd
|
|
|
|
// use '&.cm-focused' in the theme.
|
2021-07-13 20:13:13 +02:00
|
|
|
const createTheme = (theme: any): Extension => {
|
2022-04-10 11:58:11 +02:00
|
|
|
const isDarkTheme = theme.appearance === 'dark';
|
|
|
|
|
|
|
|
const baseGlobalStyle: Record<string, string> = {
|
|
|
|
color: theme.color,
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
fontFamily: theme.fontFamily,
|
|
|
|
fontSize: `${theme.fontSize}px`,
|
|
|
|
};
|
|
|
|
const baseCursorStyle: Record<string, string> = { };
|
|
|
|
const baseContentStyle: Record<string, string> = { };
|
|
|
|
const baseSelectionStyle: Record<string, string> = { };
|
|
|
|
|
|
|
|
// If we're in dark mode, the caret and selection are difficult to see.
|
|
|
|
// Adjust them appropriately
|
|
|
|
if (isDarkTheme) {
|
|
|
|
// Styling the caret requires styling both the caret itself
|
|
|
|
// and the CodeMirror caret.
|
|
|
|
// See https://codemirror.net/6/examples/styling/#themes
|
|
|
|
baseContentStyle.caretColor = 'white';
|
|
|
|
baseCursorStyle.borderLeftColor = 'white';
|
|
|
|
|
|
|
|
baseSelectionStyle.backgroundColor = '#6b6b6b';
|
|
|
|
}
|
|
|
|
|
2021-07-13 20:13:13 +02:00
|
|
|
const baseTheme = EditorView.baseTheme({
|
2022-04-10 11:58:11 +02:00
|
|
|
'&': baseGlobalStyle,
|
|
|
|
|
|
|
|
// These must be !important or more specific than CodeMirror's built-ins
|
|
|
|
'.cm-content': baseContentStyle,
|
|
|
|
'&.cm-focused .cm-cursor': baseCursorStyle,
|
|
|
|
'&.cm-focused .cm-selectionBackground, ::selection': baseSelectionStyle,
|
2021-10-30 12:51:40 +02:00
|
|
|
|
|
|
|
'&.cm-focused': {
|
|
|
|
outline: 'none',
|
|
|
|
},
|
2021-07-13 20:13:13 +02:00
|
|
|
});
|
|
|
|
|
2022-04-10 11:58:11 +02:00
|
|
|
const appearanceTheme = EditorView.theme({}, { dark: isDarkTheme });
|
2021-07-13 20:13:13 +02:00
|
|
|
|
|
|
|
const baseHeadingStyle = {
|
|
|
|
fontWeight: 'bold',
|
|
|
|
fontFamily: theme.fontFamily,
|
|
|
|
};
|
|
|
|
|
|
|
|
const syntaxHighlighting = HighlightStyle.define([
|
|
|
|
{
|
|
|
|
tag: tags.strong,
|
|
|
|
fontWeight: 'bold',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: tags.emphasis,
|
|
|
|
fontStyle: 'italic',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...baseHeadingStyle,
|
|
|
|
tag: tags.heading1,
|
|
|
|
fontSize: '1.6em',
|
|
|
|
borderBottom: `1px solid ${theme.dividerColor}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...baseHeadingStyle,
|
|
|
|
tag: tags.heading2,
|
|
|
|
fontSize: '1.4em',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...baseHeadingStyle,
|
|
|
|
tag: tags.heading3,
|
|
|
|
fontSize: '1.3em',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...baseHeadingStyle,
|
|
|
|
tag: tags.heading4,
|
|
|
|
fontSize: '1.2em',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...baseHeadingStyle,
|
|
|
|
tag: tags.heading5,
|
|
|
|
fontSize: '1.1em',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...baseHeadingStyle,
|
|
|
|
tag: tags.heading6,
|
|
|
|
fontSize: '1.0em',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: tags.list,
|
|
|
|
fontFamily: theme.fontFamily,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
return [
|
|
|
|
baseTheme,
|
|
|
|
appearanceTheme,
|
|
|
|
syntaxHighlighting,
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
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({
|
|
|
|
extensions: [
|
|
|
|
markdown(),
|
|
|
|
createTheme(theme),
|
|
|
|
history(),
|
|
|
|
drawSelection(),
|
|
|
|
highlightSpecialChars(),
|
|
|
|
EditorView.lineWrapping,
|
2021-10-31 11:32:08 +02:00
|
|
|
EditorView.contentAttributes.of({ autocapitalize: 'sentence' }),
|
2021-07-13 20:13:13 +02:00
|
|
|
defaultHighlightStyle.fallback,
|
|
|
|
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
|
|
|
}),
|
|
|
|
],
|
|
|
|
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,
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
insertText: (text: string) => {
|
|
|
|
editor.dispatch(editor.state.replaceSelection(text));
|
|
|
|
},
|
2021-07-13 20:13:13 +02:00
|
|
|
};
|
|
|
|
}
|