mirror of
https://github.com/outline/outline.git
synced 2026-06-13 03:14:59 +03:00
1d919cc56a
* fix: initialize text selection state * Delete app/hooks/useTextSelection.test.ts --------- Co-authored-by: Tom Moor <tom.moor@gmail.com>
31 lines
623 B
TypeScript
31 lines
623 B
TypeScript
import { useState } from "react";
|
|
import useEventListener from "./useEventListener";
|
|
|
|
/**
|
|
* Returns the current selected text in the document.
|
|
*
|
|
* @returns the current selected text.
|
|
*/
|
|
export function getSelectedText() {
|
|
return window.getSelection()?.toString() ?? "";
|
|
}
|
|
|
|
/**
|
|
* A hook that returns the currently selected text.
|
|
*
|
|
* @returns The selected text
|
|
*/
|
|
export default function useTextSelection() {
|
|
const [selection, setSelection] = useState(getSelectedText);
|
|
|
|
useEventListener(
|
|
"selectionchange",
|
|
() => {
|
|
setSelection(getSelectedText());
|
|
},
|
|
document
|
|
);
|
|
|
|
return selection;
|
|
}
|