remove superfluous influencer classes
This commit is contained in:
+28
-67
@@ -407,62 +407,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
class Influencer {
|
|
||||||
limit = 4;
|
|
||||||
|
|
||||||
constructor(options) {
|
|
||||||
this.limit = options?.limit ?? this.limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
static attachSearchPrefix(items, { key, splitBy }) {
|
|
||||||
return items.map((s) => (splitBy ? `${key}${splitBy}${s}` : s));
|
|
||||||
}
|
|
||||||
|
|
||||||
getSuggestions = async () => {
|
|
||||||
return [];
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
class DefaultInfluencer extends Influencer {
|
|
||||||
constructor() {
|
|
||||||
super(...arguments);
|
|
||||||
}
|
|
||||||
|
|
||||||
getSuggestions = async ({ query }) => {
|
|
||||||
if (!query) return [];
|
|
||||||
return (CONFIG.suggestions[query] ?? []).slice(0, this.limit);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
class DuckDuckGoInfluencer extends Influencer {
|
|
||||||
constructor() {
|
|
||||||
super(...arguments);
|
|
||||||
}
|
|
||||||
|
|
||||||
getSuggestions = async ({ key, search, splitBy }) => {
|
|
||||||
if (!search) return [];
|
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
window.autocompleteCallback = (res) =>
|
|
||||||
resolve(
|
|
||||||
DuckDuckGoInfluencer.attachSearchPrefix(
|
|
||||||
res
|
|
||||||
.map((s) => s.phrase)
|
|
||||||
.filter((s) => s.toLowerCase() !== search.toLowerCase())
|
|
||||||
.slice(0, this.limit),
|
|
||||||
{ key, splitBy }
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
const script = document.createElement('script');
|
|
||||||
document.querySelector('head').appendChild(script);
|
|
||||||
script.src = `https://duckduckgo.com/ac/?callback=autocompleteCallback&q=${search}`;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
class SearchComponent extends HTMLElement {
|
class SearchComponent extends HTMLElement {
|
||||||
static #INFLUENCERS = [new DefaultInfluencer(), new DuckDuckGoInfluencer()];
|
|
||||||
static #PATH_DELIMITER = '/';
|
static #PATH_DELIMITER = '/';
|
||||||
static #SEARCH_DELIMITER = "'";
|
static #SEARCH_DELIMITER = "'";
|
||||||
static #SUGGESTION_LIMIT = 4;
|
static #SUGGESTION_LIMIT = 4;
|
||||||
@@ -491,10 +436,30 @@
|
|||||||
this.shadowRoot.append(clone);
|
this.shadowRoot.append(clone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static #attachSearchPrefix(array, { key, splitBy }) {
|
||||||
|
if (!splitBy) return array;
|
||||||
|
return array.map((search) => `${key}${splitBy}${search}`);
|
||||||
|
}
|
||||||
|
|
||||||
static #escapeRegexCharacters(s) {
|
static #escapeRegexCharacters(s) {
|
||||||
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static #fetchDuckDuckGoSuggestions(search) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
window.autocompleteCallback = (res) =>
|
||||||
|
resolve(
|
||||||
|
res
|
||||||
|
.map((item) => item.phrase)
|
||||||
|
.filter((item) => item.toLowerCase() !== search.toLowerCase())
|
||||||
|
);
|
||||||
|
|
||||||
|
const script = document.createElement('script');
|
||||||
|
document.querySelector('head').appendChild(script);
|
||||||
|
script.src = `https://duckduckgo.com/ac/?callback=autocompleteCallback&q=${search}`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
static #hasProtocol(s) {
|
static #hasProtocol(s) {
|
||||||
return /^[a-zA-Z]+:\/\//i.test(s);
|
return /^[a-zA-Z]+:\/\//i.test(s);
|
||||||
}
|
}
|
||||||
@@ -595,20 +560,16 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
#onInput = async (e) => {
|
#onInput = async (e) => {
|
||||||
const query = e.currentTarget.value;
|
const q = SearchComponent.#parseQuery(e.currentTarget.value);
|
||||||
if (!query) return this.#close();
|
let suggestions = CONFIG.suggestions[q.query] ?? [];
|
||||||
const parsedQuery = SearchComponent.#parseQuery(query);
|
|
||||||
|
|
||||||
const suggestions = await Promise.all(
|
if (q.search && suggestions.length < SearchComponent.#SUGGESTION_LIMIT) {
|
||||||
SearchComponent.#INFLUENCERS.map((influencer) =>
|
const res = await SearchComponent.#fetchDuckDuckGoSuggestions(q.search);
|
||||||
influencer.getSuggestions(parsedQuery)
|
const formatted = SearchComponent.#attachSearchPrefix(res, q);
|
||||||
)
|
suggestions = suggestions.concat(formatted);
|
||||||
);
|
}
|
||||||
|
|
||||||
this.#renderSuggestions(
|
this.#renderSuggestions(suggestions, q.query);
|
||||||
[...new Set([].concat.apply([], suggestions))],
|
|
||||||
parsedQuery.query
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#onKeydown = (e) => {
|
#onKeydown = (e) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user