@fluid-topics/ft-search-context 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,9 @@ export declare class FtSearchStateManager extends FtStateManager {
18
18
  constructor(store: FtReduxStore<FtSearchPageState, FtSearchPageStateReducers>, service?: FtSearchService);
19
19
  onNewNeededRequest: (neededRequest: string) => void;
20
20
  initService(): Promise<boolean>;
21
+ addWindowListeners(): void;
22
+ removeWindowListeners(): void;
23
+ private onStorageEvent;
21
24
  private updateSearchRequest;
22
25
  setQuery(query: string): void;
23
26
  setContentLocale(contentLocale?: string): string | undefined;
@@ -53,6 +56,10 @@ export declare class FtSearchStateManager extends FtStateManager {
53
56
  setIgnoreEmptyQuery(ignoreEmptyQuery: boolean): void;
54
57
  setOpenExternalDocumentInNewTab(openExternalDocumentInNewTab: boolean): void;
55
58
  setCanUpdateUiLocale(canUpdateUiLocale: boolean): void;
59
+ private loadRecentQueries;
60
+ addRecentQuery(query: string): void;
61
+ removeRecentQuery(query: string): void;
62
+ private setRecentQueries;
56
63
  private localesDebouncer;
57
64
  updateLocales(): Promise<boolean>;
58
65
  private searchDebouncer;
@@ -8,6 +8,8 @@ import { ftAppStateManager } from "@fluid-topics/ft-app-context";
8
8
  export const searchInDocumentTitlesOnlySelector = (s) => s.request.virtualField === FtVirtualField.TITLE_ONLY
9
9
  && s.request.scope === FtSearchScope.DOCUMENTS;
10
10
  export const keywordMatchSelector = (s) => s.request.keywordMatch == FtSearchKeywordMatch.MANDATORY;
11
+ const recentQueriesStorageKey = "ft-recent-queries";
12
+ const maxRecentQueries = 20;
11
13
  export class FtSearchStateManager extends FtStateManager {
12
14
  static build(id, service) {
13
15
  return new FtSearchStateManager(createSearchPageStore(id.trim() || "context"), service);
@@ -23,6 +25,12 @@ export class FtSearchStateManager extends FtStateManager {
23
25
  this.launchSearch(this.buildSearchRequest(this.store.getState()));
24
26
  }
25
27
  };
28
+ this.onStorageEvent = (e) => {
29
+ // detect changes from any context
30
+ if (e.key === recentQueriesStorageKey) {
31
+ this.loadRecentQueries();
32
+ }
33
+ };
26
34
  this.localesDebouncer = new Debouncer(10);
27
35
  this.searchDebouncer = new Debouncer(100);
28
36
  this.launchSearch = (request) => {
@@ -65,8 +73,15 @@ export class FtSearchStateManager extends FtStateManager {
65
73
  this.store.actions.configuration(conf);
66
74
  this.setSortId(this.store.getState().request.sortId);
67
75
  });
76
+ this.loadRecentQueries();
68
77
  return this.launchSearch();
69
78
  }
79
+ addWindowListeners() {
80
+ window.addEventListener("storage", this.onStorageEvent);
81
+ }
82
+ removeWindowListeners() {
83
+ window.removeEventListener("storage", this.onStorageEvent);
84
+ }
70
85
  updateSearchRequest(partialRequest, resetPaging = false) {
71
86
  const request = deepCopy(this.store.getState().request);
72
87
  delete request.changedByConfiguration;
@@ -289,6 +304,39 @@ export class FtSearchStateManager extends FtStateManager {
289
304
  setCanUpdateUiLocale(canUpdateUiLocale) {
290
305
  this.canUpdateUiLocale = canUpdateUiLocale;
291
306
  }
307
+ loadRecentQueries() {
308
+ var _a;
309
+ this.store.actions.recentQueries(JSON.parse((_a = window.localStorage.getItem(recentQueriesStorageKey)) !== null && _a !== void 0 ? _a : "[]"));
310
+ }
311
+ addRecentQuery(query) {
312
+ query = query.trim();
313
+ if (query.length === 0) {
314
+ return;
315
+ }
316
+ const normalizedQuery = query.toLowerCase();
317
+ const newValue = this.store.getState().recentQueries.filter((q) => q.toLowerCase() !== normalizedQuery).slice(0, maxRecentQueries - 1);
318
+ newValue.unshift(query);
319
+ this.setRecentQueries(newValue);
320
+ }
321
+ removeRecentQuery(query) {
322
+ const normalizedQuery = query.trim().toLowerCase();
323
+ const original = this.store.getState().recentQueries;
324
+ const otherQueries = original.filter((q) => q.toLowerCase() !== normalizedQuery);
325
+ if (original.length !== otherQueries.length) {
326
+ this.setRecentQueries(otherQueries);
327
+ }
328
+ }
329
+ setRecentQueries(queries) {
330
+ const newValue = JSON.stringify(queries);
331
+ window.localStorage.setItem(recentQueriesStorageKey, newValue);
332
+ // notify all contexts
333
+ window.dispatchEvent(new StorageEvent("storage", {
334
+ key: recentQueriesStorageKey,
335
+ newValue,
336
+ storageArea: window.localStorage,
337
+ url: window.location.href,
338
+ }));
339
+ }
292
340
  updateLocales() {
293
341
  return this.localesDebouncer.run(async () => this.store.actions.locales((await this.service.getAvailableSearchLocales()).contentLocales));
294
342
  }
@@ -343,10 +391,8 @@ export class FtSearchStateManager extends FtStateManager {
343
391
  var _a;
344
392
  try {
345
393
  (_a = this.cancelableSuggest) === null || _a === void 0 ? void 0 : _a.cancel();
346
- // We want to keep empty request, and request greater than 3 chars
347
- // This is done to keep usage of first "empty" request, which prefill
348
- // suggestions. This will be replace with last inputs.
349
- if (request.input.length != 0 && request.input.length < 3) {
394
+ if (request.input.length < 3) {
395
+ this.store.actions.suggestResults([]);
350
396
  return;
351
397
  }
352
398
  this.cancelableSuggest = cancelable(this.service.launchSuggest(request));
@@ -10,6 +10,7 @@ export interface FtSearchPageState {
10
10
  paging: FtSearchResultsPageInfo | undefined;
11
11
  spellcheck: FtSpellcheck | undefined;
12
12
  liveQuery: string;
13
+ recentQueries: string[];
13
14
  suggestResults: Array<FtSuggestResult> | undefined;
14
15
  }
15
16
  export interface FtSearchResultsChangeEventDetail {
@@ -21,6 +21,7 @@ export const createSearchPageStore = (id) => FtReduxStore.get({
21
21
  keywordMatch: undefined,
22
22
  },
23
23
  liveQuery: "",
24
+ recentQueries: [],
24
25
  paging: undefined,
25
26
  facets: undefined,
26
27
  spellcheck: undefined,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluid-topics/ft-search-context",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "Context block for integrated search components",
5
5
  "keywords": [
6
6
  "Lit"
@@ -19,12 +19,12 @@
19
19
  "url": "ssh://git@scm.mrs.antidot.net:2222/fluidtopics/ft-web-components.git"
20
20
  },
21
21
  "dependencies": {
22
- "@fluid-topics/ft-app-context": "2.1.0",
23
- "@fluid-topics/ft-wc-utils": "2.1.0",
22
+ "@fluid-topics/ft-app-context": "2.1.2",
23
+ "@fluid-topics/ft-wc-utils": "2.1.2",
24
24
  "lit": "3.1.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@fluid-topics/public-api": "1.0.126"
27
+ "@fluid-topics/public-api": "1.0.127"
28
28
  },
29
- "gitHead": "4727cf19d04236ad1d2b2cb43ce1c46298834c7e"
29
+ "gitHead": "b6e91c3f0ecf77e454885c936913c7bed396b536"
30
30
  }