Unverified Commit 082559b5 authored by Dan MacFarlane's avatar Dan MacFarlane Committed by GitHub
Browse files

Merge pull request #88 from mcr85/master

fix menu positioning when text goes out of bounds of input or textarea (scrolled)
parents 7cee78a1 35ef283e
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
{
{
  "name": "angular-mentions",
  "name": "angular-mentions",
  "version": "0.9.0",
  "version": "0.9.1",
  "description": "Angular mentions for text fields.",
  "description": "Angular mentions for text fields.",
  "keywords": [
  "keywords": [
    "angular",
    "angular",
+17 −2
Original line number Original line Diff line number Diff line
@@ -52,6 +52,7 @@ export class MentionListComponent implements OnInit {
  items = [];
  items = [];
  activeIndex: number = 0;
  activeIndex: number = 0;
  hidden: boolean = false;
  hidden: boolean = false;
  private blockCursorSize;
  constructor(private element: ElementRef) {}
  constructor(private element: ElementRef) {}


  ngOnInit() {
  ngOnInit() {
@@ -64,10 +65,13 @@ export class MentionListComponent implements OnInit {
  position(nativeParentElement: HTMLInputElement, iframe: HTMLIFrameElement = null, dropUp: boolean) {
  position(nativeParentElement: HTMLInputElement, iframe: HTMLIFrameElement = null, dropUp: boolean) {
    let coords = { top: 0, left: 0 };
    let coords = { top: 0, left: 0 };
    if (isInputOrTextAreaElement(nativeParentElement)) {
    if (isInputOrTextAreaElement(nativeParentElement)) {
      this.blockCursorSize = this.getBlockCursorDimensions(nativeParentElement);
      const scrollToHeightDiff = (nativeParentElement.scrollHeight - nativeParentElement.clientHeight);
      const scrollToWidthDiff = (nativeParentElement.scrollWidth - nativeParentElement.clientWidth);
      // parent elements need to have postition:relative for this to work correctly?
      // parent elements need to have postition:relative for this to work correctly?
      coords = getCaretCoordinates(nativeParentElement, nativeParentElement.selectionStart);
      coords = getCaretCoordinates(nativeParentElement, nativeParentElement.selectionStart);
      coords.top = nativeParentElement.offsetTop + coords.top + 16;
      coords.top = nativeParentElement.offsetTop - scrollToHeightDiff + coords.top + this.blockCursorSize.height;
      coords.left = nativeParentElement.offsetLeft + coords.left;
      coords.left = nativeParentElement.offsetLeft - (scrollToWidthDiff ? scrollToWidthDiff + this.blockCursorSize.width : 0) + coords.left;
    }
    }
    else if (iframe) {
    else if (iframe) {
      let context: { iframe: HTMLIFrameElement, parent: Element } = { iframe: iframe, parent: iframe.offsetParent };
      let context: { iframe: HTMLIFrameElement, parent: Element } = { iframe: iframe, parent: iframe.offsetParent };
@@ -134,4 +138,15 @@ export class MentionListComponent implements OnInit {
  resetScroll() {
  resetScroll() {
    this.list.nativeElement.scrollTop = 0;
    this.list.nativeElement.scrollTop = 0;
  }
  }

  private getBlockCursorDimensions(nativeParentElement) {
    if (this.blockCursorSize) {
      return this.blockCursorSize;
    }
    const parentStyles = window.getComputedStyle(nativeParentElement);
    return {
      height: parseFloat(parentStyles.lineHeight),
      width: parseFloat(parentStyles.fontSize)
    };
  }
}
}