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';
|
2024-03-11 17:02:15 +02:00
|
|
|
import themeToCss from '@joplin/lib/services/style/themeToCss';
|
2022-08-08 17:00:14 +02:00
|
|
|
import EditLinkDialog from './EditLinkDialog';
|
|
|
|
import { defaultSearchState, SearchPanel } from './SearchPanel';
|
2024-03-11 17:02:15 +02:00
|
|
|
import ExtendedWebView, { WebViewControl } from '../ExtendedWebView';
|
2022-08-08 17:00:14 +02:00
|
|
|
|
2023-09-21 10:12:40 +02:00
|
|
|
import * as React from 'react';
|
2024-03-14 20:42:22 +02:00
|
|
|
import { forwardRef, RefObject, useEffect, useImperativeHandle } from 'react';
|
2023-11-07 14:00:36 +02:00
|
|
|
import { useMemo, useState, useCallback, useRef } from 'react';
|
2024-02-02 19:54:16 +02:00
|
|
|
import { LayoutChangeEvent, NativeSyntheticEvent, View, ViewStyle } from 'react-native';
|
2024-03-09 13:15:13 +02:00
|
|
|
import { editorFont } from '../global-style';
|
2021-07-13 20:13:13 +02:00
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
import { EditorControl as EditorBodyControl, ContentScriptData } from '@joplin/editor/types';
|
|
|
|
import { EditorControl, EditorSettings, SelectionRange, WebViewToEditorApi } 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-03-11 17:02:15 +02:00
|
|
|
import { EditorCommandType, EditorKeymap, EditorLanguageType, SearchState } from '@joplin/editor/types';
|
2023-09-21 10:12:40 +02:00
|
|
|
import SelectionFormatting, { defaultSelectionFormatting } from '@joplin/editor/SelectionFormatting';
|
2024-03-11 17:02:15 +02:00
|
|
|
import useCodeMirrorPlugins from './hooks/useCodeMirrorPlugins';
|
|
|
|
import RNToWebViewMessenger from '../../utils/ipc/RNToWebViewMessenger';
|
|
|
|
import { WebViewMessageEvent } from 'react-native-webview';
|
2024-02-02 19:54:16 +02:00
|
|
|
import { WebViewErrorEvent } from 'react-native-webview/lib/RNCWebViewNativeComponent';
|
2024-03-11 17:02:15 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
import { PluginStates } from '@joplin/lib/services/plugins/reducer';
|
|
|
|
import useEditorCommandHandler from './hooks/useEditorCommandHandler';
|
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;
|
2023-03-16 13:12:56 +02:00
|
|
|
toolbarEnabled: boolean;
|
2023-07-16 18:42:42 +02:00
|
|
|
readOnly: boolean;
|
2024-03-11 17:02:15 +02:00
|
|
|
plugins: PluginStates;
|
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);
|
2024-03-11 17:02:15 +02:00
|
|
|
const themeVariableCss = themeToCss(theme);
|
2022-04-11 12:57:49 +02:00
|
|
|
return `
|
2024-03-11 17:02:15 +02:00
|
|
|
${themeVariableCss}
|
|
|
|
|
2022-04-11 12:57:49 +02:00
|
|
|
: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]);
|
|
|
|
}
|
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
const themeStyleSheetClassName = 'note-editor-styles';
|
|
|
|
function useHtml(initialCss: string): string {
|
|
|
|
const cssRef = useRef(initialCss);
|
|
|
|
cssRef.current = initialCss;
|
|
|
|
|
|
|
|
return useMemo(() => `
|
|
|
|
<!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>
|
|
|
|
/* For better scrolling on iOS (working scrollbar) we use external, rather than internal,
|
|
|
|
scrolling. */
|
|
|
|
.cm-scroller {
|
|
|
|
overflow: none;
|
|
|
|
|
|
|
|
/* Ensure that the editor can be focused by clicking on the lower half of the screen.
|
|
|
|
Don't use 100vh to prevent a scrollbar being present for empty notes. */
|
|
|
|
min-height: 80vh;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<style class=${JSON.stringify(themeStyleSheetClassName)}>
|
|
|
|
${cssRef.current}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="CodeMirror" style="height:100%;" autocapitalize="on"></div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`, []);
|
2021-07-13 20:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 OnSetVisibleCallback = (visible: boolean)=> void;
|
|
|
|
type OnSearchStateChangeCallback = (state: SearchState)=> void;
|
|
|
|
const useEditorControl = (
|
2024-03-11 17:02:15 +02:00
|
|
|
bodyControl: EditorBodyControl,
|
2024-03-14 20:42:22 +02:00
|
|
|
webviewRef: RefObject<WebViewControl>,
|
2024-03-11 17:02:15 +02:00
|
|
|
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) => {
|
2024-03-11 17:02:15 +02:00
|
|
|
void bodyControl.execCommand(command);
|
2023-09-21 10:12:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const setSearchStateCallback = (state: SearchState) => {
|
2024-03-11 17:02:15 +02:00
|
|
|
bodyControl.setSearchState(state);
|
2023-09-21 10:12:40 +02:00
|
|
|
setSearchState(state);
|
|
|
|
};
|
|
|
|
|
|
|
|
const control: EditorControl = {
|
|
|
|
supportsCommand(command: EditorCommandType) {
|
2024-03-14 21:04:32 +02:00
|
|
|
return bodyControl.supportsCommand(command);
|
2023-09-21 10:12:40 +02:00
|
|
|
},
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-03-11 17:02:15 +02:00
|
|
|
execCommand(command, ...args: any[]) {
|
|
|
|
return bodyControl.execCommand(command, ...args);
|
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
|
2024-05-28 15:16:48 +02:00
|
|
|
focus() {
|
|
|
|
void bodyControl.execCommand(EditorCommandType.Focus);
|
|
|
|
},
|
|
|
|
|
2022-08-29 15:19:04 +02:00
|
|
|
undo() {
|
2024-03-11 17:02:15 +02:00
|
|
|
bodyControl.undo();
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
redo() {
|
2024-03-11 17:02:15 +02:00
|
|
|
bodyControl.redo();
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
select(anchor: number, head: number) {
|
2024-03-11 17:02:15 +02:00
|
|
|
bodyControl.select(anchor, head);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
setScrollPercent(fraction: number) {
|
2024-03-11 17:02:15 +02:00
|
|
|
bodyControl.setScrollPercent(fraction);
|
2023-09-21 10:12:40 +02:00
|
|
|
},
|
2022-08-29 15:19:04 +02:00
|
|
|
insertText(text: string) {
|
2024-03-11 17:02:15 +02:00
|
|
|
bodyControl.insertText(text);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
updateBody(newBody: string) {
|
2024-03-11 17:02:15 +02:00
|
|
|
bodyControl.updateBody(newBody);
|
2023-09-21 10:12:40 +02:00
|
|
|
},
|
|
|
|
updateSettings(newSettings: EditorSettings) {
|
2024-03-11 17:02:15 +02:00
|
|
|
bodyControl.updateSettings(newSettings);
|
2023-09-21 10:12:40 +02:00
|
|
|
},
|
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) {
|
2024-03-11 17:02:15 +02:00
|
|
|
bodyControl.updateLink(label, url);
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
|
|
|
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() {
|
2024-03-14 20:42:22 +02:00
|
|
|
webviewRef.current.injectJS('document.activeElement?.blur();');
|
2022-08-29 15:19:04 +02:00
|
|
|
},
|
2023-09-21 10:12:40 +02:00
|
|
|
|
2024-02-19 12:29:37 +02:00
|
|
|
setContentScripts: async (plugins: ContentScriptData[]) => {
|
2024-03-11 17:02:15 +02:00
|
|
|
return bodyControl.setContentScripts(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;
|
2024-03-14 20:42:22 +02:00
|
|
|
}, [webviewRef, bodyControl, setLinkDialogVisible, setSearchState]);
|
2022-08-29 15:19:04 +02:00
|
|
|
};
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-07-13 20:13:13 +02:00
|
|
|
function NoteEditor(props: Props, ref: any) {
|
2024-03-11 17:02:15 +02:00
|
|
|
const webviewRef = useRef<WebViewControl>(null);
|
2021-07-13 20:13:13 +02:00
|
|
|
|
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
|
|
|
` : '';
|
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
const editorSettings: EditorSettings = useMemo(() => ({
|
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,
|
|
|
|
|
2024-04-03 19:54:34 +02:00
|
|
|
indentWithTabs: true,
|
2024-03-11 17:02:15 +02:00
|
|
|
}), [props.themeId, props.readOnly]);
|
2022-08-08 17:00:14 +02:00
|
|
|
|
2021-07-13 20:13:13 +02:00
|
|
|
const injectedJavaScript = `
|
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)};
|
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
window.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);
|
2024-03-11 17:02:15 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (webviewRef.current) {
|
|
|
|
webviewRef.current.injectJS(`
|
|
|
|
const styleClass = ${JSON.stringify(themeStyleSheetClassName)};
|
|
|
|
for (const oldStyle of [...document.getElementsByClassName(styleClass)]) {
|
|
|
|
oldStyle.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
const style = document.createElement('style');
|
|
|
|
style.classList.add(styleClass);
|
|
|
|
|
|
|
|
style.appendChild(document.createTextNode(${JSON.stringify(css)}));
|
|
|
|
document.head.appendChild(style);
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
}, [css]);
|
|
|
|
|
2022-04-11 12:57:49 +02:00
|
|
|
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);
|
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
const onEditorEvent = useRef((_event: EditorEvent) => {});
|
|
|
|
|
|
|
|
const editorMessenger = useMemo(() => {
|
|
|
|
const localApi: WebViewToEditorApi = {
|
|
|
|
async onEditorEvent(event) {
|
|
|
|
onEditorEvent.current(event);
|
|
|
|
},
|
|
|
|
async logMessage(message) {
|
|
|
|
logger.debug('CodeMirror:', message);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const messenger = new RNToWebViewMessenger<WebViewToEditorApi, EditorBodyControl>(
|
|
|
|
'editor', webviewRef, localApi,
|
|
|
|
);
|
|
|
|
return messenger;
|
|
|
|
}, []);
|
|
|
|
|
2022-08-29 15:19:04 +02:00
|
|
|
const editorControl = useEditorControl(
|
2024-03-14 20:42:22 +02:00
|
|
|
editorMessenger.remoteApi, webviewRef, setLinkDialogVisible, setSearchState,
|
2022-08-29 15:19:04 +02:00
|
|
|
);
|
2022-08-08 17:00:14 +02:00
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
useEffect(() => {
|
|
|
|
editorControl.updateSettings(editorSettings);
|
|
|
|
}, [editorSettings, editorControl]);
|
|
|
|
|
|
|
|
useEditorCommandHandler(editorControl);
|
|
|
|
|
2022-08-08 17:00:14 +02:00
|
|
|
useImperativeHandle(ref, () => {
|
|
|
|
return editorControl;
|
2021-07-13 20:13:13 +02:00
|
|
|
});
|
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
useEffect(() => {
|
|
|
|
onEditorEvent.current = (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;
|
|
|
|
}
|
2021-07-13 20:13:13 +02:00
|
|
|
return;
|
2024-03-11 17:02:15 +02:00
|
|
|
};
|
|
|
|
}, [props.onChange, props.onUndoRedoDepthChange, props.onSelectionChange, editorControl]);
|
2021-07-13 20:13:13 +02:00
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
const codeMirrorPlugins = useCodeMirrorPlugins(props.plugins);
|
|
|
|
useEffect(() => {
|
|
|
|
void editorControl.setContentScripts(codeMirrorPlugins);
|
|
|
|
}, [codeMirrorPlugins, editorControl]);
|
2021-07-13 20:13:13 +02:00
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
const onLoadEnd = useCallback(() => {
|
|
|
|
editorMessenger.onWebViewLoaded();
|
|
|
|
}, [editorMessenger]);
|
2021-07-13 20:13:13 +02:00
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
const onMessage = useCallback((event: WebViewMessageEvent) => {
|
|
|
|
const data = event.nativeEvent.data;
|
2021-07-13 20:13:13 +02:00
|
|
|
|
2024-03-11 17:02:15 +02:00
|
|
|
if (data.indexOf('error:') === 0) {
|
|
|
|
logger.error('CodeMirror error', data);
|
|
|
|
return;
|
2021-07-13 20:13:13 +02:00
|
|
|
}
|
2024-03-11 17:02:15 +02:00
|
|
|
|
|
|
|
editorMessenger.onWebViewMessage(event);
|
|
|
|
}, [editorMessenger]);
|
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}
|
2024-03-11 17:02:15 +02:00
|
|
|
pluginStates={props.plugins}
|
2023-03-16 13:12:56 +02:00
|
|
|
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%',
|
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}
|
2024-03-20 12:52:58 +02:00
|
|
|
hasPluginScripts={codeMirrorPlugins.length > 0}
|
2022-08-08 17:00:14 +02:00
|
|
|
onMessage={onMessage}
|
2024-03-11 17:02:15 +02:00
|
|
|
onLoadEnd={onLoadEnd}
|
2022-08-08 17:00:14 +02:00
|
|
|
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);
|