File tree Expand file tree Collapse file tree 5 files changed +90
-0
lines changed Expand file tree Collapse file tree 5 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -96,6 +96,7 @@ class _DocumentPageState extends State<DocumentPage> {
9696 final theme = Theme .of (context);
9797 final editor = AppFlowyEditor (
9898 editorState: editorState,
99+ autoFocus: editorState.document.isEmpty,
99100 customBuilders: {
100101 'horizontal_rule' : HorizontalRuleWidgetBuilder (),
101102 },
Original file line number Diff line number Diff line change @@ -152,6 +152,7 @@ class _MyHomePageState extends State<MyHomePage> {
152152 editorState: _editorState! ,
153153 themeData: themeData,
154154 editable: true ,
155+ autoFocus: _editorState! .document.isEmpty,
155156 customBuilders: {
156157 'text/code_block' : CodeBlockNodeWidgetBuilder (),
157158 'tex' : TeXBlockNodeWidgetBuidler (),
Original file line number Diff line number Diff line change @@ -110,6 +110,20 @@ class Document {
110110 return true ;
111111 }
112112
113+ bool get isEmpty {
114+ if (root.children.isEmpty) {
115+ return true ;
116+ }
117+
118+ final node = root.children.first;
119+ if (node is TextNode &&
120+ (node.delta.isEmpty || node.delta.toPlainText ().isEmpty)) {
121+ return true ;
122+ }
123+
124+ return false ;
125+ }
126+
113127 Map <String , Object > toJson () {
114128 return {
115129 'document' : root.toJson (),
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ class AppFlowyEditor extends StatefulWidget {
3131 this .shortcutEvents = const [],
3232 this .selectionMenuItems = const [],
3333 this .editable = true ,
34+ this .autoFocus = false ,
3435 ThemeData ? themeData,
3536 }) : super (key: key) {
3637 this .themeData = themeData ??
@@ -54,6 +55,9 @@ class AppFlowyEditor extends StatefulWidget {
5455
5556 final bool editable;
5657
58+ /// Set the value to true to focus the editor on the start of the document.
59+ final bool autoFocus;
60+
5761 @override
5862 State <AppFlowyEditor > createState () => _AppFlowyEditorState ();
5963}
@@ -73,6 +77,15 @@ class _AppFlowyEditorState extends State<AppFlowyEditor> {
7377 editorState.themeData = widget.themeData;
7478 editorState.service.renderPluginService = _createRenderPlugin ();
7579 editorState.editable = widget.editable;
80+
81+ // auto focus
82+ WidgetsBinding .instance.addPostFrameCallback ((timeStamp) {
83+ if (widget.editable && widget.autoFocus) {
84+ editorState.service.selectionService.updateSelection (
85+ Selection .single (path: [0 ], startOffset: 0 ),
86+ );
87+ }
88+ });
7689 }
7790
7891 @override
Original file line number Diff line number Diff line change @@ -73,5 +73,66 @@ void main() async {
7373 final document = Document .fromJson (json);
7474 expect (document.toJson (), json);
7575 });
76+
77+ test ('isEmpty' , () {
78+ expect (
79+ true ,
80+ Document .fromJson ({
81+ 'document' : {
82+ 'type' : 'editor' ,
83+ 'children' : [
84+ {
85+ 'type' : 'text' ,
86+ 'delta' : [],
87+ }
88+ ],
89+ }
90+ }).isEmpty,
91+ );
92+
93+ expect (
94+ true ,
95+ Document .fromJson ({
96+ 'document' : {
97+ 'type' : 'editor' ,
98+ 'children' : [],
99+ }
100+ }).isEmpty,
101+ );
102+
103+ expect (
104+ true ,
105+ Document .fromJson ({
106+ 'document' : {
107+ 'type' : 'editor' ,
108+ 'children' : [
109+ {
110+ 'type' : 'text' ,
111+ 'delta' : [
112+ {'insert' : '' }
113+ ],
114+ }
115+ ],
116+ }
117+ }).isEmpty,
118+ );
119+
120+ expect (
121+ false ,
122+ Document .fromJson ({
123+ 'document' : {
124+ 'type' : 'editor' ,
125+ 'children' : [
126+ {
127+ 'type' : 'text' ,
128+ 'delta' : [
129+ {'insert' : 'Welcome to AppFlowy!' }
130+ ],
131+ }
132+ ],
133+ }
134+ }).isEmpty,
135+ );
136+ });
76137 });
77138}
You can’t perform that action at this time.
0 commit comments