From a3ee421d1aab53a676d19fb31efb78a3eb5aa98e Mon Sep 17 00:00:00 2001 From: singerdmx Date: Sat, 20 Feb 2021 01:49:47 -0800 Subject: [PATCH] Add isEmpty method for Document class --- lib/models/documents/document.dart | 14 ++++++++ lib/widgets/raw_editor.dart | 55 +++++++++++++++++------------- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/lib/models/documents/document.dart b/lib/models/documents/document.dart index bcfd5152..93585897 100644 --- a/lib/models/documents/document.dart +++ b/lib/models/documents/document.dart @@ -237,6 +237,20 @@ class Document { _root.remove(node); } } + + bool isEmpty() { + if (root.children.length != 1) { + return false; + } + + final Node node = root.children.first; + if (!node.isLast) { + return false; + } + + Delta delta = node.toDelta(); + return delta.length == 1 && delta.first.data == '\n'; + } } enum ChangeSource { diff --git a/lib/widgets/raw_editor.dart b/lib/widgets/raw_editor.dart index 6ae948ff..891bff86 100644 --- a/lib/widgets/raw_editor.dart +++ b/lib/widgets/raw_editor.dart @@ -503,13 +503,15 @@ class RawEditorState extends EditorState _focusAttachment.reparent(); super.build(context); + Document _doc = widget.controller.document; + Widget child = CompositedTransformTarget( link: _toolbarLayerLink, child: Semantics( child: _Editor( key: _editorKey, - children: _buildChildren(context), - document: widget.controller.document, + children: _buildChildren(_doc, context), + document: _doc, selection: widget.controller.selection, hasFocus: _hasFocus, textDirection: _textDirection, @@ -562,30 +564,13 @@ class RawEditorState extends EditorState requestKeyboard(); } - _buildChildren(BuildContext context) { + _buildChildren(Document doc, BuildContext context) { final result = []; Map indentLevelCounts = {}; - for (Node node in widget.controller.document.root.children) { + for (Node node in doc.root.children) { if (node is Line) { - TextLine textLine = TextLine( - line: node, - textDirection: _textDirection, - embedBuilder: widget.embedBuilder, - styles: _styles, - ); - EditableTextLine editableTextLine = EditableTextLine( - node, - null, - textLine, - 0, - _getVerticalSpacingForLine(node, _styles), - _textDirection, - widget.controller.selection, - widget.selectionColor, - widget.enableInteractiveSelection, - _hasFocus, - MediaQuery.of(context).devicePixelRatio, - _cursorCont); + EditableTextLine editableTextLine = + _getEditableTextLineFromNode(node, context); result.add(editableTextLine); } else if (node is Block) { Map attrs = node.style.attributes; @@ -612,6 +597,30 @@ class RawEditorState extends EditorState return result; } + EditableTextLine _getEditableTextLineFromNode( + Line node, BuildContext context) { + TextLine textLine = TextLine( + line: node, + textDirection: _textDirection, + embedBuilder: widget.embedBuilder, + styles: _styles, + ); + EditableTextLine editableTextLine = EditableTextLine( + node, + null, + textLine, + 0, + _getVerticalSpacingForLine(node, _styles), + _textDirection, + widget.controller.selection, + widget.selectionColor, + widget.enableInteractiveSelection, + _hasFocus, + MediaQuery.of(context).devicePixelRatio, + _cursorCont); + return editableTextLine; + } + Tuple2 _getVerticalSpacingForLine( Line line, DefaultStyles defaultStyles) { Map attrs = line.style.attributes;