Skip to content

Commit

Permalink
fix: editor no longer resets focus when page rebuilds
Browse files Browse the repository at this point in the history
  • Loading branch information
maelchiotti committed Apr 9, 2024
1 parent deadbd6 commit 9a86407
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/pages/editor/editor_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EditorPage extends ConsumerStatefulWidget {

class _EditorState extends ConsumerState<EditorPage> {
final titleController = TextEditingController();
late FleatherController fleatherController;
FleatherController? fleatherController;

void _synchronizeTitle(Note note, String? newTitle) {
if (newTitle == null) {
Expand All @@ -39,7 +39,11 @@ class _EditorState extends ConsumerState<EditorPage> {
}

void _synchronizeContent(Note note) {
note.content = jsonEncode(fleatherController.document.toDelta().toJson());
if (fleatherController == null) {
return;
}

note.content = jsonEncode(fleatherController!.document.toDelta().toJson());
ref.read(notesProvider.notifier).edit(note);
}

Expand All @@ -60,12 +64,15 @@ class _EditorState extends ConsumerState<EditorPage> {
}

titleController.text = note.title;
fleatherController = FleatherController(document: note.document);
fleatherController.addListener(() => _synchronizeContent(note));

Future(() {
ref.read(editorControllerProvider.notifier).set(fleatherController);
});
if (fleatherController == null) {
fleatherController = FleatherController(document: note.document);
fleatherController!.addListener(() => _synchronizeContent(note));

Future(() {
ref.read(editorControllerProvider.notifier).set(fleatherController!);
});
}

return Padding(
padding: Paddings.custom.pageButBottom,
Expand All @@ -85,7 +92,7 @@ class _EditorState extends ConsumerState<EditorPage> {
Padding(padding: Paddings.padding8.vertical),
Expanded(
child: FleatherField(
controller: fleatherController,
controller: fleatherController!,
autofocus: widget._autofocus,
readOnly: widget._readOnly,
expands: true,
Expand Down

0 comments on commit 9a86407

Please sign in to comment.