(
props: ExplorerProps & { searchKey?: unknown; initialSearch?: string },
)
| 245 | * @returns The search controls and the original data tree. |
| 246 | */ |
| 247 | export function SearchableExplorer( |
| 248 | props: ExplorerProps & { searchKey?: unknown; initialSearch?: string }, |
| 249 | ) { |
| 250 | const theme = useTheme() |
| 251 | const themed = createThemed(theme) |
| 252 | const [term, setTerm] = createSignal('') |
| 253 | const [debouncedTerm, setDebouncedTerm] = createSignal('') |
| 254 | const clear = () => { |
| 255 | setTerm('') |
| 256 | setDebouncedTerm('') |
| 257 | } |
| 258 | const searchKey = createMemo(() => props.searchKey) |
| 259 | createEffect( |
| 260 | on(searchKey, () => { |
| 261 | const initial = props.initialSearch ?? '' |
| 262 | setTerm(initial) |
| 263 | setDebouncedTerm(initial.toLowerCase()) |
| 264 | }), |
| 265 | ) |
| 266 | createEffect(() => { |
| 267 | const next = term().toLowerCase() |
| 268 | if (!next) { |
| 269 | setDebouncedTerm('') |
| 270 | return |
| 271 | } |
| 272 | const timeout = setTimeout(() => setDebouncedTerm(next), 150) |
| 273 | onCleanup(() => clearTimeout(timeout)) |
| 274 | }) |
| 275 | const search = createMemo<ExplorerSearch | undefined>(() => { |
| 276 | const activeTerm = debouncedTerm() |
| 277 | return activeTerm |
| 278 | ? { term: activeTerm, match: searchExplorer(props.value, activeTerm) } |
| 279 | : undefined |
| 280 | }) |
| 281 | const [nativeHighlight, setNativeHighlight] = createSignal<SearchHighlight>() |
| 282 | let root!: HTMLDivElement |
| 283 | onMount(() => { |
| 284 | const view = root.ownerDocument.defaultView! |
| 285 | const registry = view.CSS.highlights |
| 286 | // Explorers share one highlight per window, so styles.css can style it by |
| 287 | // name. Each search adds and removes only its own ranges. |
| 288 | const getHighlight = () => { |
| 289 | let highlight = registry.get(HIGHLIGHT_NAME) |
| 290 | if (!highlight) { |
| 291 | highlight = new view.Highlight() |
| 292 | registry.set(HIGHLIGHT_NAME, highlight) |
| 293 | } |
| 294 | return highlight |
| 295 | } |
| 296 | createEffect( |
| 297 | on(search, () => { |
| 298 | getHighlight() |
| 299 | const ranges = new Set<Range>() |
| 300 | const highlight: SearchHighlight = { |
| 301 | get size() { |
| 302 | return ranges.size |
| 303 | }, |
| 304 | add(range) { |
nothing calls this directly
no test coverage detected