From 8b2f62e110b8e5f11453fdbab49f30c43fa1252b Mon Sep 17 00:00:00 2001 From: Ellet Date: Sat, 9 Dec 2023 14:47:37 +0300 Subject: [PATCH] Update select header style button, raw editor state and controller --- lib/src/models/documents/document.dart | 28 +++++++----- lib/src/widgets/quill/quill_controller.dart | 15 ++++++- .../widgets/raw_editor/raw_editor_state.dart | 4 +- .../buttons/select_header_style_button.dart | 45 ++++++++++++++----- 4 files changed, 65 insertions(+), 27 deletions(-) diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart index 573633da..aa0495a2 100644 --- a/lib/src/models/documents/document.dart +++ b/lib/src/models/documents/document.dart @@ -53,12 +53,13 @@ class Document { _rules.setCustomRules(customRules); } - final StreamController _observer = StreamController.broadcast(); + final StreamController documentChangeObserver = + StreamController.broadcast(); - final History _history = History(); + final History history = History(); /// Stream of [DocChange]s applied to this document. - Stream get changes => _observer.stream; + Stream get changes => documentChangeObserver.stream; /// Inserts [data] in this document at specified [index]. /// @@ -285,7 +286,7 @@ class Document { /// /// In case the [change] is invalid, behavior of this method is unspecified. void compose(Delta delta, ChangeSource changeSource) { - assert(!_observer.isClosed); + assert(!documentChangeObserver.isClosed); delta.trim(); assert(delta.isNotEmpty); @@ -320,21 +321,21 @@ class Document { throw StateError('Compose failed'); } final change = DocChange(originalDelta, delta, changeSource); - _observer.add(change); - _history.handleDocChange(change); + documentChangeObserver.add(change); + history.handleDocChange(change); } HistoryChanged undo() { - return _history.undo(this); + return history.undo(this); } HistoryChanged redo() { - return _history.redo(this); + return history.redo(this); } - bool get hasUndo => _history.hasUndo; + bool get hasUndo => history.hasUndo; - bool get hasRedo => _history.hasRedo; + bool get hasRedo => history.hasRedo; static Delta _transform(Delta delta) { final res = Delta(); @@ -384,8 +385,8 @@ class Document { } void close() { - _observer.close(); - _history.clear(); + documentChangeObserver.close(); + history.clear(); } /// Returns plain text representation of this document. @@ -450,4 +451,7 @@ enum ChangeSource { /// Change originated from a remote action. remote, + + /// Silent change. + silent; } diff --git a/lib/src/widgets/quill/quill_controller.dart b/lib/src/widgets/quill/quill_controller.dart index e6fa0df6..50fe5a5b 100644 --- a/lib/src/widgets/quill/quill_controller.dart +++ b/lib/src/widgets/quill/quill_controller.dart @@ -54,13 +54,24 @@ class QuillController extends ChangeNotifier { notifyListeners(); } - void updateDocument(Document newDocument) { + void updateContents( + Delta delta, { + ChangeSource changeSource = ChangeSource.local, + }) { + final newDocument = Document.fromDelta(delta); + + final change = DocChange(_document.toDelta(), delta, changeSource); + newDocument.documentChangeObserver.add(change); + newDocument.history.handleDocChange(change); + _document = newDocument; notifyListeners(); } /// The current font family, null to use the default one String? _selectedFontFamily; + + /// The current font family, null to use the default one String? get selectedFontFamily => _selectedFontFamily; void selectFontFamily(String? newFontFamily) { @@ -69,6 +80,8 @@ class QuillController extends ChangeNotifier { /// The current font size, null to use the default one String? _selectedFontSize; + + /// The current font size, null to use the default one String? get selectedFontSize => _selectedFontSize; void selectFontSize(String? newFontSize) { diff --git a/lib/src/widgets/raw_editor/raw_editor_state.dart b/lib/src/widgets/raw_editor/raw_editor_state.dart index 84af3316..49602dae 100644 --- a/lib/src/widgets/raw_editor/raw_editor_state.dart +++ b/lib/src/widgets/raw_editor/raw_editor_state.dart @@ -217,8 +217,8 @@ class QuillRawEditorState extends EditorState final delta = deltaFromCliboard.compose(controller.document.toDelta()); controller - ..updateDocument( - Document.fromDelta(delta), + ..updateContents( + delta, ) ..updateSelection( TextSelection.collapsed( diff --git a/lib/src/widgets/toolbar/buttons/select_header_style_button.dart b/lib/src/widgets/toolbar/buttons/select_header_style_button.dart index da04b84a..1e8621c8 100644 --- a/lib/src/widgets/toolbar/buttons/select_header_style_button.dart +++ b/lib/src/widgets/toolbar/buttons/select_header_style_button.dart @@ -31,6 +31,7 @@ class QuillToolbarSelectHeaderStyleButton extends StatefulWidget { class _QuillToolbarSelectHeaderStyleButtonState extends State { var _selectedItem = _HeaderStyleOptions.normal; + final _controller = MenuController(); @override void initState() { super.initState(); @@ -110,25 +111,45 @@ class _QuillToolbarSelectHeaderStyleButtonState @override Widget build(BuildContext context) { - return DropdownButton<_HeaderStyleOptions>( - value: _selectedItem, - items: _HeaderStyleOptions.values + return MenuAnchor( + controller: _controller, + menuChildren: _HeaderStyleOptions.values .map( - (e) => DropdownMenuItem<_HeaderStyleOptions>( - value: e, + (e) => MenuItemButton( + // value: e, child: Text(_label(e)), - onTap: () { + onPressed: () { widget.controller.formatSelection(getAttributeByOptionsItem(e)); }, ), ) .toList(), - onChanged: (newItem) { - if (newItem == null) { - return; - } - setState(() => _selectedItem = newItem); - }, + child: IconButton( + onPressed: () { + if (_controller.isOpen) { + _controller.close(); + return; + } + _controller.open(); + }, + icon: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Text(_label(_selectedItem)), + const Icon(Icons.arrow_drop_down), + ], + ), + ), ); + // return DropdownButton<_HeaderStyleOptions>( + // value: _selectedItem, + // items: , + // onChanged: (newItem) { + // if (newItem == null) { + // return; + // } + // setState(() => _selectedItem = newItem); + // }, + // ); } }