Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 13a2677

Browse files
committed
Fix selection
1 parent d001dde commit 13a2677

File tree

7 files changed

+32
-11
lines changed

7 files changed

+32
-11
lines changed

src/components/views/rooms/wysiwyg_composer/components/PlainTextComposer.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import React, { MutableRefObject, ReactNode } from 'react';
1919
import { useComposerFunctions } from '../hooks/useComposerFunctions';
2020
import { usePlainTextInitialization } from '../hooks/usePlainTextInitialization';
2121
import { usePlainTextListeners } from '../hooks/usePlainTextListeners';
22+
import { useSetCursorPosition } from '../hooks/useSetCursorPosition';
2223
import { ComposerFunctions } from '../types';
2324
import { Editor } from "./Editor";
2425

@@ -40,6 +41,7 @@ export function PlainTextComposer({
4041
const { ref, onInput, onPaste, onKeyDown } = usePlainTextListeners(onChange, onSend);
4142
const composerFunctions = useComposerFunctions(ref);
4243
usePlainTextInitialization(initialContent, ref);
44+
useSetCursorPosition(disabled, ref);
4345

4446
return <div className={className} onInput={onInput} onPaste={onPaste} onKeyDown={onKeyDown}>
4547
<Editor ref={ref} disabled={disabled} />

src/components/views/rooms/wysiwyg_composer/components/WysiwygComposer.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ export const WysiwygComposer = memo(function WysiwygComposer(
4848
}
4949
}, [onChange, content, disabled]);
5050

51-
useSetCursorPosition(isWysiwygReady, ref);
51+
const isReady = isWysiwygReady && !disabled;
52+
useSetCursorPosition(!isReady, ref);
5253

5354
return (
5455
<div className={className}>
5556
<FormattingButtons composer={wysiwyg} formattingStates={formattingStates} />
56-
<Editor ref={ref} disabled={!isWysiwygReady || disabled} />
57+
<Editor ref={ref} disabled={!isReady} />
5758
{ children?.(ref, wysiwyg) }
5859
</div>
5960
);

src/components/views/rooms/wysiwyg_composer/hooks/useSetCursorPosition.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import { RefObject, useEffect } from "react";
1818

1919
import { setCursorPositionAtTheEnd } from "./utils";
2020

21-
export function useSetCursorPosition(isComposerReady: boolean, ref: RefObject<HTMLElement>) {
21+
export function useSetCursorPosition(disabled: boolean, ref: RefObject<HTMLElement>) {
22+
console.log(disabled, ref);
2223
useEffect(() => {
23-
if (ref.current && isComposerReady) {
24+
if (ref.current && !disabled) {
2425
setCursorPositionAtTheEnd(ref.current);
2526
}
26-
}, [ref, isComposerReady]);
27+
}, [ref, disabled]);
2728
}

src/components/views/rooms/wysiwyg_composer/hooks/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ export function setCursorPositionAtTheEnd(element: HTMLElement) {
4646
const range = document.createRange();
4747
range.selectNodeContents(element);
4848
range.collapse(false);
49-
const sel = document.getSelection();
50-
sel.removeAllRanges();
51-
sel.addRange(range);
49+
const selection = document.getSelection();
50+
selection.removeAllRanges();
51+
selection.addRange(range);
5252

5353
element.focus();
5454
}

test/components/views/rooms/wysiwyg_composer/EditWysiwygComposer-test.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ describe('EditWysiwygComposer', () => {
186186
it('Should focus when receiving an Action.FocusEditMessageComposer action', async () => {
187187
// Given we don't have focus
188188
customRender();
189+
screen.getByLabelText('Bold').focus();
189190
expect(screen.getByRole('textbox')).not.toHaveFocus();
190191

191192
// When we send the right action
@@ -201,6 +202,7 @@ describe('EditWysiwygComposer', () => {
201202
it('Should not focus when disabled', async () => {
202203
// Given we don't have focus and we are disabled
203204
customRender(true);
205+
screen.getByLabelText('Bold').focus();
204206
expect(screen.getByRole('textbox')).not.toHaveFocus();
205207

206208
// When we send an action that would cause us to get focus

test/components/views/rooms/wysiwyg_composer/SendWysiwygComposer-test.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ describe('SendWysiwygComposer', () => {
8181
it('Should focus when receiving an Action.FocusSendMessageComposer action', async () => {
8282
// Given we don't have focus
8383
customRender(jest.fn(), jest.fn());
84+
screen.getByLabelText('Bold').focus();
8485
expect(screen.getByRole('textbox')).not.toHaveFocus();
8586

8687
// When we send the right action
@@ -96,6 +97,7 @@ describe('SendWysiwygComposer', () => {
9697
it('Should focus and clear when receiving an Action.ClearAndFocusSendMessageComposer', async () => {
9798
// Given we don't have focus
9899
customRender(jest.fn(), jest.fn());
100+
screen.getByLabelText('Bold').focus();
99101
expect(screen.getByRole('textbox')).not.toHaveFocus();
100102

101103
// When we send the right action
@@ -112,6 +114,7 @@ describe('SendWysiwygComposer', () => {
112114
it('Should focus when receiving a reply_to_event action', async () => {
113115
// Given we don't have focus
114116
customRender(jest.fn(), jest.fn());
117+
screen.getByLabelText('Bold').focus();
115118
expect(screen.getByRole('textbox')).not.toHaveFocus();
116119

117120
// When we send the right action

test/components/views/rooms/wysiwyg_composer/components/WysiwygComposer-test.tsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@ describe('WysiwygComposer', () => {
7373

7474
const defaultRoomContext: IRoomState = getRoomContext(mockRoom, {});
7575

76-
const customRender = (onChange = (_content: string) => void 0, onSend = () => void 0, disabled = false) => {
76+
const customRender = (
77+
onChange = (_content: string) => void 0,
78+
onSend = () => void 0,
79+
disabled = false,
80+
initialContent?: string) => {
7781
return render(
7882
<MatrixClientContext.Provider value={mockClient}>
7983
<RoomContext.Provider value={defaultRoomContext}>
80-
<WysiwygComposer onChange={onChange} onSend={onSend} disabled={disabled} />
84+
<WysiwygComposer onChange={onChange} onSend={onSend} disabled={disabled} initialContent={initialContent} />
8185
</RoomContext.Provider>
8286
</MatrixClientContext.Provider>,
8387
);
@@ -91,6 +95,14 @@ describe('WysiwygComposer', () => {
9195
expect(screen.getByRole('textbox')).toHaveAttribute('contentEditable', "false");
9296
});
9397

98+
it('Should have focus', () => {
99+
// When
100+
customRender(jest.fn(), jest.fn(), false);
101+
102+
// Then
103+
expect(screen.getByRole('textbox')).toHaveFocus();
104+
});
105+
94106
it('Should call onChange handler', (done) => {
95107
const html = '<b>html</b>';
96108
customRender((content) => {
@@ -104,7 +116,7 @@ describe('WysiwygComposer', () => {
104116
const onSend = jest.fn();
105117
customRender(jest.fn(), onSend);
106118

107-
// When we tell its inputEventProcesser that the user pressed Enter
119+
// When we tell its inputEventProcessor that the user pressed Enter
108120
const event = new InputEvent("insertParagraph", { inputType: "insertParagraph" });
109121
const wysiwyg = { actions: { clear: () => {} } } as Wysiwyg;
110122
inputEventProcessor(event, wysiwyg);

0 commit comments

Comments
 (0)