@salesforcedevs/docs-components 1.30.1-locale-picker → 1.30.1-locale-picker-2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/docs-components",
3
- "version": "1.30.1-locale-picker",
3
+ "version": "1.30.1-locale-picker-2",
4
4
  "description": "Docs Lightning web components for DSC",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
@@ -41,6 +41,7 @@ const SCROLL_THROTTLE_DELAY = 50;
41
41
  const ELEMENT_TIMEOUT = 10000;
42
42
  const ELEMENT_CHECK_INTERVAL = 100;
43
43
  const DEFAULT_PROJECT_TITLE = "All Reference";
44
+ const BACK_TARGET_STORAGE_KEY = "redoc-back-target";
44
45
 
45
46
  export default class RedocReference extends LightningElement {
46
47
  private _referenceConfig: ReferenceConfig = { refList: [] };
@@ -51,7 +52,6 @@ export default class RedocReference extends LightningElement {
51
52
  private docPhaseWrapperElement: Element | null = null;
52
53
  private lastSidebarTop = 0;
53
54
  private _languages: OptionWithLink[] = [];
54
- private langValuePath: string = "id";
55
55
 
56
56
  /**
57
57
  * History length captured at mount (pre-Redoc), used by `onBackClick` to
@@ -131,7 +131,6 @@ export default class RedocReference extends LightningElement {
131
131
  this._languages = toJson(value) || [];
132
132
  }
133
133
 
134
- /** Currently selected locale, matched against `langValuePath`. */
135
134
  @api language: string | null = null;
136
135
 
137
136
  get specTitle(): string | null {
@@ -151,12 +150,20 @@ export default class RedocReference extends LightningElement {
151
150
  return refCount > 1;
152
151
  }
153
152
 
153
+ // Reads stored back target
154
+ private getBackTargetFromSession(): string | null {
155
+ return sessionStorage.getItem(BACK_TARGET_STORAGE_KEY);
156
+ }
157
+
158
+ private setBackTargetInSession(href: string): void {
159
+ sessionStorage.setItem(BACK_TARGET_STORAGE_KEY, href);
160
+ }
161
+
154
162
  /**
155
- * Only for the docs content type and when more than one localized
156
- * spec is available
163
+ * when more than one localized spec is available
157
164
  */
158
165
  get hasLocalePicker(): boolean {
159
- return this.isDocContentType && this.languages.length > 1;
166
+ return this.languages.length > 1;
160
167
  }
161
168
 
162
169
  /**
@@ -176,14 +183,66 @@ export default class RedocReference extends LightningElement {
176
183
  */
177
184
  private onBackClick = (event: Event): void => {
178
185
  event.preventDefault();
179
- const referrerHref = this.getSameOriginReferrerHref();
180
- if (referrerHref) {
181
- window.location.href = referrerHref;
182
- } else if (this.headerHref) {
183
- window.location.href = this.headerHref;
186
+
187
+ const target = this.getTranslatedBackTarget() ?? this.headerHref;
188
+
189
+ if (target) {
190
+ window.location.assign(target);
184
191
  }
185
192
  };
186
193
 
194
+ private getTranslatedBackTarget(): string | null {
195
+ const stored = this.getBackTargetFromSession();
196
+ if (stored) {
197
+ return this.language
198
+ ? this.translateToCurrentLocale(stored)
199
+ : stored;
200
+ }
201
+
202
+ const referrer = this.getSameOriginReferrerHref();
203
+ return referrer && !this.isLocaleHref(new URL(referrer).pathname)
204
+ ? referrer
205
+ : null;
206
+ }
207
+
208
+ /* Rewrites the stored URL's locale to the current one. Paths are
209
+ * /docs/locale/.., except the default en-us which has no locale segment.
210
+ */
211
+ private translateToCurrentLocale(storedHref: string): string {
212
+ const url = new URL(storedHref, window.location.origin);
213
+ const localeIds = this.languages.map((loc) => loc.id);
214
+ const segments = url.pathname.split("/").filter(Boolean);
215
+
216
+ if (segments[0] === "docs") {
217
+ // Drop the existing locale segment, if any
218
+ if (localeIds.includes(segments[1])) {
219
+ segments.splice(1, 1);
220
+ }
221
+ // Add the current locale
222
+ if (this.language && this.language !== "en-us") {
223
+ segments.splice(1, 0, this.language);
224
+ }
225
+ }
226
+
227
+ url.pathname = "/" + segments.join("/");
228
+ return url.href;
229
+ }
230
+
231
+ // Preserves target across locale switches, updates on normal navigation
232
+ private trackBackTarget(): void {
233
+ const referrerHref = this.getSameOriginReferrerHref();
234
+ if (!referrerHref) {
235
+ return;
236
+ }
237
+
238
+ const referrerUrl = new URL(referrerHref);
239
+ if (this.isLocaleHref(referrerUrl.pathname)) {
240
+ return;
241
+ }
242
+
243
+ this.setBackTargetInSession(referrerHref);
244
+ }
245
+
187
246
  /**
188
247
  * Returns the referrer URL when the page was reached via in-tab navigation
189
248
  * from a same-origin page; otherwise `null`. Both `initialHistoryLength`
@@ -205,6 +264,19 @@ export default class RedocReference extends LightningElement {
205
264
  }
206
265
  }
207
266
 
267
+ /**
268
+ * switching locale on this same page.
269
+ */
270
+ private isLocaleHref(pathname: string): boolean {
271
+ return this.languages.some((locale) => {
272
+ const href = locale?.link?.href;
273
+ return (
274
+ !!href &&
275
+ new URL(href, window.location.origin).pathname === pathname
276
+ );
277
+ });
278
+ }
279
+
208
280
  /** When origin is provided, pass it to the footer; otherwise use dx-footer's default. */
209
281
  get effectiveFooterOrigin(): string {
210
282
  return (
@@ -217,6 +289,9 @@ export default class RedocReference extends LightningElement {
217
289
  // so it reflects real in-tab navigation rather than Redoc's churn.
218
290
  this.initialHistoryLength = window.history.length;
219
291
 
292
+ // Track the back target, preserving it across locale switches
293
+ this.trackBackTarget();
294
+
220
295
  window.addEventListener("scroll", this.handleScrollAndResize);
221
296
  window.addEventListener("resize", this.handleScrollAndResize);
222
297
  }
@@ -443,7 +518,7 @@ export default class RedocReference extends LightningElement {
443
518
  this.appendFooterItems(apiContentDiv);
444
519
 
445
520
  // Inject the multi-spec project header into Redoc's left menu only.
446
- this.insertProjectHeaderInMenu(redocContainer);
521
+ this.insertSidebarNav(redocContainer);
447
522
 
448
523
  // Wait for footer to be rendered before updating styles
449
524
  requestAnimationFrame(() => {
@@ -460,7 +535,7 @@ export default class RedocReference extends LightningElement {
460
535
  /**
461
536
  * Inserts the project header into Redoc for multi-spec reference sets.
462
537
  */
463
- private insertProjectHeaderInMenu(redocContainer: HTMLElement): void {
538
+ private insertSidebarNav(redocContainer: HTMLElement): void {
464
539
  if (!this.showRedocHeader) {
465
540
  return;
466
541
  }
@@ -495,8 +570,7 @@ export default class RedocReference extends LightningElement {
495
570
 
496
571
  Object.assign(picker, {
497
572
  languages: this.languages,
498
- language: this.language,
499
- langValuePath: this.langValuePath
573
+ language: this.language
500
574
  });
501
575
  wrapper.appendChild(picker);
502
576