import { useState, useEffect } from "../preact/hooks.ts"; import { takeSelection, Range } from "../scrapbox-userscript-std/dom.ts"; export const useSelection = (): { text: string; range: Range; } => { const [range, setRange] = useState({ start: { line: 0, char: 0 }, end: { line: 0, char: 0 } }); const [text, setText] =useState(""); useEffect(() => { const selection = takeSelection(); const update = () => { setRange(selection.getRange()); setText(selection.getSelectedText()); }; selection.addChangeListener(update); return () => selection.removeChangeListener(update); }, []); return { text, range } as const; };