add missing methods for editor

This commit is contained in:
ammarahm-ed
2022-05-21 17:47:06 +05:00
parent 90a58965bd
commit e4e395525d
4 changed files with 51 additions and 33 deletions

View File

@@ -44,7 +44,6 @@ const EditorHeader = ({ editor }) => {
const insets = useSafeAreaInsets();
const handleBack = useRef();
const keyboardListener = useRef();
const closing = useRef(false);
const searchReplace = useEditorStore(state => state.searchReplace);
const readonly = useEditorStore(state => state.readonly);
@@ -69,12 +68,6 @@ const EditorHeader = ({ editor }) => {
editorState().currentlyEditing = false;
keyboardListener.current?.remove();
editor?.reset();
Navigation.setRoutesToUpdate([
Navigation.routeNames.NotesPage,
Navigation.routeNames.Favorites,
Navigation.routeNames.Notes,
Navigation.routeNames.Notebook
]);
}, 1);
};

View File

@@ -25,6 +25,11 @@ const Editor = React.memo(
const editor = useEditor();
editorController.current = editor;
const onError = () => {
editorController.current?.setLoading(true);
setTimeout(() => editorController.current?.setLoading(false), 10);
};
return editor.loading ? null : (
<>
<View
@@ -39,15 +44,12 @@ const Editor = React.memo(
testID={notesnook.editor.id}
ref={editor.ref}
onLoad={editor.onLoad}
// onRenderProcessGone={() => {
// onResetRequested();
// }}
// onError={() => {
// onResetRequested();
// }}
onRenderProcessGone={onError}
onError={onError}
injectedJavaScript={`globalThis.sessionId="${editor.sessionId}";`}
javaScriptEnabled={true}
focusable={true}
overScrollMode="never"
keyboardDisplayRequiresUserAction={false}
// onShouldStartLoadWithRequest={_onShouldStartLoadWithRequest}
cacheMode="LOAD_DEFAULT"
@@ -64,7 +66,7 @@ const Editor = React.memo(
allowUniversalAccessFromFileURLs={true}
originWhitelist={['*']}
source={{
uri: 'http://192.168.10.5:3000'
uri: 'http://192.168.10.8:3000'
}}
style={style}
autoManageStatusBarEnabled={false}

View File

@@ -16,6 +16,12 @@ export type EditorState = {
saveCount: 0;
};
export type EditorMessage = {
sessionId: string;
value: any;
type: string;
};
export type Note = {
[name: string]: any;
id: string | null;

View File

@@ -10,10 +10,13 @@ import { useThemeStore } from '../../../stores/use-theme-store';
import { db } from '../../../utils/database';
import { MMKV } from '../../../utils/database/mmkv';
import { eOnLoadNote, eOpenTagsDialog } from '../../../utils/events';
import filesystem from '../../../utils/filesystem';
import { tabBarRef } from '../../../utils/global-refs';
import { timeConverter } from '../../../utils/time';
import { AttachmentType } from '../../../utils/types';
import Commands from './commands';
import { EditorState, Note, Content, AppState, SavePayload } from './types';
import picker from './picker';
import { EditorState, Note, Content, AppState, SavePayload, EditorMessage } from './types';
import { defaultState, EditorEvents, isEditorLoaded, makeSessionId, post } from './utils';
export const useEditor = () => {
@@ -234,36 +237,37 @@ export const useEditor = () => {
const onMessage = useCallback(
event => {
let message = event.nativeEvent.data;
message = JSON.parse(message);
const data = event.nativeEvent.data;
let editorMessage = JSON.parse(data) as EditorMessage;
console.log(message.type);
if (message.sessionId !== sessionId && message.type !== EditorEvents.status) {
console.log(
'useEditor: message recieved from invalid session',
message.type,
logger.info('editor', editorMessage.type);
if (editorMessage.sessionId !== sessionId && editorMessage.type !== EditorEvents.status) {
logger.error(
'editor',
'invalid session',
editorMessage.type,
sessionId,
message.sessionId
editorMessage.sessionId
);
return;
}
switch (message.type) {
switch (editorMessage.type) {
case EditorEvents.logger:
console.log(message.type, message.value);
logger.info('editor-webview', editorMessage.value);
break;
case 'editor-event:content':
saveContent({
type: message.type,
content: message.value
type: editorMessage.type,
content: editorMessage.value
});
break;
case 'editor-event:selection':
break;
case 'editor-event:title':
saveContent({
type: message.type,
title: message.value
type: editorMessage.type,
title: editorMessage.value
});
break;
case 'editor-event:newtag':
@@ -271,12 +275,12 @@ export const useEditor = () => {
eSendEvent(eOpenTagsDialog, currentNote.current);
break;
case 'editor-event:tag':
if (message.value) {
if (editorMessage.value) {
if (!currentNote.current) return;
db.notes
//@ts-ignore
?.note(currentNote.current?.id)
.untag(message.value)
.untag(editorMessage.value)
.then(async () => {
useTagStore.getState().setTags();
await commands.setTags(currentNote.current);
@@ -290,8 +294,21 @@ export const useEditor = () => {
});
}
break;
case 'editor-event:picker':
picker.pick();
break;
case 'editor-event:download-attachment':
filesystem.downloadAttachment(editorMessage.value?.hash, true);
break;
default:
console.log(
'unhandled event recieved from editor: ',
editorMessage.type,
editorMessage.value
);
break;
}
eSendEvent(message.type, message);
eSendEvent(editorMessage.type, editorMessage);
},
[sessionId]
);