From e85055368a2e0bd19cccbdd9c2c91c6939ebf969 Mon Sep 17 00:00:00 2001 From: KoloMl Date: Wed, 7 Aug 2024 03:39:56 +0400 Subject: [PATCH] Apply preferences to the search wrapper --- src/lib/components/SearchWrapper.js | 35 ++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/lib/components/SearchWrapper.js b/src/lib/components/SearchWrapper.js index 440ca17..217c645 100644 --- a/src/lib/components/SearchWrapper.js +++ b/src/lib/components/SearchWrapper.js @@ -1,5 +1,6 @@ import {BaseComponent} from "$lib/components/base/BaseComponent.js"; import {QueryLexer, QuotedTermToken, TermToken, Token} from "$lib/booru/search/QueryLexer.js"; +import SearchSettings from "$lib/extension/settings/SearchSettings.js"; export class SearchWrapper extends BaseComponent { /** @type {HTMLInputElement|null} */ @@ -8,6 +9,10 @@ export class SearchWrapper extends BaseComponent { #lastParsedSearchValue = null; /** @type {Token[]} */ #cachedParsedQuery = []; + #searchSettings = new SearchSettings(); + #arePropertiesSuggestionsEnabled = false; + /** @type {"start"|"end"} */ + #propertiesSuggestionsPosition = "start"; build() { this.#searchField = this.container.querySelector('input[name=q]'); @@ -15,6 +20,16 @@ export class SearchWrapper extends BaseComponent { init() { this.#searchField.addEventListener('input', this.#onInputFindProperties.bind(this)); + + this.#searchSettings.resolvePropertiesSuggestionsEnabled() + .then(isEnabled => this.#arePropertiesSuggestionsEnabled = isEnabled); + this.#searchSettings.resolvePropertiesSuggestionsPosition() + .then(position => this.#propertiesSuggestionsPosition = position); + + this.#searchSettings.subscribe(settings => { + this.#arePropertiesSuggestionsEnabled = settings.suggestProperties; + this.#propertiesSuggestionsPosition = settings.suggestPropertiesPosition; + }); } /** @@ -22,6 +37,11 @@ export class SearchWrapper extends BaseComponent { * @param {InputEvent} event Source event to find the input element from. */ #onInputFindProperties(event) { + // Ignore events until option is enabled. + if (!this.#arePropertiesSuggestionsEnabled) { + return; + } + const currentFragment = this.#findCurrentTagFragment(); if (!currentFragment) { @@ -111,7 +131,20 @@ export class SearchWrapper extends BaseComponent { } const listContainer = autocompleteContainer.querySelector('ul'); - listContainer.prepend(...suggestedListItems); + + switch (this.#propertiesSuggestionsPosition) { + case "start": + listContainer.prepend(...suggestedListItems); + break; + + case "end": + listContainer.append(...suggestedListItems); + break; + + default: + console.warn("Invalid position for property suggestions!"); + } + autocompleteContainer.style.position = 'absolute'; autocompleteContainer.style.left = `${targetInput.offsetLeft}px`;