Example #1
0
const calcByPositions = function (testPositions1, testPositions2, targetRect, contentAreaRect, panelRect) {
  let relPos, relRect, outputPanelRect;

  const paddedContentRect = {
    x: contentAreaRect.x,
    y: contentAreaRect.y,
    w: contentAreaRect.w + (contentAreaRect.w < (panelRect.w + targetRect.w) ? panelRect.w : 0),
    h: contentAreaRect.h + (contentAreaRect.h < (panelRect.h + targetRect.h) ? panelRect.h : 0)
  };

  relPos = Rect.findBestRelativePosition(panelRect, targetRect, paddedContentRect, testPositions1);
  targetRect = Rect.clamp(targetRect, paddedContentRect);

  if (relPos) {
    relRect = Rect.relativePosition(panelRect, targetRect, relPos);
    outputPanelRect = moveTo(panelRect, relRect);
    return result(outputPanelRect, relPos);
  }

  targetRect = Rect.intersect(paddedContentRect, targetRect);
  if (targetRect) {
    relPos = Rect.findBestRelativePosition(panelRect, targetRect, paddedContentRect, testPositions2);

    if (relPos) {
      relRect = Rect.relativePosition(panelRect, targetRect, relPos);
      outputPanelRect = moveTo(panelRect, relRect);
      return result(outputPanelRect, relPos);
    }

    outputPanelRect = moveTo(panelRect, targetRect);
    return result(outputPanelRect, relPos);
  }

  return null;
};
Example #2
0
 Tools.each(tests, function (item) {
   LegacyUnit.deepEqual(
     Rect.relativePosition(sourceRect, targetRect, item[0]),
     Rect.create(item[1], item[2], item[3], item[4]),
     item[0]
   );
 });
Example #3
0
  const reposition = function (match, shouldShow?) {
    let relPos, panelRect, elementRect, contentAreaRect, panel, relRect, testPositions, smallElementWidthThreshold;
    const handler = Settings.getInlineToolbarPositionHandler(editor);

    if (editor.removed) {
      return;
    }

    if (!match || !match.toolbar.panel) {
      hideAllFloatingPanels(editor);
      return;
    }

    testPositions = [
      'bc-tc', 'tc-bc',
      'tl-bl', 'bl-tl',
      'tr-br', 'br-tr'
    ];

    panel = match.toolbar.panel;

    // Only show the panel on some events not for example nodeChange since that fires when context menu is opened
    if (shouldShow) {
      panel.show();
    }

    elementRect = getElementRect(match.element);
    panelRect = DOM.getRect(panel.getEl());
    contentAreaRect = DOM.getRect(editor.getContentAreaContainer() || editor.getBody());

    const delta = UiContainer.getUiContainerDelta(panel).getOr({ x: 0, y: 0 });
    elementRect.x += delta.x;
    elementRect.y += delta.y;
    panelRect.x += delta.x;
    panelRect.y += delta.y;
    contentAreaRect.x += delta.x;
    contentAreaRect.y += delta.y;

    smallElementWidthThreshold = 25;

    if (DOM.getStyle(match.element, 'display', true) !== 'inline') {
      // We need to use these instead of the rect values since the style
      // size properites might not be the same as the real size for a table if it has a caption
      const clientRect = match.element.getBoundingClientRect();
      elementRect.w = clientRect.width;
      elementRect.h = clientRect.height;
    }

    if (!editor.inline) {
      contentAreaRect.w = editor.getDoc().documentElement.offsetWidth;
    }

    // Inflate the elementRect so it doesn't get placed above resize handles
    if (editor.selection.controlSelection.isResizable(match.element) && elementRect.w < smallElementWidthThreshold) {
      elementRect = Rect.inflate(elementRect, 0, 8);
    }

    relPos = Rect.findBestRelativePosition(panelRect, elementRect, contentAreaRect, testPositions);
    elementRect = Rect.clamp(elementRect, contentAreaRect);

    if (relPos) {
      relRect = Rect.relativePosition(panelRect, elementRect, relPos);
      movePanelTo(panel, userConstrain(handler, relRect.x, relRect.y, elementRect, contentAreaRect, panelRect));
    } else {
      // Allow overflow below the editor to avoid placing toolbars ontop of tables
      contentAreaRect.h += panelRect.h;

      elementRect = Rect.intersect(contentAreaRect, elementRect);
      if (elementRect) {
        relPos = Rect.findBestRelativePosition(panelRect, elementRect, contentAreaRect, [
          'bc-tc', 'bl-tl', 'br-tr'
        ]);

        if (relPos) {
          relRect = Rect.relativePosition(panelRect, elementRect, relPos);
          movePanelTo(panel, userConstrain(handler, relRect.x, relRect.y, elementRect, contentAreaRect, panelRect));
        } else {
          movePanelTo(panel, userConstrain(handler, elementRect.x, elementRect.y, elementRect, contentAreaRect, panelRect));
        }
      } else {
        panel.hide();
      }
    }

    togglePositionClass(panel, relPos, function (pos1, pos2) {
      return pos1 === pos2;
    });

    // drawRect(contentAreaRect, 'blue');
    // drawRect(elementRect, 'red');
    // drawRect(panelRect, 'green');
  };