Files
JayuCloud/JC-ArtList-Frontend/src/composables/useConfirmEnter.ts
T

232 lines
8.0 KiB
TypeScript

/**
* Purpose: ComputedRef supports useConfirmEnter.ts by holding or executing the computed ref page or API role.
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
*/
import { computed, onBeforeUnmount, onMounted, type ComputedRef } from 'vue'
/**
* Purpose: UseConfirmEnterOptions supports useConfirmEnter.ts by holding or executing the use confirm enter options page or API role.
* Usage: import, call, read, or update this interface from the owning page/module while preserving the surrounding loading and permission flow.
*/
interface UseConfirmEnterOptions {
enabled: ComputedRef<boolean>
onConfirm: (event: KeyboardEvent) => void
isConfirmDisabled?: () => boolean
isTopLayer?: () => boolean
}
/**
* Purpose: CONFIRM_ENTER_DISABLED_CLASS supports useConfirmEnter.ts by holding or executing the confirm enter disabled class page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const CONFIRM_ENTER_DISABLED_CLASS = 'confirm-enter-disabled'
/**
* Purpose: DESKTOP_MIN_WIDTH supports useConfirmEnter.ts by holding or executing the desktop min width page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const DESKTOP_MIN_WIDTH = 960
/**
* Purpose: isEditableTarget supports useConfirmEnter.ts by holding or executing the is editable target page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
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"]'))
}
/**
* Purpose: hasOpenPickerOrDropdown supports useConfirmEnter.ts by holding or executing the has open picker or dropdown page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
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(',')))
}
/**
* Purpose: isDesktopViewport supports useConfirmEnter.ts by holding or executing the is desktop viewport page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const isDesktopViewport = () => {
if (typeof window === 'undefined') {
return false
}
const coarsePointer = window.matchMedia?.('(pointer: coarse)').matches ?? false
return window.innerWidth > DESKTOP_MIN_WIDTH && !coarsePointer
}
/**
* Purpose: isVisibleElement supports useConfirmEnter.ts by holding or executing the is visible element page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
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
}
/**
* Purpose: isDisabledButton supports useConfirmEnter.ts by holding or executing the is disabled button page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const isDisabledButton = (button: HTMLButtonElement) => {
return button.disabled
|| button.getAttribute('aria-disabled') === 'true'
|| button.classList.contains('ant-btn-loading')
|| button.classList.contains('ant-btn-disabled')
}
/**
* Purpose: getTopModalWrap supports useConfirmEnter.ts by holding or executing the get top modal wrap page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
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
}
/**
* Purpose: getTopModalConfirmButton supports useConfirmEnter.ts by holding or executing the get top modal confirm button page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
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
}
/**
* Purpose: useConfirmEnter supports useConfirmEnter.ts by holding or executing the use confirm enter page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
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)
})
}
/**
* Purpose: useGlobalConfirmEnter supports useConfirmEnter.ts by holding or executing the use global confirm enter page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
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)
})
}