Update select header style button, raw editor state and controller

pull/1582/head
Ellet 1 year ago
parent 38b1621d21
commit 8b2f62e110
No known key found for this signature in database
GPG Key ID: C488CC70BBCEF0D1
  1. 28
      lib/src/models/documents/document.dart
  2. 15
      lib/src/widgets/quill/quill_controller.dart
  3. 4
      lib/src/widgets/raw_editor/raw_editor_state.dart
  4. 45
      lib/src/widgets/toolbar/buttons/select_header_style_button.dart

@ -53,12 +53,13 @@ class Document {
_rules.setCustomRules(customRules); _rules.setCustomRules(customRules);
} }
final StreamController<DocChange> _observer = StreamController.broadcast(); final StreamController<DocChange> documentChangeObserver =
StreamController.broadcast();
final History _history = History(); final History history = History();
/// Stream of [DocChange]s applied to this document. /// Stream of [DocChange]s applied to this document.
Stream<DocChange> get changes => _observer.stream; Stream<DocChange> get changes => documentChangeObserver.stream;
/// Inserts [data] in this document at specified [index]. /// 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. /// In case the [change] is invalid, behavior of this method is unspecified.
void compose(Delta delta, ChangeSource changeSource) { void compose(Delta delta, ChangeSource changeSource) {
assert(!_observer.isClosed); assert(!documentChangeObserver.isClosed);
delta.trim(); delta.trim();
assert(delta.isNotEmpty); assert(delta.isNotEmpty);
@ -320,21 +321,21 @@ class Document {
throw StateError('Compose failed'); throw StateError('Compose failed');
} }
final change = DocChange(originalDelta, delta, changeSource); final change = DocChange(originalDelta, delta, changeSource);
_observer.add(change); documentChangeObserver.add(change);
_history.handleDocChange(change); history.handleDocChange(change);
} }
HistoryChanged undo() { HistoryChanged undo() {
return _history.undo(this); return history.undo(this);
} }
HistoryChanged redo() { 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) { static Delta _transform(Delta delta) {
final res = Delta(); final res = Delta();
@ -384,8 +385,8 @@ class Document {
} }
void close() { void close() {
_observer.close(); documentChangeObserver.close();
_history.clear(); history.clear();
} }
/// Returns plain text representation of this document. /// Returns plain text representation of this document.
@ -450,4 +451,7 @@ enum ChangeSource {
/// Change originated from a remote action. /// Change originated from a remote action.
remote, remote,
/// Silent change.
silent;
} }

@ -54,13 +54,24 @@ class QuillController extends ChangeNotifier {
notifyListeners(); 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; _document = newDocument;
notifyListeners(); notifyListeners();
} }
/// The current font family, null to use the default one /// The current font family, null to use the default one
String? _selectedFontFamily; String? _selectedFontFamily;
/// The current font family, null to use the default one
String? get selectedFontFamily => _selectedFontFamily; String? get selectedFontFamily => _selectedFontFamily;
void selectFontFamily(String? newFontFamily) { void selectFontFamily(String? newFontFamily) {
@ -69,6 +80,8 @@ class QuillController extends ChangeNotifier {
/// The current font size, null to use the default one /// The current font size, null to use the default one
String? _selectedFontSize; String? _selectedFontSize;
/// The current font size, null to use the default one
String? get selectedFontSize => _selectedFontSize; String? get selectedFontSize => _selectedFontSize;
void selectFontSize(String? newFontSize) { void selectFontSize(String? newFontSize) {

@ -217,8 +217,8 @@ class QuillRawEditorState extends EditorState
final delta = deltaFromCliboard.compose(controller.document.toDelta()); final delta = deltaFromCliboard.compose(controller.document.toDelta());
controller controller
..updateDocument( ..updateContents(
Document.fromDelta(delta), delta,
) )
..updateSelection( ..updateSelection(
TextSelection.collapsed( TextSelection.collapsed(

@ -31,6 +31,7 @@ class QuillToolbarSelectHeaderStyleButton extends StatefulWidget {
class _QuillToolbarSelectHeaderStyleButtonState class _QuillToolbarSelectHeaderStyleButtonState
extends State<QuillToolbarSelectHeaderStyleButton> { extends State<QuillToolbarSelectHeaderStyleButton> {
var _selectedItem = _HeaderStyleOptions.normal; var _selectedItem = _HeaderStyleOptions.normal;
final _controller = MenuController();
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -110,25 +111,45 @@ class _QuillToolbarSelectHeaderStyleButtonState
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return DropdownButton<_HeaderStyleOptions>( return MenuAnchor(
value: _selectedItem, controller: _controller,
items: _HeaderStyleOptions.values menuChildren: _HeaderStyleOptions.values
.map( .map(
(e) => DropdownMenuItem<_HeaderStyleOptions>( (e) => MenuItemButton(
value: e, // value: e,
child: Text(_label(e)), child: Text(_label(e)),
onTap: () { onPressed: () {
widget.controller.formatSelection(getAttributeByOptionsItem(e)); widget.controller.formatSelection(getAttributeByOptionsItem(e));
}, },
), ),
) )
.toList(), .toList(),
onChanged: (newItem) { child: IconButton(
if (newItem == null) { onPressed: () {
return; if (_controller.isOpen) {
} _controller.close();
setState(() => _selectedItem = newItem); 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);
// },
// );
} }
} }

Loading…
Cancel
Save