fix: Cannot type on empty document in latest changes

This commit is contained in:
Lucas.Xu 2023-05-31 11:44:37 +08:00
commit 9199d0e94d
3 changed files with 19 additions and 7 deletions

View file

@ -99,7 +99,9 @@ class _HomePageState extends State<HomePage> {
}), }),
_buildListTile(context, 'With Empty Document', () { _buildListTile(context, 'With Empty Document', () {
final jsonString = Future<String>.value( final jsonString = Future<String>.value(
jsonEncode(EditorState.empty().document.toJson()).toString(), jsonEncode(
EditorState.blank(withInitialText: true).document.toJson(),
).toString(),
); );
_loadEditor(context, jsonString); _loadEditor(context, jsonString);
}), }),
@ -108,7 +110,9 @@ class _HomePageState extends State<HomePage> {
_buildSeparator(context, 'Text Robot'), _buildSeparator(context, 'Text Robot'),
_buildListTile(context, 'Type Text Automatically', () async { _buildListTile(context, 'Type Text Automatically', () async {
final jsonString = Future<String>.value( final jsonString = Future<String>.value(
jsonEncode(EditorState.empty().document.toJson()).toString(), jsonEncode(
EditorState.blank(withInitialText: true).document.toJson(),
).toString(),
); );
await _loadEditor(context, jsonString); await _loadEditor(context, jsonString);

View file

@ -1,9 +1,6 @@
import 'dart:collection'; import 'dart:collection';
import 'package:appflowy_editor/src/core/document/attributes.dart'; import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/core/document/node.dart';
import 'package:appflowy_editor/src/core/document/path.dart';
import 'package:appflowy_editor/src/core/document/text_delta.dart';
/// [Document] represents a AppFlowy Editor document structure. /// [Document] represents a AppFlowy Editor document structure.
/// ///
@ -24,6 +21,7 @@ class Document {
} }
/// Creates a empty document with a single text node. /// Creates a empty document with a single text node.
@Deprecated('use Document.blank() instead')
factory Document.empty() { factory Document.empty() {
final root = Node( final root = Node(
type: 'document', type: 'document',
@ -34,9 +32,10 @@ class Document {
); );
} }
factory Document.blank() { factory Document.blank({bool withInitialText = false}) {
final root = Node( final root = Node(
type: 'document', type: 'document',
children: withInitialText ? [paragraphNode()] : [],
); );
return Document( return Document(
root: root, root: root,

View file

@ -58,11 +58,20 @@ class EditorState {
undoManager.state = this; undoManager.state = this;
} }
@Deprecated('use EditorState.blank() instead')
EditorState.empty() EditorState.empty()
: this( : this(
document: Document.blank(), document: Document.blank(),
); );
EditorState.blank({
bool withInitialText = true,
}) : this(
document: Document.blank(
withInitialText: withInitialText,
),
);
final Document document; final Document document;
/// Whether the editor is editable. /// Whether the editor is editable.