Files
ReceiptGenerator/src/renderer/confirmations.ts
T

24 lines
1.0 KiB
TypeScript

import { shallowRef } from 'vue'
import type { ConfirmationOptions } from '../shared/types'
export const confirmation=shallowRef<ConfirmationOptions|null>(null)
let resolvePending:((confirmed:boolean)=>void)|null=null
let previousFocus:HTMLElement|null=null
export function askConfirmation(options:ConfirmationOptions):Promise<boolean>{
if(resolvePending)return Promise.resolve(false)
previousFocus=document.activeElement instanceof HTMLElement?document.activeElement:null
confirmation.value=options
return new Promise(resolve=>{resolvePending=resolve})
}
export function answerConfirmation(confirmed:boolean):void{
const resolve=resolvePending
resolvePending=null;confirmation.value=null
resolve?.(confirmed)
}
export function restoreConfirmationFocus(event:Event):void{
event.preventDefault()
if(previousFocus?.isConnected&&previousFocus!==document.body&&!previousFocus.matches(':disabled'))previousFocus.focus({preventScroll:true})
else document.querySelector<HTMLButtonElement>('.nav-item.active')?.focus({preventScroll:true})
previousFocus=null
}