Skip to content

Commit 11656cc

Browse files
committed
Update code based on review
Update intention of checkboxUpdate -> refocusEditorAndCursor Move scroll editor into line method to codemirror util Refactor initial cursor method call for refocus
1 parent ae81688 commit 11656cc

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

src/components/atoms/CustomizedMarkdownPreviewer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface CustomizedMarkdownPreviewer {
99
attachmentMap?: ObjectMap<Attachment>
1010
updateContent?: (
1111
newContentOrUpdater: string | ((newValue: string) => string),
12-
checkboxUpdate?: boolean
12+
refocusEditorAndCursor?: boolean
1313
) => void
1414
}
1515

src/components/atoms/MarkdownPreviewer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ interface MarkdownPreviewerProps {
7070
attachmentMap?: ObjectMap<Attachment>
7171
updateContent?: (
7272
newContentOrUpdater: string | ((newValue: string) => string),
73-
checkboxUpdate?: boolean
73+
refocusEditorAndCursor?: boolean
7474
) => void
7575
}
7676

src/components/atoms/markdown/MarkdownCheckbox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface MarkdownCheckboxProps {
55
checked?: boolean
66
updateContent?: (
77
newValueOrUpdater: string | ((prevValue: string) => string),
8-
checkboxUpdate?: boolean
8+
refocusEditorAndCursor?: boolean
99
) => void
1010
}
1111

src/components/organisms/LocalSearch.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { compareEventKeyWithKeymap } from '../../lib/keymap'
3232
import { usePreferences } from '../../lib/preferences'
3333
import styled from '../../shared/lib/styled'
3434
import Icon from '../../shared/components/atoms/Icon'
35+
import { scrollEditorToLine } from '../../shared/lib/codemirror/util'
3536

3637
const LOCAL_SEARCH_MAX_RESULTS = 10000
3738

@@ -51,15 +52,6 @@ interface LocalSearchProps {
5152

5253
export type SearchResultNavigationDirection = 'next' | 'previous'
5354

54-
export function scrollEditorToLine(
55-
editor: CodeMirror.EditorFromTextArea,
56-
lineNumber: number
57-
) {
58-
const t = editor.charCoords({ line: lineNumber, ch: 0 }, 'local').top
59-
const middleHeight = editor.getScrollerElement().offsetHeight / 2
60-
editor.scrollTo(null, t - middleHeight - 5)
61-
}
62-
6355
const LocalSearch = ({
6456
searchQuery,
6557
replaceQuery,

src/components/organisms/NoteDetail.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import EditorThemeSelect from '../molecules/EditorThemeSelect'
2020
import EditorKeyMapSelect from '../molecules/EditorKeyMapSelect'
2121
import { addIpcListener, removeIpcListener } from '../../lib/electronOnly'
2222
import { MarkerRange } from 'codemirror'
23-
import LocalSearch, { scrollEditorToLine } from './LocalSearch'
23+
import LocalSearch from './LocalSearch'
2424
import { SearchReplaceOptions } from '../../lib/search/search'
2525
import {
2626
borderTop,
@@ -29,6 +29,7 @@ import {
2929
} from '../../shared/lib/styled/styleFunctions'
3030
import styled from '../../shared/lib/styled'
3131
import EditorToolbar, { formatCodeMirrorText } from './editor/EditorToolbar'
32+
import { scrollEditorToLine } from '../../shared/lib/codemirror/util'
3233

3334
type NoteDetailProps = {
3435
note: NoteDoc
@@ -164,6 +165,10 @@ class NoteDetail extends React.Component<NoteDetailProps, NoteDetailState> {
164165
}
165166

166167
setInitialCursor() {
168+
this.setCursorToLastPositionOrInit()
169+
}
170+
171+
setCursorToLastPositionOrInit() {
167172
if (this.codeMirror == null) {
168173
return
169174
}
@@ -223,7 +228,7 @@ class NoteDetail extends React.Component<NoteDetailProps, NoteDetailState> {
223228

224229
updateContent = (
225230
newValueOrUpdater: string | ((prevValue: string) => string),
226-
checkboxUpdate = false
231+
refocusEditorAndCursor = false
227232
) => {
228233
const updater =
229234
typeof newValueOrUpdater === 'string'
@@ -236,9 +241,9 @@ class NoteDetail extends React.Component<NoteDetailProps, NoteDetailState> {
236241
}
237242
},
238243
() => {
239-
if (this.codeMirror != null && checkboxUpdate) {
244+
if (this.codeMirror != null && refocusEditorAndCursor) {
240245
this.codeMirror.focus()
241-
this.setInitialCursor()
246+
this.setCursorToLastPositionOrInit()
242247
}
243248
this.queueToSave()
244249
}

src/shared/lib/codemirror/util.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,12 @@ export function loadMode(_CodeMirror: any) {
4141
return modeObj
4242
}
4343
}
44+
45+
export function scrollEditorToLine(
46+
editor: CodeMirror.EditorFromTextArea,
47+
lineNumber: number
48+
) {
49+
const t = editor.charCoords({ line: lineNumber, ch: 0 }, 'local').top
50+
const middleHeight = editor.getScrollerElement().offsetHeight / 2
51+
editor.scrollTo(null, t - middleHeight - 5)
52+
}

0 commit comments

Comments
 (0)