Skip to content

Commit 625d16c

Browse files
authored
fix: calculate TextComposer selection upon text insertion correctly (#1608)
1 parent b13de81 commit 625d16c

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/messageComposer/textComposer.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,13 @@ export class TextComposer {
212212
text,
213213
currentText.slice(finalSelection.end),
214214
].join('');
215+
215216
const finalText = textBeforeTrim.slice(
216217
0,
217218
typeof maxLengthOnEdit === 'number' ? maxLengthOnEdit : textBeforeTrim.length,
218219
);
219-
const expectedCursorPosition =
220-
currentText.slice(0, finalSelection.start).length + text.length;
221-
const cursorPosition =
222-
expectedCursorPosition >= finalText.length
223-
? finalText.length
224-
: currentText.slice(0, expectedCursorPosition).length;
220+
const expectedCursorPosition = finalSelection.start + text.length;
221+
const cursorPosition = Math.min(expectedCursorPosition, finalText.length);
225222

226223
await this.handleChange({
227224
text: finalText,

test/unit/MessageComposer/textComposer.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ describe('TextComposer', () => {
501501
selection: { start: 5, end: 5 },
502502
});
503503
expect(textComposer.text).toBe('Hello beautiful world');
504+
expect(textComposer.selection).toStrictEqual({ start: 15, end: 15 });
504505
});
505506

506507
it('should insert text at the end if no selection provided', async () => {
@@ -509,6 +510,7 @@ describe('TextComposer', () => {
509510
} = setup({ composition: message });
510511
await textComposer.insertText({ text: '!' });
511512
expect(textComposer.text).toBe('Hello world!');
513+
expect(textComposer.selection).toStrictEqual({ start: 12, end: 12 });
512514
});
513515

514516
it('should respect maxLengthOnEdit', async () => {
@@ -525,6 +527,7 @@ describe('TextComposer', () => {
525527
});
526528
await textComposer.insertText({ text: ' beautiful world' });
527529
expect(textComposer.text).toBe('Hello be');
530+
expect(textComposer.selection).toStrictEqual({ start: 8, end: 8 });
528531
});
529532

530533
it('should handle empty text insertion', async () => {
@@ -533,6 +536,7 @@ describe('TextComposer', () => {
533536
} = setup({ composition: message });
534537
await textComposer.insertText({ text: '', selection: { start: 5, end: 5 } });
535538
expect(textComposer.text).toBe('Hello world');
539+
expect(textComposer.selection).toStrictEqual({ start: 5, end: 5 });
536540
});
537541

538542
it('should handle insertion at the start of text', async () => {
@@ -541,6 +545,7 @@ describe('TextComposer', () => {
541545
} = setup({ composition: message });
542546
await textComposer.insertText({ text: 'Hi ', selection: { start: 0, end: 0 } });
543547
expect(textComposer.text).toBe('Hi Hello world');
548+
expect(textComposer.selection).toStrictEqual({ start: 3, end: 3 });
544549
});
545550

546551
it('should handle insertion at end of text', async () => {
@@ -549,6 +554,7 @@ describe('TextComposer', () => {
549554
} = setup({ composition: message });
550555
await textComposer.insertText({ text: '!', selection: { start: 11, end: 11 } });
551556
expect(textComposer.text).toBe('Hello world!');
557+
expect(textComposer.selection).toStrictEqual({ start: 12, end: 12 });
552558
});
553559

554560
it('should handle insertion with multi-character selection', async () => {
@@ -557,6 +563,7 @@ describe('TextComposer', () => {
557563
} = setup({ composition: message });
558564
await textComposer.insertText({ text: 'Hi', selection: { start: 0, end: 5 } });
559565
expect(textComposer.text).toBe('Hi world');
566+
expect(textComposer.selection).toStrictEqual({ start: 2, end: 2 });
560567
});
561568

562569
it('should handle insertion with multi-character selection and maxLengthOnEdit restricting the size', async () => {
@@ -577,6 +584,7 @@ describe('TextComposer', () => {
577584
selection: { start: 7, end: 9 },
578585
});
579586
expect(textComposer.text).toBe('Hello wHi ');
587+
expect(textComposer.selection).toStrictEqual({ start: 10, end: 10 });
580588
});
581589

582590
it('should not insert text if disabled', async () => {
@@ -588,6 +596,7 @@ describe('TextComposer', () => {
588596
selection: { start: 5, end: 5 },
589597
});
590598
expect(textComposer.text).toBe(message.text);
599+
expect(textComposer.selection).toStrictEqual({ start: 11, end: 11 });
591600
});
592601

593602
it('should reflect pasting of command trigger with partial command name', async () => {
@@ -597,6 +606,7 @@ describe('TextComposer', () => {
597606
await textComposer.insertText({ text: '/giph', selection: { start: 0, end: 11 } });
598607
expect(textComposer.text).toBe('/giph');
599608
expect(textComposer.suggestions).toBeDefined();
609+
expect(textComposer.selection).toStrictEqual({ start: 5, end: 5 });
600610
});
601611
});
602612

0 commit comments

Comments
 (0)