Add test for plain mode

This commit is contained in:
Florian Duros 2022-10-26 17:16:13 +02:00
parent f1610dae3d
commit fb751c3c7b
No known key found for this signature in database
GPG key ID: 9700AA5870258A0B
7 changed files with 213 additions and 107 deletions

View file

@ -43,7 +43,13 @@ export function PlainTextComposer({
usePlainTextInitialization(initialContent, ref);
useSetCursorPosition(disabled, ref);
return <div className={className} onInput={onInput} onPaste={onPaste} onKeyDown={onKeyDown}>
return <div
data-testid="PlainTextComposer"
className={className}
onInput={onInput}
onPaste={onPaste}
onKeyDown={onKeyDown}
>
<Editor ref={ref} disabled={disabled} />
{ children?.(ref, composerFunctions) }
</div>;

View file

@ -52,7 +52,7 @@ export const WysiwygComposer = memo(function WysiwygComposer(
useSetCursorPosition(!isReady, ref);
return (
<div className={className}>
<div data-testid="WysiwygComposer" className={className}>
<FormattingButtons composer={wysiwyg} formattingStates={formattingStates} />
<Editor ref={ref} disabled={!isReady} />
{ children?.(ref, wysiwyg) }

View file

@ -16,13 +16,10 @@ limitations under the License.
import { RefObject, useEffect } from "react";
import { setCursorPositionAtTheEnd } from "./utils";
export function usePlainTextInitialization(initialContent: string, ref: RefObject<HTMLElement>) {
useEffect(() => {
if (ref.current) {
ref.current.innerText = initialContent;
setCursorPositionAtTheEnd(ref.current);
}
}, [ref, initialContent]);
}

View file

@ -16,7 +16,7 @@ limitations under the License.
import { KeyboardEvent, SyntheticEvent, useCallback, useRef } from "react";
import { useInputEventProcessor } from "./useInputEventProcessor";
import { useSettingValue } from "../../../../../hooks/useSettings";
function isDivElement(target: EventTarget): target is HTMLDivElement {
return target instanceof HTMLDivElement;
@ -26,25 +26,25 @@ export function usePlainTextListeners(onChange: (content: string) => void, onSen
const ref = useRef<HTMLDivElement>();
const send = useCallback((() => {
if (ref.current) {
ref.current.innerText = '';
ref.current.innerHTML = '';
}
onSend();
}), [ref, onSend]);
const inputEventProcessor = useInputEventProcessor(send);
const onInput = useCallback((event: SyntheticEvent<HTMLDivElement, InputEvent | ClipboardEvent>) => {
if (isDivElement(event.target)) {
onChange(event.target.innerText);
onChange(event.target.innerHTML);
}
inputEventProcessor(event.nativeEvent);
}, [onChange, inputEventProcessor]);
}, [onChange]);
const isCtrlEnter = useSettingValue<boolean>("MessageComposerInput.ctrlEnterToSend");
const onKeyDown = useCallback((event: KeyboardEvent<HTMLDivElement>) => {
if (event.key === 'Enter') {
if (event.key === 'Enter' && !event.shiftKey && (!isCtrlEnter || (isCtrlEnter && event.ctrlKey))) {
event.preventDefault();
event.stopPropagation();
send();
}
}, [send]);
}, [isCtrlEnter, send]);
return { ref, onInput, onPaste: onInput, onKeyDown };
}