Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the search shortcut #888

Merged
merged 4 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions assets/js/search/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { default as params } from '@params';

(() => {
'use strict'

const shortcut = params?.search?.shortcut === undefined ?
['/'] :
params.search.shortcut

const keys = {}

const press = (e: KeyboardEvent) => {
if (e.type === 'keydown') {
keys[e.key] = 1
} else {
delete keys[e.key]
}
}

const isPressed = (expectedKeys: Array<string>): boolean => {
for (let key of expectedKeys) {
if (!(key in keys)) {
return false
}
}

return true
}

if (shortcut.length > 0) {
document.addEventListener('keydown', (e) => {
press(e)
if (isPressed(shortcut)) {
(document.querySelector('input.search-input') as HTMLInputElement)?.focus()
e.preventDefault()
}
})
document.addEventListener('keyup', press)
}
})()
1 change: 1 addition & 0 deletions assets/main/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import TableWrapper from 'js/table-wrapper';
import TOC from 'js/toc';
import components from './components';
import 'js/slide';
import 'js/search';

(new ActionsPanel()).run();
(new Scroller()).run();
Expand Down
2 changes: 1 addition & 1 deletion assets/main/scss/_top-app-bar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ $header-caret-width: 0.35em;
}

.search-input {
padding-left: 2.25rem;
padding-left: 2rem;
}
}

Expand Down
1 change: 1 addition & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ categoryId = "MDE4OkRpc2N1c3Npb25DYXRlZ29yeTMyODkzODg2"
indexPaginate = 100 # The number of pages per index file. Default to 1000.
# indexPreload = false # Do not preload index files.
# maxResults = 50
# shortcut = ["Control", "/"]
# Fuse.js options, the following options are available. See https://fusejs.io/api/options.html.
[search.fuse]
# ignoreLocation = true
Expand Down
1 change: 1 addition & 0 deletions exampleSite/content/docs/layouts/search/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ title = "Search"
| `search.indexPaginate` | Integer | `1000` | Index file pagination.
| `search.maxResults` | Number | `100` | The max number of search results.
| `search.resultContentCharactersCount` | Integer | `240` | The maximum characters count of result content for displaying.
| `search.shortcut` | Array | `["/"]` | The search shortcut, i.e. `["Control", "/"]`, disable it by a empty array `[]`.
| `search.fuse` | Object | - | [Fuse.js options](https://fusejs.io/api/options.html).
| `search.fuse.ignoreLocation` | Boolean | `true` |
| `search.fuse.location` | Integer | - |
Expand Down
1 change: 1 addition & 0 deletions exampleSite/content/docs/layouts/search/index.zh-hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ title = "Search"
| `search.paginate` | Integer | `10` | 分页。
| `search.indexPaginate` | Integer | `1000` | Index file pagination.
| `search.maxResults` | Number | `100` | 至多返回的搜索结果数目。
| `search.shortcut` | Array | `["/"]` | 搜索快捷键,比如 `["Control", "/"]`,空数组 `[]` 则禁用。
| `search.resultContentCharactersCount` | Integer | `240` | 搜索结果内容最大字符数。
| `search.fuse` | Object | - | [Fuse.js 参数](https://fusejs.io/api/options.html)
| `search.fuse.ignoreLocation` | Boolean | `true` |
Expand Down
1 change: 1 addition & 0 deletions exampleSite/content/docs/layouts/search/index.zh-hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ title = "Search"
| `search.paginate` | Integer | `10` | 分頁。
| `search.indexPaginate` | Integer | `1000` | Index file pagination.
| `search.maxResults` | Number | `100` | 至多返回的搜索結果數目。
| `search.shortcut` | Array | `["/"]` | 搜索快捷鍵,比如 `["Control", "/"]`,空數組 `[]` 則禁用。
| `search.resultContentCharactersCount` | Integer | `240` | 搜尋結果內容最大字符數。
| `search.fuse` | Object | - | [Fuse.js 引數](https://fusejs.io/api/options.html)
| `search.fuse.ignoreLocation` | Boolean | `true` |
Expand Down
6 changes: 6 additions & 0 deletions i18n/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ other = "Advanced"
[search_date]
other = "Date"

[search_input_placeholder]
other = "Type to search"

[search_missing_keywords]
other = "Please enter search keywords"

Expand All @@ -245,6 +248,9 @@ other = "No results found"
[search_score]
other = "Score"

[search_shortcut]
other = "{{ .Shortcut }} to search"

[search_sorting]
other = "Sorting"

Expand Down
6 changes: 6 additions & 0 deletions i18n/zh-cn.toml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ other = "高级"
[search_date]
other = "日期"

[search_input_placeholder]
other = "请输入关键词"

[search_missing_keywords]
other = "请输入搜索关键词"

Expand All @@ -235,6 +238,9 @@ other = "找不到相关结果"
[search_score]
other = "评分"

[search_shortcut]
other = "按 {{ .Shortcut }} 搜索"

[search_sorting]
other = "排序"

Expand Down
6 changes: 6 additions & 0 deletions i18n/zh-hans.toml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ other = "高级"
[search_date]
other = "日期"

[search_input_placeholder]
other = "请输入关键词"

[search_missing_keywords]
other = "请输入搜索关键词"

Expand All @@ -235,6 +238,9 @@ other = "找不到相关结果"
[search_score]
other = "评分"

[search_shortcut]
other = "按 {{ .Shortcut }} 搜索"

[search_sorting]
other = "排序"

Expand Down
6 changes: 6 additions & 0 deletions i18n/zh-hant.toml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ other = "高級"
[search_date]
other = "日期"

[search_input_placeholder]
other = "請輸入關鍵詞"

[search_missing_keywords]
other = "請輸入搜尋關鍵詞"

Expand All @@ -235,6 +238,9 @@ other = "找不到相關結果"
[search_score]
other = "評分"

[search_shortcut]
other = "按 {{ .Shortcut }} 搜索"

[search_sorting]
other = "排序"

Expand Down
6 changes: 6 additions & 0 deletions i18n/zh-hk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ other = "高級"
[search_date]
other = "日期"

[search_input_placeholder]
other = "請輸入關鍵詞"

[search_missing_keywords]
other = "請輸入搜索關鍵詞"

Expand All @@ -235,6 +238,9 @@ other = "找不到相關結果"
[search_score]
other = "評分"

[search_shortcut]
other = "按 {{ .Shortcut }} 搜索"

[search_sorting]
other = "排序"

Expand Down
6 changes: 6 additions & 0 deletions i18n/zh-tw.toml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ other = "高級"
[search_date]
other = "日期"

[search_input_placeholder]
other = "請輸入關鍵詞"

[search_missing_keywords]
other = "請輸入搜尋關鍵詞"

Expand All @@ -235,6 +238,9 @@ other = "找不到相關結果"
[search_score]
other = "評分"

[search_shortcut]
other = "按 {{ .Shortcut }} 搜索"

[search_sorting]
other = "排序"

Expand Down
9 changes: 9 additions & 0 deletions layouts/partials/functions/search-input-placeholder.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{- $placeholder := "search_input_placeholder" | i18n }}
{{- $shortcut := slice "/" }}
{{- if isset site.Params.search "shortcut" }}
{{- $shortcut = cond (gt (len site.Params.search.shortcut) 0) site.Params.search.shortcut slice }}
{{- end }}
{{- with $shortcut }}
{{- $placeholder = i18n "search_shortcut" (dict "Shortcut" (delimit . "+")) }}
{{- end }}
{{- return $placeholder -}}
11 changes: 9 additions & 2 deletions layouts/partials/header/search-bar.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{{- if .Site.Params.searchBar }}
{{- $page := .Site.GetPage "/search" }}
{{- if ne $page.RelPermalink .RelPermalink -}}
{{- $placeholder := partialCached "functions/search-input-placeholder" . . }}
<hr class="d-xxl-none">
<form class="search-bar ms-auto my-1" action="{{ with $page }}{{ .RelPermalink }}{{ end }}" novalidate>
<div class="input-group input-group-sm align-items-center">
<span class="btn btn-search disabled position-absolute left-0 border-0">
<span class="btn btn-search disabled position-absolute left-0 border-0 px-1">
<i class="fas fa-fw fa-search fa-lg"></i>
</span>
<input class="py-2 form-control border-white rounded search-input bg-body" name="q" type="search" aria-label="Search" required>
<input
class="my-1 form-control border-white rounded search-input bg-body"
name="q"
type="search"
placeholder="{{ $placeholder }}"
aria-label="Search"
required>
</div>
</form>
{{- end }}
Expand Down
3 changes: 2 additions & 1 deletion layouts/partials/search/form.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<form id="searchForm" class="col-12 search-form mb-3" novalidate>
<div class="input-group input-group-lg align-items-center">
<input class="form-control rounded border-primary pe-5" name="q" type="search" aria-label="{{ i18n "search" }}" placeholder="{{ i18n "search" }}">
{{- $placeholder := partialCached "functions/search-input-placeholder" . . }}
<input class="form-control search-input rounded border-primary pe-5" name="q" type="search" aria-label="{{ i18n "search" }}" placeholder="{{ $placeholder }}">
<button class="btn btn-sm btn-search position-absolute end-0 border-0 border-start border-secondary p-2" type="submit">
<i class="fas fa-fw fa-search text-primary"></i>
</button>
Expand Down