@salesforcedevs/docs-components 0.3.12 → 0.3.14-banner-alpha1

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.
Files changed (107) hide show
  1. package/lwc.config.json +12 -3
  2. package/package.json +17 -7
  3. package/src/modules/README.md +41 -0
  4. package/src/modules/doc/amfReference/amfReference.css +5 -0
  5. package/src/modules/doc/amfReference/amfReference.html +47 -0
  6. package/src/modules/doc/amfReference/amfReference.ts +1361 -0
  7. package/src/modules/doc/amfReference/constants.ts +76 -0
  8. package/src/modules/doc/amfReference/types.ts +133 -0
  9. package/src/modules/doc/amfReference/utils.ts +669 -0
  10. package/src/modules/doc/amfTopic/amfTopic.css +1 -0
  11. package/src/modules/doc/amfTopic/amfTopic.html +3 -0
  12. package/src/modules/doc/amfTopic/amfTopic.ts +94 -0
  13. package/src/modules/doc/amfTopic/types.ts +54 -0
  14. package/src/modules/doc/amfTopic/utils.ts +130 -0
  15. package/src/modules/doc/breadcrumbItem/breadcrumbItem.css +51 -0
  16. package/src/modules/doc/breadcrumbItem/breadcrumbItem.html +5 -0
  17. package/src/modules/doc/breadcrumbItem/breadcrumbItem.ts +64 -0
  18. package/src/modules/doc/breadcrumbs/breadcrumbs.css +27 -0
  19. package/src/modules/doc/breadcrumbs/breadcrumbs.html +60 -0
  20. package/src/modules/doc/breadcrumbs/breadcrumbs.ts +187 -0
  21. package/src/modules/doc/content/content.css +58 -103
  22. package/src/modules/doc/content/content.ts +261 -174
  23. package/src/modules/doc/contentCallout/contentCallout.css +7 -24
  24. package/src/modules/doc/contentCallout/contentCallout.html +4 -2
  25. package/src/modules/doc/contentCallout/contentCallout.ts +8 -2
  26. package/src/modules/doc/contentLayout/contentLayout.css +98 -0
  27. package/src/modules/doc/contentLayout/contentLayout.html +51 -0
  28. package/src/modules/doc/contentLayout/contentLayout.ts +322 -0
  29. package/src/modules/doc/header/header.css +103 -0
  30. package/src/modules/doc/header/header.html +160 -0
  31. package/src/modules/doc/header/header.ts +146 -0
  32. package/src/modules/doc/heading/heading.css +54 -0
  33. package/src/modules/doc/heading/heading.html +14 -0
  34. package/src/modules/doc/heading/heading.ts +65 -0
  35. package/src/modules/doc/headingAnchor/headingAnchor.css +33 -0
  36. package/src/modules/doc/headingAnchor/headingAnchor.html +19 -0
  37. package/src/modules/doc/headingAnchor/headingAnchor.ts +43 -0
  38. package/src/modules/doc/headingContent/headingContent.css +53 -0
  39. package/src/modules/doc/headingContent/headingContent.html +13 -0
  40. package/src/modules/doc/headingContent/headingContent.ts +30 -0
  41. package/src/modules/doc/phase/phase.css +55 -0
  42. package/src/modules/doc/phase/phase.html +28 -0
  43. package/src/modules/doc/phase/phase.ts +57 -0
  44. package/src/modules/doc/sprigSurvey/sprigSurvey.html +20 -0
  45. package/src/modules/doc/sprigSurvey/sprigSurvey.scoped.css +12 -0
  46. package/src/modules/doc/sprigSurvey/sprigSurvey.ts +16 -0
  47. package/src/modules/doc/toc/toc.html +3 -1
  48. package/src/modules/doc/toolbar/toolbar.ts +6 -6
  49. package/src/modules/doc/xmlContent/types.ts +114 -0
  50. package/src/modules/doc/xmlContent/utils.ts +161 -0
  51. package/src/modules/doc/xmlContent/xmlContent.css +32 -0
  52. package/src/modules/doc/xmlContent/xmlContent.html +40 -0
  53. package/src/modules/doc/xmlContent/xmlContent.ts +677 -0
  54. package/src/modules/docBaseElements/lightningElementWithState/lightningElementWithState.ts +93 -0
  55. package/src/modules/docHelpers/amfStyle/amfStyle.css +355 -0
  56. package/src/modules/docHelpers/phaseContentLayout/phaseContentLayout.css +39 -0
  57. package/src/modules/docHelpers/status/status.css +22 -0
  58. package/src/modules/docUtils/SearchSyncer/SearchSyncer.ts +85 -0
  59. package/LICENSE +0 -12
  60. package/src/modules/doc/container/__benchmarks__/container.benchmark.js +0 -43
  61. package/src/modules/doc/container/__mocks__/mockAvailableLanguages.js +0 -8
  62. package/src/modules/doc/container/__mocks__/mockAvailableVersions.js +0 -122
  63. package/src/modules/doc/container/__mocks__/mockContentFetchResponse.json +0 -5
  64. package/src/modules/doc/container/__mocks__/mockDocContent.js +0 -29
  65. package/src/modules/doc/container/__mocks__/mockNavigationFetchResponse.json +0 -4061
  66. package/src/modules/doc/container/__mocks__/mockPageReference.js +0 -8
  67. package/src/modules/doc/container/__mocks__/mockPdfUrl.js +0 -1
  68. package/src/modules/doc/container/__mocks__/mockSelectedLanguage.js +0 -8
  69. package/src/modules/doc/container/__mocks__/mockSelectedVersion.js +0 -8
  70. package/src/modules/doc/container/__mocks__/mockToc.js +0 -146
  71. package/src/modules/doc/container/__tests__/container.test.ts +0 -117
  72. package/src/modules/doc/container/container.css +0 -37
  73. package/src/modules/doc/container/container.html +0 -28
  74. package/src/modules/doc/container/container.stories.ts +0 -44
  75. package/src/modules/doc/container/container.ts +0 -367
  76. package/src/modules/doc/content/__tests__/content.test.ts +0 -99
  77. package/src/modules/doc/content/__tests__/mockDocContent.ts +0 -258
  78. package/src/modules/doc/content/__tests__/mockPageReference.ts +0 -8
  79. package/src/modules/doc/content/content.stories.ts +0 -82
  80. package/src/modules/doc/contentCallout/__tests__/contentCallout.test.ts +0 -80
  81. package/src/modules/doc/contentCallout/__tests__/mockProps.ts +0 -14
  82. package/src/modules/doc/contentCallout/contentCallout.stories.ts +0 -29
  83. package/src/modules/doc/contentMedia/__tests__/contentMedia.test.ts +0 -97
  84. package/src/modules/doc/contentMedia/contentMedia.stories.ts +0 -113
  85. package/src/modules/doc/landing/__tests__/landing.test.ts +0 -60
  86. package/src/modules/doc/landing/__tests__/mockFetch.json +0 -370
  87. package/src/modules/doc/landing/landing.css +0 -63
  88. package/src/modules/doc/landing/landing.html +0 -122
  89. package/src/modules/doc/landing/landing.stories.ts +0 -21
  90. package/src/modules/doc/landing/landing.ts +0 -222
  91. package/src/modules/doc/nav/__tests__/mockAvailableLanguages.ts +0 -8
  92. package/src/modules/doc/nav/__tests__/mockAvailableVersions.ts +0 -122
  93. package/src/modules/doc/nav/__tests__/mockPageReference.ts +0 -8
  94. package/src/modules/doc/nav/__tests__/mockPdfUrl.ts +0 -1
  95. package/src/modules/doc/nav/__tests__/mockSelectedLanguage.ts +0 -8
  96. package/src/modules/doc/nav/__tests__/mockSelectedVersion.ts +0 -8
  97. package/src/modules/doc/nav/__tests__/mockToc.ts +0 -146
  98. package/src/modules/doc/nav/__tests__/nav.test.ts +0 -58
  99. package/src/modules/doc/toc/__tests__/mockPageReference.ts +0 -8
  100. package/src/modules/doc/toc/__tests__/mockToc.ts +0 -146
  101. package/src/modules/doc/toc/__tests__/toc.test.ts +0 -29
  102. package/src/modules/doc/toolbar/__tests__/mockAvailableLanguages.ts +0 -8
  103. package/src/modules/doc/toolbar/__tests__/mockAvailableVersions.ts +0 -122
  104. package/src/modules/doc/toolbar/__tests__/mockPdfUrl.ts +0 -1
  105. package/src/modules/doc/toolbar/__tests__/mockSelectedLanguage.ts +0 -8
  106. package/src/modules/doc/toolbar/__tests__/mockSelectedVersion.ts +0 -8
  107. package/src/modules/doc/toolbar/__tests__/toolbar.test.ts +0 -44
@@ -0,0 +1,677 @@
1
+ import { api, track } from "lwc";
2
+ import { normalizeBoolean } from "dxUtils/normalizers";
3
+ import { FetchContent } from "./utils";
4
+ import {
5
+ CoveoAdvancedQueryXMLConfig,
6
+ DocLanguage,
7
+ DocVersion,
8
+ TreeNode,
9
+ Header,
10
+ HistoryState,
11
+ PageReference,
12
+ TocMap
13
+ } from "./types";
14
+ import { SearchSyncer } from "docUtils/SearchSyncer";
15
+ import { LightningElementWithState } from "docBaseElements/lightningElementWithState";
16
+ import { Breadcrumb, Language } from "typings/custom";
17
+
18
+ // TODO: Imitating from actual implementation as doc-content use it like this. We should refactor it later.
19
+ const handleContentError = (error): void => console.log(error);
20
+
21
+ const PIXEL_PER_CHARACTER_MAP: { [key: string]: number } = {
22
+ default: 7.7,
23
+ "ja-jp": 12.5
24
+ };
25
+
26
+ export default class DocXmlContent extends LightningElementWithState<{
27
+ isFetchingDocument: boolean;
28
+ isFetchingContent: boolean;
29
+ lastHighlightedSearch: string;
30
+ }> {
31
+ @api apiDomain = "https://developer.salesforce.com";
32
+ @api coveoOrganizationId!: string;
33
+ @api coveoPublicAccessToken!: string;
34
+
35
+ @api
36
+ get allLanguages(): Array<Language> {
37
+ return this._allLanguages;
38
+ }
39
+
40
+ set allLanguages(value: string) {
41
+ if (value) {
42
+ this._allLanguages = JSON.parse(value);
43
+ }
44
+ }
45
+
46
+ @api
47
+ get enableCoveo() {
48
+ return this._enableCoveo;
49
+ }
50
+
51
+ set enableCoveo(value) {
52
+ this._enableCoveo = normalizeBoolean(value);
53
+ }
54
+
55
+ private availableLanguages: Array<DocLanguage> = [];
56
+ private availableVersions: Array<DocVersion> = [];
57
+ private contentProvider: FetchContent;
58
+ private docContent = "";
59
+ private language: DocLanguage = null;
60
+ private loaded = false;
61
+ private pdfUrl = "";
62
+ private tocMap: TocMap = {};
63
+ private sidebarContent: Array<TreeNode> = null;
64
+ private version: DocVersion = null;
65
+ private docTitle = "";
66
+ private analyticsEvent = "custEv_ctaLinkClick";
67
+ private _pathName = "";
68
+ private _pageHeader?: Header;
69
+ private listenerAttached = false;
70
+ private _enableCoveo?: boolean = false;
71
+
72
+ private searchSyncer = new SearchSyncer({
73
+ callbacks: {
74
+ onSearchChange: (nextSearchString: string): void => {
75
+ if (nextSearchString !== this.pageReference.search) {
76
+ this.updatePageReference({
77
+ ...this.pageReference,
78
+ search: nextSearchString
79
+ });
80
+ }
81
+ if (nextSearchString !== this.state.lastHighlightedSearch) {
82
+ this.updateHighlighting(
83
+ new URLSearchParams(nextSearchString).get("q") || ""
84
+ );
85
+ }
86
+ },
87
+ onUrlChange: (nextSearchString: string): void => {
88
+ if (nextSearchString !== this.pageReference.search) {
89
+ this.updatePageReference({
90
+ ...this.pageReference,
91
+ search: nextSearchString
92
+ });
93
+ }
94
+ const nextSearchParam =
95
+ new URLSearchParams(nextSearchString).get("q") || "";
96
+ this.updateSearchInput(nextSearchParam);
97
+ if (nextSearchString !== this.state.lastHighlightedSearch) {
98
+ this.updateHighlighting(nextSearchParam);
99
+ }
100
+ }
101
+ },
102
+ eventName: "sidebarsearchchange",
103
+ historyMethod: window.history.pushState,
104
+ searchParam: "q",
105
+ shouldStopPropagation: true,
106
+ target: window
107
+ });
108
+ private _allLanguages: Array<Language> = [];
109
+
110
+ @track private pageReference: PageReference = {};
111
+ @track breadcrumbs: Array<Breadcrumb> = [];
112
+
113
+ constructor() {
114
+ super();
115
+ this.pageReference = this.getReferenceFromUrl();
116
+ // In order to prevent dispatching unnecessary highlight changes, we
117
+ // track these items and use their previous values for comparisons in
118
+ // `renderedCallback`:
119
+ this.state = {
120
+ isFetchingContent: false,
121
+ isFetchingDocument: false,
122
+ lastHighlightedSearch: "",
123
+ internalLinkClicked: false
124
+ };
125
+ }
126
+
127
+ connectedCallback(): void {
128
+ if (!this.pageReference?.deliverable) {
129
+ window.location.href = "/docs";
130
+ return;
131
+ }
132
+ this.contentProvider = new FetchContent(
133
+ this.apiDomain,
134
+ this.allLanguages
135
+ );
136
+ this.fetchDocument().then(() => (this.loaded = true));
137
+ window.addEventListener("popstate", this.handlePopState);
138
+
139
+ this.searchSyncer.init();
140
+ }
141
+
142
+ renderedCallback(): void {
143
+ this.setState({ internalLinkClicked: true });
144
+ const urlSectionLink =
145
+ this.pageReference?.hash?.split("#").length > 1
146
+ ? this.pageReference.hash.split("#")[1]
147
+ : this.pageReference?.hash;
148
+
149
+ const contentEl = this.template.querySelector("doc-content");
150
+ const anchorEl =
151
+ urlSectionLink &&
152
+ (contentEl?.shadowRoot?.querySelector(`[id='${urlSectionLink}']`) ||
153
+ contentEl?.shadowRoot?.querySelector(
154
+ `[name='${urlSectionLink}']`
155
+ ));
156
+
157
+ if (anchorEl) {
158
+ anchorEl.scrollIntoView();
159
+ this.setState({ internalLinkClicked: false });
160
+ }
161
+
162
+ if (
163
+ (this.prevState.isFetchingContent &&
164
+ !this.state.isFetchingContent) ||
165
+ (this.prevState.isFetchingDocument &&
166
+ !this.state.isFetchingDocument)
167
+ ) {
168
+ const prevSearchParam =
169
+ new URLSearchParams(this.state.lastHighlightedSearch).get(
170
+ "q"
171
+ ) || "";
172
+ const nextSearchParam =
173
+ new URLSearchParams(this.pageReference.search).get("q") || "";
174
+ this.updateHighlighting(nextSearchParam);
175
+ if (prevSearchParam !== nextSearchParam) {
176
+ this.updateSearchInput(nextSearchParam);
177
+ }
178
+ }
179
+ }
180
+
181
+ disconnectedCallback(): void {
182
+ window.removeEventListener("popstate", this.handlePopState);
183
+ this.searchSyncer.dispose();
184
+ if (this.listenerAttached) {
185
+ this.pageHeader.removeEventListener(
186
+ "langchange",
187
+ this.handleLanguageChange
188
+ );
189
+ this.listenerAttached = false;
190
+ }
191
+ }
192
+
193
+ private get languageId(): string {
194
+ return this.language.id.replace("-", "_");
195
+ }
196
+
197
+ private get releaseVersionId(): string {
198
+ return this.version.id;
199
+ }
200
+
201
+ private get deliverable(): string {
202
+ return this.pageReference.deliverable;
203
+ }
204
+
205
+ private get useOldSidebar(): boolean {
206
+ // Coveo is enabled and the version is greater than 51 (within the latest 3 versions)
207
+ // TODO: we need a better fix for version number check
208
+ return !(
209
+ this.enableCoveo &&
210
+ this.coveoOrganizationId &&
211
+ this.coveoPublicAccessToken &&
212
+ (!this.version?.releaseVersion ||
213
+ (this.version?.releaseVersion &&
214
+ parseInt(
215
+ this.version.releaseVersion.replace("v", ""),
216
+ 10
217
+ ) >= 53))
218
+ );
219
+ }
220
+
221
+ private get coveoAdvancedQueryConfig(): CoveoAdvancedQueryXMLConfig {
222
+ const config: { locale: string; topicid: string; version?: string } = {
223
+ locale: this.languageId,
224
+ topicid: this.deliverable
225
+ };
226
+
227
+ if (this.releaseVersionId && this.releaseVersionId !== "noversion") {
228
+ config.version = this.releaseVersionId;
229
+ }
230
+
231
+ return config;
232
+ }
233
+
234
+ private get pageHeader(): Header {
235
+ if (!this._pageHeader) {
236
+ this._pageHeader = document.querySelector("doc-header");
237
+ }
238
+
239
+ return this._pageHeader;
240
+ }
241
+
242
+ private get sidebarValue(): string {
243
+ if (this.pageReference?.contentDocumentId) {
244
+ const hashedUri = `${
245
+ this.pageReference.contentDocumentId
246
+ }${this.normalizeHash(this.pageReference.hash)}`;
247
+ if (hashedUri in this.tocMap) {
248
+ return hashedUri;
249
+ }
250
+
251
+ if (this.pageReference.contentDocumentId in this.tocMap) {
252
+ return this.pageReference.contentDocumentId;
253
+ }
254
+ }
255
+
256
+ return "";
257
+ }
258
+
259
+ private get disableVersion(): boolean {
260
+ return !this.availableVersions || this.availableVersions.length <= 1;
261
+ }
262
+
263
+ private get versionOptions(): Array<DocVersion> {
264
+ return this.disableVersion
265
+ ? this.availableVersions
266
+ : this.availableVersions.map((version) => ({
267
+ ...version,
268
+ link: {
269
+ href: this.pageReferenceToString({
270
+ ...this.pageReference,
271
+ docId: version.url
272
+ })
273
+ }
274
+ }));
275
+ }
276
+
277
+ private get breadcrumbPixelPerCharacter() {
278
+ return (
279
+ PIXEL_PER_CHARACTER_MAP[this.language.id] ||
280
+ PIXEL_PER_CHARACTER_MAP.default
281
+ );
282
+ }
283
+
284
+ private handlePopState = (): void =>
285
+ this.updatePageReference(this.getReferenceFromUrl());
286
+
287
+ handleSelect(event: CustomEvent<{ name: string }>): void {
288
+ event.stopPropagation();
289
+ const { name } = event.detail;
290
+
291
+ if (this.sidebarValue === name) {
292
+ return;
293
+ }
294
+
295
+ if (name) {
296
+ const hashIndex = name.indexOf("#");
297
+ const hash = hashIndex > -1 ? name.slice(hashIndex) : "";
298
+
299
+ const contentDocumentId =
300
+ hashIndex > -1 ? name.slice(0, hashIndex) : name;
301
+ this.updatePageReference({
302
+ ...this.pageReference,
303
+ contentDocumentId,
304
+ hash
305
+ });
306
+ this.updateUrl();
307
+ }
308
+ }
309
+
310
+ handleNavClick(event: CustomEvent<{ pageReference: PageReference }>): void {
311
+ event.stopPropagation();
312
+ const { pageReference } = event.detail;
313
+ this.updatePageReference(pageReference);
314
+ this.updateUrl();
315
+ }
316
+
317
+ handleLanguageChange = (event: CustomEvent<string>): Promise<void> => {
318
+ if (this.language && this.language.id === event.detail) {
319
+ return;
320
+ }
321
+
322
+ this.language = this.availableLanguages.find(
323
+ ({ id }) => id === event.detail
324
+ );
325
+ this.pageReference.docId = this.language.url;
326
+ this.updateUrl();
327
+ this.fetchDocument();
328
+ };
329
+
330
+ updatePageReference(newPageReference: PageReference): void {
331
+ this.pageReference.hash = newPageReference.hash;
332
+ this.pageReference.search = newPageReference.search;
333
+
334
+ if (this.isSamePage(newPageReference)) {
335
+ return;
336
+ }
337
+
338
+ const isSameDocId = this.pageReference.docId === newPageReference.docId;
339
+ this.pageReference = newPageReference;
340
+
341
+ if (!isSameDocId) {
342
+ this.fetchDocument();
343
+ return;
344
+ }
345
+
346
+ this.fetchContent()
347
+ .then(() => this.buildBreadcrumbs())
348
+ .catch(handleContentError);
349
+ }
350
+
351
+ getReferenceFromUrl(): PageReference {
352
+ const [page, docId, deliverable, contentDocumentId] =
353
+ window.location.pathname.substr(1).split("/");
354
+
355
+ const { origin: domain, hash, search } = window.location;
356
+
357
+ return {
358
+ contentDocumentId,
359
+ deliverable,
360
+ docId,
361
+ domain,
362
+ hash,
363
+ page,
364
+ search
365
+ };
366
+ }
367
+
368
+ isSamePage(reference: PageReference): boolean {
369
+ return (
370
+ this.pageReference.contentDocumentId ===
371
+ reference.contentDocumentId &&
372
+ this.pageReference.docId === reference.docId &&
373
+ this.pageReference.page === reference.page &&
374
+ this.pageReference.deliverable === reference.deliverable
375
+ );
376
+ }
377
+
378
+ async fetchDocument(): Promise<void> {
379
+ this.setState({
380
+ isFetchingDocument: true
381
+ });
382
+ const data = await this.contentProvider.fetchDocumentData(
383
+ this.pageReference.docId
384
+ );
385
+
386
+ // This could be a 404 scenario.
387
+ if (!data) {
388
+ this.setState({
389
+ isFetchingDocument: false
390
+ });
391
+ return;
392
+ }
393
+
394
+ this.docTitle = data.docTitle;
395
+ this.tocMap = data.tocMap;
396
+ this.sidebarContent = data.toc;
397
+ this.version = data.version;
398
+ this.language = data.language;
399
+ this.availableLanguages = data.availableLanguages;
400
+ this.availableVersions = data.availableVersions;
401
+ this.pdfUrl = data.pdfUrl;
402
+
403
+ this.updateHeader();
404
+
405
+ this.buildBreadcrumbs();
406
+
407
+ if (this.pageReference.deliverable !== data.deliverable) {
408
+ this.pageReference.deliverable = data.deliverable;
409
+ this.updateUrl(HistoryState.REPLACE_STATE);
410
+ }
411
+
412
+ if (
413
+ this.pageReference?.contentDocumentId?.replace(/\.htm$/, "") !==
414
+ data.id
415
+ ) {
416
+ try {
417
+ await this.fetchContent();
418
+ this.setState({
419
+ isFetchingDocument: false
420
+ });
421
+ return;
422
+ } catch (error) {
423
+ this.pageReference.contentDocumentId = `${data.id}.htm`;
424
+ this.pageReference.hash = "";
425
+ this.pageReference.search = "";
426
+ this.updateUrl(HistoryState.REPLACE_STATE);
427
+ }
428
+ }
429
+
430
+ this.docContent = data.content;
431
+ this.addMetatags();
432
+ this.setState({
433
+ isFetchingDocument: false
434
+ });
435
+ }
436
+
437
+ async fetchContent(): Promise<void> {
438
+ this.setState({
439
+ isFetchingContent: true
440
+ });
441
+ const data = await this.contentProvider.fetchContent(
442
+ this.pageReference.deliverable,
443
+ this.pageReference.contentDocumentId,
444
+ {
445
+ language: this.language.id,
446
+ version: this.version.id
447
+ }
448
+ );
449
+
450
+ if (data) {
451
+ this.docContent = data.content;
452
+ this.addMetatags();
453
+
454
+ if (!this.pageReference.hash) {
455
+ document.querySelector("main")?.scrollIntoView({
456
+ behavior: "smooth",
457
+ block: "start",
458
+ inline: "nearest"
459
+ });
460
+ }
461
+ }
462
+ this.setState({
463
+ isFetchingContent: false
464
+ });
465
+ }
466
+
467
+ updateHeader(): void {
468
+ if (!this.pageHeader) {
469
+ return;
470
+ }
471
+
472
+ if (this.docTitle) {
473
+ this.pageHeader.subtitle = this.docTitle;
474
+ }
475
+
476
+ if (this.pdfUrl) {
477
+ this.pageHeader.bailHref = this.pdfUrl;
478
+ this.pageHeader.bailLabel = "PDF";
479
+ }
480
+
481
+ if (!this.listenerAttached) {
482
+ this.pageHeader.addEventListener(
483
+ "langchange",
484
+ this.handleLanguageChange
485
+ );
486
+ this.listenerAttached = true;
487
+ }
488
+
489
+ this.pageHeader.languages = this.availableLanguages;
490
+ this.pageHeader.language = this.language?.id;
491
+
492
+ if (this.pageReference) {
493
+ const { docId, deliverable, page } = this.pageReference;
494
+ this.pageHeader.headerHref = `/${page}/${docId}/${deliverable}/`;
495
+ }
496
+ }
497
+
498
+ updateUrl(method = HistoryState.PUSH_STATE): void {
499
+ window.history[method](
500
+ {},
501
+ "docs",
502
+ this.pageReferenceToString(this.pageReference)
503
+ );
504
+ }
505
+
506
+ private updateHighlighting(searchParam: string): void {
507
+ this.dispatchHighlightChange(searchParam);
508
+ this.setState({
509
+ lastHighlightedSearch: this.pageReference.search
510
+ });
511
+ }
512
+
513
+ private updateSearchInput(searchParam: string): void {
514
+ this.template
515
+ .querySelector("doc-content-layout")
516
+ ?.setSidebarInputValue(searchParam);
517
+ }
518
+
519
+ private pageReferenceToString(reference: PageReference): string {
520
+ const { page, docId, deliverable, contentDocumentId, hash, search } =
521
+ reference;
522
+ return `/${page}/${docId}/${deliverable}/${contentDocumentId}${this.normalizeSearch(
523
+ search
524
+ )}${this.normalizeHash(hash)}`;
525
+ }
526
+
527
+ private normalizeUrlPart(part: string, sentinel: string): string {
528
+ return (
529
+ (part &&
530
+ (part.startsWith(sentinel) ? part : `${sentinel}${part}`)) ||
531
+ ""
532
+ );
533
+ }
534
+
535
+ private normalizeSearch(search: string): string {
536
+ return this.normalizeUrlPart(search, "?");
537
+ }
538
+
539
+ private normalizeHash(hash: string): string {
540
+ return this.normalizeUrlPart(hash, "#");
541
+ }
542
+
543
+ private getComposedTitle(
544
+ topicTitle: string | undefined,
545
+ docTitle: string | undefined
546
+ ): string {
547
+ // map to avoid duplicates
548
+ const titleMap = {};
549
+ if (topicTitle) {
550
+ // sometimes the h1 tag text (which is docSubTitle) contains text with new line character. For e.g, "Bulk API 2.0 Older\n Documentation",
551
+ // here it contains \n in the text context which needs to be removed
552
+ // also, there are multiple spaces in between the text, which needs to be replaced with a single space
553
+ const docTopicTitle = topicTitle
554
+ .replace(/[\t\r\n]/g, " ")
555
+ .replace(/ +/g, " ")
556
+ .trim();
557
+ titleMap[docTopicTitle] = true;
558
+ }
559
+
560
+ if (docTitle) {
561
+ titleMap[docTitle] = true;
562
+ }
563
+
564
+ titleMap["Salesforce Developers"] = true;
565
+
566
+ return Object.keys(titleMap).join(" | ");
567
+ }
568
+
569
+ private dispatchHighlightChange(term: string): void {
570
+ this.dispatchEvent(
571
+ new CustomEvent("highlightedtermchange", {
572
+ detail: term,
573
+ bubbles: true,
574
+ composed: true
575
+ })
576
+ );
577
+ }
578
+
579
+ get showBreadcrumbs(): boolean {
580
+ return this.breadcrumbs && this.breadcrumbs.length > 1;
581
+ }
582
+
583
+ get docContentStyle(): string {
584
+ return this.showBreadcrumbs ? "" : "margin-top: 48px";
585
+ }
586
+
587
+ private buildBreadcrumbs(): void {
588
+ const { contentDocumentId } = this.pageReference;
589
+ if (!contentDocumentId) {
590
+ return;
591
+ }
592
+
593
+ const currentNode = this.tocMap[contentDocumentId];
594
+
595
+ if (currentNode?.parent) {
596
+ this.breadcrumbs = this.nodeToBreadcrumb(currentNode);
597
+ } else {
598
+ this.breadcrumbs = [];
599
+ }
600
+ }
601
+
602
+ private nodeToBreadcrumb(node: TreeNode): Breadcrumb[] {
603
+ const item = {
604
+ href: this.pageReferenceToString({
605
+ ...this.pageReference,
606
+ contentDocumentId: node.name
607
+ }),
608
+ label: node.label
609
+ };
610
+
611
+ if (node.parent) {
612
+ return [...this.nodeToBreadcrumb(node.parent), item];
613
+ }
614
+
615
+ return [item];
616
+ }
617
+
618
+ // This method take docId and drops the version from the docId.
619
+ // Example:
620
+ // Takes input string: docId = "atlas.en-us.238.0.b2b_b2c_comm_dev.meta"
621
+ // Output string: filteredDocId = "atlas.en-us.b2b_b2c_comm_dev.meta"
622
+ dropVersionFromDocId(docId: string): string {
623
+ if (!this.version?.id) {
624
+ return docId;
625
+ }
626
+
627
+ const curVersion = this.version.id + ".";
628
+ const filteredDocId = docId.replace(curVersion, "");
629
+ return filteredDocId;
630
+ }
631
+
632
+ addMetatags(): void {
633
+ const div = document.createElement("div");
634
+ div.innerHTML = this.docContent;
635
+ const docDescription = div.querySelector(".shortdesc")?.textContent;
636
+ const topicTitle = div.querySelector("h1")?.textContent;
637
+
638
+ const title = document.querySelector("title");
639
+ const composedTitle = this.getComposedTitle(topicTitle, this.docTitle);
640
+
641
+ if (title) {
642
+ title.textContent = composedTitle;
643
+ }
644
+ const metatitle = document.querySelector('meta[name="title"]');
645
+ if (metatitle) {
646
+ metatitle.setAttribute("content", composedTitle);
647
+ }
648
+
649
+ if (docDescription) {
650
+ const metadescription = document.querySelector(
651
+ 'meta[name="description"]'
652
+ );
653
+ if (metadescription) {
654
+ metadescription.setAttribute("content", docDescription);
655
+ }
656
+ }
657
+
658
+ if (this.pageReference) {
659
+ const metadescription = document.querySelector(
660
+ 'link[rel="canonical"]'
661
+ );
662
+ if (metadescription) {
663
+ const copyPageReference = { ...this.pageReference };
664
+ copyPageReference.docId = copyPageReference.docId
665
+ ? this.dropVersionFromDocId(copyPageReference.docId)
666
+ : copyPageReference.docId;
667
+ metadescription.setAttribute(
668
+ "href",
669
+ window.location.protocol +
670
+ "//" +
671
+ window.location.host +
672
+ this.pageReferenceToString(copyPageReference)
673
+ );
674
+ }
675
+ }
676
+ }
677
+ }