@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
189 lines • 7.43 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
// *****************************************************************************
// Copyright (C) 2018 TypeFox 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-only WITH Classpath-exception-2.0
// *****************************************************************************
import { SearchBoxDebounceOptions } from '../tree/search-box-debounce.js';
import { ReactWidget, codicon } from '../widgets/index.js';
import { Emitter } from '../../common/event.js';
import { KeyCode, Key } from '../keyboard/keys.js';
import { nls } from '../../common/nls.js';
export var SearchBoxProps;
(function (SearchBoxProps) {
/**
* The default search box widget option.
*/
SearchBoxProps.DEFAULT = SearchBoxDebounceOptions.DEFAULT;
})(SearchBoxProps || (SearchBoxProps = {}));
/**
* The search box widget.
*/
export class SearchBox extends ReactWidget {
static { this.SPECIAL_KEYS = [
Key.ESCAPE,
Key.BACKSPACE
]; }
static { this.MAX_CONTENT_LENGTH = 15; }
constructor(props, debounce) {
super();
this.props = props;
this.debounce = debounce;
this.nextEmitter = new Emitter();
this.previousEmitter = new Emitter();
this.closeEmitter = new Emitter();
this.textChangeEmitter = new Emitter();
this.filterToggleEmitter = new Emitter();
this._isFiltering = false;
this.hasMatch = true;
this.inputText = '';
this.toDispose.pushAll([
this.nextEmitter,
this.previousEmitter,
this.closeEmitter,
this.textChangeEmitter,
this.filterToggleEmitter,
this.debounce,
this.debounce.onChanged(data => this.fireTextChange(data))
]);
this.hide();
this.addClass('theia-search-box-widget');
this.node.setAttribute('tabIndex', '0');
}
get onPrevious() {
return this.previousEmitter.event;
}
get onNext() {
return this.nextEmitter.event;
}
get onClose() {
return this.closeEmitter.event;
}
get onTextChange() {
return this.textChangeEmitter.event;
}
get onFilterToggled() {
return this.filterToggleEmitter.event;
}
get isFiltering() {
return this._isFiltering;
}
get keyCodePredicate() {
return this.canHandle.bind(this);
}
firePrevious() {
this.previousEmitter.fire(undefined);
}
fireNext() {
this.nextEmitter.fire(undefined);
}
fireClose() {
this.closeEmitter.fire(undefined);
}
fireTextChange(input) {
this.textChangeEmitter.fire(input);
}
fireFilterToggle() {
this.doFireFilterToggle();
}
doFireFilterToggle(toggleTo = !this._isFiltering) {
this._isFiltering = toggleTo;
this.filterToggleEmitter.fire(toggleTo);
this.update();
}
handle(event) {
event.preventDefault();
const keyCode = KeyCode.createKeyCode(event);
if (this.canHandle(keyCode)) {
if (Key.equals(Key.ESCAPE, keyCode) || this.isCtrlBackspace(keyCode)) {
this.hide();
}
else {
this.show();
this.handleKey(keyCode);
}
}
}
handleArrowUp() {
this.firePrevious();
}
handleArrowDown() {
this.fireNext();
}
onBeforeHide() {
this.hasMatch = true;
this.doFireFilterToggle(false);
this.inputText = '';
this.debounce.append(undefined);
this.fireClose();
}
handleKey(keyCode) {
const character = Key.equals(Key.BACKSPACE, keyCode) ? '\b' : keyCode.character;
const data = this.debounce.append(character);
if (data) {
this.inputText = data;
this.update();
}
else {
this.hide();
}
}
getTrimmedContent(data) {
if (data.length > SearchBox.MAX_CONTENT_LENGTH) {
return '...' + data.substring(data.length - SearchBox.MAX_CONTENT_LENGTH);
}
return data;
}
canHandle(keyCode) {
if (keyCode === undefined) {
return false;
}
const { ctrl, alt, meta } = keyCode;
if (this.isCtrlBackspace(keyCode)) {
return true;
}
if (ctrl || alt || meta || keyCode.key === Key.SPACE) {
return false;
}
if (keyCode.character || (this.isVisible && SearchBox.SPECIAL_KEYS.some(key => Key.equals(key, keyCode)))) {
return true;
}
return false;
}
isCtrlBackspace(keyCode) {
if (keyCode.ctrl && Key.equals(Key.BACKSPACE, keyCode)) {
return true;
}
return false;
}
updateHighlightInfo(info) {
if (info.filterText && info.filterText.length > 0) {
this.hasMatch = info.matched > 0;
this.update();
}
}
render() {
const displayText = this.inputText ? this.getTrimmedContent(this.inputText) : '';
return (_jsxs("div", { className: `theia-search-box${this.hasMatch ? '' : ' no-match'}`, children: [_jsx("span", { className: 'theia-search-input', children: displayText }), _jsxs("div", { className: 'theia-search-buttons-wrapper', children: [this.props.showFilter &&
_jsx("div", { className: `theia-search-button action-label ${this.isFiltering ? codicon('list-filter') : codicon('list-selection')}`, title: this.isFiltering ?
nls.localize('theia/core/searchbox/showAll', 'Show all items') :
nls.localize('theia/core/searchbox/showOnlyMatching', 'Show only matching items'), onClick: () => this.fireFilterToggle() }), this.props.showButtons &&
_jsxs(_Fragment, { children: [_jsx("div", { className: `theia-search-button ${this.hasMatch ? 'action-label' : 'no-match'} ${codicon('find-previous-match')}`, title: nls.localize('theia/core/searchbox/previous', 'Previous (Up)'), onClick: () => this.hasMatch && this.firePrevious() }), _jsx("div", { className: `theia-search-button ${this.hasMatch ? 'action-label' : 'no-match'} ${codicon('find-next-match')}`, title: nls.localize('theia/core/searchbox/next', 'Next (Down)'), onClick: () => this.hasMatch && this.fireNext() })] }), (this.props.showButtons || this.props.showFilter) &&
_jsx("div", { className: `theia-search-button action-label ${codicon('widget-close')}`, title: nls.localize('theia/core/searchbox/close', 'Close (Escape)'), onClick: () => this.hide() })] })] }));
}
}
/**
* Search box factory.
*/
export const SearchBoxFactory = Symbol('SearchBoxFactory');
//# sourceMappingURL=search-box.js.map