feat: SearchQuery.filterResult option lets you filter search results further (e.g. checking Marks) #4

Closed
massifrg wants to merge 2 commits from dev into main
massifrg commented 2024-06-24 17:17:18 +02:00 (Migrated from github.com)

I added an optional filter on results, that work the same way of checkWordBoundary.

In particular I wanted to search for a text that also has some Marks.

I changed the internal checkResult function -- originally intended only for the wholeWord option -- this way:

  checkResult(state: EditorState, result: SearchResult) {
    let ok = this.wholeWord
      ? checkWordBoundary(state, result.from) &&
      checkWordBoundary(state, result.to)
      : true;
    if (!ok)
      return false
    if (this.filterResult)
      ok = this.filterResult(state, result)
    return ok
  }

This are two filters that I use to check if the searched text has one or all the Marks in a set:

const allMarksFilter: (marks: Mark[]) => SearchResultFilter = (marks) => (state, result) => {
  if (marks) {
    const { from, to } = result
    return marks.every(mark => state.doc.rangeHasMark(from, to, mark))
  }
  return true
}

const oneOfMarksFilter: (marks: Mark[]) => SearchResultFilter = (marks) => (state, result) => {
  if (marks) {
    const { from, to } = result
    return !!marks.find(mark => state.doc.rangeHasMark(from, to, mark))
  }
  return true
}
I added an optional filter on results, that work the same way of `checkWordBoundary`. In particular I wanted to search for a text that also has some `Mark`s. I changed the internal `checkResult` function -- originally intended only for the `wholeWord` option -- this way: ```ts checkResult(state: EditorState, result: SearchResult) { let ok = this.wholeWord ? checkWordBoundary(state, result.from) && checkWordBoundary(state, result.to) : true; if (!ok) return false if (this.filterResult) ok = this.filterResult(state, result) return ok } ``` This are two filters that I use to check if the searched text has one or all the `Mark`s in a set: ```ts const allMarksFilter: (marks: Mark[]) => SearchResultFilter = (marks) => (state, result) => { if (marks) { const { from, to } = result return marks.every(mark => state.doc.rangeHasMark(from, to, mark)) } return true } const oneOfMarksFilter: (marks: Mark[]) => SearchResultFilter = (marks) => (state, result) => { if (marks) { const { from, to } = result return !!marks.find(mark => state.doc.rangeHasMark(from, to, mark)) } return true } ```
marijnh commented 2024-06-25 08:45:13 +02:00 (Migrated from github.com)

You have an issue where your autoformatter ran wild on the code and you apparently committed all its spurious changes.

You have an issue where your autoformatter ran wild on the code and you apparently committed all its spurious changes.
massifrg commented 2024-06-25 09:28:02 +02:00 (Migrated from github.com)

I was afraid of that.

I'll try to get my dev environment conforming to the contributing guidelines. Sorry for the waste of time.
I'm closing this PR, I'll try to be back with a more conformant one.

Please, just tell me if you find the feature useful.

I was afraid of that. I'll try to get my dev environment conforming to the contributing guidelines. Sorry for the waste of time. I'm closing this PR, I'll try to be back with a more conformant one. Please, just tell me if you find the feature useful.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
prosemirror/prosemirror-search!4
No description provided.