@prosekit/react 0.2.0 → 0.2.2

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.
@@ -27,6 +27,7 @@ import type { CoreNodeViewUserOptions } from '@prosemirror-adapter/core';
27
27
  import type { Decoration } from '@prosekit/pm/view';
28
28
  import type { DecorationSource } from '@prosekit/pm/view';
29
29
  import { Editor } from '@prosekit/core';
30
+ import type { EditorState } from '@prosekit/pm/state';
30
31
  import type { EditorView } from '@prosekit/pm/view';
31
32
  import { Extension } from '@prosekit/core';
32
33
  import { ExtensionTyping } from '@prosekit/core';
@@ -41,6 +42,7 @@ import { Popover as Popover_2 } from '@prosekit/lit/popover';
41
42
  import { PopoverProps as PopoverProps_2 } from '@prosekit/lit/popover';
42
43
  import { PositioningOptions } from '@prosekit/lit/autocomplete-popover';
43
44
  import { Priority } from '@prosekit/core';
45
+ import { Provider } from 'react';
44
46
  import { default as React_2 } from 'react';
45
47
  import { ReactNode } from 'react';
46
48
  import { ReactPortal } from 'react';
@@ -125,11 +127,10 @@ export { defineReactNodeView as defineReactNodeView_alias_1 }
125
127
  */
126
128
  export declare function defineReactNodeViewFactory(nodeViewFactory: NodeViewFactory): Extension<ExtensionTyping<string, string, CommandArgs>>;
127
129
 
128
- export declare interface EditorContext {
129
- editor: Editor;
130
- }
131
-
132
- export declare const editorContext: Context<EditorContext | null>;
130
+ /**
131
+ * @internal
132
+ */
133
+ export declare const EditorContextProvider: Provider<Editor<any> | null>;
133
134
 
134
135
  export declare const InlinePopover: React_2.ComponentType<InlinePopoverProps & React_2.RefAttributes<InlinePopover_2>>;
135
136
 
@@ -152,6 +153,11 @@ declare type PropsWithClassName<P = unknown> = P & {
152
153
  export { PropsWithClassName }
153
154
  export { PropsWithClassName as PropsWithClassName_alias_1 }
154
155
 
156
+ /**
157
+ * The root component for a ProseKit editor.
158
+ *
159
+ * @public
160
+ */
155
161
  declare const ProseKit: ComponentType<ProseKitProps>;
156
162
  export { ProseKit }
157
163
  export { ProseKit as ProseKit_alias_1 }
@@ -261,28 +267,64 @@ declare function useEditor<E extends Extension = any>(options?: {
261
267
  export { useEditor }
262
268
  export { useEditor as useEditor_alias_1 }
263
269
 
270
+ /**
271
+ * @internal
272
+ */
273
+ export declare function useEditorContext<E extends Extension>(): Editor<E> | null;
274
+
275
+ /**
276
+ * @internal
277
+ */
278
+ export declare function useEditorExtension(editor: Editor | null | undefined, extension: Extension | null): void;
279
+
264
280
  /**
265
281
  * Add an extension to the editor.
266
- *
267
- * It accepts an optional extension. If the extension is changed, the previous
282
+ */
283
+ declare function useExtension(
284
+ /**
285
+ * The extension to add to the editor. If it changes, the previous
268
286
  * extension will be removed and the new one (if not null) will be added.
269
287
  */
270
- declare function useExtension<T extends Extension = Extension>(extension: T | null): void;
288
+ extension: Extension | null, options?: UseExtensionOptions): void;
271
289
  export { useExtension }
272
290
  export { useExtension as useExtension_alias_1 }
273
291
 
274
- declare function useKeymap(keymap: Keymap, options?: {
292
+ declare interface UseExtensionOptions {
293
+ /**
294
+ * The editor to add the extension to. If not provided, it will use the
295
+ * editor from the nearest `ProseKit` component.
296
+ */
297
+ editor?: Editor;
298
+ /**
299
+ * Optional priority to add the extension with.
300
+ */
275
301
  priority?: Priority;
276
- }): void;
302
+ }
303
+ export { UseExtensionOptions }
304
+ export { UseExtensionOptions as UseExtensionOptions_alias_1 }
305
+
306
+ declare function useKeymap(keymap: Keymap, options?: UseExtensionOptions): void;
277
307
  export { useKeymap }
278
308
  export { useKeymap as useKeymap_alias_1 }
279
309
 
280
310
  export declare function useNodeViewFactory(): NodeViewFactory;
281
311
 
282
- export declare function usePriorityExtension<T extends Extension = Extension>(extension: T | null, priority?: Priority | null): void;
312
+ /**
313
+ * @internal
314
+ */
315
+ export declare function usePriorityExtension<T extends Extension = Extension>(extension: T | null, priority?: Priority | null): T | null;
283
316
 
284
317
  export declare function useReactNodeViewCreator(renderReactRenderer: ReactRendererResult['renderReactRenderer'], removeReactRenderer: ReactRendererResult['removeReactRenderer']): (options: ReactNodeViewUserOptions) => NodeViewConstructor;
285
318
 
286
319
  export declare function useReactRenderer(): ReactRendererResult;
287
320
 
321
+ /**
322
+ * Calls the given handler whenever the editor state changes.
323
+ *
324
+ * @public
325
+ */
326
+ declare function useStateUpdate(handler: (state: EditorState) => void, options?: UseExtensionOptions): void;
327
+ export { useStateUpdate }
328
+ export { useStateUpdate as useStateUpdate_alias_1 }
329
+
288
330
  export { }
@@ -1,10 +1,12 @@
1
- export { ReactNodeViewComponent } from './_tsup-dts-rollup';
2
1
  export { ProseKit } from './_tsup-dts-rollup';
3
2
  export { ProseKitProps } from './_tsup-dts-rollup';
4
3
  export { defineReactNodeView } from './_tsup-dts-rollup';
5
4
  export { ReactNodeViewOptions } from './_tsup-dts-rollup';
6
5
  export { useEditor } from './_tsup-dts-rollup';
7
6
  export { useExtension } from './_tsup-dts-rollup';
7
+ export { UseExtensionOptions } from './_tsup-dts-rollup';
8
8
  export { useKeymap } from './_tsup-dts-rollup';
9
+ export { useStateUpdate } from './_tsup-dts-rollup';
9
10
  export { PropsWithClassName } from './_tsup-dts-rollup';
11
+ export { ReactNodeViewComponent } from './_tsup-dts-rollup';
10
12
  export { ReactNodeViewProps } from './_tsup-dts-rollup';
@@ -3,11 +3,15 @@ import "@prosekit/core";
3
3
  import { createElement as createElement3 } from "react";
4
4
 
5
5
  // src/contexts/editor-context.ts
6
- import { createContext } from "react";
6
+ import { createContext, useContext } from "react";
7
7
  var editorContext = createContext(null);
8
+ function useEditorContext() {
9
+ return useContext(editorContext);
10
+ }
11
+ var EditorContextProvider = editorContext.Provider;
8
12
 
9
13
  // src/views/react-views-consumer.ts
10
- import { useMemo } from "react";
14
+ import { useMemo as useMemo2 } from "react";
11
15
 
12
16
  // src/extensions/react-node-view.ts
13
17
  import {
@@ -30,61 +34,47 @@ function defineReactNodeViewFactory(nodeViewFactory) {
30
34
 
31
35
  // src/hooks/use-extension.ts
32
36
  import "@prosekit/core";
33
- import { useEffect as useEffect2 } from "react";
34
37
 
35
- // src/hooks/use-editor.ts
36
- import {
37
- ProseKitError,
38
- defineUpdateHandler,
39
- union,
40
- defineMountHandler
41
- } from "@prosekit/core";
42
- import { useContext, useEffect, useReducer } from "react";
43
- function useEditor(options) {
44
- var _a;
45
- const update = (_a = options == null ? void 0 : options.update) != null ? _a : false;
46
- const value = useContext(editorContext);
47
- if (!value) {
48
- throw new ProseKitError(
49
- "useEditor must be used within the ProseKit component"
50
- );
38
+ // src/hooks/use-editor-extension.ts
39
+ import { EditorNotFoundError } from "@prosekit/core";
40
+ import { useEffect } from "react";
41
+ function useEditorExtension(editor, extension) {
42
+ if (!editor) {
43
+ throw new EditorNotFoundError();
51
44
  }
52
- const editor = value.editor;
53
- const forceUpdate = useForceUpdate();
54
45
  useEffect(() => {
55
- if (update) {
56
- const extension = union([
57
- defineMountHandler(forceUpdate),
58
- defineUpdateHandler(forceUpdate)
59
- ]);
46
+ if (extension) {
60
47
  return editor.use(extension);
61
48
  }
62
- }, [editor, update, forceUpdate]);
63
- return editor;
49
+ }, [editor, extension]);
64
50
  }
65
- function useForceUpdate() {
66
- const [, dispatch] = useReducer((x) => x + 1, 0);
67
- return dispatch;
51
+
52
+ // src/hooks/use-priority-extension.ts
53
+ import { withPriority } from "@prosekit/core";
54
+ import { useMemo } from "react";
55
+ function usePriorityExtension(extension, priority) {
56
+ return useMemo(() => {
57
+ return extension && priority ? withPriority(extension, priority) : extension;
58
+ }, [extension, priority]);
68
59
  }
69
60
 
70
61
  // src/hooks/use-extension.ts
71
- function useExtension(extension) {
72
- const editor = useEditor();
73
- useEffect2(() => {
74
- if (extension) {
75
- return editor.use(extension);
76
- }
77
- }, [editor, extension]);
62
+ function useExtension(extension, options) {
63
+ const editorContext2 = useEditorContext();
64
+ useEditorExtension(
65
+ (options == null ? void 0 : options.editor) || editorContext2,
66
+ usePriorityExtension(extension, options == null ? void 0 : options.priority)
67
+ );
78
68
  }
79
69
 
80
70
  // src/views/node-view/node-view-context.ts
81
- import { ProseKitError as ProseKitError2 } from "@prosekit/core";
71
+ import { ProseKitError } from "@prosekit/core";
82
72
  import { createContext as createContext2, useContext as useContext2 } from "react";
83
73
  var createNodeViewContext = createContext2(null);
84
74
  function useNodeViewFactory() {
85
75
  const nodeViewFactory = useContext2(createNodeViewContext);
86
76
  if (!nodeViewFactory) {
87
- throw new ProseKitError2("Cannot find node view factory context.");
77
+ throw new ProseKitError("Cannot find node view factory context.");
88
78
  }
89
79
  return nodeViewFactory;
90
80
  }
@@ -92,7 +82,7 @@ function useNodeViewFactory() {
92
82
  // src/views/react-views-consumer.ts
93
83
  var ReactViewsConsumer = () => {
94
84
  const nodeViewFactory = useNodeViewFactory();
95
- const extension = useMemo(
85
+ const extension = useMemo2(
96
86
  () => defineReactNodeViewFactory(nodeViewFactory),
97
87
  [nodeViewFactory]
98
88
  );
@@ -101,7 +91,7 @@ var ReactViewsConsumer = () => {
101
91
  };
102
92
 
103
93
  // src/views/react-views-provider.ts
104
- import { createElement as createElement2, useMemo as useMemo2 } from "react";
94
+ import { createElement as createElement2, useMemo as useMemo3 } from "react";
105
95
 
106
96
  // src/views/node-view/use-react-node-view-creator.ts
107
97
  import { useCallback } from "react";
@@ -190,12 +180,12 @@ function useReactNodeViewCreator(renderReactRenderer, removeReactRenderer) {
190
180
  }
191
181
 
192
182
  // src/views/react-renderer.ts
193
- import { useCallback as useCallback2, useEffect as useEffect3, useRef, useState } from "react";
183
+ import { useCallback as useCallback2, useEffect as useEffect2, useRef, useState } from "react";
194
184
  import { flushSync } from "react-dom";
195
185
  function useReactRenderer() {
196
186
  const [portals, setPortals] = useState({});
197
187
  const mountedRef = useRef(false);
198
- useEffect3(() => {
188
+ useEffect2(() => {
199
189
  requestAnimationFrame(() => {
200
190
  mountedRef.current = true;
201
191
  });
@@ -250,7 +240,7 @@ var ReactViewsProvider = ({
250
240
  renderReactRenderer,
251
241
  removeReactRenderer
252
242
  );
253
- const memoizedPortals = useMemo2(() => Object.values(portals), [portals]);
243
+ const memoizedPortals = useMemo3(() => Object.values(portals), [portals]);
254
244
  return createElement2(
255
245
  createNodeViewContext.Provider,
256
246
  { value: createReactNodeView },
@@ -266,37 +256,67 @@ var ProseKit = (props) => {
266
256
  ReactViewsProvider,
267
257
  null,
268
258
  createElement3(
269
- editorContext.Provider,
270
- { value: { editor } },
259
+ EditorContextProvider,
260
+ { value: editor },
271
261
  createElement3(ReactViewsConsumer),
272
262
  children
273
263
  )
274
264
  );
275
265
  };
276
266
 
277
- // src/hooks/use-keymap.ts
278
- import { defineKeymap } from "@prosekit/core";
279
- import { useMemo as useMemo4 } from "react";
280
-
281
- // src/hooks/use-priority-extension.ts
282
- import { withPriority } from "@prosekit/core";
283
- import { useMemo as useMemo3 } from "react";
284
- function usePriorityExtension(extension, priority) {
285
- const extensionWithPriority = useMemo3(() => {
286
- return extension && priority ? withPriority(extension, priority) : extension;
287
- }, [extension, priority]);
288
- return useExtension(extensionWithPriority);
267
+ // src/hooks/use-editor.ts
268
+ import {
269
+ ProseKitError as ProseKitError2,
270
+ defineMountHandler,
271
+ defineUpdateHandler,
272
+ union
273
+ } from "@prosekit/core";
274
+ import { useEffect as useEffect3, useReducer } from "react";
275
+ function useEditor(options) {
276
+ var _a;
277
+ const update = (_a = options == null ? void 0 : options.update) != null ? _a : false;
278
+ const editor = useEditorContext();
279
+ if (!editor) {
280
+ throw new ProseKitError2(
281
+ "useEditor must be used within the ProseKit component"
282
+ );
283
+ }
284
+ const forceUpdate = useForceUpdate();
285
+ useEffect3(() => {
286
+ if (update) {
287
+ const extension = union([
288
+ defineMountHandler(forceUpdate),
289
+ defineUpdateHandler(forceUpdate)
290
+ ]);
291
+ return editor.use(extension);
292
+ }
293
+ }, [editor, update, forceUpdate]);
294
+ return editor;
295
+ }
296
+ function useForceUpdate() {
297
+ const [, dispatch] = useReducer((x) => x + 1, 0);
298
+ return dispatch;
289
299
  }
290
300
 
291
301
  // src/hooks/use-keymap.ts
302
+ import { defineKeymap } from "@prosekit/core";
303
+ import { useMemo as useMemo4 } from "react";
292
304
  function useKeymap(keymap, options) {
293
305
  const extension = useMemo4(() => defineKeymap(keymap), [keymap]);
294
- usePriorityExtension(extension, options == null ? void 0 : options.priority);
306
+ return useExtension(extension, options);
307
+ }
308
+
309
+ // src/hooks/use-state-update.ts
310
+ import { defineUpdateHandler as defineUpdateHandler2 } from "@prosekit/core";
311
+ function useStateUpdate(handler, options) {
312
+ const extension = defineUpdateHandler2((view) => handler(view.state));
313
+ return useExtension(extension, options);
295
314
  }
296
315
  export {
297
316
  ProseKit,
298
317
  defineReactNodeView,
299
318
  useEditor,
300
319
  useExtension,
301
- useKeymap
320
+ useKeymap,
321
+ useStateUpdate
302
322
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/react",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.2.2",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -96,8 +96,8 @@
96
96
  ],
97
97
  "dependencies": {
98
98
  "@lit/react": "^1.0.2",
99
- "@prosekit/core": "^0.2.5",
100
- "@prosekit/lit": "^0.2.0",
99
+ "@prosekit/core": "^0.2.7",
100
+ "@prosekit/lit": "^0.2.1",
101
101
  "@prosekit/pm": "^0.1.1",
102
102
  "@prosemirror-adapter/core": "^0.2.6"
103
103
  },
@@ -115,13 +115,13 @@
115
115
  },
116
116
  "devDependencies": {
117
117
  "@prosekit/dev": "*",
118
- "@types/react": "^18.2.47",
118
+ "@types/react": "^18.2.48",
119
119
  "@types/react-dom": "^18.2.18",
120
120
  "react": "^18.2.0",
121
121
  "react-dom": "^18.2.0",
122
122
  "tsup": "^8.0.1",
123
123
  "typescript": "^5.3.3",
124
- "vitest": "^1.2.0"
124
+ "vitest": "^1.2.1"
125
125
  },
126
126
  "scripts": {
127
127
  "build:tsup": "tsup",
package/src/index.ts DELETED
@@ -1,11 +0,0 @@
1
- export type { ReactNodeViewComponent } from './views/node-view/react-node-view-options'
2
- export { ProseKit, type ProseKitProps } from './components/prosekit'
3
- export {
4
- defineReactNodeView,
5
- type ReactNodeViewOptions,
6
- } from './extensions/react-node-view'
7
- export { useEditor } from './hooks/use-editor'
8
- export { useExtension } from './hooks/use-extension'
9
- export { useKeymap } from './hooks/use-keymap'
10
- export { type PropsWithClassName } from './types'
11
- export type { ReactNodeViewProps } from './views/node-view/react-node-view-props'