Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

🦺 Improved validation/robustness for search inputs

Alicia Sykes24b22f504fee
diff --git a/src/components/Settings/SearchBar.vue b/src/components/Settings/SearchBar.vue
index 4a98726c..9e8be99e 100644
--- a/src/components/Settings/SearchBar.vue
+++ b/src/components/Settings/SearchBar.vue
@@ -71,16 +71,20 @@ export default {
if (this.emitFrame != null) cancelAnimationFrame(this.emitFrame);
},
methods: {
- /* Call correct function dependending on which key is pressed */
+ /* Route global keystrokes to search / hotkeys / nav
+ * Bails out for modifier combos, and keystrokes on already focused inputs */
handleKeyPress(event) {
- const currentElem = document.activeElement.id;
+ if (!this.active || event.ctrlKey || event.metaKey || event.altKey) return;
+ const target = event.target;
+ const inFilter = target?.id === 'filter-tiles';
+ const inOtherEditable = !inFilter && target && (
+ /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName) || target.isContentEditable
+ );
+ if (inOtherEditable) return;
const { key, keyCode } = event;
- const notAlreadySearching = currentElem !== 'filter-tiles';
- // If a modal is open, then do nothing
- if (!this.active) return;
- if (/^[/:!a-zA-Z]$/.test(key) && notAlreadySearching) {
- // Letter or bang key pressed - start searching
- if (this.$refs.filter) this.$refs.filter.focus();
+ // Letter or bang key pressed - start searching
+ if (/^[/:!a-zA-Z]$/.test(key) && !inFilter) {
+ this.$refs.filter?.focus();
this.userIsTypingSomething();
} else if (/^[0-9]$/.test(key)) {
// Number key pressed, check if user has a custom binding
@@ -109,7 +113,7 @@ export default {
this.emitFrame = null;
}
this.$emit('user-is-searchin', '');
- document.activeElement.blur();
+ this.$refs.filter?.blur();
this.akn.resetIndex();
},
/* If configured, launch specific app when hotkey pressed */
diff --git a/src/components/Workspace/SideBar.vue b/src/components/Workspace/SideBar.vue
index e93ade61..6988f59a 100644
--- a/src/components/Workspace/SideBar.vue
+++ b/src/components/Workspace/SideBar.vue
@@ -13,7 +13,7 @@
<transition name="slide">
<SideBarSection
v-if="isOpen[index]"
- :items="filterTiles(section.items)"
+ :items="visibleItems(section.items)"
@launch-app="launchApp"
/>
</transition>
@@ -99,8 +99,9 @@ export default {
});
});
},
- /* Return a list with visible items on a section to the user or guest */
- filterTiles(allTiles) {
+ /* Return the items in a section that the current user/guest can see
+ * and that aren't explicitly hidden from the workspace view */
+ visibleItems(allTiles) {
if (!allTiles) return [];
const currentUser = getCurrentUser();
const isGuest = isLoggedInAsGuest();
diff --git a/src/mixins/HomeMixin.js b/src/mixins/HomeMixin.js
index e1fa44e1..969d2eeb 100644
--- a/src/mixins/HomeMixin.js
+++ b/src/mixins/HomeMixin.js
@@ -163,13 +163,6 @@ const HomeMixin = {
}
return '';
},
- /* Extracts the site name from domain, used for the searching functionality */
- getDomainFromUrl(url) {
- if (!url) return '';
- const urlPattern = /^(?:https?:\/\/)?(?:w{3}\.)?([a-z\d.-]+)\.(?:[a-z.]{2,10})(?:[/\w.-]*)*/;
- const domainPattern = url.match(urlPattern);
- return domainPattern ? domainPattern[1] : '';
- },
},
};
diff --git a/src/utils/Search.js b/src/utils/Search.js
index fac076b6..50d72f35 100644
--- a/src/utils/Search.js
+++ b/src/utils/Search.js
@@ -3,18 +3,6 @@
/* Tile filtering utility */
import ErrorHandler from '@/utils/logging/ErrorHandler';
-/**
- * Extracts the site name from domain
- * @param {string} url The URL to process
- * @returns {string} The hostname from URL
- */
-const getDomainFromUrl = (url) => {
- if (!url) return '';
- const urlPattern = /^(?:https?:\/\/)?(?:w{3}\.)?([a-z\d.-]+)\.(?:[a-z.]{2,10})(?:[/\w.-]*)*/;
- const domainPattern = urlPattern.exec(url);
- return domainPattern ? domainPattern[1] : '';
-};
-
/* Normalize a string for case/punctuation-insensitive matching */
const NORMALIZE_RE = /[^\w\s\p{Alpha}]/giu;
const normalize = (input) => (input == null ? '' : input.toString().toLowerCase().replace(NORMALIZE_RE, ''));
@@ -27,7 +15,7 @@ const buildHaystack = (tile) => {
title, description, provider, url, tags,
} = tile;
const tagsStr = Array.isArray(tags) ? tags.join(' ') : (tags || '');
- return normalize(`${title || ''} ${provider || ''} ${description || ''} ${tagsStr} ${getDomainFromUrl(url)}`);
+ return normalize(`${title || ''} ${provider || ''} ${description || ''} ${tagsStr} ${url || ''}`);
};
const getHaystack = (tile) => {
diff --git a/src/utils/config/ConfigHelpers.js b/src/utils/config/ConfigHelpers.js
index bcbf323e..42c1bb78 100644
--- a/src/utils/config/ConfigHelpers.js
+++ b/src/utils/config/ConfigHelpers.js
@@ -184,14 +184,10 @@ export const componentVisibility = (appConfig) => {
* Returns a list of items which the user has assigned a hotkey to
* So that when the hotkey is pressed, the app/ service can be launched
*/
-export const getCustomKeyShortcuts = (sections) => {
- const results = [];
- sections.forEach((section) => {
- const itemsWithHotKeys = section.items.filter(item => item.hotkey);
- results.push(itemsWithHotKeys.map(item => ({ hotkey: item.hotkey, url: item.url })));
- });
- return results.flat();
-};
+export const getCustomKeyShortcuts = (sections) => (sections || [])
+ .flatMap((section) => (section.items || [])
+ .filter((item) => item.hotkey)
+ .map((item) => ({ hotkey: item.hotkey, url: item.url })));
/**
* Gets the users chosen language. Defaults to English.
diff --git a/src/views/Home.vue b/src/views/Home.vue
index 704297f3..0d730cd9 100644
--- a/src/views/Home.vue
+++ b/src/views/Home.vue
@@ -25,7 +25,6 @@
:groupId="makeSectionId(section)"
:items="section.filteredItems"
:widgets="section.widgets"
- :searchTerm="searchValue"
:itemSize="itemSizeBound"
@itemClicked="finishedSearching()"
@change-modal-visibility="updateModalVisibility"
@@ -113,7 +112,7 @@ export default {
const sections = this.singleSectionView || this.sections;
return sections.map((section) => ({
...section,
- filteredItems: this.filterTiles(section.items, this.searchValue),
+ filteredItems: this.filterTiles(section.items),
}));
},
/* Updates layout (when button clicked), and saves in local storage */
diff --git a/src/views/Minimal.vue b/src/views/Minimal.vue
index c77379ab..74d03d36 100644
--- a/src/views/Minimal.vue
+++ b/src/views/Minimal.vue
@@ -37,13 +37,13 @@
</div>
<!-- Section item groups -->
<MinimalSection
- v-for="(section, index) in sections"
+ v-for="(section, index) in filteredSections"
:key="makeSectionId(section)"
:index="index"
:title="section.name"
:icon="section.icon || undefined"
:groupId="makeSectionId(section)"
- :items="filterTiles(section.items)"
+ :items="section.filteredItems"
:widgets="section.widgets"
:displayData="section.displayData || {}"
:selected="selectedSection === index"
@@ -52,7 +52,7 @@
@itemClicked="finishedSearching()"
@change-modal-visibility="updateModalVisibility"
/>
- <div v-if="checkIfResults()" class="no-data">
+ <div v-if="checkIfResults(filteredSections)" class="no-data">
{{searchValue ? $t('home.no-results') : $t('home.no-data')}}
</div>
</div>
@@ -100,6 +100,15 @@ export default {
canScrollRight: false,
tabResizeObserver: null,
}),
+ computed: {
+ /* Just the items to display, filtered by search term */
+ filteredSections() {
+ return (this.sections || []).map((section) => ({
+ ...section,
+ filteredItems: this.filterTiles(section.items),
+ }));
+ },
+ },
watch: {
searchValue() {
this.tabbedView = !this.searchValue || this.searchValue.length === 0;
@@ -128,19 +137,6 @@ export default {
finishedSearching() {
if (this.$refs.filterComp) this.$refs.filterComp.clearMinFilterInput();
},
- /* Returns true if there is more than 1 sub-result visible during searching */
- checkIfResults() {
- if (!this.sections) return false;
- else {
- let itemsFound = true;
- this.sections.forEach((section) => {
- if (section.widgets || this.filterTiles(section.items).length > 0) {
- itemsFound = false;
- }
- });
- return itemsFound;
- }
- },
/* Make CSS styles to apply the users custom background image */
getBackgroundImage() {
if (this.appConfig && this.appConfig.backgroundImg) {