@prosekit/preact 0.5.0 → 0.5.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.
- package/dist/create-component-D7yDR5ux.js +2 -1
- package/dist/create-component-D7yDR5ux.js.map +1 -0
- package/dist/{create-props-CkTwd_m_.d.ts → create-props-EGV61dJR.d.ts} +3 -2
- package/dist/create-props-EGV61dJR.d.ts.map +1 -0
- package/dist/editor-context-imq7MdJr.js +2 -1
- package/dist/editor-context-imq7MdJr.js.map +1 -0
- package/dist/prosekit-preact-autocomplete.d.ts +11 -10
- package/dist/prosekit-preact-autocomplete.d.ts.map +1 -0
- package/dist/prosekit-preact-autocomplete.js +2 -1
- package/dist/prosekit-preact-autocomplete.js.map +1 -0
- package/dist/prosekit-preact-block-handle.d.ts +9 -8
- package/dist/prosekit-preact-block-handle.d.ts.map +1 -0
- package/dist/prosekit-preact-block-handle.js +2 -1
- package/dist/prosekit-preact-block-handle.js.map +1 -0
- package/dist/prosekit-preact-drop-indicator.d.ts +5 -4
- package/dist/prosekit-preact-drop-indicator.d.ts.map +1 -0
- package/dist/prosekit-preact-drop-indicator.js +2 -1
- package/dist/prosekit-preact-drop-indicator.js.map +1 -0
- package/dist/prosekit-preact-inline-popover.d.ts +5 -4
- package/dist/prosekit-preact-inline-popover.d.ts.map +1 -0
- package/dist/prosekit-preact-inline-popover.js +2 -1
- package/dist/prosekit-preact-inline-popover.js.map +1 -0
- package/dist/prosekit-preact-popover.d.ts +9 -8
- package/dist/prosekit-preact-popover.d.ts.map +1 -0
- package/dist/prosekit-preact-popover.js +2 -1
- package/dist/prosekit-preact-popover.js.map +1 -0
- package/dist/prosekit-preact-resizable.d.ts +7 -6
- package/dist/prosekit-preact-resizable.d.ts.map +1 -0
- package/dist/prosekit-preact-resizable.js +2 -1
- package/dist/prosekit-preact-resizable.js.map +1 -0
- package/dist/prosekit-preact-table-handle.d.ts +21 -20
- package/dist/prosekit-preact-table-handle.d.ts.map +1 -0
- package/dist/prosekit-preact-table-handle.js +2 -1
- package/dist/prosekit-preact-table-handle.js.map +1 -0
- package/dist/prosekit-preact-tooltip.d.ts +9 -8
- package/dist/prosekit-preact-tooltip.d.ts.map +1 -0
- package/dist/prosekit-preact-tooltip.js +2 -1
- package/dist/prosekit-preact-tooltip.js.map +1 -0
- package/dist/prosekit-preact.d.ts +40 -34
- package/dist/prosekit-preact.d.ts.map +1 -0
- package/dist/prosekit-preact.js +2 -1
- package/dist/prosekit-preact.js.map +1 -0
- package/package.json +14 -14
- package/src/components/autocomplete/autocomplete-empty.gen.ts +34 -0
- package/src/components/autocomplete/autocomplete-item.gen.ts +34 -0
- package/src/components/autocomplete/autocomplete-list.gen.ts +34 -0
- package/src/components/autocomplete/autocomplete-popover.gen.ts +34 -0
- package/src/components/autocomplete/index.gen.ts +7 -0
- package/src/components/block-handle/block-handle-add.gen.ts +34 -0
- package/src/components/block-handle/block-handle-draggable.gen.ts +34 -0
- package/src/components/block-handle/block-handle-popover.gen.ts +34 -0
- package/src/components/block-handle/index.gen.ts +5 -0
- package/src/components/create-component.ts +135 -0
- package/src/components/create-props.ts +13 -0
- package/src/components/drop-indicator/drop-indicator.gen.ts +34 -0
- package/src/components/drop-indicator/index.gen.ts +1 -0
- package/src/components/inline-popover/index.gen.ts +1 -0
- package/src/components/inline-popover/inline-popover.gen.ts +34 -0
- package/src/components/merge-refs.ts +35 -0
- package/src/components/popover/index.gen.ts +5 -0
- package/src/components/popover/popover-content.gen.ts +34 -0
- package/src/components/popover/popover-root.gen.ts +34 -0
- package/src/components/popover/popover-trigger.gen.ts +34 -0
- package/src/components/prosekit.ts +24 -0
- package/src/components/resizable/index.gen.ts +3 -0
- package/src/components/resizable/resizable-handle.gen.ts +34 -0
- package/src/components/resizable/resizable-root.gen.ts +34 -0
- package/src/components/table-handle/index.gen.ts +17 -0
- package/src/components/table-handle/table-handle-column-root.gen.ts +34 -0
- package/src/components/table-handle/table-handle-column-trigger.gen.ts +34 -0
- package/src/components/table-handle/table-handle-drag-preview.gen.ts +34 -0
- package/src/components/table-handle/table-handle-drop-indicator.gen.ts +34 -0
- package/src/components/table-handle/table-handle-popover-content.gen.ts +34 -0
- package/src/components/table-handle/table-handle-popover-item.gen.ts +34 -0
- package/src/components/table-handle/table-handle-root.gen.ts +34 -0
- package/src/components/table-handle/table-handle-row-root.gen.ts +34 -0
- package/src/components/table-handle/table-handle-row-trigger.gen.ts +34 -0
- package/src/components/tooltip/index.gen.ts +5 -0
- package/src/components/tooltip/tooltip-content.gen.ts +34 -0
- package/src/components/tooltip/tooltip-root.gen.ts +34 -0
- package/src/components/tooltip/tooltip-trigger.gen.ts +34 -0
- package/src/contexts/editor-context.ts +23 -0
- package/src/hooks/use-doc-change.ts +24 -0
- package/src/hooks/use-editor-extension.ts +24 -0
- package/src/hooks/use-editor.ts +57 -0
- package/src/hooks/use-extension.ts +41 -0
- package/src/hooks/use-keymap.ts +15 -0
- package/src/hooks/use-priority-extension.ts +18 -0
- package/src/hooks/use-state-update.ts +24 -0
- package/src/index.ts +16 -0
- package/src/types.ts +16 -0
|
@@ -14,4 +14,5 @@ const TooltipRoot = createComponent("prosekit-tooltip-root", "TooltipRoot", Obje
|
|
|
14
14
|
const TooltipTrigger = createComponent("prosekit-tooltip-trigger", "TooltipTrigger", Object.keys(tooltipTriggerProps), Object.keys(tooltipTriggerEvents));
|
|
15
15
|
|
|
16
16
|
//#endregion
|
|
17
|
-
export { TooltipContent, TooltipRoot, TooltipTrigger };
|
|
17
|
+
export { TooltipContent, TooltipRoot, TooltipTrigger };
|
|
18
|
+
//# sourceMappingURL=prosekit-preact-tooltip.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prosekit-preact-tooltip.js","names":["TooltipContent: ForwardRefExoticComponent<\n Partial<TooltipContentProps> &\n RefAttributes<TooltipContentElement> &\n HTMLAttributes<TooltipContentElement>\n>","TooltipRoot: ForwardRefExoticComponent<\n Partial<TooltipRootProps> &\n RefAttributes<TooltipRootElement> &\n HTMLAttributes<TooltipRootElement>\n>","TooltipTrigger: ForwardRefExoticComponent<\n Partial<TooltipTriggerProps> &\n RefAttributes<TooltipTriggerElement> &\n HTMLAttributes<TooltipTriggerElement>\n>"],"sources":["../src/components/tooltip/tooltip-content.gen.ts","../src/components/tooltip/tooltip-root.gen.ts","../src/components/tooltip/tooltip-trigger.gen.ts"],"sourcesContent":["import {\n type TooltipContentElement,\n type TooltipContentProps as Props,\n type TooltipContentEvents as Events,\n tooltipContentProps,\n tooltipContentEvents,\n} from '@prosekit/web/tooltip'\nimport type {\n ForwardRefExoticComponent,\n HTMLAttributes,\n RefAttributes,\n} from 'preact/compat'\n\nimport { createComponent } from '../create-component'\nimport type { CreateProps } from '../create-props'\n\n/**\n * Props for the {@link TooltipContent} component.\n */\nexport interface TooltipContentProps extends Partial<CreateProps<Props, Events>> {}\n \nexport const TooltipContent: ForwardRefExoticComponent<\n Partial<TooltipContentProps> &\n RefAttributes<TooltipContentElement> &\n HTMLAttributes<TooltipContentElement>\n> = createComponent<\n TooltipContentProps, \n TooltipContentElement\n>(\n 'prosekit-tooltip-content',\n 'TooltipContent',\n Object.keys(tooltipContentProps),\n Object.keys(tooltipContentEvents),\n)\n","import {\n type TooltipRootElement,\n type TooltipRootProps as Props,\n type TooltipRootEvents as Events,\n tooltipRootProps,\n tooltipRootEvents,\n} from '@prosekit/web/tooltip'\nimport type {\n ForwardRefExoticComponent,\n HTMLAttributes,\n RefAttributes,\n} from 'preact/compat'\n\nimport { createComponent } from '../create-component'\nimport type { CreateProps } from '../create-props'\n\n/**\n * Props for the {@link TooltipRoot} component.\n */\nexport interface TooltipRootProps extends Partial<CreateProps<Props, Events>> {}\n \nexport const TooltipRoot: ForwardRefExoticComponent<\n Partial<TooltipRootProps> &\n RefAttributes<TooltipRootElement> &\n HTMLAttributes<TooltipRootElement>\n> = createComponent<\n TooltipRootProps, \n TooltipRootElement\n>(\n 'prosekit-tooltip-root',\n 'TooltipRoot',\n Object.keys(tooltipRootProps),\n Object.keys(tooltipRootEvents),\n)\n","import {\n type TooltipTriggerElement,\n type TooltipTriggerProps as Props,\n type TooltipTriggerEvents as Events,\n tooltipTriggerProps,\n tooltipTriggerEvents,\n} from '@prosekit/web/tooltip'\nimport type {\n ForwardRefExoticComponent,\n HTMLAttributes,\n RefAttributes,\n} from 'preact/compat'\n\nimport { createComponent } from '../create-component'\nimport type { CreateProps } from '../create-props'\n\n/**\n * Props for the {@link TooltipTrigger} component.\n */\nexport interface TooltipTriggerProps extends Partial<CreateProps<Props, Events>> {}\n \nexport const TooltipTrigger: ForwardRefExoticComponent<\n Partial<TooltipTriggerProps> &\n RefAttributes<TooltipTriggerElement> &\n HTMLAttributes<TooltipTriggerElement>\n> = createComponent<\n TooltipTriggerProps, \n TooltipTriggerElement\n>(\n 'prosekit-tooltip-trigger',\n 'TooltipTrigger',\n Object.keys(tooltipTriggerProps),\n Object.keys(tooltipTriggerEvents),\n)\n"],"mappings":";;;;;AAqBA,MAAaA,iBAIT,gBAIF,4BACA,kBACA,OAAO,KAAK,oBAAoB,EAChC,OAAO,KAAK,qBAAqB,CAClC;;;;ACZD,MAAaC,cAIT,gBAIF,yBACA,eACA,OAAO,KAAK,iBAAiB,EAC7B,OAAO,KAAK,kBAAkB,CAC/B;;;;ACZD,MAAaC,iBAIT,gBAIF,4BACA,kBACA,OAAO,KAAK,oBAAoB,EAChC,OAAO,KAAK,qBAAqB,CAClC"}
|
|
@@ -9,50 +9,55 @@ interface ProseKitProps {
|
|
|
9
9
|
children?: ComponentChildren;
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
|
-
* The root component for a ProseKit editor.
|
|
13
|
-
*
|
|
14
|
-
* @public
|
|
15
|
-
*/
|
|
12
|
+
* The root component for a ProseKit editor.
|
|
13
|
+
*
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
16
|
declare const ProseKit: ComponentType<ProseKitProps>;
|
|
17
17
|
//#endregion
|
|
18
18
|
//#region src/hooks/use-extension.d.ts
|
|
19
19
|
interface UseExtensionOptions {
|
|
20
20
|
/**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
* The editor to add the extension to. If not provided, it will use the
|
|
22
|
+
* editor from the nearest `ProseKit` component.
|
|
23
|
+
*/
|
|
24
24
|
editor?: Editor;
|
|
25
25
|
/**
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
* Optional priority to add the extension with.
|
|
27
|
+
*/
|
|
28
28
|
priority?: Priority;
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
|
-
* Add an extension to the editor.
|
|
32
|
-
*/
|
|
33
|
-
declare function useExtension(
|
|
31
|
+
* Add an extension to the editor.
|
|
32
|
+
*/
|
|
33
|
+
declare function useExtension(
|
|
34
|
+
/**
|
|
35
|
+
* The extension to add to the editor. If it changes, the previous
|
|
36
|
+
* extension will be removed and the new one (if not null) will be added.
|
|
37
|
+
*/
|
|
38
|
+
extension: Extension | null, options?: UseExtensionOptions): void;
|
|
34
39
|
//#endregion
|
|
35
40
|
//#region src/hooks/use-doc-change.d.ts
|
|
36
41
|
/**
|
|
37
|
-
* Calls the given handler whenever the editor document changes.
|
|
38
|
-
*
|
|
39
|
-
* @public
|
|
40
|
-
*/
|
|
42
|
+
* Calls the given handler whenever the editor document changes.
|
|
43
|
+
*
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
41
46
|
declare function useDocChange(handler: (doc: ProseMirrorNode) => void, options?: UseExtensionOptions): void;
|
|
42
47
|
//#endregion
|
|
43
48
|
//#region src/hooks/use-editor.d.ts
|
|
44
49
|
/**
|
|
45
|
-
* Retrieves the editor instance from the nearest ProseKit component.
|
|
46
|
-
*
|
|
47
|
-
* @public
|
|
48
|
-
*/
|
|
50
|
+
* Retrieves the editor instance from the nearest ProseKit component.
|
|
51
|
+
*
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
49
54
|
declare function useEditor<E extends Extension = any>(options?: {
|
|
50
55
|
/**
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
* Whether to update the component when the editor is mounted or editor state
|
|
57
|
+
* is updated.
|
|
58
|
+
*
|
|
59
|
+
* @default false
|
|
60
|
+
*/
|
|
56
61
|
update?: boolean;
|
|
57
62
|
}): Editor<E>;
|
|
58
63
|
//#endregion
|
|
@@ -61,25 +66,26 @@ declare function useKeymap(keymap: Keymap, options?: UseExtensionOptions): void;
|
|
|
61
66
|
//#endregion
|
|
62
67
|
//#region src/hooks/use-state-update.d.ts
|
|
63
68
|
/**
|
|
64
|
-
* Calls the given handler whenever the editor state changes.
|
|
65
|
-
*
|
|
66
|
-
* @public
|
|
67
|
-
*/
|
|
69
|
+
* Calls the given handler whenever the editor state changes.
|
|
70
|
+
*
|
|
71
|
+
* @public
|
|
72
|
+
*/
|
|
68
73
|
declare function useStateUpdate(handler: (state: EditorState) => void, options?: UseExtensionOptions): void;
|
|
69
74
|
//#endregion
|
|
70
75
|
//#region src/types.d.ts
|
|
71
76
|
/**
|
|
72
|
-
* @internal
|
|
73
|
-
*/
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
74
79
|
type PropsWithClass<P = unknown> = P & {
|
|
75
80
|
class?: string | undefined;
|
|
76
81
|
className?: string | undefined;
|
|
77
82
|
};
|
|
78
83
|
/**
|
|
79
|
-
* @internal
|
|
80
|
-
*/
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
81
86
|
type PropsWithChildren<P = unknown> = P & {
|
|
82
87
|
children?: ComponentChildren | undefined;
|
|
83
88
|
};
|
|
84
89
|
//#endregion
|
|
85
|
-
export { type PropsWithChildren, type PropsWithClass, ProseKit, type ProseKitProps, type UseExtensionOptions, useDocChange, useEditor, useExtension, useKeymap, useStateUpdate };
|
|
90
|
+
export { type PropsWithChildren, type PropsWithClass, ProseKit, type ProseKitProps, type UseExtensionOptions, useDocChange, useEditor, useExtension, useKeymap, useStateUpdate };
|
|
91
|
+
//# sourceMappingURL=prosekit-preact.d.ts.map
|
|
@@ -0,0 +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":";;;;;;UASiB,aAAA;UACP;aACG;;AAFb;;;;;AAUa,cAAA,QAIZ,EAJsB,aAItB,CAJoC,aAIpC,CAAA;;;UCZgB,mBAAA;;;;;EDFA,MAAA,CAAA,ECON,MDPM;EAAa;;;EAEA,QAAA,CAAA,ECUjB,QDViB;AAQ9B;;;;AAAoC,iBCQpB,YAAA;;;;AAhBhB;SAAoC,EAqBvB,SArBuB,GAAA,IAAA,EAAA,OAAA,CAAA,EAsBxB,mBAtBwB,CAAA,EAAA,IAAA;;;;;;;ADFpC;AAA8B,iBEKd,YAAA,CFLc,OAAA,EAAA,CAAA,GAAA,EEMb,eFNa,EAAA,GAAA,IAAA,EAAA,OAAA,CAAA,EEOlB,mBFPkB,CAAA,EAAA,IAAA;;;;;;;;AAAb,iBGWD,SHXc,CAAA,UGWM,SHXN,GAAA,GAAA,CAAA,CAAA,QAAA,EAAA;EAAA;;;;AAU9B;;QAAqC,CAAA,EAAA,OAAA;IGSjC,MHTmB,CGSZ,CHTY,CAAA;;;iBIRP,SAAA,SAAkB,kBAAkB;;;;;;;AJFpD;AAA8B,iBKKd,cAAA,CLLc,OAAA,EAAA,CAAA,KAAA,EKMX,WLNW,EAAA,GAAA,IAAA,EAAA,OAAA,CAAA,EKOlB,mBLPkB,CAAA,EAAA,IAAA;;;;;;KMJlB,8BAA8B;;ENIzB,SAAA,CAAA,EAAA,MAAa,GAAA,SAAA;CAAA;;;;AAUjB,KMND,iBNUX,CAAA,IAAA,OAAA,CAAA,GMV4C,CNU5C,GAAA;EAAA,QAAA,CAAA,EMTY,iBNSZ,GAAA,SAAA"}
|
package/dist/prosekit-preact.js
CHANGED
|
@@ -111,4 +111,5 @@ function useStateUpdate(handler, options) {
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
//#endregion
|
|
114
|
-
export { ProseKit, useDocChange, useEditor, useExtension, useKeymap, useStateUpdate };
|
|
114
|
+
export { ProseKit, useDocChange, useEditor, useExtension, useKeymap, useStateUpdate };
|
|
115
|
+
//# sourceMappingURL=prosekit-preact.js.map
|
|
@@ -0,0 +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"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prosekit/preact",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.5.
|
|
4
|
+
"version": "0.5.1",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Preact components and utilities for ProseKit",
|
|
7
7
|
"author": {
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
},
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"funding": "https://github.com/sponsors/ocavue",
|
|
13
|
-
"homepage": "https://github.com/
|
|
13
|
+
"homepage": "https://github.com/prosekit/prosekit#readme",
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
|
-
"url": "git+https://github.com/
|
|
16
|
+
"url": "git+https://github.com/prosekit/prosekit.git",
|
|
17
17
|
"directory": "packages/preact"
|
|
18
18
|
},
|
|
19
19
|
"bugs": {
|
|
20
|
-
"url": "https://github.com/
|
|
20
|
+
"url": "https://github.com/prosekit/prosekit/issues"
|
|
21
21
|
},
|
|
22
22
|
"keywords": [
|
|
23
23
|
"ProseMirror"
|
|
@@ -64,12 +64,13 @@
|
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
66
|
"files": [
|
|
67
|
-
"dist"
|
|
67
|
+
"dist",
|
|
68
|
+
"src"
|
|
68
69
|
],
|
|
69
70
|
"dependencies": {
|
|
70
|
-
"@prosekit/core": "^0.8.
|
|
71
|
-
"@prosekit/
|
|
72
|
-
"@prosekit/
|
|
71
|
+
"@prosekit/core": "^0.8.4",
|
|
72
|
+
"@prosekit/pm": "^0.1.12",
|
|
73
|
+
"@prosekit/web": "^0.7.4"
|
|
73
74
|
},
|
|
74
75
|
"peerDependencies": {
|
|
75
76
|
"preact": ">= 10.11.0"
|
|
@@ -80,10 +81,9 @@
|
|
|
80
81
|
}
|
|
81
82
|
},
|
|
82
83
|
"devDependencies": {
|
|
83
|
-
"preact": "^10.27.
|
|
84
|
-
"tsdown": "^0.
|
|
85
|
-
"typescript": "~5.
|
|
86
|
-
"vitest": "^3.2.4",
|
|
84
|
+
"preact": "^10.27.1",
|
|
85
|
+
"tsdown": "^0.14.2",
|
|
86
|
+
"typescript": "~5.9.2",
|
|
87
87
|
"@prosekit/config-tsdown": "0.0.0",
|
|
88
88
|
"@prosekit/config-vitest": "0.0.0"
|
|
89
89
|
},
|
|
@@ -95,12 +95,12 @@
|
|
|
95
95
|
"prosekit-preact": "./src/index.ts",
|
|
96
96
|
"prosekit-preact-autocomplete": "./src/components/autocomplete/index.gen.ts",
|
|
97
97
|
"prosekit-preact-block-handle": "./src/components/block-handle/index.gen.ts",
|
|
98
|
+
"prosekit-preact-drop-indicator": "./src/components/drop-indicator/index.gen.ts",
|
|
98
99
|
"prosekit-preact-inline-popover": "./src/components/inline-popover/index.gen.ts",
|
|
99
100
|
"prosekit-preact-popover": "./src/components/popover/index.gen.ts",
|
|
100
101
|
"prosekit-preact-resizable": "./src/components/resizable/index.gen.ts",
|
|
101
102
|
"prosekit-preact-table-handle": "./src/components/table-handle/index.gen.ts",
|
|
102
|
-
"prosekit-preact-tooltip": "./src/components/tooltip/index.gen.ts"
|
|
103
|
-
"prosekit-preact-drop-indicator": "./src/components/drop-indicator/index.gen.ts"
|
|
103
|
+
"prosekit-preact-tooltip": "./src/components/tooltip/index.gen.ts"
|
|
104
104
|
}
|
|
105
105
|
},
|
|
106
106
|
"scripts": {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AutocompleteEmptyElement,
|
|
3
|
+
type AutocompleteEmptyProps as Props,
|
|
4
|
+
type AutocompleteEmptyEvents as Events,
|
|
5
|
+
autocompleteEmptyProps,
|
|
6
|
+
autocompleteEmptyEvents,
|
|
7
|
+
} from '@prosekit/web/autocomplete'
|
|
8
|
+
import type {
|
|
9
|
+
ForwardRefExoticComponent,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
RefAttributes,
|
|
12
|
+
} from 'preact/compat'
|
|
13
|
+
|
|
14
|
+
import { createComponent } from '../create-component'
|
|
15
|
+
import type { CreateProps } from '../create-props'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Props for the {@link AutocompleteEmpty} component.
|
|
19
|
+
*/
|
|
20
|
+
export interface AutocompleteEmptyProps extends Partial<CreateProps<Props, Events>> {}
|
|
21
|
+
|
|
22
|
+
export const AutocompleteEmpty: ForwardRefExoticComponent<
|
|
23
|
+
Partial<AutocompleteEmptyProps> &
|
|
24
|
+
RefAttributes<AutocompleteEmptyElement> &
|
|
25
|
+
HTMLAttributes<AutocompleteEmptyElement>
|
|
26
|
+
> = createComponent<
|
|
27
|
+
AutocompleteEmptyProps,
|
|
28
|
+
AutocompleteEmptyElement
|
|
29
|
+
>(
|
|
30
|
+
'prosekit-autocomplete-empty',
|
|
31
|
+
'AutocompleteEmpty',
|
|
32
|
+
Object.keys(autocompleteEmptyProps),
|
|
33
|
+
Object.keys(autocompleteEmptyEvents),
|
|
34
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AutocompleteItemElement,
|
|
3
|
+
type AutocompleteItemProps as Props,
|
|
4
|
+
type AutocompleteItemEvents as Events,
|
|
5
|
+
autocompleteItemProps,
|
|
6
|
+
autocompleteItemEvents,
|
|
7
|
+
} from '@prosekit/web/autocomplete'
|
|
8
|
+
import type {
|
|
9
|
+
ForwardRefExoticComponent,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
RefAttributes,
|
|
12
|
+
} from 'preact/compat'
|
|
13
|
+
|
|
14
|
+
import { createComponent } from '../create-component'
|
|
15
|
+
import type { CreateProps } from '../create-props'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Props for the {@link AutocompleteItem} component.
|
|
19
|
+
*/
|
|
20
|
+
export interface AutocompleteItemProps extends Partial<CreateProps<Props, Events>> {}
|
|
21
|
+
|
|
22
|
+
export const AutocompleteItem: ForwardRefExoticComponent<
|
|
23
|
+
Partial<AutocompleteItemProps> &
|
|
24
|
+
RefAttributes<AutocompleteItemElement> &
|
|
25
|
+
HTMLAttributes<AutocompleteItemElement>
|
|
26
|
+
> = createComponent<
|
|
27
|
+
AutocompleteItemProps,
|
|
28
|
+
AutocompleteItemElement
|
|
29
|
+
>(
|
|
30
|
+
'prosekit-autocomplete-item',
|
|
31
|
+
'AutocompleteItem',
|
|
32
|
+
Object.keys(autocompleteItemProps),
|
|
33
|
+
Object.keys(autocompleteItemEvents),
|
|
34
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AutocompleteListElement,
|
|
3
|
+
type AutocompleteListProps as Props,
|
|
4
|
+
type AutocompleteListEvents as Events,
|
|
5
|
+
autocompleteListProps,
|
|
6
|
+
autocompleteListEvents,
|
|
7
|
+
} from '@prosekit/web/autocomplete'
|
|
8
|
+
import type {
|
|
9
|
+
ForwardRefExoticComponent,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
RefAttributes,
|
|
12
|
+
} from 'preact/compat'
|
|
13
|
+
|
|
14
|
+
import { createComponent } from '../create-component'
|
|
15
|
+
import type { CreateProps } from '../create-props'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Props for the {@link AutocompleteList} component.
|
|
19
|
+
*/
|
|
20
|
+
export interface AutocompleteListProps extends Partial<CreateProps<Props, Events>> {}
|
|
21
|
+
|
|
22
|
+
export const AutocompleteList: ForwardRefExoticComponent<
|
|
23
|
+
Partial<AutocompleteListProps> &
|
|
24
|
+
RefAttributes<AutocompleteListElement> &
|
|
25
|
+
HTMLAttributes<AutocompleteListElement>
|
|
26
|
+
> = createComponent<
|
|
27
|
+
AutocompleteListProps,
|
|
28
|
+
AutocompleteListElement
|
|
29
|
+
>(
|
|
30
|
+
'prosekit-autocomplete-list',
|
|
31
|
+
'AutocompleteList',
|
|
32
|
+
Object.keys(autocompleteListProps),
|
|
33
|
+
Object.keys(autocompleteListEvents),
|
|
34
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AutocompletePopoverElement,
|
|
3
|
+
type AutocompletePopoverProps as Props,
|
|
4
|
+
type AutocompletePopoverEvents as Events,
|
|
5
|
+
autocompletePopoverProps,
|
|
6
|
+
autocompletePopoverEvents,
|
|
7
|
+
} from '@prosekit/web/autocomplete'
|
|
8
|
+
import type {
|
|
9
|
+
ForwardRefExoticComponent,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
RefAttributes,
|
|
12
|
+
} from 'preact/compat'
|
|
13
|
+
|
|
14
|
+
import { createComponent } from '../create-component'
|
|
15
|
+
import type { CreateProps } from '../create-props'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Props for the {@link AutocompletePopover} component.
|
|
19
|
+
*/
|
|
20
|
+
export interface AutocompletePopoverProps extends Partial<CreateProps<Props, Events>> {}
|
|
21
|
+
|
|
22
|
+
export const AutocompletePopover: ForwardRefExoticComponent<
|
|
23
|
+
Partial<AutocompletePopoverProps> &
|
|
24
|
+
RefAttributes<AutocompletePopoverElement> &
|
|
25
|
+
HTMLAttributes<AutocompletePopoverElement>
|
|
26
|
+
> = createComponent<
|
|
27
|
+
AutocompletePopoverProps,
|
|
28
|
+
AutocompletePopoverElement
|
|
29
|
+
>(
|
|
30
|
+
'prosekit-autocomplete-popover',
|
|
31
|
+
'AutocompletePopover',
|
|
32
|
+
Object.keys(autocompletePopoverProps),
|
|
33
|
+
Object.keys(autocompletePopoverEvents),
|
|
34
|
+
)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { AutocompleteEmpty, type AutocompleteEmptyProps } from './autocomplete-empty.gen'
|
|
2
|
+
|
|
3
|
+
export { AutocompleteItem, type AutocompleteItemProps } from './autocomplete-item.gen'
|
|
4
|
+
|
|
5
|
+
export { AutocompleteList, type AutocompleteListProps } from './autocomplete-list.gen'
|
|
6
|
+
|
|
7
|
+
export { AutocompletePopover, type AutocompletePopoverProps } from './autocomplete-popover.gen'
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BlockHandleAddElement,
|
|
3
|
+
type BlockHandleAddProps as Props,
|
|
4
|
+
type BlockHandleAddEvents as Events,
|
|
5
|
+
blockHandleAddProps,
|
|
6
|
+
blockHandleAddEvents,
|
|
7
|
+
} from '@prosekit/web/block-handle'
|
|
8
|
+
import type {
|
|
9
|
+
ForwardRefExoticComponent,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
RefAttributes,
|
|
12
|
+
} from 'preact/compat'
|
|
13
|
+
|
|
14
|
+
import { createComponent } from '../create-component'
|
|
15
|
+
import type { CreateProps } from '../create-props'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Props for the {@link BlockHandleAdd} component.
|
|
19
|
+
*/
|
|
20
|
+
export interface BlockHandleAddProps extends Partial<CreateProps<Props, Events>> {}
|
|
21
|
+
|
|
22
|
+
export const BlockHandleAdd: ForwardRefExoticComponent<
|
|
23
|
+
Partial<BlockHandleAddProps> &
|
|
24
|
+
RefAttributes<BlockHandleAddElement> &
|
|
25
|
+
HTMLAttributes<BlockHandleAddElement>
|
|
26
|
+
> = createComponent<
|
|
27
|
+
BlockHandleAddProps,
|
|
28
|
+
BlockHandleAddElement
|
|
29
|
+
>(
|
|
30
|
+
'prosekit-block-handle-add',
|
|
31
|
+
'BlockHandleAdd',
|
|
32
|
+
Object.keys(blockHandleAddProps),
|
|
33
|
+
Object.keys(blockHandleAddEvents),
|
|
34
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BlockHandleDraggableElement,
|
|
3
|
+
type BlockHandleDraggableProps as Props,
|
|
4
|
+
type BlockHandleDraggableEvents as Events,
|
|
5
|
+
blockHandleDraggableProps,
|
|
6
|
+
blockHandleDraggableEvents,
|
|
7
|
+
} from '@prosekit/web/block-handle'
|
|
8
|
+
import type {
|
|
9
|
+
ForwardRefExoticComponent,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
RefAttributes,
|
|
12
|
+
} from 'preact/compat'
|
|
13
|
+
|
|
14
|
+
import { createComponent } from '../create-component'
|
|
15
|
+
import type { CreateProps } from '../create-props'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Props for the {@link BlockHandleDraggable} component.
|
|
19
|
+
*/
|
|
20
|
+
export interface BlockHandleDraggableProps extends Partial<CreateProps<Props, Events>> {}
|
|
21
|
+
|
|
22
|
+
export const BlockHandleDraggable: ForwardRefExoticComponent<
|
|
23
|
+
Partial<BlockHandleDraggableProps> &
|
|
24
|
+
RefAttributes<BlockHandleDraggableElement> &
|
|
25
|
+
HTMLAttributes<BlockHandleDraggableElement>
|
|
26
|
+
> = createComponent<
|
|
27
|
+
BlockHandleDraggableProps,
|
|
28
|
+
BlockHandleDraggableElement
|
|
29
|
+
>(
|
|
30
|
+
'prosekit-block-handle-draggable',
|
|
31
|
+
'BlockHandleDraggable',
|
|
32
|
+
Object.keys(blockHandleDraggableProps),
|
|
33
|
+
Object.keys(blockHandleDraggableEvents),
|
|
34
|
+
)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BlockHandlePopoverElement,
|
|
3
|
+
type BlockHandlePopoverProps as Props,
|
|
4
|
+
type BlockHandlePopoverEvents as Events,
|
|
5
|
+
blockHandlePopoverProps,
|
|
6
|
+
blockHandlePopoverEvents,
|
|
7
|
+
} from '@prosekit/web/block-handle'
|
|
8
|
+
import type {
|
|
9
|
+
ForwardRefExoticComponent,
|
|
10
|
+
HTMLAttributes,
|
|
11
|
+
RefAttributes,
|
|
12
|
+
} from 'preact/compat'
|
|
13
|
+
|
|
14
|
+
import { createComponent } from '../create-component'
|
|
15
|
+
import type { CreateProps } from '../create-props'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Props for the {@link BlockHandlePopover} component.
|
|
19
|
+
*/
|
|
20
|
+
export interface BlockHandlePopoverProps extends Partial<CreateProps<Props, Events>> {}
|
|
21
|
+
|
|
22
|
+
export const BlockHandlePopover: ForwardRefExoticComponent<
|
|
23
|
+
Partial<BlockHandlePopoverProps> &
|
|
24
|
+
RefAttributes<BlockHandlePopoverElement> &
|
|
25
|
+
HTMLAttributes<BlockHandlePopoverElement>
|
|
26
|
+
> = createComponent<
|
|
27
|
+
BlockHandlePopoverProps,
|
|
28
|
+
BlockHandlePopoverElement
|
|
29
|
+
>(
|
|
30
|
+
'prosekit-block-handle-popover',
|
|
31
|
+
'BlockHandlePopover',
|
|
32
|
+
Object.keys(blockHandlePopoverProps),
|
|
33
|
+
Object.keys(blockHandlePopoverEvents),
|
|
34
|
+
)
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { BlockHandleAdd, type BlockHandleAddProps } from './block-handle-add.gen'
|
|
2
|
+
|
|
3
|
+
export { BlockHandleDraggable, type BlockHandleDraggableProps } from './block-handle-draggable.gen'
|
|
4
|
+
|
|
5
|
+
export { BlockHandlePopover, type BlockHandlePopoverProps } from './block-handle-popover.gen'
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { AnyFunction } from '@prosekit/core'
|
|
2
|
+
import { createElement } from 'preact'
|
|
3
|
+
import {
|
|
4
|
+
forwardRef,
|
|
5
|
+
type ForwardRefExoticComponent,
|
|
6
|
+
type HTMLAttributes,
|
|
7
|
+
type RefAttributes,
|
|
8
|
+
} from 'preact/compat'
|
|
9
|
+
import {
|
|
10
|
+
useEffect,
|
|
11
|
+
useLayoutEffect,
|
|
12
|
+
useMemo,
|
|
13
|
+
useRef,
|
|
14
|
+
useState,
|
|
15
|
+
} from 'preact/hooks'
|
|
16
|
+
|
|
17
|
+
import { useEditorContext } from '../contexts/editor-context'
|
|
18
|
+
|
|
19
|
+
import { mergeRefs } from './merge-refs'
|
|
20
|
+
|
|
21
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect
|
|
22
|
+
|
|
23
|
+
export function createComponent<
|
|
24
|
+
Props extends object,
|
|
25
|
+
CustomElement extends HTMLElement,
|
|
26
|
+
>(
|
|
27
|
+
tagName: string,
|
|
28
|
+
displayName: string,
|
|
29
|
+
propNames: string[],
|
|
30
|
+
eventNames: string[],
|
|
31
|
+
): ForwardRefExoticComponent<
|
|
32
|
+
Partial<Props> & RefAttributes<CustomElement> & HTMLAttributes<CustomElement>
|
|
33
|
+
> {
|
|
34
|
+
const hasEditor = propNames.includes('editor')
|
|
35
|
+
const lowerCaseEventNameMap = new Map(
|
|
36
|
+
eventNames.map((name) => [name.toLowerCase(), name]),
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
const Component = forwardRef<any, any>((props: Props, ref) => {
|
|
40
|
+
const [el, setEl] = useState<HTMLElement | null>(null)
|
|
41
|
+
|
|
42
|
+
const properties: Record<string, unknown> = {}
|
|
43
|
+
const attributes: Record<string, unknown> = {}
|
|
44
|
+
const eventHandlersRef = useRef<Record<string, AnyFunction>>({})
|
|
45
|
+
const eventHandlers: Record<string, AnyFunction> = {}
|
|
46
|
+
|
|
47
|
+
for (const [name, value] of Object.entries(props)) {
|
|
48
|
+
if (value === undefined) {
|
|
49
|
+
continue
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (propNames.includes(name)) {
|
|
53
|
+
properties[name] = value
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (name.startsWith('on')) {
|
|
58
|
+
const lowerCaseEventName = name.slice(2).toLowerCase()
|
|
59
|
+
const eventName = lowerCaseEventNameMap.get(lowerCaseEventName)
|
|
60
|
+
if (eventName) {
|
|
61
|
+
const extractDetail = eventName.endsWith('Change')
|
|
62
|
+
eventHandlers[eventName] = (event: Event) => {
|
|
63
|
+
const handler = value as AnyFunction | null
|
|
64
|
+
if (typeof handler === 'function') {
|
|
65
|
+
handler(extractDetail ? (event as CustomEvent).detail : event)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (name === 'className') {
|
|
73
|
+
attributes['class'] = value
|
|
74
|
+
} else {
|
|
75
|
+
attributes[name] = value
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const editor = useEditorContext()
|
|
80
|
+
|
|
81
|
+
if (hasEditor && editor && !properties['editor']) {
|
|
82
|
+
properties['editor'] = editor
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Set all properties.
|
|
86
|
+
useIsomorphicLayoutEffect(() => {
|
|
87
|
+
if (!el) return
|
|
88
|
+
for (const [name, value] of Object.entries(properties)) {
|
|
89
|
+
if (value !== undefined) {
|
|
90
|
+
// @ts-expect-error: we know that name is a valid property name
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
92
|
+
el[name] = value as unknown as any
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}, [el, ...propNames.map((name) => properties[name])])
|
|
96
|
+
|
|
97
|
+
// Put all event listeners extracted from `props` into `eventHandlersRef`.
|
|
98
|
+
useIsomorphicLayoutEffect(() => {
|
|
99
|
+
eventHandlersRef.current = eventHandlers
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
// Register the event listeners to the element.
|
|
103
|
+
useIsomorphicLayoutEffect(() => {
|
|
104
|
+
if (!el) {
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const fixedEventHandlers: Record<string, AnyFunction> = {}
|
|
109
|
+
|
|
110
|
+
for (const eventName of eventNames) {
|
|
111
|
+
fixedEventHandlers[eventName] = (event: Event) => {
|
|
112
|
+
eventHandlersRef.current[eventName]?.(event)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
for (const [name, handler] of Object.entries(fixedEventHandlers)) {
|
|
117
|
+
el.addEventListener(name, handler)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return () => {
|
|
121
|
+
for (const [name, handler] of Object.entries(fixedEventHandlers)) {
|
|
122
|
+
el.removeEventListener(name, handler)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}, [el])
|
|
126
|
+
|
|
127
|
+
const mergedRef = useMemo(() => mergeRefs([ref, setEl]), [ref])
|
|
128
|
+
|
|
129
|
+
return createElement(tagName, { ...attributes, ref: mergedRef })
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
Component.displayName = displayName
|
|
133
|
+
|
|
134
|
+
return Component
|
|
135
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type CreateProps<
|
|
2
|
+
Props extends { [PropName in keyof Props]: unknown },
|
|
3
|
+
Events extends { [EventName in keyof Events]: CustomEvent },
|
|
4
|
+
> = Props & CreateEventProps<Events>
|
|
5
|
+
|
|
6
|
+
type CreateEventProps<
|
|
7
|
+
Events extends { [EventName in keyof Events]: CustomEvent },
|
|
8
|
+
> = {
|
|
9
|
+
[EventName in keyof Events as `on${Capitalize<string & EventName>}`]: (
|
|
10
|
+
event: EventName extends `${string}Change` ? Events[EventName]['detail']
|
|
11
|
+
: Events[EventName],
|
|
12
|
+
) => void
|
|
13
|
+
}
|