From f2dd485ee5692a1fe994d23f23a1b0f0414b27cd Mon Sep 17 00:00:00 2001 From: omegaviv Date: Thu, 23 May 2024 12:04:43 +0530 Subject: [PATCH 1/2] fix: updated link preview node attributes --- .../link_preview_block_component.dart | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/packages/appflowy_editor_plugins/lib/src/link_preview/link_preview_block_component.dart b/packages/appflowy_editor_plugins/lib/src/link_preview/link_preview_block_component.dart index bc259f4..2052db1 100644 --- a/packages/appflowy_editor_plugins/lib/src/link_preview/link_preview_block_component.dart +++ b/packages/appflowy_editor_plugins/lib/src/link_preview/link_preview_block_component.dart @@ -2,6 +2,7 @@ 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 { @@ -9,6 +10,8 @@ class 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( @@ -132,6 +135,8 @@ class LinkPreviewBlockComponentState extends State final showActionsNotifier = ValueNotifier(false); bool alwaysShowMenu = false; + late final editorState = Provider.of(context, listen: false); + @override void initState() { super.initState(); @@ -141,6 +146,33 @@ class LinkPreviewBlockComponentState extends State parser = LinkPreviewParser(url: url, cache: widget.cache); future = parser.start(); + + future.then((_) => + WidgetsBinding.instance.addPostFrameCallback((_) => _updateNode())); + } + + 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 From de26c282370e674122de51c54f5d2f8246a6c167 Mon Sep 17 00:00:00 2001 From: omegaviv Date: Wed, 29 May 2024 19:08:41 +0530 Subject: [PATCH 2/2] fix: trailing comma --- .../lib/src/link_preview/link_preview_block_component.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/appflowy_editor_plugins/lib/src/link_preview/link_preview_block_component.dart b/packages/appflowy_editor_plugins/lib/src/link_preview/link_preview_block_component.dart index 2052db1..df35347 100644 --- a/packages/appflowy_editor_plugins/lib/src/link_preview/link_preview_block_component.dart +++ b/packages/appflowy_editor_plugins/lib/src/link_preview/link_preview_block_component.dart @@ -147,8 +147,9 @@ class LinkPreviewBlockComponentState extends State parser = LinkPreviewParser(url: url, cache: widget.cache); future = parser.start(); - future.then((_) => - WidgetsBinding.instance.addPostFrameCallback((_) => _updateNode())); + future.then( + (_) => WidgetsBinding.instance.addPostFrameCallback((_) => _updateNode()), + ); } void _updateNode() {