From 66ac5f7903fb50fad2647aba0646611f63d91b97 Mon Sep 17 00:00:00 2001 From: xiaoxustudio <1783558957@qq.com> Date: Sat, 3 Jan 2026 18:20:02 +0800 Subject: [PATCH 1/3] feat: add rules argument for getUserInput --- .../src/Core/gameScripts/getUserInput/index.tsx | 15 +++++++++++++++ packages/webgal/src/Core/util/global.ts | 7 +++++++ 2 files changed, 22 insertions(+) create mode 100644 packages/webgal/src/Core/util/global.ts diff --git a/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx b/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx index 9793d3300..4a8391987 100644 --- a/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx +++ b/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx @@ -13,6 +13,8 @@ import { getStringArgByKey } from '@/Core/util/getSentenceArg'; import { nextSentence } from '@/Core/controller/gamePlay/nextSentence'; import { setStageVar } from '@/store/stageReducer'; import { getCurrentFontFamily } from '@/hooks/useFontFamily'; +import { logger } from '@/Core/util/logger'; +import { tryToRegex } from '@/Core/util/global'; /** * 显示选择枝 @@ -26,6 +28,9 @@ export const getUserInput = (sentence: ISentence): IPerform => { let buttonText = getStringArgByKey(sentence, 'buttonText') ?? ''; buttonText = buttonText === '' ? 'OK' : buttonText; const defaultValue = getStringArgByKey(sentence, 'defaultValue'); + const rule = getStringArgByKey(sentence, 'rule'); + const ruleFlag = getStringArgByKey(sentence, 'ruleFlag'); + const ruleText = getStringArgByKey(sentence, 'ruleText'); const font = getCurrentFontFamily(); @@ -39,6 +44,16 @@ export const getUserInput = (sentence: ISentence): IPerform => { onMouseEnter={playSeEnter} onClick={() => { const userInput: HTMLInputElement = document.getElementById('user-input') as HTMLInputElement; + if (rule) { + const reg = tryToRegex(rule, ruleFlag); + if (reg && !reg.test(userInput.value)) { + if (ruleText) alert(ruleText); + return; + } + if (!reg) { + logger.warn(`getUserInput: rule ${rule} is not a valid regex`); + } + } if (userInput) { webgalStore.dispatch( setStageVar({ diff --git a/packages/webgal/src/Core/util/global.ts b/packages/webgal/src/Core/util/global.ts new file mode 100644 index 000000000..4c4a8e4d1 --- /dev/null +++ b/packages/webgal/src/Core/util/global.ts @@ -0,0 +1,7 @@ +export function tryToRegex(str: string, flag: string | null): RegExp | false { + try { + return new RegExp(str, flag || ''); + } catch (e) { + return false; + } +} From 37e06ab4881e31c7cd8bef7e619e83a9e4bb8def Mon Sep 17 00:00:00 2001 From: xiaoxustudio <1783558957@qq.com> Date: Sat, 3 Jan 2026 19:00:02 +0800 Subject: [PATCH 2/3] feat: can use $0 get input value --- packages/webgal/src/Core/gameScripts/getUserInput/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx b/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx index 4a8391987..4c5a4e82c 100644 --- a/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx +++ b/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx @@ -47,7 +47,7 @@ export const getUserInput = (sentence: ISentence): IPerform => { if (rule) { const reg = tryToRegex(rule, ruleFlag); if (reg && !reg.test(userInput.value)) { - if (ruleText) alert(ruleText); + if (ruleText) alert(ruleText.replaceAll(/\$0/g, userInput.value)); return; } if (!reg) { From 9019e858446ec6c143da31b896959dcd1a202db5 Mon Sep 17 00:00:00 2001 From: xiaoxustudio Date: Mon, 30 Mar 2026 12:42:40 +0800 Subject: [PATCH 3/3] chore: use local built-in component to show text --- .../Core/gameScripts/getUserInput/index.tsx | 7 ++++- .../src/UI/GlobalDialog/GlobalDialog.tsx | 28 +++++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx b/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx index 4c5a4e82c..7b3fc3c31 100644 --- a/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx +++ b/packages/webgal/src/Core/gameScripts/getUserInput/index.tsx @@ -15,6 +15,7 @@ import { setStageVar } from '@/store/stageReducer'; import { getCurrentFontFamily } from '@/hooks/useFontFamily'; import { logger } from '@/Core/util/logger'; import { tryToRegex } from '@/Core/util/global'; +import { showGlogalDialog } from '@/UI/GlobalDialog/GlobalDialog'; /** * 显示选择枝 @@ -47,7 +48,11 @@ export const getUserInput = (sentence: ISentence): IPerform => { if (rule) { const reg = tryToRegex(rule, ruleFlag); if (reg && !reg.test(userInput.value)) { - if (ruleText) alert(ruleText.replaceAll(/\$0/g, userInput.value)); + if (ruleText) + showGlogalDialog({ + title: ruleText.replaceAll(/\$0/g, userInput.value), + leftText: 'OK', + }); return; } if (!reg) { diff --git a/packages/webgal/src/UI/GlobalDialog/GlobalDialog.tsx b/packages/webgal/src/UI/GlobalDialog/GlobalDialog.tsx index baaf12ac7..0a253730d 100644 --- a/packages/webgal/src/UI/GlobalDialog/GlobalDialog.tsx +++ b/packages/webgal/src/UI/GlobalDialog/GlobalDialog.tsx @@ -12,10 +12,10 @@ export default function GlobalDialog() { interface IShowGlobalDialogProps { title: string; - leftText: string; - rightText: string; - leftFunc: Function; - rightFunc: Function; + leftText?: string; + rightText?: string; + leftFunc?: Function; + rightFunc?: Function; } export function showGlogalDialog(props: IShowGlobalDialogProps) { @@ -23,12 +23,12 @@ export function showGlogalDialog(props: IShowGlobalDialogProps) { webgalStore.dispatch(setVisibility({ component: 'showGlobalDialog', visibility: true })); const handleLeft = () => { playSeClick(); - props.leftFunc(); + props.leftFunc?.(); hideGlobalDialog(); }; const handleRight = () => { playSeClick(); - props.rightFunc(); + props.rightFunc?.(); hideGlobalDialog(); }; const renderElement = ( @@ -37,12 +37,16 @@ export function showGlogalDialog(props: IShowGlobalDialogProps) {
{props.title}
-
- {props.leftText} -
-
- {props.rightText} -
+ {props.leftText && ( +
+ {props.leftText} +
+ )} + {props.rightText && ( +
+ {props.rightText} +
+ )}