fix(editor): Improve dragndrop of input pills with spaces (#9656)

This commit is contained in:
Elias Meire
2024-06-07 15:37:30 +02:00
committed by GitHub
parent bb7227d18d
commit 291d46af15
5 changed files with 124 additions and 48 deletions

View File

@@ -1,5 +1,6 @@
import type { INodeProperties, NodeParameterValueType } from 'n8n-workflow';
import { isResourceLocatorValue } from 'n8n-workflow';
import { isExpression } from './expressions';
const validJsIdNameRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
@@ -46,34 +47,54 @@ export function getMappedExpression({
return `{{ ${generatePath(root, path)} }}`;
}
const unquote = (str: string) => {
if (str.startsWith('"') && str.endsWith('"')) {
return str.slice(1, -1).replace(/\\"/g, '"');
}
if (str.startsWith("'") && str.endsWith("'")) {
return str.slice(1, -1).replace(/\\'/g, "'");
}
return str;
};
export function propertyNameFromExpression(expression: string, forceBracketAccess = false): string {
const propPath = expression
.replace(/^{{\s*|\s*}}$/g, '')
.replace(/^(\$\(.*\)\.item\.json|\$json|\$node\[.*\]\.json)\.?(.*)/, '$2');
const isSingleBracketAccess = propPath.startsWith('[') && !propPath.slice(1).includes('[');
if (isSingleBracketAccess && !forceBracketAccess) {
// "['Key with spaces']" -> "Key with spaces"
return unquote(propPath.slice(1, -1));
}
return propPath;
}
export function getMappedResult(
parameter: INodeProperties,
newParamValue: string,
prevParamValue: NodeParameterValueType,
): string {
const useDataPath = !!parameter.requiresDataPath && newParamValue.startsWith('{{ $json'); // ignore when mapping from grand-parent-node
const prevValue =
parameter.type === 'resourceLocator' && isResourceLocatorValue(prevParamValue)
? prevParamValue.value
: prevParamValue;
if (useDataPath) {
const newValue = newParamValue
.replace('{{ $json', '')
.replace(new RegExp('^\\.'), '')
.replace(new RegExp('}}$'), '')
.trim();
if (prevValue && parameter.requiresDataPath === 'multiple') {
if (typeof prevValue === 'string' && prevValue.trim() === '=') {
return newValue;
} else {
return `${prevValue}, ${newValue}`;
if (parameter.requiresDataPath) {
if (parameter.requiresDataPath === 'multiple') {
const propertyName = propertyNameFromExpression(newParamValue, true);
if (typeof prevValue === 'string' && (prevValue.trim() === '=' || prevValue.trim() === '')) {
return propertyName;
}
} else {
return newValue;
return `${prevValue}, ${propertyName}`;
}
} else if (typeof prevValue === 'string' && prevValue.startsWith('=') && prevValue.length > 1) {
return propertyNameFromExpression(newParamValue);
} else if (typeof prevValue === 'string' && isExpression(prevValue) && prevValue.length > 1) {
return `${prevValue} ${newParamValue}`;
} else if (prevValue && ['string', 'json'].includes(parameter.type)) {
return prevValue === '=' ? `=${newParamValue}` : `=${prevValue} ${newParamValue}`;