Fix types and console.log
This commit is contained in:
parent
be3a66b0e6
commit
f5efa85882
2 changed files with 20 additions and 12 deletions
|
@ -18,8 +18,10 @@ import { useCallback, useEffect, useRef } from "react";
|
||||||
|
|
||||||
import useFocus from "../../../../../hooks/useFocus";
|
import useFocus from "../../../../../hooks/useFocus";
|
||||||
|
|
||||||
|
type SubSelection = Pick<Selection, 'anchorNode' | 'anchorOffset' | 'focusNode' | 'focusOffset'>;
|
||||||
|
|
||||||
export function useSelection() {
|
export function useSelection() {
|
||||||
const selectionRef = useRef({
|
const selectionRef = useRef<SubSelection>({
|
||||||
anchorNode: null,
|
anchorNode: null,
|
||||||
anchorOffset: 0,
|
anchorOffset: 0,
|
||||||
focusNode: null,
|
focusNode: null,
|
||||||
|
@ -30,6 +32,8 @@ export function useSelection() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function onSelectionChange() {
|
function onSelectionChange() {
|
||||||
const selection = document.getSelection();
|
const selection = document.getSelection();
|
||||||
|
|
||||||
|
if (selection) {
|
||||||
selectionRef.current = {
|
selectionRef.current = {
|
||||||
anchorNode: selection.anchorNode,
|
anchorNode: selection.anchorNode,
|
||||||
anchorOffset: selection.anchorOffset,
|
anchorOffset: selection.anchorOffset,
|
||||||
|
@ -37,6 +41,7 @@ export function useSelection() {
|
||||||
focusOffset: selection.focusOffset,
|
focusOffset: selection.focusOffset,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isFocused) {
|
if (isFocused) {
|
||||||
document.addEventListener('selectionchange', onSelectionChange);
|
document.addEventListener('selectionchange', onSelectionChange);
|
||||||
|
@ -47,10 +52,14 @@ export function useSelection() {
|
||||||
|
|
||||||
const selectPreviousSelection = useCallback(() => {
|
const selectPreviousSelection = useCallback(() => {
|
||||||
const range = new Range();
|
const range = new Range();
|
||||||
range.setStart(selectionRef.current.anchorNode, selectionRef.current.anchorOffset);
|
const selection = selectionRef.current;
|
||||||
range.setEnd(selectionRef.current.focusNode, selectionRef.current.focusOffset);
|
|
||||||
document.getSelection().removeAllRanges();
|
if (selection.anchorNode && selection.focusNode) {
|
||||||
document.getSelection().addRange(range);
|
range.setStart(selection.anchorNode, selectionRef.current.anchorOffset);
|
||||||
|
range.setEnd(selection.focusNode, selectionRef.current.focusOffset);
|
||||||
|
document.getSelection()?.removeAllRanges();
|
||||||
|
document.getSelection()?.addRange(range);
|
||||||
|
}
|
||||||
}, [selectionRef]);
|
}, [selectionRef]);
|
||||||
|
|
||||||
return { ...focusProps, selectPreviousSelection };
|
return { ...focusProps, selectPreviousSelection };
|
||||||
|
|
|
@ -177,7 +177,6 @@ describe('SendWysiwygComposer', () => {
|
||||||
|
|
||||||
it('Should not has placeholder', async () => {
|
it('Should not has placeholder', async () => {
|
||||||
// When
|
// When
|
||||||
console.log('here');
|
|
||||||
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
|
customRender(jest.fn(), jest.fn(), false, isRichTextEnabled);
|
||||||
await waitFor(() => expect(screen.getByRole('textbox')).toHaveAttribute('contentEditable', "true"));
|
await waitFor(() => expect(screen.getByRole('textbox')).toHaveAttribute('contentEditable', "true"));
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue