diff --git a/internal/editor/state.go b/internal/editor/state.go index 58eb6f5..589eb4d 100644 --- a/internal/editor/state.go +++ b/internal/editor/state.go @@ -3,6 +3,7 @@ package editor import ( "log" "sort" + "unicode/utf8" "pad/internal/browser" "pad/internal/ui" @@ -485,13 +486,10 @@ func SetCursorFromPoint(x, y float64) { // 3. Check if tap is to the right of the last character on this line if rightmostIdx != -1 && x > rightmostX { - // If rightmostIdx is the last glyph of the entire document - if rightmostIdx == len(layout.X)-1 { - TheState.Editor.CursorPosition = len(TheState.Editor.Buffer) - } else { - // Position after the last character on this line - TheState.Editor.CursorPosition = layout.ByteOffsets[rightmostIdx+1] - } + // Position after the last character on this line + start := layout.ByteOffsets[rightmostIdx] + _, size := utf8.DecodeRuneInString(TheState.Editor.Buffer[start:]) + TheState.Editor.CursorPosition = start + size return }