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);
}
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<DocChange> get changes => _observer.stream;
Stream<DocChange> 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;
}

@ -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) {

@ -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(

@ -31,6 +31,7 @@ class QuillToolbarSelectHeaderStyleButton extends StatefulWidget {
class _QuillToolbarSelectHeaderStyleButtonState
extends State<QuillToolbarSelectHeaderStyleButton> {
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);
// },
// );
}
}

Loading…
Cancel
Save