Skip to content

Commit

Permalink
Mobile: Fixes laurent22#9532: Fix editor cursor location no longer pr…
Browse files Browse the repository at this point in the history
…eserved
  • Loading branch information
personalizedrefrigerator committed Dec 16, 2023
1 parent e47ac1d commit a23cbcf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
4 changes: 3 additions & 1 deletion packages/app-mobile/components/NoteEditor/NoteEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ function NoteEditor(props: Props, ref: any) {
const webviewRef = useRef(null);

const setInitialSelectionJS = props.initialSelection ? `
cm.select(${props.initialSelection.start}, ${props.initialSelection.end});
cm.select(${props.initialSelection.from}, ${props.initialSelection.to});
cm.execCommand('scrollSelectionIntoView');
` : '';

const editorSettings: EditorSettings = {
Expand Down Expand Up @@ -331,6 +332,7 @@ function NoteEditor(props: Props, ref: any) {
const settings = ${JSON.stringify(editorSettings)};
cm = codeMirrorBundle.initCodeMirror(parentElement, initialText, settings);
${setInitialSelectionJS}
window.onresize = () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/app-mobile/components/NoteEditor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ export interface EditorSettings extends EditorBodySettings {
}

export interface SelectionRange {
start: number;
end: number;
from: number;
to: number;
}
23 changes: 11 additions & 12 deletions packages/app-mobile/components/screens/Note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import NoteEditor from '../NoteEditor/NoteEditor';
import { Size } from '@joplin/utils/types';
const FileViewer = require('react-native-file-viewer').default;
const React = require('react');
import { Keyboard, View, TextInput, StyleSheet, Linking, Image, Share } from 'react-native';
import { Keyboard, View, TextInput, StyleSheet, Linking, Image, Share, NativeSyntheticEvent } from 'react-native';
import { Platform, PermissionsAndroid } from 'react-native';
const { connect } = require('react-redux');
// const { MarkdownEditor } = require('@joplin/lib/../MarkdownEditor/index.js');
Expand Down Expand Up @@ -52,7 +52,7 @@ import isEditableResource from '../NoteEditor/ImageEditor/isEditableResource';
import VoiceTypingDialog from '../voiceTyping/VoiceTypingDialog';
import { voskEnabled } from '../../services/voiceTyping/vosk';
import { isSupportedLanguage } from '../../services/voiceTyping/vosk.android';
import { ChangeEvent as EditorChangeEvent, UndoRedoDepthChangeEvent } from '@joplin/editor/events';
import { ChangeEvent as EditorChangeEvent, SelectionRangeChangeEvent, UndoRedoDepthChangeEvent } from '@joplin/editor/events';
import { join } from 'path';
import { Dispatch } from 'redux';
import { RefObject } from 'react';
Expand Down Expand Up @@ -371,7 +371,6 @@ class NoteScreenComponent extends BaseScreenComponent<Props, State> implements B
this.undoRedoService_stackChange = this.undoRedoService_stackChange.bind(this);
this.screenHeader_undoButtonPress = this.screenHeader_undoButtonPress.bind(this);
this.screenHeader_redoButtonPress = this.screenHeader_redoButtonPress.bind(this);
this.body_selectionChange = this.body_selectionChange.bind(this);
this.onBodyViewerLoadEnd = this.onBodyViewerLoadEnd.bind(this);
this.onBodyViewerCheckboxChange = this.onBodyViewerCheckboxChange.bind(this);
this.onBodyChange = this.onBodyChange.bind(this);
Expand Down Expand Up @@ -640,13 +639,13 @@ class NoteScreenComponent extends BaseScreenComponent<Props, State> implements B
this.scheduleSave();
}

private body_selectionChange(event: any) {
if (this.useEditorBeta()) {
this.selection = event.selection;
} else {
this.selection = event.nativeEvent.selection;
}
}
private onPlainEdtiorSelectionChange = (event: NativeSyntheticEvent<any>) => {
this.selection = event.nativeEvent.selection;
};

private onMarkdownEditorSelectionChange = (event: SelectionRangeChangeEvent) => {
this.selection = { from: event.from, to: event.to };
};

public makeSaveAction() {
return async () => {
Expand Down Expand Up @@ -1508,7 +1507,7 @@ class NoteScreenComponent extends BaseScreenComponent<Props, State> implements B
multiline={true}
value={note.body}
onChangeText={(text: string) => this.body_changeText(text)}
onSelectionChange={this.body_selectionChange}
onSelectionChange={this.onPlainEdtiorSelectionChange}
blurOnSubmit={false}
selectionColor={theme.textSelectionColor}
keyboardAppearance={theme.keyboardAppearance}
Expand All @@ -1530,7 +1529,7 @@ class NoteScreenComponent extends BaseScreenComponent<Props, State> implements B
initialText={note.body}
initialSelection={this.selection}
onChange={this.onBodyChange}
onSelectionChange={this.body_selectionChange}
onSelectionChange={this.onMarkdownEditorSelectionChange}
onUndoRedoDepthChange={this.onUndoRedoDepthChange}
onAttach={() => this.showAttachMenu()}
readOnly={this.state.readOnly}
Expand Down

0 comments on commit a23cbcf

Please sign in to comment.