Blame view

天文台pc/tianwentai-ui/node_modules/dnd-core/src/contracts.ts 1.2 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  import { invariant } from '@react-dnd/invariant'
  
  import type { DragSource, DropTarget, Identifier } from './interfaces.js'
  
  export function validateSourceContract(source: DragSource): void {
  	invariant(
  		typeof source.canDrag === 'function',
  		'Expected canDrag to be a function.',
  	)
  	invariant(
  		typeof source.beginDrag === 'function',
  		'Expected beginDrag to be a function.',
  	)
  	invariant(
  		typeof source.endDrag === 'function',
  		'Expected endDrag to be a function.',
  	)
  }
  
  export function validateTargetContract(target: DropTarget): void {
  	invariant(
  		typeof target.canDrop === 'function',
  		'Expected canDrop to be a function.',
  	)
  	invariant(
  		typeof target.hover === 'function',
  		'Expected hover to be a function.',
  	)
  	invariant(
  		typeof target.drop === 'function',
  		'Expected beginDrag to be a function.',
  	)
  }
  
  export function validateType(
  	type: Identifier | Identifier[],
  	allowArray?: boolean,
  ): void {
  	if (allowArray && Array.isArray(type)) {
  		type.forEach((t) => validateType(t, false))
  		return
  	}
  
  	invariant(
  		typeof type === 'string' || typeof type === 'symbol',
  		allowArray
  			? 'Type can only be a string, a symbol, or an array of either.'
  			: 'Type can only be a string or a symbol.',
  	)
  }