百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-prosemirror

> 编程语言
开源

一个用于安全集成 ProseMirror 和 React 的库。

394 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个用于安全集成 ProseMirror 和 React 的库。

React ProseMirror

A fully featured library for safely integrating ProseMirror and React.

Installation

Note: React ProseMirror releases are coupled to specific prosemirror-view releases, and are not guaranteed to work with other versions of prosemirror-view. Ensure that your version of prosemirror-view matches the version in React ProseMirror's peer dependencies!

Note: React ProseMirror does not require a specific version of React, React DOM, or React Reconciler. However, you must ensure that your React Reconciler version matches your React/React DOM versions.

React version React Reconciler version
19.x 0.32.0
>= 18.2.0, s.apply(tr));
  }}
>

); }

…

tsx // SelectionWidget.tsx import { useRef } from "react"; import { useEditorEffect } from "@handlewithcare/react-prosemirror";

export function SelectionWidget() { const ref = useRef();

useEditorEffect((view) => { if (!view || !ref.current) return;

const viewClientRect = view.dom.getBoundingClientRect();
const coords = view.coordsAtPos(view.state.selection.anchor));

ref.current.style.top = coords.top - viewClientRect.top;
ref.current.style.left = coords.left - viewClientRect.left;

})

return (

); }


## API

### `ProseMirror`

```tsx
type ProseMirror = (
  props: DirectEditorProps &
    ({ defaultState: EditorState } | { state: EditorState }) & {
      children: ReactNode;
      nodeViewComponents?: {
        [nodeType: string]: ComponentType;
      };
      nodeViews?: {
        [nodeType: string]: NodeViewConstructor;
      };
      markViewComponents?: {
        [markType: string]: ComponentType;
      };
      markViews?: {
        [markType: string]: MarkViewConstructor;
      };
    }
) => JSX.Element;

Renders the ProseMirror editor.

Example usage:

typescript
import { EditorState } from "prosemirror-state";
import {
  ProseMirror,
  ProseMirrorDoc,
  reactKeys,
} from "@handlewithcare/react-prosemirror";

export function ProseMirrorEditor() {
  return (
    
      
    
  );
}

ProseMirrorDoc

typescript
type ProseMirrorDoc = (props: { as?: ReactElement }) => JSX.Element;

Renders the actual editable ProseMirror document.

This must be passed as a child to the ProseMirror component. It may be wrapped in any other components, and other children may be passed before or after.

Example usage:

typescript
import { EditorState } from "prosemirror-state";
import {
  ProseMirror,
  ProseMirrorDoc,
  reactKeys,
} from "@handlewithcare/react-prosemirror";

export function ProseMirrorEditor() {
  return (
    
      
      
        
      
      
    
  );
}

useEditorState

typescript
type useEditorState = () => EditorState | null;

Provides access to the current EditorState value.

useEditorEventCallback

typescript
type useEditorEventCallback = (
  callback: (view: EditorView | null, ...args: T) => void
) => void;

Returns a stable function reference to be used as an event handler callback.

The callback will be called with the EditorView instance as its first argument.

This hook is dependent on both the EditorContext.Provider and the LayoutGroup. It can only be used in a component that is mounted as a child of both of these providers.

useEditorEventListener

typescript
type useEditorEventListener = (
  eventType: EventType,
  listener: (view: EditorView, event: DOMEventMap[EventType]) => boolean
) => void;

Attaches an event listener at the EditorView's DOM node. See the ProseMirror docs for more details.

useEditorEffect

typescript
type useEditorEffect = (
  effect: (editorView: EditorView | null) => void | (() => void),
  dependencies?: React.DependencyList
) => void;

Registers a layout effect to run after the EditorView has been updated with the latest EditorState and Decorations.

Effects can take an EditorView instance as an argument. This hook should be used to execute layout effects that depend on the EditorView, such as for positioning DOM nodes based on ProseMirror positions.

Layout effects registered with this hook still fire synchronously after all DOM mutations, but they do so after the EditorView has been updated, even when the EditorView lives in an ancestor component.

Example usage:

typescript
import { useRef } from "react"
import { useEditorEffect } from "@handlewithcare/react-prosemirror"

export function SelectionWidget() {
  const ref = useRef()

  useEditorEffect((view) => {
    if (!view || !ref.current) return

    const viewClientRect = view.dom.getBoundingClientRect()
    const coords = view.coordsAtPos(view.state.selection.anchor))

    ref.current.style.top = coords.top - viewClientRect.top;
    ref.current.style.left = coords.left - viewClientRect.left;
  })

  return (
    
  )
}

NodeViewComponentProps

typescript
interface NodeViewComponentProps extends AllHTMLAttributes = {
  nodeProps: {
    decorations: readonly Decoration[];
    innerDecorations: DecorationSource;
    node: Node;
    getPos: () => number;
    contentDOMRef: Exclude;
  };
  ref: LegacyRef;
};

…

ts
type useEditorStateSelector = (selector: (state: EditorState) => Result): Result

Select a piece of the EditorState, a la Redux’s useSelector.

This hook will only trigger a re-render of the consuming component if the return value of the selector changes.

The provided selector must return the same value when it receieves the same inputs. If the selector creates and returns a new object when it runs, you'll enter a rerender loop.

useNodePos

typescript
type useNodePos = () => number;

…

tsx
type useStopEvent = (stopEvent: (this: NodeView, view: EditorView, event: Event) => boolean): void

This hook can be used within a node view component to register a stopEvent handler. Events for which this returns true are not handled by the editor.

useIgnoreMutation

typescript
type useIgnoreMutation = (ignoreMutation: (this: NodeView | MarkView, view: EditorView, mutation: ViewMutationRecord) => boolean): void

This hook can be used within a node view or mark view component to register an ignoreMutation handler. Mutations for which this returns true are not handled by the editor.

useSelectNode

typescript
type useSelectNode = (selectNode: () => void, deselectNode?: () => void): void

This hook can be used within a node view component to register selectNode and deselectNode handlers. The selectNode handler will only be called when a NodeSelection is created whose node is this one.

useIsNodeSelected

typescript
type useIsNodeSelected = (): boolean

This hook can be used within a node view component to subscribe to a boolean value determining whether this node is selected. The hook will return true when a NodeSelection is created whose node is this one.

useIsComposingIn

typescript
type useIsComposingIn = (): boolean

This hook can be used within a node view component to subscribe to a boolean value determining whether an IME composition is active inside the node.

widget

typescript
type widget = (
  pos: number,
  component: ForwardRefExoticComponent & WidgetComponentProps
  >,
  spec?: ReactWidgetSpec
) => Decoration(pos, pos, new ReactWidgetType(component, spec))

…

ts
type reorderSiblings = (pos: number, order: number[]): Command

When should I use this?

In order to maintain the React state across node view components, React ProseMirror uses the reactKeys plugin to track node positions across transactions and provide them with stable keys. During a reorder, for example when two nodes are swapped, ProseMirror is unable to track the positions across the resulting delete and insert steps, and so the reactKeys plugin will assign new keys to the nodes, causing their node view components to be remounted.

Sometimes, such as when using a drag-and-drop library like framer-motion, remounting the component being dragged can cause issues. The reorderSiblings command provides additional metadata to React ProseMirror that allows it to properly track the new positions for the reordered nodes, allowing it to reuse the same keys. This prevents any issues that could be caused by unexpected component remounts.

Migrations

  • Migrating from v1 to v2
  • Migrating from v2 to v3

Looking for someone to collaborate with?

Reach out to Handle with Care! We're a product development collective with years of experience bringing excellent ideas to life. We love React and ProseMirror, and we're always looking for new folks to work with!

Sponsors

The following companies, organizations, and individuals support Pitter Patter's ongoing development.

Sponsors receive regular updates about development progress, including previews of upcoming features, priority getting issues addressed as we release features, and a direct line of communication to us for support. Funders above a certain threshold join our steering committee — a lightweight governance model where significant contributors help shape our roadmap priorities.

Become a Sponsor

Sponsors

      Moment
    
  
  
    
      
      

      Lingco
    
  
  
    
      
      

      dskrpt
    
  
  
    
      
      

      Fastrepl

Issues· 6 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言