Re: #206 - Implements support for search bangs

This commit is contained in:
Alicia Sykes 2021-09-11 19:47:54 +01:00
parent 0e8eb992fb
commit 688dece915
2 changed files with 35 additions and 20 deletions

View File

@ -9,7 +9,7 @@
:placeholder="$t('search.search-placeholder')" :placeholder="$t('search.search-placeholder')"
v-on:input="userIsTypingSomething" v-on:input="userIsTypingSomething"
@keydown.esc="clearFilterInput" /> @keydown.esc="clearFilterInput" />
<p v-if="webSearchEnabled && input.length > 0" class="web-search-note"> <p v-if="(!searchPrefs.disableWebSearch) && input.length > 0" class="web-search-note">
{{ $t('search.enter-to-search-web') }} {{ $t('search.enter-to-search-web') }}
</p> </p>
</div> </div>
@ -25,7 +25,13 @@ import router from '@/router';
import ArrowKeyNavigation from '@/utils/ArrowKeyNavigation'; import ArrowKeyNavigation from '@/utils/ArrowKeyNavigation';
import ErrorHandler from '@/utils/ErrorHandler'; import ErrorHandler from '@/utils/ErrorHandler';
import { getCustomKeyShortcuts } from '@/utils/ConfigHelpers'; import { getCustomKeyShortcuts } from '@/utils/ConfigHelpers';
import { searchEngineUrls, defaultSearchEngine, defaultSearchOpeningMethod } from '@/utils/defaults'; import { getSearchEngineFromBang, findUrlForSearchEngine, stripBangs } from '@/utils/Search';
import {
searchEngineUrls,
defaultSearchEngine,
defaultSearchOpeningMethod,
searchBangs as defaultSearchBangs,
} from '@/utils/defaults';
export default { export default {
name: 'FilterTile', name: 'FilterTile',
@ -41,12 +47,8 @@ export default {
}; };
}, },
computed: { computed: {
webSearchEnabled() { searchPrefs() {
const { appConfig } = this.config; return this.config.appConfig.webSearch || {};
if (appConfig && appConfig.webSearch) {
return !appConfig.webSearch.disableWebSearch;
}
return true;
}, },
}, },
mounted() { mounted() {
@ -55,7 +57,7 @@ export default {
const { key, keyCode } = event; const { key, keyCode } = event;
/* If a modal is open, then do nothing */ /* If a modal is open, then do nothing */
if (!this.active) return; if (!this.active) return;
if (/^[a-zA-Z]$/.test(key) && currentElem !== 'filter-tiles') { if (/^[/:!a-zA-Z]$/.test(key) && currentElem !== 'filter-tiles') {
/* Letter key pressed - start searching */ /* Letter key pressed - start searching */
if (this.$refs.filter) this.$refs.filter.focus(); if (this.$refs.filter) this.$refs.filter.focus();
this.userIsTypingSomething(); this.userIsTypingSomething();
@ -107,22 +109,24 @@ export default {
window.open(url, '_blank'); window.open(url, '_blank');
} }
}, },
/* Launch web search, to correct search engine, passing in users query */
searchSubmitted() { searchSubmitted() {
// Get search preferences from appConfig // Get search preferences from appConfig
const { appConfig } = this.config; const { searchPrefs } = this;
const searchPrefs = appConfig.webSearch || {}; if (!searchPrefs.disableWebSearch) { // Only proceed if user hasn't disabled web search
if (this.webSearchEnabled) { // Only proceed if user hasn't disabled web search const bangList = { ...defaultSearchBangs, ...(searchPrefs.searchBangs || {}) };
const openingMethod = searchPrefs.openingMethod || defaultSearchOpeningMethod; const openingMethod = searchPrefs.openingMethod || defaultSearchOpeningMethod;
// Get search engine, and make URL const searchBang = getSearchEngineFromBang(this.input, bangList);
const searchEngine = searchPrefs.searchEngine || defaultSearchEngine; const searchEngine = searchPrefs.searchEngine || defaultSearchEngine;
let searchUrl = searchEngineUrls[searchEngine]; // Use either search bang, or preffered search engine
if (!searchUrl) ErrorHandler(`Search engine not found - ${searchEngine}`); let searchUrl = searchBang || searchEngine;
if (searchEngine === 'custom' && searchPrefs.customSearchEngine) { searchUrl = findUrlForSearchEngine((searchBang || searchEngine), searchEngineUrls);
searchUrl = searchPrefs.customSearchEngine; if (searchUrl) { // Append search query to URL, and launch
searchUrl += encodeURIComponent(stripBangs(this.input, bangList));
this.launchWebSearch(searchUrl, openingMethod);
this.clearFilterInput();
} }
// Append users encoded query onto search URL, and launch
searchUrl += encodeURIComponent(this.input);
this.launchWebSearch(searchUrl, openingMethod);
} }
}, },
}, },

View File

@ -185,6 +185,17 @@ module.exports = {
}, },
defaultSearchEngine: 'duckduckgo', defaultSearchEngine: 'duckduckgo',
defaultSearchOpeningMethod: 'newtab', defaultSearchOpeningMethod: 'newtab',
searchBangs: {
'/b': 'bbc',
'/d': 'duckduckgo',
'/g': 'google',
'/r': 'reddit',
'/w': 'wikipedia',
'/y': 'youtube',
'/gh': 'github',
'/so': 'stackoverflow',
'/wa': 'wolframalpha',
},
/* Available built-in colors for the theme builder */ /* Available built-in colors for the theme builder */
swatches: [ swatches: [
['#eb5cad', '#985ceb', '#5346f3', '#5c90eb'], ['#eb5cad', '#985ceb', '#5346f3', '#5c90eb'],