All files / src/flow CanvasMenu.ts

96.49% Statements 248/257
93.54% Branches 29/31
88.88% Functions 8/9
96.49% Lines 248/257

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 25892x 123x 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 66x 66x 92x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 166x 166x               166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 166x 92x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 92x 11x 11x 11x 11x     11x 11x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 11x 1x 1x 1x 1x 11x 7x 7x 11x 1x 7x 7x 7x 7x 11x 11x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 92x 4x 4x 4x 4x 92x 109x 92x 66x 66x 66x 66x 66x 66x 66x 66x 92x 92x 92x 92x 92x 92x 92x 66x 66x 66x 66x 66x 88x 88x 88x 88x 88x 88x 88x 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, state } from 'lit/decorators.js';
import { RapidElement } from '../RapidElement';
import { CustomEventType } from '../interfaces';
import { ContextMenuShortcut } from './types';
 
/**
 * Event detail for canvas menu selection. `action` is either one of the
 * built-in menu actions ('sticky', 'other', 'reflow') or the `type` of a
 * configured shortcut (e.g. 'send_msg', 'say_msg', 'set_contact_field').
 */
export interface CanvasMenuSelection {
  action: string;
  position: { x: number; y: number };
}
 
/**
 * CanvasMenu - A popup menu for adding items to the flow canvas
 * Displayed when double-clicking on empty canvas space
 */
export class CanvasMenu extends RapidElement {
  static get styles() {
    return css`
      :host {
        position: fixed;
        z-index: 10000;
        display: block;
        pointer-events: none;
      }
 
      .menu {
        position: fixed;
        background: white;
        border-radius: var(--curvature);
        box-shadow: var(--dropdown-shadow);
        padding: 0.5em 0;
        width: 265px;
        pointer-events: auto;
      }
 
      .menu-item {
        padding: 0.75em 1.5em;
        cursor: pointer;
        display: flex;
        align-items: flex-start;
        gap: 0.75em;
        transition: background-color 0.15s ease;
      }
 
      .menu-item:hover {
        background-color: rgba(0, 0, 0, 0.05);
      }
 
      .menu-item temba-icon {
        --icon-color: var(--color-text);
      }
 
      .menu-item-title {
        font-weight: 500;
        font-size: 1rem;
        color: var(--color-text-dark);
      }
 
      .divider {
        height: 1px;
        background: rgba(0, 0, 0, 0.1);
        margin: 0.5em 0;
      }
    `;
  }
 
  @property({ type: Number })
  public x = 0;
 
  @property({ type: Number })
  public y = 0;
 
  @property({ type: Boolean })
  public open = false;
 
  @property({ type: Boolean })
  public showStickyNote = true;
 
  @property({ type: Array })
  public shortcuts: ContextMenuShortcut[] = [];
 
  @property({ type: Boolean })
  public showReflow = false;
 
  @state()
  private clickPosition = { x: 0, y: 0 };
 
  protected firstUpdated(
    _changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.firstUpdated(_changedProperties);
 
    // Close menu when clicking/tapping outside — use mousedown instead of
    // click to avoid being triggered by the click synthesized from a
    // drag-and-drop (mousedown on exit + mouseup on canvas = click on
    // common ancestor). Also listen for touchstart for touch devices.
    const handleClickOutside = (e: MouseEvent | TouchEvent) => {
      if (this.open && !this.contains(e.target as Node)) {
        this.close();
      }
    };
 
    document.addEventListener('mousedown', handleClickOutside);
    document.addEventListener('touchstart', handleClickOutside, {
      passive: true
    });

    // Store cleanup function
    (this as any)._clickOutsideHandler = handleClickOutside;
  }

  disconnectedCallback(): void {
    super.disconnectedCallback();
    if ((this as any)._clickOutsideHandler) {
      document.removeEventListener(
        'mousedown',
        (this as any)._clickOutsideHandler
      );
      document.removeEventListener(
        'touchstart',
        (this as any)._clickOutsideHandler
      );
    }
  }
 
  public show(
    x: number,
    y: number,
    clickPosition: { x: number; y: number },
    showStickyNote: boolean = true,
    showReflow: boolean = false,
    shortcuts: ContextMenuShortcut[] = []
  ) {
    this.x = x;
    this.y = y;
    this.clickPosition = clickPosition;
    this.showStickyNote = showStickyNote;
    this.showReflow = showReflow;
    this.shortcuts = shortcuts;
    this.open = true;
 
    // Adjust position after menu renders to ensure it fits on screen
    requestAnimationFrame(() => {
      this.adjustPosition();
    });
  }
 
  private adjustPosition(): void {
    const menuElement = this.shadowRoot?.querySelector('.menu') as HTMLElement;
    if (!menuElement) return;
 
    const menuRect = menuElement.getBoundingClientRect();
    const viewportWidth = window.innerWidth;
    const viewportHeight = window.innerHeight;
    const margin = 10; // margin from viewport edges
 
    let adjustedX = this.x;
    let adjustedY = this.y;

    // Check if menu goes off the right edge
    if (this.x + menuRect.width + margin > viewportWidth) {
      adjustedX = viewportWidth - menuRect.width - margin;
    }
 
    // Check if menu goes off the bottom edge
    if (this.y + menuRect.height + margin > viewportHeight) {
      adjustedY = viewportHeight - menuRect.height - margin;
    }
 
    // Update position if needed
    if (adjustedX !== this.x || adjustedY !== this.y) {
      this.x = adjustedX;
      this.y = adjustedY;
    }
  }
 
  public close(fireCanceledEvent: boolean = true) {
    if (this.open) {
      this.open = false;
      // Fire close event so parent can clean up, but only if not from a selection
      if (fireCanceledEvent) {
        this.fireCustomEvent(CustomEventType.Canceled, {});
      }
    }
  }
 
  private handleMenuItemClick(action: CanvasMenuSelection['action']) {
    this.fireCustomEvent(CustomEventType.Selection, {
      action,
      position: this.clickPosition
    } as CanvasMenuSelection);
    // Close without firing canceled event since we made a selection
    this.close(false);
  }
 
  public render(): TemplateResult {
    if (!this.open) {
      return html``;
    }
 
    return html`
      <div class="menu" style="left: ${this.x}px; top: ${this.y}px;">
        ${this.shortcuts.map(
          (shortcut) => html`
            <div
              class="menu-item"
              @click=${() => this.handleMenuItemClick(shortcut.type)}
            >
              <temba-icon name="${shortcut.icon}" size="1.25"></temba-icon>
              <div class="menu-item-title">${shortcut.name}</div>
            </div>
          `
        )}
 
        <div
          class="menu-item"
          @click=${() => this.handleMenuItemClick('other')}
        >
          <temba-icon name="plus" size="1.25"></temba-icon>
          <div class="menu-item-title">Add Other</div>
        </div>
 
        ${this.showStickyNote
          ? html`
              <div class="divider"></div>
 
              <div
                class="menu-item"
                @click=${() => this.handleMenuItemClick('sticky')}
              >
                <temba-icon name="note" size="1.25"></temba-icon>
                <div class="menu-item-title">Add Sticky Note</div>
              </div>
            `
          : ''}
        ${this.showReflow
          ? html`
              <div class="divider"></div>
 
              <div
                class="menu-item"
                @click=${() => this.handleMenuItemClick('reflow')}
              >
                <temba-icon name="flow" size="1.25"></temba-icon>
                <div class="menu-item-title">Reflow</div>
              </div>
            `
          : ''}
      </div>
    `;
  }
}