All files / src/list ShortcutList.ts

55.05% Statements 109/198
100% Branches 2/2
18.18% Functions 2/11
55.05% Lines 109/198

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 19992x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x       92x                                                                                                       92x       92x   92x                                                               92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x  
import { css, html, PropertyValueMap, TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';
import { CustomEventType, Shortcut } from '../interfaces';
import { StoreMonitorElement } from '../store/StoreMonitorElement';
import { Options } from '../display/Options';
 
export class ShortcutList extends StoreMonitorElement {
  static get styles() {
    return css`
      temba-options {
        display: block;
        width: 100%;
        flex-grow: 1;
      }
 
      .options-empty {
        height: 0;
        overflow: hidden;
      }
 
      .no-match {
        margin: 5px 10px;
        padding: 10px;
      }
 
      .filter {
        background: #f3f3f3;
        padding: 0.1em 0.3em;
        border-radius: var(--curvature);
      }
 
      .search-box {
        padding: 6px 8px;
        border-bottom: 1px solid var(--color-widget-border, #e6e6e6);
      }
 
      .search-box input {
        width: 100%;
        border: none;
        outline: none;
        font-size: 0.9em;
        padding: 4px 0;
        background: transparent;
        font-family: inherit;
      }
    `;
  }
 
  @property({ type: String })
  filter: string;
 
  @property({ type: Boolean })
  showSearch = false;
 
  @property({ type: Array })
  filteredShortcuts = [];
 
  @property({ type: Number })
  cursorIndex = 0;
 
  constructor() {
    super();
  }
 
  protected firstUpdated(
    changes: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.firstUpdated(changes);
  }
 
  public storeUpdated(): void {
    this.filteredShortcuts = this.store.getShortcuts();
  }
 
  public updated(
    changes: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.updated(changes);
 
    if (changes.has('filter')) {
      if (!this.filter) {
        this.filteredShortcuts = this.store.getShortcuts();
      } else {
        this.filteredShortcuts = this.store
          .getShortcuts()
          .filter((shortcut: Shortcut) =>
            shortcut.name.toLowerCase().includes(this.filter.toLowerCase())
          );
      }
      this.cursorIndex = 0;
    }
  }

  public renderShortcut(shortcut: Shortcut): TemplateResult {
    return html`<div style="display:flex;align-items: center;min-width:0">
      <div
        style="
          overflow: hidden;
          text-overflow: ellipsis;
          width:175px; 
          padding-right: 10px;
          white-space: nowrap;"
      >
        ${shortcut.name}
      </div>
      <div
        style=" 
          font-size: 0.9em;
          color: rgba(0, 0, 0, 0.4);
          flex:1;
          overflow: hidden;
          text-overflow: ellipsis;

          display: -webkit-box;
          line-height: 16px;
          max-height: 16px;
          -webkit-line-clamp: 1;
          -webkit-box-orient: vertical;
        "
      >
        ${shortcut.text}
      </div>
    </div>`;
  }

  public getShortcut() {
    const options = this.shadowRoot.querySelector('temba-options') as Options;
    return options.getSelection();
  }

  private handleSearchInput(evt: Event) {
    this.filter = (evt.target as HTMLInputElement).value;
  }

  private handleSearchKeyDown(evt: KeyboardEvent) {
    if (evt.key === 'ArrowDown') {
      evt.preventDefault();
      this.cursorIndex = Math.min(
        this.cursorIndex + 1,
        this.filteredShortcuts.length - 1
      );
    } else if (evt.key === 'ArrowUp') {
      evt.preventDefault();
      this.cursorIndex = Math.max(this.cursorIndex - 1, 0);
    } else if (evt.key === 'Enter') {
      evt.preventDefault();
      evt.stopPropagation();
      if (this.filteredShortcuts.length > 0) {
        const selected = this.filteredShortcuts[this.cursorIndex];
        this.fireCustomEvent(CustomEventType.Selection, { selected });
      }
    }
  }

  public focusSearch() {
    const input = this.shadowRoot?.querySelector(
      '.search-box input'
    ) as HTMLInputElement;
    if (input) {
      input.focus();
    }
  }

  public render(): TemplateResult {
    return html`
      ${this.showSearch
        ? html`<div class="search-box">
            <input
              type="text"
              placeholder="Search shortcuts..."
              @input=${this.handleSearchInput}
              @keydown=${this.handleSearchKeyDown}
            />
          </div>`
        : null}
      ${this.filteredShortcuts.length === 0
        ? html`<div class="no-match">
            ${this.filter
              ? html`No matches for <span class="filter">${this.filter}</span>.`
              : html`No shortcuts yet, create some to quickly include them in
                your messages.`}
          </div>`
        : null}
 
      <temba-options
        class="options-${this.filteredShortcuts.length === 0
          ? 'empty'
          : 'full'}"
        block
        cursorHover
        visible
        .renderOption=${this.renderShortcut}
        .cursorIndex=${this.cursorIndex}
        .options=${this.filteredShortcuts}
      ></temba-options>
    `;
  }
}