feat: added property new-tab to type: search

This commit is contained in:
ralphocdol 2024-06-09 21:18:56 +08:00
parent b83bf9bc09
commit 06b21e3d27
5 changed files with 11 additions and 1 deletions

View File

@ -725,10 +725,13 @@ Preview:
| <kbd>Ctrl</kbd> + <kbd>Enter</kbd> | Perform search in a new tab | Search input is focused and not empty | | <kbd>Ctrl</kbd> + <kbd>Enter</kbd> | Perform search in a new tab | Search input is focused and not empty |
| <kbd>Escape</kbd> | Leave focus | Search input is focused | | <kbd>Escape</kbd> | Leave focus | Search input is focused |
If property `new-tab` is set to `true`, keys <kbd>Enter</kbd> and <kbd>Ctrl</kbd> + <kbd>Enter</kbd> will be switched.
#### Properties #### Properties
| Name | Type | Required | Default | | Name | Type | Required | Default |
| ---- | ---- | -------- | ------- | | ---- | ---- | -------- | ------- |
| search-engine | string | no | duckduckgo | | search-engine | string | no | duckduckgo |
| new-tab | boolean | no | false |
| bangs | array | no | | | bangs | array | no | |
##### `search-engine` ##### `search-engine`

View File

@ -370,6 +370,10 @@ kbd:active {
box-shadow: 0 0 0 0 var(--color-widget-background-highlight); box-shadow: 0 0 0 0 var(--color-widget-background-highlight);
} }
new-tab {
display: none;
}
.content-bounds { .content-bounds {
max-width: 1600px; max-width: 1600px;
margin-inline: auto; margin-inline: auto;

View File

@ -120,6 +120,7 @@ function setupSearchboxes() {
const inputElement = widget.getElementsByClassName("search-input")[0]; const inputElement = widget.getElementsByClassName("search-input")[0];
const bangElement = widget.getElementsByClassName("search-bang")[0]; const bangElement = widget.getElementsByClassName("search-bang")[0];
const bangs = widget.querySelectorAll(".search-bangs > input"); const bangs = widget.querySelectorAll(".search-bangs > input");
const newTab = widget.getElementsByTagName("new-tab")[0].innerHTML === "true";
const bangsMap = {}; const bangsMap = {};
const kbdElement = widget.getElementsByTagName("kbd")[0]; const kbdElement = widget.getElementsByTagName("kbd")[0];
let currentBang = null; let currentBang = null;
@ -154,7 +155,7 @@ function setupSearchboxes() {
const url = searchUrlTemplate.replace("!QUERY!", encodeURIComponent(query)); const url = searchUrlTemplate.replace("!QUERY!", encodeURIComponent(query));
if (event.ctrlKey) { if (newTab && !event.ctrlKey || !newTab && event.ctrlKey) {
window.open(url, '_blank').focus(); window.open(url, '_blank').focus();
} else { } else {
window.location.href = url; window.location.href = url;

View File

@ -20,5 +20,6 @@
<div class="search-bang"></div> <div class="search-bang"></div>
<kbd class="hide-on-mobile" title="Press [S] to focus the search input">S</kbd> <kbd class="hide-on-mobile" title="Press [S] to focus the search input">S</kbd>
<new-tab>{{ .NewTab }}</new-tab>
</div> </div>
{{ end }} {{ end }}

View File

@ -19,6 +19,7 @@ type Search struct {
cachedHTML template.HTML `yaml:"-"` cachedHTML template.HTML `yaml:"-"`
SearchEngine string `yaml:"search-engine"` SearchEngine string `yaml:"search-engine"`
Bangs []SearchBang `yaml:"bangs"` Bangs []SearchBang `yaml:"bangs"`
NewTab bool `yaml:"new-tab"`
} }
func convertSearchUrl(url string) string { func convertSearchUrl(url string) string {