@salesforcedevs/docs-components 0.53.6 → 0.54.0-alpha01

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.
@@ -0,0 +1,94 @@
1
+ import { LightningElement, api } from "lwc";
2
+ import {
3
+ createDocumentationElement,
4
+ createEndpointElement,
5
+ createMethodElement,
6
+ createSecurityElement,
7
+ createSummaryElement,
8
+ createTypeElement
9
+ } from "./utils";
10
+ import type { TopicModel } from "./types";
11
+
12
+ export default class AmfTopic extends LightningElement {
13
+ private _model;
14
+ private amf;
15
+ private type;
16
+
17
+ @api
18
+ get model(): TopicModel {
19
+ return this._model;
20
+ }
21
+
22
+ set model(value: TopicModel) {
23
+ if (
24
+ !this.amf ||
25
+ (value && this._model && value.amf !== this._model?.amf)
26
+ ) {
27
+ this.amf = value && clone(value.amf);
28
+ }
29
+ if (
30
+ !this.type ||
31
+ (value && this._model && value.type !== this._model?.type)
32
+ ) {
33
+ this.type = value && clone(value.type);
34
+ }
35
+
36
+ this._model = value;
37
+ if (this._model) {
38
+ this.update();
39
+ }
40
+ // else { Remove child? No model, seems like no component should be shown. }
41
+ }
42
+
43
+ update(): void {
44
+ const container = this.template.querySelector("div.topic-container");
45
+ const { id } = this.model;
46
+ const type = this.type;
47
+ const amf = this.amf;
48
+ const { parser } = this.model;
49
+ let element;
50
+
51
+ if (type === "summary") {
52
+ element = createSummaryElement(amf);
53
+ } else if (type === "endpoint") {
54
+ const endpointModel = parser.computeEndpointApiModel(amf, id);
55
+ element = createEndpointElement(amf, endpointModel, id);
56
+ } else if (type === "method") {
57
+ const endpointMethodModel = parser.computeEndpointApiMethodModel(
58
+ amf,
59
+ id
60
+ );
61
+ const methodModel = parser.computeMethodApiModel(amf, id);
62
+ element = createMethodElement(
63
+ amf,
64
+ endpointMethodModel,
65
+ methodModel
66
+ );
67
+ } else if (type === "security") {
68
+ const securityModel = parser.computeSecurityApiModel(amf, id);
69
+ element = createSecurityElement(amf, securityModel);
70
+ } else if (type === "type") {
71
+ const mediaTypes = parser.computeApiMediaTypes(amf);
72
+ const typeModel = parser.computeTypeApiModel(amf, id);
73
+ element = createTypeElement(amf, typeModel, mediaTypes);
74
+ } else if (type === "documentation") {
75
+ const docsModel = parser.computeDocsApiModel(amf, id);
76
+ element = createDocumentationElement(amf, docsModel);
77
+ }
78
+
79
+ if (container.firstChild) {
80
+ container.firstChild.remove();
81
+ }
82
+ container.appendChild(element);
83
+ }
84
+ }
85
+
86
+ /**
87
+ * The underlying web components we use from api-console mutate their models we pass in.
88
+ * Since LWC makes them Read Only, we need to copy them before passing to the Web Component.
89
+ * @param value JSON Serializable object to clone.
90
+ * @returns A copy of Value. One that has been serialized and parsed via JSON. (Functions, Regex, etc are not preserved.)
91
+ */
92
+ function clone(value): object {
93
+ return JSON.parse(JSON.stringify(value));
94
+ }
@@ -0,0 +1,54 @@
1
+ import { Json } from "@lwrjs/types";
2
+
3
+ export type ApiSummaryElement = HTMLElement & {
4
+ amf: Json;
5
+ hideToc: boolean;
6
+ titleLevel: string;
7
+ };
8
+
9
+ export type ApiEndpointElement = HTMLElement & {
10
+ amf: Json;
11
+ inlineMethods: boolean;
12
+ noNavigation: boolean;
13
+ selected: string;
14
+ endpoint: Json;
15
+ noTryIt: boolean;
16
+ };
17
+
18
+ export type ApiMethodElement = HTMLElement & {
19
+ amf: Json;
20
+ noNavigation: boolean;
21
+ endpoint: Json;
22
+ method: Json;
23
+ noTryIt: boolean;
24
+ };
25
+
26
+ export type ApiSecurityElement = HTMLElement & {
27
+ amf: Json;
28
+ security: Json;
29
+ };
30
+
31
+ export type ApiTypeElement = HTMLElement & {
32
+ amf: Json;
33
+ type: Json;
34
+ mediaTypes: Json;
35
+ };
36
+
37
+ export type ApiDocElement = HTMLElement & {
38
+ amf: Json;
39
+ shape: Json;
40
+ };
41
+
42
+ export type AmfModel = Json;
43
+
44
+ export interface AmfParser {
45
+ parse(): void;
46
+ parsedModel: any;
47
+ }
48
+
49
+ export interface TopicModel {
50
+ id: string;
51
+ type: string;
52
+ amf: AmfModel;
53
+ parser: AmfParser;
54
+ }
@@ -0,0 +1,117 @@
1
+ import type {
2
+ ApiDocElement,
3
+ ApiEndpointElement,
4
+ ApiMethodElement,
5
+ ApiSecurityElement,
6
+ ApiSummaryElement,
7
+ ApiTypeElement
8
+ } from "./types";
9
+
10
+ import { Json } from "@lwrjs/types";
11
+
12
+ export function createSummaryElement(amf: Json): HTMLElement {
13
+ const summaryElement: ApiSummaryElement = document.createElement(
14
+ "api-summary"
15
+ ) as ApiSummaryElement;
16
+ summaryElement.amf = amf;
17
+ summaryElement.hideToc = true;
18
+ summaryElement.titleLevel = "1";
19
+ summaryElement.setAttribute(
20
+ "exportparts",
21
+ [
22
+ "api-title",
23
+ "api-title-label",
24
+ "api-version",
25
+ "marked-description",
26
+ "info-section",
27
+ "info-inline-desc",
28
+ "license-section",
29
+ "separator",
30
+ "toc"
31
+ ].join(",")
32
+ );
33
+ return summaryElement;
34
+ }
35
+
36
+ /**
37
+ * Creates a `api-endpoint-documentation` web element from web API and current selection.
38
+ */
39
+ export function createEndpointElement(
40
+ amf: Json,
41
+ endpointModel: Json,
42
+ selected: string
43
+ ): HTMLElement {
44
+ const el: ApiEndpointElement = document.createElement(
45
+ "api-endpoint-documentation"
46
+ ) as ApiEndpointElement;
47
+ el.amf = amf;
48
+ el.inlineMethods = false;
49
+ el.noNavigation = true;
50
+ el.selected = selected;
51
+ el.endpoint = endpointModel;
52
+ el.noTryIt = true;
53
+ return el;
54
+ }
55
+
56
+ /**
57
+ * Creates a `api-method-documentation` web element from web API and current selection.
58
+ */
59
+ export function createMethodElement(
60
+ amf: Json,
61
+ endpointMethodModel: Json,
62
+ methodModel: Json
63
+ ): HTMLElement {
64
+ const el: ApiMethodElement = document.createElement(
65
+ "api-method-documentation"
66
+ ) as ApiMethodElement;
67
+ el.amf = amf;
68
+ el.noNavigation = true;
69
+ el.endpoint = endpointMethodModel;
70
+ el.method = methodModel;
71
+ el.noTryIt = true;
72
+ el.setAttribute("renderSecurity", "");
73
+ el.setAttribute("renderCodeSnippets", "");
74
+ return el;
75
+ }
76
+
77
+ /**
78
+ * Creates a documentation element for Security AMF type
79
+ *
80
+ */
81
+ export function createSecurityElement(amf: Json, securityModel: Json): HTMLElement {
82
+ const el: ApiSecurityElement = document.createElement(
83
+ "api-security-documentation"
84
+ ) as ApiSecurityElement;
85
+ el.setAttribute("exportparts", ["security-title"].join(","));
86
+ el.amf = amf;
87
+ el.security = securityModel;
88
+ return el;
89
+ }
90
+
91
+ /**
92
+ * Creates a documentation element for Type AMF type
93
+ *
94
+ * @param {string} amfId Reference ID as passed in from amfConfig
95
+ * @param {string} selected Currently selected `@id`.
96
+ */
97
+ export function createTypeElement(amf: Json, typeModel: Json, mediaTypes: Json): HTMLElement {
98
+ const el: ApiTypeElement = document.createElement("api-type-documentation") as ApiTypeElement;
99
+ el.setAttribute("exportparts", ["type-title"].join(","));
100
+ el.amf = amf;
101
+ el.type = typeModel;
102
+ el.mediaTypes = mediaTypes;
103
+ return el;
104
+ }
105
+
106
+ /**
107
+ * Creates a documentation element for Docs AMF type
108
+ *
109
+ * @param {string} amfId Reference ID as passed in from amfConfig
110
+ * @param {string} selected Currently selected `@id`.
111
+ */
112
+ export function createDocumentationElement(amf: Json, docsModel: Json): HTMLElement {
113
+ const el: ApiDocElement = document.createElement("api-documentation-document") as ApiDocElement;
114
+ el.amf = amf;
115
+ el.shape = docsModel;
116
+ return el;
117
+ }
@@ -0,0 +1,100 @@
1
+ :host {
2
+ --dx-c-content-vertical-spacing: var(--dx-g-spacing-lg);
3
+
4
+ display: block;
5
+ }
6
+
7
+ dx-sidebar,
8
+ dx-sidebar-old {
9
+ --dx-c-sidebar-height: 100%;
10
+ --dx-c-sidebar-vertical-padding: var(--dx-c-content-vertical-spacing);
11
+
12
+ z-index: 5;
13
+ }
14
+
15
+ dx-toc {
16
+ --dx-c-toc-width: unset;
17
+
18
+ height: calc(100% - var(--dx-c-content-vertical-spacing) * 2);
19
+ margin: var(--dx-c-content-vertical-spacing) 0;
20
+ overflow-y: auto;
21
+ }
22
+
23
+ dx-sidebar,
24
+ dx-toc {
25
+ display: block;
26
+ }
27
+
28
+ .content {
29
+ display: flex;
30
+ }
31
+
32
+ .content-body-doc-phase-container {
33
+ flex: 1;
34
+ }
35
+
36
+ .content-body-container {
37
+ display: flex;
38
+ flex-direction: row;
39
+ padding-right: var(--dx-g-page-padding-horizontal);
40
+ }
41
+
42
+ .content-body {
43
+ margin: var(--dx-g-spacing-sm) var(--dx-c-content-vertical-spacing)
44
+ var(--dx-g-spacing-xl);
45
+ flex: 1;
46
+ width: 0;
47
+ }
48
+
49
+ .is-sticky {
50
+ height: 100vh;
51
+ position: sticky;
52
+ top: 0;
53
+ }
54
+
55
+ .right-nav-bar {
56
+ max-width: 275px;
57
+ }
58
+
59
+ dx-breadcrumbs {
60
+ display: block;
61
+ margin-bottom: var(--dx-g-spacing-2xl);
62
+ }
63
+
64
+ @media screen and (max-width: 1024px) {
65
+ .right-nav-bar {
66
+ display: none;
67
+ }
68
+ }
69
+
70
+ @media screen and (max-width: 800px) {
71
+ dx-breadcrumbs {
72
+ display: none;
73
+ }
74
+
75
+ .content-body {
76
+ margin-top: var(--dx-c-content-vertical-spacing);
77
+ }
78
+ }
79
+
80
+ @media screen and (max-width: 768px) {
81
+ .content {
82
+ flex-direction: column;
83
+ }
84
+
85
+ .content-body-container {
86
+ padding-right: 0;
87
+ }
88
+
89
+ .left-nav-bar {
90
+ --dx-c-sidebar-height: 80vh;
91
+
92
+ height: unset;
93
+ z-index: 10;
94
+ }
95
+
96
+ .content-body {
97
+ margin-left: var(--dx-g-spacing-mlg, 20px);
98
+ margin-right: var(--dx-g-spacing-mlg, 20px);
99
+ }
100
+ }
@@ -0,0 +1,55 @@
1
+ <template>
2
+ <div class="content">
3
+ <template if:true={useOldSidebar}>
4
+ <dx-sidebar-old
5
+ class="is-sticky left-nav-bar"
6
+ trees={sidebarContent}
7
+ value={sidebarValue}
8
+ header={sidebarHeader}
9
+ >
10
+ <slot name="sidebar-header" slot="header"></slot>
11
+ </dx-sidebar-old>
12
+ </template>
13
+ <template if:false={useOldSidebar}>
14
+ <dx-sidebar
15
+ class="is-sticky left-nav-bar"
16
+ trees={sidebarContent}
17
+ value={sidebarValue}
18
+ header={sidebarHeader}
19
+ coveo-organization-id={coveoOrganizationId}
20
+ coveo-public-access-token={coveoPublicAccessToken}
21
+ coveo-search-hub={coveoSearchHub}
22
+ coveo-advanced-query-config={coveoAdvancedQueryConfig}
23
+ >
24
+ <slot name="sidebar-header" slot="header"></slot>
25
+ </dx-sidebar>
26
+ </template>
27
+ <div class="content-body-doc-phase-container">
28
+ <slot name="doc-phase"></slot>
29
+ <div class="content-body-container">
30
+ <div class="content-body">
31
+ <dx-breadcrumbs
32
+ if:true={breadcrumbs}
33
+ breadcrumbs={breadcrumbs}
34
+ truncate
35
+ hide-current-location
36
+ ></dx-breadcrumbs>
37
+ <dx-breadcrumbs
38
+ if:false={breadcrumbs}
39
+ pathname={pathname}
40
+ hide-current-location
41
+ ></dx-breadcrumbs>
42
+ <slot onslotchange={onSlotChange}></slot>
43
+ </div>
44
+ <div class="right-nav-bar is-sticky">
45
+ <dx-toc
46
+ if:true={showToc}
47
+ title={tocTitle}
48
+ options={tocOptions}
49
+ value={tocValue}
50
+ ></dx-toc>
51
+ </div>
52
+ </div>
53
+ </div>
54
+ </div>
55
+ </template>
@@ -0,0 +1,242 @@
1
+ import { LightningElement, api, track } from "lwc";
2
+ import { closest } from "kagekiri";
3
+ import { toJson } from "utils/normalizers";
4
+ import { highlightTerms } from "utils/highlight";
5
+ import { SearchSyncer } from "../../utils/SearchSyncer/SearchSyncer";
6
+
7
+ type AnchorMap = { [key: string]: { intersect: boolean; id: string } };
8
+
9
+ const TOC_HEADER_TAG = "DOC-HEADING";
10
+ const HIGHLIGHTABLE_SELECTOR = [
11
+ "p",
12
+ "h1",
13
+ "h2",
14
+ "h3",
15
+ "h4",
16
+ "h5",
17
+ "h6",
18
+ "li",
19
+ "dl",
20
+ "th",
21
+ "td"
22
+ ].join(",");
23
+
24
+ export default class ContentLayout extends LightningElement {
25
+ @api sidebarValue: string;
26
+ @api sidebarHeader: string;
27
+ @api tocTitle: string;
28
+ @api enableSlotChange = false;
29
+ @api coveoOrganizationId!: string;
30
+ @api coveoPublicAccessToken!: string;
31
+ @api coveoSearchHub!: string;
32
+ @api coveoAdvancedQueryConfig!: string;
33
+ @api useOldSidebar?: boolean = false;
34
+
35
+ @api
36
+ get breadcrumbs() {
37
+ return this._breadcrumbs;
38
+ }
39
+
40
+ set breadcrumbs(value) {
41
+ if (value) {
42
+ this._breadcrumbs = toJson(value);
43
+ }
44
+ }
45
+
46
+ @api
47
+ get sidebarContent() {
48
+ return this._sidebarContent;
49
+ }
50
+
51
+ set sidebarContent(value) {
52
+ this._sidebarContent = toJson(value);
53
+ }
54
+
55
+ @api
56
+ get tocOptions() {
57
+ return this._tocOptions;
58
+ }
59
+
60
+ set tocOptions(value) {
61
+ this._tocOptions = toJson(value);
62
+ }
63
+
64
+ @api
65
+ setSidebarInputValue(searchTerm: string): void {
66
+ this.template.querySelector("dx-sidebar")?.setInputValue(searchTerm);
67
+ }
68
+
69
+ @track
70
+ private _sidebarContent: unknown;
71
+
72
+ private _breadcrumbs = null;
73
+
74
+ @track
75
+ private _tocOptions: Array<unknown>;
76
+
77
+ private anchoredElements: AnchorMap = {};
78
+ private lastScrollPosition: number;
79
+ private observer?: IntersectionObserver;
80
+ private searchSyncer = new SearchSyncer({
81
+ callbacks: {
82
+ onUrlChange: (nextSearchString: string): void => {
83
+ this.updateHighlightsAndSearch(nextSearchString);
84
+ },
85
+ onSearchChange: (nextSearchString: string): void => {
86
+ this.dispatchHighlightChange(
87
+ new URLSearchParams(nextSearchString).get("q") || ""
88
+ );
89
+ }
90
+ },
91
+ eventName: "sidebarsearchchange",
92
+ historyMethod: window.history.pushState,
93
+ searchParam: "q",
94
+ shouldStopPropagation: true,
95
+ target: window
96
+ });
97
+ private tocValue?: string = undefined;
98
+
99
+ get showToc(): boolean {
100
+ return this.tocOptions && this.tocOptions.length > 0;
101
+ }
102
+
103
+ // ? This could be a good default for pathname in dx-breadcrumbs. Using this getter for now as a workaround.
104
+ get pathname(): string {
105
+ return window.location.pathname;
106
+ }
107
+
108
+ connectedCallback(): void {
109
+ const hasParentHighlightListener = closest(
110
+ "doc-xml-content",
111
+ this.template.host
112
+ );
113
+ if (!hasParentHighlightListener) {
114
+ window.addEventListener(
115
+ "highlightedtermchange",
116
+ this.updateHighlighted
117
+ );
118
+ this.searchSyncer.init();
119
+ }
120
+ }
121
+
122
+ disconnectedCallback(): void {
123
+ this.disconnectObserver();
124
+ window.removeEventListener(
125
+ "highlightedtermchange",
126
+ this.updateHighlighted
127
+ );
128
+ this.searchSyncer.dispose();
129
+ }
130
+
131
+ updateHighlighted = (event: Event): void =>
132
+ highlightTerms(
133
+ this.querySelectorAll(HIGHLIGHTABLE_SELECTOR),
134
+ (event as CustomEvent<string>).detail
135
+ );
136
+
137
+ onSlotChange(event: Event): void {
138
+ if (!this.enableSlotChange) {
139
+ return;
140
+ }
141
+
142
+ this.disconnectObserver();
143
+ this.observer = new IntersectionObserver((entries) => {
144
+ entries.forEach(
145
+ (entry) =>
146
+ (this.anchoredElements[
147
+ entry.target.getAttribute("id")
148
+ ].intersect = entry.isIntersecting)
149
+ );
150
+ this.calculateActualSection();
151
+ });
152
+
153
+ const anchoredTags = (event.target as HTMLSlotElement)
154
+ .assignedElements()
155
+ .filter(({ tagName }) => tagName === TOC_HEADER_TAG)
156
+ .map((tag) => {
157
+ tag.id = tag.hash;
158
+ return tag;
159
+ });
160
+
161
+ this._tocOptions = anchoredTags.map((tag) => ({
162
+ anchor: `#${tag.hash}`,
163
+ id: tag.id,
164
+ label: tag.title
165
+ }));
166
+
167
+ this.scrollToHash(anchoredTags);
168
+
169
+ anchoredTags.forEach((section) => {
170
+ const id = section.getAttribute("id");
171
+ this.anchoredElements[id] = {
172
+ id,
173
+ intersect: false
174
+ };
175
+ this.observer.observe(section);
176
+ });
177
+ }
178
+
179
+ private disconnectObserver(): void {
180
+ if (this.observer) {
181
+ this.observer.disconnect();
182
+ this.observer = null;
183
+ }
184
+ }
185
+
186
+ private scrollToHash(anchoredTags: Array<Element>): void {
187
+ let { hash } = window.location;
188
+
189
+ if (hash) {
190
+ hash = hash.substr(1);
191
+ const toScrollElement = anchoredTags.find(
192
+ (element) => element.getAttribute("id") === hash
193
+ );
194
+ if (toScrollElement) {
195
+ toScrollElement.scrollIntoView({ behavior: "auto" });
196
+ }
197
+ }
198
+ }
199
+
200
+ private calculateActualSection(): void {
201
+ const currentScrollPosition = document.documentElement.scrollTop;
202
+ const id = Object.keys(this.anchoredElements).find(
203
+ (_id) => this.anchoredElements[_id].intersect
204
+ );
205
+ if (id) {
206
+ this.assignElementId(id);
207
+ } else if (currentScrollPosition < this.lastScrollPosition) {
208
+ // The user has scroll up since last update
209
+ this.assignElementId(this.calculatePreviousElementId());
210
+ }
211
+
212
+ this.lastScrollPosition = currentScrollPosition;
213
+ }
214
+
215
+ private calculatePreviousElementId(): string {
216
+ const keys = Object.keys(this.anchoredElements);
217
+ const currentIndex = keys.findIndex((id) => this.tocValue === id);
218
+
219
+ return currentIndex > 0 ? keys[currentIndex - 1] : undefined;
220
+ }
221
+
222
+ private assignElementId(id: string): void {
223
+ this.tocValue = id;
224
+ }
225
+
226
+ private dispatchHighlightChange(term: string): void {
227
+ this.dispatchEvent(
228
+ new CustomEvent("highlightedtermchange", {
229
+ detail: term,
230
+ bubbles: true,
231
+ composed: true
232
+ })
233
+ );
234
+ }
235
+
236
+ private updateHighlightsAndSearch(nextSearchString: string): void {
237
+ const nextSearchParam =
238
+ new URLSearchParams(nextSearchString).get("q") || "";
239
+ this.setSidebarInputValue(nextSearchParam);
240
+ this.dispatchHighlightChange(nextSearchParam);
241
+ }
242
+ }