Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: customized color options #270

Merged
merged 6 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 3 additions & 79 deletions example/lib/pages/simple_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class SimpleEditor extends StatelessWidget {
bulletedListItem,
numberedListItem,
linkItem,
textColorItem,
highlightColorItem
buildTextColorItem(),
buildHighlightColorItem()
],
editorState: editorState,
scrollController: scrollController,
Expand All @@ -69,7 +69,7 @@ class SimpleEditor extends StatelessWidget {
editorState: editorState,
toolbarItems: [
textDecorationMobileToolbarItem,
textAndBackgroundColorMobileToolbarItem,
buildTextAndBackgroundColorMobileToolbarItem(),
headingMobileToolbarItem,
todoListMobileToolbarItem,
listMobileToolbarItem,
Expand All @@ -78,82 +78,6 @@ class SimpleEditor extends StatelessWidget {
codeMobileToolbarItem,
// dividerMobileToolbarItem,
],
textColorOptions: [
ColorOption(
colorHex: Colors.grey.toHex(),
name: AppFlowyEditorLocalizations.current.fontColorGray,
),
ColorOption(
colorHex: Colors.brown.toHex(),
name: AppFlowyEditorLocalizations.current.fontColorBrown,
),
ColorOption(
colorHex: Colors.yellow.toHex(),
name: AppFlowyEditorLocalizations.current.fontColorYellow,
),
ColorOption(
colorHex: Colors.green.toHex(),
name: AppFlowyEditorLocalizations.current.fontColorGreen,
),
ColorOption(
colorHex: Colors.blue.toHex(),
name: AppFlowyEditorLocalizations.current.fontColorBlue,
),
ColorOption(
colorHex: Colors.purple.toHex(),
name: AppFlowyEditorLocalizations.current.fontColorPurple,
),
ColorOption(
colorHex: Colors.pink.toHex(),
name: AppFlowyEditorLocalizations.current.fontColorPink,
),
ColorOption(
colorHex: Colors.red.toHex(),
name: AppFlowyEditorLocalizations.current.fontColorRed,
),
],
backgroundColorOptions: [
ColorOption(
colorHex: Colors.grey.withOpacity(0.3).toHex(),
name: AppFlowyEditorLocalizations
.current.backgroundColorGray,
),
ColorOption(
colorHex: Colors.brown.withOpacity(0.3).toHex(),
name: AppFlowyEditorLocalizations
.current.backgroundColorBrown,
),
ColorOption(
colorHex: Colors.yellow.withOpacity(0.3).toHex(),
name: AppFlowyEditorLocalizations
.current.backgroundColorYellow,
),
ColorOption(
colorHex: Colors.green.withOpacity(0.3).toHex(),
name: AppFlowyEditorLocalizations
.current.backgroundColorGreen,
),
ColorOption(
colorHex: Colors.blue.withOpacity(0.3).toHex(),
name: AppFlowyEditorLocalizations
.current.backgroundColorBlue,
),
ColorOption(
colorHex: Colors.purple.withOpacity(0.3).toHex(),
name: AppFlowyEditorLocalizations
.current.backgroundColorPurple,
),
ColorOption(
colorHex: Colors.pink.withOpacity(0.3).toHex(),
name: AppFlowyEditorLocalizations
.current.backgroundColorPink,
),
ColorOption(
colorHex: Colors.red.withOpacity(0.3).toHex(),
name: AppFlowyEditorLocalizations
.current.backgroundColorRed,
),
],
),
],
);
Expand Down
6 changes: 4 additions & 2 deletions lib/src/editor/toolbar/desktop/items/color/color_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ void showColorMenu(
EditorState editorState,
Selection selection, {
String? currentColorHex,
List<ColorOption>? textColorOptions,
List<ColorOption>? highlightColorOptions,
required bool isTextColor,
}) {
// Since link format is only available for single line selection,
Expand Down Expand Up @@ -44,8 +46,8 @@ void showColorMenu(
editorState: editorState,
selectedColorHex: currentColorHex,
colorOptions: isTextColor
? generateTextColorOptions()
: generateHighlightColorOptions(),
? textColorOptions ?? generateTextColorOptions()
: highlightColorOptions ?? generateHighlightColorOptions(),
onSubmittedColorHex: (color) {
isTextColor
? formatFontColor(
Expand Down
4 changes: 2 additions & 2 deletions lib/src/editor/toolbar/desktop/items/color/color_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class ResetTextColorButton extends StatelessWidget {
onPressed: () {
final selection = editorState.selection!;
editorState
.formatDelta(selection, {BuiltInAttributeKey.textColor: null});
.formatDelta(selection, {FlowyRichTextKeys.textColor: null});
dismissOverlay();
},
icon: FlowySvg(
Expand Down Expand Up @@ -225,7 +225,7 @@ class ClearHighlightColorButton extends StatelessWidget {
final selection = editorState.selection!;
editorState.formatDelta(
selection,
{BuiltInAttributeKey.highlightColor: null},
{FlowyRichTextKeys.highlightColor: null},
);
dismissOverlay();
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';

final highlightColorItem = ToolbarItem(
id: 'editor.highlightColor',
group: 4,
isActive: onlyShowInTextType,
builder: (context, editorState) {
String? highlightColorHex;
ToolbarItem buildHighlightColorItem({List<ColorOption>? colorOptions}) {
return ToolbarItem(
id: 'editor.highlightColor',
group: 4,
isActive: onlyShowInTextType,
builder: (context, editorState) {
String? highlightColorHex;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this ever get assigned any other value than null? Seems redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

highlightColorHex is used to show a checked mark on current color options(if it has a color), I assigned value to it now(same with text color).
image

Nice catch, Thanks!


final selection = editorState.selection!;
final nodes = editorState.getNodesInSelection(selection);
final isHighlight = nodes.allSatisfyInSelection(selection, (delta) {
return delta.everyAttributes(
(attributes) => attributes[FlowyRichTextKeys.highlightColor] != null,
final selection = editorState.selection!;
final nodes = editorState.getNodesInSelection(selection);
final isHighlight = nodes.allSatisfyInSelection(selection, (delta) {
return delta.everyAttributes((attributes) {
highlightColorHex = attributes[FlowyRichTextKeys.highlightColor];
return highlightColorHex != null;
});
});
return IconItemWidget(
iconName: 'toolbar/highlight_color',
iconSize: const Size.square(14),
isHighlight: isHighlight,
tooltip: AppFlowyEditorLocalizations.current.highlightColor,
onPressed: () {
showColorMenu(
context,
editorState,
selection,
currentColorHex: highlightColorHex,
isTextColor: false,
highlightColorOptions: colorOptions,
);
},
);
});
return IconItemWidget(
iconName: 'toolbar/highlight_color',
iconSize: const Size.square(14),
isHighlight: isHighlight,
tooltip: AppFlowyEditorLocalizations.current.highlightColor,
onPressed: () {
showColorMenu(
context,
editorState,
selection,
currentColorHex: highlightColorHex,
isTextColor: false,
);
},
);
},
);
},
);
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';

final textColorItem = ToolbarItem(
id: 'editor.textColor',
group: 4,
isActive: onlyShowInTextType,
builder: (context, editorState) {
String? textColorHex;
final selection = editorState.selection!;
final nodes = editorState.getNodesInSelection(selection);
final isHighlight = nodes.allSatisfyInSelection(selection, (delta) {
return delta.everyAttributes(
(attributes) => attributes[FlowyRichTextKeys.textColor] != null,
ToolbarItem buildTextColorItem({
List<ColorOption>? colorOptions,
}) {
return ToolbarItem(
id: 'editor.textColor',
group: 4,
isActive: onlyShowInTextType,
builder: (context, editorState) {
String? textColorHex;
final selection = editorState.selection!;
final nodes = editorState.getNodesInSelection(selection);
final isHighlight = nodes.allSatisfyInSelection(selection, (delta) {
return delta.everyAttributes((attributes) {
textColorHex = attributes[FlowyRichTextKeys.textColor];
return (textColorHex != null);
});
});
return IconItemWidget(
iconName: 'toolbar/text_color',
isHighlight: isHighlight,
iconSize: const Size.square(14),
tooltip: AppFlowyEditorLocalizations.current.textColor,
onPressed: () {
showColorMenu(
context,
editorState,
selection,
currentColorHex: textColorHex,
isTextColor: true,
textColorOptions: colorOptions,
);
},
);
});
return IconItemWidget(
iconName: 'toolbar/text_color',
isHighlight: isHighlight,
iconSize: const Size.square(14),
tooltip: AppFlowyEditorLocalizations.current.textColor,
onPressed: () {
showColorMenu(
context,
editorState,
selection,
currentColorHex: textColorHex,
isTextColor: true,
);
},
);
},
);
},
);
}
77 changes: 1 addition & 76 deletions lib/src/editor/toolbar/mobile/mobile_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,78 +20,7 @@ class MobileToolbar extends StatelessWidget {
this.buttonSpacing = 8.0,
this.buttonBorderWidth = 1.0,
this.buttonSelectedBorderWidth = 2.0,
this.textColorOptions = const [
ColorOption(
colorHex: '#808080',
name: 'Gray',
),
ColorOption(
colorHex: '#A52A2A',
name: 'Brown',
),
ColorOption(
colorHex: '#FFFF00',
name: 'Yellow',
),
ColorOption(
colorHex: '#008000',
name: 'Green',
),
ColorOption(
colorHex: '#0000FF',
name: 'Blue',
),
ColorOption(
colorHex: '#800080',
name: 'Purple',
),
ColorOption(
colorHex: '#FFC0CB',
name: 'Pink',
),
ColorOption(
colorHex: '#FF0000',
name: 'Red',
),
],
this.backgroundColorOptions = const [
ColorOption(
colorHex: '#4D4D4D',
name: 'Gray',
),
ColorOption(
colorHex: '#A52A2A',
name: 'Brown',
),
ColorOption(
colorHex: '#FFFF00',
name: 'Yellow',
),
ColorOption(
colorHex: '#008000',
name: 'Green',
),
ColorOption(
colorHex: '#0000FF',
name: 'Blue',
),
ColorOption(
colorHex: '#800080',
name: 'Purple',
),
ColorOption(
colorHex: '#FFC0CB',
name: 'Pink',
),
ColorOption(
colorHex: '#FF0000',
name: 'Red',
),
],
}) : assert(
textColorOptions.length > 0 && backgroundColorOptions.length > 0,
);

});
final EditorState editorState;
final List<MobileToolbarItem> toolbarItems;
// MobileToolbarStyle parameters
Expand All @@ -108,8 +37,6 @@ class MobileToolbar extends StatelessWidget {
final double buttonSpacing;
final double buttonBorderWidth;
final double buttonSelectedBorderWidth;
final List<ColorOption> textColorOptions;
final List<ColorOption> backgroundColorOptions;

@override
Widget build(BuildContext context) {
Expand All @@ -133,8 +60,6 @@ class MobileToolbar extends StatelessWidget {
buttonSpacing: buttonSpacing,
buttonBorderWidth: buttonBorderWidth,
buttonSelectedBorderWidth: buttonSelectedBorderWidth,
textColorOptions: textColorOptions,
backgroundColorOptions: backgroundColorOptions,
child: MobileToolbarWidget(
// Use selection as key to force rebuild toolbar widget when selection changed.
key: ValueKey(selection),
Expand Down
Loading