Skip to content

Commit 7e1cbde

Browse files
committed
fix: add setColor and removeColor methods
1 parent 03d57ad commit 7e1cbde

File tree

10 files changed

+42
-21
lines changed

10 files changed

+42
-21
lines changed

android/src/main/java/com/swmansion/enriched/EnrichedTextInputViewManager.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ class EnrichedTextInputViewManager : SimpleViewManager<EnrichedTextInputView>(),
251251
view?.verifyAndToggleStyle(EnrichedSpans.UNORDERED_LIST)
252252
}
253253

254-
override fun toggleColor(view: EnrichedTextInputView?, color: String) {
254+
override fun setColor(view: EnrichedTextInputView?, color: String) {
255+
// no-op for now
256+
}
257+
258+
override fun removeColor(view: EnrichedTextInputView?) {
255259
// no-op for now
256260
}
257261

example/src/App.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
prepareImageDimensions,
3535
} from './utils/prepareImageDimensions';
3636
import type { OnChangeColorEvent } from '../../src/EnrichedTextInputNativeComponent';
37-
import ColorPreview from './components/ColorPreview';
37+
import { ColorPreview } from './components/ColorPreview';
3838

3939
type StylesState = OnChangeStateEvent;
4040

@@ -306,6 +306,10 @@ export default function App() {
306306
}
307307
};
308308

309+
const handleRemoveColor = () => {
310+
ref.current?.removeColor();
311+
};
312+
309313
return (
310314
<>
311315
<ScrollView
@@ -357,6 +361,11 @@ export default function App() {
357361
onPress={openValueModal}
358362
style={styles.valueButton}
359363
/>
364+
<Button
365+
title="Remove color"
366+
onPress={handleRemoveColor}
367+
style={styles.valueButton}
368+
/>
360369
<HtmlSection currentHtml={currentHtml} />
361370
{DEBUG_SCROLLABLE && <View style={styles.scrollPlaceholder} />}
362371
<ColorPreview color={selectionColor} />

example/src/components/ColorPreview.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { StyleSheet, Text, View } from 'react-native';
2+
import { type FC } from 'react';
23

34
type Props = {
45
color: string;
56
};
67

7-
const ColorPreview: React.FC<Props> = ({ color }) => {
8+
export const ColorPreview: FC<Props> = ({ color }) => {
89
return (
910
<>
1011
<View
@@ -28,5 +29,3 @@ const styles = StyleSheet.create({
2829
borderRadius: 20,
2930
},
3031
});
31-
32-
export default ColorPreview;

example/src/components/Toolbar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export const Toolbar: FC<ToolbarProps> = ({
152152
};
153153

154154
const handleColorButtonPress = (color: string) => {
155-
editorRef?.current?.toggleColor(color);
155+
editorRef?.current?.setColor(color);
156156
};
157157

158158
const isActive = (item: Item) => {

ios/EnrichedTextInputView.mm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,10 +773,13 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args {
773773
[self toggleRegularStyle: [UnderlineStyle getStyleType]];
774774
} else if([commandName isEqualToString:@"toggleStrikeThrough"]) {
775775
[self toggleRegularStyle: [StrikethroughStyle getStyleType]];
776-
} else if([commandName isEqualToString:@"toggleColor"]) {
776+
} else if([commandName isEqualToString:@"setColor"]) {
777777
NSString *colorText = (NSString *)args[0];
778778
UIColor *color = [UIColor colorFromString: colorText];
779-
[self toggleColorStyle: color];
779+
[self setColor: color];
780+
} else if ([commandName isEqualToString:@"removeColor"]) {
781+
ColorStyle *colorStyle = stylesDict[@([ColorStyle getStyleType])];
782+
[colorStyle removeAttributes: textView.selectedRange];
780783
} else if([commandName isEqualToString:@"toggleInlineCode"]) {
781784
[self toggleRegularStyle: [InlineCodeStyle getStyleType]];
782785
} else if([commandName isEqualToString:@"addLink"]) {
@@ -959,7 +962,7 @@ - (void)tryEmittingOnChangeHtmlEvent {
959962

960963
// MARK: - Styles manipulation
961964

962-
- (void)toggleColorStyle:(UIColor *)color {
965+
- (void)setColor:(UIColor *)color {
963966
ColorStyle *colorStyle = stylesDict[@(Colored)];
964967

965968
[colorStyle applyStyle:textView.selectedRange color: color];

ios/inputParser/InputParser.mm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,15 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
201201
NSMutableSet *fixedEndedStyles = [endedStyles mutableCopy];
202202
NSMutableSet *stylesToBeReAdded = [[NSMutableSet alloc] init];
203203

204-
for (NSNumber *style in endedStyles) {
204+
for(NSNumber *style in endedStyles) {
205205
NSInteger styleBeginning = [currentActiveStylesBeginning[style] integerValue];
206206

207207
for(NSNumber *activeStyle in currentActiveStyles) {
208208
NSInteger activeStyleBeginning = [currentActiveStylesBeginning[activeStyle] integerValue];
209-
209+
210+
// we end the styles that began after the currently ended style but not at the "i" (cause the old style ended at exactly "i-1"
211+
// also the ones that began in the exact same place but are "inner" in relation to them due to StyleTypeEnum integer values
212+
210213
if((activeStyleBeginning > styleBeginning && activeStyleBeginning < i) ||
211214
(activeStyleBeginning == styleBeginning && activeStyleBeginning < i && [activeStyle integerValue] > [style integerValue])) {
212215
[fixedEndedStyles addObject:activeStyle];

ios/utils/ColorExtension.mm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ + (UIColor *)colorFromString:(NSString *)string {
5353
@"grey": [UIColor grayColor],
5454
@"transparent": [UIColor clearColor],
5555
@"aliceblue": [UIColor colorWithRed:0.941 green:0.973 blue:1.0 alpha:1.0],
56-
// Add additional CSS names here, e.g., "chartreuse": [UIColor colorWithRed:0.498 green:1.0 blue:0.0 alpha:1.0],
5756
};
5857

5958
UIColor *namedColor = namedColors[input];
@@ -167,7 +166,7 @@ - (NSString *)hexString {
167166
b = components[2];
168167
} else {
169168
// Unsupported color space (e.g., pattern colors)
170-
return @"#FFFFFF"; // Or return nil for better error handling
169+
return @"#FFFFFF";
171170
}
172171

173172
int red = (int)lroundf(r * 255.0f);

src/EnrichedTextInput.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export interface EnrichedTextInputInstance extends NativeMethods {
6060
text: string,
6161
attributes?: Record<string, string>
6262
) => void;
63-
toggleColor: (color: string) => void;
63+
setColor: (color: string) => void;
64+
removeColor: () => void;
6465
}
6566

6667
export interface OnChangeMentionEvent {
@@ -299,8 +300,11 @@ export const EnrichedTextInput = ({
299300

300301
Commands.startMention(nullthrows(nativeRef.current), indicator);
301302
},
302-
toggleColor: (color: string) => {
303-
Commands.toggleColor(nullthrows(nativeRef.current), color);
303+
setColor: (color: string) => {
304+
Commands.setColor(nullthrows(nativeRef.current), color);
305+
},
306+
removeColor: () => {
307+
Commands.removeColor(nullthrows(nativeRef.current));
304308
},
305309
}));
306310

src/EnrichedTextInputNativeComponent.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,8 @@ interface NativeCommands {
209209
text: string,
210210
payload: string
211211
) => void;
212-
toggleColor: (
213-
viewRef: React.ElementRef<ComponentType>,
214-
color: string
215-
) => void;
212+
setColor: (viewRef: React.ElementRef<ComponentType>, color: string) => void;
213+
removeColor: (viewRef: React.ElementRef<ComponentType>) => void;
216214
}
217215

218216
export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
@@ -239,7 +237,8 @@ export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
239237
'addImage',
240238
'startMention',
241239
'addMention',
242-
'toggleColor',
240+
'setColor',
241+
'removeColor',
243242
],
244243
});
245244

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export type {
66
OnLinkDetected,
77
OnMentionDetected,
88
OnChangeSelectionEvent,
9+
OnChangeColorEvent,
910
} from './EnrichedTextInputNativeComponent';

0 commit comments

Comments
 (0)