Files
Automata/packages/editor-ui/src/plugins/i18n/utils.ts
Iván Ovejero 5fec563c5c i18n feedback refactorings (#2597)
*  Create endpoint for node credential translation

*  Add API helper method in FE

* 🔨 Add creds JSON files to tsconfig

*  Refactor credentials loading

*  Refactor calls in CredentialConfig

* ✏️ Add dummy translations

*  Split translations per node

* 🔥 Remove deprecated method

*  Refactor nesting in collections

* 🚚 Rename topParameter methods for accuracy

* ✏️ Fill out GitHub dummy cred

* 🚚 Clarify naming for collection utils

* ✏️ Fill out dummy translation

* 🔥 Remove surplus colons

* 🔥 Remove logging

*  Restore missing space

* 🔥 Remove lingering colon

*  Add path to InputLabel calls

* ✏️ Fill out dummy translations

* 🐛 Fix multipleValuesButtonText logic

*  Add sample properties to be deleted

*  Render deeply nested params

* 📦 Update package-lock.json

* 🔥 remove logging

* ✏️ Add dummy value to Slack translation

* ✏️ Add placeholder to dummy translation

*  Fix placeholder rendering for button text

* 👕 Fix lint

* 🔥 Remove outdated comment

* 🐛 Pass in missing arg for placeholder

* ✏️ Fill out Slack translation

*  Add explanatory comment

* ✏️ Fill out dummy translation

* ✏️ Update documentation

* 🔥 Remove broken link

* ✏️ Add pending functionality

* ✏️ Fix indentation

* 🐛 Fix method call in CredentialEdit

*  Implement eventTriggerDescription

* 🐛 Fix table-json-binary radio buttons

* ✏️ Clarify usage of eventTriggerDescription

* 🔥 Remove unneeded arg

* 🐛 Fix display in CodeEdit and TextEdit

* 🔥 Remove logging

* ✏️ Add translation for test cred options

* ✏️ Add test for separate file in same dir

* ✏️ Add test for versioned node

* ✏️ Add test for node in grouped dir

* ✏️ Add minor clarifications

* ✏️ Add nested collection test

* ✏️ Add pending functionality

*  Generalize collections handling

* 🚚 Rename helper to remove redundancy

* 🚚 Improve naming in helpers

* ✏️ Improve helpers documentation

* ✏️ Improve i18n methods documentation

* 🚚 Make endpoint naming consistent

* ✏️ Add final newlines

* ✏️ Clean up JSON examples

*  Reuse i18n method

*  Improve utils readability

*  Return early if cred translation exists

* 🔥 Remove dummy translations
2022-01-07 22:02:21 +01:00

77 lines
2.4 KiB
TypeScript

/**
* Derive the middle key, i.e. the segment of the render key located between
* the initial key (path to parameters root) and the property to render.
*
* Used by `nodeText()` to handle nested params.
*
* Location: `n8n-nodes-base.nodes.github.nodeView.<middleKey>.placeholder`
*/
export function deriveMiddleKey(
path: string,
parameter: { name: string; type: string; },
) {
let middleKey = parameter.name;
if (
isTopLevelCollection(path, parameter) ||
isNestedInCollectionLike(path)
) {
const pathSegments = normalize(path).split('.');
middleKey = insertOptionsAndValues(pathSegments).join('.');
}
if (
isNestedCollection(path, parameter) ||
isFixedCollection(path, parameter)
) {
const pathSegments = [...normalize(path).split('.'), parameter.name];
middleKey = insertOptionsAndValues(pathSegments).join('.');
}
return middleKey;
}
/**
* Check if a param path is for a param nested inside a `collection` or
* `fixedCollection` param.
*/
export const isNestedInCollectionLike = (path: string) => path.split('.').length >= 3;
const isTopLevelCollection = (path: string, parameter: { type: string }) =>
path.split('.').length === 2 && parameter.type === 'collection';
const isNestedCollection = (path: string, parameter: { type: string }) =>
path.split('.').length > 2 && parameter.type === 'collection';
/**
* Check if the param is a normal `fixedCollection`, i.e. a FC other than the wrapper
* that sits at the root of a node's top-level param and contains all of them.
*/
const isFixedCollection = (path: string, parameter: { type: string }) =>
parameter.type === 'fixedCollection' && path !== 'parameters';
/**
* Remove all indices and the `parameters.` prefix from a parameter path.
*
* Example: `parameters.a[0].b` → `a.b`
*/
export const normalize = (path: string) => path.replace(/\[.*?\]/g, '').replace('parameters.', '');
/**
* Insert `'options'` and `'values'` on an alternating basis in a string array of
* indefinite length. Helper to create a valid render key for a collection-like param.
*
* Example: `['a', 'b', 'c']` → `['a', 'options', 'b', 'values', 'c']`
*/
export const insertOptionsAndValues = (pathSegments: string[]) => {
return pathSegments.reduce<string[]>((acc, cur, i) => {
acc.push(cur);
if (i === pathSegments.length - 1) return acc;
acc.push(i % 2 === 0 ? 'options' : 'values');
return acc;
}, []);
};