Skip to content

Commit

Permalink
Bullet text watcher fixes & enhancement
Browse files Browse the repository at this point in the history
- Fix auto bullet not working with auto-suggesting keyboards: they underline the last word, and on enter the text changes includes the whole word because the underline is removed, instead of just the newline character as previously thought.
- Allow bullet indentation.
  • Loading branch information
maltaisn committed Dec 30, 2022
1 parent 9520ab2 commit 417c116
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Change default focus to title when creating a new note.
- Fix animated swipe icons not working on API <23.
- Allow zero lines in note preview (#83).
- Fix auto bullet feature not working with auto-suggesting keyboards.

## v1.4.5 (2022-09-25)
- Fixed cursor moving to first line when scrolling (#60, thanks to @nhoeher)
Expand Down
36 changes: 15 additions & 21 deletions app/src/main/kotlin/com/maltaisn/notes/ui/edit/BulletTextWatcher.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Nicolas Maltais
* Copyright 2022 Nicolas Maltais
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,8 @@ class BulletTextWatcher : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit

override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) {
if (text != null && count == 1 && text[start] == '\n') {
val end = start + count - 1
if (text != null && count - before == 1 && text[end] == '\n') {
// User inserted a single char, a line break.
// Find start position of current line.
var pos = start - 1
Expand All @@ -43,27 +44,16 @@ class BulletTextWatcher : TextWatcher {
val lineStart = pos + 1

// Check if current line used a list bullet.
val bullet = text[lineStart]
if (bullet in Note.BULLET_CHARS) {
// Get the bullet text, i.e the bullet with all following whitespaces.
val bulletText = buildString {
append(bullet)
pos = lineStart + 1
while (pos < start) {
val c = text[pos]
if (!c.isWhitespace()) {
break
}
append(text[pos])
pos++
}
}
bulletChange = if (pos == start) {
// Last item was blank, create no new bullet and delete current one.
{ it.delete(lineStart, start + 1) }
val lineText = text.substring(lineStart, end)
val bulletMatch = BULLET_REGEX.find(lineText)
if (bulletMatch != null) {
val bulletText = bulletMatch.value
bulletChange = if (lineStart + bulletText.length == end) {
// Last item was blank, create no new bullet and delete current one (also delete newline).
{ it.delete(lineStart, end + 1) }
} else {
// Last item wasn't blank, insert bullet text on new line.
{ it.insert(start + 1, bulletText, 0, bulletText.length) }
{ it.insert(end + 1, bulletText, 0, bulletText.length) }
}
}
}
Expand All @@ -76,4 +66,8 @@ class BulletTextWatcher : TextWatcher {
bulletChange = null
change?.invoke(text ?: return)
}

companion object {
val BULLET_REGEX = """\s*[${Note.BULLET_CHARS}]\s*""".toRegex()
}
}

0 comments on commit 417c116

Please sign in to comment.