fix: quote block flashes in ai chat page when generating answer (#7553)

This commit is contained in:
Lucas 2025-03-17 13:03:47 +08:00 committed by GitHub
parent eddb623fba
commit 6d327adb83
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 45 additions and 12 deletions

View File

@ -64,8 +64,12 @@ class _AppFlowyEditorMarkdownState extends State<_AppFlowyEditorMarkdown> {
super.didUpdateWidget(oldWidget);
if (oldWidget.markdown != widget.markdown) {
editorState.dispose();
editorState = _parseMarkdown(widget.markdown.trim());
final editorState = _parseMarkdown(
widget.markdown.trim(),
previousDocument: this.editorState.document,
);
this.editorState.dispose();
this.editorState = editorState;
scrollController.dispose();
scrollController = EditorScrollController(
editorState: editorState,
@ -129,8 +133,30 @@ class _AppFlowyEditorMarkdownState extends State<_AppFlowyEditorMarkdown> {
);
}
EditorState _parseMarkdown(String markdown) {
EditorState _parseMarkdown(
String markdown, {
Document? previousDocument,
}) {
// merge the nodes from the previous document with the new document to keep the same node ids
final document = customMarkdownToDocument(markdown);
final documentIterator = NodeIterator(
document: document,
startNode: document.root,
);
if (previousDocument != null) {
final previousDocumentIterator = NodeIterator(
document: previousDocument,
startNode: previousDocument.root,
);
while (
documentIterator.moveNext() && previousDocumentIterator.moveNext()) {
final currentNode = documentIterator.current;
final previousNode = previousDocumentIterator.current;
if (currentNode.path.equals(previousNode.path)) {
currentNode.id = previousNode.id;
}
}
}
final editorState = EditorState(document: document);
return editorState;
}

View File

@ -5,6 +5,9 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:universal_platform/universal_platform.dart';
/// In memory cache of the quote block height to avoid flashing when the quote block is updated.
Map<String, double> _quoteBlockHeightCache = {};
typedef QuoteBlockIconBuilder = Widget Function(
BuildContext context,
Node node,
@ -115,7 +118,9 @@ class _QuoteBlockComponentWidgetState extends State<QuoteBlockComponentWidget>
@override
Node get node => widget.node;
ValueNotifier<double> quoteBlockHeightNotifier = ValueNotifier(0);
late ValueNotifier<double> quoteBlockHeightNotifier = ValueNotifier(
_quoteBlockHeightCache[node.id] ?? 0,
);
StreamSubscription<EditorTransactionValue>? _transactionSubscription;
@ -266,17 +271,19 @@ class _QuoteBlockComponentWidgetState extends State<QuoteBlockComponentWidget>
void _updateQuoteBlockHeight() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
final renderObject = layoutBuilderKey.currentContext?.findRenderObject();
double height = _quoteBlockHeightCache[node.id] ?? 0;
if (renderObject != null && renderObject is RenderBox) {
if (UniversalPlatform.isMobile) {
quoteBlockHeightNotifier.value =
renderObject.size.height - padding.top;
height = renderObject.size.height - padding.top;
} else {
quoteBlockHeightNotifier.value =
renderObject.size.height - padding.top * 2;
height = renderObject.size.height - padding.top * 2;
}
} else {
quoteBlockHeightNotifier.value = 0;
height = 0;
}
quoteBlockHeightNotifier.value = height;
_quoteBlockHeightCache[node.id] = height;
});
}
}

View File

@ -90,8 +90,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: ec7350d
resolved-ref: ec7350da7c298639c85e88229033da0c8aae8578
ref: "5070212"
resolved-ref: "5070212ee0f02182a8acdd760b4d7b42264baec4"
url: "https://github.com/AppFlowy-IO/appflowy-editor.git"
source: git
version: "5.1.0"

View File

@ -180,7 +180,7 @@ dependency_overrides:
appflowy_editor:
git:
url: https://github.com/AppFlowy-IO/appflowy-editor.git
ref: "ec7350d"
ref: "5070212"
appflowy_editor_plugins:
git: