@prosekit/preact 0.5.0 → 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/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 +12 -11
- 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 +124 -34
- package/dist/prosekit-preact.d.ts.map +1 -0
- package/dist/prosekit-preact.js +137 -23
- package/dist/prosekit-preact.js.map +1 -0
- package/package.json +16 -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 +37 -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/extensions/preact-mark-view.ts +93 -0
- package/src/extensions/preact-node-view.ts +93 -0
- package/src/hooks/use-doc-change.ts +24 -0
- package/src/hooks/use-editor-derived-value.ts +81 -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 +32 -0
- package/src/types.ts +16 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EditorNotFoundError,
|
|
3
|
+
type Editor,
|
|
4
|
+
type Extension,
|
|
5
|
+
} from '@prosekit/core'
|
|
6
|
+
import { useEffect } from 'preact/hooks'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export function useEditorExtension(
|
|
12
|
+
editor: Editor | null | undefined,
|
|
13
|
+
extension: Extension | null,
|
|
14
|
+
): void {
|
|
15
|
+
if (!editor) {
|
|
16
|
+
throw new EditorNotFoundError()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (extension) {
|
|
21
|
+
return editor.use(extension)
|
|
22
|
+
}
|
|
23
|
+
}, [editor, extension])
|
|
24
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineMountHandler,
|
|
3
|
+
defineUpdateHandler,
|
|
4
|
+
ProseKitError,
|
|
5
|
+
union,
|
|
6
|
+
type Editor,
|
|
7
|
+
type Extension,
|
|
8
|
+
} from '@prosekit/core'
|
|
9
|
+
import {
|
|
10
|
+
useEffect,
|
|
11
|
+
useReducer,
|
|
12
|
+
} from 'preact/hooks'
|
|
13
|
+
|
|
14
|
+
import { useEditorContext } from '../contexts/editor-context'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Retrieves the editor instance from the nearest ProseKit component.
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
export function useEditor<E extends Extension = any>(options?: {
|
|
22
|
+
/**
|
|
23
|
+
* Whether to update the component when the editor is mounted or editor state
|
|
24
|
+
* is updated.
|
|
25
|
+
*
|
|
26
|
+
* @default false
|
|
27
|
+
*/
|
|
28
|
+
update?: boolean
|
|
29
|
+
}): Editor<E> {
|
|
30
|
+
const update = options?.update ?? false
|
|
31
|
+
|
|
32
|
+
const editor = useEditorContext<E>()
|
|
33
|
+
if (!editor) {
|
|
34
|
+
throw new ProseKitError(
|
|
35
|
+
'useEditor must be used within the ProseKit component',
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const forceUpdate = useForceUpdate()
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (update) {
|
|
43
|
+
const extension = union(
|
|
44
|
+
defineMountHandler(forceUpdate),
|
|
45
|
+
defineUpdateHandler(forceUpdate),
|
|
46
|
+
)
|
|
47
|
+
return editor.use(extension)
|
|
48
|
+
}
|
|
49
|
+
}, [editor, update, forceUpdate])
|
|
50
|
+
|
|
51
|
+
return editor
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function useForceUpdate() {
|
|
55
|
+
const [, dispatch] = useReducer((x: number) => x + 1, 0)
|
|
56
|
+
return dispatch
|
|
57
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Editor,
|
|
3
|
+
Extension,
|
|
4
|
+
Priority,
|
|
5
|
+
} from '@prosekit/core'
|
|
6
|
+
|
|
7
|
+
import { useEditorContext } from '../contexts/editor-context'
|
|
8
|
+
|
|
9
|
+
import { useEditorExtension } from './use-editor-extension'
|
|
10
|
+
import { usePriorityExtension } from './use-priority-extension'
|
|
11
|
+
|
|
12
|
+
export interface UseExtensionOptions {
|
|
13
|
+
/**
|
|
14
|
+
* The editor to add the extension to. If not provided, it will use the
|
|
15
|
+
* editor from the nearest `ProseKit` component.
|
|
16
|
+
*/
|
|
17
|
+
editor?: Editor
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Optional priority to add the extension with.
|
|
21
|
+
*/
|
|
22
|
+
priority?: Priority
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Add an extension to the editor.
|
|
27
|
+
*/
|
|
28
|
+
export function useExtension(
|
|
29
|
+
/**
|
|
30
|
+
* The extension to add to the editor. If it changes, the previous
|
|
31
|
+
* extension will be removed and the new one (if not null) will be added.
|
|
32
|
+
*/
|
|
33
|
+
extension: Extension | null,
|
|
34
|
+
options?: UseExtensionOptions,
|
|
35
|
+
): void {
|
|
36
|
+
const editorContext = useEditorContext()
|
|
37
|
+
useEditorExtension(
|
|
38
|
+
options?.editor || editorContext,
|
|
39
|
+
usePriorityExtension(extension, options?.priority),
|
|
40
|
+
)
|
|
41
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineKeymap,
|
|
3
|
+
type Keymap,
|
|
4
|
+
} from '@prosekit/core'
|
|
5
|
+
import { useMemo } from 'preact/hooks'
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
useExtension,
|
|
9
|
+
type UseExtensionOptions,
|
|
10
|
+
} from './use-extension'
|
|
11
|
+
|
|
12
|
+
export function useKeymap(keymap: Keymap, options?: UseExtensionOptions): void {
|
|
13
|
+
const extension = useMemo(() => defineKeymap(keymap), [keymap])
|
|
14
|
+
useExtension(extension, options)
|
|
15
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
withPriority,
|
|
3
|
+
type Extension,
|
|
4
|
+
type Priority,
|
|
5
|
+
} from '@prosekit/core'
|
|
6
|
+
import { useMemo } from 'preact/hooks'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export function usePriorityExtension<T extends Extension = Extension>(
|
|
12
|
+
extension: T | null,
|
|
13
|
+
priority?: Priority | null,
|
|
14
|
+
): T | null {
|
|
15
|
+
return useMemo(() => {
|
|
16
|
+
return extension && priority ? withPriority(extension, priority) : extension
|
|
17
|
+
}, [extension, priority])
|
|
18
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defineUpdateHandler } from '@prosekit/core'
|
|
2
|
+
import type { EditorState } from '@prosekit/pm/state'
|
|
3
|
+
import { useMemo } from 'preact/hooks'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
useExtension,
|
|
7
|
+
type UseExtensionOptions,
|
|
8
|
+
} from './use-extension'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Calls the given handler whenever the editor state changes.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export function useStateUpdate(
|
|
16
|
+
handler: (state: EditorState) => void,
|
|
17
|
+
options?: UseExtensionOptions,
|
|
18
|
+
): void {
|
|
19
|
+
const extension = useMemo(
|
|
20
|
+
() => defineUpdateHandler((view) => handler(view.state)),
|
|
21
|
+
[handler],
|
|
22
|
+
)
|
|
23
|
+
useExtension(extension, options)
|
|
24
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export {
|
|
2
|
+
ProseKit,
|
|
3
|
+
type ProseKitProps,
|
|
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'
|
|
17
|
+
export { useDocChange } from './hooks/use-doc-change'
|
|
18
|
+
export { useEditor } from './hooks/use-editor'
|
|
19
|
+
export {
|
|
20
|
+
useEditorDerivedValue,
|
|
21
|
+
type UseEditorDerivedOptions,
|
|
22
|
+
} from './hooks/use-editor-derived-value'
|
|
23
|
+
export {
|
|
24
|
+
useExtension,
|
|
25
|
+
type UseExtensionOptions,
|
|
26
|
+
} from './hooks/use-extension'
|
|
27
|
+
export { useKeymap } from './hooks/use-keymap'
|
|
28
|
+
export { useStateUpdate } from './hooks/use-state-update'
|
|
29
|
+
export type {
|
|
30
|
+
PropsWithChildren,
|
|
31
|
+
PropsWithClass,
|
|
32
|
+
} from './types'
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ComponentChildren } from 'preact'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export type PropsWithClass<P = unknown> = P & {
|
|
7
|
+
class?: string | undefined
|
|
8
|
+
className?: string | undefined
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export type PropsWithChildren<P = unknown> = P & {
|
|
15
|
+
children?: ComponentChildren | undefined
|
|
16
|
+
}
|