|
| 1 | +import * as utils from './utils.client'; |
| 2 | + |
| 3 | +/** |
| 4 | + * A web component that generates a table of contents based on the headings in |
| 5 | + * a provided scope element. The generated list updates itself when a heading |
| 6 | + * is is added or removed from the scope |
| 7 | + */ |
| 8 | +class MaTableOfContents extends HTMLElement { |
| 9 | + static observedAttributes = ['scope', 'heading-level']; |
| 10 | + |
| 11 | + #_level: utils.HeadingLevel = '2'; |
| 12 | + #headingListItemMap: utils.HeadingToListItemsMap; |
| 13 | + #headingsInScope: Set<HTMLHeadingElement>; |
| 14 | + #labelHeadings: Set<HTMLHeadingElement>; |
| 15 | + #headingsToIgnore: Set<HTMLHeadingElement>; |
| 16 | + #listElement: HTMLUListElement | HTMLOListElement; |
| 17 | + #listItems: Set<HTMLLIElement>; |
| 18 | + #scopeRoot: HTMLElement; |
| 19 | + #observer: MutationObserver; |
| 20 | + |
| 21 | + /** The id of the scope element */ |
| 22 | + set scope(id: string) { |
| 23 | + if (this.getAttribute('scope') !== id) { |
| 24 | + this.setAttribute('scope-level', id); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + this.#scopeRoot = document.getElementById(id) || document.querySelector('main'); |
| 29 | + |
| 30 | + if (!this.#scopeRoot) throw new Error(`Could not locate scope element by id ${id}`); |
| 31 | + |
| 32 | + if (this.isConnected) { |
| 33 | + this.#observer?.disconnect(); |
| 34 | + this.#observer?.observe(this.#scopeRoot, { childList: true, subtree: true }); |
| 35 | + } |
| 36 | + |
| 37 | + this.update(); |
| 38 | + } |
| 39 | + |
| 40 | + /** The heading level to display in the table of contents */ |
| 41 | + set headingLevel(level: utils.HeadingLevel) { |
| 42 | + if (this.getAttribute('heading-level') !== level) { |
| 43 | + this.setAttribute('heading-level', level); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + this.#_level = level; |
| 48 | + this.update(); |
| 49 | + } |
| 50 | + |
| 51 | + /** A Set of heading elements to ignore in the table of contents */ |
| 52 | + set headingsToIgnore(value: Set<HTMLHeadingElement>) { |
| 53 | + this.#headingsToIgnore = value; |
| 54 | + this.update(); |
| 55 | + } |
| 56 | + get headingsToIgnore() { |
| 57 | + return this.#headingsToIgnore; |
| 58 | + } |
| 59 | + |
| 60 | + constructor() { |
| 61 | + super(); |
| 62 | + this.#observer = this.setupMutationObserver(); |
| 63 | + this.#headingListItemMap = new Map(); |
| 64 | + this.#headingsToIgnore = new Set(); |
| 65 | + } |
| 66 | + |
| 67 | + attributeChangedCallback(name: string, _: string, newValue: string) { |
| 68 | + switch (name) { |
| 69 | + case 'scope': |
| 70 | + this.scope = newValue; |
| 71 | + break; |
| 72 | + case 'heading-level': |
| 73 | + this.headingLevel = newValue as utils.HeadingLevel; |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + connectedCallback() { |
| 79 | + this.#observer.observe(this.#scopeRoot, { childList: true, subtree: true }); |
| 80 | + |
| 81 | + this.#listElement = this.querySelector('ul,ol') || document.createElement('ul'); |
| 82 | + if (!this.querySelector('ul,ol')) { |
| 83 | + this.appendChild(this.#listElement); |
| 84 | + } |
| 85 | + |
| 86 | + this.#labelHeadings = new Set( |
| 87 | + this.#listElement?.['ariaLabelledByElements'].filter((element) => element instanceof HTMLHeadingElement) || [], |
| 88 | + ); |
| 89 | + this.#headingsToIgnore = this.headingsToIgnore.union(this.#labelHeadings); |
| 90 | + this.update(); |
| 91 | + } |
| 92 | + |
| 93 | + disconnectedCallback() { |
| 94 | + this.#observer.disconnect(); |
| 95 | + } |
| 96 | + |
| 97 | + setupMutationObserver = () => { |
| 98 | + const callback = (mutationList: MutationRecord[]) => { |
| 99 | + let headingsAdded = false; |
| 100 | + let headingsRemoved = false; |
| 101 | + |
| 102 | + for (const mutation of mutationList) { |
| 103 | + headingsAdded = Array.from(mutation.addedNodes).some((node) => node instanceof HTMLHeadingElement); |
| 104 | + headingsRemoved = Array.from(mutation.removedNodes).some((node) => node instanceof HTMLHeadingElement); |
| 105 | + } |
| 106 | + |
| 107 | + if (headingsAdded || headingsRemoved) { |
| 108 | + this.update(); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + return new MutationObserver(callback); |
| 113 | + }; |
| 114 | + |
| 115 | + update = () => { |
| 116 | + if (!this.#_level || !this.#scopeRoot || !this.#listElement) return; |
| 117 | + |
| 118 | + // get the current headings and listItems and update the map to reflect the |
| 119 | + // current state |
| 120 | + this.#headingsInScope = utils.getHeadingsInScope(this.#scopeRoot, this.#_level, this.#headingsToIgnore); |
| 121 | + this.#listItems = utils.getListItems(this.querySelector('ul')); |
| 122 | + utils.mapHeadingsToListItems(this.#headingsInScope, this.#listItems, this.#headingListItemMap); |
| 123 | + |
| 124 | + // Loop over each heading in the map and create or update its listItems |
| 125 | + this.#headingListItemMap.forEach((listCollection, heading) => { |
| 126 | + if (heading instanceof utils.MissingHeading) return; |
| 127 | + if (listCollection.length === 0) { |
| 128 | + const li = utils.createListItemsForHeading(heading); |
| 129 | + listCollection.push(li); |
| 130 | + } else { |
| 131 | + listCollection.forEach((listItem) => utils.createListItemsForHeading(heading, listItem)); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + // (re)add the updated listItems |
| 136 | + this.#listElement.replaceChildren(); |
| 137 | + this.#headingsInScope.forEach((heading) => { |
| 138 | + const li = this.#headingListItemMap.get(heading)?.[0]; |
| 139 | + this.#listElement.appendChild(li); |
| 140 | + }); |
| 141 | + |
| 142 | + // If there are no headings in scope, hide the label headings |
| 143 | + if (this.#headingsInScope.size === 0) { |
| 144 | + this.hidden = true; |
| 145 | + this.#labelHeadings.forEach((heading) => (heading.hidden = true)); |
| 146 | + } else { |
| 147 | + this.hidden = null; |
| 148 | + this.#labelHeadings.forEach((heading) => (heading.hidden = null)); |
| 149 | + } |
| 150 | + |
| 151 | + // clean up list items that point to headings that are no longer in scope |
| 152 | + this.#headingListItemMap.keys().forEach((key) => { |
| 153 | + if (key instanceof utils.MissingHeading) { |
| 154 | + this.#headingListItemMap.delete(key); |
| 155 | + } |
| 156 | + }); |
| 157 | + }; |
| 158 | +} |
| 159 | + |
| 160 | +customElements.define('ma-table-of-contents', MaTableOfContents); |
0 commit comments