Skip to content

Commit c03149a

Browse files
duom青源duom青源
authored andcommitted
feat: change data structure
1 parent ead5d83 commit c03149a

File tree

2 files changed

+61
-46
lines changed

2 files changed

+61
-46
lines changed

example/src/App.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import {
77
ImageResolvedAssetSource,
88
Image,
99
View,
10-
processColor,
1110
} from 'react-native';
1211
import VariableTextInputView, {
1312
IATTextViewBase,
13+
IInserTextAttachmentItem,
14+
ITextType,
1415
} from 'react-native-variable-text-input';
1516
export const App = () => {
1617
const inPutRef = React.createRef<IATTextViewBase>();
@@ -21,7 +22,11 @@ export const App = () => {
2122
const data: ImageResolvedAssetSource = Image.resolveAssetSource(
2223
require('./[苦笑].png')
2324
);
24-
inPutRef.current?.insertEmoji({ img: data, tag: '[苦笑]' });
25+
inPutRef.current?.insertEmoji({
26+
img: data,
27+
emojiTag: '[苦笑]',
28+
type: ITextType.emoji,
29+
});
2530
};
2631
const blur = () => {
2732
inPutRef.current?.blur();
@@ -32,25 +37,25 @@ export const App = () => {
3237
name: '测试tag',
3338
color: 'red',
3439
id: '123344',
40+
type: ITextType.tagText,
3541
});
3642
};
3743
const changeAttributedText = () => {
3844
const imageData: ImageResolvedAssetSource = Image.resolveAssetSource(
3945
require('./[苦笑].png')
4046
);
41-
const emojiData = { img: imageData, tag: '[苦笑]' };
47+
const emojiData = { img: imageData, emojiTag: '[苦笑]' };
48+
const tagData: IInserTextAttachmentItem = {
49+
tag: '#',
50+
name: '测试tag',
51+
color: 'red',
52+
id: '123344',
53+
type: ITextType.tagText,
54+
};
4255
inPutRef.current?.changeAttributedText([
4356
{ type: 0, text: '普通字符' },
44-
{ type: 1, emojiData },
45-
{
46-
type: 2,
47-
targData: {
48-
tag: '#',
49-
name: '测试tag',
50-
color: processColor('red'),
51-
id: '123344',
52-
},
53-
},
57+
{ type: 1, ...emojiData },
58+
tagData,
5459
]);
5560
};
5661
const focus = () => {
@@ -108,7 +113,7 @@ const styles = StyleSheet.create({
108113
box: {
109114
backgroundColor: 'blue',
110115
color: '#fff',
111-
fontSize: 18,
116+
fontSize: 10,
112117
width: '100%',
113118
minHeight: 100,
114119
padding: 0,

src/index.tsx

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,25 @@ export enum ITextType {
3535
normal = 0,
3636
tagText = 2,
3737
}
38+
interface PrivateItemData {
39+
type: ITextType;
40+
text?: string;
41+
color?: ProcessedColorValue | null | undefined;
42+
tag?: '@' | '#';
43+
name?: string;
44+
id?: string;
45+
img?: ImageResolvedAssetSource; //emoji图片
46+
emojiTag?: string; //[微笑] //emojitag
47+
}
3848
export interface IInserTextAttachmentItem {
3949
type: ITextType;
40-
targData?: ISendTagMensage;
41-
emojiData?: IInsertEmojiConfig;
4250
text?: string;
51+
color?: ColorValue;
52+
tag?: '@' | '#';
53+
name?: string;
54+
id?: string;
55+
img?: ImageResolvedAssetSource; //emoji图片
56+
emojiTag?: string; //[微笑] //emojitag
4357
}
4458
interface IProps {
4559
onMention?: () => void;
@@ -63,27 +77,11 @@ interface IProps {
6377
e: NativeSyntheticEvent<TextInputContentSizeChangeEventData>
6478
) => void;
6579
}
66-
interface IInsertTagConfig {
67-
tag: '@' | '#';
68-
name: string;
69-
id: string;
70-
color: ColorValue;
71-
}
72-
interface ISendTagMensage {
73-
color: ProcessedColorValue | null | undefined;
74-
tag: '@' | '#';
75-
name: string;
76-
id: string;
77-
}
78-
interface IInsertEmojiConfig {
79-
img: ImageResolvedAssetSource;
80-
tag: string; //[微笑]
81-
}
8280
export type IATTextViewBase = {
8381
focus: () => void;
8482
blur: () => void;
85-
insertEmoji: (img: IInsertEmojiConfig) => void;
86-
insertMentions: (data: IInsertTagConfig) => void;
83+
insertEmoji: (img: IInserTextAttachmentItem) => void;
84+
insertMentions: (data: IInserTextAttachmentItem) => void;
8785
changeAttributedText: (data: IInserTextAttachmentItem[]) => void;
8886
};
8987
export type IATTextViewRef = React.ForwardedRef<IATTextViewBase>;
@@ -119,34 +117,46 @@ const VariableTextInputView = forwardRef(
119117
UIManager.dispatchViewManagerCommand(
120118
reactTag,
121119
commandId,
122-
!!data ? [data] : []
120+
!!data ? data : []
123121
);
124122
};
125-
const insertEmoji = (data: IInsertEmojiConfig) => {
123+
const insertEmoji = (data: IInserTextAttachmentItem) => {
124+
const sendData: PrivateItemData = {
125+
...data,
126+
color: processColor(data.color),
127+
};
126128
if (Platform.OS === 'android') {
127-
callNativeMethod('insertEmoji', data);
129+
callNativeMethod('insertEmoji', [sendData]);
128130
} else {
129-
VariableTextInputViewManager.insertEmoji(data);
131+
VariableTextInputViewManager.insertEmoji(sendData);
130132
}
131133
};
132-
const insertMentions = (data: IInsertTagConfig) => {
133-
const sendData: ISendTagMensage = {
134+
const insertMentions = (data: IInserTextAttachmentItem) => {
135+
const sendData: PrivateItemData = {
136+
...data,
134137
color: processColor(data.color),
135-
tag: data.tag,
136-
name: data.name,
137-
id: data.id,
138138
};
139139
if (Platform.OS === 'ios') {
140140
VariableTextInputViewManager.insertMentions(sendData);
141141
} else {
142-
callNativeMethod('insertMentions', sendData);
142+
callNativeMethod('insertMentions', [sendData]);
143143
}
144144
};
145145
const changeAttributedText = (data: IInserTextAttachmentItem[]) => {
146+
const sendData: PrivateItemData[] = [];
147+
if (data.length > 0) {
148+
data.forEach((item) => {
149+
const newItem: PrivateItemData = {
150+
...item,
151+
color: processColor(item.color),
152+
};
153+
sendData.push(newItem);
154+
});
155+
}
146156
if (Platform.OS === 'android') {
147-
callNativeMethod('changeAttributedText', data);
157+
callNativeMethod('changeAttributedText', sendData);
148158
} else {
149-
VariableTextInputViewManager.changeAttributedText(data);
159+
VariableTextInputViewManager.changeAttributedText(sendData);
150160
}
151161
};
152162
const onContentSizeChange = (event: any) => {

0 commit comments

Comments
 (0)