fix(editor): a rich text field focused without a cursor puts it at the end

AppFlowy's editor takes the focus without a selection when it is focused
from outside, by Tab or by a field moving it there (Todo's quick add, its
down arrow into the description), so it showed no cursor and took no
typing until clicked. Focused so, it now places the cursor at the end of
its last text.

Whats-New: Moving into a text with Tab or the arrow keys now puts the cursor there, ready to type.
Whats-New-De: Wer mit Tab oder den Pfeiltasten in einen Text wechselt, hat den Cursor jetzt gleich dort und kann tippen.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-11-09 23:12:29 +01:00
commit 6356bed37d
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -117,6 +117,26 @@ class _RichContentEditorState() extends State<RichContentEditor> {
if (_focus.hasFocus != _focused) {
setState(() => _focused = _focus.hasFocus);
}
// Focus given without a click (asked for, or Tab from the field before)
// leaves the editor without a caret, and what is typed goes nowhere: it
// goes to the end of the text then.
if (_focus.hasFocus && _editorState.selection == null) {
if (_lastText(_editorState.document.root) case final node?) {
_editorState.selection = Selection.collapsed(
Position(path: node.path, offset: node.delta!.length),
);
}
}
}
/// The last node under [node] holding text, if any.
static Node? _lastText(Node node) {
for (final child in node.children.reversed) {
if (_lastText(child) case final found?) {
return found;
}
}
return node.delta != null ? node : null;
}
void _onTransaction(EditorTransactionValue value) {