feat: Add hideOtherSidebarContent option (#661)
Allow user to configure whether or not to hide other sidebar content while showing searching results. Related issue: https://github.com/docsifyjs/docsify/issues/535
This commit is contained in:
parent
a39b214733
commit
4a23c4acf2
4 changed files with 31 additions and 5 deletions
|
|
@ -37,7 +37,9 @@ By default, the hyperlink on the current page is recognized and the content is s
|
||||||
},
|
},
|
||||||
|
|
||||||
// Headline depth, 1 - 6
|
// Headline depth, 1 - 6
|
||||||
depth: 2
|
depth: 2,
|
||||||
|
|
||||||
|
hideOtherSidebarContent: false, // whether or not to hide other sidebar content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ export function main(config) {
|
||||||
'</button>' +
|
'</button>' +
|
||||||
'<aside class="sidebar">' +
|
'<aside class="sidebar">' +
|
||||||
(config.name ?
|
(config.name ?
|
||||||
`<h1><a class="app-name-link" data-nosearch>${
|
`<h1 class="app-name"><a class="app-name-link" data-nosearch>${
|
||||||
config.logo ?
|
config.logo ?
|
||||||
`<img alt=${config.name} src=${config.logo}>` :
|
`<img alt=${config.name} src=${config.logo}>` :
|
||||||
config.name
|
config.name
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import {search} from './search'
|
import {search} from './search'
|
||||||
|
|
||||||
let NO_DATA_TEXT = ''
|
let NO_DATA_TEXT = ''
|
||||||
|
let options
|
||||||
|
|
||||||
function style() {
|
function style() {
|
||||||
const code = `
|
const code = `
|
||||||
|
|
@ -86,12 +87,16 @@ function style() {
|
||||||
|
|
||||||
.search p.empty {
|
.search p.empty {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.app-name.hide, .sidebar-nav.hide {
|
||||||
|
display: none;
|
||||||
}`
|
}`
|
||||||
|
|
||||||
Docsify.dom.style(code)
|
Docsify.dom.style(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
function tpl(opts, defaultValue = '') {
|
function tpl(defaultValue = '') {
|
||||||
const html =
|
const html =
|
||||||
`<div class="input-wrap">
|
`<div class="input-wrap">
|
||||||
<input type="search" value="${defaultValue}" />
|
<input type="search" value="${defaultValue}" />
|
||||||
|
|
@ -116,11 +121,18 @@ function doSearch(value) {
|
||||||
const $search = Docsify.dom.find('div.search')
|
const $search = Docsify.dom.find('div.search')
|
||||||
const $panel = Docsify.dom.find($search, '.results-panel')
|
const $panel = Docsify.dom.find($search, '.results-panel')
|
||||||
const $clearBtn = Docsify.dom.find($search, '.clear-button')
|
const $clearBtn = Docsify.dom.find($search, '.clear-button')
|
||||||
|
const $sidebarNav = Docsify.dom.find('.sidebar-nav')
|
||||||
|
const $appName = Docsify.dom.find('.app-name')
|
||||||
|
|
||||||
if (!value) {
|
if (!value) {
|
||||||
$panel.classList.remove('show')
|
$panel.classList.remove('show')
|
||||||
$clearBtn.classList.remove('show')
|
$clearBtn.classList.remove('show')
|
||||||
$panel.innerHTML = ''
|
$panel.innerHTML = ''
|
||||||
|
|
||||||
|
if (options.hideOtherSidebarContent) {
|
||||||
|
$sidebarNav.classList.remove('hide')
|
||||||
|
$appName.classList.remove('hide')
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const matchs = search(value)
|
const matchs = search(value)
|
||||||
|
|
@ -138,6 +150,10 @@ function doSearch(value) {
|
||||||
$panel.classList.add('show')
|
$panel.classList.add('show')
|
||||||
$clearBtn.classList.add('show')
|
$clearBtn.classList.add('show')
|
||||||
$panel.innerHTML = html || `<p class="empty">${NO_DATA_TEXT}</p>`
|
$panel.innerHTML = html || `<p class="empty">${NO_DATA_TEXT}</p>`
|
||||||
|
if (options.hideOtherSidebarContent) {
|
||||||
|
$sidebarNav.classList.add('hide')
|
||||||
|
$appName.classList.add('hide')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function bindEvents() {
|
function bindEvents() {
|
||||||
|
|
@ -188,16 +204,22 @@ function updateNoData(text, path) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateOptions(opts) {
|
||||||
|
options = opts
|
||||||
|
}
|
||||||
|
|
||||||
export function init(opts, vm) {
|
export function init(opts, vm) {
|
||||||
const keywords = vm.router.parse().query.s
|
const keywords = vm.router.parse().query.s
|
||||||
|
|
||||||
|
updateOptions(opts)
|
||||||
style()
|
style()
|
||||||
tpl(opts, keywords)
|
tpl(keywords)
|
||||||
bindEvents()
|
bindEvents()
|
||||||
keywords && setTimeout(_ => doSearch(keywords), 500)
|
keywords && setTimeout(_ => doSearch(keywords), 500)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function update(opts, vm) {
|
export function update(opts, vm) {
|
||||||
|
updateOptions(opts)
|
||||||
updatePlaceholder(opts.placeholder, vm.route.path)
|
updatePlaceholder(opts.placeholder, vm.route.path)
|
||||||
updateNoData(opts.noData, vm.route.path)
|
updateNoData(opts.noData, vm.route.path)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@ const CONFIG = {
|
||||||
noData: 'No Results!',
|
noData: 'No Results!',
|
||||||
paths: 'auto',
|
paths: 'auto',
|
||||||
depth: 2,
|
depth: 2,
|
||||||
maxAge: 86400000 // 1 day
|
maxAge: 86400000, // 1 day
|
||||||
|
hideOtherSidebarContent: false
|
||||||
}
|
}
|
||||||
|
|
||||||
const install = function (hook, vm) {
|
const install = function (hook, vm) {
|
||||||
|
|
@ -21,6 +22,7 @@ const install = function (hook, vm) {
|
||||||
CONFIG.placeholder = opts.placeholder || CONFIG.placeholder
|
CONFIG.placeholder = opts.placeholder || CONFIG.placeholder
|
||||||
CONFIG.noData = opts.noData || CONFIG.noData
|
CONFIG.noData = opts.noData || CONFIG.noData
|
||||||
CONFIG.depth = opts.depth || CONFIG.depth
|
CONFIG.depth = opts.depth || CONFIG.depth
|
||||||
|
CONFIG.hideOtherSidebarContent = opts.hideOtherSidebarContent || CONFIG.hideOtherSidebarContent
|
||||||
}
|
}
|
||||||
|
|
||||||
const isAuto = CONFIG.paths === 'auto'
|
const isAuto = CONFIG.paths === 'auto'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue