@salesforcedevs/docs-components 1.31.0-md-alpha1 → 1.31.0-md-doctoolbar-alpha

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/lwc.config.json CHANGED
@@ -18,6 +18,7 @@
18
18
  "doc/contentMedia",
19
19
  "doc/docXmlContent",
20
20
  "doc/lwcContentLayout",
21
+ "doc/unifiedContentLayout",
21
22
  "doc/header",
22
23
  "doc/heading",
23
24
  "doc/headingAnchor",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/docs-components",
3
- "version": "1.31.0-md-alpha1",
3
+ "version": "1.31.0-md-doctoolbar-alpha",
4
4
  "description": "Docs Lightning web components for DSC",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
@@ -9,8 +9,7 @@ import { buildDocLinkClickHandler } from "dxUtils/analytics";
9
9
  import ContentActionToolbar from "doc/contentActionToolbar";
10
10
 
11
11
  const CONTENT_ACTION_TOOLBAR_TAG = "doc-content-action-toolbar";
12
- const HEADING_SELECTOR = "h1, h2, h3, h4, h5, h6";
13
- const CONTENT_BODY_SELECTOR = ".content-body-container .content-body";
12
+ const PAGE_HEADING_SELECTOR = "h1";
14
13
 
15
14
  type AnchorMap = { [key: string]: { intersect: boolean; id: string } };
16
15
 
@@ -287,8 +286,7 @@ export default class ContentLayout extends LightningElement {
287
286
 
288
287
  /**
289
288
  * Inserts the content action toolbar into the slotted content immediately
290
- * after the first heading (any level) found inside the content body. This is
291
- * the page's H1 when present, otherwise the first heading in document order.
289
+ * after the first H1 found inside this layout.
292
290
  */
293
291
  protected updateContentActionToolbar(): void {
294
292
  if (!this.showContentActionToolbar || !this.sidebarValue) {
@@ -296,18 +294,9 @@ export default class ContentLayout extends LightningElement {
296
294
  return;
297
295
  }
298
296
 
299
- const contentBody = querySelector(
300
- CONTENT_BODY_SELECTOR,
301
- this.template.host
302
- ) as HTMLElement | null;
303
-
304
- if (!contentBody) {
305
- return;
306
- }
307
-
308
297
  const heading = querySelector(
309
- HEADING_SELECTOR,
310
- contentBody
298
+ PAGE_HEADING_SELECTOR,
299
+ this.template.host
311
300
  ) as HTMLElement | null;
312
301
 
313
302
  if (!heading) {
@@ -1,7 +1,6 @@
1
1
  :host {
2
2
  --dx-footer-margin-top: 142px;
3
3
  --doc-c-redoc-sidebar-top: calc(
4
- var(--dx-g-global-header-height) + var(--dx-g-doc-header-height) +
5
- var(--dx-g-spacing-xl)
4
+ var(--dx-g-global-header-height) + var(--dx-g-doc-header-height)
6
5
  );
7
6
  }
@@ -3,9 +3,12 @@ import { createElement, LightningElement, api } from "lwc";
3
3
  import DocPhase from "doc/phase";
4
4
  import DxFooter from "dx/footer";
5
5
  import DxIcon from "dx/icon";
6
+ import SidebarFooterNav from "dx/sidebarFooterNav";
6
7
  import SprigSurvey from "doc/sprigSurvey";
7
8
  import { throttle } from "throttle-debounce";
8
9
  import { pollUntil } from "dxUtils/async";
10
+ import { toJson } from "dxUtils/normalizers";
11
+ import type { OptionWithLink } from "typings/custom";
9
12
 
10
13
  declare global {
11
14
  interface Window {
@@ -37,7 +40,10 @@ type ReferenceConfig = {
37
40
  const SCROLL_THROTTLE_DELAY = 50;
38
41
  const ELEMENT_TIMEOUT = 10000;
39
42
  const ELEMENT_CHECK_INTERVAL = 100;
40
- const REFERENCES_SEGMENT = "/references/";
43
+ const DEFAULT_PROJECT_TITLE = "All Reference";
44
+ const BACK_TARGET_STORAGE_KEY = "redoc-back-target";
45
+ const DOCS_PATH_SEGMENT = "docs";
46
+ const DEFAULT_LOCALE = "en-us";
41
47
 
42
48
  export default class RedocReference extends LightningElement {
43
49
  private _referenceConfig: ReferenceConfig = { refList: [] };
@@ -47,6 +53,7 @@ export default class RedocReference extends LightningElement {
47
53
  private docHeaderElement: Element | null = null;
48
54
  private docPhaseWrapperElement: Element | null = null;
49
55
  private lastSidebarTop = 0;
56
+ private _languages: OptionWithLink[] = [];
50
57
 
51
58
  /**
52
59
  * History length captured at mount (pre-Redoc), used by `onBackClick` to
@@ -91,19 +98,86 @@ export default class RedocReference extends LightningElement {
91
98
  * Project title (same value passed to `<doc-header>` as `subtitle`). Used
92
99
  * inside the Redoc-rendered UI to label the parent project.
93
100
  */
94
- @api projectTitle: string | null = "All Reference";
101
+ @api
102
+ get projectTitle(): string | null {
103
+ return this.isDocContentType
104
+ ? this._projectTitle
105
+ : DEFAULT_PROJECT_TITLE;
106
+ }
107
+ set projectTitle(value: string | null) {
108
+ this._projectTitle = value;
109
+ }
110
+ private _projectTitle: string | null = null;
111
+
112
+ /**
113
+ * Href to navigate to when the back link is clicked AND there is no
114
+ * usable referrer (e.g. the user opened the page directly in a fresh
115
+ * tab).
116
+ */
117
+ @api headerHref: string | null = null;
118
+
119
+ @api
120
+ get contentType(): string {
121
+ return this._contentType;
122
+ }
123
+ set contentType(value: string) {
124
+ this._contentType = value;
125
+ }
126
+ private _contentType: string = "";
127
+
128
+ @api
129
+ get languages(): OptionWithLink[] {
130
+ return this._languages;
131
+ }
132
+ set languages(value: string | OptionWithLink[]) {
133
+ this._languages = toJson(value) || [];
134
+ }
135
+
136
+ @api language: string | null = null;
95
137
 
96
138
  get specTitle(): string | null {
97
139
  return this.getSelectedReference()?.title ?? null;
98
140
  }
99
141
 
142
+ get isDocContentType(): boolean {
143
+ return this.contentType === "docs";
144
+ }
145
+
146
+ get isReferenceContentType(): boolean {
147
+ return this.contentType === "references";
148
+ }
149
+
150
+ get isMultiSpecSet(): boolean {
151
+ const refCount = this.referenceConfig?.refList?.length ?? 0;
152
+ return refCount > 1;
153
+ }
154
+
155
+ // Reads stored back target
156
+ private getBackTargetFromSession(): string | null {
157
+ return sessionStorage.getItem(BACK_TARGET_STORAGE_KEY);
158
+ }
159
+
160
+ private setBackTargetInSession(href: string): void {
161
+ sessionStorage.setItem(BACK_TARGET_STORAGE_KEY, href);
162
+ }
163
+
164
+ /**
165
+ * when more than one localized spec is available
166
+ */
167
+ get hasLocalePicker(): boolean {
168
+ return this.languages.length > 1;
169
+ }
170
+
100
171
  /**
101
- * Whether to show the project header (only for multi-spec reference sets).
172
+ * Whether to show the project header. Shown for multi-spec reference
173
+ * sets, and for any docs-content spec topic so it always has a back
174
+ * link.
102
175
  */
103
176
  get showRedocHeader(): boolean {
104
- const refCount = this._referenceConfig?.refList?.length ?? 0;
105
- const isMultiSpecSet = refCount > 1;
106
- return isMultiSpecSet && !!(this.projectTitle || this.specTitle);
177
+ return (
178
+ (this.isReferenceContentType && this.isMultiSpecSet) ||
179
+ this.isDocContentType
180
+ );
107
181
  }
108
182
 
109
183
  /**
@@ -111,16 +185,68 @@ export default class RedocReference extends LightningElement {
111
185
  */
112
186
  private onBackClick = (event: Event): void => {
113
187
  event.preventDefault();
188
+
189
+ const target = this.getTranslatedBackTarget() ?? this.headerHref;
190
+
191
+ if (target) {
192
+ window.location.assign(target);
193
+ }
194
+ };
195
+
196
+ private getTranslatedBackTarget(): string | null {
197
+ const stored = this.getBackTargetFromSession();
198
+ if (stored) {
199
+ return this.language
200
+ ? this.translateToCurrentLocale(stored)
201
+ : stored;
202
+ }
203
+
204
+ const referrer = this.getSameOriginReferrerHref();
205
+ return referrer && !this.isLocaleHref(new URL(referrer).pathname)
206
+ ? referrer
207
+ : null;
208
+ }
209
+
210
+ /* The locale segment sits after "docs" for docs pages,
211
+ * and after the product segment for references.
212
+ */
213
+ private translateToCurrentLocale(storedHref: string): string {
214
+ const url = new URL(storedHref, window.location.origin);
215
+ const localeIds = this.languages.map((loc) => loc.id);
216
+ const segments = url.pathname.split("/").filter(Boolean);
217
+
218
+ // docs -> right after "docs"; references -> after the product segment.
219
+ const localeIndex = this.isDocContentType ? 1 : 2;
220
+
221
+ if (segments[0] === DOCS_PATH_SEGMENT) {
222
+ // Drop the existing locale segment, if any
223
+ if (localeIds.includes(segments[localeIndex])) {
224
+ segments.splice(localeIndex, 1);
225
+ }
226
+ // Add the current locale
227
+ if (this.language && this.language !== DEFAULT_LOCALE) {
228
+ segments.splice(localeIndex, 0, this.language);
229
+ }
230
+ }
231
+
232
+ url.pathname = "/" + segments.join("/");
233
+ return url.href;
234
+ }
235
+
236
+ // Preserves target across locale switches, updates on normal navigation
237
+ private trackBackTarget(): void {
114
238
  const referrerHref = this.getSameOriginReferrerHref();
115
- if (referrerHref) {
116
- window.location.href = referrerHref;
239
+ if (!referrerHref) {
117
240
  return;
118
241
  }
119
- const fallbackHref = this.getReferencesRootHref();
120
- if (fallbackHref) {
121
- window.location.href = fallbackHref;
242
+
243
+ const referrerUrl = new URL(referrerHref);
244
+ if (this.isLocaleHref(referrerUrl.pathname)) {
245
+ return;
122
246
  }
123
- };
247
+
248
+ this.setBackTargetInSession(referrerHref);
249
+ }
124
250
 
125
251
  /**
126
252
  * Returns the referrer URL when the page was reached via in-tab navigation
@@ -144,16 +270,16 @@ export default class RedocReference extends LightningElement {
144
270
  }
145
271
 
146
272
  /**
147
- * Derives the project's `.../references` root from the current URL by
148
- * trimming any trailing reference id (and deeper segments). Returns null
149
- * when the URL doesn't contain a `/references` segment.
273
+ * switching locale on this same page.
150
274
  */
151
- private getReferencesRootHref(): string | null {
152
- const { pathname } = window.location;
153
- const idx = pathname.lastIndexOf(REFERENCES_SEGMENT);
154
- return idx === -1
155
- ? null
156
- : pathname.slice(0, idx + REFERENCES_SEGMENT.length);
275
+ private isLocaleHref(pathname: string): boolean {
276
+ return this.languages.some((locale) => {
277
+ const href = locale?.link?.href;
278
+ return (
279
+ !!href &&
280
+ new URL(href, window.location.origin).pathname === pathname
281
+ );
282
+ });
157
283
  }
158
284
 
159
285
  /** When origin is provided, pass it to the footer; otherwise use dx-footer's default. */
@@ -168,6 +294,9 @@ export default class RedocReference extends LightningElement {
168
294
  // so it reflects real in-tab navigation rather than Redoc's churn.
169
295
  this.initialHistoryLength = window.history.length;
170
296
 
297
+ // Track the back target, preserving it across locale switches
298
+ this.trackBackTarget();
299
+
171
300
  window.addEventListener("scroll", this.handleScrollAndResize);
172
301
  window.addEventListener("resize", this.handleScrollAndResize);
173
302
  }
@@ -394,7 +523,7 @@ export default class RedocReference extends LightningElement {
394
523
  this.appendFooterItems(apiContentDiv);
395
524
 
396
525
  // Inject the multi-spec project header into Redoc's left menu only.
397
- this.insertProjectHeaderInMenu(redocContainer);
526
+ this.insertSidebarNav(redocContainer);
398
527
 
399
528
  // Wait for footer to be rendered before updating styles
400
529
  requestAnimationFrame(() => {
@@ -409,22 +538,46 @@ export default class RedocReference extends LightningElement {
409
538
  }
410
539
 
411
540
  /**
412
- * Inserts the project header into Redoc for multi-spec reference sets.
541
+ * Inserts the project header and LNB footer into Redoc.
413
542
  */
414
- private insertProjectHeaderInMenu(redocContainer: HTMLElement): void {
415
- if (!this.showRedocHeader) {
416
- return;
543
+ private insertSidebarNav(redocContainer: HTMLElement): void {
544
+ // Select the LNB and content area of Redoc and insert the requried header.
545
+ if (this.showRedocHeader) {
546
+ redocContainer
547
+ .querySelectorAll<HTMLElement>(".menu-content, .api-content")
548
+ .forEach((target) => {
549
+ target.insertBefore(
550
+ this.buildProjectHeaderDom(),
551
+ target.firstChild
552
+ );
553
+ });
417
554
  }
418
555
 
419
- // Select the LNB and content area of Redoc and insert the requried header.
420
- redocContainer
421
- .querySelectorAll<HTMLElement>(".menu-content, .api-content")
422
- .forEach((target) => {
423
- target.insertBefore(
424
- this.buildProjectHeaderDom(),
425
- target.firstChild
426
- );
427
- });
556
+ // Locale picker
557
+ if (this.hasLocalePicker) {
558
+ const menuContent = redocContainer.querySelector(".menu-content");
559
+ menuContent?.appendChild(this.buildLocalePickerDom());
560
+ }
561
+ }
562
+
563
+ /**
564
+ * Builds the locale picker DOM by reusing `dx-sidebar-footer-nav`
565
+ */
566
+ private buildLocalePickerDom(): HTMLElement {
567
+ const wrapper = document.createElement("div");
568
+ wrapper.className = "redoc-footer-nav";
569
+
570
+ const picker = createElement("dx-sidebar-footer-nav", {
571
+ is: SidebarFooterNav
572
+ });
573
+
574
+ Object.assign(picker, {
575
+ languages: this.languages,
576
+ language: this.language
577
+ });
578
+ wrapper.appendChild(picker);
579
+
580
+ return wrapper;
428
581
  }
429
582
 
430
583
  /**
@@ -0,0 +1,19 @@
1
+ :host {
2
+ display: block;
3
+ }
4
+
5
+ .content-type-docs doc-phase {
6
+ --doc-c-phase-top: calc(
7
+ var(--dx-g-global-header-height) + var(--dx-g-doc-header-height) +
8
+ var(--dx-g-spacing-xl)
9
+ );
10
+ }
11
+
12
+ @media screen and (max-width: 768px) {
13
+ .content-type-docs doc-phase {
14
+ --doc-c-phase-top: calc(
15
+ var(--dx-g-global-header-height) + var(--dx-g-doc-header-height) +
16
+ 40px
17
+ );
18
+ }
19
+ }
@@ -0,0 +1,29 @@
1
+ <template>
2
+ <doc-content-layout
3
+ class="content-type content-type-markdown content-type-docs"
4
+ breadcrumbs={breadcrumbs}
5
+ share-title={shareTitle}
6
+ share-twitter-via={twitterVia}
7
+ sidebar-header={sidebarHeader}
8
+ sidebar-value={sidebarValue}
9
+ sidebar-content={sidebarContent}
10
+ toc-title={tocTitle}
11
+ toc-options={tocOptions}
12
+ toc-aria-level={tocAriaLevel}
13
+ enable-slot-change="true"
14
+ languages={languages}
15
+ language={language}
16
+ show-footer={enableFooter}
17
+ show-content-action-toolbar={showContentActionToolbar}
18
+ origin={origin}
19
+ dev-center={devCenter}
20
+ brand={brand}
21
+ >
22
+ <doc-phase
23
+ slot="doc-phase"
24
+ lwc:if={docPhaseInfo}
25
+ doc-phase-info={docPhaseInfo}
26
+ ></doc-phase>
27
+ <slot></slot>
28
+ </doc-content-layout>
29
+ </template>
@@ -0,0 +1,88 @@
1
+ import { LightningElement, api } from "lwc";
2
+ import { toJson } from "dxUtils/normalizers";
3
+ import type { OptionWithLink, TreeNode } from "typings/custom";
4
+
5
+ /**
6
+ * Per-topic type emitted by the docs content-type parser
7
+ * (see @salesforcedevs/sfdocs-doc-framework: `TopicTypeEnum`). Only `spec`
8
+ * is meaningful inside this component; everything else renders as a plain
9
+ * markdown-style tile.
10
+ */
11
+ const TOPIC_TYPE_SPEC = "spec";
12
+
13
+ /**
14
+ * Translates per-topic `topicType` into the generic `showForwardArrow` flag
15
+ * that `dx-tree-tile` reads (spec topics get the forward arrow icon for Redoc).
16
+ */
17
+ function decorateTopicsWithForwardArrow(
18
+ topics: Array<TreeNode & { topicType?: string }> | undefined
19
+ ): TreeNode[] | undefined {
20
+ return topics?.map((topic) => {
21
+ const { topicType, ...decorated } = topic;
22
+ if (topicType === TOPIC_TYPE_SPEC) {
23
+ decorated.showForwardArrow = true;
24
+ }
25
+ if (decorated.children) {
26
+ decorated.children = decorateTopicsWithForwardArrow(
27
+ decorated.children
28
+ );
29
+ }
30
+ return decorated;
31
+ });
32
+ }
33
+
34
+ /**
35
+ * Wrapper around `doc-content-layout` for the "docs" content type emitted by
36
+ * the `DocsContentTypeParser`.
37
+ */
38
+ export default class UnifiedContentLayout extends LightningElement {
39
+ @api breadcrumbs: string | null = null;
40
+ @api sidebarHeader?: string;
41
+ @api sidebarValue?: string;
42
+ @api tocTitle?: string;
43
+ @api tocOptions?: string;
44
+ @api tocAriaLevel?: string;
45
+ @api languages?: OptionWithLink[];
46
+ @api language?: string;
47
+ @api devCenter: any = null;
48
+ @api brand: any = null;
49
+ @api showContentActionToolbar = false;
50
+
51
+ /** Optional origin URL for the footer MFE (e.g. wp-json endpoint). */
52
+ @api origin: string | null = null;
53
+
54
+ /** Article name from breadcrumbs, used as share title (e.g. for social share). */
55
+ @api shareTitle: string | null = null;
56
+
57
+ /** Optional Twitter "via" handle (e.g. SalesforceDevs) for social share. */
58
+ @api twitterVia: string | null = null;
59
+
60
+ @api hideFooter = false;
61
+
62
+ private _docPhaseInfo: string | null = null;
63
+ private _sidebarContent: unknown = null;
64
+
65
+ @api
66
+ get docPhaseInfo(): string | null {
67
+ return this._docPhaseInfo;
68
+ }
69
+
70
+ set docPhaseInfo(value: string | null) {
71
+ this._docPhaseInfo = value || null;
72
+ }
73
+
74
+ @api
75
+ get sidebarContent(): unknown {
76
+ return this._sidebarContent;
77
+ }
78
+
79
+ set sidebarContent(value: string) {
80
+ this._sidebarContent = decorateTopicsWithForwardArrow(
81
+ toJson(value)?.topics
82
+ );
83
+ }
84
+
85
+ private get enableFooter(): boolean {
86
+ return !this.hideFooter;
87
+ }
88
+ }