2021-07-13 20:13:13 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
import { themeStyle } from '@joplin/lib/theme';
|
2022-08-08 17:00:14 +02:00
|
|
|
import EditLinkDialog from './EditLinkDialog';
|
|
|
|
import { defaultSearchState, SearchPanel } from './SearchPanel';
|
2022-09-05 13:46:13 +02:00
|
|
|
import ExtendedWebView from '../ExtendedWebView';
|
2022-08-08 17:00:14 +02:00
|
|
|
|
2023-09-21 10:12:40 +02:00
|
|
|
import * as React from 'react';
|
2023-11-07 14:00:36 +02:00
|
|
|
import { forwardRef, useImperativeHandle } from 'react';
|
|
|
|
import { useMemo, useState, useCallback, useRef } from 'react';
|
2024-02-02 19:54:16 +02:00
|
|
|
import { LayoutChangeEvent, NativeSyntheticEvent, View, ViewStyle } from 'react-native';
|
2021-07-13 20:13:13 +02:00
|
|
|
const { editorFont } = require('../global-style');
|
|
|
|
|
2023-09-21 10:12:40 +02:00
|
|
|
import { EditorControl, EditorSettings, SelectionRange } from './types';
|
2022-08-08 17:00:14 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
2022-08-29 15:19:04 +02:00
|
|
|
import MarkdownToolbar from './MarkdownToolbar/MarkdownToolbar';
|
2023-09-21 10:12:40 +02:00
|
|
|
import { ChangeEvent, EditorEvent, EditorEventType, SelectionRangeChangeEvent, UndoRedoDepthChangeEvent } from '@joplin/editor/events';
|
2024-02-19 12:29:37 +02:00
|
|
|
import { EditorCommandType, EditorKeymap, EditorLanguageType, ContentScriptData, SearchState } from '@joplin/editor/types';
|
2023-09-21 10:12:40 +02:00
|
|
|
import supportsCommand from '@joplin/editor/CodeMirror/editorCommands/supportsCommand';
|
|
|
|
import SelectionFormatting, { defaultSelectionFormatting } from '@joplin/editor/SelectionFormatting';
|
2024-02-02 19:54:16 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
import { WebViewErrorEvent } from 'react-native-webview/lib/RNCWebViewNativeComponent';
|
2022-04-11 12:56:45 +02:00
|
|
|
|
2021-07-13 20:13:13 +02:00
|
|
|
type ChangeEventHandler = (event: ChangeEvent)=> void;
|
|
|
|
type UndoRedoDepthChangeHandler = (event: UndoRedoDepthChangeEvent)=> void;
|
2023-09-21 10:12:40 +02:00
|
|
|
type SelectionChangeEventHandler = (event: SelectionRangeChangeEvent)=> void;
|
2022-08-29 15:19:04 +02:00
|
|
|
type OnAttachCallback = ()=> void;
|
2021-07-13 20:13:13 +02:00
|
|
|
|
2024-02-02 19:54:16 +02:00
|
|
|
const logger = Logger.create('NoteEditor');
|
|
|
|
|
2021-07-13 20:13:13 +02:00
|
|
|
interface Props {
|
|
|
|
themeId: number;
|
|
|
|
initialText: string;
|
2023-09-21 10:12:40 +02:00
|
|
|
initialSelection?: SelectionRange;
|
2022-08-29 15:19:04 +02:00
|
|
|
style: ViewStyle;
|
|
|
|
contentStyle?: ViewStyle;
|
2023-03-16 13:12:56 +02:00
|
|
|
toolbarEnabled: boolean;
|
2023-07-16 18:42:42 +02:00
|
|
|
readOnly: boolean;
|
2022-04-11 12:56:45 +02:00
|
|
|
|
2021-07-13 20:13:13 +02:00
|
|
|
onChange: ChangeEventHandler;
|
2022-04-11 12:56:45 +02:00
|
|
|
onSelectionChange: SelectionChangeEventHandler;
|
2021-07-13 20:13:13 +02:00
|
|
|
onUndoRedoDepthChange: UndoRedoDepthChangeHandler;
|
2022-08-29 15:19:04 +02:00
|
|
|
onAttach: OnAttachCallback;
|
2021-07-13 20:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function fontFamilyFromSettings() {
|
2022-08-18 12:43:54 +02:00
|
|
|
const font = editorFont(Setting.value('style.editor.fontFamily'));
|
|
|
|
return font ? `${font}, sans-serif` : 'sans-serif';
|
2021-07-13 20:13:13 +02:00
|
|
|
}
|
|
|
|
|
2022-04-11 12:57:49 +02:00
|
|
|
function useCss(themeId: number): string {
|
|
|
|
return useMemo(() => {
|
|
|
|
const theme = themeStyle(themeId);
|
|
|
|
return `
|
|
|
|
:root {
|
|
|
|
background-color: ${theme.backgroundColor};
|
|
|
|
}
|
2022-07-28 18:02:46 +02:00
|
|
|
|
|
|
|
body {
|
2022-08-08 17:00:14 +02:00
|
|
|
margin: 0;
|
|
|
|
height: 100vh;
|
|
|
|
width: 100vh;
|
|
|
|
width: 100vw;
|
|
|
|
min-width: 100vw;
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
|
|
|
padding-left: 1px;
|
|
|
|
padding-right: 1px;
|
|
|
|
padding-bottom: 1px;
|
|
|
|
padding-top: 10px;
|
|
|
|
|
2022-07-28 18:02:46 +02:00
|
|
|
font-size: 13pt;
|
|
|
|
}
|
2022-04-11 12:57:49 +02:00
|
|
|
`;
|
|
|
|
}, [themeId]);
|
|
|
|
}
|
|
|
|
|
2021-07-13 20:13:13 +02:00
|
|
|
function useHtml(css: string): string {
|
|
|
|
const [html, setHtml] = useState('');
|
|
|
|
|
2022-08-08 17:00:14 +02:00
|
|
|
useMemo(() => {
|
|
|
|
setHtml(`
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
|
|
|
<title>${_('Note editor')}</title>
|
|
|
|
<style>
|
2023-12-17 22:57:40 +02:00
|
|
|
/* For better scrolling on iOS (working scrollbar) we use external, rather than internal,
|
|
|
|
scrolling. */
|
|
|
|
.cm-scroller {
|
|
|
|
overflow: none;
|
|
|
|
|
2024-02-26 12:16:23 +02:00
|
|
|
/* Ensure that the editor can be focused by clicking on the lower half of the screen.
|
2023-12-17 22:57:40 +02:00
|
|
|
Don't use 100vh to prevent a scrollbar being present for empty notes. */
|
|
|
|
min-height: 80vh;
|
2022-08-08 17:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
${css}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="CodeMirror" style="height:100%;" autocapitalize="on"></div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`);
|
2021-07-13 20:13:13 +02:00
|
|
|
}, [css]);
|
|
|
|
|
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
|
|
|
function editorTheme(themeId: number) {
|
2023-01-07 19:47:52 +02:00
|
|
|
const fontSizeInPx = Setting.value('style.editor.fontSize');
|
|
|
|
|
|
|
|
// Convert from `px` to `em`. To support font size scaling based on
|
|
|
|
// system accessibility settings, we need to provide font sizes in `em`.
|
|
|
|
// 16px is about 1em with the default root font size.
|
|
|
|
const estimatedFontSizeInEm = fontSizeInPx / 16;
|
|
|
|
|
2021-07-13 20:13:13 +02:00
|
|
|
return {
|
|
|
|
...themeStyle(themeId),
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
// To allow accessibility font scaling, we also need to set the
|
|
|
|
// fontSize to a value in `em`s (relative scaling relative to
|
|
|
|
// parent font size).
|
|
|
|
fontSizeUnits: 'em',
|
2023-01-07 19:47:52 +02:00
|
|
|
fontSize: estimatedFontSizeInEm,
|
2021-07-13 20:13:13 +02:00
|
|
|
fontFamily: fontFamilyFromSettings(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-29 15:19:04 +02:00
|
|
|
type OnInjectJSCallback = (js: string)=> void;
|
|
|
|
type OnSetVisibleCallback = (visible: boolean)=> void;
|
|
|
|
type OnSearchStateChangeCallback = (state: SearchState)=> void;
|
|
|
|
const useEditorControl = (
|
|
|
|
injectJS: OnInjectJSCallback, setLinkDialogVisible: OnSetVisibleCallback,
|
2023-09-21 10:12:40 +02:00
|
|
|
setSearchState: OnSearchStateChangeCallback,
|
2022-08-29 15:19:04 +02:00
|
|
|
): EditorControl => {
|
|
|
|
return useMemo(() => {
|
2023-09-21 10:12:40 +02:00
|
|
|
const execCommand = (command: EditorCommandType) => {
|
|
|
|
injectJS(`cm.execCommand(${JSON.stringify(command)})`);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setSearchStateCallback = (state: SearchState) => {
|
|
|
|
injectJS(`cm.setSearchState(${JSON.stringify(state)})`);
|
|
|
|
setSearchState(state);
|
|
|
|
};
|
|
|
|
|
|
|
|
const control: EditorControl = {
|
|
|
|
supportsCommand(command: EditorCommandType) {
|
|
|
|
return supportsCommand(command);
|
|
|
|
},
|
|
|
|
execCommand,
|
|
|
|
|
2022-08-29 15:19:04 +02:00
|
|
|
undo() {
|
2023-09-21 10:12:40 +02:00
|
|
|
injectJS('cm.undo()');
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
redo() {
|
2023-09-21 10:12:40 +02:00
|
|
|
injectJS('cm.redo()');
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
select(anchor: number, head: number) {
|
|
|
|
injectJS(
|
2023-08-22 12:58:53 +02:00
|
|
|
`cm.select(${JSON.stringify(anchor)}, ${JSON.stringify(head)});`,
|
2022-08-29 15:19:04 +02:00
|
|
|
);
|
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
setScrollPercent(fraction: number) {
|
|
|
|
injectJS(`cm.setScrollFraction(${JSON.stringify(fraction)})`);
|
|
|
|
},
|
2022-08-29 15:19:04 +02:00
|
|
|
insertText(text: string) {
|
|
|
|
injectJS(`cm.insertText(${JSON.stringify(text)});`);
|
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
updateBody(newBody: string) {
|
|
|
|
injectJS(`cm.updateBody(${JSON.stringify(newBody)});`);
|
|
|
|
},
|
|
|
|
updateSettings(newSettings: EditorSettings) {
|
|
|
|
injectJS(`cm.updateSettings(${JSON.stringify(newSettings)})`);
|
|
|
|
},
|
2022-08-29 15:19:04 +02:00
|
|
|
|
|
|
|
toggleBolded() {
|
2023-09-21 10:12:40 +02:00
|
|
|
execCommand(EditorCommandType.ToggleBolded);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
toggleItalicized() {
|
2023-09-21 10:12:40 +02:00
|
|
|
execCommand(EditorCommandType.ToggleItalicized);
|
|
|
|
},
|
|
|
|
toggleOrderedList() {
|
|
|
|
execCommand(EditorCommandType.ToggleNumberedList);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
toggleUnorderedList() {
|
2023-09-25 17:45:25 +02:00
|
|
|
execCommand(EditorCommandType.ToggleBulletedList);
|
2023-09-21 10:12:40 +02:00
|
|
|
},
|
|
|
|
toggleTaskList() {
|
|
|
|
execCommand(EditorCommandType.ToggleCheckList);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
toggleCode() {
|
2023-09-21 10:12:40 +02:00
|
|
|
execCommand(EditorCommandType.ToggleCode);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
toggleMath() {
|
2023-09-21 10:12:40 +02:00
|
|
|
execCommand(EditorCommandType.ToggleMath);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
toggleHeaderLevel(level: number) {
|
2023-09-21 10:12:40 +02:00
|
|
|
const levelToCommand = [
|
|
|
|
EditorCommandType.ToggleHeading1,
|
|
|
|
EditorCommandType.ToggleHeading2,
|
|
|
|
EditorCommandType.ToggleHeading3,
|
|
|
|
EditorCommandType.ToggleHeading4,
|
|
|
|
EditorCommandType.ToggleHeading5,
|
|
|
|
];
|
|
|
|
|
|
|
|
const index = level - 1;
|
|
|
|
|
|
|
|
if (index < 0 || index >= levelToCommand.length) {
|
|
|
|
throw new Error(`Unsupported header level ${level}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
execCommand(levelToCommand[index]);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
increaseIndent() {
|
2023-09-21 10:12:40 +02:00
|
|
|
execCommand(EditorCommandType.IndentMore);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
decreaseIndent() {
|
2023-09-21 10:12:40 +02:00
|
|
|
execCommand(EditorCommandType.IndentLess);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
updateLink(label: string, url: string) {
|
|
|
|
injectJS(`cm.updateLink(
|
|
|
|
${JSON.stringify(label)},
|
|
|
|
${JSON.stringify(url)}
|
|
|
|
);`);
|
|
|
|
},
|
|
|
|
scrollSelectionIntoView() {
|
2023-09-21 10:12:40 +02:00
|
|
|
execCommand(EditorCommandType.ScrollSelectionIntoView);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
showLinkDialog() {
|
|
|
|
setLinkDialogVisible(true);
|
|
|
|
},
|
|
|
|
hideLinkDialog() {
|
|
|
|
setLinkDialogVisible(false);
|
|
|
|
},
|
|
|
|
hideKeyboard() {
|
|
|
|
injectJS('document.activeElement?.blur();');
|
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
|
2024-02-19 12:29:37 +02:00
|
|
|
setContentScripts: async (plugins: ContentScriptData[]) => {
|
|
|
|
injectJS(`cm.setContentScripts(${JSON.stringify(plugins)});`);
|
2023-09-21 10:12:40 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
setSearchState: setSearchStateCallback,
|
|
|
|
|
2022-08-29 15:19:04 +02:00
|
|
|
searchControl: {
|
|
|
|
findNext() {
|
2023-09-21 10:12:40 +02:00
|
|
|
execCommand(EditorCommandType.FindNext);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
findPrevious() {
|
2023-09-21 10:12:40 +02:00
|
|
|
execCommand(EditorCommandType.FindPrevious);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
replaceNext() {
|
|
|
|
execCommand(EditorCommandType.ReplaceNext);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
replaceAll() {
|
2023-09-21 10:12:40 +02:00
|
|
|
execCommand(EditorCommandType.ReplaceAll);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
|
2022-08-29 15:19:04 +02:00
|
|
|
showSearch() {
|
2023-11-07 14:00:36 +02:00
|
|
|
execCommand(EditorCommandType.ShowSearch);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
hideSearch() {
|
2023-11-07 14:00:36 +02:00
|
|
|
execCommand(EditorCommandType.HideSearch);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
setSearchState: setSearchStateCallback,
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
};
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
return control;
|
2023-11-07 14:00:36 +02:00
|
|
|
}, [injectJS, setLinkDialogVisible, setSearchState]);
|
2022-08-29 15:19:04 +02:00
|
|
|
};
|
|
|
|
|
2021-07-13 20:13:13 +02:00
|
|
|
function NoteEditor(props: Props, ref: any) {
|
|
|
|
const webviewRef = useRef(null);
|
|
|
|
|
2022-04-11 12:56:45 +02:00
|
|
|
const setInitialSelectionJS = props.initialSelection ? `
|
|
|
|
cm.select(${props.initialSelection.start}, ${props.initialSelection.end});
|
2023-12-17 22:58:22 +02:00
|
|
|
cm.execCommand('scrollSelectionIntoView');
|
2022-04-11 12:56:45 +02:00
|
|
|
` : '';
|
|
|
|
|
2022-08-08 17:00:14 +02:00
|
|
|
const editorSettings: EditorSettings = {
|
2022-08-21 23:03:41 +02:00
|
|
|
themeId: props.themeId,
|
2022-08-08 17:00:14 +02:00
|
|
|
themeData: editorTheme(props.themeId),
|
2022-08-27 14:53:46 +02:00
|
|
|
katexEnabled: Setting.value('markdown.plugin.katex'),
|
|
|
|
spellcheckEnabled: Setting.value('editor.mobile.spellcheckEnabled'),
|
2023-09-21 10:12:40 +02:00
|
|
|
language: EditorLanguageType.Markdown,
|
|
|
|
useExternalSearch: true,
|
2023-07-16 18:42:42 +02:00
|
|
|
readOnly: props.readOnly,
|
2023-09-21 10:12:40 +02:00
|
|
|
|
|
|
|
keymap: EditorKeymap.Default,
|
|
|
|
|
|
|
|
automatchBraces: false,
|
|
|
|
ignoreModifiers: false,
|
|
|
|
|
|
|
|
indentWithTabs: false,
|
2022-08-08 17:00:14 +02:00
|
|
|
};
|
|
|
|
|
2021-07-13 20:13:13 +02:00
|
|
|
const injectedJavaScript = `
|
|
|
|
function postMessage(name, data) {
|
|
|
|
window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
|
|
data,
|
2022-04-11 12:56:45 +02:00
|
|
|
name,
|
2021-07-13 20:13:13 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function logMessage(...msg) {
|
|
|
|
postMessage('onLog', { value: msg });
|
|
|
|
}
|
|
|
|
|
2022-08-08 17:00:14 +02:00
|
|
|
// Globalize logMessage, postMessage
|
|
|
|
window.logMessage = logMessage;
|
|
|
|
window.postMessage = postMessage;
|
2021-07-13 20:13:13 +02:00
|
|
|
|
2022-08-08 17:00:14 +02:00
|
|
|
window.onerror = (message, source, lineno) => {
|
|
|
|
window.ReactNativeWebView.postMessage(
|
|
|
|
"error: " + message + " in file://" + source + ", line " + lineno
|
|
|
|
);
|
|
|
|
};
|
2023-09-21 10:12:40 +02:00
|
|
|
window.onunhandledrejection = (event) => {
|
|
|
|
window.ReactNativeWebView.postMessage(
|
|
|
|
"error: Unhandled promise rejection: " + event
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-08-08 17:00:14 +02:00
|
|
|
if (!window.cm) {
|
|
|
|
// This variable is not used within this script
|
|
|
|
// but is called using "injectJavaScript" from
|
|
|
|
// the wrapper component.
|
|
|
|
window.cm = null;
|
2021-07-13 20:13:13 +02:00
|
|
|
|
2022-08-08 17:00:14 +02:00
|
|
|
try {
|
|
|
|
${shim.injectedJs('codeMirrorBundle')};
|
2022-06-26 19:21:38 +02:00
|
|
|
|
2022-08-08 17:00:14 +02:00
|
|
|
const parentElement = document.getElementsByClassName('CodeMirror')[0];
|
|
|
|
const initialText = ${JSON.stringify(props.initialText)};
|
|
|
|
const settings = ${JSON.stringify(editorSettings)};
|
|
|
|
|
|
|
|
cm = codeMirrorBundle.initCodeMirror(parentElement, initialText, settings);
|
2023-12-17 22:58:22 +02:00
|
|
|
|
2022-08-08 17:00:14 +02:00
|
|
|
${setInitialSelectionJS}
|
|
|
|
|
|
|
|
window.onresize = () => {
|
2023-09-21 10:12:40 +02:00
|
|
|
cm.execCommand('scrollSelectionIntoView');
|
2022-08-08 17:00:14 +02:00
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
window.ReactNativeWebView.postMessage("error:" + e.message + ": " + JSON.stringify(e))
|
|
|
|
}
|
2021-07-13 20:13:13 +02:00
|
|
|
}
|
2022-07-10 16:00:21 +02:00
|
|
|
true;
|
2021-07-13 20:13:13 +02:00
|
|
|
`;
|
|
|
|
|
2022-04-11 12:57:49 +02:00
|
|
|
const css = useCss(props.themeId);
|
|
|
|
const html = useHtml(css);
|
2023-09-21 10:12:40 +02:00
|
|
|
const [selectionState, setSelectionState] = useState<SelectionFormatting>(defaultSelectionFormatting);
|
2022-08-08 17:00:14 +02:00
|
|
|
const [linkDialogVisible, setLinkDialogVisible] = useState(false);
|
2022-08-29 15:19:04 +02:00
|
|
|
const [searchState, setSearchState] = useState(defaultSearchState);
|
|
|
|
|
2023-09-21 10:12:40 +02:00
|
|
|
// Runs [js] in the context of the CodeMirror frame.
|
2022-08-08 17:00:14 +02:00
|
|
|
const injectJS = (js: string) => {
|
2022-09-05 13:46:13 +02:00
|
|
|
webviewRef.current.injectJS(js);
|
2022-08-08 17:00:14 +02:00
|
|
|
};
|
|
|
|
|
2022-08-29 15:19:04 +02:00
|
|
|
const editorControl = useEditorControl(
|
2023-11-07 14:00:36 +02:00
|
|
|
injectJS, setLinkDialogVisible, setSearchState,
|
2022-08-29 15:19:04 +02:00
|
|
|
);
|
2022-08-08 17:00:14 +02:00
|
|
|
|
|
|
|
useImperativeHandle(ref, () => {
|
|
|
|
return editorControl;
|
2021-07-13 20:13:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const onMessage = useCallback((event: any) => {
|
|
|
|
const data = event.nativeEvent.data;
|
|
|
|
|
|
|
|
if (data.indexOf('error:') === 0) {
|
2024-02-02 19:54:16 +02:00
|
|
|
logger.error('CodeMirror:', data);
|
2021-07-13 20:13:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const msg = JSON.parse(data);
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2021-07-13 20:13:13 +02:00
|
|
|
const handlers: Record<string, Function> = {
|
|
|
|
onLog: (event: any) => {
|
2024-02-02 19:54:16 +02:00
|
|
|
logger.info('CodeMirror:', ...event.value);
|
2021-07-13 20:13:13 +02:00
|
|
|
},
|
|
|
|
|
2023-09-21 10:12:40 +02:00
|
|
|
onEditorEvent: (event: EditorEvent) => {
|
|
|
|
let exhaustivenessCheck: never;
|
|
|
|
switch (event.kind) {
|
|
|
|
case EditorEventType.Change:
|
|
|
|
props.onChange(event);
|
|
|
|
break;
|
|
|
|
case EditorEventType.UndoRedoDepthChange:
|
|
|
|
props.onUndoRedoDepthChange(event);
|
|
|
|
break;
|
|
|
|
case EditorEventType.SelectionRangeChange:
|
|
|
|
props.onSelectionChange(event);
|
|
|
|
break;
|
|
|
|
case EditorEventType.SelectionFormattingChange:
|
|
|
|
setSelectionState(event.formatting);
|
|
|
|
break;
|
|
|
|
case EditorEventType.EditLink:
|
|
|
|
editorControl.showLinkDialog();
|
|
|
|
break;
|
|
|
|
case EditorEventType.UpdateSearchDialog:
|
|
|
|
setSearchState(event.searchState);
|
|
|
|
|
|
|
|
if (event.searchState.dialogVisible) {
|
|
|
|
editorControl.searchControl.showSearch();
|
|
|
|
} else {
|
|
|
|
editorControl.searchControl.hideSearch();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EditorEventType.Scroll:
|
|
|
|
// Not handled
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exhaustivenessCheck = event;
|
|
|
|
return exhaustivenessCheck;
|
|
|
|
}
|
|
|
|
return;
|
2022-08-08 17:00:14 +02:00
|
|
|
},
|
2021-07-13 20:13:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (handlers[msg.name]) {
|
|
|
|
handlers[msg.name](msg.data);
|
|
|
|
} else {
|
2024-02-02 19:54:16 +02:00
|
|
|
logger.warn('Unsupported CodeMirror message:', msg);
|
2021-07-13 20:13:13 +02:00
|
|
|
}
|
2022-08-29 15:19:04 +02:00
|
|
|
}, [props.onSelectionChange, props.onUndoRedoDepthChange, props.onChange, editorControl]);
|
2021-07-13 20:13:13 +02:00
|
|
|
|
2024-02-02 19:54:16 +02:00
|
|
|
const onError = useCallback((event: NativeSyntheticEvent<WebViewErrorEvent>) => {
|
|
|
|
logger.error(`Load error: Code ${event.nativeEvent.code}: ${event.nativeEvent.description}`);
|
2022-08-29 15:19:04 +02:00
|
|
|
}, []);
|
2021-07-13 20:13:13 +02:00
|
|
|
|
2023-08-18 10:45:04 +02:00
|
|
|
const [hasSpaceForToolbar, setHasSpaceForToolbar] = useState(true);
|
|
|
|
const toolbarEnabled = props.toolbarEnabled && hasSpaceForToolbar;
|
|
|
|
|
|
|
|
const onContainerLayout = useCallback((event: LayoutChangeEvent) => {
|
|
|
|
const containerHeight = event.nativeEvent.layout.height;
|
|
|
|
|
|
|
|
if (containerHeight < 140) {
|
|
|
|
setHasSpaceForToolbar(false);
|
|
|
|
} else {
|
|
|
|
setHasSpaceForToolbar(true);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2023-03-16 13:12:56 +02:00
|
|
|
const toolbar = <MarkdownToolbar
|
|
|
|
style={{
|
|
|
|
// Don't show the markdown toolbar if there isn't enough space
|
|
|
|
// for it:
|
|
|
|
flexShrink: 1,
|
|
|
|
}}
|
|
|
|
editorSettings={editorSettings}
|
|
|
|
editorControl={editorControl}
|
|
|
|
selectionState={selectionState}
|
|
|
|
searchState={searchState}
|
|
|
|
onAttach={props.onAttach}
|
2023-07-16 18:42:42 +02:00
|
|
|
readOnly={props.readOnly}
|
2023-03-16 13:12:56 +02:00
|
|
|
/>;
|
|
|
|
|
2022-08-08 17:00:14 +02:00
|
|
|
return (
|
2023-08-18 10:45:04 +02:00
|
|
|
<View
|
|
|
|
testID='note-editor-root'
|
|
|
|
onLayout={onContainerLayout}
|
|
|
|
style={{
|
|
|
|
...props.style,
|
|
|
|
flexDirection: 'column',
|
|
|
|
}}
|
|
|
|
>
|
2022-08-08 17:00:14 +02:00
|
|
|
<EditLinkDialog
|
|
|
|
visible={linkDialogVisible}
|
|
|
|
themeId={props.themeId}
|
|
|
|
editorControl={editorControl}
|
|
|
|
selectionState={selectionState}
|
|
|
|
/>
|
|
|
|
<View style={{
|
|
|
|
flexGrow: 1,
|
|
|
|
flexShrink: 0,
|
2022-08-29 15:19:04 +02:00
|
|
|
minHeight: '30%',
|
|
|
|
...props.contentStyle,
|
2022-08-08 17:00:14 +02:00
|
|
|
}}>
|
2022-09-05 13:46:13 +02:00
|
|
|
<ExtendedWebView
|
2022-09-12 11:46:12 +02:00
|
|
|
webviewInstanceId='NoteEditor'
|
2023-12-17 22:57:40 +02:00
|
|
|
scrollEnabled={true}
|
2022-08-08 17:00:14 +02:00
|
|
|
ref={webviewRef}
|
2022-09-05 13:46:13 +02:00
|
|
|
html={html}
|
2022-08-08 17:00:14 +02:00
|
|
|
injectedJavaScript={injectedJavaScript}
|
|
|
|
onMessage={onMessage}
|
|
|
|
onError={onError}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
|
|
|
|
<SearchPanel
|
|
|
|
editorSettings={editorSettings}
|
|
|
|
searchControl={editorControl.searchControl}
|
|
|
|
searchState={searchState}
|
|
|
|
/>
|
2022-08-29 15:19:04 +02:00
|
|
|
|
2023-08-18 10:45:04 +02:00
|
|
|
{toolbarEnabled ? toolbar : null}
|
2022-08-08 17:00:14 +02:00
|
|
|
</View>
|
|
|
|
);
|
2021-07-13 20:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default forwardRef(NoteEditor);
|