Skip to content

Commit

Permalink
Move Controller outside of configurations data class (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
AtlasAutocode authored Jul 30, 2024
1 parent 4d6380f commit 23fbb43
Show file tree
Hide file tree
Showing 25 changed files with 272 additions and 142 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,18 @@ Instantiate a controller:
QuillController _controller = QuillController.basic();
```

Use the `QuillEditor`, and `QuillToolbar` widgets,
Use the `QuillEditor`, and `QuillSimpleToolbar` widgets,
and attach the `QuillController` to them:

```dart
QuillToolbar.simple(
configurations: QuillSimpleToolbarConfigurations(controller: _controller),
QuillSimpleToolbar(
controller: _controller,
configurations: QuillSimpleToolbarConfigurations(),
),
Expanded(
child: QuillEditor.basic(
configurations: QuillEditorConfigurations(controller: _controller),
controller: _controller,
configurations: const QuillEditorConfigurations(),
),
)
```
Expand Down
2 changes: 1 addition & 1 deletion doc/attribute_introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ On `QuillEditor` or `QuillEditorConfigurations` **doesn't exist** a param that l
##### The editor
```dart
QuillEditor.basic(
controller: controller,
configurations: QuillEditorConfigurations(
controller: controller,
customStyleBuilder: (Attribute<dynamic> attribute) {
if (attribute.key.equals(highlightKey)) {
return TextStyle(color: Colors.black, backgroundColor: Colors.yellow);
Expand Down
1 change: 1 addition & 0 deletions doc/configurations/custom_buttons.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Each `QuillCustomButton` is used as part of the `customButtons` option as follow

```dart
QuillToolbar.simple(
controller: _controller,
configurations: QuillSimpleToolbarConfigurations(
customButtons: [
QuillToolbarCustomButtonOptions(
Expand Down
6 changes: 2 additions & 4 deletions doc/custom_embed_blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ Future<void> _addEditNote(BuildContext context, {Document? document}) async {
],
),
content: QuillEditor.basic(
configurations: const QuillEditorConfigurations(
controller: quillEditorController,
readOnly: false,
),
controller: quillEditorController,
configurations: const QuillEditorConfigurations(),
),
),
);
Expand Down
4 changes: 2 additions & 2 deletions doc/translation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ with:

```dart
QuillToolbar.simple(
controller: _controller,
configurations: QuillSimpleToolbarConfigurations(
controller: _controller,
sharedConfigurations: const QuillSharedConfigurations(
locale: Locale('de'),
),
),
),
Expanded(
child: QuillEditor.basic(
controller: _controller,
configurations: QuillEditorConfigurations(
controller: _controller,
sharedConfigurations: const QuillSharedConfigurations(
locale: Locale('de'),
),
Expand Down
3 changes: 3 additions & 0 deletions example/lib/screens/quill/my_quill_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import 'embeds/timestamp_embed.dart';

class MyQuillEditor extends StatelessWidget {
const MyQuillEditor({
required this.controller,
required this.configurations,
required this.scrollController,
required this.focusNode,
super.key,
});

final QuillController controller;
final QuillEditorConfigurations configurations;
final ScrollController scrollController;
final FocusNode focusNode;
Expand All @@ -34,6 +36,7 @@ class MyQuillEditor extends StatelessWidget {
return QuillEditor(
scrollController: scrollController,
focusNode: focusNode,
controller: controller,
configurations: configurations.copyWith(
elementOptions: const QuillEditorElementOptions(
codeBlock: QuillEditorCodeBlockElementOptions(
Expand Down
6 changes: 5 additions & 1 deletion example/lib/screens/quill/my_quill_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,12 @@ class MyQuillToolbar extends StatelessWidget {
);
}
return QuillToolbar.simple(
controller: controller,

/// configurations parameter:
/// Optional: if not provided will use the configuration set when the controller was instantiated.
/// Override: Provide parameter here to override the default configuration - useful if configuration will change.
configurations: QuillSimpleToolbarConfigurations(
controller: controller,
showAlignmentButtons: true,
multiRowsDisplay: true,
fontFamilyValues: {
Expand Down
3 changes: 2 additions & 1 deletion example/lib/screens/quill/quill_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class QuillScreen extends StatefulWidget {
}

class _QuillScreenState extends State<QuillScreen> {
/// Instantiate the controller
final _controller = QuillController.basic();
final _editorFocusNode = FocusNode();
final _editorScrollController = ScrollController();
Expand Down Expand Up @@ -101,9 +102,9 @@ class _QuillScreenState extends State<QuillScreen> {
builder: (context) {
return Expanded(
child: MyQuillEditor(
controller: _controller,
configurations: QuillEditorConfigurations(
sharedConfigurations: _sharedConfigurations,
controller: _controller,
),
scrollController: _editorScrollController,
focusNode: _editorFocusNode,
Expand Down
10 changes: 5 additions & 5 deletions example/lib/screens/simple/simple_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class _SimpleScreenState extends State<SimpleScreen> {
body: Column(
children: [
QuillToolbar.simple(
configurations:
QuillSimpleToolbarConfigurations(controller: _controller),
controller: _controller,
configurations: const QuillSimpleToolbarConfigurations(),
),
Expanded(
child: QuillEditor.basic(
configurations: QuillEditorConfigurations(
controller: _controller,
padding: const EdgeInsets.all(16),
controller: _controller,
configurations: const QuillEditorConfigurations(
padding: EdgeInsets.all(16),
),
),
),
Expand Down
4 changes: 4 additions & 0 deletions lib/src/controller/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import 'quill_controller.dart';

extension QuillControllerExt on BuildContext {
QuillController? get quilController {
// ignore: deprecated_member_use_from_same_package
return quillSimpleToolbarConfigurations?.controller ??
// ignore: deprecated_member_use_from_same_package
quillEditorConfigurations?.controller;
}

QuillController get requireQuillController {
// ignore: deprecated_member_use_from_same_package
return quillSimpleToolbarConfigurations?.controller ??
// ignore: deprecated_member_use_from_same_package
quillEditorConfigurations?.controller ??
(throw ArgumentError(
'The quill provider is required, you must only call requireQuillController inside the QuillToolbar and QuillEditor'));
Expand Down
18 changes: 15 additions & 3 deletions lib/src/controller/quill_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import '../document/structs/doc_change.dart';
import '../document/style.dart';
import '../editor/config/editor_configurations.dart';
import '../editor_toolbar_controller_shared/clipboard/clipboard_service_provider.dart';
import '../toolbar/config/simple_toolbar_configurations.dart';
import 'quill_controller_configurations.dart';

typedef ReplaceTextCallback = bool Function(int index, int len, Object? data);
Expand Down Expand Up @@ -52,13 +53,24 @@ class QuillController extends ChangeNotifier {

final QuillControllerConfigurations configurations;

/// Local copy of editor configurations enables fail-safe setting from editor _initState method
/// Editor configurations
///
/// Caches configuration set in QuillEditor ctor.
QuillEditorConfigurations? _editorConfigurations;
QuillEditorConfigurations? get editorConfigurations =>
configurations.editorConfigurations ?? _editorConfigurations;
QuillEditorConfigurations get editorConfigurations =>
_editorConfigurations ?? const QuillEditorConfigurations();
set editorConfigurations(QuillEditorConfigurations? value) =>
_editorConfigurations = value;

/// Toolbar configurations
///
/// Caches configuration set in QuillSimpleToolbar ctor.
QuillSimpleToolbarConfigurations? _toolbarConfigurations;
QuillSimpleToolbarConfigurations get toolbarConfigurations =>
_toolbarConfigurations ?? const QuillSimpleToolbarConfigurations();
set toolbarConfigurations(QuillSimpleToolbarConfigurations? value) =>
_toolbarConfigurations = value;

/// Document managed by this controller.
Document _document;

Expand Down
12 changes: 7 additions & 5 deletions lib/src/controller/quill_controller_configurations.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import '../editor/config/editor_configurations.dart';
import '../editor/config/editor_configurations.dart'
show QuillEditorConfigurations;

class QuillControllerConfigurations {
const QuillControllerConfigurations(
{this.editorConfigurations,
{@Deprecated(
'This parameter is not used and will be removed in future versions.')
this.editorConfigurations,
this.onClipboardPaste,
this.requireScriptFontFeatures = false});

/// Provides central access to editor configurations required for controller actions
///
/// Future: will be changed to 'required final'
@Deprecated(
'This parameter is not used and will be removed in future versions.')
final QuillEditorConfigurations? editorConfigurations;

/// Callback when the user pastes and data has not already been processed
Expand Down
14 changes: 10 additions & 4 deletions lib/src/editor/config/editor_configurations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class QuillEditorConfigurations extends Equatable {
/// Important note for the maintainers
/// When editing this class please update the [copyWith] function too.
const QuillEditorConfigurations({
required this.controller,
@Deprecated(
'controller should be passed directly to the editor - this parameter will be removed in future versions.')
this.controller,
this.sharedConfigurations = const QuillSharedConfigurations(),
this.scrollable = true,
this.padding = EdgeInsets.zero,
Expand Down Expand Up @@ -86,7 +88,8 @@ class QuillEditorConfigurations extends Equatable {

final QuillSharedConfigurations sharedConfigurations;

final QuillController controller;
@Deprecated('controller will be removed in future versions.')
final QuillController? controller;

/// The text placeholder in the quill editor
final String? placeholder;
Expand All @@ -97,7 +100,8 @@ class QuillEditorConfigurations extends Equatable {
/// by any shortcut or keyboard operation. The text is still selectable.
///
/// Defaults to `false`. Must not be `null`.
bool get readOnly => controller.readOnly;
// ignore: deprecated_member_use_from_same_package
bool get readOnly => controller?.readOnly != false;

/// Override [readOnly] for checkbox.
///
Expand Down Expand Up @@ -381,7 +385,8 @@ class QuillEditorConfigurations extends Equatable {
@override
List<Object?> get props => [
placeholder,
controller.readOnly,
// ignore: deprecated_member_use_from_same_package
controller?.readOnly,
];

// We might use code generator like freezed but sometimes it can be limited
Expand Down Expand Up @@ -445,6 +450,7 @@ class QuillEditorConfigurations extends Equatable {
}) {
return QuillEditorConfigurations(
sharedConfigurations: sharedConfigurations ?? this.sharedConfigurations,
// ignore: deprecated_member_use_from_same_package
controller: controller ?? this.controller,
placeholder: placeholder ?? this.placeholder,
checkBoxReadOnly: checkBoxReadOnly ?? this.checkBoxReadOnly,
Expand Down
Loading

0 comments on commit 23fbb43

Please sign in to comment.