All files / src/flow EditorToolbar.ts

98.16% Statements 590/601
83.33% Branches 70/84
85.71% Functions 12/14
98.16% Lines 590/601

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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 60296x 425x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 58x 58x 57x 57x 57x 58x 1x 1x 58x 1x 1x 1x 1x 1x 1x 1x 1x 58x 6x 6x 58x 58x 58x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 93x 96x 31x 31x 31x 31x 96x 26x 26x 26x 26x 25x 25x 26x 26x 26x 26x 26x 96x 96x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x   25x 25x 25x       25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 476x 476x 476x 476x 476x 476x 476x 476x 476x 96x 4063x 27x 27x 4063x 4063x 96x 483x 483x 483x 483x 483x 483x 483x 483x 483x 161x 161x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 161x 322x 322x 322x 322x 483x 483x 483x 483x 120x 120x 120x 120x 483x 483x 483x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 483x               483x 120x 483x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 69x 69x 96x 96x 96x 96x 96x 96x 96x 69x 69x 69x 96x 96x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 267x 267x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 209x 209x 127x 127x 267x 267x 267x 70x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 267x 127x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 69x 69x 69x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 195x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 69x 69x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x  
import { css, html, TemplateResult } from 'lit';
import { property, state } from 'lit/decorators.js';
import { RapidElement } from '../RapidElement';
import { CustomEventType } from '../interfaces';
import { Icon } from '../Icons';
 
export interface LanguageOption {
  name: string;
  value: string;
  percent?: number;
}
 
export const PRIMARY_LANGUAGE_OPTION_VALUE = '__primary_language__';
 
export class EditorToolbar extends RapidElement {
  static get styles() {
    return css`
      :host {
        display: block;
      }
 
      .editor-toolbar {
        --toolbar-control-height: 28px;
        --toolbar-translation-control-height: 28px;
        display: flex;
        align-items: center;
        padding: 6px 12px;
        background: #fff;
        border-bottom: 1px solid #e8e8e8;
        flex-shrink: 0;
        gap: 8px;
      }
 
      .toolbar-left {
        display: flex;
        align-items: center;
        gap: 2px;
      }
 
      .toolbar-right {
        display: flex;
        align-items: center;
        gap: 2px;
        margin-left: auto;
      }
 
      .toolbar-btn {
        width: var(--toolbar-control-height);
        height: var(--toolbar-control-height);
        border: none;
        background: transparent;
        border-radius: var(--curvature);
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0;
        color: #888;
        font-size: 16px;
        line-height: 1;
        outline: none;
      }
 
      .toolbar-btn:focus {
        outline: none;
      }
 
      .toolbar-btn:focus-visible {
        outline: 2px solid #0064c8;
        outline-offset: 2px;
      }
 
      .toolbar-btn:hover {
        background: rgba(0, 0, 0, 0.06);
        color: #555;
      }
 
      .toolbar-btn:disabled {
        opacity: 0.3;
        cursor: default;
        background: transparent;
      }
 
      .toolbar-btn.active {
        background: rgba(0, 100, 200, 0.1);
        color: #0064c8;
      }
 
      .toolbar-btn.active:hover {
        background: rgba(0, 100, 200, 0.15);
      }
 
      .toolbar-tip {
        display: flex;
        align-items: center;
      }
 
      .toolbar-divider {
        width: 1px;
        height: 16px;
        background: #e0e0e0;
        margin: 0 4px;
      }
 
      .toolbar-group {
        display: flex;
        align-items: center;
        gap: 4px;
        height: var(--toolbar-control-height);
        box-sizing: border-box;
        padding: 0 3px;
        border: 1px solid #d7dce2;
        border-radius: calc(var(--curvature) + 2px);
        background: #f7f9fb;
      }
 
      .toolbar-group-divider {
        width: 1px;
        height: 18px;
        background: #d7dce2;
        margin: 0 2px;
      }
 
      .toolbar-language {
        position: relative;
        display: flex;
        align-items: center;
      }
 
      .toolbar-language-group {
        display: flex;
        align-items: center;
        gap: 6px;
        margin-left: 2px;
      }
 
      .toolbar-zoom-group {
        gap: 2px;
      }
 
      .language-pill {
        display: flex;
        align-items: center;
        gap: 6px;
        background: #e9eef4;
        color: #0064c8;
        height: var(--toolbar-translation-control-height);
        padding: 0 8px;
        border-radius: var(--curvature);
        box-sizing: border-box;
        font-size: 13px;
        font-weight: 400;
        white-space: nowrap;
        cursor: pointer;
        --icon-color: #0064c8;
        --icon-size: 16px;
        border: none;
        outline: none;
      }
 
      .language-pill:hover {
        filter: brightness(1.04);
      }
 
      .language-pill.primary {
        background: #fff;
        border: 1px solid #d7dce2;
      }
 
      .language-pill.complete {
        background: #d4f5e0;
        color: #1a7f37;
        --icon-color: #1a7f37;
      }
 
      .language-pill-caret {
        margin-left: 1px;
        --icon-color: currentColor;
        --icon-size: 12px;
      }
 
      .language-percent {
        display: inline-block;
        font-size: 12px;
        font-weight: 700;
        line-height: 1;
        color: #0064c8;
        white-space: nowrap;
      }
 
      .language-pill.complete .language-percent {
        color: #1a7f37;
      }
 
      .toolbar-zoom-level {
        font-size: 12px;
        min-width: 40px;
        text-align: center;
        color: #555;
        font-weight: 500;
      }
 
      .toolbar-translation {
        display: flex;
        align-items: center;
        gap: 4px;
      }
 
      .toolbar-btn.language-tool {
        width: var(--toolbar-translation-control-height);
        height: var(--toolbar-translation-control-height);
      }
    `;
  }
 
  @property({ type: Boolean, attribute: 'message-view' })
  messageView = false;
 
  @property({ type: Number })
  zoom = 1.0;
 
  @property({ type: Boolean, attribute: 'zoom-initialized' })
  zoomInitialized = false;
 
  @property({ type: Boolean, attribute: 'zoom-fitted' })
  zoomFitted = false;
 
  @property({ type: Boolean, attribute: 'revisions-active' })
  revisionsActive = false;
 
  @property({ type: Boolean, attribute: 'is-saving' })
  isSaving = false;
 
  @property({ type: Boolean, attribute: 'search-disabled' })
  searchDisabled = false;
 
  @property({ type: Array })
  languageOptions: LanguageOption[] = [];
 
  @property({ type: String, attribute: 'current-language-name' })
  currentLanguageName = '';
 
  @property({ type: Boolean, attribute: 'is-base-language' })
  isBaseLanguage = true;
 
  @property({ type: Number, attribute: 'language-percent' })
  languagePercent = 0;
 
  @property({ type: Boolean, attribute: 'show-localization-tools' })
  showLocalizationTools = false;
 
  @property({ type: Boolean, attribute: 'has-pending-translations' })
  hasPendingTranslations = false;
 
  @property({ type: Boolean, attribute: 'auto-translate-disabled' })
  autoTranslateDisabled = false;
 
  @property({ type: Boolean, attribute: 'auto-translating' })
  autoTranslating = false;
 
  @state()
  private showLanguageOptions = false;
 
  private fireToolbarAction(action: string, detail: any = {}): void {
    this.fireCustomEvent(CustomEventType.ButtonClicked, { action, ...detail });
  }
 
  private handleLanguageIconClick(): void {
    if (this.showLanguageOptions) {
      this.showLanguageOptions = false;
      return;
    }
    this.showLanguageOptions = true;
    requestAnimationFrame(() => {
      const close = () => {
        this.showLanguageOptions = false;
        document.removeEventListener('click', close);
      };
      document.addEventListener('click', close, { once: true });
    });
  }
 
  private handleLanguageOptionSelected(event: CustomEvent): void {
    if (!this.showLanguageOptions) return;
    const selected = event.detail?.selected;
    if (selected?.value === PRIMARY_LANGUAGE_OPTION_VALUE) {
      this.fireToolbarAction('language-change', { isPrimary: true });
    } else if (selected?.value) {
      this.fireToolbarAction('language-change', {
        languageCode: selected.value
      });
    }
    this.showLanguageOptions = false;
  }
 
  private isMacPlatform(): boolean {
    return (
      typeof navigator !== 'undefined' &&
      /Mac|iPod|iPhone|iPad/.test(navigator.platform)
    );
  }
 
  private getSearchShortcutLabel(): string {
    return this.isMacPlatform() ? '⌘F' : 'Ctrl+F';
  }
 
  private renderTip(
    text: string | TemplateResult,
    content: TemplateResult
  ): TemplateResult {
    return html`
      <temba-tip
        class="toolbar-tip"
        .text=${typeof text === 'string' ? text : ''}
        .content=${typeof text === 'string' ? null : text}
        position="top"
      >
        ${content}
      </temba-tip>
    `;
  }
 
  private renderShortcutLabel(label: string, shortcut: string): TemplateResult {
    return html`<span style="display:inline-flex; align-items:center; gap:8px;">
      <span>${label}</span>
      <kbd>${shortcut}</kbd>
    </span>`;
  }
 
  private renderLanguageOption(
    option: LanguageOption,
    selected: boolean
  ): TemplateResult {
    if (option.value === PRIMARY_LANGUAGE_OPTION_VALUE) {
      const primaryBackground = selected ? '#e1e8ef' : '#edf1f5';
      return html`
        <div
          style="display:flex; align-items:center; justify-content:space-between; gap:8px; background:${primaryBackground}; color:#2f3f52; border-radius:4px; padding:6px 10px;"
        >
          <span>${option.name}</span>
          <span
            style="display:inline-flex; align-items:center; border-radius:999px; background:rgba(47, 63, 82, 0.12); color:#2f3f52; font-size:10px; font-weight:700; line-height:1; padding:3px 7px;"
            >Original</span
          >
        </div>
      `;
    }
 
    const isComplete = option.percent === 100;
    const optionBg = isComplete ? '#d4f5e0' : '';
    const optionHoverBg = isComplete ? '#c0edce' : '';
    const optionRadius = isComplete ? 'border-radius:4px;' : '';
    const percentColor = isComplete ? 'color:#1a7f37;' : 'color:#5f6b7a;';
 
    return html`
      <div
        style="display:flex; align-items:center; justify-content:space-between; gap:8px; padding:6px 10px; ${optionBg
          ? `background:${optionBg};`
          : ''} ${optionRadius}"
        @mouseenter=${isComplete
          ? (e: MouseEvent) => {
              (e.currentTarget as HTMLElement).style.background = optionHoverBg;
            }
          : null}
        @mouseleave=${isComplete
          ? (e: MouseEvent) => {
              (e.currentTarget as HTMLElement).style.background = optionBg;
            }
          : null}
      >
        <span style="${isComplete ? 'color:#1a7f37;' : ''}"
          >${option.name}</span
        >
        <span style="font-size:11px; font-weight:600; ${percentColor}"
          >${option.percent ?? 0}%</span
        >
      </div>
    `;
  }
 
  public render(): TemplateResult {
    const showLanguageControls = this.languageOptions.length > 1;
    const searchTargetLabel = this.messageView ? 'Search table' : 'Search flow';
 
    return html`
      <div class="editor-toolbar">
        <div class="toolbar-left">
          ${this.renderTip(
            'Flow View',
            html`
              <button
                class="toolbar-btn ${!this.messageView ? 'active' : ''}"
                @click=${() =>
                  this.fireToolbarAction('view-change', { view: 'flow' })}
                aria-label="Flow View"
              >
                <temba-icon name="flow" size="1"></temba-icon>
              </button>
            `
          )}
          ${this.renderTip(
            'Table View',
            html`
              <button
                class="toolbar-btn ${this.messageView ? 'active' : ''}"
                @click=${() =>
                  this.fireToolbarAction('view-change', { view: 'table' })}
                aria-label="Table View"
              >
                <temba-icon name=${Icon.quick_replies} size="1"></temba-icon>
              </button>
            `
          )}
          ${showLanguageControls
            ? html`
                <div class="toolbar-divider"></div>
                <div class="toolbar-language-group">
                  <div class="toolbar-language">
                    ${this.renderTip(
                      'Change language',
                      html`
                        <button
                          class="language-pill ${this.isBaseLanguage
                            ? 'primary'
                            : this.languagePercent === 100
                              ? 'complete'
                              : ''}"
                          id="language-btn"
                          @click=${this.handleLanguageIconClick}
                          aria-label="Change language"
                        >
                          <temba-icon name=${Icon.language}></temba-icon>
                          <span>${this.currentLanguageName}</span>
                          ${!this.isBaseLanguage
                            ? html`<span class="language-percent"
                                >${this.languagePercent}%</span
                              >`
                            : ''}
                          <temba-icon
                            class="language-pill-caret"
                            name=${this.showLanguageOptions
                              ? Icon.arrow_up
                              : Icon.arrow_down}
                          ></temba-icon>
                        </button>
                      `
                    )}
                    <temba-options
                      .anchorTo=${this.shadowRoot?.querySelector(
                        '#language-btn'
                      ) as HTMLElement}
                      .options=${this.languageOptions}
                      .renderOption=${this.renderLanguageOption}
                      ?visible=${this.showLanguageOptions}
                      @temba-selection=${this.handleLanguageOptionSelected}
                      style="--temba-options-option-margin:4px; --temba-options-option-padding:0; --temba-options-option-radius:4px;"
                      min-width="230"
                    ></temba-options>
                  </div>
                  ${this.showLocalizationTools
                    ? this.renderTranslationTools()
                    : ''}
                </div>
              `
            : ''}
        </div>
        <div class="toolbar-right">
          ${!this.messageView
            ? html`
                ${this.renderTip(
                  'Zoom to fit',
                  html`
                    <button
                      class="toolbar-btn"
                      @click=${() => this.fireToolbarAction('zoom-to-fit')}
                      ?disabled=${!this.zoomInitialized || this.zoomFitted}
                      aria-label="Zoom to fit"
                    >
                      <temba-icon name=${Icon.zoom_fit} size="1"></temba-icon>
                    </button>
                  `
                )}
                <div class="toolbar-divider"></div>
                ${this.renderTip(
                  'Zoom out',
                  html`
                    <button
                      class="toolbar-btn"
                      @click=${() => this.fireToolbarAction('zoom-out')}
                      ?disabled=${!this.zoomInitialized || this.zoom <= 0.3}
                      aria-label="Zoom out"
                    >
                      −
                    </button>
                  `
                )}
                <span class="toolbar-zoom-level"
                  >${this.zoomInitialized
                    ? `${Math.round(this.zoom * 100)}%`
                    : ''}</span
                >
                ${this.renderTip(
                  'Zoom in',
                  html`
                    <button
                      class="toolbar-btn"
                      @click=${() => this.fireToolbarAction('zoom-in')}
                      ?disabled=${!this.zoomInitialized || this.zoom >= 1.0}
                      aria-label="Zoom in"
                    >
                      +
                    </button>
                  `
                )}
                <div class="toolbar-divider"></div>
                ${this.renderTip(
                  'Zoom to 100%',
                  html`
                    <button
                      class="toolbar-btn"
                      @click=${() => this.fireToolbarAction('zoom-to-full')}
                      ?disabled=${!this.zoomInitialized || this.zoom >= 1.0}
                      aria-label="Zoom to 100%"
                    >
                      <temba-icon name=${Icon.zoom_in} size="1"></temba-icon>
                    </button>
                  `
                )}
                <div class="toolbar-divider"></div>
              `
            : ''}
          ${this.renderTip(
            'Revisions',
            html`
              <button
                class="toolbar-btn ${this.revisionsActive ? 'active' : ''}"
                @click=${() => this.fireToolbarAction('revisions')}
                aria-label="Revisions"
              >
                <temba-icon
                  name=${this.isSaving ? 'progress_spinner' : 'revisions'}
                  size="1"
                  ?spin=${this.isSaving}
                ></temba-icon>
              </button>
            `
          )}
          <div class="toolbar-divider"></div>
          ${this.renderTip(
            this.renderShortcutLabel(
              searchTargetLabel,
              this.getSearchShortcutLabel()
            ),
            html`
              <button
                class="toolbar-btn"
                @click=${() => this.fireToolbarAction('search')}
                ?disabled=${this.searchDisabled}
                aria-label=${searchTargetLabel}
              >
                <temba-icon name=${Icon.search} size="1"></temba-icon>
              </button>
            `
          )}
        </div>
      </div>
    `;
  }
 
  private renderTranslationTools(): TemplateResult {
    if (!this.hasPendingTranslations && !this.autoTranslating) {
      return html``;
    }
    const label = this.autoTranslating
      ? 'Stop auto translate'
      : 'Auto translate';
    return html`
      <div class="toolbar-translation">
        ${this.renderTip(
          label,
          html`
            <button
              class="toolbar-btn language-tool ${this.autoTranslating
                ? 'active'
                : ''}"
              @click=${() => this.fireToolbarAction('auto-translate')}
              ?disabled=${this.autoTranslateDisabled}
              aria-label=${label}
            >
              <temba-icon
                name=${this.autoTranslating ? 'progress_spinner' : Icon.ai}
                size="1"
                ?spin=${this.autoTranslating}
              ></temba-icon>
            </button>
          `
        )}
      </div>
    `;
  }
}