Skip to content

Commit

Permalink
Fix syntax error not showing up on mouse button and key parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Jan 17, 2025
1 parent 64771c6 commit 431acff
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
29 changes: 19 additions & 10 deletions newIDE/app/src/EventsSheet/ParameterFields/KeyboardKeyField.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,23 @@ const keyNamesSet = new Set(keyNames);
const stringRegex = /^"(\w*)"$/;
const valueRegex = /^\w*$/;

const isKeyValid = (expression: string): boolean => {
const isValidLiteralKeyboardKey = (expression: string): boolean => {
const matches = expression.match(stringRegex);
// Return true by default as it could be an expression.
return matches && matches[1] !== undefined
? keyNamesSet.has(matches[1])
: true;
// the expression is not a literal value
: false;
};

// This is not the negation of the function above as both return false for
// non-literal expressions.
const isInvalidLiteralKeyboardKey = (expression: string): boolean => {
const matches = expression.match(stringRegex);
// Return true by default as it could be an expression.
return matches && matches[1] !== undefined
? !keyNamesSet.has(matches[1])
// the expression is not a literal value
: false;
};

const getStringContent = (expression: string): string => {
Expand All @@ -162,18 +173,15 @@ export default React.forwardRef<ParameterFieldProps, ParameterFieldInterface>(

// If the current value is not in the list, display an expression field.
const [isExpressionField, setIsExpressionField] = React.useState(
!!props.value && !isKeyValid(props.value)
!!props.value && !isValidLiteralKeyboardKey(props.value)
);

const switchFieldType = () => {
setIsExpressionField(!isExpressionField);
};

const onChangeSelectValue = (value: string) => {
const oldValue = getStringContent(props.value);
if (oldValue !== value) {
props.onChange(valueRegex.test(value) ? `"${value}"` : value);
}
props.onChange(valueRegex.test(value) ? `"${value}"` : value);
};

const onChangeTextValue = (value: string) => {
Expand Down Expand Up @@ -215,7 +223,7 @@ export default React.forwardRef<ParameterFieldProps, ParameterFieldInterface>(
errorText={
!props.value ? (
<Trans>You must select a key.</Trans>
) : !isKeyValid(props.value) ? (
) : !isValidLiteralKeyboardKey(props.value) ? (
<Trans>
You must select a valid key. "{props.value}" is not valid.
</Trans>
Expand Down Expand Up @@ -266,6 +274,7 @@ export default React.forwardRef<ParameterFieldProps, ParameterFieldInterface>(

export const renderInlineKeyboardKey = ({
value,
expressionIsValid,
InvalidParameterValue,
}: ParameterInlineRendererProps) => {
if (!value) {
Expand All @@ -276,7 +285,7 @@ export const renderInlineKeyboardKey = ({
);
}

return isKeyValid(value) ? (
return expressionIsValid && !isInvalidLiteralKeyboardKey(value) ? (
value
) : (
<InvalidParameterValue>{value}</InvalidParameterValue>
Expand Down
43 changes: 34 additions & 9 deletions newIDE/app/src/EventsSheet/ParameterFields/MouseButtonField.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ const mouseButtons = [
label: t`Forward (Additional button, typically the Browser Forward button)`,
},
];
const mouseButtonsSet = new Set(mouseButtons.map(button => button.value));

const stringRegex = /^"(\w*)"$/;

const isValidLiteralMouseButton = (expression: string): boolean => {
const matches = expression.match(stringRegex);
return matches && matches[1] !== undefined
? mouseButtonsSet.has(matches[1])
// the expression is not a literal value
: false;
};

// This is not the negation of the function above as both return false for
// non-literal expressions.
const isInvalidLiteralMouseButton = (expression: string): boolean => {
const matches = expression.match(stringRegex);
// Return true by default as it could be an expression.
return matches && matches[1] !== undefined
? !mouseButtonsSet.has(matches[1])
// the expression is not a literal value
: false;
};

export default React.forwardRef<ParameterFieldProps, ParameterFieldInterface>(
function MouseButtonField(props, ref) {
Expand All @@ -48,13 +70,9 @@ export default React.forwardRef<ParameterFieldProps, ParameterFieldInterface>(
focus,
}));

const isCurrentValueInList = mouseButtons.some(
mouseButton => `"${mouseButton.value}"` === props.value
);

// If the current value is not in the list, display an expression field.
const [isExpressionField, setIsExpressionField] = React.useState(
!!props.value && !isCurrentValueInList
!!props.value && !isValidLiteralMouseButton(props.value)
);

const switchFieldType = () => {
Expand Down Expand Up @@ -144,13 +162,20 @@ export default React.forwardRef<ParameterFieldProps, ParameterFieldInterface>(

export const renderInlineMouseButton = ({
value,
expressionIsValid,
InvalidParameterValue,
}: ParameterInlineRendererProps) => {
return value ? (
if (!value) {
return (
<InvalidParameterValue isEmpty>
<Trans>Choose a mouse button</Trans>
</InvalidParameterValue>
);
}

return expressionIsValid && !isInvalidLiteralMouseButton(value) ? (
value
) : (
<InvalidParameterValue isEmpty>
<Trans>Choose a mouse button</Trans>
</InvalidParameterValue>
<InvalidParameterValue>{value}</InvalidParameterValue>
);
};

0 comments on commit 431acff

Please sign in to comment.