getLinkInfo method

Map<String, String?> getLinkInfo({
  1. int? blockIndex,
  2. TextSelection? selection,
})

Returns existing link text and url for the current or specified selection.

Implementation

Map<String, String?> getLinkInfo(
    {int? blockIndex, TextSelection? selection}) {
  final effectiveBlockIndex =
      blockIndex ?? _editorWidgetState?.focusedBlockIndex ?? 0;
  final effectiveSelection =
      selection ?? _editorWidgetState?.selectionForToolbar;
  final info = focusedTableInfo;

  if (info != null) {
    if (effectiveSelection != null &&
        effectiveSelection.isValid &&
        !effectiveSelection.isCollapsed) {
      final cell = _documentController.document.blocks[info.blockIndex]
          as TableNode;
      final cellNode = cell.getCell(info.row, info.col);
      final text = cellNode.plainText;
      final selectedText = (effectiveSelection.start >= 0 &&
              effectiveSelection.end <= text.length)
          ? text.substring(effectiveSelection.start, effectiveSelection.end)
          : null;
      final linkInfo = _documentController.getCellLinkInfoAt(
          info.blockIndex, info.row, info.col, effectiveSelection.start);
      return {
        'text': selectedText ?? linkInfo['text'],
        'url': linkInfo['url'],
      };
    } else {
      final offset = effectiveSelection?.start ??
          _editorWidgetState?.cursorOffset ??
          0;
      return _documentController.getCellLinkInfoAt(
          info.blockIndex, info.row, info.col, offset);
    }
  } else {
    if (effectiveSelection != null &&
        effectiveSelection.isValid &&
        !effectiveSelection.isCollapsed) {
      if (effectiveBlockIndex >= 0 &&
          effectiveBlockIndex < _documentController.document.blocks.length) {
        final block =
            _documentController.document.blocks[effectiveBlockIndex];
        final text = block.plainText;
        final selectedText = (effectiveSelection.start >= 0 &&
                effectiveSelection.end <= text.length)
            ? text.substring(
                effectiveSelection.start, effectiveSelection.end)
            : null;
        final linkInfo = _documentController.getLinkInfoAt(
            effectiveBlockIndex, effectiveSelection.start);
        return {
          'text': selectedText ?? linkInfo['text'],
          'url': linkInfo['url'],
        };
      }
    } else {
      final offset =
          effectiveSelection?.start ?? _editorWidgetState?.cursorOffset ?? 0;
      return _documentController.getLinkInfoAt(effectiveBlockIndex, offset);
    }
  }
  return {'text': null, 'url': null};
}