@4site/engrid-scripts 0.28.0 → 0.28.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,10 @@
1
1
  export declare class InputPlaceholders {
2
2
  private defaultPlaceholders;
3
+ private selectorToI18nKey;
4
+ private customSelectors;
3
5
  constructor();
4
6
  private shouldRun;
5
7
  private run;
8
+ private resolvePlaceholder;
6
9
  private addPlaceholder;
7
10
  }
@@ -12,6 +12,9 @@
12
12
  import { ENGrid } from ".";
13
13
  export class InputPlaceholders {
14
14
  constructor() {
15
+ // NOTE: for selectors listed in selectorToI18nKey below, these English
16
+ // strings are shadowed by the i18n dictionary — edit
17
+ // interfaces/i18n-options.ts ("placeholders.*" keys) instead of here.
15
18
  this.defaultPlaceholders = {
16
19
  "input#en__field_supporter_firstName": "First Name",
17
20
  "input#en__field_supporter_lastName": "Last Name",
@@ -57,10 +60,30 @@ export class InputPlaceholders {
57
60
  "input#en__field_supporter_billingRegion": "Billing Region",
58
61
  "input#en__field_supporter_billingPostcode": "Billing Postal Code",
59
62
  };
63
+ // Maps the default-placeholder selectors to i18n dictionary keys, so the
64
+ // built-in strings follow the page language. Selectors the client overrides
65
+ // via the Placeholders option are never translated.
66
+ this.selectorToI18nKey = {
67
+ "input#en__field_supporter_firstName": "placeholders.firstName",
68
+ "input#en__field_supporter_lastName": "placeholders.lastName",
69
+ "input#en__field_supporter_emailAddress": "placeholders.emailAddress",
70
+ "input#en__field_supporter_phoneNumber": "placeholders.phoneNumberOptional",
71
+ ".en__mandatory input#en__field_supporter_phoneNumber": "placeholders.phoneNumber",
72
+ ".i-required input#en__field_supporter_phoneNumber": "placeholders.phoneNumber",
73
+ "input#en__field_supporter_phoneNumber2": "placeholders.phoneNumber2Optional",
74
+ "input#en__field_supporter_country": "placeholders.country",
75
+ "input#en__field_supporter_address1": "placeholders.address1",
76
+ "input#en__field_supporter_address2": "placeholders.address2",
77
+ "input#en__field_supporter_city": "placeholders.city",
78
+ "input#en__field_supporter_region": "placeholders.region",
79
+ "input#en__field_supporter_postcode": "placeholders.postcode",
80
+ };
81
+ this.customSelectors = new Set();
60
82
  if (this.shouldRun()) {
61
83
  // If there's a Placeholders option, merge it with the default placeholders
62
84
  const placeholders = ENGrid.getOption("Placeholders");
63
85
  if (placeholders) {
86
+ this.customSelectors = new Set(Object.keys(placeholders));
64
87
  this.defaultPlaceholders = Object.assign(Object.assign({}, this.defaultPlaceholders), placeholders);
65
88
  }
66
89
  this.run();
@@ -72,9 +95,18 @@ export class InputPlaceholders {
72
95
  run() {
73
96
  Object.keys(this.defaultPlaceholders).forEach((selector) => {
74
97
  if (selector in this.defaultPlaceholders)
75
- this.addPlaceholder(selector, this.defaultPlaceholders[selector]);
98
+ this.addPlaceholder(selector, this.resolvePlaceholder(selector));
76
99
  });
77
100
  }
101
+ // Built-in placeholder strings follow the page language; client-provided
102
+ // Placeholders options always win.
103
+ resolvePlaceholder(selector) {
104
+ const key = this.selectorToI18nKey[selector];
105
+ if (key && !this.customSelectors.has(selector)) {
106
+ return ENGrid.t(key);
107
+ }
108
+ return this.defaultPlaceholders[selector];
109
+ }
78
110
  addPlaceholder(selector, placeholder) {
79
111
  const fieldEl = document.querySelector(selector);
80
112
  if (fieldEl) {
@@ -12,6 +12,19 @@ export const I18nDefaults = {
12
12
  "translateFields.select": "Select",
13
13
  "translateFields.recipientTo": "To:",
14
14
  "a11y.errorSummary": "There are {count} errors: {messages}.",
15
+ // InputPlaceholders component defaults
16
+ "placeholders.firstName": "First Name",
17
+ "placeholders.lastName": "Last Name",
18
+ "placeholders.emailAddress": "Email Address",
19
+ "placeholders.phoneNumber": "Phone Number",
20
+ "placeholders.phoneNumberOptional": "Phone Number (Optional)",
21
+ "placeholders.phoneNumber2Optional": "000-000-0000 (Optional)",
22
+ "placeholders.country": "Country",
23
+ "placeholders.address1": "Street Address",
24
+ "placeholders.address2": "Apt., Ste., Bldg.",
25
+ "placeholders.city": "City",
26
+ "placeholders.region": "Region",
27
+ "placeholders.postcode": "ZIP Code",
15
28
  },
16
29
  es: {
17
30
  "rememberMe.label": "Recuérdame",
@@ -26,5 +39,18 @@ export const I18nDefaults = {
26
39
  "translateFields.select": "Seleccione",
27
40
  "translateFields.recipientTo": "Para:",
28
41
  "a11y.errorSummary": "Hay {count} errores: {messages}.",
42
+ // InputPlaceholders component defaults
43
+ "placeholders.firstName": "Nombre",
44
+ "placeholders.lastName": "Apellidos",
45
+ "placeholders.emailAddress": "Correo electrónico",
46
+ "placeholders.phoneNumber": "Teléfono",
47
+ "placeholders.phoneNumberOptional": "Teléfono (opcional)",
48
+ "placeholders.phoneNumber2Optional": "000-000-0000 (opcional)",
49
+ "placeholders.country": "País",
50
+ "placeholders.address1": "Calle y número",
51
+ "placeholders.address2": "Depto., Piso, Edif.",
52
+ "placeholders.city": "Ciudad",
53
+ "placeholders.region": "Provincia/Estado",
54
+ "placeholders.postcode": "Código Postal",
29
55
  },
30
56
  };
@@ -134,7 +134,20 @@ export class TranslateFields {
134
134
  ? simplecountriesSelect.cloneNode(true)
135
135
  : null;
136
136
  if (field instanceof HTMLInputElement && field.placeholder != "") {
137
- if (!fieldLabel || fieldLabel.innerHTML == field.placeholder) {
137
+ // Translate the placeholder when it mirrors the label (the common
138
+ // case). Compare normalized visible text so template whitespace and
139
+ // required-marker markup don't break the match. Order matters:
140
+ // trim before stripping the marker, or labels like "Name *\n"
141
+ // keep the asterisk.
142
+ const labelText = ((fieldLabel === null || fieldLabel === void 0 ? void 0 : fieldLabel.textContent) || "")
143
+ .replace(/\s+/g, " ")
144
+ .trim()
145
+ .replace(/\s*\*$/, "");
146
+ const placeholderText = field.placeholder
147
+ .replace(/\s+/g, " ")
148
+ .trim()
149
+ .replace(/\s*\*$/, "");
150
+ if (!fieldLabel || labelText === placeholderText) {
138
151
  field.dataset.original = field.placeholder;
139
152
  field.placeholder = translation;
140
153
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const AppVersion = "0.28.0";
1
+ export declare const AppVersion = "0.28.1";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const AppVersion = "0.28.0";
1
+ export const AppVersion = "0.28.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4site/engrid-scripts",
3
- "version": "0.28.0",
3
+ "version": "0.28.1",
4
4
  "description": "Scripts for the ENGRID Project",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "strongly-typed-events": "^2.0.9"
26
26
  },
27
27
  "homepage": "https://github.com/4site-interactive-studios/engrid#readme",
28
- "gitHead": "354dfda54591f849c4ab105d881034f7f7a2fff5",
28
+ "gitHead": "32b7076b094f29226c5f6b90d80c5bbe87062abe",
29
29
  "devDependencies": {
30
30
  "nodemon": "^2.0.20",
31
31
  "typescript": "^4.9.5"