Skip to content

Commit

Permalink
chore: reanme highlight color to background color (#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Feb 28, 2024
1 parent 9b9b2e7 commit a52a2e7
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ extension AppFlowyRichTextAttributes on Attributes {
}

Color? get backgroundColor {
final highlightColor = this[AppFlowyRichTextKeys.highlightColor] as String?;
final highlightColor =
this[AppFlowyRichTextKeys.backgroundColor] as String?;
return highlightColor?.tryToColor();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class AppFlowyRichTextKeys {
static String underline = 'underline';
static String strikethrough = 'strikethrough';
static String textColor = 'font_color';
static String highlightColor = 'bg_color';
static String backgroundColor = 'bg_color';
static String findBackgroundColor = 'find_bg_color';
static String code = 'code';
static String href = 'href';
Expand All @@ -17,7 +17,7 @@ class AppFlowyRichTextKeys {
underline,
strikethrough,
textColor,
highlightColor,
backgroundColor,
];

// The values supported toggled even if the selection is collapsed.
Expand All @@ -27,5 +27,8 @@ class AppFlowyRichTextKeys {
underline,
strikethrough,
code,
fontFamily,
textColor,
backgroundColor,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Future<void> onInsert(
);
}

// delete the '\' character if the shortcut event is ignored.
// delete the '\' character if the shortcut event is ignored.
if (backSlashLocation > 0) {
final transaction = editorState.transaction
..deleteText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ KeyEventResult _toggleHighlight(
final nodes = editorState.getNodesInSelection(selection);
final isHighlighted = nodes.allSatisfyInSelection(selection, (delta) {
return delta.everyAttributes(
(attributes) => attributes[AppFlowyRichTextKeys.highlightColor] != null,
(attributes) => attributes[AppFlowyRichTextKeys.backgroundColor] != null,
);
});

editorState.formatDelta(
selection,
{
AppFlowyRichTextKeys.highlightColor:
AppFlowyRichTextKeys.backgroundColor:
isHighlighted ? null : style.highlightColor.toHex(),
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ToolbarItem buildHighlightColorItem({List<ColorOption>? colorOptions}) {
final nodes = editorState.getNodesInSelection(selection);
final isHighlight = nodes.allSatisfyInSelection(selection, (delta) {
return delta.everyAttributes((attributes) {
highlightColorHex = attributes[AppFlowyRichTextKeys.highlightColor];
highlightColorHex = attributes[AppFlowyRichTextKeys.backgroundColor];
return highlightColorHex != null;
});
});
Expand All @@ -29,7 +29,7 @@ ToolbarItem buildHighlightColorItem({List<ColorOption>? colorOptions}) {
showClearButton = delta.whereType<TextInsert>().any(
(element) {
return element
.attributes?[AppFlowyRichTextKeys.highlightColor] !=
.attributes?[AppFlowyRichTextKeys.backgroundColor] !=
null;
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class _BackgroundColorOptionsWidgetsState
final nodes = widget.editorState.getNodesInSelection(selection);
final hasTextColor = nodes.allSatisfyInSelection(selection, (delta) {
return delta.everyAttributes(
(attributes) => attributes[AppFlowyRichTextKeys.highlightColor] != null,
(attributes) =>
attributes[AppFlowyRichTextKeys.backgroundColor] != null,
);
});

Expand All @@ -48,7 +49,7 @@ class _BackgroundColorOptionsWidgetsState
setState(() {
widget.editorState.formatDelta(
selection,
{AppFlowyRichTextKeys.highlightColor: null},
{AppFlowyRichTextKeys.backgroundColor: null},
);
});
}
Expand All @@ -60,7 +61,7 @@ class _BackgroundColorOptionsWidgetsState
final isSelected = nodes.allSatisfyInSelection(selection, (delta) {
return delta.everyAttributes(
(attributes) =>
attributes[AppFlowyRichTextKeys.highlightColor] ==
attributes[AppFlowyRichTextKeys.backgroundColor] ==
e.colorHex,
);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/src/editor/toolbar/utils/format_color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void formatHighlightColor(
}) {
editorState.formatDelta(
selection,
{AppFlowyRichTextKeys.highlightColor: color},
{AppFlowyRichTextKeys.backgroundColor: color},
withUpdateSelection: withUpdateSelection,
);
}
Expand Down
20 changes: 12 additions & 8 deletions lib/src/editor_state.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:collection';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/editor_component/service/scroll/auto_scroller.dart';
Expand Down Expand Up @@ -107,7 +108,9 @@ class EditorState {
/// Sets the selection of the editor.
set selection(Selection? value) {
// clear the toggled style when the selection is changed.
toggledStyle.clear();
if (selectionNotifier.value != value) {
_toggledStyle.clear();
}

selectionNotifier.value = value;
}
Expand Down Expand Up @@ -159,13 +162,14 @@ class EditorState {
///
/// NOTES: It only works once;
/// after the selection is changed, the toggled style will be cleared.
final toggledStyle = <String, bool>{};
late final toggledStyleNotifier =
ValueNotifier<Map<String, bool>>(toggledStyle);

void updateToggledStyle(String key, bool value) {
toggledStyle[key] = value;
toggledStyleNotifier.value = {...toggledStyle};
UnmodifiableMapView<String, dynamic> get toggledStyle =>
UnmodifiableMapView<String, dynamic>(_toggledStyle);
final _toggledStyle = Attributes();
late final toggledStyleNotifier = ValueNotifier<Attributes>(toggledStyle);

void updateToggledStyle(String key, dynamic value) {
_toggledStyle[key] = value;
toggledStyleNotifier.value = {..._toggledStyle};
}

final UndoManager undoManager = UndoManager();
Expand Down
4 changes: 2 additions & 2 deletions lib/src/plugins/html/html_document_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class DocumentHTMLDecoder extends Converter<String, Document> {
if (backgroundColor != null) {
final highlightColor = backgroundColor.tryToColor()?.toHex();
if (highlightColor != null) {
attributes[AppFlowyRichTextKeys.highlightColor] = highlightColor;
attributes[AppFlowyRichTextKeys.backgroundColor] = highlightColor;
}
}

Expand All @@ -430,7 +430,7 @@ class DocumentHTMLDecoder extends Converter<String, Document> {
if (background != null) {
final highlightColor = background.tryToColor()?.toHex();
if (highlightColor != null) {
attributes[AppFlowyRichTextKeys.highlightColor] = highlightColor;
attributes[AppFlowyRichTextKeys.backgroundColor] = highlightColor;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/plugins/quill_delta/quill_delta_encoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class QuillDeltaEncoder extends Converter<Delta, Document> {
final backgroundColor = attributes?['background'] as String?;
final backgroundHex = _convertColorToHexString(backgroundColor);
if (backgroundHex != null) {
attrs[AppFlowyRichTextKeys.highlightColor] = backgroundHex;
attrs[AppFlowyRichTextKeys.backgroundColor] = backgroundHex;
}
node.updateAttributes({
'delta': (node.delta?..insert(text, attributes: attrs))?.toJson(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void main() {
node?.allSatisfyInSelection(selection, (delta) {
return delta.whereType<TextInsert>().every(
(element) =>
element.attributes?[AppFlowyRichTextKeys.highlightColor] ==
element.attributes?[AppFlowyRichTextKeys.backgroundColor] ==
Colors.red.withOpacity(0.3).toHex(),
);
}),
Expand All @@ -106,7 +106,7 @@ void main() {
node?.allSatisfyInSelection(selection, (delta) {
return delta.whereType<TextInsert>().every(
(element) =>
element.attributes?[AppFlowyRichTextKeys.highlightColor] ==
element.attributes?[AppFlowyRichTextKeys.backgroundColor] ==
null,
);
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main() async {
(tester) async {
await _testUpdateTextColorByCommandX(
tester,
AppFlowyRichTextKeys.highlightColor,
AppFlowyRichTextKeys.backgroundColor,
LogicalKeyboardKey.keyH,
);
});
Expand Down

0 comments on commit a52a2e7

Please sign in to comment.