kui-shell
Version:
This is the monorepo for Kui, the hybrid command-line/GUI electron-based Kubernetes tool
125 lines • 5.91 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = require("debug");
const path_1 = require("path");
const UI = require("@kui-shell/core/api/ui-lite");
const inject_1 = require("@kui-shell/core/api/inject");
const debug = debug_1.default('plugins/core-support/text-search');
function registerListener() {
return __awaiter(this, void 0, void 0, function* () {
if (typeof document === 'undefined')
return;
const root = path_1.dirname(require.resolve('@kui-shell/plugin-core-support/package.json'));
inject_1.injectCSS(path_1.join(root, 'web/css/text-search.css'));
const app = yield Promise.resolve().then(() => require('electron'));
const addVisibilityStatusToDocument = () => document.body.classList.add('search-bar-is-visible');
const removeVisibilityStatusFromDocument = () => document.body.classList.remove('search-bar-is-visible');
const searchBar = document.createElement('div');
searchBar.setAttribute('id', 'search-bar');
searchBar.style.opacity = '0';
searchBar.innerHTML = `<div id='search-container'><div id='search-input-container'><input id='search-input' type='text' aria-label='Search' placeholder='search term'/><span id='search-found-text' class='no-search-yet'></span></div><span id='search-close-button'><svg focusable="false" preserveAspectRatio="xMidYMid meet" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M12 4.7l-.7-.7L8 7.3 4.7 4l-.7.7L7.3 8 4 11.3l.7.7L8 8.7l3.3 3.3.7-.7L8.7 8z"></path></svg></span></div>`;
const input = searchBar.querySelector('#search-input');
input.onfocus = () => input.select();
input.onclick = (evt) => {
evt.stopPropagation();
return false;
};
const page = document.querySelector('body > .page');
page.insertBefore(searchBar, page.querySelector('main').nextSibling);
const searchInput = document.getElementById('search-input');
const searchFoundText = document.getElementById('search-found-text');
const stopSearch = (clear) => {
app.remote.getCurrentWebContents().stopFindInPage('clearSelection');
if (clear) {
setTimeout(() => {
UI.getCurrentPrompt().focus();
}, 300);
}
};
const closeSearchBox = () => {
searchBar.classList.remove('visible');
removeVisibilityStatusFromDocument();
searchFoundText.innerHTML = '';
stopSearch(true);
};
const searchCloseButton = document.getElementById('search-close-button');
searchCloseButton.onclick = closeSearchBox;
const searchText = (value) => {
searchFoundText.classList.remove('no-search-yet');
app.remote.getCurrentWebContents().findInPage(value);
};
app.remote.getCurrentWebContents().on('found-in-page', (event, result) => {
if (!result.finalUpdate) {
return;
}
const v = searchInput.value;
searchInput.value = '';
searchInput.value = v;
searchInput.focus();
if (result.matches === 1) {
searchFoundText.innerText = 'no matches';
}
else if (result.matches === 2) {
searchFoundText.innerText = '1 match';
}
else {
searchFoundText.innerText = result.matches - 1 + ' matches';
}
});
searchInput.onpaste = (evt) => {
evt.stopPropagation();
};
searchInput.addEventListener('click', () => {
searchInput.focus();
});
searchInput.addEventListener('keyup', (e) => {
if (e.key === 'Enter') {
if (searchInput.value.length > 0) {
searchText(searchInput.value);
}
else {
searchFoundText.innerHTML = '';
stopSearch(true);
}
}
else if (e.key === 'Escape') {
e.stopPropagation();
closeSearchBox();
}
});
document.body.addEventListener('keydown', function (e) {
if (!e.defaultPrevented &&
e.keyCode === UI.Keys.Codes.F &&
((e.ctrlKey && process.platform !== 'darwin') || e.metaKey)) {
searchBar.classList.add('visible');
addVisibilityStatusToDocument();
searchBar.style.opacity = '';
searchFoundText.innerText = 'hit enter to search';
searchFoundText.classList.add('no-search-yet');
searchInput.focus();
}
});
window.onbeforeunload = () => {
stopSearch(false);
app.remote.getCurrentWebContents().removeAllListeners('found-in-page');
};
});
}
exports.default = () => {
try {
registerListener();
}
catch (err) {
debug('Not running in electron environment');
}
};
//# sourceMappingURL=text-search.js.map