Repository navigation
Expand file tree
/
Copy pathscrollable-focus.js
More file actions
158 lines (145 loc) · 5.09 KB
/
Copy pathscrollable-focus.js
File metadata and controls
158 lines (145 loc) · 5.09 KB
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
/**
* MkDocs Material accessibility extras:
* - Search overlay (role="dialog") gets an accessible name.
* - Scrollable code blocks and tables: focusable + unique region labels.
* - Code "copy" toolbar nav: unique aria-label per block.
*/
(function () {
var STR = {
en: {
siteSearch: "Site search",
codeActions: "Code block {n} — toolbar (copy to clipboard)",
codeRegion: "Code block {n} (scroll horizontally if needed)",
codeRegionCaption: "{caption} — code block {n}",
tableRegion: "Data table {n} (scroll horizontally if needed)",
tableRegionCaption: "{caption} — table {n}",
},
es: {
siteSearch: "Búsqueda en el sitio",
codeActions: "Bloque de código {n} — barra de acciones (copiar al portapapeles)",
codeRegion: "Bloque de código {n} (desplazar horizontalmente si hace falta)",
codeRegionCaption: "{caption} — bloque de código {n}",
tableRegion: "Tabla de datos {n} (desplazar horizontalmente si hace falta)",
tableRegionCaption: "{caption} — tabla {n}",
},
de: {
siteSearch: "Websuche",
codeActions: "Codeblock {n} — Aktionen (in die Zwischenablage kopieren)",
codeRegion: "Codeblock {n} (bei Bedarf horizontal scrollen)",
codeRegionCaption: "{caption} — Codeblock {n}",
tableRegion: "Datentabelle {n} (bei Bedarf horizontal scrollen)",
tableRegionCaption: "{caption} — Tabelle {n}",
},
fr: {
siteSearch: "Recherche sur le site",
codeActions: "Bloc de code {n} — barre d’actions (copier dans le presse-papiers)",
codeRegion: "Bloc de code {n} (faites défiler horizontalement si besoin)",
codeRegionCaption: "{caption} — bloc de code {n}",
tableRegion: "Tableau de données {n} (faites défiler horizontalement si besoin)",
tableRegionCaption: "{caption} — tableau {n}",
},
};
function locale() {
var l = (document.documentElement.getAttribute("lang") || "en").toLowerCase();
var base = l.split("-")[0];
return STR[base] ? base : "en";
}
function txt(key, replacements) {
var bundle = STR[locale()] || STR.en;
var s = bundle[key] || STR.en[key] || key;
if (replacements) {
Object.keys(replacements).forEach(function (k) {
s = s.split("{" + k + "}").join(replacements[k]);
});
}
return s;
}
function labelFromFigure(el) {
var fig = el.closest("figure");
if (fig) {
var cap = fig.querySelector("figcaption");
if (cap && cap.textContent.trim()) {
return cap.textContent.trim();
}
}
return null;
}
function tableCaptionFromWrapper(el) {
var table = el.querySelector("table");
if (!table) return null;
var cap = table.querySelector("caption");
if (cap && cap.textContent.trim()) return cap.textContent.trim();
return null;
}
function enhanceSearchDialog() {
document.querySelectorAll('.md-search[role="dialog"]').forEach(function (el) {
if (el.getAttribute("aria-labelledby") || el.getAttribute("aria-label")) {
return;
}
el.setAttribute("aria-label", txt("siteSearch"));
});
}
function enhanceCodeNav() {
document.querySelectorAll(".md-code__nav").forEach(function (nav, idx) {
if (nav.getAttribute("aria-label")) return;
nav.setAttribute("aria-label", txt("codeActions", { n: String(idx + 1) }));
});
}
function enhanceHighlights() {
var n = 0;
document.querySelectorAll(".md-typeset .highlight").forEach(function (el) {
if (el.getAttribute("tabindex") !== null) return;
n += 1;
el.setAttribute("tabindex", "0");
el.setAttribute("role", "region");
var cap = labelFromFigure(el);
var label = cap
? txt("codeRegionCaption", { caption: cap, n: String(n) })
: txt("codeRegion", { n: String(n) });
el.setAttribute("aria-label", label);
});
}
function enhanceTables() {
var n = 0;
document.querySelectorAll(".md-typeset__table").forEach(function (el) {
if (el.getAttribute("tabindex") !== null) return;
n += 1;
el.setAttribute("tabindex", "0");
el.setAttribute("role", "region");
var cap = tableCaptionFromWrapper(el);
var label = cap
? txt("tableRegionCaption", { caption: cap, n: String(n) })
: txt("tableRegion", { n: String(n) });
el.setAttribute("aria-label", label);
});
}
function enhance() {
enhanceSearchDialog();
enhanceCodeNav();
enhanceHighlights();
enhanceTables();
}
var instantNavSubscribed = false;
function subscribeInstantNav() {
if (instantNavSubscribed) return;
var bus =
(typeof document$ !== "undefined" && document$) ||
(typeof window !== "undefined" && window.document$);
if (bus && typeof bus.subscribe === "function") {
bus.subscribe(enhance);
instantNavSubscribed = true;
}
}
function setup() {
enhance();
subscribeInstantNav();
if (!instantNavSubscribed) {
setTimeout(subscribeInstantNav, 0);
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", setup);
} else {
setup();
}
})();