|
| 1 | +import { VerificationPair } from "@service/limit-service/verification/common" |
| 2 | +import verificationProcessor from "@service/limit-service/verification/processor" |
| 3 | +import { LINK_STYLE } from "../modal-style" |
| 4 | +import { t as t_, locale, I18nKey, tN as tN_, I18nResultItem } from "@i18n" |
| 5 | +import { LimitMessage, verificationMessages } from "@i18n/message/app/limit" |
| 6 | + |
| 7 | +const tV = (key: I18nKey<LimitMessage['verification']>, param?: any) => { |
| 8 | + return t_(verificationMessages, { key, param }) |
| 9 | +} |
| 10 | + |
| 11 | +const tVN = (key: I18nKey<LimitMessage['verification']>, param?: any) => { |
| 12 | + return tN_<LimitMessage['verification'], HTMLElement>(verificationMessages, { key, param }) |
| 13 | +} |
| 14 | + |
| 15 | +const generatePromptEle = (text: string): HTMLElement => { |
| 16 | + const b = document.createElement('b') |
| 17 | + b.innerText = text |
| 18 | + const style: Partial<CSSStyleDeclaration> = { |
| 19 | + userSelect: "none", |
| 20 | + } |
| 21 | + Object.assign(b.style || {}, style) |
| 22 | + return b |
| 23 | +} |
| 24 | + |
| 25 | +const ANSWER_CANVAS_FONT_SIZE = 20 |
| 26 | + |
| 27 | +const generatePromptCanvas = (text: string): HTMLCanvasElement => { |
| 28 | + const ele = document.createElement('canvas') |
| 29 | + const ctx = ele.getContext("2d") |
| 30 | + const height = Math.floor(ANSWER_CANVAS_FONT_SIZE * 1.3) |
| 31 | + ele.height = height |
| 32 | + const font = LINK_STYLE.fontFamily |
| 33 | + // Set font to measure width |
| 34 | + ctx.font = font |
| 35 | + const { width } = ctx.measureText(text) |
| 36 | + ele.width = width |
| 37 | + // Need set font again after width changed |
| 38 | + ctx.font = font |
| 39 | + ctx.fillStyle = 'rgb(238, 238, 238)' |
| 40 | + ctx.fillText(text, 0, ANSWER_CANVAS_FONT_SIZE) |
| 41 | + return ele |
| 42 | +} |
| 43 | + |
| 44 | +const CONFIRM_STYLE: Partial<CSSStyleDeclaration> = { |
| 45 | + width: "500px", |
| 46 | + left: "calc(50vw - 250px)", |
| 47 | + top: "200px", |
| 48 | + height: "250px", |
| 49 | + padding: "50px 20px 20px 20px", |
| 50 | + position: "absolute", |
| 51 | + textAlign: "center", |
| 52 | + background: "#111", |
| 53 | + borderRadius: "8px", |
| 54 | +} |
| 55 | + |
| 56 | +const INPUT_STYLE: Partial<CSSStyleDeclaration> = { |
| 57 | + color: "#000", |
| 58 | + padding: "2px 6px", |
| 59 | + borderRadius: "2px", |
| 60 | + marginTop: "20px", |
| 61 | + width: "200px", |
| 62 | + border: "none", |
| 63 | + outline: "none", |
| 64 | +} |
| 65 | + |
| 66 | +const FOOT_STYLE: Partial<CSSStyleDeclaration> = { |
| 67 | + display: "flex", |
| 68 | + marginTop: "20px", |
| 69 | + justifyContent: "center", |
| 70 | +} |
| 71 | + |
| 72 | +const FOOT_BTN_STYLE: Partial<CSSStyleDeclaration> = { |
| 73 | + cursor: "pointer", |
| 74 | + padding: "5px 10px", |
| 75 | + textDecoration: "underline", |
| 76 | +} |
| 77 | + |
| 78 | +const createFooterBtn = (text: string, onClick: () => void): HTMLDivElement => { |
| 79 | + const btn = document.createElement('div') |
| 80 | + btn.innerText = text |
| 81 | + Object.assign(btn.style || {}, FOOT_BTN_STYLE) |
| 82 | + btn.addEventListener('click', onClick) |
| 83 | + return btn |
| 84 | +} |
| 85 | + |
| 86 | +export class DelayConfirm { |
| 87 | + public dom: HTMLDivElement |
| 88 | + private incorrectMessage: string |
| 89 | + private answerValue: string |
| 90 | + private isPassword: boolean = false |
| 91 | + private input: HTMLInputElement |
| 92 | + private visible = false |
| 93 | + private onSuccess: () => void |
| 94 | + private onError: (msg: string) => void |
| 95 | + |
| 96 | + constructor(option: timer.option.DailyLimitOption) { |
| 97 | + const { limitLevel = "nothing", limitPassword, limitVerifyDifficulty } = option || {} |
| 98 | + let tips: I18nResultItem<HTMLElement>[] = [] |
| 99 | + if (limitLevel === "password" && limitPassword) { |
| 100 | + this.answerValue = limitPassword |
| 101 | + this.isPassword = true |
| 102 | + tips.push(tV(msg => msg.pswInputTip)) |
| 103 | + this.incorrectMessage = tV(msg => msg.incorrectPsw) |
| 104 | + } else if (limitLevel === "verification") { |
| 105 | + const pair: VerificationPair = verificationProcessor.generate(limitVerifyDifficulty, locale) |
| 106 | + const { prompt, promptParam, answer } = pair || {} |
| 107 | + this.answerValue = typeof answer === 'function' ? tV(answer) : answer |
| 108 | + this.incorrectMessage = tV(msg => msg.incorrectAnswer) |
| 109 | + if (prompt) { |
| 110 | + const promptTxt = typeof prompt === 'function' |
| 111 | + ? tV(prompt, { ...promptParam, answer: this.answerValue }) |
| 112 | + : prompt |
| 113 | + tips = tVN(msg => msg.inputTip, { prompt: generatePromptEle(promptTxt) }) |
| 114 | + } else { |
| 115 | + const answer: HTMLElement = limitVerifyDifficulty === 'disgusting' |
| 116 | + ? generatePromptCanvas(this.answerValue) |
| 117 | + : generatePromptEle(this.answerValue) |
| 118 | + tips = tVN(msg => msg.inputTip2, { answer }) |
| 119 | + } |
| 120 | + } else { |
| 121 | + return |
| 122 | + } |
| 123 | + this.dom = document.createElement("div") |
| 124 | + Object.assign(this.dom.style || {}, CONFIRM_STYLE) |
| 125 | + this.dom.style.display = 'none' |
| 126 | + // tips |
| 127 | + const tipContainer = document.createElement('div') |
| 128 | + tipContainer.append(...tips) |
| 129 | + this.dom.append(tipContainer) |
| 130 | + this.initInput() |
| 131 | + this.initFooter() |
| 132 | + } |
| 133 | + |
| 134 | + private initInput() { |
| 135 | + this.input = document.createElement('input') |
| 136 | + this.input.addEventListener("keypress", e => e?.key === "Enter" && this.handleConfirmClick()) |
| 137 | + Object.assign(this.input.style || {}, INPUT_STYLE) |
| 138 | + this.isPassword && (this.input.type = 'password') |
| 139 | + this.dom.append(this.input) |
| 140 | + } |
| 141 | + |
| 142 | + private initFooter() { |
| 143 | + const footer = document.createElement('div') |
| 144 | + Object.assign(footer.style || {}, FOOT_STYLE) |
| 145 | + const cancelBtn = createFooterBtn('Cancel', () => this.hide()) |
| 146 | + const confirmBtn = createFooterBtn('Confirm', () => this.handleConfirmClick()) |
| 147 | + footer.append(confirmBtn) |
| 148 | + footer.append(cancelBtn) |
| 149 | + this.dom.append(footer) |
| 150 | + } |
| 151 | + |
| 152 | + private show() { |
| 153 | + if (this.visible) return |
| 154 | + this.visible = true |
| 155 | + this.dom?.style && (this.dom.style.display = "inherit") |
| 156 | + this.input?.focus?.() |
| 157 | + } |
| 158 | + |
| 159 | + private hide() { |
| 160 | + if (!this.visible) return |
| 161 | + this.visible = false |
| 162 | + this.onSuccess = undefined |
| 163 | + this.onError = undefined |
| 164 | + this.dom?.style && (this.dom.style.display = "none") |
| 165 | + } |
| 166 | + |
| 167 | + private handleConfirmClick() { |
| 168 | + const inputValue = this.input.value |
| 169 | + if (inputValue === this.answerValue) { |
| 170 | + this.onSuccess?.() |
| 171 | + } else { |
| 172 | + this.onError?.(this.incorrectMessage) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + async doConfirm(): Promise<void> { |
| 177 | + if (!this.dom) return Promise.resolve() |
| 178 | + return new Promise<void>((resolve) => { |
| 179 | + this.onSuccess = resolve |
| 180 | + this.onError = msg => alert(msg) |
| 181 | + this.show() |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
| 185 | + |
0 commit comments