Rich text editor for Flutter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
7.0 KiB

import 'package:flutter/material.dart';
import '../../../../extensions.dart';
import '../../../extensions/quill_configurations_ext.dart';
import '../../../l10n/extensions/localizations.dart';
import '../../../models/documents/attribute.dart';
import '../base_button/base_value_button.dart';
import '../base_toolbar.dart';
Fix block multi-line selection style (#1876) * toggle_style_button : calls to options.afterButtonPressed replaced by call to class function afterButtonPressed to allow default call to base button settings quill_icon_button: L26 build for isSelected updated to call afterButtonPressed = same as if not selected QuillController _updateSelection removed param=source because not used; added new param insertNewline when true set tog to style of preceding char (last entered); updated replaceText to call _updateSelection for NL document collectStyle: Selecting the start of a line, user expects the style to be the visible style of the line including inline styles * color_button calls afterButtonPressed insert at start of line uses style for line * Remove comments * Fix formatting issue * Fix FontFamily and Size button actions * Fix FontFamily and Size button actions * Value setting Stateful toolbar buttons derive from base class * Rename base class as QuillToolbarBaseValueButton * Fixes for before_push script * Removed deprecated functions * Move clipboard actions to QuillController * Fix: collectAllIndividualStylesAndEmbed for result span * Add: Clipboard toolbar buttons * export: Clipboard toolbar buttons * Fix: Dividers not shown in toolbar when multiRowsDisplay. Fix: Toolbar drop buttons clipped when !multiRowsDisplay * Add: test for QuillController clipboard Dart Formatted * Localizations updated * QuillControllerConfigurations and clipboard paste * Fix: CheckList action * Fix: Multiline selection and refactor toolbar buttons * Add tests: Multiline selection --------- Co-authored-by: Douglas Ward <dward@scied.com>
11 months ago
class QuillToolbarFontFamilyButton extends QuillToolbarBaseButton<
QuillToolbarFontFamilyButtonOptions,
QuillToolbarFontFamilyButtonExtraOptions> {
QuillToolbarFontFamilyButton({
required super.controller,
@Deprecated('Please use the default display text from the options')
this.defaultDisplayText,
super.options = const QuillToolbarFontFamilyButtonOptions(),
super.key,
}) : assert(options.rawItemsMap?.isNotEmpty ?? (true)),
assert(
options.initialValue == null || options.initialValue!.isNotEmpty,
);
final String? defaultDisplayText;
@override
QuillToolbarFontFamilyButtonState createState() =>
QuillToolbarFontFamilyButtonState();
}
Fix block multi-line selection style (#1876) * toggle_style_button : calls to options.afterButtonPressed replaced by call to class function afterButtonPressed to allow default call to base button settings quill_icon_button: L26 build for isSelected updated to call afterButtonPressed = same as if not selected QuillController _updateSelection removed param=source because not used; added new param insertNewline when true set tog to style of preceding char (last entered); updated replaceText to call _updateSelection for NL document collectStyle: Selecting the start of a line, user expects the style to be the visible style of the line including inline styles * color_button calls afterButtonPressed insert at start of line uses style for line * Remove comments * Fix formatting issue * Fix FontFamily and Size button actions * Fix FontFamily and Size button actions * Value setting Stateful toolbar buttons derive from base class * Rename base class as QuillToolbarBaseValueButton * Fixes for before_push script * Removed deprecated functions * Move clipboard actions to QuillController * Fix: collectAllIndividualStylesAndEmbed for result span * Add: Clipboard toolbar buttons * export: Clipboard toolbar buttons * Fix: Dividers not shown in toolbar when multiRowsDisplay. Fix: Toolbar drop buttons clipped when !multiRowsDisplay * Add: test for QuillController clipboard Dart Formatted * Localizations updated * QuillControllerConfigurations and clipboard paste * Fix: CheckList action * Fix: Multiline selection and refactor toolbar buttons * Add tests: Multiline selection --------- Co-authored-by: Douglas Ward <dward@scied.com>
11 months ago
class QuillToolbarFontFamilyButtonState extends QuillToolbarBaseButtonState<
QuillToolbarFontFamilyButton,
QuillToolbarFontFamilyButtonOptions,
QuillToolbarFontFamilyButtonExtraOptions,
String> {
@override
String get currentStateValue {
final attribute =
controller.getSelectionStyle().attributes[options.attribute.key];
return attribute == null
? _defaultDisplayText
: (_getKeyName(attribute.value) ?? _defaultDisplayText);
}
String get _defaultDisplayText {
return options.initialValue ??
widget.options.defaultDisplayText ??
widget.defaultDisplayText ??
context.loc.font;
}
Map<String, String> get rawItemsMap {
final rawItemsMap =
context.quillSimpleToolbarConfigurations?.fontFamilyValues ??
options.rawItemsMap ??
{
'Sans Serif': 'sans-serif',
'Serif': 'serif',
'Monospace': 'monospace',
'Ibarra Real Nova': 'ibarra-real-nova',
'SquarePeg': 'square-peg',
'Nunito': 'nunito',
'Pacifico': 'pacifico',
'Roboto Mono': 'roboto-mono',
context.loc.clear: 'Clear'
};
return rawItemsMap;
}
String? _getKeyName(String value) {
for (final entry in rawItemsMap.entries) {
if (entry.value == value) {
return entry.key;
}
}
return null;
}
@override
String get defaultTooltip => context.loc.fontFamily;
Fix reported issues (#1886) * toggle_style_button : calls to options.afterButtonPressed replaced by call to class function afterButtonPressed to allow default call to base button settings quill_icon_button: L26 build for isSelected updated to call afterButtonPressed = same as if not selected QuillController _updateSelection removed param=source because not used; added new param insertNewline when true set tog to style of preceding char (last entered); updated replaceText to call _updateSelection for NL document collectStyle: Selecting the start of a line, user expects the style to be the visible style of the line including inline styles * color_button calls afterButtonPressed insert at start of line uses style for line * Remove comments * Fix formatting issue * Fix FontFamily and Size button actions * Fix FontFamily and Size button actions * Value setting Stateful toolbar buttons derive from base class * Rename base class as QuillToolbarBaseValueButton * Fixes for before_push script * Removed deprecated functions * Move clipboard actions to QuillController * Fix: collectAllIndividualStylesAndEmbed for result span * Add: Clipboard toolbar buttons * export: Clipboard toolbar buttons * Fix: Dividers not shown in toolbar when multiRowsDisplay. Fix: Toolbar drop buttons clipped when !multiRowsDisplay * Add: test for QuillController clipboard Dart Formatted * Localizations updated * QuillControllerConfigurations and clipboard paste * Fix: CheckList action * Fix: Multiline selection and refactor toolbar buttons * Add tests: Multiline selection * Fix header toggle button and new block attribute support. * Fix QuillToolbarColorButtonExtraOptions --------- Co-authored-by: Douglas Ward <dward@scied.com>
11 months ago
@override
IconData get defaultIconData => Icons.font_download_outlined;
void _onPressed() {
if (_menuController.isOpen) {
_menuController.close();
} else {
_menuController.open();
}
afterButtonPressed?.call();
}
final _menuController = MenuController();
@override
Widget build(BuildContext context) {
final baseButtonConfigurations = context.quillToolbarBaseButtonOptions;
final childBuilder =
options.childBuilder ?? baseButtonConfigurations?.childBuilder;
if (childBuilder != null) {
return childBuilder(
options,
QuillToolbarFontFamilyButtonExtraOptions(
currentValue: currentValue,
defaultDisplayText: _defaultDisplayText,
controller: controller,
context: context,
onPressed: _onPressed,
),
);
}
return UtilityWidgets.maybeWidget(
enabled: tooltip.isNotEmpty || options.overrideTooltipByFontFamily,
wrapper: (child) {
var effectiveTooltip = tooltip;
if (options.overrideTooltipByFontFamily) {
effectiveTooltip = effectiveTooltip.isNotEmpty
? '$effectiveTooltip: $currentValue'
: '${context.loc.font}: $currentValue';
}
return Tooltip(message: effectiveTooltip, child: child);
},
child: MenuAnchor(
controller: _menuController,
menuChildren: [
for (final MapEntry<String, String> fontFamily in rawItemsMap.entries)
MenuItemButton(
key: ValueKey(fontFamily.key),
// value: fontFamily.value,
// height: options.itemHeight ?? kMinInteractiveDimension,
// padding: options.itemPadding,
onPressed: () {
final newValue = fontFamily.value;
final keyName = _getKeyName(newValue);
setState(() {
if (keyName != 'Clear') {
currentValue = keyName ?? _defaultDisplayText;
} else {
currentValue = _defaultDisplayText;
}
if (keyName != null) {
controller.formatSelection(
Attribute.fromKeyValue(
Attribute.font.key,
newValue == 'Clear' ? null : newValue,
),
);
options.onSelected?.call(newValue);
}
});
},
child: Text(
fontFamily.key.toString(),
style: TextStyle(
fontFamily:
options.renderFontFamilies ? fontFamily.value : null,
color: fontFamily.value == 'Clear'
? options.defaultItemColor
: null,
),
),
),
],
child: Builder(
builder: (context) {
final isMaterial3 = Theme.of(context).useMaterial3;
if (!isMaterial3) {
return RawMaterialButton(
onPressed: _onPressed,
child: _buildContent(context),
);
}
return QuillToolbarIconButton(
isSelected: false,
iconTheme: iconTheme,
onPressed: _onPressed,
icon: _buildContent(context),
);
},
),
),
);
}
Widget _buildContent(BuildContext context) {
final hasFinalWidth = options.width != null;
return Padding(
padding: options.padding ?? const EdgeInsets.fromLTRB(10, 0, 0, 0),
child: Row(
mainAxisSize: !hasFinalWidth ? MainAxisSize.min : MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
UtilityWidgets.maybeWidget(
enabled: hasFinalWidth,
wrapper: (child) => Expanded(child: child),
child: Text(
currentValue,
maxLines: 1,
overflow: options.labelOverflow,
style: options.style ??
TextStyle(
fontSize: iconSize / 1.15,
// color: iconTheme?.iconUnselectedFillColor ??
// theme.iconTheme.color,
),
),
),
Icon(
Icons.arrow_drop_down,
size: iconSize * iconButtonFactor,
// color: iconTheme?.iconUnselectedFillColor ?? theme.iconTheme.color,
)
],
),
);
}
}