Blame view

天文台pc/tianwentai-ui/node_modules/react-dnd/src/hooks/useDrag/useRegisteredDragSource.ts 1.11 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  import type { SourceConnector } from '../../internals/index.js'
  import { registerSource } from '../../internals/index.js'
  import type { DragSourceMonitor } from '../../types/index.js'
  import type { DragSourceHookSpec } from '../types.js'
  import { useDragDropManager } from '../useDragDropManager.js'
  import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect.js'
  import { useDragSource } from './useDragSource.js'
  import { useDragType } from './useDragType.js'
  
  export function useRegisteredDragSource<O, R, P>(
  	spec: DragSourceHookSpec<O, R, P>,
  	monitor: DragSourceMonitor<O, R>,
  	connector: SourceConnector,
  ): void {
  	const manager = useDragDropManager()
  	const handler = useDragSource(spec, monitor, connector)
  	const itemType = useDragType(spec)
  
  	useIsomorphicLayoutEffect(
  		function registerDragSource() {
  			if (itemType != null) {
  				const [handlerId, unregister] = registerSource(
  					itemType,
  					handler,
  					manager,
  				)
  				monitor.receiveHandlerId(handlerId)
  				connector.receiveHandlerId(handlerId)
  				return unregister
  			}
  			return
  		},
  		[manager, monitor, connector, handler, itemType],
  	)
  }