Skip to content

Commit 2fc322a

Browse files
committed
feat: preserve note position when editing
1 parent 7d48be5 commit 2fc322a

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎app/src/androidTest/kotlin/org/qownnotes/mobile/AppLaunchTest.kt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,26 @@ class AppLaunchTest {
331331
assertTrue(textSizeOf(R.id.markdown_editor) > initial)
332332
}
333333

334+
@Test
335+
fun editingStartsNearTheCurrentReadingPosition() {
336+
val content = (1..80).joinToString("\n\n") {
337+
"Paragraph $it of a note that is longer than a screen."
338+
}
339+
importAccount("alice", "Long note", "etag-1", 10, content)
340+
composeRule.onNodeWithText("Long note").performClick()
341+
composeRule.waitForTag("markdown-view")
342+
composeRule.onNodeWithTag("markdown-view").performTouchInput { swipeUp() }
343+
344+
composeRule.enterEditMode()
345+
346+
var selection = 0
347+
onView(withId(R.id.markdown_editor)).check { view, _ ->
348+
selection = (view as TextView).selectionStart
349+
}
350+
assertTrue("expected selection after the start of the note", selection > 0)
351+
assertTrue("expected selection before the end of the note", selection < content.length)
352+
}
353+
334354
@Test
335355
fun noteTextSizeStopsAtItsSmallestStep() {
336356
importAccount("alice", "Existing note", "etag-1", 10)

‎app/src/main/kotlin/org/qownnotes/mobile/MainActivity.kt‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,12 @@ private fun NoteDetailScreen(
537537
// spans rescale without re-rendering the note.
538538
renderedView?.setTextSize(TypedValue.COMPLEX_UNIT_SP, noteTextSizeSp.toFloat())
539539
}
540+
LaunchedEffect(editing, editor) {
541+
val view = editor ?: return@LaunchedEffect
542+
if (!editing) return@LaunchedEffect
543+
repeat(2) { withFrameNanos { } }
544+
view.bringPointIntoView(selectionStart)
545+
}
540546
val latestDraft by rememberUpdatedState(draft)
541547
val latestNote by rememberUpdatedState(note)
542548
val latestEditing by rememberUpdatedState(editing)
@@ -678,8 +684,12 @@ private fun NoteDetailScreen(
678684
scope.launch {
679685
component.beginEditing(localId)?.let { editable ->
680686
draft = editable.content
681-
selectionStart = editable.content.length
682-
selectionEnd = editable.content.length
687+
selectionStart = sourceOffsetForReadingPosition(
688+
renderedView,
689+
scrollState.value,
690+
editable.content
691+
)
692+
selectionEnd = selectionStart
683693
editing = true
684694
}
685695
}
@@ -910,6 +920,21 @@ private class RenderedNote {
910920
}
911921
}
912922

923+
private fun sourceOffsetForReadingPosition(
924+
view: AppCompatTextView?,
925+
scrollY: Int,
926+
markdown: String
927+
): Int {
928+
val layout = view?.layout ?: return markdown.length
929+
val renderedLength = view.text.length
930+
if (renderedLength == 0 || markdown.isEmpty()) return 0
931+
val line = layout.getLineForVertical(scrollY.coerceIn(0, layout.height))
932+
val renderedOffset = layout.getLineStart(line)
933+
val approximateOffset = (renderedOffset.toLong() * markdown.length / renderedLength).toInt()
934+
if (approximateOffset == 0) return 0
935+
return markdown.lastIndexOf('\n', (approximateOffset - 1).coerceAtLeast(0)) + 1
936+
}
937+
913938
/**
914939
* Finds text in the note that is being read. The bar stays above the note while the note scrolls,
915940
* so the query and the position within the matches remain visible while moving through them.

0 commit comments

Comments
 (0)