功能:完善订单流程和加固
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
import { computed, onBeforeUnmount, onMounted, type ComputedRef } from 'vue'
|
||||
|
||||
interface UseConfirmEnterOptions {
|
||||
enabled: ComputedRef<boolean>
|
||||
onConfirm: (event: KeyboardEvent) => void
|
||||
isConfirmDisabled?: () => boolean
|
||||
isTopLayer?: () => boolean
|
||||
}
|
||||
|
||||
export const CONFIRM_ENTER_DISABLED_CLASS = 'confirm-enter-disabled'
|
||||
const DESKTOP_MIN_WIDTH = 960
|
||||
|
||||
const isEditableTarget = (target: EventTarget | null) => {
|
||||
if (!(target instanceof HTMLElement)) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (target.isContentEditable) {
|
||||
return true
|
||||
}
|
||||
|
||||
return target.tagName.toLowerCase() === 'textarea' || Boolean(target.closest('[contenteditable="true"]'))
|
||||
}
|
||||
|
||||
const hasOpenPickerOrDropdown = () => {
|
||||
return Boolean(document.querySelector([
|
||||
'.ant-select-dropdown:not(.ant-select-dropdown-hidden)',
|
||||
'.ant-picker-dropdown:not(.ant-picker-dropdown-hidden)',
|
||||
'.ant-cascader-dropdown:not(.ant-cascader-dropdown-hidden)',
|
||||
'.ant-dropdown:not(.ant-dropdown-hidden)',
|
||||
'.ant-popover:not(.ant-popover-hidden)'
|
||||
].join(',')))
|
||||
}
|
||||
|
||||
const isDesktopViewport = () => {
|
||||
if (typeof window === 'undefined') {
|
||||
return false
|
||||
}
|
||||
|
||||
const coarsePointer = window.matchMedia?.('(pointer: coarse)').matches ?? false
|
||||
return window.innerWidth > DESKTOP_MIN_WIDTH && !coarsePointer
|
||||
}
|
||||
|
||||
const isVisibleElement = (element: Element) => {
|
||||
if (!(element instanceof HTMLElement)) {
|
||||
return false
|
||||
}
|
||||
|
||||
const style = window.getComputedStyle(element)
|
||||
return style.display !== 'none'
|
||||
&& style.visibility !== 'hidden'
|
||||
&& style.opacity !== '0'
|
||||
&& element.getClientRects().length > 0
|
||||
}
|
||||
|
||||
const isDisabledButton = (button: HTMLButtonElement) => {
|
||||
return button.disabled
|
||||
|| button.getAttribute('aria-disabled') === 'true'
|
||||
|| button.classList.contains('ant-btn-loading')
|
||||
|| button.classList.contains('ant-btn-disabled')
|
||||
}
|
||||
|
||||
const getTopModalWrap = () => {
|
||||
const modals = Array.from(document.querySelectorAll<HTMLElement>('.ant-modal-wrap'))
|
||||
.filter(isVisibleElement)
|
||||
|
||||
if (!modals.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
const sortedModals = modals
|
||||
.map((modal, index) => {
|
||||
const zIndex = Number.parseInt(window.getComputedStyle(modal).zIndex || '0', 10)
|
||||
return {
|
||||
modal,
|
||||
index,
|
||||
zIndex: Number.isNaN(zIndex) ? 0 : zIndex
|
||||
}
|
||||
})
|
||||
.sort((left, right) => left.zIndex - right.zIndex || left.index - right.index)
|
||||
|
||||
return sortedModals[sortedModals.length - 1]?.modal ?? null
|
||||
}
|
||||
|
||||
const getTopModalConfirmButton = () => {
|
||||
const topModal = getTopModalWrap()
|
||||
if (!topModal || topModal.classList.contains(CONFIRM_ENTER_DISABLED_CLASS)) {
|
||||
return null
|
||||
}
|
||||
|
||||
const button = topModal.querySelector<HTMLButtonElement>([
|
||||
'.ant-modal-confirm-btns .ant-btn-primary',
|
||||
'.ant-modal-footer .ant-btn-primary'
|
||||
].join(','))
|
||||
|
||||
if (!button || !isVisibleElement(button) || isDisabledButton(button)) {
|
||||
return null
|
||||
}
|
||||
|
||||
return button
|
||||
}
|
||||
|
||||
export const useConfirmEnter = ({
|
||||
enabled,
|
||||
onConfirm,
|
||||
isConfirmDisabled,
|
||||
isTopLayer
|
||||
}: UseConfirmEnterOptions) => {
|
||||
const canConfirm = computed(() => (
|
||||
enabled.value
|
||||
&& !(isConfirmDisabled?.() ?? false)
|
||||
&& (isTopLayer?.() ?? true)
|
||||
))
|
||||
|
||||
const handleKeydown = (event: KeyboardEvent) => {
|
||||
if (
|
||||
event.key !== 'Enter'
|
||||
|| event.defaultPrevented
|
||||
|| event.shiftKey
|
||||
|| event.ctrlKey
|
||||
|| event.altKey
|
||||
|| event.metaKey
|
||||
|| event.isComposing
|
||||
|| !canConfirm.value
|
||||
|| isEditableTarget(event.target)
|
||||
|| hasOpenPickerOrDropdown()
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
onConfirm(event)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
document.addEventListener('keydown', handleKeydown)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
document.removeEventListener('keydown', handleKeydown)
|
||||
})
|
||||
}
|
||||
|
||||
export const useGlobalConfirmEnter = () => {
|
||||
const handleKeydown = (event: KeyboardEvent) => {
|
||||
if (
|
||||
event.key !== 'Enter'
|
||||
|| event.defaultPrevented
|
||||
|| event.shiftKey
|
||||
|| event.ctrlKey
|
||||
|| event.altKey
|
||||
|| event.metaKey
|
||||
|| event.isComposing
|
||||
|| (event as KeyboardEvent & { keyCode?: number }).keyCode === 229
|
||||
|| !isDesktopViewport()
|
||||
|| isEditableTarget(event.target)
|
||||
|| hasOpenPickerOrDropdown()
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
const confirmButton = getTopModalConfirmButton()
|
||||
if (!confirmButton) {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
event.stopImmediatePropagation()
|
||||
confirmButton.click()
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
document.addEventListener('keydown', handleKeydown)
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
document.removeEventListener('keydown', handleKeydown)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user