@eclipse-glsp/client
Version:
A sprotty-based client for GLSP
141 lines • 7.55 kB
JavaScript
;
/********************************************************************************
* Copyright (c) 2023-2025 Business Informatics Group (TU Wien) and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var SearchAutocompletePalette_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SearchAutocompletePalette = exports.SearchAutocompleteSuggestion = void 0;
const sprotty_1 = require("@eclipse-glsp/sprotty");
const inversify_1 = require("inversify");
const lodash_1 = require("lodash");
const autocomplete_suggestion_provider_1 = require("../../base/auto-complete/autocomplete-suggestion-provider");
const base_autocomplete_palette_1 = require("../../base/auto-complete/base-autocomplete-palette");
const css_feedback_1 = require("../../base/feedback/css-feedback");
const messages_1 = require("../../base/messages");
const model_1 = require("../../model");
const reposition_1 = require("../viewport/reposition");
const CSS_SEARCH_HIDDEN = 'search-hidden';
const CSS_SEARCH_HIGHLIGHTED = 'search-highlighted';
var SearchAutocompleteSuggestion;
(function (SearchAutocompleteSuggestion) {
function is(suggestion) {
return suggestion.element !== undefined;
}
SearchAutocompleteSuggestion.is = is;
})(SearchAutocompleteSuggestion || (exports.SearchAutocompleteSuggestion = SearchAutocompleteSuggestion = {}));
let SearchAutocompletePalette = SearchAutocompletePalette_1 = class SearchAutocompletePalette extends base_autocomplete_palette_1.BaseAutocompletePalette {
constructor() {
super(...arguments);
/**
* Cache the suggestions so that we don't have to retrieve them again for CSS manipulation.
*/
this.cachedSuggestions = [];
this.handleSuggestionChanged = (0, lodash_1.debounce)(this.doHandleSuggestionChanged, 300, { trailing: true });
}
id() {
return SearchAutocompletePalette_1.ID;
}
/**
* The search context for the search palette. This is used to filter the suggestions
*/
get searchContext() {
return [autocomplete_suggestion_provider_1.AutocompleteSuggestionProviderContext.CANVAS];
}
initializeContents(containerElement) {
super.initializeContents(containerElement);
this.autocompleteWidget.inputField.placeholder = messages_1.messages.search.placeholder;
containerElement.setAttribute('aria-label', messages_1.messages.search.label);
}
async retrieveSuggestions(root, input) {
const suggestions = await this.provideSearchSuggestions(root, input);
this.cachedSuggestions = suggestions;
return suggestions.map(s => s.action);
}
async provideSearchSuggestions(root, input) {
return (await Promise.all(this.suggestionRegistry.providersForContext(this.searchContext).flatMap(provider => provider.getSuggestions(root, input))))
.flat(1)
.filter(SearchAutocompleteSuggestion.is);
}
async visibleSuggestionsChanged(root, labeledActions) {
await this.applyCSS(this.getHiddenElements(root, this.getSuggestionsFromLabeledActions(labeledActions)), CSS_SEARCH_HIDDEN);
await this.deleteCSS(this.getSuggestionsFromLabeledActions(labeledActions)
.filter(s => s.element)
.map(s => s.element), CSS_SEARCH_HIDDEN);
}
async selectedSuggestionChanged(root, labeledAction) {
await this.handleSuggestionChanged(root, labeledAction);
}
async doHandleSuggestionChanged(root, labeledAction) {
await this.deleteAllCSS(root, CSS_SEARCH_HIGHLIGHTED);
if (labeledAction !== undefined) {
const suggestions = this.getSuggestionsFromLabeledActions([labeledAction]);
const actions = [];
suggestions.map(currElem => actions.push(reposition_1.RepositionAction.create([currElem.element.id])));
this.actionDispatcher.dispatchAll(actions);
await this.applyCSS(suggestions.map(s => s.element), CSS_SEARCH_HIGHLIGHTED);
}
}
show(root, ...contextElementIds) {
this.actionDispatcher.dispatch(sprotty_1.SelectAllAction.create(false));
super.show(root, ...contextElementIds);
}
hide() {
if (this.root !== undefined) {
this.deleteAllCSS(this.root, CSS_SEARCH_HIDDEN, CSS_SEARCH_HIGHLIGHTED);
this.autocompleteWidget.inputField.value = '';
}
super.hide();
}
applyCSS(elements, ...add) {
const actions = elements.map(element => (0, css_feedback_1.applyCssClasses)(element, ...add));
return this.actionDispatcher.dispatchAll(actions);
}
deleteCSS(elements, ...remove) {
const actions = elements.map(element => (0, css_feedback_1.deleteCssClasses)(element, ...remove));
return this.actionDispatcher.dispatchAll(actions);
}
deleteAllCSS(root, ...remove) {
const actions = (0, sprotty_1.toArray)(root.index.all().map(element => (0, css_feedback_1.deleteCssClasses)(element, ...remove)));
return this.actionDispatcher.dispatchAll(actions);
}
getSuggestionsFromLabeledActions(labeledActions) {
return this.cachedSuggestions.filter(c => labeledActions.find(s => (0, lodash_1.isEqual)(s, c.action)));
}
getHiddenElements(root, suggestions) {
return (0, sprotty_1.toArray)(root.index
.all()
.filter(element => element instanceof sprotty_1.GNode || element instanceof model_1.GEdge)
.filter(element => suggestions.find(suggestion => suggestion.element.id === element.id) === undefined));
}
};
exports.SearchAutocompletePalette = SearchAutocompletePalette;
SearchAutocompletePalette.ID = 'glsp.search-autocomplete-palette';
__decorate([
(0, inversify_1.inject)(sprotty_1.TYPES.IAutocompleteSuggestionProviderRegistry),
__metadata("design:type", Object)
], SearchAutocompletePalette.prototype, "suggestionRegistry", void 0);
exports.SearchAutocompletePalette = SearchAutocompletePalette = SearchAutocompletePalette_1 = __decorate([
(0, inversify_1.injectable)()
], SearchAutocompletePalette);
//# sourceMappingURL=search-palette.js.map