forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfirm.ts
More file actions
63 lines (56 loc) · 1.49 KB
/
Copy pathconfirm.ts
File metadata and controls
63 lines (56 loc) · 1.49 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
import { defineStore } from 'pinia'
interface ConfirmOptions {
title?: string
description?: string
cancelText?: string
confirmText?: string
/** 确认按钮使用红色 destructive 样式 */
destructive?: boolean
/** 确认回调 */
onConfirm?: () => void | Promise<void>
/** 取消回调(可选) */
onCancel?: () => void
}
export const useConfirmStore = defineStore('confirm', () => {
const isOpen = ref(false)
const title = ref('提示')
const description = ref('')
const cancelText = ref('取消')
const confirmText = ref('确定')
const destructive = ref(false)
let _onConfirm: (() => void | Promise<void>) | null = null
let _onCancel: (() => void) | null = null
function confirm(options: ConfirmOptions) {
title.value = options.title ?? '提示'
description.value = options.description ?? ''
cancelText.value = options.cancelText ?? '取消'
confirmText.value = options.confirmText ?? '确定'
destructive.value = options.destructive ?? false
_onConfirm = options.onConfirm ?? null
_onCancel = options.onCancel ?? null
isOpen.value = true
}
function handleConfirm() {
_onConfirm?.()
isOpen.value = false
_onConfirm = null
_onCancel = null
}
function handleCancel() {
_onCancel?.()
isOpen.value = false
_onConfirm = null
_onCancel = null
}
return {
isOpen,
title,
description,
cancelText,
confirmText,
destructive,
confirm,
handleConfirm,
handleCancel,
}
})