Format code

pull/162/head
Xin Yao 4 years ago
parent bc5eb86a8f
commit df602cfa9b
  1. 2
      lib/models/documents/nodes/line.dart
  2. 8
      lib/models/documents/style.dart
  3. 9
      lib/widgets/controller.dart
  4. 32
      lib/widgets/editor.dart
  5. 6
      lib/widgets/raw_editor.dart
  6. 377
      lib/widgets/toolbar.dart

@ -197,7 +197,7 @@ class Line extends Container<Leaf?> {
}
applyStyle(newStyle);
final blockStyle = newStyle.getNotNullValueBlockExceptHeader();
final blockStyle = newStyle.getBlockExceptHeader();
if (blockStyle == null) {
return;
} // No block-level changes

@ -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;
}
}

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

@ -176,18 +176,25 @@ class QuillEditor extends StatefulWidget {
final ScrollPhysics? scrollPhysics;
final ValueChanged<String>? 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;
}
}

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

@ -432,7 +432,8 @@ class _SelectHeaderStyleButtonState extends State<SelectHeaderStyleButton> {
@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<HistoryButton> {
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<IndentButton> {
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<ClearFormatButton> {
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<ClearFormatButton> {
}
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<Widget> children;

Loading…
Cancel
Save