Commit c7d9c9b3 authored by dmacfarlane's avatar dmacfarlane
Browse files

fix: menu postion when element is in a table

parent b175f3ed
Loading
Loading
Loading
Loading
+16 −11
Original line number Diff line number Diff line
@@ -34,35 +34,40 @@ import { getCaretCoordinates } from './caret-coords';
export class MentionListComponent {
  items = [];
  activeIndex: number = 0;
  hidden = false;
  hidden: boolean = false;
  @Output() itemClick = new EventEmitter();
  constructor(private _element: ElementRef) {}

  // lots of confusion here between relative coordinates and containers
  position(nativeParentElement: HTMLInputElement, iframe: HTMLIFrameElement = null) {
    let coords = { top: 0, left: 0 };
    if (isInputOrTextAreaElement(nativeParentElement)) {
      coords = getCaretCoordinates(nativeParentElement, nativeParentElement.selectionStart);
      coords.top = nativeParentElement.offsetTop + coords.top + 16;
      coords.left = nativeParentElement.offsetLeft + coords.left;
      let doc = document.documentElement;
      let scrollLeft = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
      let scrollTop = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);

      // bounding rectangles are relative to view, offsets are relative to container?
      let carret = getCaretCoordinates(nativeParentElement, nativeParentElement.selectionStart);
      let parentRelativeToContainer: any = nativeParentElement.getBoundingClientRect();

      coords.top = carret.top + parentRelativeToContainer.y + scrollTop + 16;
      coords.left = carret.left + parentRelativeToContainer.x + scrollLeft;
    }
    else if (iframe) {
      let context: { iframe: HTMLIFrameElement, parent: Element } = { iframe: iframe, parent: iframe.offsetParent };
      coords = getContentEditableCaretCoords(context);
    }
    else {
      // lots of confusion here between relative coordinates and containers
      let doc = document.documentElement;
      let scrollLeft = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
      let scrollTop = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);

      // bounding rectables are relative to view, offsets are relative to container?
      // bounding rectangles are relative to view, offsets are relative to container?
      let caretRelativeToView = getContentEditableCaretCoords({ iframe: iframe });
      let parentRelativeToContainer: any = nativeParentElement.getBoundingClientRect();

      coords.top = caretRelativeToView.top - parentRelativeToContainer.y +
        nativeParentElement.offsetTop - scrollTop;
      coords.left = caretRelativeToView.left - parentRelativeToContainer.x +
        nativeParentElement.offsetLeft - scrollLeft;
      coords.top = caretRelativeToView.top - parentRelativeToContainer.y + nativeParentElement.offsetTop - scrollTop;
      coords.left = caretRelativeToView.left - parentRelativeToContainer.x + nativeParentElement.offsetLeft - scrollLeft;
    }
    let el: HTMLElement = this._element.nativeElement;
    el.style.position = "absolute";