@prosekit/preact 0.5.1 → 0.6.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/dist/prosekit-preact-autocomplete.d.ts +1 -1
- package/dist/prosekit-preact.d.ts +85 -1
- package/dist/prosekit-preact.d.ts.map +1 -1
- package/dist/prosekit-preact.js +136 -23
- package/dist/prosekit-preact.js.map +1 -1
- package/package.json +7 -5
- package/src/components/prosekit.ts +14 -1
- package/src/extensions/preact-mark-view.ts +93 -0
- package/src/extensions/preact-node-view.ts +93 -0
- package/src/hooks/use-editor-derived-value.ts +81 -0
- package/src/index.ts +16 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CreateProps } from "./create-props-EGV61dJR.js";
|
|
2
|
-
import { AutocompleteEmptyElement, AutocompleteEmptyEvents, AutocompleteEmptyProps as AutocompleteEmptyProps$1, AutocompleteItemElement, AutocompleteItemEvents, AutocompleteItemProps as AutocompleteItemProps$1, AutocompleteListElement, AutocompleteListEvents, AutocompleteListProps as AutocompleteListProps$1, AutocompletePopoverElement, AutocompletePopoverEvents, AutocompletePopoverProps as AutocompletePopoverProps$1 } from "@prosekit/web/autocomplete";
|
|
3
2
|
import { ForwardRefExoticComponent, HTMLAttributes, RefAttributes } from "preact/compat";
|
|
3
|
+
import { AutocompleteEmptyElement, AutocompleteEmptyEvents, AutocompleteEmptyProps as AutocompleteEmptyProps$1, AutocompleteItemElement, AutocompleteItemEvents, AutocompleteItemProps as AutocompleteItemProps$1, AutocompleteListElement, AutocompleteListEvents, AutocompleteListProps as AutocompleteListProps$1, AutocompletePopoverElement, AutocompletePopoverEvents, AutocompletePopoverProps as AutocompletePopoverProps$1 } from "@prosekit/web/autocomplete";
|
|
4
4
|
|
|
5
5
|
//#region src/components/autocomplete/autocomplete-empty.gen.d.ts
|
|
6
6
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { MarkViewContext, NodeViewContext } from "@prosemirror-adapter/preact";
|
|
1
2
|
import { ComponentChildren, ComponentType } from "preact";
|
|
2
3
|
import { Editor, Extension, Keymap, Priority } from "@prosekit/core";
|
|
4
|
+
import { CoreMarkViewUserOptions, CoreNodeViewUserOptions } from "@prosemirror-adapter/core";
|
|
3
5
|
import { ProseMirrorNode } from "@prosekit/pm/model";
|
|
4
6
|
import { EditorState } from "@prosekit/pm/state";
|
|
5
7
|
|
|
@@ -15,6 +17,60 @@ interface ProseKitProps {
|
|
|
15
17
|
*/
|
|
16
18
|
declare const ProseKit: ComponentType<ProseKitProps>;
|
|
17
19
|
//#endregion
|
|
20
|
+
//#region src/extensions/preact-mark-view.d.ts
|
|
21
|
+
/**
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
interface PreactMarkViewProps extends MarkViewContext {}
|
|
25
|
+
/**
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
type PreactMarkViewComponent = ComponentType<PreactMarkViewProps>;
|
|
29
|
+
/**
|
|
30
|
+
* Options for {@link definePreactMarkView}.
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
interface PreactMarkViewOptions extends CoreMarkViewUserOptions<PreactMarkViewComponent> {
|
|
35
|
+
/**
|
|
36
|
+
* The name of the mark type.
|
|
37
|
+
*/
|
|
38
|
+
name: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Defines a mark view using a Preact component.
|
|
42
|
+
*
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
declare function definePreactMarkView(options: PreactMarkViewOptions): Extension;
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/extensions/preact-node-view.d.ts
|
|
48
|
+
/**
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
interface PreactNodeViewProps extends NodeViewContext {}
|
|
52
|
+
/**
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
type PreactNodeViewComponent = ComponentType<PreactNodeViewProps>;
|
|
56
|
+
/**
|
|
57
|
+
* Options for {@link definePreactNodeView}.
|
|
58
|
+
*
|
|
59
|
+
* @public
|
|
60
|
+
*/
|
|
61
|
+
interface PreactNodeViewOptions extends CoreNodeViewUserOptions<PreactNodeViewComponent> {
|
|
62
|
+
/**
|
|
63
|
+
* The name of the node type.
|
|
64
|
+
*/
|
|
65
|
+
name: string;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Defines a node view using a Preact component.
|
|
69
|
+
*
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
declare function definePreactNodeView(options: PreactNodeViewOptions): Extension;
|
|
73
|
+
//#endregion
|
|
18
74
|
//#region src/hooks/use-extension.d.ts
|
|
19
75
|
interface UseExtensionOptions {
|
|
20
76
|
/**
|
|
@@ -61,6 +117,34 @@ declare function useEditor<E extends Extension = any>(options?: {
|
|
|
61
117
|
update?: boolean;
|
|
62
118
|
}): Editor<E>;
|
|
63
119
|
//#endregion
|
|
120
|
+
//#region src/hooks/use-editor-derived-value.d.ts
|
|
121
|
+
interface UseEditorDerivedOptions<E extends Extension = any> {
|
|
122
|
+
/**
|
|
123
|
+
* The editor to add the extension to. If not provided, it will use the
|
|
124
|
+
* editor from the nearest `ProseKit` component.
|
|
125
|
+
*/
|
|
126
|
+
editor?: Editor<E>;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* A hook that runs a function to derive a value from the editor instance after
|
|
130
|
+
* editor state changes.
|
|
131
|
+
*
|
|
132
|
+
* This is useful when you need to render something based on the editor state,
|
|
133
|
+
* for example, whether the selected text is wrapped in an italic mark.
|
|
134
|
+
*
|
|
135
|
+
* @public
|
|
136
|
+
*/
|
|
137
|
+
declare function useEditorDerivedValue<E extends Extension, Derived>(
|
|
138
|
+
/**
|
|
139
|
+
* A function that receives the editor instance and returns a derived value.
|
|
140
|
+
*
|
|
141
|
+
* It will be called whenever the editor's document state changes, or when it
|
|
142
|
+
* mounts.
|
|
143
|
+
*
|
|
144
|
+
* This function should be memoized.
|
|
145
|
+
*/
|
|
146
|
+
derive: (editor: Editor<E>) => Derived, options?: UseEditorDerivedOptions<E>): Derived;
|
|
147
|
+
//#endregion
|
|
64
148
|
//#region src/hooks/use-keymap.d.ts
|
|
65
149
|
declare function useKeymap(keymap: Keymap, options?: UseExtensionOptions): void;
|
|
66
150
|
//#endregion
|
|
@@ -87,5 +171,5 @@ type PropsWithChildren<P = unknown> = P & {
|
|
|
87
171
|
children?: ComponentChildren | undefined;
|
|
88
172
|
};
|
|
89
173
|
//#endregion
|
|
90
|
-
export { type PropsWithChildren, type PropsWithClass, ProseKit, type ProseKitProps, type UseExtensionOptions, useDocChange, useEditor, useExtension, useKeymap, useStateUpdate };
|
|
174
|
+
export { type PreactMarkViewComponent, type PreactMarkViewOptions, type PreactMarkViewProps, type PreactNodeViewComponent, type PreactNodeViewOptions, type PreactNodeViewProps, type PropsWithChildren, type PropsWithClass, ProseKit, type ProseKitProps, type UseEditorDerivedOptions, type UseExtensionOptions, definePreactMarkView, definePreactNodeView, useDocChange, useEditor, useEditorDerivedValue, useExtension, useKeymap, useStateUpdate };
|
|
91
175
|
//# sourceMappingURL=prosekit-preact.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prosekit-preact.d.ts","names":[],"sources":["../src/components/prosekit.ts","../src/hooks/use-extension.ts","../src/hooks/use-doc-change.ts","../src/hooks/use-editor.ts","../src/hooks/use-keymap.ts","../src/hooks/use-state-update.ts","../src/types.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"prosekit-preact.d.ts","names":[],"sources":["../src/components/prosekit.ts","../src/extensions/preact-mark-view.ts","../src/extensions/preact-node-view.ts","../src/hooks/use-extension.ts","../src/hooks/use-doc-change.ts","../src/hooks/use-editor.ts","../src/hooks/use-editor-derived-value.ts","../src/hooks/use-keymap.ts","../src/hooks/use-state-update.ts","../src/types.ts"],"sourcesContent":[],"mappings":";;;;;;;;UAYiB,aAAA;UACP;aACG;;;;AAFb;;;AAEa,cAQA,QARA,EAQU,aARV,CAQwB,aARxB,CAAA;;;;;;UCWI,mBAAA,SAA4B,iBDb7C;;;;AAE8B,KCgBlB,uBAAA,GAA0B,aDhBR,CCgBsB,mBDhBtB,CAAA;AAQ9B;;;;;UCeiB,qBAAA,SAA8B,wBAAwB;;;AAZvE;EAKY,IAAA,EAAA,MAAA;;;;;;AAwCZ;AAAoC,iBAApB,oBAAA,CAAoB,OAAA,EAAU,qBAAV,CAAA,EAAkC,SAAlC;;;;;;UC7CnB,mBAAA,SAA4B,iBFb7C;;;;AAE8B,KEgBlB,uBAAA,GAA0B,aFhBR,CEgBsB,mBFhBtB,CAAA;AAQ9B;;;;;UEeiB,qBAAA,SAA8B,wBAAwB;;;ADZvE;EAKY,IAAA,EAAA,MAAA;;;;;;AAwCZ;AAAoC,iBCApB,oBAAA,CDAoB,OAAA,ECAU,qBDAV,CAAA,ECAkC,SDAlC;;;UE3DnB,mBAAA;;;;;WAKN;;AHJX;;UACU,CAAA,EGQG,QHRH;;;AASV;;AAAqC,iBGKrB,YAAA;;;;;WAKH,4BACD;;;;;;;;iBCnBI,YAAA,gBACC,oCACL;;;;;;;;iBCII,oBAAoB,wBLgBnC;;AAxBD;;;;;EAUa,MAAA,CAAA,EAAA,OAcZ;CAAA,CAAA,EKRG,MLQH,CKRU,CLQV,CAAA;;;UMvBgB,kCAAkC;;;;;WAKxC,OAAO;;ANNlB;;;;;AAUA;;;;AAAoC,iBMQpB,qBNRoB,CAAA,UMQY,SNRZ,EAAA,OAAA,CAAA;;;;ACGpC;AAKA;;;;MAAmD,EAAA,CAAA,MAAA,EKShC,MLTgC,CKSzB,CLTyB,CAAA,EAAA,GKSlB,OLTkB,EAAA,OAAA,CAAA,EKUvC,uBLVuC,CKUf,CLVe,CAAA,CAAA,EKWhD,OLXgD;;;iBMnBnC,SAAA,SAAkB,kBAAkB;;;;;;;;iBCGpC,cAAA,kBACG,gCACP;;;;;;KCXA,8BAA8B;;;;ATO1C;;;AAEa,KSDD,iBTCC,CAAA,IAAA,OAAA,CAAA,GSDgC,CTChC,GAAA;EAAiB,QAAA,CAAA,ESAjB,iBTAiB,GAAA,SAAA;AAQ9B,CAAA"}
|
package/dist/prosekit-preact.js
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
1
1
|
import { EditorContextProvider, useEditorContext } from "./editor-context-imq7MdJr.js";
|
|
2
|
+
import { ProsemirrorAdapterProvider, useMarkViewContext, useMarkViewFactory, useNodeViewContext, useNodeViewFactory } from "@prosemirror-adapter/preact";
|
|
2
3
|
import { h } from "preact";
|
|
3
4
|
import { useEffect, useMemo, useReducer } from "preact/hooks";
|
|
4
|
-
import { EditorNotFoundError, ProseKitError, defineDocChangeHandler, defineKeymap, defineMountHandler, defineUpdateHandler, union, withPriority } from "@prosekit/core";
|
|
5
|
+
import { EditorNotFoundError, ProseKitError, defineDocChangeHandler, defineKeymap, defineMarkViewComponent, defineMarkViewFactory, defineMountHandler, defineNodeViewComponent, defineNodeViewFactory, defineUpdateHandler, union, withPriority } from "@prosekit/core";
|
|
6
|
+
import { useSyncExternalStore } from "preact/compat";
|
|
5
7
|
|
|
6
|
-
//#region src/components/prosekit.ts
|
|
7
|
-
/**
|
|
8
|
-
* The root component for a ProseKit editor.
|
|
9
|
-
*
|
|
10
|
-
* @public
|
|
11
|
-
*/
|
|
12
|
-
const ProseKit = (props) => {
|
|
13
|
-
const { editor, children } = props;
|
|
14
|
-
return h(EditorContextProvider, {
|
|
15
|
-
value: editor,
|
|
16
|
-
children
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
//#endregion
|
|
21
8
|
//#region src/hooks/use-editor-extension.ts
|
|
22
9
|
/**
|
|
23
10
|
* @internal
|
|
@@ -50,6 +37,94 @@ function useExtension(extension, options) {
|
|
|
50
37
|
useEditorExtension(options?.editor || editorContext, usePriorityExtension(extension, options?.priority));
|
|
51
38
|
}
|
|
52
39
|
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/extensions/preact-mark-view.ts
|
|
42
|
+
function withMarkViewProps(component) {
|
|
43
|
+
return function MarkViewPropsWrapper() {
|
|
44
|
+
return h(component, useMarkViewContext());
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
const PreactMarkViewConsumer = () => {
|
|
51
|
+
const markViewFactory = useMarkViewFactory();
|
|
52
|
+
useExtension(useMemo(() => definePreactMarkViewFactory(markViewFactory), [markViewFactory]));
|
|
53
|
+
return null;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Defines a mark view using a Preact component.
|
|
57
|
+
*
|
|
58
|
+
* @public
|
|
59
|
+
*/
|
|
60
|
+
function definePreactMarkView(options) {
|
|
61
|
+
const { name, component,...userOptions } = options;
|
|
62
|
+
return defineMarkViewComponent({
|
|
63
|
+
group: "preact",
|
|
64
|
+
name,
|
|
65
|
+
args: {
|
|
66
|
+
...userOptions,
|
|
67
|
+
component: withMarkViewProps(component)
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function definePreactMarkViewFactory(factory) {
|
|
72
|
+
return defineMarkViewFactory({
|
|
73
|
+
group: "preact",
|
|
74
|
+
factory
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/extensions/preact-node-view.ts
|
|
80
|
+
function withNodeViewProps(component) {
|
|
81
|
+
return function NodeViewPropsWrapper() {
|
|
82
|
+
return h(component, useNodeViewContext());
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @internal
|
|
87
|
+
*/
|
|
88
|
+
const PreactNodeViewConsumer = () => {
|
|
89
|
+
const nodeViewFactory = useNodeViewFactory();
|
|
90
|
+
useExtension(useMemo(() => definePreactNodeViewFactory(nodeViewFactory), [nodeViewFactory]));
|
|
91
|
+
return null;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Defines a node view using a Preact component.
|
|
95
|
+
*
|
|
96
|
+
* @public
|
|
97
|
+
*/
|
|
98
|
+
function definePreactNodeView(options) {
|
|
99
|
+
const { name, component,...userOptions } = options;
|
|
100
|
+
return defineNodeViewComponent({
|
|
101
|
+
group: "preact",
|
|
102
|
+
name,
|
|
103
|
+
args: {
|
|
104
|
+
...userOptions,
|
|
105
|
+
component: withNodeViewProps(component)
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function definePreactNodeViewFactory(factory) {
|
|
110
|
+
return defineNodeViewFactory({
|
|
111
|
+
group: "preact",
|
|
112
|
+
factory
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/components/prosekit.ts
|
|
118
|
+
/**
|
|
119
|
+
* The root component for a ProseKit editor.
|
|
120
|
+
*
|
|
121
|
+
* @public
|
|
122
|
+
*/
|
|
123
|
+
const ProseKit = (props) => {
|
|
124
|
+
const { editor, children } = props;
|
|
125
|
+
return h(ProsemirrorAdapterProvider, null, h(EditorContextProvider, { value: editor }, h(PreactNodeViewConsumer, null), h(PreactMarkViewConsumer, null), children));
|
|
126
|
+
};
|
|
127
|
+
|
|
53
128
|
//#endregion
|
|
54
129
|
//#region src/hooks/use-doc-change.ts
|
|
55
130
|
/**
|
|
@@ -58,8 +133,7 @@ function useExtension(extension, options) {
|
|
|
58
133
|
* @public
|
|
59
134
|
*/
|
|
60
135
|
function useDocChange(handler, options) {
|
|
61
|
-
|
|
62
|
-
useExtension(extension, options);
|
|
136
|
+
useExtension(useMemo(() => defineDocChangeHandler((view) => handler(view.state.doc)), [handler]), options);
|
|
63
137
|
}
|
|
64
138
|
|
|
65
139
|
//#endregion
|
|
@@ -91,11 +165,51 @@ function useForceUpdate() {
|
|
|
91
165
|
return dispatch;
|
|
92
166
|
}
|
|
93
167
|
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/hooks/use-editor-derived-value.ts
|
|
170
|
+
/**
|
|
171
|
+
* A hook that runs a function to derive a value from the editor instance after
|
|
172
|
+
* editor state changes.
|
|
173
|
+
*
|
|
174
|
+
* This is useful when you need to render something based on the editor state,
|
|
175
|
+
* for example, whether the selected text is wrapped in an italic mark.
|
|
176
|
+
*
|
|
177
|
+
* @public
|
|
178
|
+
*/
|
|
179
|
+
function useEditorDerivedValue(derive, options) {
|
|
180
|
+
const editorContext = useEditorContext();
|
|
181
|
+
const editor = options?.editor ?? editorContext;
|
|
182
|
+
if (!editor) throw new EditorNotFoundError();
|
|
183
|
+
const [subscribe, getSnapshot] = useMemo(() => {
|
|
184
|
+
return createEditorStore(editor, derive);
|
|
185
|
+
}, [editor, derive]);
|
|
186
|
+
return useSyncExternalStore(subscribe, getSnapshot);
|
|
187
|
+
}
|
|
188
|
+
function createEditorStore(editor, derive) {
|
|
189
|
+
let dirty = true;
|
|
190
|
+
let derived;
|
|
191
|
+
const subscribe = (onChange) => {
|
|
192
|
+
const handleChange = () => {
|
|
193
|
+
dirty = true;
|
|
194
|
+
onChange();
|
|
195
|
+
};
|
|
196
|
+
const extension = union(defineUpdateHandler(handleChange), defineMountHandler(handleChange));
|
|
197
|
+
return editor.use(extension);
|
|
198
|
+
};
|
|
199
|
+
const getSnapshot = () => {
|
|
200
|
+
if (dirty) {
|
|
201
|
+
dirty = false;
|
|
202
|
+
derived = derive(editor);
|
|
203
|
+
}
|
|
204
|
+
return derived;
|
|
205
|
+
};
|
|
206
|
+
return [subscribe, getSnapshot];
|
|
207
|
+
}
|
|
208
|
+
|
|
94
209
|
//#endregion
|
|
95
210
|
//#region src/hooks/use-keymap.ts
|
|
96
211
|
function useKeymap(keymap, options) {
|
|
97
|
-
|
|
98
|
-
useExtension(extension, options);
|
|
212
|
+
useExtension(useMemo(() => defineKeymap(keymap), [keymap]), options);
|
|
99
213
|
}
|
|
100
214
|
|
|
101
215
|
//#endregion
|
|
@@ -106,10 +220,9 @@ function useKeymap(keymap, options) {
|
|
|
106
220
|
* @public
|
|
107
221
|
*/
|
|
108
222
|
function useStateUpdate(handler, options) {
|
|
109
|
-
|
|
110
|
-
useExtension(extension, options);
|
|
223
|
+
useExtension(useMemo(() => defineUpdateHandler((view) => handler(view.state)), [handler]), options);
|
|
111
224
|
}
|
|
112
225
|
|
|
113
226
|
//#endregion
|
|
114
|
-
export { ProseKit, useDocChange, useEditor, useExtension, useKeymap, useStateUpdate };
|
|
227
|
+
export { ProseKit, definePreactMarkView, definePreactNodeView, useDocChange, useEditor, useEditorDerivedValue, useExtension, useKeymap, useStateUpdate };
|
|
115
228
|
//# sourceMappingURL=prosekit-preact.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prosekit-preact.js","names":["ProseKit: ComponentType<ProseKitProps>"],"sources":["../src/components/prosekit.ts","../src/hooks/use-editor-extension.ts","../src/hooks/use-priority-extension.ts","../src/hooks/use-extension.ts","../src/hooks/use-doc-change.ts","../src/hooks/use-editor.ts","../src/hooks/use-keymap.ts","../src/hooks/use-state-update.ts"],"sourcesContent":["import type { Editor } from '@prosekit/core'\nimport {\n h,\n type ComponentChildren,\n type ComponentType,\n} from 'preact'\n\nimport { EditorContextProvider } from '../contexts/editor-context'\n\nexport interface ProseKitProps {\n editor: Editor\n children?: ComponentChildren\n}\n\n/**\n * The root component for a ProseKit editor.\n *\n * @public\n */\nexport const ProseKit: ComponentType<ProseKitProps> = (props) => {\n const { editor, children } = props\n\n return h(EditorContextProvider, { value: editor, children })\n}\n","import {\n EditorNotFoundError,\n type Editor,\n type Extension,\n} from '@prosekit/core'\nimport { useEffect } from 'preact/hooks'\n\n/**\n * @internal\n */\nexport function useEditorExtension(\n editor: Editor | null | undefined,\n extension: Extension | null,\n): void {\n if (!editor) {\n throw new EditorNotFoundError()\n }\n\n useEffect(() => {\n if (extension) {\n return editor.use(extension)\n }\n }, [editor, extension])\n}\n","import {\n withPriority,\n type Extension,\n type Priority,\n} from '@prosekit/core'\nimport { useMemo } from 'preact/hooks'\n\n/**\n * @internal\n */\nexport function usePriorityExtension<T extends Extension = Extension>(\n extension: T | null,\n priority?: Priority | null,\n): T | null {\n return useMemo(() => {\n return extension && priority ? withPriority(extension, priority) : extension\n }, [extension, priority])\n}\n","import type {\n Editor,\n Extension,\n Priority,\n} from '@prosekit/core'\n\nimport { useEditorContext } from '../contexts/editor-context'\n\nimport { useEditorExtension } from './use-editor-extension'\nimport { usePriorityExtension } from './use-priority-extension'\n\nexport interface UseExtensionOptions {\n /**\n * The editor to add the extension to. If not provided, it will use the\n * editor from the nearest `ProseKit` component.\n */\n editor?: Editor\n\n /**\n * Optional priority to add the extension with.\n */\n priority?: Priority\n}\n\n/**\n * Add an extension to the editor.\n */\nexport function useExtension(\n /**\n * The extension to add to the editor. If it changes, the previous\n * extension will be removed and the new one (if not null) will be added.\n */\n extension: Extension | null,\n options?: UseExtensionOptions,\n): void {\n const editorContext = useEditorContext()\n useEditorExtension(\n options?.editor || editorContext,\n usePriorityExtension(extension, options?.priority),\n )\n}\n","import { defineDocChangeHandler } from '@prosekit/core'\nimport type { ProseMirrorNode } from '@prosekit/pm/model'\nimport { useMemo } from 'preact/hooks'\n\nimport {\n useExtension,\n type UseExtensionOptions,\n} from './use-extension'\n\n/**\n * Calls the given handler whenever the editor document changes.\n *\n * @public\n */\nexport function useDocChange(\n handler: (doc: ProseMirrorNode) => void,\n options?: UseExtensionOptions,\n): void {\n const extension = useMemo(\n () => defineDocChangeHandler((view) => handler(view.state.doc)),\n [handler],\n )\n useExtension(extension, options)\n}\n","import {\n defineMountHandler,\n defineUpdateHandler,\n ProseKitError,\n union,\n type Editor,\n type Extension,\n} from '@prosekit/core'\nimport {\n useEffect,\n useReducer,\n} from 'preact/hooks'\n\nimport { useEditorContext } from '../contexts/editor-context'\n\n/**\n * Retrieves the editor instance from the nearest ProseKit component.\n *\n * @public\n */\nexport function useEditor<E extends Extension = any>(options?: {\n /**\n * Whether to update the component when the editor is mounted or editor state\n * is updated.\n *\n * @default false\n */\n update?: boolean\n}): Editor<E> {\n const update = options?.update ?? false\n\n const editor = useEditorContext<E>()\n if (!editor) {\n throw new ProseKitError(\n 'useEditor must be used within the ProseKit component',\n )\n }\n\n const forceUpdate = useForceUpdate()\n\n useEffect(() => {\n if (update) {\n const extension = union(\n defineMountHandler(forceUpdate),\n defineUpdateHandler(forceUpdate),\n )\n return editor.use(extension)\n }\n }, [editor, update, forceUpdate])\n\n return editor\n}\n\nfunction useForceUpdate() {\n const [, dispatch] = useReducer((x: number) => x + 1, 0)\n return dispatch\n}\n","import {\n defineKeymap,\n type Keymap,\n} from '@prosekit/core'\nimport { useMemo } from 'preact/hooks'\n\nimport {\n useExtension,\n type UseExtensionOptions,\n} from './use-extension'\n\nexport function useKeymap(keymap: Keymap, options?: UseExtensionOptions): void {\n const extension = useMemo(() => defineKeymap(keymap), [keymap])\n useExtension(extension, options)\n}\n","import { defineUpdateHandler } from '@prosekit/core'\nimport type { EditorState } from '@prosekit/pm/state'\nimport { useMemo } from 'preact/hooks'\n\nimport {\n useExtension,\n type UseExtensionOptions,\n} from './use-extension'\n\n/**\n * Calls the given handler whenever the editor state changes.\n *\n * @public\n */\nexport function useStateUpdate(\n handler: (state: EditorState) => void,\n options?: UseExtensionOptions,\n): void {\n const extension = useMemo(\n () => defineUpdateHandler((view) => handler(view.state)),\n [handler],\n )\n useExtension(extension, options)\n}\n"],"mappings":";;;;;;;;;;;AAmBA,MAAaA,YAA0C,UAAU;CAC/D,MAAM,EAAE,QAAQ,aAAa;AAE7B,QAAO,EAAE,uBAAuB;EAAE,OAAO;EAAQ;EAAU,CAAC;;;;;;;;ACZ9D,SAAgB,mBACd,QACA,WACM;AACN,KAAI,CAAC,OACH,OAAM,IAAI,qBAAqB;AAGjC,iBAAgB;AACd,MAAI,UACF,QAAO,OAAO,IAAI,UAAU;IAE7B,CAAC,QAAQ,UAAU,CAAC;;;;;;;;ACZzB,SAAgB,qBACd,WACA,UACU;AACV,QAAO,cAAc;AACnB,SAAO,aAAa,WAAW,aAAa,WAAW,SAAS,GAAG;IAClE,CAAC,WAAW,SAAS,CAAC;;;;;;;;ACW3B,SAAgB,aAKd,WACA,SACM;CACN,MAAM,gBAAgB,kBAAkB;AACxC,oBACE,SAAS,UAAU,eACnB,qBAAqB,WAAW,SAAS,SAAS,CACnD;;;;;;;;;;ACzBH,SAAgB,aACd,SACA,SACM;CACN,MAAM,YAAY,cACV,wBAAwB,SAAS,QAAQ,KAAK,MAAM,IAAI,CAAC,EAC/D,CAAC,QAAQ,CACV;AACD,cAAa,WAAW,QAAQ;;;;;;;;;;ACFlC,SAAgB,UAAqC,SAQvC;CACZ,MAAM,SAAS,SAAS,UAAU;CAElC,MAAM,SAAS,kBAAqB;AACpC,KAAI,CAAC,OACH,OAAM,IAAI,cACR,uDACD;CAGH,MAAM,cAAc,gBAAgB;AAEpC,iBAAgB;AACd,MAAI,QAAQ;GACV,MAAM,YAAY,MAChB,mBAAmB,YAAY,EAC/B,oBAAoB,YAAY,CACjC;AACD,UAAO,OAAO,IAAI,UAAU;;IAE7B;EAAC;EAAQ;EAAQ;EAAY,CAAC;AAEjC,QAAO;;AAGT,SAAS,iBAAiB;CACxB,MAAM,GAAG,YAAY,YAAY,MAAc,IAAI,GAAG,EAAE;AACxD,QAAO;;;;;AC5CT,SAAgB,UAAU,QAAgB,SAAqC;CAC7E,MAAM,YAAY,cAAc,aAAa,OAAO,EAAE,CAAC,OAAO,CAAC;AAC/D,cAAa,WAAW,QAAQ;;;;;;;;;;ACClC,SAAgB,eACd,SACA,SACM;CACN,MAAM,YAAY,cACV,qBAAqB,SAAS,QAAQ,KAAK,MAAM,CAAC,EACxD,CAAC,QAAQ,CACV;AACD,cAAa,WAAW,QAAQ"}
|
|
1
|
+
{"version":3,"file":"prosekit-preact.js","names":["PreactMarkViewConsumer: FunctionComponent","PreactNodeViewConsumer: FunctionComponent","ProseKit: ComponentType<ProseKitProps>","derived: Derived"],"sources":["../src/hooks/use-editor-extension.ts","../src/hooks/use-priority-extension.ts","../src/hooks/use-extension.ts","../src/extensions/preact-mark-view.ts","../src/extensions/preact-node-view.ts","../src/components/prosekit.ts","../src/hooks/use-doc-change.ts","../src/hooks/use-editor.ts","../src/hooks/use-editor-derived-value.ts","../src/hooks/use-keymap.ts","../src/hooks/use-state-update.ts"],"sourcesContent":["import {\n EditorNotFoundError,\n type Editor,\n type Extension,\n} from '@prosekit/core'\nimport { useEffect } from 'preact/hooks'\n\n/**\n * @internal\n */\nexport function useEditorExtension(\n editor: Editor | null | undefined,\n extension: Extension | null,\n): void {\n if (!editor) {\n throw new EditorNotFoundError()\n }\n\n useEffect(() => {\n if (extension) {\n return editor.use(extension)\n }\n }, [editor, extension])\n}\n","import {\n withPriority,\n type Extension,\n type Priority,\n} from '@prosekit/core'\nimport { useMemo } from 'preact/hooks'\n\n/**\n * @internal\n */\nexport function usePriorityExtension<T extends Extension = Extension>(\n extension: T | null,\n priority?: Priority | null,\n): T | null {\n return useMemo(() => {\n return extension && priority ? withPriority(extension, priority) : extension\n }, [extension, priority])\n}\n","import type {\n Editor,\n Extension,\n Priority,\n} from '@prosekit/core'\n\nimport { useEditorContext } from '../contexts/editor-context'\n\nimport { useEditorExtension } from './use-editor-extension'\nimport { usePriorityExtension } from './use-priority-extension'\n\nexport interface UseExtensionOptions {\n /**\n * The editor to add the extension to. If not provided, it will use the\n * editor from the nearest `ProseKit` component.\n */\n editor?: Editor\n\n /**\n * Optional priority to add the extension with.\n */\n priority?: Priority\n}\n\n/**\n * Add an extension to the editor.\n */\nexport function useExtension(\n /**\n * The extension to add to the editor. If it changes, the previous\n * extension will be removed and the new one (if not null) will be added.\n */\n extension: Extension | null,\n options?: UseExtensionOptions,\n): void {\n const editorContext = useEditorContext()\n useEditorExtension(\n options?.editor || editorContext,\n usePriorityExtension(extension, options?.priority),\n )\n}\n","import {\n defineMarkViewComponent,\n defineMarkViewFactory,\n type Extension,\n} from '@prosekit/core'\nimport type { MarkViewConstructor } from '@prosekit/pm/view'\nimport type { CoreMarkViewUserOptions } from '@prosemirror-adapter/core'\nimport {\n useMarkViewContext,\n useMarkViewFactory,\n type MarkViewContext,\n type PreactMarkViewUserOptions,\n} from '@prosemirror-adapter/preact'\nimport {\n h,\n type ComponentType,\n type FunctionComponent,\n} from 'preact'\nimport { useMemo } from 'preact/hooks'\n\nimport { useExtension } from '../hooks/use-extension'\n\n/**\n * @public\n */\nexport interface PreactMarkViewProps extends MarkViewContext {}\n\n/**\n * @public\n */\nexport type PreactMarkViewComponent = ComponentType<PreactMarkViewProps>\n\n/**\n * Options for {@link definePreactMarkView}.\n *\n * @public\n */\nexport interface PreactMarkViewOptions extends CoreMarkViewUserOptions<PreactMarkViewComponent> {\n /**\n * The name of the mark type.\n */\n name: string\n}\n\nfunction withMarkViewProps(component: PreactMarkViewComponent) {\n return function MarkViewPropsWrapper() {\n const props: PreactMarkViewProps = useMarkViewContext()\n return h(component, props)\n }\n}\n\n/**\n * @internal\n */\nexport const PreactMarkViewConsumer: FunctionComponent = () => {\n const markViewFactory = useMarkViewFactory()\n const extension = useMemo(\n () => definePreactMarkViewFactory(markViewFactory),\n [markViewFactory],\n )\n useExtension(extension)\n\n return null\n}\n\n/**\n * Defines a mark view using a Preact component.\n *\n * @public\n */\nexport function definePreactMarkView(options: PreactMarkViewOptions): Extension {\n const { name, component, ...userOptions } = options\n\n const args: PreactMarkViewUserOptions = {\n ...userOptions,\n component: withMarkViewProps(component),\n }\n\n return defineMarkViewComponent<PreactMarkViewUserOptions>({\n group: 'preact',\n name,\n args,\n })\n}\n\nfunction definePreactMarkViewFactory(\n factory: (options: PreactMarkViewUserOptions) => MarkViewConstructor,\n) {\n return defineMarkViewFactory<PreactMarkViewUserOptions>({\n group: 'preact',\n factory,\n })\n}\n","import {\n defineNodeViewComponent,\n defineNodeViewFactory,\n type Extension,\n} from '@prosekit/core'\nimport type { NodeViewConstructor } from '@prosekit/pm/view'\nimport type { CoreNodeViewUserOptions } from '@prosemirror-adapter/core'\nimport {\n useNodeViewContext,\n useNodeViewFactory,\n type NodeViewContext,\n type PreactNodeViewUserOptions,\n} from '@prosemirror-adapter/preact'\nimport {\n h,\n type ComponentType,\n type FunctionComponent,\n} from 'preact'\nimport { useMemo } from 'preact/hooks'\n\nimport { useExtension } from '../hooks/use-extension'\n\n/**\n * @public\n */\nexport interface PreactNodeViewProps extends NodeViewContext {}\n\n/**\n * @public\n */\nexport type PreactNodeViewComponent = ComponentType<PreactNodeViewProps>\n\n/**\n * Options for {@link definePreactNodeView}.\n *\n * @public\n */\nexport interface PreactNodeViewOptions extends CoreNodeViewUserOptions<PreactNodeViewComponent> {\n /**\n * The name of the node type.\n */\n name: string\n}\n\nfunction withNodeViewProps(component: PreactNodeViewComponent) {\n return function NodeViewPropsWrapper() {\n const props: PreactNodeViewProps = useNodeViewContext()\n return h(component, props)\n }\n}\n\n/**\n * @internal\n */\nexport const PreactNodeViewConsumer: FunctionComponent = () => {\n const nodeViewFactory = useNodeViewFactory()\n const extension = useMemo(\n () => definePreactNodeViewFactory(nodeViewFactory),\n [nodeViewFactory],\n )\n useExtension(extension)\n\n return null\n}\n\n/**\n * Defines a node view using a Preact component.\n *\n * @public\n */\nexport function definePreactNodeView(options: PreactNodeViewOptions): Extension {\n const { name, component, ...userOptions } = options\n\n const args: PreactNodeViewUserOptions = {\n ...userOptions,\n component: withNodeViewProps(component),\n }\n\n return defineNodeViewComponent<PreactNodeViewUserOptions>({\n group: 'preact',\n name,\n args,\n })\n}\n\nfunction definePreactNodeViewFactory(\n factory: (options: PreactNodeViewUserOptions) => NodeViewConstructor,\n) {\n return defineNodeViewFactory<PreactNodeViewUserOptions>({\n group: 'preact',\n factory,\n })\n}\n","import type { Editor } from '@prosekit/core'\nimport { ProsemirrorAdapterProvider } from '@prosemirror-adapter/preact'\nimport {\n h,\n type ComponentChildren,\n type ComponentType,\n} from 'preact'\n\nimport { EditorContextProvider } from '../contexts/editor-context'\nimport { PreactMarkViewConsumer } from '../extensions/preact-mark-view'\nimport { PreactNodeViewConsumer } from '../extensions/preact-node-view'\n\nexport interface ProseKitProps {\n editor: Editor\n children?: ComponentChildren\n}\n\n/**\n * The root component for a ProseKit editor.\n *\n * @public\n */\nexport const ProseKit: ComponentType<ProseKitProps> = (props) => {\n const { editor, children } = props\n\n return h(\n ProsemirrorAdapterProvider,\n null,\n h(\n EditorContextProvider,\n { value: editor },\n h(PreactNodeViewConsumer, null),\n h(PreactMarkViewConsumer, null),\n children,\n ),\n )\n}\n","import { defineDocChangeHandler } from '@prosekit/core'\nimport type { ProseMirrorNode } from '@prosekit/pm/model'\nimport { useMemo } from 'preact/hooks'\n\nimport {\n useExtension,\n type UseExtensionOptions,\n} from './use-extension'\n\n/**\n * Calls the given handler whenever the editor document changes.\n *\n * @public\n */\nexport function useDocChange(\n handler: (doc: ProseMirrorNode) => void,\n options?: UseExtensionOptions,\n): void {\n const extension = useMemo(\n () => defineDocChangeHandler((view) => handler(view.state.doc)),\n [handler],\n )\n useExtension(extension, options)\n}\n","import {\n defineMountHandler,\n defineUpdateHandler,\n ProseKitError,\n union,\n type Editor,\n type Extension,\n} from '@prosekit/core'\nimport {\n useEffect,\n useReducer,\n} from 'preact/hooks'\n\nimport { useEditorContext } from '../contexts/editor-context'\n\n/**\n * Retrieves the editor instance from the nearest ProseKit component.\n *\n * @public\n */\nexport function useEditor<E extends Extension = any>(options?: {\n /**\n * Whether to update the component when the editor is mounted or editor state\n * is updated.\n *\n * @default false\n */\n update?: boolean\n}): Editor<E> {\n const update = options?.update ?? false\n\n const editor = useEditorContext<E>()\n if (!editor) {\n throw new ProseKitError(\n 'useEditor must be used within the ProseKit component',\n )\n }\n\n const forceUpdate = useForceUpdate()\n\n useEffect(() => {\n if (update) {\n const extension = union(\n defineMountHandler(forceUpdate),\n defineUpdateHandler(forceUpdate),\n )\n return editor.use(extension)\n }\n }, [editor, update, forceUpdate])\n\n return editor\n}\n\nfunction useForceUpdate() {\n const [, dispatch] = useReducer((x: number) => x + 1, 0)\n return dispatch\n}\n","import {\n defineMountHandler,\n defineUpdateHandler,\n EditorNotFoundError,\n union,\n type Editor,\n type Extension,\n} from '@prosekit/core'\nimport { useSyncExternalStore } from 'preact/compat'\nimport { useMemo } from 'preact/hooks'\n\nimport { useEditorContext } from '../contexts/editor-context'\n\nexport interface UseEditorDerivedOptions<E extends Extension = any> {\n /**\n * The editor to add the extension to. If not provided, it will use the\n * editor from the nearest `ProseKit` component.\n */\n editor?: Editor<E>\n}\n\n/**\n * A hook that runs a function to derive a value from the editor instance after\n * editor state changes.\n *\n * This is useful when you need to render something based on the editor state,\n * for example, whether the selected text is wrapped in an italic mark.\n *\n * @public\n */\nexport function useEditorDerivedValue<E extends Extension, Derived>(\n /**\n * A function that receives the editor instance and returns a derived value.\n *\n * It will be called whenever the editor's document state changes, or when it\n * mounts.\n *\n * This function should be memoized.\n */\n derive: (editor: Editor<E>) => Derived,\n options?: UseEditorDerivedOptions<E>,\n): Derived {\n const editorContext = useEditorContext<E>()\n const editor = options?.editor ?? editorContext\n if (!editor) {\n throw new EditorNotFoundError()\n }\n\n const [subscribe, getSnapshot] = useMemo(() => {\n return createEditorStore(editor, derive)\n }, [editor, derive])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n\nfunction createEditorStore<Derived, E extends Extension = any>(editor: Editor<E>, derive: (editor: Editor<E>) => Derived) {\n let dirty = true\n let derived: Derived\n\n const subscribe = (onChange: VoidFunction): VoidFunction => {\n const handleChange = () => {\n dirty = true\n onChange()\n }\n const extension = union(\n defineUpdateHandler(handleChange),\n defineMountHandler(handleChange),\n )\n return editor.use(extension)\n }\n\n const getSnapshot = () => {\n if (dirty) {\n dirty = false\n derived = derive(editor)\n }\n return derived\n }\n\n return [subscribe, getSnapshot] as const\n}\n","import {\n defineKeymap,\n type Keymap,\n} from '@prosekit/core'\nimport { useMemo } from 'preact/hooks'\n\nimport {\n useExtension,\n type UseExtensionOptions,\n} from './use-extension'\n\nexport function useKeymap(keymap: Keymap, options?: UseExtensionOptions): void {\n const extension = useMemo(() => defineKeymap(keymap), [keymap])\n useExtension(extension, options)\n}\n","import { defineUpdateHandler } from '@prosekit/core'\nimport type { EditorState } from '@prosekit/pm/state'\nimport { useMemo } from 'preact/hooks'\n\nimport {\n useExtension,\n type UseExtensionOptions,\n} from './use-extension'\n\n/**\n * Calls the given handler whenever the editor state changes.\n *\n * @public\n */\nexport function useStateUpdate(\n handler: (state: EditorState) => void,\n options?: UseExtensionOptions,\n): void {\n const extension = useMemo(\n () => defineUpdateHandler((view) => handler(view.state)),\n [handler],\n )\n useExtension(extension, options)\n}\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prosekit/preact",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Preact components and utilities for ProseKit",
|
|
7
7
|
"author": {
|
|
@@ -68,9 +68,11 @@
|
|
|
68
68
|
"src"
|
|
69
69
|
],
|
|
70
70
|
"dependencies": {
|
|
71
|
+
"@prosemirror-adapter/core": "^0.4.5",
|
|
72
|
+
"@prosemirror-adapter/preact": "^0.4.5",
|
|
73
|
+
"@prosekit/web": "^0.7.4",
|
|
71
74
|
"@prosekit/core": "^0.8.4",
|
|
72
|
-
"@prosekit/pm": "^0.1.12"
|
|
73
|
-
"@prosekit/web": "^0.7.4"
|
|
75
|
+
"@prosekit/pm": "^0.1.12"
|
|
74
76
|
},
|
|
75
77
|
"peerDependencies": {
|
|
76
78
|
"preact": ">= 10.11.0"
|
|
@@ -81,8 +83,8 @@
|
|
|
81
83
|
}
|
|
82
84
|
},
|
|
83
85
|
"devDependencies": {
|
|
84
|
-
"preact": "^10.27.
|
|
85
|
-
"tsdown": "^0.
|
|
86
|
+
"preact": "^10.27.2",
|
|
87
|
+
"tsdown": "^0.15.6",
|
|
86
88
|
"typescript": "~5.9.2",
|
|
87
89
|
"@prosekit/config-tsdown": "0.0.0",
|
|
88
90
|
"@prosekit/config-vitest": "0.0.0"
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Editor } from '@prosekit/core'
|
|
2
|
+
import { ProsemirrorAdapterProvider } from '@prosemirror-adapter/preact'
|
|
2
3
|
import {
|
|
3
4
|
h,
|
|
4
5
|
type ComponentChildren,
|
|
@@ -6,6 +7,8 @@ import {
|
|
|
6
7
|
} from 'preact'
|
|
7
8
|
|
|
8
9
|
import { EditorContextProvider } from '../contexts/editor-context'
|
|
10
|
+
import { PreactMarkViewConsumer } from '../extensions/preact-mark-view'
|
|
11
|
+
import { PreactNodeViewConsumer } from '../extensions/preact-node-view'
|
|
9
12
|
|
|
10
13
|
export interface ProseKitProps {
|
|
11
14
|
editor: Editor
|
|
@@ -20,5 +23,15 @@ export interface ProseKitProps {
|
|
|
20
23
|
export const ProseKit: ComponentType<ProseKitProps> = (props) => {
|
|
21
24
|
const { editor, children } = props
|
|
22
25
|
|
|
23
|
-
return h(
|
|
26
|
+
return h(
|
|
27
|
+
ProsemirrorAdapterProvider,
|
|
28
|
+
null,
|
|
29
|
+
h(
|
|
30
|
+
EditorContextProvider,
|
|
31
|
+
{ value: editor },
|
|
32
|
+
h(PreactNodeViewConsumer, null),
|
|
33
|
+
h(PreactMarkViewConsumer, null),
|
|
34
|
+
children,
|
|
35
|
+
),
|
|
36
|
+
)
|
|
24
37
|
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineMarkViewComponent,
|
|
3
|
+
defineMarkViewFactory,
|
|
4
|
+
type Extension,
|
|
5
|
+
} from '@prosekit/core'
|
|
6
|
+
import type { MarkViewConstructor } from '@prosekit/pm/view'
|
|
7
|
+
import type { CoreMarkViewUserOptions } from '@prosemirror-adapter/core'
|
|
8
|
+
import {
|
|
9
|
+
useMarkViewContext,
|
|
10
|
+
useMarkViewFactory,
|
|
11
|
+
type MarkViewContext,
|
|
12
|
+
type PreactMarkViewUserOptions,
|
|
13
|
+
} from '@prosemirror-adapter/preact'
|
|
14
|
+
import {
|
|
15
|
+
h,
|
|
16
|
+
type ComponentType,
|
|
17
|
+
type FunctionComponent,
|
|
18
|
+
} from 'preact'
|
|
19
|
+
import { useMemo } from 'preact/hooks'
|
|
20
|
+
|
|
21
|
+
import { useExtension } from '../hooks/use-extension'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export interface PreactMarkViewProps extends MarkViewContext {}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export type PreactMarkViewComponent = ComponentType<PreactMarkViewProps>
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Options for {@link definePreactMarkView}.
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export interface PreactMarkViewOptions extends CoreMarkViewUserOptions<PreactMarkViewComponent> {
|
|
39
|
+
/**
|
|
40
|
+
* The name of the mark type.
|
|
41
|
+
*/
|
|
42
|
+
name: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function withMarkViewProps(component: PreactMarkViewComponent) {
|
|
46
|
+
return function MarkViewPropsWrapper() {
|
|
47
|
+
const props: PreactMarkViewProps = useMarkViewContext()
|
|
48
|
+
return h(component, props)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
export const PreactMarkViewConsumer: FunctionComponent = () => {
|
|
56
|
+
const markViewFactory = useMarkViewFactory()
|
|
57
|
+
const extension = useMemo(
|
|
58
|
+
() => definePreactMarkViewFactory(markViewFactory),
|
|
59
|
+
[markViewFactory],
|
|
60
|
+
)
|
|
61
|
+
useExtension(extension)
|
|
62
|
+
|
|
63
|
+
return null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Defines a mark view using a Preact component.
|
|
68
|
+
*
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export function definePreactMarkView(options: PreactMarkViewOptions): Extension {
|
|
72
|
+
const { name, component, ...userOptions } = options
|
|
73
|
+
|
|
74
|
+
const args: PreactMarkViewUserOptions = {
|
|
75
|
+
...userOptions,
|
|
76
|
+
component: withMarkViewProps(component),
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return defineMarkViewComponent<PreactMarkViewUserOptions>({
|
|
80
|
+
group: 'preact',
|
|
81
|
+
name,
|
|
82
|
+
args,
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function definePreactMarkViewFactory(
|
|
87
|
+
factory: (options: PreactMarkViewUserOptions) => MarkViewConstructor,
|
|
88
|
+
) {
|
|
89
|
+
return defineMarkViewFactory<PreactMarkViewUserOptions>({
|
|
90
|
+
group: 'preact',
|
|
91
|
+
factory,
|
|
92
|
+
})
|
|
93
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineNodeViewComponent,
|
|
3
|
+
defineNodeViewFactory,
|
|
4
|
+
type Extension,
|
|
5
|
+
} from '@prosekit/core'
|
|
6
|
+
import type { NodeViewConstructor } from '@prosekit/pm/view'
|
|
7
|
+
import type { CoreNodeViewUserOptions } from '@prosemirror-adapter/core'
|
|
8
|
+
import {
|
|
9
|
+
useNodeViewContext,
|
|
10
|
+
useNodeViewFactory,
|
|
11
|
+
type NodeViewContext,
|
|
12
|
+
type PreactNodeViewUserOptions,
|
|
13
|
+
} from '@prosemirror-adapter/preact'
|
|
14
|
+
import {
|
|
15
|
+
h,
|
|
16
|
+
type ComponentType,
|
|
17
|
+
type FunctionComponent,
|
|
18
|
+
} from 'preact'
|
|
19
|
+
import { useMemo } from 'preact/hooks'
|
|
20
|
+
|
|
21
|
+
import { useExtension } from '../hooks/use-extension'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export interface PreactNodeViewProps extends NodeViewContext {}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export type PreactNodeViewComponent = ComponentType<PreactNodeViewProps>
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Options for {@link definePreactNodeView}.
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export interface PreactNodeViewOptions extends CoreNodeViewUserOptions<PreactNodeViewComponent> {
|
|
39
|
+
/**
|
|
40
|
+
* The name of the node type.
|
|
41
|
+
*/
|
|
42
|
+
name: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function withNodeViewProps(component: PreactNodeViewComponent) {
|
|
46
|
+
return function NodeViewPropsWrapper() {
|
|
47
|
+
const props: PreactNodeViewProps = useNodeViewContext()
|
|
48
|
+
return h(component, props)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
export const PreactNodeViewConsumer: FunctionComponent = () => {
|
|
56
|
+
const nodeViewFactory = useNodeViewFactory()
|
|
57
|
+
const extension = useMemo(
|
|
58
|
+
() => definePreactNodeViewFactory(nodeViewFactory),
|
|
59
|
+
[nodeViewFactory],
|
|
60
|
+
)
|
|
61
|
+
useExtension(extension)
|
|
62
|
+
|
|
63
|
+
return null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Defines a node view using a Preact component.
|
|
68
|
+
*
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export function definePreactNodeView(options: PreactNodeViewOptions): Extension {
|
|
72
|
+
const { name, component, ...userOptions } = options
|
|
73
|
+
|
|
74
|
+
const args: PreactNodeViewUserOptions = {
|
|
75
|
+
...userOptions,
|
|
76
|
+
component: withNodeViewProps(component),
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return defineNodeViewComponent<PreactNodeViewUserOptions>({
|
|
80
|
+
group: 'preact',
|
|
81
|
+
name,
|
|
82
|
+
args,
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function definePreactNodeViewFactory(
|
|
87
|
+
factory: (options: PreactNodeViewUserOptions) => NodeViewConstructor,
|
|
88
|
+
) {
|
|
89
|
+
return defineNodeViewFactory<PreactNodeViewUserOptions>({
|
|
90
|
+
group: 'preact',
|
|
91
|
+
factory,
|
|
92
|
+
})
|
|
93
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineMountHandler,
|
|
3
|
+
defineUpdateHandler,
|
|
4
|
+
EditorNotFoundError,
|
|
5
|
+
union,
|
|
6
|
+
type Editor,
|
|
7
|
+
type Extension,
|
|
8
|
+
} from '@prosekit/core'
|
|
9
|
+
import { useSyncExternalStore } from 'preact/compat'
|
|
10
|
+
import { useMemo } from 'preact/hooks'
|
|
11
|
+
|
|
12
|
+
import { useEditorContext } from '../contexts/editor-context'
|
|
13
|
+
|
|
14
|
+
export interface UseEditorDerivedOptions<E extends Extension = any> {
|
|
15
|
+
/**
|
|
16
|
+
* The editor to add the extension to. If not provided, it will use the
|
|
17
|
+
* editor from the nearest `ProseKit` component.
|
|
18
|
+
*/
|
|
19
|
+
editor?: Editor<E>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A hook that runs a function to derive a value from the editor instance after
|
|
24
|
+
* editor state changes.
|
|
25
|
+
*
|
|
26
|
+
* This is useful when you need to render something based on the editor state,
|
|
27
|
+
* for example, whether the selected text is wrapped in an italic mark.
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export function useEditorDerivedValue<E extends Extension, Derived>(
|
|
32
|
+
/**
|
|
33
|
+
* A function that receives the editor instance and returns a derived value.
|
|
34
|
+
*
|
|
35
|
+
* It will be called whenever the editor's document state changes, or when it
|
|
36
|
+
* mounts.
|
|
37
|
+
*
|
|
38
|
+
* This function should be memoized.
|
|
39
|
+
*/
|
|
40
|
+
derive: (editor: Editor<E>) => Derived,
|
|
41
|
+
options?: UseEditorDerivedOptions<E>,
|
|
42
|
+
): Derived {
|
|
43
|
+
const editorContext = useEditorContext<E>()
|
|
44
|
+
const editor = options?.editor ?? editorContext
|
|
45
|
+
if (!editor) {
|
|
46
|
+
throw new EditorNotFoundError()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const [subscribe, getSnapshot] = useMemo(() => {
|
|
50
|
+
return createEditorStore(editor, derive)
|
|
51
|
+
}, [editor, derive])
|
|
52
|
+
|
|
53
|
+
return useSyncExternalStore(subscribe, getSnapshot)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function createEditorStore<Derived, E extends Extension = any>(editor: Editor<E>, derive: (editor: Editor<E>) => Derived) {
|
|
57
|
+
let dirty = true
|
|
58
|
+
let derived: Derived
|
|
59
|
+
|
|
60
|
+
const subscribe = (onChange: VoidFunction): VoidFunction => {
|
|
61
|
+
const handleChange = () => {
|
|
62
|
+
dirty = true
|
|
63
|
+
onChange()
|
|
64
|
+
}
|
|
65
|
+
const extension = union(
|
|
66
|
+
defineUpdateHandler(handleChange),
|
|
67
|
+
defineMountHandler(handleChange),
|
|
68
|
+
)
|
|
69
|
+
return editor.use(extension)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const getSnapshot = () => {
|
|
73
|
+
if (dirty) {
|
|
74
|
+
dirty = false
|
|
75
|
+
derived = derive(editor)
|
|
76
|
+
}
|
|
77
|
+
return derived
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return [subscribe, getSnapshot] as const
|
|
81
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,8 +2,24 @@ export {
|
|
|
2
2
|
ProseKit,
|
|
3
3
|
type ProseKitProps,
|
|
4
4
|
} from './components/prosekit'
|
|
5
|
+
export {
|
|
6
|
+
definePreactMarkView,
|
|
7
|
+
type PreactMarkViewComponent,
|
|
8
|
+
type PreactMarkViewOptions,
|
|
9
|
+
type PreactMarkViewProps,
|
|
10
|
+
} from './extensions/preact-mark-view'
|
|
11
|
+
export {
|
|
12
|
+
definePreactNodeView,
|
|
13
|
+
type PreactNodeViewComponent,
|
|
14
|
+
type PreactNodeViewOptions,
|
|
15
|
+
type PreactNodeViewProps,
|
|
16
|
+
} from './extensions/preact-node-view'
|
|
5
17
|
export { useDocChange } from './hooks/use-doc-change'
|
|
6
18
|
export { useEditor } from './hooks/use-editor'
|
|
19
|
+
export {
|
|
20
|
+
useEditorDerivedValue,
|
|
21
|
+
type UseEditorDerivedOptions,
|
|
22
|
+
} from './hooks/use-editor-derived-value'
|
|
7
23
|
export {
|
|
8
24
|
useExtension,
|
|
9
25
|
type UseExtensionOptions,
|