Пример #1
0
  /**
   * Returns the start offset of the next sibling of the parent element. Returns -1 if there is no
   * previous sibling in the parent.
   *
   * @param vexWidget VexWidget to use.
   */
  public static int getPreviousSiblingStart(IVexWidget vexWidget) {
    int startOffset;

    if (vexWidget.hasSelection()) {
      startOffset = vexWidget.getSelectionStart();
    } else {
      Box box =
          vexWidget.findInnermostBox(
              new IBoxFilter() {
                public boolean matches(Box box) {
                  return box instanceof BlockBox && box.getElement() != null;
                }
              });

      if (box.getElement() == vexWidget.getDocument().getRootElement()) {
        return -1;
      }

      startOffset = box.getElement().getStartOffset();
    }

    int previousSiblingStart = -1;
    VEXElement parent = vexWidget.getDocument().getElementAt(startOffset);
    List<VEXNode> children = parent.getChildNodes();
    for (VEXNode child : children) {
      if (startOffset == child.getStartOffset()) {
        break;
      }
      previousSiblingStart = child.getStartOffset();
    }
    return previousSiblingStart;
  }
Пример #2
0
  /**
   * Iterate over all rows in the table containing the caret.
   *
   * @param vexWidget IVexWidget to iterate over.
   * @param callback Caller-provided callback that this method calls for each row in the current
   *     table.
   */
  public static void iterateTableRows(IVexWidget vexWidget, ElementOrRangeCallback callback) {

    final StyleSheet ss = vexWidget.getStyleSheet();
    final VEXDocument doc = vexWidget.getDocument();
    final int offset = vexWidget.getCaretOffset();

    // This may or may not be a table
    // In any case, it's the element that contains the top-level table
    // children
    VEXElement table = doc.getElementAt(offset);

    while (table != null && !LayoutUtils.isTableChild(ss, table)) {
      table = table.getParent();
    }

    while (table != null && LayoutUtils.isTableChild(ss, table)) {
      table = table.getParent();
    }

    if (table == null || table.getParent() == null) {
      return;
    }

    final List<Element> tableChildren = new ArrayList<Element>();
    final boolean[] found = new boolean[] {false};
    LayoutUtils.iterateChildrenByDisplayStyle(
        ss,
        LayoutUtils.TABLE_CHILD_STYLES,
        table,
        new ElementOrRangeCallback() {
          public void onElement(Element child, String displayStyle) {
            if (offset >= child.getStartOffset() && offset <= child.getEndOffset()) {
              found[0] = true;
            }
            tableChildren.add(child);
          }

          public void onRange(VEXElement parent, int startOffset, int endOffset) {
            if (!found[0]) {
              tableChildren.clear();
            }
          }
        });

    if (!found[0]) {
      return;
    }

    int startOffset = tableChildren.get(0).getStartOffset();
    int endOffset = tableChildren.get(tableChildren.size() - 1).getEndOffset() + 1;
    LayoutUtils.iterateTableRows(ss, table, startOffset, endOffset, callback);
  }