Skip to content
GitHub

Search

Search and replace text within the editor. This is built on top of prosemirror-search.

import 'prosekit/basic/style.css'
import 'prosekit/basic/typography.css'
import 'prosekit/extensions/search/style.css'

import { createEditor } from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'

import { defineExtension } from './extension'
import Search from './search'

export default function Editor() {
  const editor = useMemo(() => {
    const extension = defineExtension()
    return createEditor({
      extension,
      defaultContent: '<p>Baa, baa, black sheep,</p>'
        + '<p>Have you any wool?</p>'
        + '<p>Yes, sir, yes, sir,</p>'
        + '<p>Three bags full;</p>'
        + '<p>One for the master,</p>'
        + '<p>And one for the dame,</p>'
        + '<p>And one for the little boy</p>'
        + '<p>Who lives down the lane.</p>',
    })
  }, [])

  return (
    <ProseKit editor={editor}>
      <div className='box-border h-full w-full min-h-36 overflow-y-hidden overflow-x-hidden rounded-md border border-solid border-gray-200 dark:border-gray-700 shadow flex flex-col bg-white dark:bg-gray-950 color-black dark:color-white'>
        <div className='relative w-full flex-1 box-border overflow-y-scroll'>
          <Search />
          <div ref={editor.mount} className='ProseMirror box-border min-h-full px-[max(4rem,_calc(50%-20rem))] py-8 outline-none outline-0 [&_span[data-mention="user"]]:text-blue-500 [&_span[data-mention="tag"]]:text-violet-500'></div>
        </div>
      </div>
    </ProseKit>
  )
}

To highlight search matches, you must load the style.css file or define your own styles for the .ProseMirror-search-match (search match) and .ProseMirror-active-search-match (active match) classes.

import 'prosekit/extensions/search/style.css'

Call defineSearchCommands() to define related commands.

import { defineSearchCommandsfunction defineSearchCommands(): SearchCommandsExtension
Defines commands for search and replace.
@public
} from 'prosekit/extensions/search'
const extensionconst extension: SearchCommandsExtension = defineSearchCommandsfunction defineSearchCommands(): SearchCommandsExtension
Defines commands for search and replace.
@public
()

In your search component, when the search text and replace text change, you should create a new extension with the specified text. Typically, you will need to call the useExtension() function from various framework bindings.

import { defineSearchQueryfunction defineSearchQuery(options: SearchQueryOptions): PlainExtension
Defines an extension that stores a current search query and replace string.
@public
} from 'prosekit/extensions/search'
const extensionconst extension: PlainExtension = defineSearchQueryfunction defineSearchQuery(options: SearchQueryOptions): PlainExtension
Defines an extension that stores a current search query and replace string.
@public
({ searchSearchQueryOptions.search: string
The search string (or regular expression).
: 'foo', replaceSearchQueryOptions.replace?: string | undefined
The replace text.
: 'bar' })

Find the next instance of the search query after the current selection and move the selection to it.

editorconst editor: Editor<SearchCommandsExtension>.commands
Editor<SearchCommandsExtension>.commands: ToCommandAction<{
    findNext: [];
    findPrev: [];
    findNextNoWrap: [];
    findPrevNoWrap: [];
    replaceNext: [];
    replaceNextNoWrap: [];
    replaceCurrent: [];
    replaceAll: [];
}>
All {@link CommandAction } s defined by the editor.
.findNext
findNext: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Find the previous instance of the search query and move the selection to it.

editorconst editor: Editor<SearchCommandsExtension>.commands
Editor<SearchCommandsExtension>.commands: ToCommandAction<{
    findNext: [];
    findPrev: [];
    findNextNoWrap: [];
    findPrevNoWrap: [];
    replaceNext: [];
    replaceNextNoWrap: [];
    replaceCurrent: [];
    replaceAll: [];
}>
All {@link CommandAction } s defined by the editor.
.findPrev
findPrev: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Find the next instance of the search query and move the selection to it. Don’t wrap around at the end of document or search range.

editorconst editor: Editor<SearchCommandsExtension>.commands
Editor<SearchCommandsExtension>.commands: ToCommandAction<{
    findNext: [];
    findPrev: [];
    findNextNoWrap: [];
    findPrevNoWrap: [];
    replaceNext: [];
    replaceNextNoWrap: [];
    replaceCurrent: [];
    replaceAll: [];
}>
All {@link CommandAction } s defined by the editor.
.findNextNoWrap
findNextNoWrap: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Find the previous instance of the search query and move the selection to it. Don’t wrap at the start of the document or search range.

editorconst editor: Editor<SearchCommandsExtension>.commands
Editor<SearchCommandsExtension>.commands: ToCommandAction<{
    findNext: [];
    findPrev: [];
    findNextNoWrap: [];
    findPrevNoWrap: [];
    replaceNext: [];
    replaceNextNoWrap: [];
    replaceCurrent: [];
    replaceAll: [];
}>
All {@link CommandAction } s defined by the editor.
.findPrevNoWrap
findPrevNoWrap: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Replace the currently selected instance of the search query, and move to the next one. Or select the next match, if none is already selected.

editorconst editor: Editor<SearchCommandsExtension>.commands
Editor<SearchCommandsExtension>.commands: ToCommandAction<{
    findNext: [];
    findPrev: [];
    findNextNoWrap: [];
    findPrevNoWrap: [];
    replaceNext: [];
    replaceNextNoWrap: [];
    replaceCurrent: [];
    replaceAll: [];
}>
All {@link CommandAction } s defined by the editor.
.replaceNext
replaceNext: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Replace the next instance of the search query. Don’t wrap around at the end of the document.

editorconst editor: Editor<SearchCommandsExtension>.commands
Editor<SearchCommandsExtension>.commands: ToCommandAction<{
    findNext: [];
    findPrev: [];
    findNextNoWrap: [];
    findPrevNoWrap: [];
    replaceNext: [];
    replaceNextNoWrap: [];
    replaceCurrent: [];
    replaceAll: [];
}>
All {@link CommandAction } s defined by the editor.
.replaceNextNoWrap
replaceNextNoWrap: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Replace the currently selected instance of the search query, if any, and keep it selected.

editorconst editor: Editor<SearchCommandsExtension>.commands
Editor<SearchCommandsExtension>.commands: ToCommandAction<{
    findNext: [];
    findPrev: [];
    findNextNoWrap: [];
    findPrevNoWrap: [];
    replaceNext: [];
    replaceNextNoWrap: [];
    replaceCurrent: [];
    replaceAll: [];
}>
All {@link CommandAction } s defined by the editor.
.replaceCurrent
replaceCurrent: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()

Replace all instances of the search query.

editorconst editor: Editor<SearchCommandsExtension>.commands
Editor<SearchCommandsExtension>.commands: ToCommandAction<{
    findNext: [];
    findPrev: [];
    findNextNoWrap: [];
    findPrevNoWrap: [];
    replaceNext: [];
    replaceNextNoWrap: [];
    replaceCurrent: [];
    replaceAll: [];
}>
All {@link CommandAction } s defined by the editor.
.replaceAll
replaceAll: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()