@@ -4,9 +4,11 @@ import {
44 DragHandlePlugin ,
55 dragHandlePluginDefaultKey ,
66} from '@tiptap/extension-drag-handle'
7+ import type { Node } from '@tiptap/pm/model'
8+ import type { Plugin , PluginKey } from '@tiptap/pm/state'
79import type { Editor } from '@tiptap/vue-3'
810import type { PropType } from 'vue'
9- import { defineComponent , h , onBeforeUnmount , onMounted , ref } from 'vue'
11+ import { defineComponent , h , nextTick , onBeforeUnmount , onMounted , ref , shallowRef } from 'vue'
1012
1113type Optional < T , K extends keyof T > = Pick < Partial < T > , K > & Omit < T , K >
1214
@@ -20,7 +22,7 @@ export const DragHandle = defineComponent({
2022
2123 props : {
2224 pluginKey : {
23- type : [ String , Object ] as PropType < DragHandleProps [ 'pluginKey' ] > ,
25+ type : [ String , Object ] as PropType < PluginKey | string > ,
2426 default : dragHandlePluginDefaultKey ,
2527 } ,
2628
@@ -39,6 +41,16 @@ export const DragHandle = defineComponent({
3941 default : null ,
4042 } ,
4143
44+ onElementDragStart : {
45+ type : Function as PropType < DragHandleProps [ 'onElementDragStart' ] > ,
46+ default : null ,
47+ } ,
48+
49+ onElementDragEnd : {
50+ type : Function as PropType < DragHandleProps [ 'onElementDragEnd' ] > ,
51+ default : null ,
52+ } ,
53+
4254 class : {
4355 type : String as PropType < DragHandleProps [ 'class' ] > ,
4456 default : 'drag-handle' ,
@@ -47,25 +59,45 @@ export const DragHandle = defineComponent({
4759
4860 setup ( props , { slots } ) {
4961 const root = ref < HTMLElement | null > ( null )
62+ const pluginHandle = shallowRef < { plugin : Plugin ; unbind : ( ) => void } | null > ( null )
5063
51- onMounted ( ( ) => {
52- const { editor, pluginKey, onNodeChange, computePositionConfig } = props
53-
54- editor . registerPlugin (
55- DragHandlePlugin ( {
56- editor,
57- element : root . value as HTMLElement ,
58- pluginKey,
59- computePositionConfig : { ...defaultComputePositionConfig , ...computePositionConfig } ,
60- onNodeChange,
61- } ) . plugin ,
62- )
64+ onMounted ( async ( ) => {
65+ await nextTick ( )
66+
67+ const { editor, pluginKey, onNodeChange, onElementDragEnd, onElementDragStart, computePositionConfig } = props
68+
69+ if ( ! root . value ) {
70+ return
71+ }
72+ if ( ! props . editor || props . editor . isDestroyed ) {
73+ return
74+ }
75+
76+ const init = DragHandlePlugin ( {
77+ editor,
78+ element : root . value ,
79+ pluginKey,
80+ computePositionConfig : { ...defaultComputePositionConfig , ...computePositionConfig } ,
81+ onNodeChange,
82+ onElementDragStart,
83+ onElementDragEnd,
84+ } )
85+
86+ pluginHandle . value = init
87+ props . editor . registerPlugin ( init . plugin )
6388 } )
6489
6590 onBeforeUnmount ( ( ) => {
66- const { pluginKey, editor } = props
91+ if ( ! pluginHandle . value ) {
92+ return
93+ }
94+
95+ if ( props . editor && ! props . editor . isDestroyed ) {
96+ props . editor . unregisterPlugin ( props . pluginKey )
97+ }
6798
68- editor . unregisterPlugin ( pluginKey as string )
99+ pluginHandle . value . unbind ?.( )
100+ pluginHandle . value = null
69101 } )
70102
71103 return ( ) =>
0 commit comments