Skip to content
GitHubDiscord

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,
  type NodeJSON,
} from 'prosekit/core'
import { ProseKit } from 'prosekit/react'
import { useMemo } from 'react'

import { sampleContent } from '../../sample/sample-doc-search'
import { Search } from '../../ui/search'

import { defineExtension } from './extension'

interface EditorProps {
  initialContent?: NodeJSON
}

export default function Editor(props: EditorProps) {
  const defaultContent = props.initialContent ?? sampleContent
  const editor = useMemo(() => {
    const extension = defineExtension()
    return createEditor({
      extension,
      defaultContent,
    })
  }, [defaultContent])

  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-sm flex flex-col bg-white dark:bg-gray-950 text-black dark:text-white">
        <div className="relative w-full flex-1 box-border overflow-y-auto">
          <Search />
          <div ref={editor.mount} className="ProseMirror box-border min-h-full px-[max(4rem,calc(50%-20rem))] py-8 outline-hidden 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`.
()