File size: 1,040 Bytes
4bcd925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
import { ref } from 'vue'

type ConfirmOptions = {
  title?: string
  message: string
  confirmText?: string
  cancelText?: string
}

export function useConfirmDialog() {
  const open = ref(false)
  const title = ref('确认操作')
  const message = ref('')
  const confirmText = ref('确定')
  const cancelText = ref('取消')
  let resolver: ((value: boolean) => void) | null = null

  const ask = (options: ConfirmOptions) =>
    new Promise<boolean>((resolve) => {
      title.value = options.title || '确认操作'
      message.value = options.message
      confirmText.value = options.confirmText || '确定'
      cancelText.value = options.cancelText || '取消'
      open.value = true
      resolver = resolve
    })

  const confirm = () => {
    open.value = false
    resolver?.(true)
    resolver = null
  }

  const cancel = () => {
    open.value = false
    resolver?.(false)
    resolver = null
  }

  return {
    open,
    title,
    message,
    confirmText,
    cancelText,
    ask,
    confirm,
    cancel,
  }
}