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

fix: updated link preview node attributes #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import 'package:flutter/material.dart';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher_string.dart';

class LinkPreviewBlockKeys {
const LinkPreviewBlockKeys._();

static const String type = 'link_preview';
static const String url = 'url';
static const String title = 'title';
static const String description = 'description';
}

Node linkPreviewNode({required String url}) => Node(
Expand Down Expand Up @@ -132,6 +135,8 @@ class LinkPreviewBlockComponentState extends State<LinkPreviewBlockComponent>
final showActionsNotifier = ValueNotifier<bool>(false);
bool alwaysShowMenu = false;

late final editorState = Provider.of<EditorState>(context, listen: false);

@override
void initState() {
super.initState();
Expand All @@ -141,6 +146,34 @@ class LinkPreviewBlockComponentState extends State<LinkPreviewBlockComponent>

parser = LinkPreviewParser(url: url, cache: widget.cache);
future = parser.start();

future.then(
(_) => WidgetsBinding.instance.addPostFrameCallback((_) => _updateNode()),
);
Comment on lines 148 to +152
Copy link
Contributor

@Xazin Xazin Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor thing, we shouldn't even need to call the parser if the attributes exist.

I will amend with a new commit.

The idea is to avoid a new request if the node already contains the information.

}

void _updateNode() {
if (_hasAllAttributes() || !mounted) return;
final title = parser.getContent(LinkPreviewRegex.title);
final description = parser.getContent(LinkPreviewRegex.description);
final transaction = editorState.transaction
..updateNode(
node,
{
LinkPreviewBlockKeys.url: node.attributes[LinkPreviewBlockKeys.url],
LinkPreviewBlockKeys.title: title ?? 'Link Preview title',
LinkPreviewBlockKeys.description:
description ?? 'Link Preview description',
},
);
editorState.apply(transaction);
}

bool _hasAllAttributes() {
final attributes = node.attributes;
return attributes[LinkPreviewBlockKeys.url] != null &&
attributes[LinkPreviewBlockKeys.title] != null &&
attributes[LinkPreviewBlockKeys.description] != null;
}

@override
Expand Down