diff --git a/frontend/app_flowy/lib/plugins/doc/document_page.dart b/frontend/app_flowy/lib/plugins/doc/document_page.dart index 9d8df7d98a..76ac3f2523 100644 --- a/frontend/app_flowy/lib/plugins/doc/document_page.dart +++ b/frontend/app_flowy/lib/plugins/doc/document_page.dart @@ -96,6 +96,7 @@ class _DocumentPageState extends State { final theme = Theme.of(context); final editor = AppFlowyEditor( editorState: editorState, + autoFocus: editorState.document.isEmpty, customBuilders: { 'horizontal_rule': HorizontalRuleWidgetBuilder(), }, diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/main.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/main.dart index a21d9a8f23..a81b823f37 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/main.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/main.dart @@ -117,6 +117,7 @@ class _MyHomePageState extends State { child: AppFlowyEditor( editorState: _editorState!, editable: true, + autoFocus: _editorState!.document.isEmpty, themeData: _editorThemeData, customBuilders: { 'text/code_block': CodeBlockNodeWidgetBuilder(), diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/core/document/document.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/core/document/document.dart index f085118b70..3cf5b837b9 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/core/document/document.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/core/document/document.dart @@ -110,6 +110,20 @@ class Document { return true; } + bool get isEmpty { + if (root.children.isEmpty) { + return true; + } + + final node = root.children.first; + if (node is TextNode && + (node.delta.isEmpty || node.delta.toPlainText().isEmpty)) { + return true; + } + + return false; + } + Map toJson() { return { 'document': root.toJson(), diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart index af9c9e7380..2180534756 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/editor_service.dart @@ -31,6 +31,7 @@ class AppFlowyEditor extends StatefulWidget { this.shortcutEvents = const [], this.selectionMenuItems = const [], this.editable = true, + this.autoFocus = false, ThemeData? themeData, }) : super(key: key) { this.themeData = themeData ?? @@ -54,6 +55,9 @@ class AppFlowyEditor extends StatefulWidget { final bool editable; + /// Set the value to true to focus the editor on the start of the document. + final bool autoFocus; + @override State createState() => _AppFlowyEditorState(); } @@ -73,6 +77,15 @@ class _AppFlowyEditorState extends State { editorState.themeData = widget.themeData; editorState.service.renderPluginService = _createRenderPlugin(); editorState.editable = widget.editable; + + // auto focus + WidgetsBinding.instance.addPostFrameCallback((timeStamp) { + if (widget.editable && widget.autoFocus) { + editorState.service.selectionService.updateSelection( + Selection.single(path: [0], startOffset: 0), + ); + } + }); } @override diff --git a/frontend/app_flowy/packages/appflowy_editor/test/core/document/document_test.dart b/frontend/app_flowy/packages/appflowy_editor/test/core/document/document_test.dart index a8059d584a..26e3a12c57 100644 --- a/frontend/app_flowy/packages/appflowy_editor/test/core/document/document_test.dart +++ b/frontend/app_flowy/packages/appflowy_editor/test/core/document/document_test.dart @@ -73,5 +73,66 @@ void main() async { final document = Document.fromJson(json); expect(document.toJson(), json); }); + + test('isEmpty', () { + expect( + true, + Document.fromJson({ + 'document': { + 'type': 'editor', + 'children': [ + { + 'type': 'text', + 'delta': [], + } + ], + } + }).isEmpty, + ); + + expect( + true, + Document.fromJson({ + 'document': { + 'type': 'editor', + 'children': [], + } + }).isEmpty, + ); + + expect( + true, + Document.fromJson({ + 'document': { + 'type': 'editor', + 'children': [ + { + 'type': 'text', + 'delta': [ + {'insert': ''} + ], + } + ], + } + }).isEmpty, + ); + + expect( + false, + Document.fromJson({ + 'document': { + 'type': 'editor', + 'children': [ + { + 'type': 'text', + 'delta': [ + {'insert': 'Welcome to AppFlowy!'} + ], + } + ], + } + }).isEmpty, + ); + }); }); }