@prosekit/react 0.1.6 → 0.2.1

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.
@@ -38,9 +38,10 @@ import type { Node as Node_2 } from '@prosekit/pm/model';
38
38
  import type { NodeViewConstructor } from '@prosekit/pm/view';
39
39
  import { Options } from 'tsup';
40
40
  import { Popover as Popover_2 } from '@prosekit/lit/popover';
41
- import { PopoverOptions } from '@prosekit/lit/autocomplete-popover';
42
41
  import { PopoverProps as PopoverProps_2 } from '@prosekit/lit/popover';
42
+ import { PositioningOptions } from '@prosekit/lit/autocomplete-popover';
43
43
  import { Priority } from '@prosekit/core';
44
+ import { Provider } from 'react';
44
45
  import { default as React_2 } from 'react';
45
46
  import { ReactNode } from 'react';
46
47
  import { ReactPortal } from 'react';
@@ -125,11 +126,10 @@ export { defineReactNodeView as defineReactNodeView_alias_1 }
125
126
  */
126
127
  export declare function defineReactNodeViewFactory(nodeViewFactory: NodeViewFactory): Extension<ExtensionTyping<string, string, CommandArgs>>;
127
128
 
128
- export declare interface EditorContext {
129
- editor: Editor;
130
- }
131
-
132
- export declare const editorContext: Context<EditorContext | null>;
129
+ /**
130
+ * @internal
131
+ */
132
+ export declare const EditorContextProvider: Provider<Editor<any> | null>;
133
133
 
134
134
  export declare const InlinePopover: React_2.ComponentType<InlinePopoverProps & React_2.RefAttributes<InlinePopover_2>>;
135
135
 
@@ -139,10 +139,10 @@ export declare type NodeViewFactory = (options: ReactNodeViewUserOptions) => Nod
139
139
 
140
140
  export declare const Popover: React_2.ComponentType<PopoverProps & React_2.RefAttributes<Popover_2>>;
141
141
 
142
- export { PopoverOptions }
143
-
144
142
  export declare type PopoverProps = React_2.PropsWithChildren<PropsWithClassName<PopoverProps_2>>;
145
143
 
144
+ export { PositioningOptions }
145
+
146
146
  /**
147
147
  * @internal
148
148
  */
@@ -152,6 +152,11 @@ declare type PropsWithClassName<P = unknown> = P & {
152
152
  export { PropsWithClassName }
153
153
  export { PropsWithClassName as PropsWithClassName_alias_1 }
154
154
 
155
+ /**
156
+ * The root component for a ProseKit editor.
157
+ *
158
+ * @public
159
+ */
155
160
  declare const ProseKit: ComponentType<ProseKitProps>;
156
161
  export { ProseKit }
157
162
  export { ProseKit as ProseKit_alias_1 }
@@ -261,25 +266,52 @@ declare function useEditor<E extends Extension = any>(options?: {
261
266
  export { useEditor }
262
267
  export { useEditor as useEditor_alias_1 }
263
268
 
269
+ /**
270
+ * @internal
271
+ */
272
+ export declare function useEditorContext<E extends Extension>(): Editor<E> | null;
273
+
274
+ /**
275
+ * @internal
276
+ */
277
+ export declare function useEditorExtension(editor: Editor | null | undefined, extension: Extension | null): void;
278
+
264
279
  /**
265
280
  * Add an extension to the editor.
266
- *
267
- * It accepts an optional extension. If the extension is changed, the previous
281
+ */
282
+ declare function useExtension(
283
+ /**
284
+ * The extension to add to the editor. If it changes, the previous
268
285
  * extension will be removed and the new one (if not null) will be added.
269
286
  */
270
- declare function useExtension<T extends Extension = Extension>(extension: T | null): void;
287
+ extension: Extension | null, options?: UseExtensionOptions): void;
271
288
  export { useExtension }
272
289
  export { useExtension as useExtension_alias_1 }
273
290
 
274
- declare function useKeymap(keymap: Keymap, options?: {
291
+ declare interface UseExtensionOptions {
292
+ /**
293
+ * The editor to add the extension to. If not provided, it will use the
294
+ * editor from the nearest `ProseKit` component.
295
+ */
296
+ editor?: Editor;
297
+ /**
298
+ * Optional priority to add the extension with.
299
+ */
275
300
  priority?: Priority;
276
- }): void;
301
+ }
302
+ export { UseExtensionOptions }
303
+ export { UseExtensionOptions as UseExtensionOptions_alias_1 }
304
+
305
+ declare function useKeymap(keymap: Keymap, options?: UseExtensionOptions): void;
277
306
  export { useKeymap }
278
307
  export { useKeymap as useKeymap_alias_1 }
279
308
 
280
309
  export declare function useNodeViewFactory(): NodeViewFactory;
281
310
 
282
- export declare function usePriorityExtension<T extends Extension = Extension>(extension: T | null, priority?: Priority | null): void;
311
+ /**
312
+ * @internal
313
+ */
314
+ export declare function usePriorityExtension<T extends Extension = Extension>(extension: T | null, priority?: Priority | null): T | null;
283
315
 
284
316
  export declare function useReactNodeViewCreator(renderReactRenderer: ReactRendererResult['renderReactRenderer'], removeReactRenderer: ReactRendererResult['removeReactRenderer']): (options: ReactNodeViewUserOptions) => NodeViewConstructor;
285
317
 
@@ -1,3 +1,3 @@
1
- export { PopoverOptions } from './_tsup-dts-rollup';
1
+ export { PositioningOptions } from './_tsup-dts-rollup';
2
2
  export { AutocompletePopover_alias_1 as AutocompletePopover } from './_tsup-dts-rollup';
3
3
  export { AutocompletePopoverProps_alias_1 as AutocompletePopoverProps } from './_tsup-dts-rollup';
@@ -5,6 +5,7 @@ export { defineReactNodeView } from './_tsup-dts-rollup';
5
5
  export { ReactNodeViewOptions } from './_tsup-dts-rollup';
6
6
  export { useEditor } from './_tsup-dts-rollup';
7
7
  export { useExtension } from './_tsup-dts-rollup';
8
+ export { UseExtensionOptions } from './_tsup-dts-rollup';
8
9
  export { useKeymap } from './_tsup-dts-rollup';
9
10
  export { PropsWithClassName } from './_tsup-dts-rollup';
10
11
  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,32 +256,54 @@ 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);
295
307
  }
296
308
  export {
297
309
  ProseKit,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/react",
3
3
  "type": "module",
4
- "version": "0.1.6",
4
+ "version": "0.2.1",
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.1.9",
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'