-
Notifications
You must be signed in to change notification settings - Fork 28
/
TableOfContents.tsx
247 lines (211 loc) · 7.77 KB
/
TableOfContents.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import * as React from 'react';
import { WebPartTitle } from "@pnp/spfx-controls-react/lib/WebPartTitle";
import styles from './TableOfContents.module.scss';
import { ITableOfContentsProps } from './ITableOfContentsProps';
/**
* Describes a link for a header
*/
interface Link {
/**
* The Source html element.
*/
element: HTMLElement | undefined;
/**
* Child nodes for the link.
*/
childNodes: Link[];
/**
* Parent link. Undefined for the root link.
*/
parent: Link | undefined;
}
export default class TableOfContents extends React.Component<ITableOfContentsProps, {}> {
private static timeout = 500;
private static h2Tag = "h2";
private static h3Tag = "h3";
private static h4Tag = "h4";
/**
* Gets a nested list of links based on the list of headers specified.
* @param headers List of HtmlElements for H2, H3, and H4 headers.
*/
private getLinks(headers: HTMLElement[]): Link[] {
// create a root link that will be a root for links' tree
const root: Link = { childNodes: [], parent: undefined, element: undefined };
let prevLink: Link | null = null;
for (let i = 0; i < headers.length; i++) {
const header = headers[i];
const link: Link = { childNodes: [], parent: undefined, element: header };
if (i === 0) {
// the first header is always added as a child of the root
link.parent = root;
root.childNodes.push(link);
} else {
const prevHeader = headers[i - 1];
// compare the current header and the previous one to define where to add new link
const compare = this.compareHeaders(header.tagName, prevHeader.tagName);
if (compare === 0) {
// if headers are on the same level, add header to the same parent
link.parent = prevLink.parent;
prevLink.parent.childNodes.push(link);
} else if (compare < 0) {
let targetParent = prevLink.parent;
// if current header bigger than the previous one, go up in the hierarchy to find a place to add link
// go up in the hierarchy of links until a link with bigger tag is found or until the root link found
// i.e. for H4 look for H3 or H2, for H3 look for H2, for H2 look for the root.
while ((targetParent != root) && (this.compareHeaders(header.tagName, targetParent.element.tagName) <= 0)) {
targetParent = targetParent.parent;
}
link.parent = targetParent;
targetParent.childNodes.push(link);
} else {
// if current header is smaller thab the previous one, add link for it as a child of the previous link
link.parent = prevLink;
prevLink.childNodes.push(link);
}
}
prevLink = link;
}
// return list of links for top-level headers
return root.childNodes;
}
/**
* Compares two header tags by their weights.
* The function is used to compare the size of headers (e.g. should H3 go under H2?)
* @param header1
* @param header2
*/
private compareHeaders(header1: string, header2: string): number {
return this.getHeaderWeight(header1) - this.getHeaderWeight(header2);
}
/**
* Returns a digital weight of a tag. Used for comparing header tags.
* @param header
*/
private getHeaderWeight(header: string): number {
switch (header.toLowerCase()) {
case (TableOfContents.h2Tag):
return 2;
case (TableOfContents.h3Tag):
return 3;
case (TableOfContents.h4Tag):
return 4;
default:
throw new Error('Unknown header: ' + header);
}
}
/**
* Returns html elements in the current page specified by the query selector.
*/
private getHtmlElements(querySelector: string): HTMLElement[] {
if (querySelector.length === 0) {
return [];
} else {
const elements = document.querySelectorAll(querySelector);
const htmlElements: HTMLElement[] = [];
for (let i = 0; i < elements.length; i++) {
htmlElements.push(elements[i] as HTMLElement);
}
return htmlElements;
}
}
/**
* Returns a query selector based on the specified props
* @param props
*/
private getQuerySelector(props: ITableOfContentsProps) {
const queryParts = [];
if (props.showHeading2) {
queryParts.push(TableOfContents.h2Tag);
}
if (props.showHeading3) {
queryParts.push(TableOfContents.h3Tag);
}
if (props.showHeading4) {
queryParts.push(TableOfContents.h4Tag);
}
return queryParts.join(',');
}
/**
* Filters elements with empty text.
* @param element
*/
private filterEmpty(element: HTMLElement): boolean {
return element.innerText.trim() !== '';
}
/**
* Filters elements that are inside <aside> tag and thus not related to a page.
* @param element
*/
private filterAside(element: HTMLElement): boolean {
let inAsideTag = false;
let parentElement = element.parentElement;
while (parentElement) {
if (parentElement.tagName.toLocaleLowerCase() === 'aside') {
inAsideTag = true;
break;
}
parentElement = parentElement.parentElement;
}
return !inAsideTag;
}
/**
* Returns a click handler that scrolls a page to the specified element.
*/
private scrollToHeader = (target: HTMLElement) => {
return (event: React.SyntheticEvent) => {
event.preventDefault();
document.location.hash = target.id;
target.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'nearest' });
};
}
/**
* Creates a list of components to display from a list of links.
* @param links
*/
private renderLinks(links: Link[]): JSX.Element[] {
// for each link render a <li> element with a link. If the link has got childNodes, additionaly render <ul> with child links.
const elements = links.map((link, index) => {
return (
<li key={index}>
<a onClick={this.scrollToHeader(link.element)} href={'#' + link.element.id}>{link.element.innerText}</a>
{link.childNodes.length > 0 ? (<ul>{this.renderLinks(link.childNodes)}</ul>) : ''}
</li>
);
});
return elements;
}
/**
* Force the component to re-render with a specified interval.
* This is needed to get valid id values for headers to use in links. Right after the rendering headers won't have valid ids, they are assigned later once the whole page got rendered.
* The component will display the correct list of headers on the first render and will be able to process clicks (as a link to an HTMLElement is stored by the component).
* Once valid ids got assigned to headers by SharePoint code, the component will get valid ids for headers. This way a link from ToC can be copied by a user and it will be a valid link to a header.
*/
public componentDidMount() {
setInterval(() => {
this.setState({});
}, TableOfContents.timeout);
}
public render(): JSX.Element {
// get headers, then filter out empty and headers from <aside> tags
const querySelector = this.getQuerySelector(this.props);
const headers = this.getHtmlElements(querySelector).filter(this.filterEmpty).filter(this.filterAside);
// create a list of links from headers
const links = this.getLinks(headers);
// create components from a list of links
const toc = (<ul>{this.renderLinks(links)}</ul>);
// add CSS class to hide in mobile view if needed
const hideInMobileViewClass = this.props.hideInMobileView ? (styles.hideInMobileView) : '';
return (
<section className={styles.tableOfContents}>
<div className={hideInMobileViewClass}>
<WebPartTitle displayMode={this.props.displayMode}
title={this.props.title}
updateProperty={this.props.updateProperty} />
<nav>
{toc}
</nav>
</div>
</section>
);
}
}