From df602cfa9b762d9c0248c081dbd5ddf308f7805f Mon Sep 17 00:00:00 2001 From: Xin Yao Date: Thu, 22 Apr 2021 21:50:50 -0700 Subject: [PATCH] Format code --- lib/models/documents/nodes/line.dart | 2 +- lib/models/documents/style.dart | 8 +- lib/widgets/controller.dart | 9 +- lib/widgets/editor.dart | 32 ++- lib/widgets/raw_editor.dart | 6 +- lib/widgets/toolbar.dart | 377 ++++++++++++++------------- 6 files changed, 236 insertions(+), 198 deletions(-) diff --git a/lib/models/documents/nodes/line.dart b/lib/models/documents/nodes/line.dart index 632d44d1..ec933b52 100644 --- a/lib/models/documents/nodes/line.dart +++ b/lib/models/documents/nodes/line.dart @@ -197,7 +197,7 @@ class Line extends Container { } applyStyle(newStyle); - final blockStyle = newStyle.getNotNullValueBlockExceptHeader(); + final blockStyle = newStyle.getBlockExceptHeader(); if (blockStyle == null) { return; } // No block-level changes diff --git a/lib/models/documents/style.dart b/lib/models/documents/style.dart index 4efce0f2..ff78d9ed 100644 --- a/lib/models/documents/style.dart +++ b/lib/models/documents/style.dart @@ -49,16 +49,12 @@ class Style { Attribute? getBlockExceptHeader() { for (final val in values) { - if (val.isBlockExceptHeader) { + if (val.isBlockExceptHeader && val.value != null) { return val; } } - return null; - } - - Attribute? getNotNullValueBlockExceptHeader() { for (final val in values) { - if (val.isBlockExceptHeader && val.value != null) { + if (val.isBlockExceptHeader) { return val; } } diff --git a/lib/widgets/controller.dart b/lib/widgets/controller.dart index 1b7a8180..59aa1e09 100644 --- a/lib/widgets/controller.dart +++ b/lib/widgets/controller.dart @@ -11,7 +11,11 @@ import '../models/quill_delta.dart'; import '../utils/diff_delta.dart'; class QuillController extends ChangeNotifier { - QuillController({required this.document, required this.selection, this.iconSize = 18, this.toolbarHeightFactor = 2}); + QuillController( + {required this.document, + required this.selection, + this.iconSize = 18, + this.toolbarHeightFactor = 2}); factory QuillController.basic() { return QuillController( @@ -80,7 +84,8 @@ class QuillController extends ChangeNotifier { bool get hasRedo => document.hasRedo; void replaceText( - int index, int len, Object? data, TextSelection? textSelection, {bool ignoreFocus = false}) { + int index, int len, Object? data, TextSelection? textSelection, + {bool ignoreFocus = false}) { assert(data is String || data is Embeddable); Delta? delta; diff --git a/lib/widgets/editor.dart b/lib/widgets/editor.dart index 42fd769c..d15a8789 100644 --- a/lib/widgets/editor.dart +++ b/lib/widgets/editor.dart @@ -176,18 +176,25 @@ class QuillEditor extends StatefulWidget { final ScrollPhysics? scrollPhysics; final ValueChanged? onLaunchUrl; // Returns whether gesture is handled - final bool Function(TapDownDetails details, TextPosition Function(Offset offset))? onTapDown; + final bool Function( + TapDownDetails details, TextPosition Function(Offset offset))? onTapDown; // Returns whether gesture is handled - final bool Function(TapUpDetails details, TextPosition Function(Offset offset))? onTapUp; + final bool Function( + TapUpDetails details, TextPosition Function(Offset offset))? onTapUp; // Returns whether gesture is handled - final bool Function(LongPressStartDetails details, TextPosition Function(Offset offset))? onSingleLongTapStart; + final bool Function( + LongPressStartDetails details, TextPosition Function(Offset offset))? + onSingleLongTapStart; // Returns whether gesture is handled - final bool Function(LongPressMoveUpdateDetails details, TextPosition Function(Offset offset))? onSingleLongTapMoveUpdate; + final bool Function(LongPressMoveUpdateDetails details, + TextPosition Function(Offset offset))? onSingleLongTapMoveUpdate; // Returns whether gesture is handled - final bool Function(LongPressEndDetails details, TextPosition Function(Offset offset))? onSingleLongTapEnd; + final bool Function( + LongPressEndDetails details, TextPosition Function(Offset offset))? + onSingleLongTapEnd; final EmbedBuilder embedBuilder; @@ -339,7 +346,8 @@ class _QuillEditorSelectionGestureDetectorBuilder if (_state.widget.onSingleLongTapMoveUpdate != null) { final renderEditor = getRenderEditor(); if (renderEditor != null) { - if (_state.widget.onSingleLongTapMoveUpdate!(details, renderEditor.getPositionForOffset)) { + if (_state.widget.onSingleLongTapMoveUpdate!( + details, renderEditor.getPositionForOffset)) { return; } } @@ -468,7 +476,8 @@ class _QuillEditorSelectionGestureDetectorBuilder if (_state.widget.onTapDown != null) { final renderEditor = getRenderEditor(); if (renderEditor != null) { - if (_state.widget.onTapDown!(details, renderEditor.getPositionForOffset)) { + if (_state.widget.onTapDown!( + details, renderEditor.getPositionForOffset)) { return; } } @@ -481,7 +490,8 @@ class _QuillEditorSelectionGestureDetectorBuilder if (_state.widget.onTapUp != null) { final renderEditor = getRenderEditor(); if (renderEditor != null) { - if (_state.widget.onTapUp!(details, renderEditor.getPositionForOffset)) { + if (_state.widget.onTapUp!( + details, renderEditor.getPositionForOffset)) { return; } } @@ -523,7 +533,8 @@ class _QuillEditorSelectionGestureDetectorBuilder if (_state.widget.onSingleLongTapStart != null) { final renderEditor = getRenderEditor(); if (renderEditor != null) { - if (_state.widget.onSingleLongTapStart!(details, renderEditor.getPositionForOffset)) { + if (_state.widget.onSingleLongTapStart!( + details, renderEditor.getPositionForOffset)) { return; } } @@ -557,7 +568,8 @@ class _QuillEditorSelectionGestureDetectorBuilder if (_state.widget.onSingleLongTapEnd != null) { final renderEditor = getRenderEditor(); if (renderEditor != null) { - if (_state.widget.onSingleLongTapEnd!(details, renderEditor.getPositionForOffset)) { + if (_state.widget.onSingleLongTapEnd!( + details, renderEditor.getPositionForOffset)) { return; } } diff --git a/lib/widgets/raw_editor.dart b/lib/widgets/raw_editor.dart index 7114e670..35e3aa68 100644 --- a/lib/widgets/raw_editor.dart +++ b/lib/widgets/raw_editor.dart @@ -913,14 +913,16 @@ class RawEditorState extends EditorState return; } _showCaretOnScreen(); - _cursorCont.startOrStopCursorTimerIfNeeded(_hasFocus, widget.controller.selection); + _cursorCont.startOrStopCursorTimerIfNeeded( + _hasFocus, widget.controller.selection); if (hasConnection) { _cursorCont ..stopCursorTimer(resetCharTicks: false) ..startCursorTimer(); } - SchedulerBinding.instance!.addPostFrameCallback((_) => _updateOrDisposeSelectionOverlayIfNeeded()); + SchedulerBinding.instance!.addPostFrameCallback( + (_) => _updateOrDisposeSelectionOverlayIfNeeded()); if (mounted) { setState(() { // Use widget.controller.value in build() diff --git a/lib/widgets/toolbar.dart b/lib/widgets/toolbar.dart index 8bcb3329..c5fe2bab 100644 --- a/lib/widgets/toolbar.dart +++ b/lib/widgets/toolbar.dart @@ -432,7 +432,8 @@ class _SelectHeaderStyleButtonState extends State { @override Widget build(BuildContext context) { - return _selectHeadingStyleButtonBuilder(context, _value, _selectAttribute, widget.controller.iconSize); + return _selectHeadingStyleButtonBuilder( + context, _value, _selectAttribute, widget.controller.iconSize); } } @@ -774,7 +775,8 @@ class _HistoryButtonState extends State { highlightElevation: 0, hoverElevation: 0, size: widget.controller.iconSize * 1.77, - icon: Icon(widget.icon, size: widget.controller.iconSize, color: _iconColor), + icon: Icon(widget.icon, + size: widget.controller.iconSize, color: _iconColor), fillColor: fillColor, onPressed: _changeHistory, ); @@ -839,7 +841,8 @@ class _IndentButtonState extends State { highlightElevation: 0, hoverElevation: 0, size: widget.controller.iconSize * 1.77, - icon: Icon(widget.icon, size: widget.controller.iconSize, color: iconColor), + icon: + Icon(widget.icon, size: widget.controller.iconSize, color: iconColor), fillColor: fillColor, onPressed: () { final indent = widget.controller @@ -893,7 +896,8 @@ class _ClearFormatButtonState extends State { highlightElevation: 0, hoverElevation: 0, size: widget.controller.iconSize * 1.77, - icon: Icon(widget.icon, size: widget.controller.iconSize, color: iconColor), + icon: Icon(widget.icon, + size: widget.controller.iconSize, color: iconColor), fillColor: fillColor, onPressed: () { for (final k @@ -905,7 +909,9 @@ class _ClearFormatButtonState extends State { } class QuillToolbar extends StatefulWidget implements PreferredSizeWidget { - const QuillToolbar({required this.children, this.toolBarHeight = 36, Key? key}) : super(key: key); + const QuillToolbar( + {required this.children, this.toolBarHeight = 36, Key? key}) + : super(key: key); factory QuillToolbar.basic({ required QuillController controller, @@ -932,178 +938,195 @@ class QuillToolbar extends StatefulWidget implements PreferredSizeWidget { }) { controller.iconSize = toolbarIconSize; - return QuillToolbar(key: key, toolBarHeight: toolbarIconSize * controller.toolbarHeightFactor, children: [ - Visibility( - visible: showHistory, - child: HistoryButton( - icon: Icons.undo_outlined, - controller: controller, - undo: true, - ), - ), - Visibility( - visible: showHistory, - child: HistoryButton( - icon: Icons.redo_outlined, - controller: controller, - undo: false, - ), - ), - const SizedBox(width: 0.6), - Visibility( - visible: showBoldButton, - child: ToggleStyleButton( - attribute: Attribute.bold, - icon: Icons.format_bold, - controller: controller, - ), - ), - const SizedBox(width: 0.6), - Visibility( - visible: showItalicButton, - child: ToggleStyleButton( - attribute: Attribute.italic, - icon: Icons.format_italic, - controller: controller, - ), - ), - const SizedBox(width: 0.6), - Visibility( - visible: showUnderLineButton, - child: ToggleStyleButton( - attribute: Attribute.underline, - icon: Icons.format_underline, - controller: controller, - ), - ), - const SizedBox(width: 0.6), - Visibility( - visible: showStrikeThrough, - child: ToggleStyleButton( - attribute: Attribute.strikeThrough, - icon: Icons.format_strikethrough, - controller: controller, - ), - ), - const SizedBox(width: 0.6), - Visibility( - visible: showColorButton, - child: ColorButton( - icon: Icons.color_lens, - controller: controller, - background: false, - ), - ), - const SizedBox(width: 0.6), - Visibility( - visible: showBackgroundColorButton, - child: ColorButton( - icon: Icons.format_color_fill, - controller: controller, - background: true, - ), - ), - const SizedBox(width: 0.6), - Visibility( - visible: showClearFormat, - child: ClearFormatButton( - icon: Icons.format_clear, - controller: controller, - ), - ), - const SizedBox(width: 0.6), - Visibility( - visible: onImagePickCallback != null, - child: ImageButton( - icon: Icons.image, - controller: controller, - imageSource: ImageSource.gallery, - onImagePickCallback: onImagePickCallback, - ), - ), - const SizedBox(width: 0.6), - Visibility( - visible: onImagePickCallback != null, - child: ImageButton( - icon: Icons.photo_camera, - controller: controller, - imageSource: ImageSource.camera, - onImagePickCallback: onImagePickCallback, - ), - ), - Visibility( - visible: showHeaderStyle, child: VerticalDivider(indent: 12, endIndent: 12, color: Colors.grey.shade400)), - Visibility(visible: showHeaderStyle, child: SelectHeaderStyleButton(controller: controller)), - VerticalDivider(indent: 12, endIndent: 12, color: Colors.grey.shade400), - Visibility( - visible: showListNumbers, - child: ToggleStyleButton( - attribute: Attribute.ol, - controller: controller, - icon: Icons.format_list_numbered, - ), - ), - Visibility( - visible: showListBullets, - child: ToggleStyleButton( - attribute: Attribute.ul, - controller: controller, - icon: Icons.format_list_bulleted, - ), - ), - Visibility( - visible: showListCheck, - child: ToggleCheckListButton( - attribute: Attribute.unchecked, - controller: controller, - icon: Icons.check_box, - ), - ), - Visibility( - visible: showCodeBlock, - child: ToggleStyleButton( - attribute: Attribute.codeBlock, - controller: controller, - icon: Icons.code, - ), - ), - Visibility( - visible: !showListNumbers && !showListBullets && !showListCheck && !showCodeBlock, - child: VerticalDivider(indent: 12, endIndent: 12, color: Colors.grey.shade400)), - Visibility( - visible: showQuote, - child: ToggleStyleButton( - attribute: Attribute.blockQuote, - controller: controller, - icon: Icons.format_quote, - ), - ), - Visibility( - visible: showIndent, - child: IndentButton( - icon: Icons.format_indent_increase, - controller: controller, - isIncrease: true, - ), - ), - Visibility( - visible: showIndent, - child: IndentButton( - icon: Icons.format_indent_decrease, - controller: controller, - isIncrease: false, - ), - ), - Visibility(visible: showQuote, child: VerticalDivider(indent: 12, endIndent: 12, color: Colors.grey.shade400)), - Visibility(visible: showLink, child: LinkStyleButton(controller: controller)), - Visibility( - visible: showHorizontalRule, - child: InsertEmbedButton( - controller: controller, - icon: Icons.horizontal_rule, - ), - ), - ]); + return QuillToolbar( + key: key, + toolBarHeight: toolbarIconSize * controller.toolbarHeightFactor, + children: [ + Visibility( + visible: showHistory, + child: HistoryButton( + icon: Icons.undo_outlined, + controller: controller, + undo: true, + ), + ), + Visibility( + visible: showHistory, + child: HistoryButton( + icon: Icons.redo_outlined, + controller: controller, + undo: false, + ), + ), + const SizedBox(width: 0.6), + Visibility( + visible: showBoldButton, + child: ToggleStyleButton( + attribute: Attribute.bold, + icon: Icons.format_bold, + controller: controller, + ), + ), + const SizedBox(width: 0.6), + Visibility( + visible: showItalicButton, + child: ToggleStyleButton( + attribute: Attribute.italic, + icon: Icons.format_italic, + controller: controller, + ), + ), + const SizedBox(width: 0.6), + Visibility( + visible: showUnderLineButton, + child: ToggleStyleButton( + attribute: Attribute.underline, + icon: Icons.format_underline, + controller: controller, + ), + ), + const SizedBox(width: 0.6), + Visibility( + visible: showStrikeThrough, + child: ToggleStyleButton( + attribute: Attribute.strikeThrough, + icon: Icons.format_strikethrough, + controller: controller, + ), + ), + const SizedBox(width: 0.6), + Visibility( + visible: showColorButton, + child: ColorButton( + icon: Icons.color_lens, + controller: controller, + background: false, + ), + ), + const SizedBox(width: 0.6), + Visibility( + visible: showBackgroundColorButton, + child: ColorButton( + icon: Icons.format_color_fill, + controller: controller, + background: true, + ), + ), + const SizedBox(width: 0.6), + Visibility( + visible: showClearFormat, + child: ClearFormatButton( + icon: Icons.format_clear, + controller: controller, + ), + ), + const SizedBox(width: 0.6), + Visibility( + visible: onImagePickCallback != null, + child: ImageButton( + icon: Icons.image, + controller: controller, + imageSource: ImageSource.gallery, + onImagePickCallback: onImagePickCallback, + ), + ), + const SizedBox(width: 0.6), + Visibility( + visible: onImagePickCallback != null, + child: ImageButton( + icon: Icons.photo_camera, + controller: controller, + imageSource: ImageSource.camera, + onImagePickCallback: onImagePickCallback, + ), + ), + Visibility( + visible: showHeaderStyle, + child: VerticalDivider( + indent: 12, endIndent: 12, color: Colors.grey.shade400)), + Visibility( + visible: showHeaderStyle, + child: SelectHeaderStyleButton(controller: controller)), + VerticalDivider( + indent: 12, endIndent: 12, color: Colors.grey.shade400), + Visibility( + visible: showListNumbers, + child: ToggleStyleButton( + attribute: Attribute.ol, + controller: controller, + icon: Icons.format_list_numbered, + ), + ), + Visibility( + visible: showListBullets, + child: ToggleStyleButton( + attribute: Attribute.ul, + controller: controller, + icon: Icons.format_list_bulleted, + ), + ), + Visibility( + visible: showListCheck, + child: ToggleCheckListButton( + attribute: Attribute.unchecked, + controller: controller, + icon: Icons.check_box, + ), + ), + Visibility( + visible: showCodeBlock, + child: ToggleStyleButton( + attribute: Attribute.codeBlock, + controller: controller, + icon: Icons.code, + ), + ), + Visibility( + visible: !showListNumbers && + !showListBullets && + !showListCheck && + !showCodeBlock, + child: VerticalDivider( + indent: 12, endIndent: 12, color: Colors.grey.shade400)), + Visibility( + visible: showQuote, + child: ToggleStyleButton( + attribute: Attribute.blockQuote, + controller: controller, + icon: Icons.format_quote, + ), + ), + Visibility( + visible: showIndent, + child: IndentButton( + icon: Icons.format_indent_increase, + controller: controller, + isIncrease: true, + ), + ), + Visibility( + visible: showIndent, + child: IndentButton( + icon: Icons.format_indent_decrease, + controller: controller, + isIncrease: false, + ), + ), + Visibility( + visible: showQuote, + child: VerticalDivider( + indent: 12, endIndent: 12, color: Colors.grey.shade400)), + Visibility( + visible: showLink, + child: LinkStyleButton(controller: controller)), + Visibility( + visible: showHorizontalRule, + child: InsertEmbedButton( + controller: controller, + icon: Icons.horizontal_rule, + ), + ), + ]); } final List children;