forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
583 lines (494 loc) Β· 15.7 KB
/
index.js
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import { isMobile, mobileBreakpoint } from '../util/env.js';
import * as dom from '../util/dom.js';
/** @typedef {import('../Docsify.js').Constructor} Constructor */
/**
* @template {!Constructor} T
* @param {T} Base - The class to extend
*/
export function Events(Base) {
return class Events extends Base {
#intersectionObserver;
#isScrolling;
#title = dom.$.title;
// Initialization
// =========================================================================
/**
* Initialize Docsify events
* One-time setup of listeners, observers, and tasks.
* @void
*/
initEvent() {
const { topMargin } = this.config;
// Apply topMargin to scrolled content
if (topMargin) {
const value =
typeof topMargin === 'number' ? `${topMargin}px` : topMargin;
document.documentElement.style.setProperty(
'--scroll-padding-top',
value,
);
}
this.#initCover();
this.#initSkipToContent();
this.#initSidebar();
this.#initSidebarToggle();
this.#initKeyBindings();
}
// Sub-Initializations
// =========================================================================
/**
* Initialize cover observer
* Toggles sticky behavior when when cover is not in view
* @void
*/
#initCover() {
const coverElm = dom.find('section.cover');
if (!coverElm) {
dom.toggleClass(dom.body, 'add', 'sticky');
return;
}
const observer = new IntersectionObserver(entries => {
const isIntersecting = entries[0].isIntersecting;
const op = isIntersecting ? 'remove' : 'add';
dom.toggleClass(dom.body, op, 'sticky');
});
observer.observe(coverElm);
}
/**
* Initialize heading observer
* Toggles sidebar active item based on top viewport edge intersection
* @void
*/
#initHeadings() {
const headingElms = dom.findAll('#main :where(h1, h2, h3, h4, h5)');
const headingsInView = new Set();
// Mark sidebar active item on heading intersection
this.#intersectionObserver?.disconnect();
this.#intersectionObserver = new IntersectionObserver(
entries => {
if (this.#isScrolling) {
return;
}
for (const entry of entries) {
const op = entry.isIntersecting ? 'add' : 'delete';
headingsInView[op](entry.target);
}
const activeHeading =
headingsInView.size > 1
? // Sort headings by proximity to viewport top and select first
Array.from(headingsInView).sort((a, b) =>
a.compareDocumentPosition(b) &
Node.DOCUMENT_POSITION_FOLLOWING
? -1
: 1,
)[0]
: // Get first and only item in set.
// May be undefined if no headings are in view.
headingsInView.values().next().value;
if (activeHeading) {
const id = activeHeading.getAttribute('id');
const href = this.router.toURL(this.router.getCurrentPath(), {
id,
});
const newSidebarActiveElm = this.#markSidebarActiveElm(href);
newSidebarActiveElm?.scrollIntoView({
behavior: 'instant',
block: 'nearest',
inline: 'nearest',
});
}
},
{
rootMargin: '0% 0% -50% 0%', // Top half of viewport
},
);
headingElms.forEach(elm => {
this.#intersectionObserver.observe(elm);
});
}
/**
* Initialize keyboard bindings
* @void
*/
#initKeyBindings() {
const { keyBindings } = this.config;
const modifierKeys = ['alt', 'ctrl', 'meta', 'shift'];
if (keyBindings && keyBindings.constructor === Object) {
// Prepare key binding configurations
Object.values(keyBindings || []).forEach(bindingConfig => {
const { bindings } = bindingConfig;
if (!bindings) {
return;
}
// Convert bindings to arrays
// Ex: 'alt+t' => ['alt+t']
bindingConfig.bindings = Array.isArray(bindings)
? bindings
: [bindings];
// Convert key sequences to sorted arrays (modifiers first)
// Ex: ['alt+t', 't+ctrl'] => [['alt', 't'], ['ctrl', 't']]
bindingConfig.bindings = bindingConfig.bindings.map(keys => {
const sortedKeys = [[], []]; // Modifier keys, non-modifier keys
if (typeof keys === 'string') {
keys = keys.split('+');
}
keys.forEach(key => {
const isModifierKey = modifierKeys.includes(key);
const targetArray = sortedKeys[isModifierKey ? 0 : 1];
const newKeyValue = key.trim().toLowerCase();
targetArray.push(newKeyValue);
});
sortedKeys.forEach(arr => arr.sort());
return sortedKeys.flat();
});
});
// Handle keyboard events
dom.on('keydown', e => {
const isTextEntry = document.activeElement.matches(
'input, select, textarea',
);
if (isTextEntry) {
return;
}
const bindingConfigs = Object.values(keyBindings || []);
const matchingConfigs = bindingConfigs.filter(
({ bindings }) =>
bindings &&
// bindings: [['alt', 't'], ['ctrl', 't']]
bindings.some(keys =>
// keys: ['alt', 't']
keys.every(
// k: 'alt'
k =>
(modifierKeys.includes(k) && e[k + 'Key']) ||
e.key === k || // Ex: " ", "a"
e.code.toLowerCase() === k || // "space"
e.code.toLowerCase() === `key${k}`, // "keya"
),
),
);
matchingConfigs.forEach(({ callback }) => {
e.preventDefault();
callback(e);
});
});
}
}
/**
* Initialize sidebar event listeners
*
* @void
*/
#initSidebar() {
const sidebarElm = document.querySelector('.sidebar');
if (!sidebarElm) {
return;
}
// Auto-toggle on resolution change
window
?.matchMedia?.(`(max-width: ${mobileBreakpoint})`)
.addEventListener('change', evt => {
this.#toggleSidebar(!evt.matches);
});
// Collapse toggle
dom.on(sidebarElm, 'click', ({ target }) => {
const linkElm = target.closest('a');
const linkParent = linkElm?.closest('li');
const subSidebar = linkParent?.querySelector('.app-sub-sidebar');
if (subSidebar) {
dom.toggleClass(linkParent, 'collapse');
}
});
}
/**
* Initialize sidebar show/hide toggle behavior
*
* @void
*/
#initSidebarToggle() {
const contentElm = dom.find('main > .content');
const toggleElm = dom.find('button.sidebar-toggle');
if (!toggleElm) {
return;
}
let lastContentFocusElm;
// Store last focused content element (restored via #toggleSidebar)
dom.on(contentElm, 'focusin', e => {
const focusAttr = 'data-restore-focus';
lastContentFocusElm?.removeAttribute(focusAttr);
lastContentFocusElm = e.target;
lastContentFocusElm.setAttribute(focusAttr, '');
});
// Toggle sidebar
dom.on(toggleElm, 'click', e => {
e.stopPropagation();
this.#toggleSidebar();
});
}
/**
* Initialize skip to content behavior
*
* @void
*/
#initSkipToContent() {
const skipElm = document.querySelector('#skip-to-content');
if (!skipElm) {
return;
}
skipElm.addEventListener('click', evt => {
const focusElm = this.#focusContent();
evt.preventDefault();
focusElm?.scrollIntoView({
behavior: 'smooth',
});
});
}
// Callbacks
// =========================================================================
/**
* Handle rendering UI element updates and new content
* @void
*/
onRender() {
const currentPath = this.router.toURL(this.router.getCurrentPath());
const currentTitle = dom.find(
`.sidebar a[href='${currentPath}']`,
)?.innerText;
// Update page title
dom.$.title = currentTitle || this.#title;
this.#markAppNavActiveElm();
this.#markSidebarCurrentPage();
this.#initHeadings();
}
/**
* Handle navigation events
*
* @param {undefined|"history"|"navigate"} source Type of navigation where
* undefined is initial load, "history" is forward/back, and "navigate" is
* user click/tap
* @void
*/
onNavigate(source) {
const { auto2top, topMargin } = this.config;
const { path, query } = this.route;
this.#markSidebarActiveElm();
// Note: Scroll position set by browser on forward/back (i.e. "history")
if (source !== 'history') {
// Anchor link
if (query.id) {
const headingElm = dom.find(
`.markdown-section :where(h1, h2, h3, h4, h5)[id="${query.id}"]`,
);
if (headingElm) {
this.#watchNextScroll();
headingElm.scrollIntoView({
behavior: 'smooth',
block: 'start',
});
}
}
// User click/tap
else if (source === 'navigate') {
// Scroll to top
if (auto2top) {
document.scrollingElement.scrollTop = topMargin ?? 0;
}
}
}
// Clicked anchor link
if (path === '/' || (query.id && source === 'navigate')) {
isMobile() && this.#toggleSidebar(false);
}
// Clicked anchor link or page load with anchor ID
if (query.id || source === 'navigate') {
this.#focusContent();
}
}
// Functions
// =========================================================================
/**
* Set focus on the main content area: current route ID, first heading, or
* the main content container
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus
* @param {Object} options HTMLElement focus() method options
* @returns HTMLElement|undefined
* @void
*/
#focusContent(options = {}) {
const settings = {
preventScroll: true,
...options,
};
const { query } = this.route;
const focusEl = query.id
? // Heading ID
dom.find(`#${query.id}`)
: // First heading
dom.find('#main :where(h1, h2, h3, h4, h5, h6)') ||
// Content container
dom.find('#main');
// Move focus to content area
focusEl?.focus(settings);
return focusEl;
}
/**
* Marks the active app nav item
*
* @param {string} [href] Matching element HREF value. If unspecified,
* defaults to the current path (without query params)
* @void
*/
#markAppNavActiveElm() {
const href = decodeURIComponent(this.router.toURL(this.route.path));
['.app-nav', '.app-nav-merged'].forEach(selector => {
const navElm = dom.find(selector);
if (!navElm) {
return;
}
const newActive = dom
.findAll(navElm, 'a')
.sort((a, b) => b.href.length - a.href.length)
.find(
a =>
href.includes(a.getAttribute('href')) ||
href.includes(decodeURI(a.getAttribute('href'))),
)
?.closest('li');
const oldActive = dom.find(navElm, 'li.active');
if (newActive && newActive !== oldActive) {
oldActive?.classList.remove('active');
newActive.classList.add('active');
}
});
}
/**
* Marks the active sidebar item
*
* @param {string} [href] Matching element HREF value. If unspecified,
* defaults to the current path (with query params)
* @returns Element|undefined
*/
#markSidebarActiveElm(href) {
href ??= this.router.toURL(this.router.getCurrentPath());
const sidebar = dom.find('.sidebar');
if (!sidebar) {
return;
}
const oldActive = dom.find(sidebar, 'li.active');
const newActive = dom
.find(
sidebar,
`a[href="${href}"], a[href="${decodeURIComponent(href)}"]`,
)
?.closest('li');
if (newActive && newActive !== oldActive) {
oldActive?.classList.remove('active');
newActive.classList.add('active');
}
return newActive;
}
/**
* Marks the current page in the sidebar
*
* @param {string} [href] Matching sidebar element HREF value. If
* unspecified, defaults to the current path (without query params)
* @returns Element|undefined
*/
#markSidebarCurrentPage(href) {
href ??= this.router.toURL(this.route.path);
const sidebar = dom.find('.sidebar');
if (!sidebar) {
return;
}
const path = href?.split('?')[0];
const oldPage = dom.find(sidebar, 'li[aria-current]');
const newPage = dom
.find(
sidebar,
`a[href="${path}"], a[href="${decodeURIComponent(path)}"]`,
)
?.closest('li');
if (newPage && newPage !== oldPage) {
oldPage?.removeAttribute('aria-current');
newPage.setAttribute('aria-current', 'page');
}
return newPage;
}
#toggleSidebar(force) {
const sidebarElm = dom.find('.sidebar');
if (!sidebarElm) {
return;
}
const ariaElms = dom.findAll('[aria-controls="__sidebar"]');
const inertElms = dom.findAll(
'body > *:not(main, script), main > .content',
);
const isShow = sidebarElm.classList.toggle('show', force);
// Set aria-expanded attribute
ariaElms.forEach(toggleElm => {
toggleElm.setAttribute(
'aria-expanded',
force ?? sidebarElm.classList.contains('show'),
);
});
// Add inert attributes (focus trap)
if (isShow && isMobile()) {
inertElms.forEach(elm => elm.setAttribute('inert', ''));
}
// Remove inert attributes
else {
inertElms.forEach(elm => elm.removeAttribute('inert'));
}
if (isShow) {
sidebarElm.focus();
}
// Restore focus
else {
const restoreElm = document.querySelector(
'main > .content [data-restore-focus]',
);
if (restoreElm) {
restoreElm.focus({
preventScroll: true,
});
}
}
}
/**
* Monitor next scroll start/end and set #isScrolling to true/false
* accordingly. Listeners are removed after the start/end events are fired.
* @void
*/
#watchNextScroll() {
// Scroll start
document.addEventListener(
'scroll',
() => {
this.#isScrolling = true;
// Scroll end
if ('onscrollend' in window) {
document.addEventListener(
'scrollend',
() => (this.#isScrolling = false),
{ once: true },
);
}
// Browsers w/o native scrollend event support (Safari)
else {
const callback = () => {
clearTimeout(scrollTimer);
scrollTimer = setTimeout(() => {
document.removeEventListener('scroll', callback);
this.#isScrolling = false;
}, 100);
};
let scrollTimer;
document.addEventListener('scroll', callback, false);
}
},
{ once: true },
);
}
};
}