Pass inital search query to search panel

Let’s say I have a global search to search all my stored texts and a local search so search only the currently opened text. The latter can easily be realized with CM6’s own search panel.

I’ve implemented the former with my own search algorithm. Now I want to pass the search query from the global search to the CM6 search once the editor is opened.

I tried passing the search query vie GET parameter and then calling view.dispatch like this:

view.dispatch({
    effects: setSearchQuery.of(new SearchQuery({
        search: queryStringFromGetParamater,
    }))
})

However, this doesn’t work. If I hit Mod+G or Mod+G en empty search panel shows up. If I dispatch the effect above again after the search panel has been opened, the query is applied.

It seems like opening the panel for the first time somehow resets the query.

I think what’s happening is that the search extension hasn’t been initialized yet when you dispatch the setSearchQuery effect, so nothing is listening to it, and it gets ignored.

@codemirror/search 0.19.7 exports a search() function that you can use to initialize the search state right when you create your editor state. That should help with this.

1 Like

Oh, yeah, cool. Updated to 0.19.7 and the queries are applied correctly. However, the search panel is open from the very beginning, no matter if there’s an initial search query or not. Is this intended?

No, that was definitely not intended. @codemirror/search 0.19.8 fixes it.

1 Like

It works! Awesome support, as always! Thanks for this insanely fast implementation, Marijn!