|
| 1 | +--- |
| 2 | +import type { CxxRevision, Language } from "@src/types"; |
| 3 | +import { getRevisions } from "@src/lib/revision"; |
| 4 | +
|
| 5 | +const pageRevision = Astro.locals.starlightRoute.entry.data.cppdoc?.revision; |
| 6 | +const since = pageRevision?.since as CxxRevision | undefined; |
| 7 | +const until = pageRevision?.until as CxxRevision | undefined; |
| 8 | +
|
| 9 | +// This may look hacky, but we now infer whether the current page is for C or |
| 10 | +// C++ by examining the page's path in the file system. |
| 11 | +const path = Astro.locals.starlightRoute.entry.filePath; |
| 12 | +let language: Language; |
| 13 | +if (pageRevision?.lang) { |
| 14 | + language = pageRevision.lang as Language; |
| 15 | +} else if (path.includes("src/content/docs/cpp/")) { |
| 16 | + language = "C++"; |
| 17 | +} else { |
| 18 | + language = "C"; |
| 19 | +} |
| 20 | +
|
| 21 | +const revisions = getRevisions(language, { since, until }); |
| 22 | +--- |
| 23 | + |
| 24 | +<select id="revision-selector" class="revision-selector"> |
| 25 | + <option value="">Select a revision ...</option> |
| 26 | + {revisions.map((rev) => <option value={rev}>{rev}</option>)} |
| 27 | +</select> |
| 28 | + |
| 29 | +<style> |
| 30 | + .revision-selector { |
| 31 | + font-size: 0.9em; |
| 32 | + } |
| 33 | +</style> |
| 34 | + |
| 35 | +<style is:global> |
| 36 | + .autorev-hidden { |
| 37 | + display: none; |
| 38 | + } |
| 39 | +</style> |
| 40 | + |
| 41 | +<script> |
| 42 | + import { selectedRevision } from "./store"; |
| 43 | + import { isRevisionInRange } from "@src/lib/revision"; |
| 44 | + import type { CxxRevision } from "@src/types"; |
| 45 | + |
| 46 | + const selector = document.getElementById( |
| 47 | + "revision-selector" |
| 48 | + ) as HTMLSelectElement | null; |
| 49 | + if (selector) { |
| 50 | + selector.addEventListener("change", () => { |
| 51 | + if (selector.value === "") { |
| 52 | + // The user selects the "Select a revision ..." option. |
| 53 | + selectedRevision.set(null); |
| 54 | + } else { |
| 55 | + selectedRevision.set(selector.value as CxxRevision); |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + selectedRevision.subscribe((rev) => { |
| 61 | + // Toggle all HTML elements with data-autorev-since and data-autorev-until |
| 62 | + // attributes. |
| 63 | + const elements = document.querySelectorAll<HTMLElement>( |
| 64 | + "[data-autorev-since], [data-autorev-until]" |
| 65 | + ); |
| 66 | + elements.forEach((el) => { |
| 67 | + if (!rev) { |
| 68 | + el.classList.remove("autorev-hidden"); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const since = el.dataset.autorevSince as CxxRevision | undefined; |
| 73 | + const until = el.dataset.autorevUntil as CxxRevision | undefined; |
| 74 | + const inRangeTest = isRevisionInRange(rev, { since, until }); |
| 75 | + if (inRangeTest === undefined || inRangeTest) { |
| 76 | + // An undefined inRangeTest indicates that the revisions associated with |
| 77 | + // the element are of a language different than the page's language. We |
| 78 | + // should always display such elements. |
| 79 | + el.classList.remove("autorev-hidden"); |
| 80 | + } else { |
| 81 | + el.classList.add("autorev-hidden"); |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | +</script> |
0 commit comments