@prosekit/vue 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.
Files changed (92) hide show
  1. package/dist/{create-component-wOxhYs6n.js → create-component-C3NIgDxm.js} +3 -3
  2. package/dist/create-component-C3NIgDxm.js.map +1 -0
  3. package/dist/{create-emits-QdHrV9Ip.d.ts → create-emits-QrQextXf.d.ts} +3 -2
  4. package/dist/create-emits-QrQextXf.d.ts.map +1 -0
  5. package/dist/editor-context-DKYvJpUt.js +2 -1
  6. package/dist/editor-context-DKYvJpUt.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 +3 -2
  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 +3 -2
  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 +3 -2
  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 +3 -2
  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 +3 -2
  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 +3 -2
  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 +3 -2
  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 +3 -2
  38. package/dist/prosekit-vue-tooltip.js.map +1 -0
  39. package/dist/prosekit-vue.d.ts +70 -64
  40. package/dist/prosekit-vue.d.ts.map +1 -0
  41. package/dist/prosekit-vue.js +2 -1
  42. package/dist/prosekit-vue.js.map +1 -0
  43. package/package.json +19 -18
  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-extension.ts +34 -0
  85. package/src/hooks/use-editor.ts +57 -0
  86. package/src/hooks/use-extension.spec.ts +90 -0
  87. package/src/hooks/use-extension.ts +41 -0
  88. package/src/hooks/use-keymap.ts +22 -0
  89. package/src/hooks/use-priority-extension.ts +24 -0
  90. package/src/hooks/use-state-update.ts +20 -0
  91. package/src/index.ts +24 -0
  92. package/src/injection/editor-context.ts +25 -0
@@ -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,24 @@
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
+ useExtension,
21
+ type UseExtensionOptions,
22
+ } from './hooks/use-extension'
23
+ export { useKeymap } from './hooks/use-keymap'
24
+ 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
+ }