Support multiple lines
parent
7fcc65a3fe
commit
5db885e337
|
@ -34,8 +34,7 @@ export const Editor = memo(
|
|||
function Editor({ disabled, placeholder, leftComponent, rightComponent }: EditorProps, ref,
|
||||
) {
|
||||
const isExpanded = useIsExpanded(ref as MutableRefObject<HTMLDivElement | null>, HEIGHT_BREAKING_POINT);
|
||||
const { onFocus, onBlur, selectPreviousSelection } =
|
||||
useSelection(ref as MutableRefObject<HTMLDivElement | null>);
|
||||
const { onFocus, onBlur, selectPreviousSelection } = useSelection();
|
||||
|
||||
return <div
|
||||
data-testid="WysiwygComposerEditor"
|
||||
|
|
|
@ -14,13 +14,15 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { RefObject, useCallback, useEffect, useRef } from "react";
|
||||
import { useCallback, useEffect, useRef } from "react";
|
||||
|
||||
import useFocus from "../../../../../hooks/useFocus";
|
||||
|
||||
export function useSelection(ref: RefObject<HTMLDivElement>) {
|
||||
export function useSelection() {
|
||||
const selectionRef = useRef({
|
||||
anchorNode: null,
|
||||
anchorOffset: 0,
|
||||
focusNode: null,
|
||||
focusOffset: 0,
|
||||
});
|
||||
const [isFocused, focusProps] = useFocus();
|
||||
|
@ -28,9 +30,10 @@ export function useSelection(ref: RefObject<HTMLDivElement>) {
|
|||
useEffect(() => {
|
||||
function onSelectionChange() {
|
||||
const selection = document.getSelection();
|
||||
console.log('selection', selection);
|
||||
selectionRef.current = {
|
||||
anchorNode: selection.anchorNode,
|
||||
anchorOffset: selection.anchorOffset,
|
||||
focusNode: selection.focusNode,
|
||||
focusOffset: selection.focusOffset,
|
||||
};
|
||||
}
|
||||
|
@ -44,11 +47,11 @@ export function useSelection(ref: RefObject<HTMLDivElement>) {
|
|||
|
||||
const selectPreviousSelection = useCallback(() => {
|
||||
const range = new Range();
|
||||
range.setStart(ref.current.firstChild, selectionRef.current.anchorOffset);
|
||||
range.setEnd(ref.current.firstChild, selectionRef.current.focusOffset);
|
||||
range.setStart(selectionRef.current.anchorNode, selectionRef.current.anchorOffset);
|
||||
range.setEnd(selectionRef.current.focusNode, selectionRef.current.focusOffset);
|
||||
document.getSelection().removeAllRanges();
|
||||
document.getSelection().addRange(range);
|
||||
}, [selectionRef, ref]);
|
||||
}, [selectionRef]);
|
||||
|
||||
return { ...focusProps, selectPreviousSelection };
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue