mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-13 01:24:21 +01:00
Extracted formatting function and added aliases support
This commit is contained in:
parent
d6e360a3a5
commit
4f772c5c69
3 changed files with 55 additions and 3 deletions
|
@ -7,7 +7,13 @@ import { getTermContexts } from './match_query';
|
|||
import store from './utils/store';
|
||||
import { TermContext } from './query/lex';
|
||||
import { $$ } from './utils/dom';
|
||||
import { fetchLocalAutocomplete, fetchSuggestions, SuggestionsPopup, TermSuggestion } from './utils/suggestions';
|
||||
import {
|
||||
formatLocalAutocompleteResult,
|
||||
fetchLocalAutocomplete,
|
||||
fetchSuggestions,
|
||||
SuggestionsPopup,
|
||||
TermSuggestion,
|
||||
} from './utils/suggestions';
|
||||
|
||||
let inputField: HTMLInputElement | null = null,
|
||||
originalTerm: string | undefined,
|
||||
|
@ -173,7 +179,7 @@ function listenAutocomplete() {
|
|||
|
||||
const suggestions = localAc
|
||||
.matchPrefix(trimPrefixes(originalTerm), suggestionsCount)
|
||||
.map(({ name, imageCount }) => ({ label: `${name} (${imageCount})`, value: name }));
|
||||
.map(formatLocalAutocompleteResult);
|
||||
|
||||
if (suggestions.length) {
|
||||
popup.renderSuggestions(suggestions).showForField(targetedInput);
|
||||
|
|
|
@ -2,6 +2,7 @@ import { fetchMock } from '../../../test/fetch-mock.ts';
|
|||
import {
|
||||
fetchLocalAutocomplete,
|
||||
fetchSuggestions,
|
||||
formatLocalAutocompleteResult,
|
||||
purgeSuggestionsCache,
|
||||
SuggestionsPopup,
|
||||
TermSuggestion,
|
||||
|
@ -11,6 +12,7 @@ import path from 'path';
|
|||
import { LocalAutocompleter } from '../local-autocompleter.ts';
|
||||
import { afterEach } from 'vitest';
|
||||
import { fireEvent } from '@testing-library/dom';
|
||||
import { getRandomIntBetween } from '../../../test/randomness.ts';
|
||||
|
||||
const mockedSuggestionsEndpoint = '/endpoint?term=';
|
||||
const mockedSuggestionsResponse = [
|
||||
|
@ -331,4 +333,35 @@ describe('Suggestions', () => {
|
|||
expect(() => fetchLocalAutocomplete()).rejects.toThrowError('Received error from server');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatLocalAutocompleteResult', () => {
|
||||
it('should format suggested tags as tag name and the count', () => {
|
||||
const tagName = 'safe';
|
||||
const tagCount = getRandomIntBetween(5, 10);
|
||||
|
||||
const resultObject = formatLocalAutocompleteResult({
|
||||
name: tagName,
|
||||
aliasName: tagName,
|
||||
imageCount: tagCount,
|
||||
});
|
||||
|
||||
expect(resultObject.label).toBe(`${tagName} (${tagCount})`);
|
||||
expect(resultObject.value).toBe(tagName);
|
||||
});
|
||||
|
||||
it('should display original alias name for aliased tags', () => {
|
||||
const tagName = 'safe';
|
||||
const tagAlias = 'rating:safe';
|
||||
const tagCount = getRandomIntBetween(5, 10);
|
||||
|
||||
const resultObject = formatLocalAutocompleteResult({
|
||||
name: tagName,
|
||||
aliasName: tagAlias,
|
||||
imageCount: tagCount,
|
||||
});
|
||||
|
||||
expect(resultObject.label).toBe(`${tagAlias} ⇒ ${tagName} (${tagCount})`);
|
||||
expect(resultObject.value).toBe(tagName);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { makeEl } from './dom.ts';
|
||||
import { mouseMoveThenOver } from './events.ts';
|
||||
import { handleError } from './requests.ts';
|
||||
import { LocalAutocompleter } from './local-autocompleter.ts';
|
||||
import { LocalAutocompleter, Result } from './local-autocompleter.ts';
|
||||
|
||||
export interface TermSuggestion {
|
||||
label: string;
|
||||
|
@ -175,3 +175,16 @@ export async function fetchLocalAutocomplete(): Promise<LocalAutocompleter> {
|
|||
.then(resp => resp.arrayBuffer())
|
||||
.then(buf => new LocalAutocompleter(buf));
|
||||
}
|
||||
|
||||
export function formatLocalAutocompleteResult(result: Result): TermSuggestion {
|
||||
let tagName = result.name;
|
||||
|
||||
if (tagName !== result.aliasName) {
|
||||
tagName = `${result.aliasName} ⇒ ${tagName}`;
|
||||
}
|
||||
|
||||
return {
|
||||
value: result.name,
|
||||
label: `${tagName} (${result.imageCount})`,
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue