Skip to content

Commit 2351c3e

Browse files
committed
feat(search): Localization for search placeholder, close #80
1 parent 079bd00 commit 2351c3e

File tree

6 files changed

+41
-6
lines changed

6 files changed

+41
-6
lines changed

docs/plugins.md

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ window.$docsify = {
8989
// ...
9090
})
9191

92+
hook.mounted(function() {
93+
// Called after initial completion. Only trigger once, no arguments.
94+
})
95+
9296
hook.ready(function() {
9397
// Called after initial completion, no arguments.
9498
})

docs/zh-cn/plugins.md

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ window.$docsify = {
8484
// ...
8585
})
8686

87+
hook.mounted(function() {
88+
// 初始化完成后调用 ,只调用一次,没有参数。
89+
})
90+
8791
hook.ready(function() {
8892
// 初始化并第一次加完成数据后调用,没有参数。
8993
})

src/core/init/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export function initMixin (proto) {
1818
initEvent(vm) // Bind events
1919
initRoute(vm) // Add hashchange eventListener
2020
initFetch(vm) // Fetch data
21+
callHook(vm, 'mounted')
2122
}
2223
}
2324

src/core/init/lifecycle.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { noop } from '../util/core'
22

33
export function initLifecycle (vm) {
4-
const hooks = ['init', 'beforeEach', 'afterEach', 'doneEach', 'ready']
4+
const hooks = [
5+
'init',
6+
'mounted',
7+
'beforeEach',
8+
'afterEach',
9+
'doneEach',
10+
'ready'
11+
]
512

613
vm._hooks = {}
714
vm._lifecycle = {}

src/plugins/search/component.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function style () {
6767

6868
function tpl (opts) {
6969
const html =
70-
`<input type="search" placeholder="${opts.placeholder}" />` +
70+
`<input type="search" />` +
7171
'<div class="results-panel"></div>' +
7272
'</div>'
7373
const el = dom.create('div', html)
@@ -108,9 +108,25 @@ function bindEvents () {
108108
})
109109
}
110110

111-
export default function (opts) {
111+
function updatePlaceholder (text, path) {
112+
const $input = dom.getNode('.search input[type="search"]')
113+
114+
if (typeof text === 'string') {
115+
$input.placeholder = text
116+
} else {
117+
const match = Object.keys(text).find(key => path.indexOf(key) > -1)
118+
$input.placeholder = text[match]
119+
}
120+
}
121+
122+
export function init (opts) {
112123
dom = Docsify.dom
113124
style()
114125
tpl(opts)
115126
bindEvents()
116127
}
128+
129+
export function update (opts, vm) {
130+
updatePlaceholder(opts.placeholder, vm.route.path)
131+
}
132+

src/plugins/search/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import initComponet from './component'
1+
import { init as initComponet, update as updateComponent } from './component'
22
import { init as initSearch } from './search'
33

44
const CONFIG = {
@@ -21,11 +21,14 @@ const install = function (hook, vm) {
2121

2222
const isAuto = CONFIG.paths === 'auto'
2323

24-
hook.ready(_ => {
24+
hook.mounted(_ => {
2525
initComponet(CONFIG)
2626
isAuto && initSearch(CONFIG, vm)
2727
})
28-
!isAuto && hook.doneEach(_ => initSearch(CONFIG, vm))
28+
hook.doneEach(_ => {
29+
updateComponent(CONFIG, vm)
30+
!isAuto && initSearch(CONFIG, vm)
31+
})
2932
}
3033

3134
window.$docsify.plugins = [].concat(install, window.$docsify.plugins)

0 commit comments

Comments
 (0)