Skip to content

Search

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

'use client'

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.ts'
import { Search } from '../../ui/search/index.ts'

import { defineExtension } from './extension.ts'

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-[canvas] 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 defineSearchQuery() to store the search state, and defineSearchCommands() to define related commands.

import { unionfunction union<const E extends readonly Extension[]>(...exts: E): Union<E> (+1 overload)
Merges multiple extensions into one. You can pass multiple extensions as arguments or a single array containing multiple extensions. Throws if no extensions are provided.
@example```ts function defineFancyNodes() { return union( defineFancyParagraph(), defineFancyHeading(), ) } ```@example```ts function defineFancyNodes() { return union([ defineFancyParagraph(), defineFancyHeading(), ]) } ```
} from 'prosekit/core'
import { defineSearchCommandsfunction defineSearchCommands(): SearchCommandsExtension
Defines commands for search and replace.
, defineSearchQueryfunction defineSearchQuery(options?: SearchQueryOptions): PlainExtension
Defines an extension that stores a current search query and replace string. When called without options, it stores an empty query, which can be updated later with the `setSearchQuery` command.
} from 'prosekit/extensions/search'
const extensionconst extension: Union<readonly [PlainExtension, SearchCommandsExtension]> = unionunion<readonly [PlainExtension, SearchCommandsExtension]>(exts_0: PlainExtension, exts_1: SearchCommandsExtension): Union<readonly [PlainExtension, SearchCommandsExtension]> (+1 overload)
Merges multiple extensions into one. You can pass multiple extensions as arguments or a single array containing multiple extensions. Throws if no extensions are provided.
@example```ts function defineFancyNodes() { return union( defineFancyParagraph(), defineFancyHeading(), ) } ```@example```ts function defineFancyNodes() { return union([ defineFancyParagraph(), defineFancyHeading(), ]) } ```
(defineSearchQueryfunction defineSearchQuery(options?: SearchQueryOptions): PlainExtension
Defines an extension that stores a current search query and replace string. When called without options, it stores an empty query, which can be updated later with the `setSearchQuery` command.
(), defineSearchCommandsfunction defineSearchCommands(): SearchCommandsExtension
Defines commands for search and replace.
())

In your search component, dispatch the setSearchQuery command whenever the search text or a search option changes. The command is a no-op when the query is unchanged, so you can dispatch it on every input change. You can also pass options to defineSearchQuery() to start with a fixed query.

Read the current match count with getSearchStatus(), or register a handler with defineSearchStatusHandler() to render a match counter.

import { createEditorfunction createEditor<E extends Extension>(options: EditorOptions<E>): Editor<E> } from 'prosekit/core'
import { defineSearchStatusHandlerfunction defineSearchStatusHandler(handler: SearchStatusHandler): PlainExtension
Registers a handler that is called whenever the search status changes. It can be used to render a match counter.
, getSearchStatusfunction getSearchStatus(state: EditorState): SearchStatus
Returns the current search status.
} from 'prosekit/extensions/search'
const extensionconst extension: PlainExtension = defineSearchStatusHandlerfunction defineSearchStatusHandler(handler: SearchStatusHandler): PlainExtension
Registers a handler that is called whenever the search status changes. It can be used to render a match counter.
((statusstatus: SearchStatus) => {
consolevar console: Console.logConsole.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
(`Match ${statusstatus: SearchStatus.activeSearchStatus.active: number
The one-based position of the match that the selection sits on, or 0 when the selection is not on a match.
} of ${statusstatus: SearchStatus.totalSearchStatus.total: number
The total number of matches for the current search query.
}`)
}) const editorconst editor: Editor<PlainExtension> = createEditorcreateEditor<PlainExtension>(options: EditorOptions<PlainExtension>): Editor<PlainExtension>({ extensionEditorOptions<PlainExtension>.extension: PlainExtension
The extension to use when creating the editor.
})
const statusconst status: SearchStatus = getSearchStatusfunction getSearchStatus(state: EditorState): SearchStatus
Returns the current search status.
(editorconst editor: Editor<PlainExtension>.stateEditor<PlainExtension>.state: EditorState
The editor's current state.
)

Update the search query and select the first match at or after the selection start, wrapping around to the first match in the document. When the query matches nothing, a selection left by a previous query collapses to its start. An empty query clears the highlights and leaves the selection alone.

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

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<{
    setSearchQuery: [options: SearchQueryOptions];
    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<{
    setSearchQuery: [options: SearchQueryOptions];
    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<{
    setSearchQuery: [options: SearchQueryOptions];
    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<{
    setSearchQuery: [options: SearchQueryOptions];
    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<{
    setSearchQuery: [options: SearchQueryOptions];
    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<{
    setSearchQuery: [options: SearchQueryOptions];
    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<{
    setSearchQuery: [options: SearchQueryOptions];
    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<{
    setSearchQuery: [options: SearchQueryOptions];
    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`.
()