@prosekit/vue 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.
Files changed (93) hide show
  1. package/dist/{create-component-wOxhYs6n.js → create-component-BREKWlWW.js} +4 -4
  2. package/dist/create-component-BREKWlWW.js.map +1 -0
  3. package/dist/{create-emits-QdHrV9Ip.d.ts → create-emits-B8725q2G.d.ts} +3 -2
  4. package/dist/create-emits-B8725q2G.d.ts.map +1 -0
  5. package/dist/{editor-context-DKYvJpUt.js → editor-context-BfHHplRP.js} +2 -1
  6. package/dist/editor-context-BfHHplRP.js.map +1 -0
  7. package/dist/prosekit-vue-autocomplete.d.ts +19 -18
  8. package/dist/prosekit-vue-autocomplete.d.ts.map +1 -0
  9. package/dist/prosekit-vue-autocomplete.js +4 -3
  10. package/dist/prosekit-vue-autocomplete.js.map +1 -0
  11. package/dist/prosekit-vue-block-handle.d.ts +15 -14
  12. package/dist/prosekit-vue-block-handle.d.ts.map +1 -0
  13. package/dist/prosekit-vue-block-handle.js +4 -3
  14. package/dist/prosekit-vue-block-handle.js.map +1 -0
  15. package/dist/prosekit-vue-drop-indicator.d.ts +7 -6
  16. package/dist/prosekit-vue-drop-indicator.d.ts.map +1 -0
  17. package/dist/prosekit-vue-drop-indicator.js +4 -3
  18. package/dist/prosekit-vue-drop-indicator.js.map +1 -0
  19. package/dist/prosekit-vue-inline-popover.d.ts +7 -6
  20. package/dist/prosekit-vue-inline-popover.d.ts.map +1 -0
  21. package/dist/prosekit-vue-inline-popover.js +4 -3
  22. package/dist/prosekit-vue-inline-popover.js.map +1 -0
  23. package/dist/prosekit-vue-popover.d.ts +15 -14
  24. package/dist/prosekit-vue-popover.d.ts.map +1 -0
  25. package/dist/prosekit-vue-popover.js +4 -3
  26. package/dist/prosekit-vue-popover.js.map +1 -0
  27. package/dist/prosekit-vue-resizable.d.ts +11 -10
  28. package/dist/prosekit-vue-resizable.d.ts.map +1 -0
  29. package/dist/prosekit-vue-resizable.js +4 -3
  30. package/dist/prosekit-vue-resizable.js.map +1 -0
  31. package/dist/prosekit-vue-table-handle.d.ts +39 -38
  32. package/dist/prosekit-vue-table-handle.d.ts.map +1 -0
  33. package/dist/prosekit-vue-table-handle.js +4 -3
  34. package/dist/prosekit-vue-table-handle.js.map +1 -0
  35. package/dist/prosekit-vue-tooltip.d.ts +15 -14
  36. package/dist/prosekit-vue-tooltip.d.ts.map +1 -0
  37. package/dist/prosekit-vue-tooltip.js +4 -3
  38. package/dist/prosekit-vue-tooltip.js.map +1 -0
  39. package/dist/prosekit-vue.d.ts +95 -68
  40. package/dist/prosekit-vue.d.ts.map +1 -0
  41. package/dist/prosekit-vue.js +39 -26
  42. package/dist/prosekit-vue.js.map +1 -0
  43. package/package.json +18 -17
  44. package/src/components/autocomplete/autocomplete-empty.gen.ts +33 -0
  45. package/src/components/autocomplete/autocomplete-item.gen.ts +33 -0
  46. package/src/components/autocomplete/autocomplete-list.gen.ts +33 -0
  47. package/src/components/autocomplete/autocomplete-popover.gen.ts +33 -0
  48. package/src/components/autocomplete/index.gen.ts +7 -0
  49. package/src/components/block-handle/block-handle-add.gen.ts +33 -0
  50. package/src/components/block-handle/block-handle-draggable.gen.ts +33 -0
  51. package/src/components/block-handle/block-handle-popover.gen.ts +33 -0
  52. package/src/components/block-handle/index.gen.ts +5 -0
  53. package/src/components/create-component.ts +93 -0
  54. package/src/components/create-emits.ts +8 -0
  55. package/src/components/drop-indicator/drop-indicator.gen.ts +33 -0
  56. package/src/components/drop-indicator/index.gen.ts +1 -0
  57. package/src/components/inline-popover/index.gen.ts +1 -0
  58. package/src/components/inline-popover/inline-popover.gen.ts +33 -0
  59. package/src/components/popover/index.gen.ts +5 -0
  60. package/src/components/popover/popover-content.gen.ts +33 -0
  61. package/src/components/popover/popover-root.gen.ts +33 -0
  62. package/src/components/popover/popover-trigger.gen.ts +33 -0
  63. package/src/components/prosekit.ts +36 -0
  64. package/src/components/resizable/index.gen.ts +3 -0
  65. package/src/components/resizable/resizable-handle.gen.ts +33 -0
  66. package/src/components/resizable/resizable-root.gen.ts +33 -0
  67. package/src/components/table-handle/index.gen.ts +17 -0
  68. package/src/components/table-handle/table-handle-column-root.gen.ts +33 -0
  69. package/src/components/table-handle/table-handle-column-trigger.gen.ts +33 -0
  70. package/src/components/table-handle/table-handle-drag-preview.gen.ts +33 -0
  71. package/src/components/table-handle/table-handle-drop-indicator.gen.ts +33 -0
  72. package/src/components/table-handle/table-handle-popover-content.gen.ts +33 -0
  73. package/src/components/table-handle/table-handle-popover-item.gen.ts +33 -0
  74. package/src/components/table-handle/table-handle-root.gen.ts +33 -0
  75. package/src/components/table-handle/table-handle-row-root.gen.ts +33 -0
  76. package/src/components/table-handle/table-handle-row-trigger.gen.ts +33 -0
  77. package/src/components/tooltip/index.gen.ts +5 -0
  78. package/src/components/tooltip/tooltip-content.gen.ts +33 -0
  79. package/src/components/tooltip/tooltip-root.gen.ts +33 -0
  80. package/src/components/tooltip/tooltip-trigger.gen.ts +33 -0
  81. package/src/extensions/vue-mark-view.ts +98 -0
  82. package/src/extensions/vue-node-view.ts +98 -0
  83. package/src/hooks/use-doc-change.ts +20 -0
  84. package/src/hooks/use-editor-derived-value.ts +51 -0
  85. package/src/hooks/use-editor-extension.ts +34 -0
  86. package/src/hooks/use-editor.ts +57 -0
  87. package/src/hooks/use-extension.spec.ts +90 -0
  88. package/src/hooks/use-extension.ts +41 -0
  89. package/src/hooks/use-keymap.ts +22 -0
  90. package/src/hooks/use-priority-extension.ts +24 -0
  91. package/src/hooks/use-state-update.ts +20 -0
  92. package/src/index.ts +28 -0
  93. package/src/injection/editor-context.ts +25 -0
@@ -0,0 +1,98 @@
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 NodeViewFactory,
13
+ type VueNodeViewUserOptions,
14
+ } from '@prosemirror-adapter/vue'
15
+ import {
16
+ computed,
17
+ defineComponent,
18
+ h,
19
+ type DefineComponent,
20
+ } from 'vue'
21
+
22
+ import { useExtension } from '../hooks/use-extension'
23
+
24
+ /**
25
+ * @public
26
+ */
27
+ export interface VueNodeViewProps extends NodeViewContext {}
28
+
29
+ /**
30
+ * @public
31
+ */
32
+ export type VueNodeViewComponent = DefineComponent<VueNodeViewProps, any, any>
33
+
34
+ /**
35
+ * Options for {@link defineVueNodeView}.
36
+ *
37
+ * @public
38
+ */
39
+ export interface VueNodeViewOptions extends CoreNodeViewUserOptions<VueNodeViewComponent> {
40
+ /**
41
+ * The name of the node type.
42
+ */
43
+ name: string
44
+ }
45
+
46
+ function withNodeViewProps(component: VueNodeViewComponent) {
47
+ return defineComponent({
48
+ name: 'NodeViewPropsWrapper',
49
+ setup: () => {
50
+ const props: Readonly<VueNodeViewProps> = useNodeViewContext()
51
+ return () => h(component, props)
52
+ },
53
+ })
54
+ }
55
+
56
+ /**
57
+ * @internal
58
+ */
59
+ export const VueNodeViewsConsumer: DefineComponent = /* @__PURE__ */ defineComponent({
60
+ name: 'VueNodeViewsConsumer',
61
+ setup: () => {
62
+ const nodeViewFactory: NodeViewFactory = useNodeViewFactory()
63
+ const extension = computed(() => {
64
+ return defineVueNodeViewFactory(nodeViewFactory)
65
+ })
66
+ useExtension(extension)
67
+ return (): null => null
68
+ },
69
+ })
70
+
71
+ /**
72
+ * Defines a node view using a Vue component.
73
+ *
74
+ * @public
75
+ */
76
+ export function defineVueNodeView(options: VueNodeViewOptions): Extension {
77
+ const { name, component, ...userOptions } = options
78
+
79
+ const args: VueNodeViewUserOptions = {
80
+ ...userOptions,
81
+ component: withNodeViewProps(component),
82
+ }
83
+
84
+ return defineNodeViewComponent<VueNodeViewUserOptions>({
85
+ group: 'vue',
86
+ name,
87
+ args,
88
+ })
89
+ }
90
+
91
+ function defineVueNodeViewFactory(
92
+ factory: (options: VueNodeViewUserOptions) => NodeViewConstructor,
93
+ ) {
94
+ return defineNodeViewFactory<VueNodeViewUserOptions>({
95
+ group: 'vue',
96
+ factory,
97
+ })
98
+ }
@@ -0,0 +1,20 @@
1
+ import { defineDocChangeHandler } from '@prosekit/core'
2
+ import type { ProseMirrorNode } from '@prosekit/pm/model'
3
+
4
+ import {
5
+ useExtension,
6
+ type UseExtensionOptions,
7
+ } from './use-extension'
8
+
9
+ /**
10
+ * Calls the given handler whenever the editor document changes.
11
+ *
12
+ * @public
13
+ */
14
+ export function useDocChange(
15
+ handler: (doc: ProseMirrorNode) => void,
16
+ options?: UseExtensionOptions,
17
+ ): void {
18
+ const extension = defineDocChangeHandler((view) => handler(view.state.doc))
19
+ useExtension(extension, options)
20
+ }
@@ -0,0 +1,51 @@
1
+ import type {
2
+ Editor,
3
+ Extension,
4
+ } from '@prosekit/core'
5
+ import {
6
+ computed,
7
+ toValue,
8
+ type MaybeRefOrGetter,
9
+ type Ref,
10
+ type ShallowRef,
11
+ } from 'vue'
12
+
13
+ import { useEditor } from './use-editor'
14
+
15
+ export interface UseEditorDerivedOptions<E extends Extension = any> {
16
+ /**
17
+ * The editor to add the extension to. If not provided, it will use the
18
+ * editor from the nearest `<ProseKit>` component.
19
+ */
20
+ editor?: MaybeRefOrGetter<Editor<E>>
21
+ }
22
+
23
+ /**
24
+ * Runs a function to derive a value from the editor instance after editor state
25
+ * changes.
26
+ *
27
+ * This is useful when you need to render something based on the editor state,
28
+ * for example, whether the selected text is wrapped in an italic mark.
29
+ *
30
+ * It returns a shallow ref of the derived value that updates whenever the editor
31
+ * state changes.
32
+ *
33
+ * @public
34
+ */
35
+ export function useEditorDerivedValue<E extends Extension, Derived>(
36
+ /**
37
+ * A function that receives the editor instance and returns a derived value.
38
+ *
39
+ * It will be called whenever the editor's document state changes, or when it
40
+ * mounts.
41
+ */
42
+ derive: (editor: Editor<E>) => Derived,
43
+ options?: UseEditorDerivedOptions<E>,
44
+ ): ShallowRef<Derived> {
45
+ const initialEditor = options?.editor
46
+ const editorAccessor: Ref<Editor<E>> = initialEditor
47
+ ? computed(() => toValue(initialEditor))
48
+ : useEditor<E>({ update: true })
49
+
50
+ return computed(() => derive(toValue(editorAccessor)))
51
+ }
@@ -0,0 +1,34 @@
1
+ import {
2
+ EditorNotFoundError,
3
+ type Editor,
4
+ type Extension,
5
+ } from '@prosekit/core'
6
+ import {
7
+ toValue,
8
+ watchPostEffect,
9
+ type MaybeRefOrGetter,
10
+ } from 'vue'
11
+
12
+ import { useEditorContext } from '../injection/editor-context'
13
+
14
+ /**
15
+ * @internal
16
+ */
17
+ export function useEditorExtension(
18
+ editorRef: MaybeRefOrGetter<Editor> | null | undefined,
19
+ extensionRef: MaybeRefOrGetter<Extension | null> | null,
20
+ ): void {
21
+ const editorContext = useEditorContext()
22
+
23
+ watchPostEffect((onCleanup) => {
24
+ const editor = toValue(editorRef) || toValue(editorContext)
25
+ const extension = toValue(extensionRef)
26
+
27
+ if (!editor) {
28
+ throw new EditorNotFoundError()
29
+ }
30
+ if (extension) {
31
+ onCleanup(editor.use(extension))
32
+ }
33
+ })
34
+ }
@@ -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
+ onMounted,
11
+ onUnmounted,
12
+ shallowRef,
13
+ triggerRef,
14
+ type ShallowRef,
15
+ } from 'vue'
16
+
17
+ import { useEditorContext } from '../injection/editor-context'
18
+
19
+ /**
20
+ * Retrieves the editor instance from the nearest ProseKit component.
21
+ *
22
+ * @public
23
+ */
24
+ export function useEditor<E extends Extension = any>(options?: {
25
+ /**
26
+ * Whether to update the component when the editor is mounted or editor state
27
+ * is updated.
28
+ *
29
+ * @default false
30
+ */
31
+ update?: boolean
32
+ }): ShallowRef<Editor<E>> {
33
+ const update = options?.update ?? false
34
+
35
+ const editor = useEditorContext<E>()
36
+ if (!editor) {
37
+ throw new ProseKitError(
38
+ 'useEditor must be used within the ProseKit component',
39
+ )
40
+ }
41
+
42
+ const editorRef = shallowRef<Editor<E>>(editor)
43
+
44
+ if (update) {
45
+ onMounted(() => {
46
+ const forceUpdate = () => triggerRef(editorRef)
47
+ const extension = union(
48
+ defineMountHandler(forceUpdate),
49
+ defineUpdateHandler(forceUpdate),
50
+ )
51
+ const dispose = editor.use(extension)
52
+ onUnmounted(dispose)
53
+ })
54
+ }
55
+
56
+ return editorRef
57
+ }
@@ -0,0 +1,90 @@
1
+ import {
2
+ createEditor,
3
+ defineNodeSpec,
4
+ definePlugin,
5
+ union,
6
+ type defineBaseCommands,
7
+ type Editor,
8
+ } from '@prosekit/core'
9
+ import {
10
+ Plugin,
11
+ PluginKey,
12
+ } from '@prosekit/pm/state'
13
+ import { mount } from '@vue/test-utils'
14
+ import {
15
+ describe,
16
+ expect,
17
+ it,
18
+ vi,
19
+ } from 'vitest'
20
+ import { defineComponent } from 'vue'
21
+
22
+ import { useExtension } from './use-extension'
23
+
24
+ function defineText() {
25
+ return defineNodeSpec({ name: 'text', topNode: true })
26
+ }
27
+
28
+ describe('useExtension', () => {
29
+ it('should register and dispose an extension', async () => {
30
+ const editor = createEditor({
31
+ extension: union(defineText()),
32
+ }) as Editor<ReturnType<typeof defineBaseCommands>>
33
+
34
+ editor.mount(document.createElement('div'))
35
+
36
+ const pluginKey = new PluginKey('test-plugin-key')
37
+ const extension = definePlugin(new Plugin({ key: pluginKey }))
38
+ const hasPlugin = (): boolean => {
39
+ const plugins = editor.state.plugins
40
+ return plugins.some((plugin) => plugin.spec.key === pluginKey)
41
+ }
42
+ const usePlugin = () => useExtension(extension, { editor })
43
+
44
+ const TestComponent = defineComponent({
45
+ setup: () => {
46
+ usePlugin()
47
+ },
48
+ render: () => null,
49
+ })
50
+
51
+ await vi.waitFor(() => expect(hasPlugin()).toBe(false))
52
+ const wrapper = mount(TestComponent)
53
+ await vi.waitFor(() => expect(hasPlugin()).toBe(true))
54
+ wrapper.unmount()
55
+ await vi.waitFor(() => expect(hasPlugin()).toBe(false))
56
+ })
57
+
58
+ it('should not print warning if the editor context is not available', () => {
59
+ const editor = createEditor({
60
+ extension: union(defineText()),
61
+ }) as Editor<ReturnType<typeof defineBaseCommands>>
62
+ editor.mount(document.createElement('div'))
63
+
64
+ const pluginKey = new PluginKey('test-plugin-key')
65
+ const extension = definePlugin(new Plugin({ key: pluginKey }))
66
+
67
+ const TestComponent = defineComponent({
68
+ setup: () => {
69
+ useExtension(extension, { editor })
70
+ },
71
+ render: () => null,
72
+ })
73
+
74
+ const messages: string[] = []
75
+ const warnHandler = vi.fn((message: string) => {
76
+ messages.push(message)
77
+ })
78
+
79
+ const wrapper = mount(TestComponent, {
80
+ global: {
81
+ config: {
82
+ warnHandler,
83
+ },
84
+ },
85
+ })
86
+ wrapper.unmount()
87
+
88
+ expect(messages).toEqual([])
89
+ })
90
+ })
@@ -0,0 +1,41 @@
1
+ import type {
2
+ Editor,
3
+ Extension,
4
+ Priority,
5
+ } from '@prosekit/core'
6
+ import type { MaybeRefOrGetter } from 'vue'
7
+
8
+ import { useEditorExtension } from './use-editor-extension'
9
+ import { usePriorityExtension } from './use-priority-extension'
10
+
11
+ export interface UseExtensionOptions {
12
+ /**
13
+ * The editor to add the extension to. If not provided, it will use the
14
+ * editor from the nearest `<ProseKit>` component.
15
+ */
16
+ editor?: MaybeRefOrGetter<Editor>
17
+
18
+ /**
19
+ * Optional priority to add the extension with.
20
+ */
21
+ priority?: Priority
22
+ }
23
+
24
+ /**
25
+ * Add an extension to the editor.
26
+ *
27
+ * @public
28
+ */
29
+ export function useExtension(
30
+ /**
31
+ * The ref to an extension to add to the editor. If it changes, the previous
32
+ * extension will be removed and the new one (if not null) will be added.
33
+ */
34
+ extension: MaybeRefOrGetter<Extension | null>,
35
+ options?: UseExtensionOptions,
36
+ ): void {
37
+ useEditorExtension(
38
+ options?.editor,
39
+ usePriorityExtension(extension, options?.priority),
40
+ )
41
+ }
@@ -0,0 +1,22 @@
1
+ import {
2
+ defineKeymap,
3
+ type Keymap,
4
+ } from '@prosekit/core'
5
+ import {
6
+ computed,
7
+ toValue,
8
+ type MaybeRefOrGetter,
9
+ } from 'vue'
10
+
11
+ import {
12
+ useExtension,
13
+ type UseExtensionOptions,
14
+ } from './use-extension'
15
+
16
+ export function useKeymap(
17
+ keymap: MaybeRefOrGetter<Keymap>,
18
+ options?: UseExtensionOptions,
19
+ ): void {
20
+ const extension = computed(() => defineKeymap(toValue(keymap)))
21
+ useExtension(extension, options)
22
+ }
@@ -0,0 +1,24 @@
1
+ import {
2
+ withPriority,
3
+ type Extension,
4
+ type Priority,
5
+ } from '@prosekit/core'
6
+ import {
7
+ computed,
8
+ toValue,
9
+ type ComputedRef,
10
+ type MaybeRefOrGetter,
11
+ } from 'vue'
12
+
13
+ /**
14
+ * @internal
15
+ */
16
+ export function usePriorityExtension<T extends Extension = Extension>(
17
+ extension: MaybeRefOrGetter<T | null>,
18
+ priority: Priority | null | undefined,
19
+ ): ComputedRef<T | null> {
20
+ return computed(() => {
21
+ const ext = toValue(extension)
22
+ return ext && priority ? withPriority(ext, priority) : ext
23
+ })
24
+ }
@@ -0,0 +1,20 @@
1
+ import { defineUpdateHandler } from '@prosekit/core'
2
+ import type { EditorState } from '@prosekit/pm/state'
3
+
4
+ import {
5
+ useExtension,
6
+ type UseExtensionOptions,
7
+ } from './use-extension'
8
+
9
+ /**
10
+ * Calls the given handler whenever the editor state changes.
11
+ *
12
+ * @public
13
+ */
14
+ export function useStateUpdate(
15
+ handler: (state: EditorState) => void,
16
+ options?: UseExtensionOptions,
17
+ ): void {
18
+ const extension = defineUpdateHandler((view) => handler(view.state))
19
+ useExtension(extension, options)
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,28 @@
1
+ export {
2
+ ProseKit,
3
+ type ProseKitProps,
4
+ } from './components/prosekit'
5
+ export {
6
+ defineVueMarkView,
7
+ type VueMarkViewComponent,
8
+ type VueMarkViewOptions,
9
+ type VueMarkViewProps,
10
+ } from './extensions/vue-mark-view'
11
+ export {
12
+ defineVueNodeView,
13
+ type VueNodeViewComponent,
14
+ type VueNodeViewOptions,
15
+ type VueNodeViewProps,
16
+ } from './extensions/vue-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'
@@ -0,0 +1,25 @@
1
+ import type {
2
+ Editor,
3
+ Extension,
4
+ } from '@prosekit/core'
5
+ import {
6
+ inject,
7
+ provide,
8
+ type InjectionKey,
9
+ } from 'vue'
10
+
11
+ const symbol = Symbol('prosekit-vue-editor-context') as InjectionKey<Editor>
12
+
13
+ /**
14
+ * @internal
15
+ */
16
+ export function provideEditor(editor: Editor): void {
17
+ provide(symbol, editor)
18
+ }
19
+
20
+ /**
21
+ * @internal
22
+ */
23
+ export function useEditorContext<E extends Extension>(): Editor<E> | undefined {
24
+ return inject(symbol, undefined)
25
+ }