Skip to content

Commit 3eaa31c

Browse files
committed
feat: #931 highlight the status of the currently selected style in toolbar
1 parent 78dcd8e commit 3eaa31c

File tree

11 files changed

+240
-70
lines changed

11 files changed

+240
-70
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'package:appflowy_editor/appflowy_editor.dart';
2+
3+
extension EditorStateExtensions on EditorState {
4+
List<TextNode> get selectedTextNodes =>
5+
service.selectionService.currentSelectedNodes
6+
.whereType<TextNode>()
7+
.toList(growable: false);
8+
}

frontend/app_flowy/packages/appflowy_editor/lib/src/extensions/text_node_extensions.dart

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,56 +29,63 @@ extension TextNodeExtension on TextNode {
2929
}
3030

3131
bool allSatisfyLinkInSelection(Selection selection) =>
32-
allSatisfyInSelection(StyleKey.href, selection, (value) {
32+
allSatisfyInSelection(selection, StyleKey.href, (value) {
3333
return value != null;
3434
});
3535

3636
bool allSatisfyBoldInSelection(Selection selection) =>
37-
allSatisfyInSelection(StyleKey.bold, selection, (value) {
37+
allSatisfyInSelection(selection, StyleKey.bold, (value) {
3838
return value == true;
3939
});
4040

4141
bool allSatisfyItalicInSelection(Selection selection) =>
42-
allSatisfyInSelection(StyleKey.italic, selection, (value) {
42+
allSatisfyInSelection(selection, StyleKey.italic, (value) {
4343
return value == true;
4444
});
4545

4646
bool allSatisfyUnderlineInSelection(Selection selection) =>
47-
allSatisfyInSelection(StyleKey.underline, selection, (value) {
47+
allSatisfyInSelection(selection, StyleKey.underline, (value) {
4848
return value == true;
4949
});
5050

5151
bool allSatisfyStrikethroughInSelection(Selection selection) =>
52-
allSatisfyInSelection(StyleKey.strikethrough, selection, (value) {
52+
allSatisfyInSelection(selection, StyleKey.strikethrough, (value) {
5353
return value == true;
5454
});
5555

5656
bool allSatisfyInSelection(
57-
String styleKey,
5857
Selection selection,
58+
String styleKey,
5959
bool Function(dynamic value) test,
6060
) {
61-
final ops = delta.whereType<TextInsert>();
62-
final startOffset =
63-
selection.isBackward ? selection.start.offset : selection.end.offset;
64-
final endOffset =
65-
selection.isBackward ? selection.end.offset : selection.start.offset;
66-
var start = 0;
67-
for (final op in ops) {
68-
if (start >= endOffset) {
69-
break;
61+
if (StyleKey.globalStyleKeys.contains(styleKey)) {
62+
if (attributes.containsKey(styleKey)) {
63+
return test(attributes[styleKey]);
7064
}
71-
final length = op.length;
72-
if (start < endOffset && start + length > startOffset) {
73-
if (op.attributes == null ||
74-
!op.attributes!.containsKey(styleKey) ||
75-
!test(op.attributes![styleKey])) {
76-
return false;
65+
} else if (StyleKey.partialStyleKeys.contains(styleKey)) {
66+
final ops = delta.whereType<TextInsert>();
67+
final startOffset =
68+
selection.isBackward ? selection.start.offset : selection.end.offset;
69+
final endOffset =
70+
selection.isBackward ? selection.end.offset : selection.start.offset;
71+
var start = 0;
72+
for (final op in ops) {
73+
if (start >= endOffset) {
74+
break;
7775
}
76+
final length = op.length;
77+
if (start < endOffset && start + length > startOffset) {
78+
if (op.attributes == null ||
79+
!op.attributes!.containsKey(styleKey) ||
80+
!test(op.attributes![styleKey])) {
81+
return false;
82+
}
83+
}
84+
start += length;
7885
}
79-
start += length;
86+
return true;
8087
}
81-
return true;
88+
return false;
8289
}
8390

8491
bool allNotSatisfyInSelection(
@@ -111,29 +118,44 @@ extension TextNodeExtension on TextNode {
111118
}
112119

113120
extension TextNodesExtension on List<TextNode> {
114-
bool allSatisfyBoldInSelection(Selection selection) =>
115-
allSatisfyInSelection(StyleKey.bold, selection, true);
121+
bool allSatisfyBoldInSelection(Selection selection) => allSatisfyInSelection(
122+
selection,
123+
StyleKey.bold,
124+
(value) => value == true,
125+
);
116126

117127
bool allSatisfyItalicInSelection(Selection selection) =>
118-
allSatisfyInSelection(StyleKey.italic, selection, true);
128+
allSatisfyInSelection(
129+
selection,
130+
StyleKey.italic,
131+
(value) => value == true,
132+
);
119133

120134
bool allSatisfyUnderlineInSelection(Selection selection) =>
121-
allSatisfyInSelection(StyleKey.underline, selection, true);
135+
allSatisfyInSelection(
136+
selection,
137+
StyleKey.underline,
138+
(value) => value == true,
139+
);
122140

123141
bool allSatisfyStrikethroughInSelection(Selection selection) =>
124-
allSatisfyInSelection(StyleKey.strikethrough, selection, true);
142+
allSatisfyInSelection(
143+
selection,
144+
StyleKey.strikethrough,
145+
(value) => value == true,
146+
);
125147

126148
bool allSatisfyInSelection(
127-
String styleKey,
128149
Selection selection,
129-
dynamic matchValue,
150+
String styleKey,
151+
bool Function(dynamic value) test,
130152
) {
131153
if (isEmpty) {
132154
return false;
133155
}
134156
if (length == 1) {
135-
return first.allSatisfyInSelection(styleKey, selection, (value) {
136-
return value == matchValue;
157+
return first.allSatisfyInSelection(selection, styleKey, (value) {
158+
return test(value);
137159
});
138160
} else {
139161
for (var i = 0; i < length; i++) {
@@ -154,8 +176,8 @@ extension TextNodesExtension on List<TextNode> {
154176
end: Position(path: node.path, offset: node.toRawString().length),
155177
);
156178
}
157-
if (!node.allSatisfyInSelection(styleKey, newSelection, (value) {
158-
return value == matchValue;
179+
if (!node.allSatisfyInSelection(newSelection, styleKey, (value) {
180+
return test(value);
159181
})) {
160182
return false;
161183
}

frontend/app_flowy/packages/appflowy_editor/lib/src/render/rich_text/rich_text_style.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class StyleKey {
4747
StyleKey.italic,
4848
StyleKey.underline,
4949
StyleKey.strikethrough,
50+
StyleKey.backgroundColor,
51+
StyleKey.href,
5052
];
5153

5254
static List<String> globalStyleKeys = [

0 commit comments

Comments
 (0)