Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom links #124

Open
Ai-Kiwi opened this issue Aug 16, 2023 · 1 comment
Open

Custom links #124

Ai-Kiwi opened this issue Aug 16, 2023 · 1 comment

Comments

@Ai-Kiwi
Copy link

Ai-Kiwi commented Aug 16, 2023

Is there/there should be, a way to create custom links for things such as going @userid, ideally the system would then replace the userId which is the real text with the username and allow it to be clickable and run whatever code you have.

@Vov4yk
Copy link

Vov4yk commented Dec 21, 2023

Hello @Ai-Kiwi , I'm working on the same task.
It looks like the library allow us to do it in our code.
(there are snippets just to give the idea how to work, code could be optimized)

  1. Create own LinkableElement
class MentionElement extends LinkableElement {
  MentionElement(String url, this.mention, [String? text]) : super(text, url);
  final MentionedChatUser mention;

  @override
  String toString() {
    return "MentionElement: '$url' ($text)";
  }

  @override
  bool operator ==(other) => equals(other);

  @override
  bool equals(other) => other is MentionElement && super.equals(other);
}
  1. Create own linkifier
class MentionLinkifier extends Linkifier {
  final List<MentionedChatUser> mentions;

  MentionLinkifier({required this.mentions});

  @override
  List<LinkifyElement> parse(
    List<LinkifyElement> elements,
    LinkifyOptions options,
  ) {
    final list = <LinkifyElement>[];

    elements.forEach((element) {
      String objText = element.text;
      if (element is TextElement && mentions.isNotEmpty) {
        final atIndex = element.text.indexOf('@');
        if (atIndex >= 0) {
          if (atIndex != 0) {
            list.add(TextElement(objText.substring(0, atIndex)));
            objText = objText.substring(atIndex);
          }
          final comps = objText.split(' ');
          final mention =
              mentions.firstWhereOrNull((e) => '@${e.tag}' == comps.first);
          if (mention != null) {
            list.add(MentionElement('${mention.user.name}', mention));
          } else {
            list.add(TextElement('@'));
          }
          if (comps.length > 1) {
            final toCut = '@${mention?.tag ?? ''}';
            list.addAll(
                parse([TextElement(objText.substring(toCut.length))], options));
          }
        } else {
          list.add(TextElement(objText));
        }
      } else {
        list.add(element);
      }
    });
    return list;
  }
}
  1. Add yours linkifier during Linkify object creation
linkifiers: [
        const UrlLinkifier(),
        const EmailLinkifier(),
        if (mentions.isNotEmpty) MentionLinkifier(mentions: mentions),
      ],
  1. Add some logic to onOpen
onOpen: (link) async {
        UrlLaunchHelper.tryLaunch(context, link.url);
        if (link is MentionLinkifier) {
          ...
        }
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants