removeLink method

void removeLink({
  1. int? blockIndex,
  2. TextSelection? selection,
})

Removes the hyperlink from the current selection or active link span.

Implementation

void removeLink({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) {
      _documentController.applyCellLink(info.blockIndex, info.row,
          info.col, effectiveSelection.start, effectiveSelection.end, null);
    } else {
      final offset = effectiveSelection?.start ??
          _editorWidgetState?.cursorOffset ??
          0;
      final cell = _documentController.document.blocks[info.blockIndex]
          as TableNode;
      final cellNode = cell.getCell(info.row, info.col);
      final loc = cellNode.getSpanAt(offset);
      if (loc.spanIndex < cellNode.spans.length) {
        var spanStart = 0;
        for (var i = 0; i < loc.spanIndex; i++) {
          spanStart += cellNode.spans[i].text.length;
        }
        final spanEnd = spanStart + cellNode.spans[loc.spanIndex].text.length;
        _documentController.applyCellLink(info.blockIndex, info.row,
            info.col, spanStart, spanEnd, null);
      }
    }
  } else {
    if (effectiveSelection != null &&
        effectiveSelection.isValid &&
        !effectiveSelection.isCollapsed) {
      _documentController.applyLink(effectiveBlockIndex,
          effectiveSelection.start, effectiveSelection.end, null);
    } else {
      final offset = effectiveSelection?.start ??
          _editorWidgetState?.cursorOffset ??
          0;
      final block =
          _documentController.document.blocks[effectiveBlockIndex];
      final loc = block.getSpanAt(offset);
      if (loc.spanIndex < block.spans.length) {
        var spanStart = 0;
        for (var i = 0; i < loc.spanIndex; i++) {
          spanStart += block.spans[i].text.length;
        }
        final spanEnd = spanStart + block.spans[loc.spanIndex].text.length;
        _documentController.applyLink(
            effectiveBlockIndex, spanStart, spanEnd, null);
      }
    }
  }
  _editorWidgetState?.rebuild();
}