@salesforcedevs/arch-components 1.27.21 → 1.27.22

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.
@@ -1,169 +0,0 @@
1
- import { api, LightningElement, track } from 'lwc';
2
- import { createImageUrl, sendInteractionEvent, InteractionEventTypes } from 'arch/helpers';
3
-
4
- const ARCHITECT_BASE_REGEX = /^https?:\/\/([\w-]*\.)?architect/; // matches roughly any subdomain of architect
5
-
6
- export default class extends LightningElement {
7
- private observer!: MutationObserver;
8
- private didSetContent = false;
9
-
10
- @api socialTitle: string | null = null;
11
- @api lastUpdated: string | null = null;
12
- @api readingTime: string | null = null;
13
- @api navHidden: string | null = null;
14
- @api imageHash: string | null = null;
15
-
16
- @track private headings: {
17
- href: string;
18
- id: string;
19
- label: string;
20
- }[] = [];
21
-
22
- private get showHeadingsNav() {
23
- return this.navHidden === null && this.headings.length > 0;
24
- }
25
-
26
- private get rootClassName() {
27
- return 'root';
28
- }
29
-
30
- connectedCallback() {
31
- this.observer = new MutationObserver(() => {
32
- this.setContent();
33
- });
34
- this.observer.observe(this.template.host, {
35
- characterData: true,
36
- childList: true,
37
- subtree: true
38
- });
39
- this.setContent();
40
- const that = this;
41
- window.addEventListener('hashchange', function locationHashChanged() {
42
- const heading: HTMLElement | null = that.template.querySelector(`[id="${window.location.hash.substring(1)}"]`);
43
- if (heading) {
44
- heading.style.scrollMarginTop = `var(--dx-g-global-header-height)`; // required adjustment for global nav
45
- heading.scrollIntoView({
46
- behavior: 'instant', // we can't use smooth here anymore, because the global nav MFE resizes itself and messes up scrolling
47
- block: "start",
48
- });
49
- }
50
- });
51
- }
52
-
53
- disconnectedCallback() {
54
- this.observer.disconnect();
55
- }
56
-
57
- renderedCallback() {
58
- if (!this.didSetContent) {
59
- this.didSetContent = true;
60
- this.setContent();
61
- if (window.location.hash) {
62
- this.scrollToHash(window.location.hash);
63
- }
64
- }
65
- }
66
-
67
- private onHeadingClick(e: MouseEvent) {
68
- const anchor = e.currentTarget as HTMLAnchorElement;
69
- this.scrollToHash(anchor.hash);
70
- }
71
-
72
- private onContentClick(e: MouseEvent) {
73
- const element = e.target as HTMLElement;
74
- const targetIsImage =
75
- element instanceof HTMLImageElement &&
76
- element.parentElement instanceof HTMLAnchorElement;
77
-
78
- if (element instanceof HTMLAnchorElement || targetIsImage) {
79
- const anchorEl = targetIsImage ? element.parentElement : element;
80
- const url = new URL(
81
- anchorEl.href || element.src,
82
- window.location.protocol + '//' + window.location.host
83
- );
84
- const filename = url.pathname.substr(
85
- url.pathname.lastIndexOf('/') + 1
86
- );
87
-
88
- if (anchorEl.hasAttribute('download') || filename.includes('.')) {
89
- const regexImg = new RegExp(/[^\s]+(.*?).(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF|webp)$/);
90
- if (regexImg.test(filename)) {
91
- sendInteractionEvent('Image Click', InteractionEventTypes.IMAGE_CLICK, e, element)
92
- } else {
93
- sendInteractionEvent('File Download', InteractionEventTypes.FILE_DOWNLOAD, e, anchorEl)
94
- }
95
- } else if (anchorEl.target) {
96
- sendInteractionEvent('Link Click', InteractionEventTypes.CLICK, e, anchorEl);
97
- }
98
- }
99
- }
100
-
101
- private scrollToHash(hash: string) {
102
- const heading: HTMLElement | null = this.template.querySelector(`[id="${hash.split('?')[0].substring(1)}"]`);
103
- if (heading) {
104
- heading.style.scrollMarginTop = `var(--dx-g-global-header-height)`; // required adjustment for global nav
105
- heading.scrollIntoView({
106
- behavior: "instant",
107
- block: "start",
108
- });
109
- }
110
- }
111
-
112
- private setContent() {
113
- const main = this.template.querySelector('main');
114
- if (main) {
115
- main.innerHTML = this.template.host.innerHTML.replace(
116
- /(<table>[\s\S]+?<\/table>)/g,
117
- `<div class="table">$1</div>`
118
- );
119
- this.headings = Array.from(main.querySelectorAll('h2,h3')).map(
120
- (n) => {
121
- const label = n.innerText.trim();
122
- const id = label.replace(/[^a-zA-Z0-9]/g, '_');
123
- const href = `#${id}`;
124
- const style =
125
- n.tagName === 'H3' ? 'padding-left: 1.25rem' : '';
126
- // Side effects in map ftw
127
- n.id = id;
128
- return { href, id, label, style };
129
- }
130
- );
131
- Array.from(main.querySelectorAll('a')).forEach((el) => {
132
- if (
133
- !el.href.startsWith('/') &&
134
- !ARCHITECT_BASE_REGEX.test(el.href) &&
135
- !el.href.startsWith('about:blank#') &&
136
- !el.href.startsWith('http://localhost') &&
137
- !el.href.startsWith('https://sfdc.co') &&
138
- !el.href.startsWith('https://sf-archs.cdn.salesforce-experience.com') &&
139
- !el.href.includes('herokuapp.com')
140
- ) {
141
- const doc = el.ownerDocument;
142
- const img = doc.createElement('img');
143
- img.src = 'https://developer.salesforce.com/ns-assets/new_window.svg';
144
- img.style =
145
- 'display: inline;height: 1.2rem;margin:0 0 0 5px;';
146
- img.alt = 'Open link in new window';
147
- el.appendChild(img);
148
- el.target = '_blank';
149
- el.rel = 'noopener';
150
- }
151
- });
152
- Array.from(main.querySelectorAll('img')).forEach((el) => {
153
- el.src = createImageUrl(el.src, this.imageHash);
154
- });
155
- }
156
- }
157
-
158
- private get showScroll() {
159
- return parseInt(this.readingTime, 10) > 4;
160
- }
161
-
162
- private onScrollClick() {
163
- window.scroll({
164
- top: 0,
165
- left: 0,
166
- behavior: 'smooth'
167
- });
168
- }
169
- }