All files / src/renderer/lib variables.tsx

97.91% Statements 47/48
93.75% Branches 15/16
100% Functions 15/15
97.56% Lines 40/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149  10x   10x 10x                     10x 10x 10x   10x 180x   10x 10x 10x                                         10x         25x 25x 7x 7x     18x 17x   1x       10x       9x   90x 90x 90x                                 10x         17x   102x 102x                 34x 34x                       42x   10x 27x     42x 42x                         10x 180x 21x 427x 44x            
import { ScriptEditorCtx } from "shared/lib/scripts/context";
import uniq from "lodash/uniq";
import { ScriptNormalized } from "shared/lib/entities/entitiesTypes";
import l10n from "shared/lib/lang/l10n";
import {
  customEventVariableCode,
  customEventVariableName,
  globalVariableCode,
  localVariableCode,
  localVariableName,
  tempVariableCode,
  tempVariableName,
  variableDisplayName,
} from "shared/lib/variables/variableNames";
import { Variable } from "shared/lib/resources/types";
import { sortByName } from "shared/lib/helpers/sort";
import { variableName } from "shared/lib/entities/entitiesHelpers";
import keyBy from "lodash/keyBy";
 
const arrayNStrings = (n: number) =>
  Array.from(Array(n).keys()).map((n) => String(n));
 
const localVariables = arrayNStrings(6);
const tempVariables = arrayNStrings(2);
const customEventVariables = arrayNStrings(10);
 
type VariablesLookup = { [name: string]: Variable | undefined };
 
export interface NamedVariable {
  id: string; // The id to use in dropdown value
  code: string; // The code to use in dialogue (when wrapped by $ or #)
  name: string; // The plain name used when rendering mention tags
  displayName: string; // The name, including array length, shown in lists
  group: string; // Group name that variable belongs to
}
 
interface VariableGroup {
  name: string; // The group name
  variables: NamedVariable[]; // Variables in the group
}
 
/******************************************************************************
 * Available Variables List (for using in Dropdowns etc)
 */
 
export const namedVariablesByContext = (
  context: ScriptEditorCtx,
  variables: Variable[],
  customEvent: ScriptNormalized | undefined,
): NamedVariable[] => {
  const variablesLookup = keyBy(variables, "id");
  if (context.type === "script") {
    Eif (customEvent) {
      return namedCustomEventVariables(customEvent, variables);
    }
    return [];
  } else if (context.type === "entity" || context.type === "prefab") {
    return namedEntityVariables(context.entityId, variables, variablesLookup);
  } else {
    return namedGlobalVariables(variables);
  }
};
 
export const namedCustomEventVariables = (
  customEvent: ScriptNormalized,
  variables: Variable[],
): NamedVariable[] => {
  return ([] as NamedVariable[]).concat(
    customEventVariables.map((variable) => {
      const id = customEventVariableCode(variable);
      const name = customEventVariableName(variable, customEvent);
      return {
        id,
        code: id,
        name,
        displayName: variableDisplayName(
          name,
          customEvent.variables[id]?.passByReference === "array"
            ? customEvent.variables[id].length
            : undefined,
        ),
        group: l10n("SIDEBAR_PARAMETERS"),
      };
    }),
    namedGlobalVariables(variables),
  );
};
 
const namedEntityVariables = (
  entityId: string,
  variables: Variable[],
  variablesLookup: VariablesLookup,
): NamedVariable[] => {
  return ([] as NamedVariable[]).concat(
    localVariables.map((variable) => {
      const name = localVariableName(variable, entityId, variablesLookup);
      return {
        id: localVariableCode(variable),
        code: localVariableCode(variable),
        name,
        displayName: name,
        group: l10n("FIELD_LOCAL"),
      };
    }),
    tempVariables.map((variable) => {
      const name = tempVariableName(variable);
      return {
        id: tempVariableCode(variable),
        code: tempVariableCode(variable),
        name,
        displayName: name,
        group: l10n("FIELD_TEMPORARY"),
      };
    }),
    namedGlobalVariables(variables),
  );
};
 
const isGlobalVariable = (variable: Variable) => !variable.id.includes("__L");
 
const namedGlobalVariables = (variables: Variable[]): NamedVariable[] =>
  variables
    .filter(isGlobalVariable)
    .map((variable, index) => {
      const name = variableName(variable, index);
      return {
        id: variable.id,
        code: globalVariableCode(variable.id),
        name,
        displayName: variableDisplayName(
          name,
          variable.type === "array" ? variable.length : undefined,
        ),
        group: l10n("FIELD_GLOBAL"),
      };
    })
    .sort(sortByName);
 
export const groupVariables = (variables: NamedVariable[]): VariableGroup[] => {
  const groups = uniq(variables.map((f) => f.group));
  return groups.map((g) => {
    const groupVariables = variables.filter((f) => f.group === g);
    return {
      name: g,
      variables: groupVariables,
    };
  });
};