From 06b21e3d2743b8579484192f53ba5818221d61ac Mon Sep 17 00:00:00 2001 From: ralphocdol Date: Sun, 9 Jun 2024 21:18:56 +0800 Subject: [PATCH 1/2] feat: added property new-tab to type: search --- docs/configuration.md | 3 +++ internal/assets/static/main.css | 4 ++++ internal/assets/static/main.js | 3 ++- internal/assets/templates/search.html | 1 + internal/widget/search.go | 1 + 5 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index f547700..cabc454 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -725,10 +725,13 @@ Preview: | Ctrl + Enter | Perform search in a new tab | Search input is focused and not empty | | Escape | Leave focus | Search input is focused | +If property `new-tab` is set to `true`, keys Enter and Ctrl + Enter will be switched. + #### Properties | Name | Type | Required | Default | | ---- | ---- | -------- | ------- | | search-engine | string | no | duckduckgo | +| new-tab | boolean | no | false | | bangs | array | no | | ##### `search-engine` diff --git a/internal/assets/static/main.css b/internal/assets/static/main.css index 62b4480..fd76035 100644 --- a/internal/assets/static/main.css +++ b/internal/assets/static/main.css @@ -370,6 +370,10 @@ kbd:active { box-shadow: 0 0 0 0 var(--color-widget-background-highlight); } +new-tab { + display: none; +} + .content-bounds { max-width: 1600px; margin-inline: auto; diff --git a/internal/assets/static/main.js b/internal/assets/static/main.js index 3e10b96..d38db77 100644 --- a/internal/assets/static/main.js +++ b/internal/assets/static/main.js @@ -120,6 +120,7 @@ function setupSearchboxes() { const inputElement = widget.getElementsByClassName("search-input")[0]; const bangElement = widget.getElementsByClassName("search-bang")[0]; const bangs = widget.querySelectorAll(".search-bangs > input"); + const newTab = widget.getElementsByTagName("new-tab")[0].innerHTML === "true"; const bangsMap = {}; const kbdElement = widget.getElementsByTagName("kbd")[0]; let currentBang = null; @@ -154,7 +155,7 @@ function setupSearchboxes() { const url = searchUrlTemplate.replace("!QUERY!", encodeURIComponent(query)); - if (event.ctrlKey) { + if (newTab && !event.ctrlKey || !newTab && event.ctrlKey) { window.open(url, '_blank').focus(); } else { window.location.href = url; diff --git a/internal/assets/templates/search.html b/internal/assets/templates/search.html index a0dfaeb..998e133 100644 --- a/internal/assets/templates/search.html +++ b/internal/assets/templates/search.html @@ -20,5 +20,6 @@
S + {{ .NewTab }} {{ end }} diff --git a/internal/widget/search.go b/internal/widget/search.go index 9cfd64e..cf4c9d2 100644 --- a/internal/widget/search.go +++ b/internal/widget/search.go @@ -19,6 +19,7 @@ type Search struct { cachedHTML template.HTML `yaml:"-"` SearchEngine string `yaml:"search-engine"` Bangs []SearchBang `yaml:"bangs"` + NewTab bool `yaml:"new-tab"` } func convertSearchUrl(url string) string { From edb59ffdc604a651cf50f0c4ed0dd059d5a95481 Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Tue, 11 Jun 2024 20:30:10 +0100 Subject: [PATCH 2/2] Small change to implementation for new tab --- docs/configuration.md | 7 ++++++- internal/assets/static/main.css | 4 ---- internal/assets/static/main.js | 6 +++--- internal/assets/templates/search.html | 3 +-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index cabc454..1e07bf2 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -725,7 +725,9 @@ Preview: | Ctrl + Enter | Perform search in a new tab | Search input is focused and not empty | | Escape | Leave focus | Search input is focused | -If property `new-tab` is set to `true`, keys Enter and Ctrl + Enter will be switched. +> [!TIP] +> +> You can use the property `new-tab` with a value of `true` if you want to show search results in a new tab by default. Ctrl + Enter will then show results in the same tab. #### Properties | Name | Type | Required | Default | @@ -742,6 +744,9 @@ Either a value from the table below or a URL to a custom search engine. Use `{QU | duckduckgo | `https://duckduckgo.com/?q={QUERY}` | | google | `https://www.google.com/search?q={QUERY}` | +##### `new-tab` +When set to `true`, swaps the shortcuts for showing results in the same or new tab, defaulting to showing results in a new tab. + ##### `bangs` What now? [Bangs](https://duckduckgo.com/bangs). They're shortcuts that allow you to use the same search box for many different sites. Assuming you have it configured, if for example you start your search input with `!yt` you'd be able to perform a search on YouTube: diff --git a/internal/assets/static/main.css b/internal/assets/static/main.css index fd76035..62b4480 100644 --- a/internal/assets/static/main.css +++ b/internal/assets/static/main.css @@ -370,10 +370,6 @@ kbd:active { box-shadow: 0 0 0 0 var(--color-widget-background-highlight); } -new-tab { - display: none; -} - .content-bounds { max-width: 1600px; margin-inline: auto; diff --git a/internal/assets/static/main.js b/internal/assets/static/main.js index d38db77..fb86b02 100644 --- a/internal/assets/static/main.js +++ b/internal/assets/static/main.js @@ -107,7 +107,7 @@ function updateRelativeTimeForElements(elements) } } -function setupSearchboxes() { +function setupSearchBoxes() { const searchWidgets = document.getElementsByClassName("search"); if (searchWidgets.length == 0) { @@ -117,10 +117,10 @@ function setupSearchboxes() { for (let i = 0; i < searchWidgets.length; i++) { const widget = searchWidgets[i]; const defaultSearchUrl = widget.dataset.defaultSearchUrl; + const newTab = widget.dataset.newTab === "true"; const inputElement = widget.getElementsByClassName("search-input")[0]; const bangElement = widget.getElementsByClassName("search-bang")[0]; const bangs = widget.querySelectorAll(".search-bangs > input"); - const newTab = widget.getElementsByTagName("new-tab")[0].innerHTML === "true"; const bangsMap = {}; const kbdElement = widget.getElementsByTagName("kbd")[0]; let currentBang = null; @@ -552,7 +552,7 @@ async function setupPage() { try { setupClocks() setupCarousels(); - setupSearchboxes(); + setupSearchBoxes(); setupCollapsibleLists(); setupCollapsibleGrids(); setupDynamicRelativeTime(); diff --git a/internal/assets/templates/search.html b/internal/assets/templates/search.html index 998e133..1b507cb 100644 --- a/internal/assets/templates/search.html +++ b/internal/assets/templates/search.html @@ -3,7 +3,7 @@ {{ define "widget-content-classes" }}widget-content-frameless{{ end }} {{ define "widget-content" }} -