@salesforcedevs/docs-components 1.29.0-toolbar-alpha2 → 1.29.0-toolbar-alpha3

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.29.0-toolbar-alpha2",
3
+ "version": "1.29.0-toolbar-alpha3",
4
4
  "description": "Docs Lightning web components for DSC",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
@@ -1,4 +1,4 @@
1
- import { LightningElement } from "lwc";
1
+ import { LightningElement, api } from "lwc";
2
2
  import { track } from "dxUtils/analytics";
3
3
 
4
4
  const DEFAULT_COPY_TOOLTIP_LABEL = "Click to copy";
@@ -11,11 +11,26 @@ const VIEW_MARKDOWN_LABEL = "View as Markdown";
11
11
  const COPY_URL_LABEL = "Copy URL to Markdown";
12
12
 
13
13
  export default class ContentActionToolbar extends LightningElement {
14
+ @api
15
+ get pageUrl(): string | undefined {
16
+ return this._pageUrl;
17
+ }
18
+ set pageUrl(value: string | undefined) {
19
+ if (this._pageUrl === value) {
20
+ return;
21
+ }
22
+ this._pageUrl = value;
23
+ this.markdownUrl = this.deriveMarkdownUrl();
24
+ }
25
+ private _pageUrl?: string;
26
+
14
27
  copyMarkdownLabel: string = DEFAULT_COPY_TOOLTIP_LABEL;
15
28
  copyUrlLabel: string = DEFAULT_COPY_TOOLTIP_LABEL;
16
29
 
17
30
  private copyTooltipResetTimeout: number | null = null;
18
31
 
32
+ private markdownUrl: string | null = this.deriveMarkdownUrl();
33
+
19
34
  get copyMarkdownButtonText(): string {
20
35
  return COPY_MARKDOWN_LABEL;
21
36
  }
@@ -29,8 +44,7 @@ export default class ContentActionToolbar extends LightningElement {
29
44
  }
30
45
 
31
46
  async handleCopyMarkdown(event: Event) {
32
- const markdownUrl = this.getMarkdownUrl();
33
- if (!markdownUrl) {
47
+ if (!this.markdownUrl) {
34
48
  return;
35
49
  }
36
50
 
@@ -38,11 +52,11 @@ export default class ContentActionToolbar extends LightningElement {
38
52
  event,
39
53
  "custEv_linkClick",
40
54
  COPY_MARKDOWN_LABEL,
41
- markdownUrl
55
+ this.markdownUrl
42
56
  );
43
57
 
44
58
  try {
45
- const response = await fetch(markdownUrl);
59
+ const response = await fetch(this.markdownUrl);
46
60
  if (!response.ok) {
47
61
  return;
48
62
  }
@@ -55,8 +69,7 @@ export default class ContentActionToolbar extends LightningElement {
55
69
  }
56
70
 
57
71
  handleViewMarkdown(event: Event) {
58
- const markdownUrl = this.getMarkdownUrl();
59
- if (!markdownUrl) {
72
+ if (!this.markdownUrl) {
60
73
  return;
61
74
  }
62
75
 
@@ -64,15 +77,14 @@ export default class ContentActionToolbar extends LightningElement {
64
77
  event,
65
78
  "custEv_linkClick",
66
79
  VIEW_MARKDOWN_LABEL,
67
- markdownUrl
80
+ this.markdownUrl
68
81
  );
69
82
 
70
- window.open(markdownUrl, "_blank", "noopener,noreferrer");
83
+ window.open(this.markdownUrl, "_blank", "noopener,noreferrer");
71
84
  }
72
85
 
73
86
  async handleCopyUrl(event: Event) {
74
- const markdownUrl = this.getMarkdownUrl();
75
- if (!markdownUrl) {
87
+ if (!this.markdownUrl) {
76
88
  return;
77
89
  }
78
90
 
@@ -80,11 +92,11 @@ export default class ContentActionToolbar extends LightningElement {
80
92
  event,
81
93
  "custEv_linkClick",
82
94
  COPY_URL_LABEL,
83
- markdownUrl
95
+ this.markdownUrl
84
96
  );
85
97
 
86
98
  try {
87
- await navigator.clipboard.writeText(markdownUrl);
99
+ await navigator.clipboard.writeText(this.markdownUrl);
88
100
  this.flashCopied("copyUrlLabel");
89
101
  } catch (error) {
90
102
  console.error(error);
@@ -111,8 +123,9 @@ export default class ContentActionToolbar extends LightningElement {
111
123
  * query string stripped, or `null` when the current page does not end
112
124
  * with `.html`.
113
125
  */
114
- private getMarkdownUrl(): string | null {
115
- const url = new URL(window.location.href);
126
+ private deriveMarkdownUrl(): string | null {
127
+ const sourceHref = this._pageUrl || window.location.href;
128
+ const url = new URL(sourceHref, window.location.href);
116
129
  url.hash = "";
117
130
  url.search = "";
118
131
 
@@ -303,26 +303,22 @@ export default class ContentLayout extends LightningElement {
303
303
  return;
304
304
  }
305
305
 
306
- if (
307
- this.contentActionToolbarElement?.previousElementSibling === heading
308
- ) {
309
- return;
310
- }
306
+ const toolbar = (this.contentActionToolbarElement ??
307
+ createElement(CONTENT_ACTION_TOOLBAR_TAG, {
308
+ is: ContentActionToolbar
309
+ })) as HTMLElement & { pageUrl?: string };
311
310
 
312
- this.removeContentActionToolbar();
311
+ toolbar.pageUrl = this.sidebarValue;
312
+
313
+ if (toolbar.previousElementSibling !== heading) {
314
+ heading.parentNode?.insertBefore(toolbar, heading.nextSibling);
315
+ }
313
316
 
314
- const toolbar = createElement(CONTENT_ACTION_TOOLBAR_TAG, {
315
- is: ContentActionToolbar
316
- }) as unknown as HTMLElement;
317
- heading.parentNode?.insertBefore(toolbar, heading.nextSibling);
318
317
  this.contentActionToolbarElement = toolbar;
319
318
  }
320
319
 
321
320
  protected removeContentActionToolbar(): void {
322
- if (this.contentActionToolbarElement) {
323
- this.contentActionToolbarElement.remove();
324
- this.contentActionToolbarElement = null;
325
- }
321
+ this.contentActionToolbarElement?.remove();
326
322
  }
327
323
 
328
324
  disconnectedCallback(): void {
@@ -342,6 +338,7 @@ export default class ContentLayout extends LightningElement {
342
338
  this.template.removeEventListener("click", this.handleLinkClick);
343
339
 
344
340
  this.removeContentActionToolbar();
341
+ this.contentActionToolbarElement = null;
345
342
  }
346
343
 
347
344
  restoreScroll() {