@@ -132,25 +132,52 @@ class SingleColumnLayoutSelectionStyler extends SingleColumnLayoutStylePhase {
132
132
editorStyleLog.finer (' - extent: ${textSelection ?.extent }' );
133
133
134
134
if (viewModel is TextComponentViewModel ) {
135
- final componentTextColor = viewModel.textStyleBuilder ({}).color;
136
-
137
- final textWithSelectionAttributions =
138
- textSelection != null && _selectedTextColorStrategy != null && componentTextColor != null
139
- ? (viewModel.text.copyText (0 )
140
- ..addAttribution (
141
- ColorAttribution (_selectedTextColorStrategy !(
142
- originalTextColor: componentTextColor,
143
- selectionHighlightColor: _selectionStyles.selectionColor,
144
- )),
145
- SpanRange (textSelection.start, textSelection.end - 1 ),
146
- // The selected range might already have a color attribution. We want to override it
147
- // with the selected text color.
148
- overwriteConflictingSpans: true ,
149
- ))
150
- : viewModel.text;
135
+ AttributedText ? textWithSelectionAttributions;
136
+
137
+ if (textSelection != null && _selectedTextColorStrategy != null ) {
138
+ final selectedRange = SpanRange (textSelection.start, textSelection.end - 1 );
139
+
140
+ final componentTextColor = viewModel.textStyleBuilder ({}).color;
141
+ if (componentTextColor != null ) {
142
+ // Compute the selected text color for the default color of the node. If there is any
143
+ // text color attributions in the selected range, they will override this color.
144
+ textWithSelectionAttributions = viewModel.text.copyText (0 )
145
+ ..addAttribution (
146
+ ColorAttribution (_selectedTextColorStrategy !(
147
+ originalTextColor: componentTextColor,
148
+ selectionHighlightColor: _selectionStyles.selectionColor,
149
+ )),
150
+ selectedRange,
151
+ // Override any existing color attributions.
152
+ overwriteConflictingSpans: true ,
153
+ );
154
+ }
155
+
156
+ final coloredSpans = viewModel.text.getAttributionSpansInRange (
157
+ attributionFilter: (attr) => attr is ColorAttribution ,
158
+ range: selectedRange,
159
+ // We should only change the selected portion of each span.
160
+ resizeSpansToFitInRange: true ,
161
+ );
162
+ if (coloredSpans.isNotEmpty) {
163
+ // Compute and apply the selected text color for each span that has a color attribution.
164
+ textWithSelectionAttributions ?? = viewModel.text.copyText (0 );
165
+ for (final span in coloredSpans) {
166
+ textWithSelectionAttributions.addAttribution (
167
+ ColorAttribution (_selectedTextColorStrategy !(
168
+ originalTextColor: (span.attribution as ColorAttribution ).color,
169
+ selectionHighlightColor: _selectionStyles.selectionColor,
170
+ )),
171
+ SpanRange (span.start, span.end),
172
+ // Override any existing color attributions.
173
+ overwriteConflictingSpans: true ,
174
+ );
175
+ }
176
+ }
177
+ }
151
178
152
179
viewModel
153
- ..text = textWithSelectionAttributions
180
+ ..text = textWithSelectionAttributions ?? viewModel.text
154
181
..selection = textSelection
155
182
..selectionColor = _selectionStyles.selectionColor
156
183
..highlightWhenEmpty = highlightWhenEmpty;
0 commit comments