Skip to content

Commit 72ba0d2

Browse files
committed
fix: handle emoji characters when spliting strings
1 parent ece5357 commit 72ba0d2

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

src/draftConverter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ export class DraftConverter {
207207
// Split the text into groups by their range
208208
let currentRanges: (RawDraftEntityRange | RawDraftInlineStyleRange)[] = [];
209209
let currentText: string = "";
210-
for (let i = 0; i < options.block.text.length; i++) {
210+
const text = Array.from(options.block.text);
211+
for (let i = 0; i < text.length; i++) {
211212
let styles = stylesAtPosition[i] || [];
212213
if (
213214
styles.length !== currentRanges.length ||
@@ -219,7 +220,8 @@ export class DraftConverter {
219220
currentText = "";
220221
currentRanges = styles;
221222
}
222-
currentText += options.block.text[i];
223+
224+
currentText += text[i];
223225
}
224226

225227
if (currentText) {

test/draft-inputs.test.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,101 @@ test("draft-medium-complex", () => {
3636
const converter = new DraftConverter();
3737
expect(converter.convert(draftMediumComplex as any)).toMatchSnapshot();
3838
});
39+
40+
test("draft-medium-complex", () => {
41+
const converter = new DraftConverter();
42+
expect(
43+
converter.convert({
44+
blocks: [
45+
{
46+
key: "a5rh8",
47+
text: "🕺🏽 Krewe of Boo hosts a jazz second-line through the French Quarter at 3pm, followed by happy hour at Pat O'Brien's. (Details)",
48+
type: "unstyled",
49+
depth: 0,
50+
inlineStyleRanges: [
51+
{
52+
style: "BOLD",
53+
length: 12,
54+
offset: 3,
55+
},
56+
{
57+
style: "ITALIC",
58+
length: 7,
59+
offset: 118,
60+
},
61+
],
62+
entityRanges: [
63+
{
64+
offset: 118,
65+
length: 7,
66+
key: 0,
67+
},
68+
],
69+
data: {},
70+
},
71+
],
72+
// @ts-ignore
73+
entityMap: [
74+
{
75+
type: "LINK",
76+
mutability: "MUTABLE",
77+
data: {
78+
rel: "noreferrer",
79+
url: "https://www.kreweofboo.com/secondline",
80+
href: "https://www.kreweofboo.com/secondline",
81+
target: "_blank",
82+
},
83+
},
84+
],
85+
})
86+
).toEqual({
87+
type: "doc",
88+
content: [
89+
{
90+
type: "paragraph",
91+
content: [
92+
{
93+
type: "text",
94+
text: "🕺🏽 ",
95+
marks: [],
96+
},
97+
{
98+
type: "text",
99+
text: "Krewe of Boo",
100+
marks: [
101+
{
102+
type: "bold",
103+
},
104+
],
105+
},
106+
{
107+
type: "text",
108+
text: " hosts a jazz second-line through the French Quarter at 3pm, followed by happy hour at Pat O'Brien's. (",
109+
marks: [],
110+
},
111+
{
112+
type: "text",
113+
text: "Details",
114+
marks: [
115+
{
116+
type: "link",
117+
attrs: {
118+
href: "https://www.kreweofboo.com/secondline",
119+
target: "_blank",
120+
},
121+
},
122+
{
123+
type: "italic",
124+
},
125+
],
126+
},
127+
{
128+
type: "text",
129+
text: ")",
130+
marks: [],
131+
},
132+
],
133+
},
134+
],
135+
});
136+
});

0 commit comments

Comments
 (0)