Skip to content

Commit

Permalink
fix: bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 1, 2023
1 parent d1cea6d commit 95d5c9b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion framework/core/js/src/forum/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default class Search<T extends SearchAttrs = SearchAttrs> extends Compone
const searchLabel = extractText(app.translator.trans('core.forum.header.search_placeholder'));

return (
<div role="search" aria-label={app.translator.trans('core.forum.header.search_role_label')}>
<div role="search" className="Search" aria-label={app.translator.trans('core.forum.header.search_role_label')}>
<Input
type="search"
className="Search-input"
Expand Down
34 changes: 22 additions & 12 deletions framework/core/js/src/forum/components/SearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch

protected searchState!: SearchState;

protected query!: Stream<string>;

/**
* An array of SearchSources.
*/
Expand Down Expand Up @@ -62,6 +64,7 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch

this.searchState = this.attrs.searchState;
this.sources = this.attrs.sources;
this.query = Stream(this.searchState.getValue() || '');
}

title(): Mithril.Children {
Expand Down Expand Up @@ -90,9 +93,9 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch
prefixIcon="fas fa-search"
aria-label={searchLabel}
placeholder={searchLabel}
value={this.searchState.getValue()}
value={this.query()}
onchange={(value: string) => {
this.searchState.setValue(value);
this.query(value);
this.inputScroll(this.inputElement()[0]?.scrollLeft ?? 0);
}}
inputAttrs={{ className: 'SearchModal-input' }}
Expand Down Expand Up @@ -142,10 +145,10 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch
const items = new ItemList<Mithril.Children>();

const loading = this.loadingSources.includes(this.activeSource().resource);
const shouldShowResults = !!this.searchState.getValue() && !loading;
const shouldShowResults = !!this.query() && !loading;
const gambits = this.gambits();
const fullPageLink = this.activeSource().fullPage(this.searchState.getValue());
const results = this.activeSource()?.view(this.searchState.getValue());
const fullPageLink = this.activeSource().fullPage(this.query());
const results = this.activeSource()?.view(this.query());

if (shouldShowResults && fullPageLink) {
items.add(
Expand Down Expand Up @@ -205,15 +208,15 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch
switchSource(source: SearchSource) {
if (this.activeSource() !== source) {
this.activeSource(source);
this.search(this.searchState.getValue());
this.search(this.query());
this.inputElement().focus();
m.redraw();
}
}

gambits(): JSX.Element[] {
const gambits = app.search.gambits.for(this.activeSource().resource).filter((gambit) => gambit.enabled());
const query = this.searchState.getValue();
const query = this.query();

// We group the boolean gambits together to produce an initial item of
// is:unread,sticky,locked, etc.
Expand Down Expand Up @@ -357,10 +360,10 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch
suggest(text: string, fromTyped: string, start: number) {
const $input = this.inputElement() as JQuery<HTMLInputElement>;

const query = this.searchState.getValue();
const query = this.query();
const replaced = query.slice(0, start) + text + query.slice(start + fromTyped.length);

this.searchState.setValue(replaced);
this.query(replaced);
$input[0].focus();
setTimeout(() => {
$input[0].setSelectionRange(start + text.length, start + text.length);
Expand All @@ -375,7 +378,7 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch
* @example `lorem ipsum is:unread dolor` => `lorem ipsum <mark>is:unread</mark> dolor`
*/
gambifyInput(): Mithril.Children {
const query = this.searchState.getValue();
const query = this.query();
let marked = query;

app.search.gambits.match(this.activeSource().resource, query, (gambit: IGambit, matches: string[], negate: boolean, bit: string) => {
Expand Down Expand Up @@ -439,6 +442,11 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch
});
}

onremove(vnode: Mithril.VnodeDOM<CustomAttrs, this>) {
this.searchState.setValue(this.query());
super.onremove(vnode);
}

search(query: string) {
if (!query) return;

Expand Down Expand Up @@ -484,7 +492,9 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch
selectedUrl = item.find('a').attr('href');
}

if (this.searchState.getValue() && selectedUrl) {
const query = this.query();

if (query && selectedUrl) {
m.route.set(selectedUrl);
} else {
item.find('button')[0].click();
Expand All @@ -495,7 +505,7 @@ export default class SearchModal<CustomAttrs extends ISearchModalAttrs = ISearch
* Clear the search
*/
clear() {
this.searchState.clear();
this.query('');
}

/**
Expand Down

0 comments on commit 95d5c9b

Please sign in to comment.