forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (62 loc) · 2.21 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
import {
init as initComponent,
update as updateComponent,
} from './component.js';
import { init as initSearch } from './search.js';
const CONFIG = {
placeholder: 'Type to search',
noData: 'No Results!',
paths: 'auto',
depth: 2,
maxAge: 86400000, // 1 day
hideOtherSidebarContent: false,
namespace: undefined,
pathNamespaces: undefined,
keyBindings: ['/', 'meta+k', 'ctrl+k'],
};
const install = function (hook, vm) {
const { util } = Docsify;
const opts = vm.config.search || CONFIG;
if (Array.isArray(opts)) {
CONFIG.paths = opts;
} else if (typeof opts === 'object') {
CONFIG.paths = Array.isArray(opts.paths) ? opts.paths : 'auto';
CONFIG.maxAge = util.isPrimitive(opts.maxAge) ? opts.maxAge : CONFIG.maxAge;
CONFIG.placeholder = opts.placeholder || CONFIG.placeholder;
CONFIG.noData = opts.noData || CONFIG.noData;
CONFIG.depth = opts.depth || CONFIG.depth;
CONFIG.hideOtherSidebarContent =
opts.hideOtherSidebarContent || CONFIG.hideOtherSidebarContent;
CONFIG.namespace = opts.namespace || CONFIG.namespace;
CONFIG.pathNamespaces = opts.pathNamespaces || CONFIG.pathNamespaces;
CONFIG.keyBindings = opts.keyBindings || CONFIG.keyBindings;
}
const isAuto = CONFIG.paths === 'auto';
hook.init(() => {
const { keyBindings } = vm.config;
// Add key bindings
if (keyBindings.constructor === Object) {
keyBindings.focusSearch = {
bindings: CONFIG.keyBindings,
callback(e) {
const sidebarElm = document.querySelector('.sidebar');
const sidebarToggleElm = document.querySelector('.sidebar-toggle');
const searchElm = sidebarElm?.querySelector('input[type="search"]');
const isSidebarHidden = sidebarElm?.getBoundingClientRect().x < 0;
isSidebarHidden && sidebarToggleElm?.click();
setTimeout(() => searchElm?.focus(), isSidebarHidden ? 250 : 0);
},
};
}
});
hook.mounted(_ => {
initComponent(CONFIG, vm);
!isAuto && initSearch(CONFIG, vm);
});
hook.doneEach(_ => {
updateComponent(CONFIG, vm);
isAuto && initSearch(CONFIG, vm);
});
};
window.$docsify = window.$docsify || {};
$docsify.plugins = [install, ...($docsify.plugins || [])];