mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Merge pull request #1397 from LucasXu0/auto_focus
feat: implement auto focus
This commit is contained in:
commit
fa4e3d0d2a
@ -96,6 +96,7 @@ class _DocumentPageState extends State<DocumentPage> {
|
|||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
final editor = AppFlowyEditor(
|
final editor = AppFlowyEditor(
|
||||||
editorState: editorState,
|
editorState: editorState,
|
||||||
|
autoFocus: editorState.document.isEmpty,
|
||||||
customBuilders: {
|
customBuilders: {
|
||||||
'horizontal_rule': HorizontalRuleWidgetBuilder(),
|
'horizontal_rule': HorizontalRuleWidgetBuilder(),
|
||||||
},
|
},
|
||||||
|
@ -117,6 +117,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
child: AppFlowyEditor(
|
child: AppFlowyEditor(
|
||||||
editorState: _editorState!,
|
editorState: _editorState!,
|
||||||
editable: true,
|
editable: true,
|
||||||
|
autoFocus: _editorState!.document.isEmpty,
|
||||||
themeData: _editorThemeData,
|
themeData: _editorThemeData,
|
||||||
customBuilders: {
|
customBuilders: {
|
||||||
'text/code_block': CodeBlockNodeWidgetBuilder(),
|
'text/code_block': CodeBlockNodeWidgetBuilder(),
|
||||||
|
@ -110,6 +110,20 @@ class Document {
|
|||||||
return true;
|
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<String, Object> toJson() {
|
Map<String, Object> toJson() {
|
||||||
return {
|
return {
|
||||||
'document': root.toJson(),
|
'document': root.toJson(),
|
||||||
|
@ -31,6 +31,7 @@ class AppFlowyEditor extends StatefulWidget {
|
|||||||
this.shortcutEvents = const [],
|
this.shortcutEvents = const [],
|
||||||
this.selectionMenuItems = const [],
|
this.selectionMenuItems = const [],
|
||||||
this.editable = true,
|
this.editable = true,
|
||||||
|
this.autoFocus = false,
|
||||||
ThemeData? themeData,
|
ThemeData? themeData,
|
||||||
}) : super(key: key) {
|
}) : super(key: key) {
|
||||||
this.themeData = themeData ??
|
this.themeData = themeData ??
|
||||||
@ -54,6 +55,9 @@ class AppFlowyEditor extends StatefulWidget {
|
|||||||
|
|
||||||
final bool editable;
|
final bool editable;
|
||||||
|
|
||||||
|
/// Set the value to true to focus the editor on the start of the document.
|
||||||
|
final bool autoFocus;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<AppFlowyEditor> createState() => _AppFlowyEditorState();
|
State<AppFlowyEditor> createState() => _AppFlowyEditorState();
|
||||||
}
|
}
|
||||||
@ -73,6 +77,15 @@ class _AppFlowyEditorState extends State<AppFlowyEditor> {
|
|||||||
editorState.themeData = widget.themeData;
|
editorState.themeData = widget.themeData;
|
||||||
editorState.service.renderPluginService = _createRenderPlugin();
|
editorState.service.renderPluginService = _createRenderPlugin();
|
||||||
editorState.editable = widget.editable;
|
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
|
@override
|
||||||
|
@ -73,5 +73,66 @@ void main() async {
|
|||||||
final document = Document.fromJson(json);
|
final document = Document.fromJson(json);
|
||||||
expect(document.toJson(), 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,
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user