|
| 1 | +import { ITerminalAddon, Terminal } from '@xterm/xterm'; |
| 2 | +import { ISearchOptions, SearchAddon } from '@xterm/addon-search'; |
| 3 | +import './search.css'; |
| 4 | + |
| 5 | +export interface SearchBarOption extends ISearchOptions { |
| 6 | + searchAddon: SearchAddon; |
| 7 | +} |
| 8 | + |
| 9 | +const ADDON_MARKER_NAME = 'xterm-search-bar__addon'; |
| 10 | + |
| 11 | +export class SearchBarAddon implements ITerminalAddon { |
| 12 | + private readonly options: Partial<SearchBarOption>; |
| 13 | + // @ts-ignore |
| 14 | + private terminal: Terminal; |
| 15 | + // @ts-ignore |
| 16 | + private readonly searchAddon: SearchAddon; |
| 17 | + // @ts-ignore |
| 18 | + private searchBarElement: HTMLDivElement; |
| 19 | + // @ts-ignore |
| 20 | + private searchKey: string; |
| 21 | + |
| 22 | + constructor(options: Partial<SearchBarOption>) { |
| 23 | + this.options = options || {}; |
| 24 | + if (this.options && this.options.searchAddon) { |
| 25 | + this.searchAddon = this.options.searchAddon; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + activate(terminal: Terminal): void { |
| 30 | + this.terminal = terminal; |
| 31 | + if (!this.searchAddon) { |
| 32 | + console.error('Cannot use search bar addon until search addon has been loaded!'); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + dispose() { |
| 37 | + this.hidden(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Show the bar in the term |
| 42 | + * @returns empty |
| 43 | + * @memberof SearchBarAddon necessary search addon instance |
| 44 | + */ |
| 45 | + show() { |
| 46 | + if (!this.terminal || !this.terminal.element) { |
| 47 | + console.error('Cannot show search bar addon until terminal has been initialized!'); |
| 48 | + return; |
| 49 | + } |
| 50 | + if (this.searchBarElement) { |
| 51 | + this.searchBarElement.style.visibility = 'visible'; |
| 52 | + (this.searchBarElement.querySelector('input') as HTMLInputElement).select(); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + this.terminal.element.style.position = 'relative'; |
| 57 | + const element = document.createElement('div'); |
| 58 | + element.innerHTML = ` |
| 59 | + <input type="text" class="search-bar__input" name="search-bar__input"/> |
| 60 | + <button class="search-bar__btn prev"></button> |
| 61 | + <button class="search-bar__btn next"></button> |
| 62 | + <button class="search-bar__btn close"></button> |
| 63 | + `; |
| 64 | + element.className = ADDON_MARKER_NAME; |
| 65 | + const parentElement = <HTMLElement>this.terminal.element.parentElement; |
| 66 | + this.searchBarElement = element; |
| 67 | + if (!['relative', 'absoulte', 'fixed'].includes(parentElement.style.position)) { |
| 68 | + parentElement.style.position = 'relative'; |
| 69 | + } |
| 70 | + parentElement.appendChild(this.searchBarElement); |
| 71 | + this.on('.search-bar__btn.close', 'click', () => { |
| 72 | + this.hidden(); |
| 73 | + }); |
| 74 | + this.on('.search-bar__btn.next', 'click', () => { |
| 75 | + this.searchAddon.findNext(this.searchKey, { |
| 76 | + incremental: false |
| 77 | + }); |
| 78 | + }); |
| 79 | + this.on('.search-bar__btn.prev', 'click', () => { |
| 80 | + this.searchAddon.findPrevious(this.searchKey, { |
| 81 | + incremental: false |
| 82 | + }); |
| 83 | + }); |
| 84 | + this.on('.search-bar__input', 'keyup', (e: any) => { |
| 85 | + this.searchKey = (e.target as HTMLInputElement).value; |
| 86 | + this.searchAddon.findNext(this.searchKey, { |
| 87 | + incremental: e.key !== `Enter` |
| 88 | + }); |
| 89 | + }); |
| 90 | + (this.searchBarElement.querySelector('input') as HTMLInputElement).select(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * You can manually call close, also can click the close button on the bar |
| 95 | + * @memberof SearchBarAddon |
| 96 | + */ |
| 97 | + hidden() { |
| 98 | + if (this.searchBarElement && (this.terminal.element as HTMLElement).parentElement) { |
| 99 | + this.searchBarElement.style.visibility = 'hidden'; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private on(selector: string, event: string, cb: (e: any) => void) { |
| 104 | + const parentElement = <HTMLElement>(this.terminal.element as HTMLElement).parentElement; |
| 105 | + parentElement.addEventListener(event, (e) => { |
| 106 | + let target = e.target; |
| 107 | + |
| 108 | + while (target !== document.querySelector(selector)) { |
| 109 | + if (target === parentElement) { |
| 110 | + target = null; |
| 111 | + break; |
| 112 | + } |
| 113 | + |
| 114 | + target = (target as HTMLElement).parentElement; |
| 115 | + } |
| 116 | + |
| 117 | + if (target === document.querySelector(selector)) { |
| 118 | + cb.call(this, e); |
| 119 | + e.stopPropagation(); |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * You can customize your own style, and then add CSS string template after search bar init |
| 126 | + * @param {string} newStyle |
| 127 | + * @memberof SearchBarAddon |
| 128 | + */ |
| 129 | + addNewStyle(newStyle: string) { |
| 130 | + let styleElement = document.getElementById(ADDON_MARKER_NAME) as HTMLStyleElement; |
| 131 | + |
| 132 | + if (!styleElement) { |
| 133 | + styleElement = document.createElement('style'); |
| 134 | + styleElement.type = 'text/css'; |
| 135 | + styleElement.id = ADDON_MARKER_NAME; |
| 136 | + document.getElementsByTagName('head')[0].appendChild(styleElement); |
| 137 | + } |
| 138 | + |
| 139 | + styleElement.appendChild(document.createTextNode(newStyle)); |
| 140 | + } |
| 141 | +} |
0 commit comments