@handlewithcare/react-prosemirror 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +12 -0
- package/README.md +705 -0
- package/dist/cjs/browser.js +53 -0
- package/dist/cjs/components/ChildNodeViews.js +376 -0
- package/dist/cjs/components/CursorWrapper.js +91 -0
- package/dist/cjs/components/CustomNodeView.js +79 -0
- package/dist/cjs/components/DocNodeView.js +104 -0
- package/dist/cjs/components/LayoutGroup.js +111 -0
- package/dist/cjs/components/MarkView.js +115 -0
- package/dist/cjs/components/NativeWidgetView.js +109 -0
- package/dist/cjs/components/NodeView.js +196 -0
- package/dist/cjs/components/NodeViewComponentProps.js +4 -0
- package/dist/cjs/components/OutputSpec.js +88 -0
- package/dist/cjs/components/ProseMirror.js +103 -0
- package/dist/cjs/components/ProseMirrorDoc.js +92 -0
- package/dist/cjs/components/SeparatorHackView.js +100 -0
- package/dist/cjs/components/TextNodeView.js +112 -0
- package/dist/cjs/components/TrailingHackView.js +90 -0
- package/dist/cjs/components/WidgetView.js +95 -0
- package/dist/cjs/components/WidgetViewComponentProps.js +4 -0
- package/dist/cjs/components/__tests__/ProseMirror.composition.test.js +398 -0
- package/dist/cjs/components/__tests__/ProseMirror.domchange.test.js +270 -0
- package/dist/cjs/components/__tests__/ProseMirror.draw-decoration.test.js +1010 -0
- package/dist/cjs/components/__tests__/ProseMirror.draw.test.js +337 -0
- package/dist/cjs/components/__tests__/ProseMirror.node-view.test.js +315 -0
- package/dist/cjs/components/__tests__/ProseMirror.selection.test.js +444 -0
- package/dist/cjs/components/__tests__/ProseMirror.test.js +382 -0
- package/dist/cjs/contexts/ChildDescriptorsContext.js +19 -0
- package/dist/cjs/contexts/EditorContext.js +12 -0
- package/dist/cjs/contexts/EditorStateContext.js +12 -0
- package/dist/cjs/contexts/LayoutGroupContext.js +12 -0
- package/dist/cjs/contexts/NodeViewContext.js +12 -0
- package/dist/cjs/contexts/SelectNodeContext.js +12 -0
- package/dist/cjs/contexts/StopEventContext.js +12 -0
- package/dist/cjs/contexts/__tests__/DeferredLayoutEffects.test.js +141 -0
- package/dist/cjs/decorations/ReactWidgetType.js +58 -0
- package/dist/cjs/decorations/computeDocDeco.js +44 -0
- package/dist/cjs/decorations/internalTypes.js +4 -0
- package/dist/cjs/decorations/iterDeco.js +79 -0
- package/dist/cjs/decorations/viewDecorations.js +163 -0
- package/dist/cjs/dom.js +142 -0
- package/dist/cjs/hooks/__tests__/useEditorViewLayoutEffect.test.js +108 -0
- package/dist/cjs/hooks/useClientOnly.js +18 -0
- package/dist/cjs/hooks/useComponentEventListeners.js +39 -0
- package/dist/cjs/hooks/useEditor.js +287 -0
- package/dist/cjs/hooks/useEditorEffect.js +35 -0
- package/dist/cjs/hooks/useEditorEventCallback.js +33 -0
- package/dist/cjs/hooks/useEditorEventListener.js +34 -0
- package/dist/cjs/hooks/useEditorState.js +16 -0
- package/dist/cjs/hooks/useForceUpdate.js +15 -0
- package/dist/cjs/hooks/useLayoutGroupEffect.js +19 -0
- package/dist/cjs/hooks/useNodeViewDescriptor.js +115 -0
- package/dist/cjs/hooks/useReactKeys.js +17 -0
- package/dist/cjs/hooks/useSelectNode.js +28 -0
- package/dist/cjs/hooks/useStopEvent.js +24 -0
- package/dist/cjs/index.js +53 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/plugins/__tests__/reactKeys.test.js +81 -0
- package/dist/cjs/plugins/beforeInputPlugin.js +143 -0
- package/dist/cjs/plugins/componentEventListeners.js +35 -0
- package/dist/cjs/plugins/componentEventListenersPlugin.js +35 -0
- package/dist/cjs/plugins/reactKeys.js +96 -0
- package/dist/cjs/props.js +269 -0
- package/dist/cjs/selection/SelectionDOMObserver.js +174 -0
- package/dist/cjs/selection/hasFocusAndSelection.js +35 -0
- package/dist/cjs/selection/selectionFromDOM.js +77 -0
- package/dist/cjs/selection/selectionToDOM.js +226 -0
- package/dist/cjs/ssr.js +85 -0
- package/dist/cjs/testing/editorViewTestHelpers.js +111 -0
- package/dist/cjs/testing/setupProseMirrorView.js +94 -0
- package/dist/cjs/viewdesc.js +664 -0
- package/dist/esm/browser.js +43 -0
- package/dist/esm/components/ChildNodeViews.js +318 -0
- package/dist/esm/components/CursorWrapper.js +40 -0
- package/dist/esm/components/CustomNodeView.js +28 -0
- package/dist/esm/components/DocNodeView.js +53 -0
- package/dist/esm/components/LayoutGroup.js +66 -0
- package/dist/esm/components/MarkView.js +64 -0
- package/dist/esm/components/NativeWidgetView.js +58 -0
- package/dist/esm/components/NodeView.js +145 -0
- package/dist/esm/components/NodeViewComponentProps.js +1 -0
- package/dist/esm/components/OutputSpec.js +38 -0
- package/dist/esm/components/ProseMirror.js +52 -0
- package/dist/esm/components/ProseMirrorDoc.js +34 -0
- package/dist/esm/components/SeparatorHackView.js +49 -0
- package/dist/esm/components/TextNodeView.js +102 -0
- package/dist/esm/components/TrailingHackView.js +39 -0
- package/dist/esm/components/WidgetView.js +44 -0
- package/dist/esm/components/WidgetViewComponentProps.js +1 -0
- package/dist/esm/components/__tests__/ProseMirror.composition.test.js +395 -0
- package/dist/esm/components/__tests__/ProseMirror.domchange.test.js +266 -0
- package/dist/esm/components/__tests__/ProseMirror.draw-decoration.test.js +967 -0
- package/dist/esm/components/__tests__/ProseMirror.draw.test.js +294 -0
- package/dist/esm/components/__tests__/ProseMirror.node-view.test.js +272 -0
- package/dist/esm/components/__tests__/ProseMirror.selection.test.js +440 -0
- package/dist/esm/components/__tests__/ProseMirror.test.js +339 -0
- package/dist/esm/contexts/ChildDescriptorsContext.js +9 -0
- package/dist/esm/contexts/EditorContext.js +7 -0
- package/dist/esm/contexts/EditorStateContext.js +2 -0
- package/dist/esm/contexts/LayoutGroupContext.js +2 -0
- package/dist/esm/contexts/NodeViewContext.js +2 -0
- package/dist/esm/contexts/SelectNodeContext.js +2 -0
- package/dist/esm/contexts/StopEventContext.js +2 -0
- package/dist/esm/contexts/__tests__/DeferredLayoutEffects.test.js +98 -0
- package/dist/esm/decorations/ReactWidgetType.js +40 -0
- package/dist/esm/decorations/computeDocDeco.js +44 -0
- package/dist/esm/decorations/internalTypes.js +1 -0
- package/dist/esm/decorations/iterDeco.js +73 -0
- package/dist/esm/decorations/viewDecorations.js +163 -0
- package/dist/esm/dom.js +105 -0
- package/dist/esm/hooks/__tests__/useEditorViewLayoutEffect.test.js +99 -0
- package/dist/esm/hooks/useClientOnly.js +8 -0
- package/dist/esm/hooks/useComponentEventListeners.js +54 -0
- package/dist/esm/hooks/useEditor.js +278 -0
- package/dist/esm/hooks/useEditorEffect.js +38 -0
- package/dist/esm/hooks/useEditorEventCallback.js +35 -0
- package/dist/esm/hooks/useEditorEventListener.js +28 -0
- package/dist/esm/hooks/useEditorState.js +8 -0
- package/dist/esm/hooks/useForceUpdate.js +8 -0
- package/dist/esm/hooks/useLayoutGroupEffect.js +9 -0
- package/dist/esm/hooks/useNodeViewDescriptor.js +105 -0
- package/dist/esm/hooks/useReactKeys.js +7 -0
- package/dist/esm/hooks/useSelectNode.js +18 -0
- package/dist/esm/hooks/useStopEvent.js +14 -0
- package/dist/esm/index.js +11 -0
- package/dist/esm/plugins/__tests__/reactKeys.test.js +77 -0
- package/dist/esm/plugins/beforeInputPlugin.js +133 -0
- package/dist/esm/plugins/componentEventListeners.js +25 -0
- package/dist/esm/plugins/componentEventListenersPlugin.js +25 -0
- package/dist/esm/plugins/reactKeys.js +81 -0
- package/dist/esm/props.js +251 -0
- package/dist/esm/selection/SelectionDOMObserver.js +164 -0
- package/dist/esm/selection/hasFocusAndSelection.js +17 -0
- package/dist/esm/selection/selectionFromDOM.js +59 -0
- package/dist/esm/selection/selectionToDOM.js +196 -0
- package/dist/esm/ssr.js +82 -0
- package/dist/esm/testing/editorViewTestHelpers.js +88 -0
- package/dist/esm/testing/setupProseMirrorView.js +76 -0
- package/dist/esm/viewdesc.js +654 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/browser.d.ts +15 -0
- package/dist/types/components/ChildNodeViews.d.ts +9 -0
- package/dist/types/components/CursorWrapper.d.ts +5 -0
- package/dist/types/components/CustomNodeView.d.ts +21 -0
- package/dist/types/components/DocNodeView.d.ts +20 -0
- package/dist/types/components/LayoutGroup.d.ts +12 -0
- package/dist/types/components/MarkView.d.ts +9 -0
- package/dist/types/components/NativeWidgetView.d.ts +8 -0
- package/dist/types/components/NodeView.d.ts +11 -0
- package/dist/types/components/NodeViewComponentProps.d.ts +12 -0
- package/dist/types/components/OutputSpec.d.ts +8 -0
- package/dist/types/components/ProseMirror.d.ts +15 -0
- package/dist/types/components/ProseMirrorDoc.d.ts +10 -0
- package/dist/types/components/SeparatorHackView.d.ts +6 -0
- package/dist/types/components/TextNodeView.d.ts +23 -0
- package/dist/types/components/TrailingHackView.d.ts +6 -0
- package/dist/types/components/WidgetView.d.ts +8 -0
- package/dist/types/components/WidgetViewComponentProps.d.ts +6 -0
- package/dist/types/components/__tests__/ProseMirror.composition.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.domchange.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.draw-decoration.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.draw.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.node-view.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.selection.test.d.ts +1 -0
- package/dist/types/components/__tests__/ProseMirror.test.d.ts +1 -0
- package/dist/types/contexts/ChildDescriptorsContext.d.ts +6 -0
- package/dist/types/contexts/EditorContext.d.ts +14 -0
- package/dist/types/contexts/EditorStateContext.d.ts +2 -0
- package/dist/types/contexts/LayoutGroupContext.d.ts +5 -0
- package/dist/types/contexts/NodeViewContext.d.ts +6 -0
- package/dist/types/contexts/SelectNodeContext.d.ts +3 -0
- package/dist/types/contexts/StopEventContext.d.ts +3 -0
- package/dist/types/contexts/__tests__/DeferredLayoutEffects.test.d.ts +1 -0
- package/dist/types/decorations/ReactWidgetType.d.ts +39 -0
- package/dist/types/decorations/computeDocDeco.d.ts +13 -0
- package/dist/types/decorations/internalTypes.d.ts +16 -0
- package/dist/types/decorations/iterDeco.d.ts +3 -0
- package/dist/types/decorations/viewDecorations.d.ts +13 -0
- package/dist/types/dom.d.ts +22 -0
- package/dist/types/hooks/__tests__/useEditorViewLayoutEffect.test.d.ts +1 -0
- package/dist/types/hooks/useClientOnly.d.ts +1 -0
- package/dist/types/hooks/useComponentEventListeners.d.ts +33 -0
- package/dist/types/hooks/useEditor.d.ts +66 -0
- package/dist/types/hooks/useEditorEffect.d.ts +17 -0
- package/dist/types/hooks/useEditorEventCallback.d.ts +15 -0
- package/dist/types/hooks/useEditorEventListener.d.ts +8 -0
- package/dist/types/hooks/useEditorState.d.ts +5 -0
- package/dist/types/hooks/useForceUpdate.d.ts +5 -0
- package/dist/types/hooks/useLayoutGroupEffect.d.ts +3 -0
- package/dist/types/hooks/useNodeViewDescriptor.d.ts +11 -0
- package/dist/types/hooks/useReactKeys.d.ts +5 -0
- package/dist/types/hooks/useSelectNode.d.ts +1 -0
- package/dist/types/hooks/useStopEvent.d.ts +2 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/plugins/__tests__/reactKeys.test.d.ts +1 -0
- package/dist/types/plugins/beforeInputPlugin.d.ts +3 -0
- package/dist/types/plugins/componentEventListeners.d.ts +4 -0
- package/dist/types/plugins/componentEventListenersPlugin.d.ts +4 -0
- package/dist/types/plugins/reactKeys.d.ts +19 -0
- package/dist/types/props.d.ts +1174 -0
- package/dist/types/selection/SelectionDOMObserver.d.ts +34 -0
- package/dist/types/selection/hasFocusAndSelection.d.ts +3 -0
- package/dist/types/selection/selectionFromDOM.d.ts +4 -0
- package/dist/types/selection/selectionToDOM.d.ts +9 -0
- package/dist/types/ssr.d.ts +19 -0
- package/dist/types/testing/editorViewTestHelpers.d.ts +23 -0
- package/dist/types/testing/setupProseMirrorView.d.ts +2 -0
- package/dist/types/viewdesc.d.ts +131 -0
- package/package.json +113 -0
package/README.md
ADDED
|
@@ -0,0 +1,705 @@
|
|
|
1
|
+
# React ProseMirror
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="react-prosemirror-logo.png" alt="React ProseMirror Logo" width="120px" height="120px"/>
|
|
5
|
+
<br>
|
|
6
|
+
<em>A fully featured library for safely integrating ProseMirror and React.</em>
|
|
7
|
+
<br>
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
[](https://gitter.im/nytimes/react-prosemirror?utm_source=badge&utm_medium=badge&utm_content=badge)
|
|
11
|
+
|
|
12
|
+
## What happened to `@nytimes/react-prosemirror`?
|
|
13
|
+
|
|
14
|
+
On Jan. 17, 2025, the maintainers of the `@nytimes/react-prosemirror` library
|
|
15
|
+
decided fork the v2 release into its own project. It had been developed entirely
|
|
16
|
+
by one developer, @smoores-dev, who had not worked for NYT in years, and was
|
|
17
|
+
struggling to move the project at the speed he desired.
|
|
18
|
+
|
|
19
|
+
This project is that fork, and all future development on the React-based
|
|
20
|
+
ProseMirror renderer will happen in this repository, published as
|
|
21
|
+
`@handlewithcare/react-prosemirror` on NPM.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
npm:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm install @handlewithcare/react-prosemirror
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
yarn:
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
yarn add @handlewithcare/react-prosemirror
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
<!-- toc -->
|
|
38
|
+
|
|
39
|
+
- [The Problem](#the-problem)
|
|
40
|
+
- [The Solution](#the-solution)
|
|
41
|
+
- [Rendering ProseMirror Views within React](#rendering-prosemirror-views-within-react)
|
|
42
|
+
- [`useEditorEffect`](#useeditoreffect)
|
|
43
|
+
- [`useEditorEventCallback`](#useeditoreventcallback)
|
|
44
|
+
- [`useEditorEventListener`](#useeditoreventlistener)
|
|
45
|
+
- [Building node views with React](#building-node-views-with-react)
|
|
46
|
+
- [What's changing in v2?](#whats-changing-in-v2)
|
|
47
|
+
- [API changes](#api-changes)
|
|
48
|
+
- [API](#api)
|
|
49
|
+
- [`ProseMirror`](#prosemirror)
|
|
50
|
+
- [`ProseMirrorDoc`](#prosemirrordoc)
|
|
51
|
+
- [`useEditorState`](#useeditorstate)
|
|
52
|
+
- [`useEditorEventCallback`](#useeditoreventcallback-1)
|
|
53
|
+
- [`useEditorEventListener`](#useeditoreventlistener-1)
|
|
54
|
+
- [`useEditorEffect`](#useeditoreffect-1)
|
|
55
|
+
- [`NodeViewComponentProps`](#nodeviewcomponentprops)
|
|
56
|
+
- [`useStopEvent`](#usestopevent)
|
|
57
|
+
- [`useSelectNode`](#useselectnode)
|
|
58
|
+
- [`widget`](#widget)
|
|
59
|
+
|
|
60
|
+
<!-- tocstop -->
|
|
61
|
+
|
|
62
|
+
## The Problem
|
|
63
|
+
|
|
64
|
+
To make updates efficient, React separates updates into phases so that it can
|
|
65
|
+
process updates in batches. In the first phase, application code renders a
|
|
66
|
+
virtual document. In the second phase, the React DOM renderer finalizes the
|
|
67
|
+
update by reconciling the real document with the virtual document.
|
|
68
|
+
|
|
69
|
+
On the other hand, the ProseMirror View library renders ProseMirror documents in
|
|
70
|
+
a single-phase update. Unlike React, it also allows built-in editing features of
|
|
71
|
+
the browser to modify the document under some circumstances, deriving state
|
|
72
|
+
updates from view updates rather than the other way around.
|
|
73
|
+
|
|
74
|
+
It is possible to use both React DOM and ProseMirror View, but using React DOM
|
|
75
|
+
to render ProseMirror View components safely requires careful consideration of
|
|
76
|
+
differences between the rendering approaches taken by each framework. The first
|
|
77
|
+
phase of a React update should be free of side effects, which requires that
|
|
78
|
+
updates to the ProseMirror View happen in the second phase. This means that
|
|
79
|
+
during the first phase, React components actually have access to a different
|
|
80
|
+
(newer) version of the EditorState than the one in the Editorview. As a result
|
|
81
|
+
code that dispatches transactions may dispatch transactions based on incorrect
|
|
82
|
+
state. Code that invokes methods of the ProseMirror view may make bad
|
|
83
|
+
assumptions about its state that cause incorrect behavior or errors.
|
|
84
|
+
|
|
85
|
+
It's also challenging to effectively use React to define node views for
|
|
86
|
+
ProseMirror documents. Both ProseMirror and React expect to have full control
|
|
87
|
+
over their respective parts of the DOM. They both modify and destroy DOM nodes
|
|
88
|
+
as needed. Previous solutions (including previous iterations of this library)
|
|
89
|
+
have attempted to work around this power struggle by producing wrapper elements
|
|
90
|
+
to hand to ProseMirror, and then mounting React nodes within these (usually with
|
|
91
|
+
React Portals).
|
|
92
|
+
|
|
93
|
+
This approach works, but tenuously. Having additional nodes in the document that
|
|
94
|
+
ProseMirror isn't strictly aware of can cause issues with its change detection
|
|
95
|
+
system, leading to challenging edge cases.
|
|
96
|
+
[Here's an example](https://github.com/nytimes/react-prosemirror/issues/42).
|
|
97
|
+
These extra wrapping elements also make it challenging to produce semantic
|
|
98
|
+
markup and introduce challenges when styling.
|
|
99
|
+
|
|
100
|
+
## The Solution
|
|
101
|
+
|
|
102
|
+
This library provides an alternate implementation of ProseMirror's EditorView.
|
|
103
|
+
It uses React as the rendering engine, rather than ProseMirror's home-brewed DOM
|
|
104
|
+
update system. This allows us to provide a more comfortable integration with
|
|
105
|
+
ProseMirror's powerful data model, transformations, and event management
|
|
106
|
+
systems.
|
|
107
|
+
|
|
108
|
+
### Rendering ProseMirror Views within React
|
|
109
|
+
|
|
110
|
+
This library provides a set of React contexts and hooks for consuming them that
|
|
111
|
+
ensure safe access to the EditorView from React components. This allows us to
|
|
112
|
+
build React applications that contain ProseMirror Views, even when the
|
|
113
|
+
EditorState is lifted into React state, or a global state management system like
|
|
114
|
+
Redux.
|
|
115
|
+
|
|
116
|
+
The simplest way to make use of these contexts is with the `<ProseMirror />`
|
|
117
|
+
component. The `<ProseMirror />` component can be used controlled (via the
|
|
118
|
+
`state` prop) or uncontrolled (via the `defaultState` prop).
|
|
119
|
+
|
|
120
|
+
```tsx
|
|
121
|
+
import { EditorState } from "prosemirror-state";
|
|
122
|
+
import {
|
|
123
|
+
ProseMirror,
|
|
124
|
+
ProseMirrorDoc,
|
|
125
|
+
reactKeys,
|
|
126
|
+
} from "@handlewithcare/react-prosemirror";
|
|
127
|
+
|
|
128
|
+
export function ProseMirrorEditor() {
|
|
129
|
+
return (
|
|
130
|
+
<ProseMirror
|
|
131
|
+
defaultState={EditorState.create({
|
|
132
|
+
schema,
|
|
133
|
+
plugins: [
|
|
134
|
+
// The reactKeys plugin is required for the ProseMirror component to work!
|
|
135
|
+
reactKeys(),
|
|
136
|
+
],
|
|
137
|
+
})}
|
|
138
|
+
>
|
|
139
|
+
<ProseMirrorDoc />
|
|
140
|
+
</ProseMirror>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
The EditorState can also easily be lifted out of the ProseMirror component and
|
|
146
|
+
passed as a prop.
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
import { EditorState } from "prosemirror-state";
|
|
150
|
+
import { schema } from "prosemirror-schema-basic";
|
|
151
|
+
import {
|
|
152
|
+
ProseMirror,
|
|
153
|
+
ProseMirrorDoc,
|
|
154
|
+
reactKeys,
|
|
155
|
+
} from "@handlewithcare/react-prosemirror";
|
|
156
|
+
|
|
157
|
+
export function ProseMirrorEditor() {
|
|
158
|
+
const [editorState, setEditorState] = useState(
|
|
159
|
+
EditorState.create({ schema, plugins: [reactKeys()] })
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<ProseMirror
|
|
164
|
+
state={editorState}
|
|
165
|
+
dispatchTransaction={(tr) => {
|
|
166
|
+
setEditorState((s) => s.apply(tr));
|
|
167
|
+
}}
|
|
168
|
+
>
|
|
169
|
+
<ProseMirrorDoc />
|
|
170
|
+
</ProseMirror>
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The `EditorView` interface exposes several useful methods that provide access to
|
|
176
|
+
the DOM or data derived from its layout, such as `coordsFromPos`. These methods
|
|
177
|
+
should only be accessed outside of the render cycle, to ensure that the DOM has
|
|
178
|
+
been updated to match the latest state. React ProseMirror provides two hooks to
|
|
179
|
+
enable this access pattern: `useEditorEffect` and `useEditorEventCallback`. Both
|
|
180
|
+
of these hooks can be used from any children of the ProseMirror component.
|
|
181
|
+
|
|
182
|
+
#### `useEditorEffect`
|
|
183
|
+
|
|
184
|
+
Often, it's necessary to position React components relative to specific
|
|
185
|
+
positions in the ProseMirror document. For example, you might have some widget
|
|
186
|
+
that needs to be positioned at the user's cursor. In order to ensure that this
|
|
187
|
+
positioning happens when the DOM is in sync with the latest EditorState, we can
|
|
188
|
+
use `useEditorEffect`.
|
|
189
|
+
|
|
190
|
+
```tsx
|
|
191
|
+
// SelectionWidget.tsx
|
|
192
|
+
import { useRef } from "react";
|
|
193
|
+
import { useEditorEffect } from "@handlewithcare/react-prosemirror";
|
|
194
|
+
|
|
195
|
+
export function SelectionWidget() {
|
|
196
|
+
const ref = useRef();
|
|
197
|
+
|
|
198
|
+
useEditorEffect((view) => {
|
|
199
|
+
if (!view || !ref.current) return;
|
|
200
|
+
|
|
201
|
+
const viewClientRect = view.dom.getBoundingClientRect();
|
|
202
|
+
const coords = view.coordsAtPos(view.state.selection.anchor));
|
|
203
|
+
|
|
204
|
+
ref.current.style.top = coords.top - viewClientRect.top;
|
|
205
|
+
ref.current.style.left = coords.left - viewClientRect.left;
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
return (
|
|
209
|
+
<div
|
|
210
|
+
ref={ref}
|
|
211
|
+
style={{
|
|
212
|
+
position: "absolute"
|
|
213
|
+
}}
|
|
214
|
+
/>
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ProseMirrorEditor.tsx
|
|
219
|
+
import {
|
|
220
|
+
ProseMirror,
|
|
221
|
+
ProseMirrorDoc,
|
|
222
|
+
reactKeys
|
|
223
|
+
} from "@handlewithcare/react-prosemirror";
|
|
224
|
+
import { EditorState } from "prosemirror-state";
|
|
225
|
+
import { schema } from "prosemirror-schema-basic";
|
|
226
|
+
|
|
227
|
+
import { SelectionWidget } from "./SelectionWidget.tsx";
|
|
228
|
+
|
|
229
|
+
export function ProseMirrorEditor() {
|
|
230
|
+
const [editorState, setEditorState] = useState(
|
|
231
|
+
EditorState.create({ schema, plugins: [reactKeys()] })
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
return (
|
|
235
|
+
<ProseMirror
|
|
236
|
+
state={editorState}
|
|
237
|
+
dispatchTransaction={(tr) => {
|
|
238
|
+
setEditorState(s => s.apply(tr))
|
|
239
|
+
}}
|
|
240
|
+
>
|
|
241
|
+
<ProseMirrorDoc />
|
|
242
|
+
{/*
|
|
243
|
+
We have to mount all components that need to access the
|
|
244
|
+
EditorView as children of the ProseMirror component
|
|
245
|
+
*/}
|
|
246
|
+
<SelectionWidget />
|
|
247
|
+
</ProseMirror>
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
#### `useEditorEventCallback`
|
|
253
|
+
|
|
254
|
+
It's also often necessary to dispatch transactions or execute side effects in
|
|
255
|
+
response to user actions, like mouse clicks and keyboard events. Note: if you
|
|
256
|
+
need to respond to keyboard events from _within_ the `contenteditable` element,
|
|
257
|
+
you should instead use [`useEditorEventListener`](#useEditorEventListener).
|
|
258
|
+
|
|
259
|
+
However, if you need to dispatch a transaction in response to some event
|
|
260
|
+
dispatched from a React component, like a tooltip or a toolbar button, you can
|
|
261
|
+
use `useEditorEventCallback` to create a stable function reference that can
|
|
262
|
+
safely access the latest value of the `EditorView`.
|
|
263
|
+
|
|
264
|
+
```tsx
|
|
265
|
+
// BoldButton.tsx
|
|
266
|
+
import { toggleMark } from "prosemirror-commands";
|
|
267
|
+
import { useEditorEventCallback } from "@handlewithcare/react-prosemirror";
|
|
268
|
+
|
|
269
|
+
export function BoldButton() {
|
|
270
|
+
const onClick = useEditorEventCallback((view) => {
|
|
271
|
+
if (!view) return;
|
|
272
|
+
const toggleBoldMark = toggleMark(view.state.schema.marks.bold);
|
|
273
|
+
toggleBoldMark(view.state, view.dispatch, view);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
return <button onClick={onClick}>Bold</button>;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// ProseMirrorEditor.tsx
|
|
280
|
+
import {
|
|
281
|
+
ProseMirror,
|
|
282
|
+
ProseMirrorDoc,
|
|
283
|
+
reactKeys,
|
|
284
|
+
} from "@handlewithcare/react-prosemirror";
|
|
285
|
+
import { EditorState } from "prosemirror-state";
|
|
286
|
+
import { schema } from "prosemirror-schema-basic";
|
|
287
|
+
|
|
288
|
+
import { BoldButton } from "./BoldButton.tsx";
|
|
289
|
+
|
|
290
|
+
export function ProseMirrorEditor() {
|
|
291
|
+
const [editorState, setEditorState] = useState(
|
|
292
|
+
EditorState.create({ schema, plugins: [reactKeys()] })
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
return (
|
|
296
|
+
<ProseMirror
|
|
297
|
+
state={editorState}
|
|
298
|
+
dispatchTransaction={(tr) => {
|
|
299
|
+
setEditorState((s) => s.apply(tr));
|
|
300
|
+
}}
|
|
301
|
+
>
|
|
302
|
+
<ProseMirrorDoc />
|
|
303
|
+
{/*
|
|
304
|
+
We have to mount all components that need to access the
|
|
305
|
+
EditorView as children of the ProseMirror component
|
|
306
|
+
*/}
|
|
307
|
+
<BoldButton />
|
|
308
|
+
</ProseMirror>
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
#### `useEditorEventListener`
|
|
314
|
+
|
|
315
|
+
`useEditorEventCallback` produces functions that can be passed to React
|
|
316
|
+
components as event handlers. If you need to listen to events that originate
|
|
317
|
+
_within the `contenteditable` node_, however, those event listeners need to be
|
|
318
|
+
registered with the `EditorView`'s `handleDOMEvents` prop.
|
|
319
|
+
|
|
320
|
+
You can use the `useEditorEventListener` hook to accomplish this. It takes an
|
|
321
|
+
`eventType` and an event listener. The event listener follows the usual
|
|
322
|
+
semantics for ProseMirror's `handleDOMEvents` prop:
|
|
323
|
+
|
|
324
|
+
- Returning `true` or calling `event.preventDefault` will prevent other
|
|
325
|
+
listeners from running.
|
|
326
|
+
- Returning `true` will not automatically call `event.preventDefault`; if you
|
|
327
|
+
want to prevent the default contenteditable behavior, you must call
|
|
328
|
+
`event.preventDefault`.
|
|
329
|
+
|
|
330
|
+
You can use this hook to implement custom behavior in your NodeViews:
|
|
331
|
+
|
|
332
|
+
```tsx
|
|
333
|
+
import { forwardRef, Ref } from "react";
|
|
334
|
+
import {
|
|
335
|
+
useEditorEventListener,
|
|
336
|
+
NodeViewComponentProps,
|
|
337
|
+
} from "@handlewithcare/react-prosemirror";
|
|
338
|
+
|
|
339
|
+
const Paragraph = forwardRef<HTMLParagraphElement, NodeViewComponentProps>(
|
|
340
|
+
function Paragraph({ children, nodeProps, ...props }, ref) {
|
|
341
|
+
useEditorEventListener("keydown", (view, event) => {
|
|
342
|
+
const { pos, node } = nodeProps;
|
|
343
|
+
|
|
344
|
+
if (event.code !== "ArrowDown") {
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
const nodeEnd = pos + node.nodeSize;
|
|
348
|
+
const { selection } = view.state;
|
|
349
|
+
if (selection.anchor < pos || selection.anchor > nodeEnd) {
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
event.preventDefault();
|
|
353
|
+
alert("No down keys allowed!");
|
|
354
|
+
return true;
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
return (
|
|
358
|
+
<p ref={ref} {...props}>
|
|
359
|
+
{children}
|
|
360
|
+
</p>
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
);
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### Building node views with React
|
|
367
|
+
|
|
368
|
+
The other way to integrate React and ProseMirror is to have ProseMirror render
|
|
369
|
+
node views using React components. Because React ProseMirror renders the
|
|
370
|
+
ProseMirror document with React, node view components don't need to do anything
|
|
371
|
+
special other than fulfill the
|
|
372
|
+
[`NodeViewComponentProps`](#nodeviewcomponentprops) interface.
|
|
373
|
+
|
|
374
|
+
```tsx
|
|
375
|
+
import { forwardRef, Ref } from "react";
|
|
376
|
+
import {
|
|
377
|
+
ProseMirror,
|
|
378
|
+
ProseMirrorDoc,
|
|
379
|
+
useEditorEventCallback,
|
|
380
|
+
NodeViewComponentProps,
|
|
381
|
+
reactKeys,
|
|
382
|
+
} from "@handlewithcare/react-prosemirror";
|
|
383
|
+
import { EditorState } from "prosemirror-state";
|
|
384
|
+
import { schema } from "prosemirror-schema-basic";
|
|
385
|
+
|
|
386
|
+
// Paragraph is more or less a normal React component, taking and rendering
|
|
387
|
+
// its children. All node view components _must_ forward refs to their top-level
|
|
388
|
+
// DOM elements. All node view components _should_ spread all of the props that they
|
|
389
|
+
// receive onto their top-level DOM elements; this is required for node Decorations
|
|
390
|
+
// that apply attributes rather than wrapping nodes in an additional element.
|
|
391
|
+
const Paragraph = forwardRef<HTMLParagraphElement, NodeViewComponentProps>(
|
|
392
|
+
function Paragraph({ children, nodeProps, ...props }, ref) {
|
|
393
|
+
const onClick = useEditorEventCallback((view) =>
|
|
394
|
+
view.dispatch(view.state.tr.deleteSelection())
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
return (
|
|
398
|
+
<p ref={ref} {...props} onClick={onClick}>
|
|
399
|
+
{children}
|
|
400
|
+
</p>
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
function ProseMirrorEditor() {
|
|
406
|
+
return (
|
|
407
|
+
<ProseMirror
|
|
408
|
+
defaultState={EditorState.create({ schema, plugins: [reactKeys()] })}
|
|
409
|
+
nodeViews={{
|
|
410
|
+
paragraph: Paragraph,
|
|
411
|
+
}}
|
|
412
|
+
>
|
|
413
|
+
<ProseMirrorDoc />
|
|
414
|
+
</ProseMirror>
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
## What's changing in v2?
|
|
420
|
+
|
|
421
|
+
The upcoming v2 release constitutes a significant re-write of the library.
|
|
422
|
+
|
|
423
|
+
Previously, React ProseMirror relied on ProseMirror's EditorView to manage the
|
|
424
|
+
DOM for the editor. To integrate it with React, we used React
|
|
425
|
+
[portals](https://react.dev/reference/react-dom/createPortal) to render
|
|
426
|
+
components into ProseMirror-managed DOM nodes, and a
|
|
427
|
+
[useLayoutEffect](https://react.dev/reference/react/useLayoutEffect) to sync
|
|
428
|
+
state updates with the EditorView instance.
|
|
429
|
+
|
|
430
|
+
This approach provided some challenges. First, portals have to be rendered into
|
|
431
|
+
existing, stable DOM nodes, so all React-based custom node views ended up
|
|
432
|
+
wrapped in extra HTML elements. This made styling and producing valid DOM more
|
|
433
|
+
challenging than it should be, and introduced corner cases in browser
|
|
434
|
+
contenteditable implementations. Second, we induced a double render for every
|
|
435
|
+
ProseMirror update, and during the first render, React-based custom node views
|
|
436
|
+
will be rendered with the previous state. This is technically another form of
|
|
437
|
+
the state tearing that this library was designed to prevent, as all _other_
|
|
438
|
+
React components will be rendered with the new state!
|
|
439
|
+
|
|
440
|
+
To overcome these challenges, the new release moves rendering responsibility
|
|
441
|
+
entirely into React. We disabled the EditorView's DOM update cycle, and
|
|
442
|
+
implemented the same update algorithm that prosemirror-view uses with React
|
|
443
|
+
components. The result is a more idiomatic, React-based library, which doesn't
|
|
444
|
+
have any of the issues of the original implementation.
|
|
445
|
+
|
|
446
|
+
### API changes
|
|
447
|
+
|
|
448
|
+
- The [`ProseMirror`](#prosemirror) component API has changed slightly:
|
|
449
|
+
- Instead of passing a `mount` prop with a ref to a child element, users must
|
|
450
|
+
render a [`ProseMirrorDoc`](#prosemirrordoc) component as a child of the
|
|
451
|
+
`ProseMirror` component.
|
|
452
|
+
- The `nodeViews` prop no longer matches the ProseMirror API. Instead,
|
|
453
|
+
`nodeViews` should be a map from node type name to React components, each of
|
|
454
|
+
which must take [`NodeViewComponentProps`](#nodeviewcomponentprops) as
|
|
455
|
+
props. This map should be memoized, or defined outside the React component
|
|
456
|
+
entirely.
|
|
457
|
+
- To pass standard ProseMirror node view constructors, use the
|
|
458
|
+
`customNodeViews` prop
|
|
459
|
+
- The API that React-based node views must implement has changed:
|
|
460
|
+
- There is no longer any need to provide a ProseMirror node view constructor
|
|
461
|
+
function. React-based node views are now just React components that accept
|
|
462
|
+
`NodeViewComponentProps` as props.
|
|
463
|
+
- Props related to the ProseMirror node, such as the node itself, the `getPos`
|
|
464
|
+
function, and decorations, now live in the `nodeProps` property. All other
|
|
465
|
+
props _must_ be spread onto the root element of the node view component,
|
|
466
|
+
aside from `children`, which may be rendered anywhere in the component, as
|
|
467
|
+
appropriate.
|
|
468
|
+
- All node view components must forward their ref to the root element of the
|
|
469
|
+
component.
|
|
470
|
+
- To implement features that would normally live in the node view spec, there
|
|
471
|
+
are new hooks, such as [`useStopEvent`](#usestopevent) and
|
|
472
|
+
[`useSelectNode`](#useselectnode)
|
|
473
|
+
- There is a new export, [`widget`](#widget), which behaves similarly to
|
|
474
|
+
`Decoration.widget` from `prosemirror-view`, but takes a React component
|
|
475
|
+
instead of a `toDOM` method.
|
|
476
|
+
|
|
477
|
+
## API
|
|
478
|
+
|
|
479
|
+
### `ProseMirror`
|
|
480
|
+
|
|
481
|
+
```tsx
|
|
482
|
+
type ProseMirror = (
|
|
483
|
+
props: DirectEditorProps &
|
|
484
|
+
({ defaultState: EditorState } | { state: EditorState }) & {
|
|
485
|
+
children: ReactNode;
|
|
486
|
+
nodeViews?: {
|
|
487
|
+
[nodeType: string]: ForwardRefExoticComponent<
|
|
488
|
+
NodeViewComponentProps & RefAttributes<any>
|
|
489
|
+
>;
|
|
490
|
+
};
|
|
491
|
+
customNodeViews?: {
|
|
492
|
+
[nodeType: string]: NodeViewConstructor;
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
) => JSX.Element;
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
Renders the ProseMirror editor.
|
|
499
|
+
|
|
500
|
+
Example usage:
|
|
501
|
+
|
|
502
|
+
```tsx
|
|
503
|
+
import { EditorState } from "prosemirror-state";
|
|
504
|
+
import {
|
|
505
|
+
ProseMirror,
|
|
506
|
+
ProseMirrorDoc,
|
|
507
|
+
reactKeys,
|
|
508
|
+
} from "@handlewithcare/react-prosemirror";
|
|
509
|
+
|
|
510
|
+
export function ProseMirrorEditor() {
|
|
511
|
+
return (
|
|
512
|
+
<ProseMirror
|
|
513
|
+
defaultState={EditorState.create({ schema, plugins: [reactKeys()] })}
|
|
514
|
+
>
|
|
515
|
+
<ProseMirrorDoc />
|
|
516
|
+
</ProseMirror>
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
### `ProseMirrorDoc`
|
|
522
|
+
|
|
523
|
+
```tsx
|
|
524
|
+
type ProseMirrorDoc = (props: { as?: ReactElement }) => JSX.Element;
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
Renders the actual editable ProseMirror document.
|
|
528
|
+
|
|
529
|
+
This **must** be passed as a child to the `ProseMirror` component. It may be
|
|
530
|
+
wrapped in any other components, and other children may be passed before or
|
|
531
|
+
after
|
|
532
|
+
|
|
533
|
+
Example usage:
|
|
534
|
+
|
|
535
|
+
```tsx
|
|
536
|
+
import { EditorState } from "prosemirror-state";
|
|
537
|
+
import {
|
|
538
|
+
ProseMirror,
|
|
539
|
+
ProseMirrorDoc,
|
|
540
|
+
reactKeys,
|
|
541
|
+
} from "@handlewithcare/react-prosemirror";
|
|
542
|
+
|
|
543
|
+
export function ProseMirrorEditor() {
|
|
544
|
+
return (
|
|
545
|
+
<ProseMirror
|
|
546
|
+
defaultState={EditorState.create({ schema, plugins: [reactKeys()] })}
|
|
547
|
+
>
|
|
548
|
+
<ToolBar />
|
|
549
|
+
<SomeWrapper>
|
|
550
|
+
<ProseMirrorDoc as={<article />} />
|
|
551
|
+
</SomeWrapper>
|
|
552
|
+
<Footnotes />
|
|
553
|
+
</ProseMirror>
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
### `useEditorState`
|
|
559
|
+
|
|
560
|
+
```tsx
|
|
561
|
+
type useEditorState = () => EditorState | null;
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
Provides access to the current EditorState value.
|
|
565
|
+
|
|
566
|
+
### `useEditorEventCallback`
|
|
567
|
+
|
|
568
|
+
```tsx
|
|
569
|
+
type useEditorEventCallback = <T extends unknown[]>(
|
|
570
|
+
callback: (view: EditorView | null, ...args: T) => void
|
|
571
|
+
) => void;
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
Returns a stable function reference to be used as an event handler callback.
|
|
575
|
+
|
|
576
|
+
The callback will be called with the EditorView instance as its first argument.
|
|
577
|
+
|
|
578
|
+
This hook is dependent on both the `EditorContext.Provider` and the
|
|
579
|
+
`LayoutGroup`. It can only be used in a component that is mounted as a child of
|
|
580
|
+
both of these providers.
|
|
581
|
+
|
|
582
|
+
### `useEditorEventListener`
|
|
583
|
+
|
|
584
|
+
```tsx
|
|
585
|
+
type useEditorEventListener = <EventType extends DOMEventMap>(
|
|
586
|
+
eventType: EventType,
|
|
587
|
+
listener: (view: EditorView, event: DOMEventMap[EventType]) => boolean
|
|
588
|
+
) => void;
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
Attaches an event listener at the `EditorView`'s DOM node. See
|
|
592
|
+
[the ProseMirror docs](https://prosemirror.net/docs/ref/#view.EditorProps.handleDOMEvents)
|
|
593
|
+
for more details.
|
|
594
|
+
|
|
595
|
+
### `useEditorEffect`
|
|
596
|
+
|
|
597
|
+
```tsx
|
|
598
|
+
type useEditorEffect = (
|
|
599
|
+
effect: (editorView: EditorView | null) => void | (() => void),
|
|
600
|
+
dependencies?: React.DependencyList
|
|
601
|
+
) => void;
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
Registers a layout effect to run after the EditorView has been updated with the
|
|
605
|
+
latest EditorState and Decorations.
|
|
606
|
+
|
|
607
|
+
Effects can take an EditorView instance as an argument. This hook should be used
|
|
608
|
+
to execute layout effects that depend on the EditorView, such as for positioning
|
|
609
|
+
DOM nodes based on ProseMirror positions.
|
|
610
|
+
|
|
611
|
+
Layout effects registered with this hook still fire synchronously after all DOM
|
|
612
|
+
mutations, but they do so _after_ the EditorView has been updated, even when the
|
|
613
|
+
EditorView lives in an ancestor component.
|
|
614
|
+
|
|
615
|
+
Example usage:
|
|
616
|
+
|
|
617
|
+
```tsx
|
|
618
|
+
import { useRef } from "react"
|
|
619
|
+
import { useEditorEffect } from "@handlewithcare/react-prosemirror"
|
|
620
|
+
|
|
621
|
+
export function SelectionWidget() {
|
|
622
|
+
const ref = useRef()
|
|
623
|
+
|
|
624
|
+
useEditorEffect((view) => {
|
|
625
|
+
if (!view || !ref.current) return
|
|
626
|
+
|
|
627
|
+
const viewClientRect = view.dom.getBoundingClientRect()
|
|
628
|
+
const coords = view.coordsAtPos(view.state.selection.anchor))
|
|
629
|
+
|
|
630
|
+
ref.current.style.top = coords.top - viewClientRect.top;
|
|
631
|
+
ref.current.style.left = coords.left - viewClientRect.left;
|
|
632
|
+
})
|
|
633
|
+
|
|
634
|
+
return (
|
|
635
|
+
<div
|
|
636
|
+
ref={ref}
|
|
637
|
+
style={{
|
|
638
|
+
position: "absolute"
|
|
639
|
+
}}
|
|
640
|
+
/>
|
|
641
|
+
)
|
|
642
|
+
}
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
### `NodeViewComponentProps`
|
|
646
|
+
|
|
647
|
+
```tsx
|
|
648
|
+
type NodeViewComponentProps = {
|
|
649
|
+
nodeProps: {
|
|
650
|
+
decorations: readonly Decoration[];
|
|
651
|
+
innerDecorations: DecorationSource;
|
|
652
|
+
node: Node;
|
|
653
|
+
children?: ReactNode | ReactNode[];
|
|
654
|
+
getPos: () => number;
|
|
655
|
+
};
|
|
656
|
+
} & HTMLAttributes<HTMLElement>;
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
The props that will be passed to all node view components. These props map
|
|
660
|
+
directly to the arguments passed to
|
|
661
|
+
[`NodeViewConstructor` functions](https://prosemirror.net/docs/ref/#view.NodeViewConstructor)
|
|
662
|
+
by the default ProseMirror EditorView implementation.
|
|
663
|
+
|
|
664
|
+
Node view components may also be passed _any_ other valid HTML attribute props,
|
|
665
|
+
and should pass them through to their top-level DOM element.
|
|
666
|
+
[See the above example](#building-node-views-with-react) for more details.
|
|
667
|
+
|
|
668
|
+
In addition to accepting these props, all node view components _must_ forward
|
|
669
|
+
their ref to their top-level DOM element.
|
|
670
|
+
|
|
671
|
+
### `useStopEvent`
|
|
672
|
+
|
|
673
|
+
```tsx
|
|
674
|
+
type useStopEvent = (stopEvent: (view: EditorView, event: Event) => boolean): void
|
|
675
|
+
```
|
|
676
|
+
|
|
677
|
+
This hook can be used within a node view component to register a
|
|
678
|
+
[stopEvent handler](https://prosemirror.net/docs/ref/#view.NodeView.stopEvent).
|
|
679
|
+
Events for which this returns true are not handled by the editor.
|
|
680
|
+
|
|
681
|
+
### `useSelectNode`
|
|
682
|
+
|
|
683
|
+
```tsx
|
|
684
|
+
type useSelectNode = (selectNode: () => void, deselectNode?: () => void): void
|
|
685
|
+
```
|
|
686
|
+
|
|
687
|
+
This hook can be used within a node view component to register
|
|
688
|
+
[selectNode and deselectNode handlers](https://prosemirror.net/docs/ref/#view.NodeView.selectNode).
|
|
689
|
+
The selectNode handler will only be called when a NodeSelection is created whose
|
|
690
|
+
node is this one.
|
|
691
|
+
|
|
692
|
+
### `widget`
|
|
693
|
+
|
|
694
|
+
```tsx
|
|
695
|
+
type widget = (
|
|
696
|
+
pos: number,
|
|
697
|
+
component: ForwardRefExoticComponent<
|
|
698
|
+
RefAttributes<HTMLElement> & WidgetComponentProps
|
|
699
|
+
>,
|
|
700
|
+
spec?: ReactWidgetSpec
|
|
701
|
+
) => Decoration(pos, pos, new ReactWidgetType(component, spec))
|
|
702
|
+
```
|
|
703
|
+
|
|
704
|
+
Like ProseMirror View's `Decoration.widget`, but with support for React
|
|
705
|
+
components.
|