UNPKG

@finos/legend-application-pure-ide

Version:
104 lines 3.27 kB
/** * Copyright (c) 2020-present, Goldman Sachs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ActionState, assertErrorThrown, deleteEntry, } from '@finos/legend-shared'; import { action, computed, flow, makeObservable, observable } from 'mobx'; import { getSearchResultEntry, } from '../server/models/SearchEntry.js'; export class TextSearchResult { searchState; searchEntries = []; constructor(searchState, searchEntries) { makeObservable(this, { searchEntries: observable, numberOfFiles: computed, numberOfResults: computed, }); this.searchState = searchState; this.searchEntries = searchEntries; } dismissSearchEntry(value) { deleteEntry(this.searchEntries, value); if (!this.searchEntries.length) { this.searchState.setResult(undefined); } } get numberOfFiles() { return this.searchEntries.length; } get numberOfResults() { return this.searchEntries.flatMap((entry) => entry.coordinates).length; } } export class TextSearchState { ideStore; loadState = ActionState.create(); searchInput; text = ''; isCaseSensitive = false; isRegExp = false; result; constructor(ideStore) { makeObservable(this, { text: observable, isCaseSensitive: observable, isRegExp: observable, result: observable, setText: action, setCaseSensitive: action, setRegExp: action, setResult: action, search: flow, }); this.ideStore = ideStore; } setSearchInput(el) { this.searchInput = el; } focus() { this.searchInput?.focus(); } select() { this.searchInput?.select(); } *search() { if (this.loadState.isInProgress || this.text.length <= 3) { return; } this.loadState.inProgress(); try { const results = (yield this.ideStore.client.searchText(this.text, this.isCaseSensitive, this.isRegExp)).map((result) => getSearchResultEntry(result)); this.setResult(new TextSearchResult(this, results)); this.loadState.pass(); } catch (error) { assertErrorThrown(error); this.ideStore.applicationStore.notificationService.notifyError(error); this.loadState.fail(); } } setText(value) { this.text = value; } setCaseSensitive(value) { this.isCaseSensitive = value; } setRegExp(value) { this.isRegExp = value; } setResult(val) { this.result = val; } } //# sourceMappingURL=TextSearchState.js.map