mobileTextSpanDecoratorForAttribute function

TextSpan mobileTextSpanDecoratorForAttribute(
  1. BuildContext context,
  2. Node node,
  3. int index,
  4. TextInsert text,
  5. TextSpan before,
  6. TextSpan after,
)

Support mobile platform

  • customize the href text span

Implementation

TextSpan mobileTextSpanDecoratorForAttribute(
  BuildContext context,
  Node node,
  int index,
  TextInsert text,
  TextSpan before,
  TextSpan after,
) {
  final attributes = text.attributes;
  if (attributes == null) {
    return before;
  }
  final editorState = context.read<EditorState>();

  final hrefAddress = attributes[AppFlowyRichTextKeys.href] as String?;
  if (hrefAddress != null) {
    Timer? timer;

    final tapGestureRecognizer = TapGestureRecognizer()
      ..onTapUp = (_) async {
        if (timer != null && timer!.isActive) {
          // Implement single tap logic
          safeLaunchUrl(hrefAddress);
          timer!.cancel();
          return;
        }
      };

    tapGestureRecognizer.onTapDown = (_) {
      final selection = Selection.single(
        path: node.path,
        startOffset: index,
        endOffset: index + text.text.length,
      );
      editorState.updateSelectionWithReason(
        selection,
        reason: SelectionUpdateReason.uiEvent,
      );

      timer = Timer(const Duration(milliseconds: 500), () {
        // Implement long tap logic
        showDialog<void>(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text(AppFlowyEditorL10n.current.editLink),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(4),
              ),
              content: LinkEditForm(
                node: node,
                index: index,
                hrefText: text.text,
                hrefAddress: hrefAddress,
                editorState: editorState,
                selection: selection,
              ),
            );
          },
        );
      });
    };
    return TextSpan(
      style: before.style,
      text: text.text,
      recognizer: tapGestureRecognizer,
    );
  }

  return before;
}