@prosekit/react 0.1.1 → 0.1.3
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/dist/_tsup-dts-rollup.d.ts +141 -79
- package/dist/prosekit-react.d.ts +3 -3
- package/dist/prosekit-react.js +217 -58
- package/package.json +6 -5
- package/src/index.ts +4 -3
@@ -1,5 +1,6 @@
|
|
1
1
|
/// <reference types="react" />
|
2
2
|
|
3
|
+
import type { Attrs } from '@prosekit/pm/model';
|
3
4
|
import { AutocompleteEmpty as AutocompleteEmpty_2 } from '@prosekit/lit/autocomplete-empty';
|
4
5
|
import { AutocompleteEmptyProps as AutocompleteEmptyProps_2 } from '@prosekit/lit/autocomplete-empty';
|
5
6
|
import { AutocompleteItem as AutocompleteItem_2 } from '@prosekit/lit/autocomplete-item';
|
@@ -19,109 +20,105 @@ import { ComboBoxProps as ComboBoxProps_2 } from '@prosekit/lit/combo-box';
|
|
19
20
|
import { CommandArgs } from '@prosekit/core';
|
20
21
|
import { ComponentType } from 'react';
|
21
22
|
import { Context } from 'react';
|
23
|
+
import { CoreNodeView } from '@prosemirror-adapter/core';
|
24
|
+
import type { CoreNodeViewSpec } from '@prosemirror-adapter/core';
|
25
|
+
import type { CoreNodeViewUserOptions } from '@prosemirror-adapter/core';
|
26
|
+
import type { Decoration } from '@prosekit/pm/view';
|
27
|
+
import type { DecorationSource } from '@prosekit/pm/view';
|
22
28
|
import { Editor } from '@prosekit/core';
|
29
|
+
import type { EditorView } from '@prosekit/pm/view';
|
23
30
|
import { Extension } from '@prosekit/core';
|
24
31
|
import { ExtensionTyping } from '@prosekit/core';
|
25
|
-
import
|
32
|
+
import { FC } from 'react';
|
26
33
|
import { InlinePopover as InlinePopover_2 } from '@prosekit/lit/inline-popover';
|
27
34
|
import { InlinePopoverProps as InlinePopoverProps_2 } from '@prosekit/lit/inline-popover';
|
28
35
|
import { Keymap } from '@prosekit/core';
|
29
|
-
import type {
|
36
|
+
import type { Node as Node_2 } from '@prosekit/pm/model';
|
37
|
+
import type { NodeViewConstructor } from '@prosekit/pm/view';
|
30
38
|
import { Options } from 'tsup';
|
31
39
|
import { Popover as Popover_2 } from '@prosekit/lit/popover';
|
32
40
|
import { PopoverOptions } from '@prosekit/lit/autocomplete-popover';
|
33
41
|
import { PopoverProps as PopoverProps_2 } from '@prosekit/lit/popover';
|
34
|
-
import
|
42
|
+
import { Priority } from '@prosekit/core';
|
35
43
|
import { default as React_2 } from 'react';
|
36
|
-
import
|
37
|
-
import
|
38
|
-
import { useNodeViewContext } from '@prosemirror-adapter/react';
|
39
|
-
import { useNodeViewFactory } from '@prosemirror-adapter/react';
|
40
|
-
import { UserProjectConfigExport } from 'vitest/dist/config.js';
|
44
|
+
import { ReactNode } from 'react';
|
45
|
+
import { ReactPortal } from 'react';
|
41
46
|
|
42
|
-
declare const AutocompleteEmpty:
|
47
|
+
declare const AutocompleteEmpty: React_2.ComponentType<AutocompleteEmptyProps & React_2.RefAttributes<AutocompleteEmpty_2>>;
|
43
48
|
export { AutocompleteEmpty }
|
44
49
|
export { AutocompleteEmpty as AutocompleteEmpty_alias_1 }
|
45
50
|
|
46
|
-
declare type AutocompleteEmptyProps =
|
47
|
-
className?: string;
|
48
|
-
children?: React_2.ReactNode;
|
49
|
-
} & AutocompleteEmptyProps_2>;
|
51
|
+
declare type AutocompleteEmptyProps = React_2.PropsWithChildren<PropsWithClassName<AutocompleteEmptyProps_2>>;
|
50
52
|
export { AutocompleteEmptyProps }
|
51
53
|
export { AutocompleteEmptyProps as AutocompleteEmptyProps_alias_1 }
|
52
54
|
|
53
|
-
declare const AutocompleteItem:
|
55
|
+
declare const AutocompleteItem: React_2.ComponentType<AutocompleteItemProps & React_2.RefAttributes<AutocompleteItem_2>>;
|
54
56
|
export { AutocompleteItem }
|
55
57
|
export { AutocompleteItem as AutocompleteItem_alias_1 }
|
56
58
|
|
57
|
-
declare type AutocompleteItemProps =
|
58
|
-
className?: string;
|
59
|
-
children?: React_2.ReactNode;
|
60
|
-
} & AutocompleteItemProps_2>;
|
59
|
+
declare type AutocompleteItemProps = React_2.PropsWithChildren<PropsWithClassName<AutocompleteItemProps_2>>;
|
61
60
|
export { AutocompleteItemProps }
|
62
61
|
export { AutocompleteItemProps as AutocompleteItemProps_alias_1 }
|
63
62
|
|
64
|
-
declare const AutocompleteList:
|
63
|
+
declare const AutocompleteList: React_2.ComponentType<AutocompleteListProps & React_2.RefAttributes<AutocompleteList_2>>;
|
65
64
|
export { AutocompleteList }
|
66
65
|
export { AutocompleteList as AutocompleteList_alias_1 }
|
67
66
|
|
68
|
-
declare type AutocompleteListProps =
|
69
|
-
className?: string;
|
70
|
-
children?: React_2.ReactNode;
|
71
|
-
} & AutocompleteListProps_2>;
|
67
|
+
declare type AutocompleteListProps = React_2.PropsWithChildren<PropsWithClassName<AutocompleteListProps_2>>;
|
72
68
|
export { AutocompleteListProps }
|
73
69
|
export { AutocompleteListProps as AutocompleteListProps_alias_1 }
|
74
70
|
|
75
|
-
declare const AutocompletePopover:
|
71
|
+
declare const AutocompletePopover: React_2.ComponentType<AutocompletePopoverProps & React_2.RefAttributes<AutocompletePopover_2>>;
|
76
72
|
export { AutocompletePopover }
|
77
73
|
export { AutocompletePopover as AutocompletePopover_alias_1 }
|
78
74
|
|
79
|
-
declare type AutocompletePopoverProps =
|
80
|
-
className?: string;
|
81
|
-
children?: React_2.ReactNode;
|
82
|
-
} & AutocompletePopoverProps_2>;
|
75
|
+
declare type AutocompletePopoverProps = React_2.PropsWithChildren<PropsWithClassName<AutocompletePopoverProps_2>>;
|
83
76
|
export { AutocompletePopoverProps }
|
84
77
|
export { AutocompletePopoverProps as AutocompletePopoverProps_alias_1 }
|
85
78
|
|
86
|
-
export declare const ComboBox:
|
79
|
+
export declare const ComboBox: React_2.ComponentType<ComboBoxProps & React_2.RefAttributes<ComboBox_2>>;
|
87
80
|
|
88
|
-
export declare const ComboBoxInput:
|
81
|
+
export declare const ComboBoxInput: React_2.ComponentType<ComboBoxInputProps & React_2.RefAttributes<ComboBoxInput_2>>;
|
89
82
|
|
90
|
-
export declare type ComboBoxInputProps =
|
91
|
-
className?: string;
|
92
|
-
children?: React_2.ReactNode;
|
93
|
-
} & ComboBoxInputProps_2>;
|
83
|
+
export declare type ComboBoxInputProps = React_2.PropsWithChildren<PropsWithClassName<ComboBoxInputProps_2>>;
|
94
84
|
|
95
|
-
export declare const ComboBoxItem:
|
85
|
+
export declare const ComboBoxItem: React_2.ComponentType<ComboBoxItemProps & React_2.RefAttributes<ComboBoxItem_2>>;
|
96
86
|
|
97
|
-
export declare type ComboBoxItemProps =
|
98
|
-
className?: string;
|
99
|
-
children?: React_2.ReactNode;
|
100
|
-
} & ComboBoxItemProps_2>;
|
87
|
+
export declare type ComboBoxItemProps = React_2.PropsWithChildren<PropsWithClassName<ComboBoxItemProps_2>>;
|
101
88
|
|
102
|
-
export declare const ComboBoxList:
|
89
|
+
export declare const ComboBoxList: React_2.ComponentType<ComboBoxListProps & React_2.RefAttributes<ComboBoxList_2>>;
|
103
90
|
|
104
|
-
export declare type ComboBoxListProps =
|
105
|
-
className?: string;
|
106
|
-
children?: React_2.ReactNode;
|
107
|
-
} & ComboBoxListProps_2>;
|
91
|
+
export declare type ComboBoxListProps = React_2.PropsWithChildren<PropsWithClassName<ComboBoxListProps_2>>;
|
108
92
|
|
109
|
-
export declare type ComboBoxProps =
|
110
|
-
|
111
|
-
|
112
|
-
|
93
|
+
export declare type ComboBoxProps = React_2.PropsWithChildren<PropsWithClassName<ComboBoxProps_2>>;
|
94
|
+
|
95
|
+
export declare const createNodeViewContext: Context<NodeViewFactory | null>;
|
96
|
+
|
97
|
+
declare type CreateReactNodeView = ReturnType<typeof useReactNodeViewCreator>;
|
98
|
+
export { CreateReactNodeView }
|
99
|
+
export { CreateReactNodeView as CreateReactNodeView_alias_1 }
|
113
100
|
|
114
101
|
export declare const default_alias: Options | Options[] | ((overrideOptions: Options) => Options | Options[] | Promise<Options | Options[]>);
|
115
102
|
|
116
|
-
export declare const default_alias_1:
|
103
|
+
export declare const default_alias_1: {
|
104
|
+
test: {
|
105
|
+
environment: "jsdom";
|
106
|
+
};
|
107
|
+
};
|
117
108
|
|
109
|
+
/**
|
110
|
+
* Defines a node view using a React component.
|
111
|
+
*
|
112
|
+
* @public
|
113
|
+
*/
|
118
114
|
declare function defineReactNodeView(options: ReactNodeViewOptions): Extension;
|
119
115
|
export { defineReactNodeView }
|
120
116
|
export { defineReactNodeView as defineReactNodeView_alias_1 }
|
121
117
|
|
122
|
-
|
123
|
-
|
124
|
-
|
118
|
+
/**
|
119
|
+
* @internal
|
120
|
+
*/
|
121
|
+
export declare function defineReactNodeViewFactory(nodeViewFactory: NodeViewFactory): Extension<ExtensionTyping<string, string, CommandArgs>>;
|
125
122
|
|
126
123
|
export declare interface EditorContext {
|
127
124
|
editor: Editor;
|
@@ -129,21 +126,26 @@ export declare interface EditorContext {
|
|
129
126
|
|
130
127
|
export declare const editorContext: Context<EditorContext | null>;
|
131
128
|
|
132
|
-
export declare const InlinePopover:
|
129
|
+
export declare const InlinePopover: React_2.ComponentType<InlinePopoverProps & React_2.RefAttributes<InlinePopover_2>>;
|
130
|
+
|
131
|
+
export declare type InlinePopoverProps = React_2.PropsWithChildren<PropsWithClassName<InlinePopoverProps_2>>;
|
133
132
|
|
134
|
-
export declare type
|
135
|
-
className?: string;
|
136
|
-
children?: React_2.ReactNode;
|
137
|
-
} & InlinePopoverProps_2>;
|
133
|
+
export declare type NodeViewFactory = (options: ReactNodeViewUserOptions) => NodeViewConstructor;
|
138
134
|
|
139
|
-
export declare const Popover:
|
135
|
+
export declare const Popover: React_2.ComponentType<PopoverProps & React_2.RefAttributes<Popover_2>>;
|
140
136
|
|
141
137
|
export { PopoverOptions }
|
142
138
|
|
143
|
-
export declare type PopoverProps =
|
144
|
-
|
145
|
-
|
146
|
-
|
139
|
+
export declare type PopoverProps = React_2.PropsWithChildren<PropsWithClassName<PopoverProps_2>>;
|
140
|
+
|
141
|
+
/**
|
142
|
+
* @internal
|
143
|
+
*/
|
144
|
+
declare type PropsWithClassName<P = unknown> = P & {
|
145
|
+
className?: string | undefined;
|
146
|
+
};
|
147
|
+
export { PropsWithClassName }
|
148
|
+
export { PropsWithClassName as PropsWithClassName_alias_1 }
|
147
149
|
|
148
150
|
declare const ProseKit: ComponentType<ProseKitProps>;
|
149
151
|
export { ProseKit }
|
@@ -151,33 +153,85 @@ export { ProseKit as ProseKit_alias_1 }
|
|
151
153
|
|
152
154
|
declare interface ProseKitProps {
|
153
155
|
editor: Editor;
|
154
|
-
children?:
|
156
|
+
children?: ReactNode;
|
155
157
|
}
|
156
158
|
export { ProseKitProps }
|
157
159
|
export { ProseKitProps as ProseKitProps_alias_1 }
|
158
160
|
|
159
|
-
declare
|
160
|
-
|
161
|
-
|
161
|
+
export declare class ReactNodeView extends CoreNodeView<ReactNodeViewComponent> implements ReactRenderer<ReactNodeViewProps> {
|
162
|
+
key: string;
|
163
|
+
context: ReactNodeViewProps;
|
164
|
+
updateContext: () => void;
|
165
|
+
render: () => ReactPortal;
|
166
|
+
}
|
167
|
+
|
168
|
+
/**
|
169
|
+
* @public
|
170
|
+
*/
|
171
|
+
declare type ReactNodeViewComponent = ComponentType<ReactNodeViewProps>;
|
172
|
+
export { ReactNodeViewComponent }
|
173
|
+
export { ReactNodeViewComponent as ReactNodeViewComponent_alias_1 }
|
162
174
|
|
163
|
-
|
175
|
+
/**
|
176
|
+
* Options for {@link defineReactNodeView}.
|
177
|
+
*
|
178
|
+
* @public
|
179
|
+
*/
|
180
|
+
declare interface ReactNodeViewOptions extends ReactNodeViewUserOptions {
|
181
|
+
/**
|
182
|
+
* The name of the node.
|
183
|
+
*/
|
164
184
|
name: string;
|
165
|
-
component: React.ComponentType<ReactNodeViewComponentProps>;
|
166
|
-
dom?: string | (() => HTMLElement);
|
167
|
-
contentDOM?: string | (() => HTMLElement);
|
168
185
|
}
|
169
186
|
export { ReactNodeViewOptions }
|
170
187
|
export { ReactNodeViewOptions as ReactNodeViewOptions_alias_1 }
|
171
188
|
|
172
|
-
|
173
|
-
|
174
|
-
|
189
|
+
/**
|
190
|
+
* @public
|
191
|
+
*/
|
192
|
+
declare interface ReactNodeViewProps {
|
193
|
+
contentRef: (node: HTMLElement | null) => void;
|
194
|
+
view: EditorView;
|
195
|
+
getPos: () => number | undefined;
|
196
|
+
setAttrs: (attrs: Attrs) => void;
|
197
|
+
node: Node_2;
|
198
|
+
selected: boolean;
|
199
|
+
decorations: readonly Decoration[];
|
200
|
+
innerDecorations: DecorationSource;
|
201
|
+
}
|
202
|
+
export { ReactNodeViewProps }
|
203
|
+
export { ReactNodeViewProps as ReactNodeViewProps_alias_1 }
|
175
204
|
|
176
|
-
declare
|
177
|
-
|
205
|
+
export declare type ReactNodeViewSpec = CoreNodeViewSpec<ReactNodeViewComponent>;
|
206
|
+
|
207
|
+
export declare type ReactNodeViewUserOptions = CoreNodeViewUserOptions<ReactNodeViewComponent>;
|
208
|
+
|
209
|
+
export declare interface ReactRenderer<Context> {
|
210
|
+
key: string;
|
211
|
+
context: Context;
|
212
|
+
render: () => ReactPortal;
|
213
|
+
updateContext: () => void;
|
214
|
+
}
|
215
|
+
|
216
|
+
export declare interface ReactRendererResult {
|
217
|
+
readonly portals: Record<string, ReactPortal>;
|
218
|
+
readonly renderReactRenderer: (nodeView: ReactRenderer<unknown>, update?: boolean) => void;
|
219
|
+
readonly removeReactRenderer: (nodeView: ReactRenderer<unknown>) => void;
|
178
220
|
}
|
179
|
-
|
180
|
-
export
|
221
|
+
|
222
|
+
export declare const ReactViewsConsumer: FC;
|
223
|
+
|
224
|
+
declare const ReactViewsProvider: FC<{
|
225
|
+
children: ReactNode;
|
226
|
+
}>;
|
227
|
+
export { ReactViewsProvider }
|
228
|
+
export { ReactViewsProvider as ReactViewsProvider_alias_1 }
|
229
|
+
|
230
|
+
declare function useEditor<E extends Extension = any>(options?: {
|
231
|
+
update?: boolean;
|
232
|
+
}): Editor<E>;
|
233
|
+
export { useEditor }
|
234
|
+
export { useEditor as useEditor_alias_1 }
|
181
235
|
|
182
236
|
/**
|
183
237
|
* Add an extension to the editor.
|
@@ -189,10 +243,18 @@ declare function useExtension<T extends Extension = Extension>(extension: T | nu
|
|
189
243
|
export { useExtension }
|
190
244
|
export { useExtension as useExtension_alias_1 }
|
191
245
|
|
192
|
-
declare function useKeymap(keymap: Keymap
|
246
|
+
declare function useKeymap(keymap: Keymap, options?: {
|
247
|
+
priority?: Priority;
|
248
|
+
}): void;
|
193
249
|
export { useKeymap }
|
194
250
|
export { useKeymap as useKeymap_alias_1 }
|
195
251
|
|
196
|
-
export
|
252
|
+
export declare function useNodeViewFactory(): NodeViewFactory;
|
253
|
+
|
254
|
+
export declare function usePriorityExtension<T extends Extension = Extension>(extension: T | null, priority?: Priority | null): void;
|
255
|
+
|
256
|
+
export declare function useReactNodeViewCreator(renderReactRenderer: ReactRendererResult['renderReactRenderer'], removeReactRenderer: ReactRendererResult['removeReactRenderer']): (options: ReactNodeViewUserOptions) => NodeViewConstructor;
|
257
|
+
|
258
|
+
export declare function useReactRenderer(): ReactRendererResult;
|
197
259
|
|
198
260
|
export { }
|
package/dist/prosekit-react.d.ts
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
export {
|
1
|
+
export { ReactNodeViewComponent } from './_tsup-dts-rollup';
|
2
2
|
export { ProseKit } from './_tsup-dts-rollup';
|
3
3
|
export { ProseKitProps } from './_tsup-dts-rollup';
|
4
4
|
export { defineReactNodeView } from './_tsup-dts-rollup';
|
5
|
-
export { ReactNodeViewComponentProps } from './_tsup-dts-rollup';
|
6
5
|
export { ReactNodeViewOptions } from './_tsup-dts-rollup';
|
7
6
|
export { useEditor } from './_tsup-dts-rollup';
|
8
|
-
export { UseEditorOptions } from './_tsup-dts-rollup';
|
9
7
|
export { useExtension } from './_tsup-dts-rollup';
|
10
8
|
export { useKeymap } from './_tsup-dts-rollup';
|
9
|
+
export { PropsWithClassName } from './_tsup-dts-rollup';
|
10
|
+
export { ReactNodeViewProps } from './_tsup-dts-rollup';
|
package/dist/prosekit-react.js
CHANGED
@@ -1,52 +1,28 @@
|
|
1
|
-
// src/index.ts
|
2
|
-
import { useNodeViewContext as useNodeViewContext2 } from "@prosemirror-adapter/react";
|
3
|
-
|
4
1
|
// src/components/prosekit.ts
|
5
2
|
import "@prosekit/core";
|
6
|
-
import {
|
7
|
-
ProsemirrorAdapterProvider,
|
8
|
-
useNodeViewFactory
|
9
|
-
} from "@prosemirror-adapter/react";
|
10
|
-
import { createElement as createElement2, useMemo } from "react";
|
3
|
+
import { createElement as createElement3 } from "react";
|
11
4
|
|
12
5
|
// src/contexts/editor-context.ts
|
13
6
|
import { createContext } from "react";
|
14
7
|
var editorContext = createContext(null);
|
15
8
|
|
9
|
+
// src/views/react-views-consumer.ts
|
10
|
+
import { useMemo } from "react";
|
11
|
+
|
16
12
|
// src/extensions/react-node-view.ts
|
17
|
-
import {
|
18
|
-
import {
|
19
|
-
useNodeViewContext
|
20
|
-
} from "@prosemirror-adapter/react";
|
21
|
-
import { createElement } from "react";
|
13
|
+
import { defineNodeViewFactory } from "@prosekit/core";
|
22
14
|
function defineReactNodeView(options) {
|
23
|
-
|
15
|
+
const { name, ...userOptions } = options;
|
16
|
+
return defineNodeViewFactory({
|
24
17
|
group: "react",
|
25
|
-
name
|
26
|
-
args:
|
18
|
+
name,
|
19
|
+
args: userOptions
|
27
20
|
});
|
28
21
|
}
|
29
|
-
function
|
30
|
-
|
31
|
-
}) {
|
32
|
-
return defineNodeViewEffect({
|
22
|
+
function defineReactNodeViewFactory(nodeViewFactory) {
|
23
|
+
return defineNodeViewFactory({
|
33
24
|
group: "react",
|
34
|
-
factory:
|
35
|
-
const nodeViewOptions = args;
|
36
|
-
return nodeViewFactory({
|
37
|
-
component: function ReactNodeViewWrapper() {
|
38
|
-
const context = useNodeViewContext();
|
39
|
-
return createElement(nodeViewOptions.component, context);
|
40
|
-
},
|
41
|
-
// Optional: add some options
|
42
|
-
as: () => {
|
43
|
-
const dom = document.createElement("div");
|
44
|
-
dom.style.display = "contents";
|
45
|
-
return dom;
|
46
|
-
},
|
47
|
-
contentAs: "code"
|
48
|
-
});
|
49
|
-
}
|
25
|
+
factory: nodeViewFactory
|
50
26
|
});
|
51
27
|
}
|
52
28
|
|
@@ -93,43 +69,226 @@ function useExtension(extension) {
|
|
93
69
|
}, [editor, extension]);
|
94
70
|
}
|
95
71
|
|
72
|
+
// src/views/node-view/node-view-context.ts
|
73
|
+
import { ProseKitError as ProseKitError2 } from "@prosekit/core";
|
74
|
+
import { createContext as createContext2, useContext as useContext2 } from "react";
|
75
|
+
var createNodeViewContext = createContext2(null);
|
76
|
+
function useNodeViewFactory() {
|
77
|
+
const nodeViewFactory = useContext2(createNodeViewContext);
|
78
|
+
if (!nodeViewFactory) {
|
79
|
+
throw new ProseKitError2("Cannot find node view factory context.");
|
80
|
+
}
|
81
|
+
return nodeViewFactory;
|
82
|
+
}
|
83
|
+
|
84
|
+
// src/views/react-views-consumer.ts
|
85
|
+
var ReactViewsConsumer = () => {
|
86
|
+
const nodeViewFactory = useNodeViewFactory();
|
87
|
+
const extension = useMemo(
|
88
|
+
() => defineReactNodeViewFactory(nodeViewFactory),
|
89
|
+
[nodeViewFactory]
|
90
|
+
);
|
91
|
+
useExtension(extension);
|
92
|
+
return null;
|
93
|
+
};
|
94
|
+
|
95
|
+
// src/views/react-views-provider.ts
|
96
|
+
import { createElement as createElement2, useMemo as useMemo2 } from "react";
|
97
|
+
|
98
|
+
// src/views/node-view/use-react-node-view-creator.ts
|
99
|
+
import { useCallback } from "react";
|
100
|
+
|
101
|
+
// src/views/node-view/react-node-view.ts
|
102
|
+
import { _getId } from "@prosekit/core";
|
103
|
+
import { CoreNodeView } from "@prosemirror-adapter/core";
|
104
|
+
import { createElement } from "react";
|
105
|
+
import { createPortal } from "react-dom";
|
106
|
+
var ReactNodeView = class extends CoreNodeView {
|
107
|
+
constructor() {
|
108
|
+
super(...arguments);
|
109
|
+
this.key = _getId();
|
110
|
+
this.context = {
|
111
|
+
contentRef: (element) => {
|
112
|
+
if (element && this.contentDOM && element.firstChild !== this.contentDOM)
|
113
|
+
element.appendChild(this.contentDOM);
|
114
|
+
},
|
115
|
+
view: this.view,
|
116
|
+
getPos: this.getPos,
|
117
|
+
setAttrs: this.setAttrs,
|
118
|
+
node: this.node,
|
119
|
+
selected: this.selected,
|
120
|
+
decorations: this.decorations,
|
121
|
+
innerDecorations: this.innerDecorations
|
122
|
+
};
|
123
|
+
this.updateContext = () => {
|
124
|
+
Object.assign(this.context, {
|
125
|
+
node: this.node,
|
126
|
+
selected: this.selected,
|
127
|
+
decorations: this.decorations,
|
128
|
+
innerDecorations: this.innerDecorations
|
129
|
+
});
|
130
|
+
};
|
131
|
+
this.render = () => {
|
132
|
+
const UserComponent = this.component;
|
133
|
+
return createPortal(
|
134
|
+
createElement(UserComponent, this.context),
|
135
|
+
this.dom,
|
136
|
+
this.key
|
137
|
+
);
|
138
|
+
};
|
139
|
+
}
|
140
|
+
};
|
141
|
+
|
142
|
+
// src/views/node-view/use-react-node-view-creator.ts
|
143
|
+
function useReactNodeViewCreator(renderReactRenderer, removeReactRenderer) {
|
144
|
+
const createReactNodeView = useCallback(
|
145
|
+
(options) => (node, view, getPos, decorations, innerDecorations) => {
|
146
|
+
const nodeView = new ReactNodeView({
|
147
|
+
node,
|
148
|
+
view,
|
149
|
+
getPos,
|
150
|
+
decorations,
|
151
|
+
innerDecorations,
|
152
|
+
options: {
|
153
|
+
...options,
|
154
|
+
onUpdate() {
|
155
|
+
var _a;
|
156
|
+
(_a = options.onUpdate) == null ? void 0 : _a.call(options);
|
157
|
+
renderReactRenderer(nodeView);
|
158
|
+
},
|
159
|
+
selectNode() {
|
160
|
+
var _a;
|
161
|
+
(_a = options.selectNode) == null ? void 0 : _a.call(options);
|
162
|
+
renderReactRenderer(nodeView);
|
163
|
+
},
|
164
|
+
deselectNode() {
|
165
|
+
var _a;
|
166
|
+
(_a = options.deselectNode) == null ? void 0 : _a.call(options);
|
167
|
+
renderReactRenderer(nodeView);
|
168
|
+
},
|
169
|
+
destroy() {
|
170
|
+
var _a;
|
171
|
+
(_a = options.destroy) == null ? void 0 : _a.call(options);
|
172
|
+
removeReactRenderer(nodeView);
|
173
|
+
}
|
174
|
+
}
|
175
|
+
});
|
176
|
+
renderReactRenderer(nodeView, false);
|
177
|
+
return nodeView;
|
178
|
+
},
|
179
|
+
[removeReactRenderer, renderReactRenderer]
|
180
|
+
);
|
181
|
+
return createReactNodeView;
|
182
|
+
}
|
183
|
+
|
184
|
+
// src/views/react-renderer.ts
|
185
|
+
import { useCallback as useCallback2, useEffect as useEffect3, useRef, useState } from "react";
|
186
|
+
import { flushSync } from "react-dom";
|
187
|
+
function useReactRenderer() {
|
188
|
+
const [portals, setPortals] = useState({});
|
189
|
+
const mountedRef = useRef(false);
|
190
|
+
useEffect3(() => {
|
191
|
+
requestAnimationFrame(() => {
|
192
|
+
mountedRef.current = true;
|
193
|
+
});
|
194
|
+
return () => {
|
195
|
+
mountedRef.current = false;
|
196
|
+
};
|
197
|
+
}, []);
|
198
|
+
const maybeFlushSync = useCallback2((fn) => {
|
199
|
+
if (mountedRef.current)
|
200
|
+
flushSync(fn);
|
201
|
+
else
|
202
|
+
fn();
|
203
|
+
}, []);
|
204
|
+
const renderReactRenderer = useCallback2(
|
205
|
+
(nodeView, update = true) => {
|
206
|
+
maybeFlushSync(() => {
|
207
|
+
if (update)
|
208
|
+
nodeView.updateContext();
|
209
|
+
setPortals((prev) => ({
|
210
|
+
...prev,
|
211
|
+
[nodeView.key]: nodeView.render()
|
212
|
+
}));
|
213
|
+
});
|
214
|
+
},
|
215
|
+
[maybeFlushSync]
|
216
|
+
);
|
217
|
+
const removeReactRenderer = useCallback2(
|
218
|
+
(nodeView) => {
|
219
|
+
maybeFlushSync(() => {
|
220
|
+
setPortals((prev) => {
|
221
|
+
const next = { ...prev };
|
222
|
+
delete next[nodeView.key];
|
223
|
+
return next;
|
224
|
+
});
|
225
|
+
});
|
226
|
+
},
|
227
|
+
[maybeFlushSync]
|
228
|
+
);
|
229
|
+
return {
|
230
|
+
portals,
|
231
|
+
renderReactRenderer,
|
232
|
+
removeReactRenderer
|
233
|
+
};
|
234
|
+
}
|
235
|
+
|
236
|
+
// src/views/react-views-provider.ts
|
237
|
+
var ReactViewsProvider = ({
|
238
|
+
children
|
239
|
+
}) => {
|
240
|
+
const { renderReactRenderer, removeReactRenderer, portals } = useReactRenderer();
|
241
|
+
const createReactNodeView = useReactNodeViewCreator(
|
242
|
+
renderReactRenderer,
|
243
|
+
removeReactRenderer
|
244
|
+
);
|
245
|
+
const memoizedPortals = useMemo2(() => Object.values(portals), [portals]);
|
246
|
+
return createElement2(
|
247
|
+
createNodeViewContext.Provider,
|
248
|
+
{ value: createReactNodeView },
|
249
|
+
children,
|
250
|
+
memoizedPortals
|
251
|
+
);
|
252
|
+
};
|
253
|
+
|
96
254
|
// src/components/prosekit.ts
|
97
255
|
var ProseKit = (props) => {
|
98
256
|
const { editor, children } = props;
|
99
|
-
return
|
100
|
-
|
257
|
+
return createElement3(
|
258
|
+
ReactViewsProvider,
|
101
259
|
null,
|
102
|
-
|
103
|
-
|
260
|
+
createElement3(
|
261
|
+
editorContext.Provider,
|
104
262
|
{ value: { editor } },
|
105
|
-
|
106
|
-
|
263
|
+
createElement3(ReactViewsConsumer),
|
264
|
+
children
|
107
265
|
)
|
108
266
|
);
|
109
267
|
};
|
110
|
-
function RendererRegister() {
|
111
|
-
const nodeViewFactory = useNodeViewFactory();
|
112
|
-
const extension = useMemo(
|
113
|
-
() => defineReactNodeViewRenderer({ nodeViewFactory }),
|
114
|
-
[nodeViewFactory]
|
115
|
-
);
|
116
|
-
useExtension(extension);
|
117
|
-
return null;
|
118
|
-
}
|
119
|
-
var EditorContextProvider = editorContext.Provider;
|
120
268
|
|
121
269
|
// src/hooks/use-keymap.ts
|
122
270
|
import { defineKeymap } from "@prosekit/core";
|
123
|
-
import { useMemo as
|
124
|
-
|
125
|
-
|
126
|
-
|
271
|
+
import { useMemo as useMemo4 } from "react";
|
272
|
+
|
273
|
+
// src/hooks/use-priority-extension.ts
|
274
|
+
import { withPriority } from "@prosekit/core";
|
275
|
+
import { useMemo as useMemo3 } from "react";
|
276
|
+
function usePriorityExtension(extension, priority) {
|
277
|
+
const extensionWithPriority = useMemo3(() => {
|
278
|
+
return extension && priority ? withPriority(extension, priority) : extension;
|
279
|
+
}, [extension, priority]);
|
280
|
+
return useExtension(extensionWithPriority);
|
281
|
+
}
|
282
|
+
|
283
|
+
// src/hooks/use-keymap.ts
|
284
|
+
function useKeymap(keymap, options) {
|
285
|
+
const extension = useMemo4(() => defineKeymap(keymap), [keymap]);
|
286
|
+
usePriorityExtension(extension, options == null ? void 0 : options.priority);
|
127
287
|
}
|
128
288
|
export {
|
129
289
|
ProseKit,
|
130
290
|
defineReactNodeView,
|
131
291
|
useEditor,
|
132
292
|
useExtension,
|
133
|
-
useKeymap
|
134
|
-
useNodeViewContext2 as useNodeViewContext
|
293
|
+
useKeymap
|
135
294
|
};
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@prosekit/react",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.1.
|
4
|
+
"version": "0.1.3",
|
5
5
|
"private": false,
|
6
6
|
"author": {
|
7
7
|
"name": "ocavue",
|
@@ -88,7 +88,8 @@
|
|
88
88
|
"@lit/react": "^1.0.2",
|
89
89
|
"@prosekit/core": "^0.1.0",
|
90
90
|
"@prosekit/lit": "^0.1.0",
|
91
|
-
"@
|
91
|
+
"@prosekit/pm": "^0.1.0",
|
92
|
+
"@prosemirror-adapter/core": "^0.2.6"
|
92
93
|
},
|
93
94
|
"peerDependencies": {
|
94
95
|
"react": ">= 17.0.0",
|
@@ -104,13 +105,13 @@
|
|
104
105
|
},
|
105
106
|
"devDependencies": {
|
106
107
|
"@prosekit/dev": "*",
|
107
|
-
"@types/react": "^18.2.
|
108
|
+
"@types/react": "^18.2.45",
|
108
109
|
"@types/react-dom": "^18.2.17",
|
109
110
|
"react": "^18.2.0",
|
110
111
|
"react-dom": "^18.2.0",
|
111
112
|
"tsup": "^8.0.1",
|
112
|
-
"typescript": "^5.3.
|
113
|
-
"vitest": "^0.
|
113
|
+
"typescript": "^5.3.3",
|
114
|
+
"vitest": "^1.0.4"
|
114
115
|
},
|
115
116
|
"scripts": {
|
116
117
|
"build:tsup": "tsup",
|
package/src/index.ts
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
export {
|
1
|
+
export type { ReactNodeViewComponent } from './views/node-view/react-node-view-options'
|
2
2
|
export { ProseKit, type ProseKitProps } from './components/prosekit'
|
3
3
|
export {
|
4
4
|
defineReactNodeView,
|
5
|
-
type ReactNodeViewComponentProps,
|
6
5
|
type ReactNodeViewOptions,
|
7
6
|
} from './extensions/react-node-view'
|
8
|
-
export { useEditor
|
7
|
+
export { useEditor } from './hooks/use-editor'
|
9
8
|
export { useExtension } from './hooks/use-extension'
|
10
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'
|