@ithinkdt/page 4.2.2 → 4.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/modal.d.ts +4 -4
- package/src/modal.js +19 -11
- package/src/plugin.js +26 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ithinkdt/page",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "iThinkDT Page",
|
|
@@ -49,9 +49,9 @@
|
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"typescript": "~6.0.3",
|
|
52
|
-
"vite": "^8.2.
|
|
53
|
-
"vue": "^3.5.
|
|
54
|
-
"vue-router": "^5.
|
|
52
|
+
"vite": "^8.2.2",
|
|
53
|
+
"vue": "^3.5.42",
|
|
54
|
+
"vue-router": "^5.3.0"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"release": "pnpm publish --no-git-checks"
|
package/src/modal.d.ts
CHANGED
|
@@ -3,16 +3,16 @@ import { CSSProperties, ComputedRef, DefineComponent, MaybeRef, VNodeChild } fro
|
|
|
3
3
|
import { MaybePromise } from '@ithinkdt/common'
|
|
4
4
|
|
|
5
5
|
export interface ModalOptionsBase {
|
|
6
|
-
title?: VNodeChild | (() => VNodeChild)
|
|
7
|
-
content: VNodeChild | (() => VNodeChild)
|
|
6
|
+
title?: VNodeChild | ((params: Omit<ModalRenderParam, 'title' | 'content' | 'footer' | 'footerExtra'>) => VNodeChild)
|
|
7
|
+
content: VNodeChild | ((params: Omit<ModalRenderParam, 'content'>) => VNodeChild)
|
|
8
8
|
confirmText?: VNodeChild | (() => VNodeChild)
|
|
9
9
|
confirmDisabled?: MaybeRef<boolean | undefined>
|
|
10
10
|
confirmLoading?: MaybeRef<boolean | undefined>
|
|
11
11
|
cancelText?: VNodeChild | (() => VNodeChild)
|
|
12
12
|
cancelDisabled?: MaybeRef<boolean | undefined>
|
|
13
13
|
cancelLoading?: MaybeRef<boolean | undefined>
|
|
14
|
-
footer?: VNodeChild | ((params: ModalRenderParam) => VNodeChild)
|
|
15
|
-
footerExtra?: VNodeChild | ((params: ModalRenderParam) => VNodeChild)
|
|
14
|
+
footer?: VNodeChild | ((params: Omit<ModalRenderParam, 'content' | 'footer' | 'footerExtra'>) => VNodeChild)
|
|
15
|
+
footerExtra?: VNodeChild | ((params: Omit<ModalRenderParam, 'content' | 'footer' | 'footerExtra'>) => VNodeChild)
|
|
16
16
|
closable?: MaybeRef<boolean | undefined>
|
|
17
17
|
closeOnEsc?: MaybeRef<boolean | undefined>
|
|
18
18
|
showMask?: MaybeRef<boolean | undefined>
|
package/src/modal.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import { tryOnScopeDispose } from '@vueuse/core'
|
|
2
2
|
import { nanoid } from 'nanoid'
|
|
3
|
-
import { computed, defineComponent, h, hasInjectionContext, inject,
|
|
3
|
+
import { computed, defineComponent, h, hasInjectionContext, inject, onActivated, onDeactivated, provide, reactive, ref, renderSlot, toValue, unref, useSlots } from 'vue'
|
|
4
4
|
|
|
5
|
-
import { IgnoreRejectionError, PAGE_INJECTION, pageInjection } from './plugin.js'
|
|
5
|
+
import { IgnoreRejectionError, PAGE_INJECTION, initModals, pageInjection } from './plugin.js'
|
|
6
6
|
|
|
7
|
+
const ModalProviderInjectionKey = Symbol('__MODAL_PROVIDER__')
|
|
7
8
|
export const ModalProvider = defineComponent({
|
|
8
9
|
name: 'ModalProvider',
|
|
9
10
|
setup() {
|
|
10
|
-
const
|
|
11
|
-
|
|
11
|
+
const modalProviderInjection = inject(ModalProviderInjectionKey, () => inject(PAGE_INJECTION, pageInjection), true)
|
|
12
|
+
const { modals, addModal } = initModals()
|
|
13
|
+
provide(ModalProviderInjectionKey, { ...modalProviderInjection, __modals: modals, addModal, __isGlobal: false })
|
|
14
|
+
const slots = useSlots()
|
|
15
|
+
return () => [renderSlot(slots, 'default'), ...(modalProviderInjection.__isGlobal === false ? [] : pageInjection.__modals.value), ...modals.value]
|
|
12
16
|
},
|
|
13
17
|
})
|
|
14
18
|
|
|
15
19
|
const ModalInjectionKey = Symbol('__MODAL__')
|
|
16
20
|
|
|
17
21
|
export function useModal({ content, confirmText, confirmLoading, cancelText, cancelLoading, footer, footerExtra, onConfirm, onCancel, onClose, title, ...options }) {
|
|
18
|
-
const { i18n: useI18n, getModalRenderer, addModal } = (hasInjectionContext() ? inject(
|
|
22
|
+
const { i18n: useI18n, getModalRenderer, addModal } = (hasInjectionContext() ? inject(ModalProviderInjectionKey, pageInjection) : pageInjection) ?? {}
|
|
19
23
|
if (!getModalRenderer) {
|
|
20
24
|
throw new Error('`useModal` not found `getModalRenderer` in injection, please install the page plugin.')
|
|
21
25
|
}
|
|
@@ -64,6 +68,8 @@ export function useModal({ content, confirmText, confirmLoading, cancelText, can
|
|
|
64
68
|
...options,
|
|
65
69
|
confirmLoading: computed(() => unref(confirmLoading) ?? confirming.value),
|
|
66
70
|
cancelLoading: computed(() => unref(cancelLoading) ?? canceling.value),
|
|
71
|
+
confirmText: computed(() => toValue(confirmText ?? (confirmText === null ? null : t('common.confirm.defaultOk')))),
|
|
72
|
+
cancelText: computed(() => toValue(cancelText ?? (cancelText === null ? null : t('common.page.form.cancelText')))),
|
|
67
73
|
visible,
|
|
68
74
|
onAfterOpen: () => {
|
|
69
75
|
options.onAfterOpen?.()
|
|
@@ -115,14 +121,16 @@ export function useModal({ content, confirmText, confirmLoading, cancelText, can
|
|
|
115
121
|
})
|
|
116
122
|
|
|
117
123
|
return () => {
|
|
124
|
+
const title0 = title2.value ?? (typeof title === 'function' ? title(renderParam) : title)
|
|
125
|
+
const footer0 = typeof footer === 'function' ? footer({ ...renderParam, title: title0 }) : footer
|
|
126
|
+
const footerExtra0 = typeof footerExtra === 'function' ? footerExtra({ ...renderParam, title: title0 }) : footerExtra
|
|
127
|
+
const content0 = typeof content === 'function' ? content({ ...renderParam, title: title0, footer: footer0, footerExtra: footerExtra0 }) : content
|
|
118
128
|
return renderModal({
|
|
119
129
|
...renderParam,
|
|
120
|
-
title:
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
confirmText: toValue(confirmText ?? (confirmText === null ? null : t('common.confirm.defaultOk'))),
|
|
125
|
-
cancelText: toValue(cancelText ?? (cancelText === null ? null : t('common.page.form.cancelText'))),
|
|
130
|
+
title: title0,
|
|
131
|
+
footer: footer0,
|
|
132
|
+
footerExtra: footerExtra0,
|
|
133
|
+
content: content0,
|
|
126
134
|
})
|
|
127
135
|
}
|
|
128
136
|
},
|
package/src/plugin.js
CHANGED
|
@@ -7,22 +7,9 @@ export class IgnoreRejectionError extends Error {}
|
|
|
7
7
|
export let pageInjection
|
|
8
8
|
export function plugin(app, options) {
|
|
9
9
|
if (options.getModalRenderer) {
|
|
10
|
-
|
|
11
|
-
options.
|
|
12
|
-
|
|
13
|
-
options.__modals.value = [
|
|
14
|
-
...options.__modals.value,
|
|
15
|
-
modal,
|
|
16
|
-
]
|
|
17
|
-
}
|
|
18
|
-
return () => {
|
|
19
|
-
const index = options.__modals.value.indexOf(modal)
|
|
20
|
-
if (index !== -1) {
|
|
21
|
-
options.__modals.value.splice(index, 1)
|
|
22
|
-
options.__modals.value = [...options.__modals.value] // trigger reactivity
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
10
|
+
const { modals, addModal } = initModals()
|
|
11
|
+
options.__modals = modals
|
|
12
|
+
options.addModal = addModal
|
|
26
13
|
}
|
|
27
14
|
pageInjection = { keyField: 'key', ...options }
|
|
28
15
|
app.provide(PAGE_INJECTION, pageInjection)
|
|
@@ -45,3 +32,26 @@ export const PageProvider = defineComponent(
|
|
|
45
32
|
return () => renderSlot(slots, 'default')
|
|
46
33
|
},
|
|
47
34
|
)
|
|
35
|
+
|
|
36
|
+
export function initModals() {
|
|
37
|
+
const modals = shallowRef([])
|
|
38
|
+
const addModal = (modal) => {
|
|
39
|
+
if (!modals.value.includes(modal)) {
|
|
40
|
+
modals.value = [
|
|
41
|
+
...modals.value,
|
|
42
|
+
modal,
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
return () => {
|
|
46
|
+
const index = modals.value.indexOf(modal)
|
|
47
|
+
if (index !== -1) {
|
|
48
|
+
modals.value.splice(index, 1)
|
|
49
|
+
modals.value = [...modals.value] // trigger reactivity
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
modals,
|
|
55
|
+
addModal,
|
|
56
|
+
}
|
|
57
|
+
}
|