From 1039c5517fd02fa0aec6426af30dc0501b384c86 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Sat, 16 Jul 2022 20:08:41 +0800 Subject: [PATCH] fix: rebuilding node widgets when the subtype changes --- .../packages/flowy_editor/lib/document/node.dart | 13 +++++++++---- .../flowy_editor/lib/document/state_tree.dart | 2 +- .../packages/flowy_editor/lib/editor_state.dart | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart b/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart index 0747416335..55e96451b1 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/document/node.dart @@ -13,8 +13,9 @@ class Node extends ChangeNotifier with LinkedListEntry { String? get subtype { // TODO: make 'subtype' as a const value. if (attributes.containsKey('subtype')) { - assert(attributes['subtype'] is String, 'subtype must be a [String]'); - return attributes['subtype'] as String; + assert(attributes['subtype'] is String?, + 'subtype must be a [String] or [null]'); + return attributes['subtype'] as String?; } return null; } @@ -63,12 +64,16 @@ class Node extends ChangeNotifier with LinkedListEntry { } void updateAttributes(Attributes attributes) { + bool shouldNotifyParent = + this.attributes['subtype'] != attributes['subtype']; + for (final attribute in attributes.entries) { this.attributes[attribute.key] = attribute.value; } - // Notify the new attributes - parent?.notifyListeners(); + // if attributes contains 'subtype', should notify parent to rebuild node + // else, just notify current node. + shouldNotifyParent ? parent?.notifyListeners() : notifyListeners(); } Node? childAtIndex(int index) { diff --git a/frontend/app_flowy/packages/flowy_editor/lib/document/state_tree.dart b/frontend/app_flowy/packages/flowy_editor/lib/document/state_tree.dart index af343f54a0..01ab07381e 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/document/state_tree.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/document/state_tree.dart @@ -51,7 +51,7 @@ class StateTree { if (updatedNode == null) { return null; } - final previousAttributes = {...updatedNode.attributes}; + final previousAttributes = Attributes.from(updatedNode.attributes); updatedNode.updateAttributes(attributes); return previousAttributes; } diff --git a/frontend/app_flowy/packages/flowy_editor/lib/editor_state.dart b/frontend/app_flowy/packages/flowy_editor/lib/editor_state.dart index ccbe3ebdbc..0199bfe337 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/editor_state.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/editor_state.dart @@ -39,7 +39,7 @@ class EditorState { void update(Node node, Attributes attributes) { _applyOperation(UpdateOperation( path: node.path, - attributes: Attributes.from(attributes)..addAll(attributes), + attributes: Attributes.from(node.attributes)..addAll(attributes), oldAttributes: node.attributes, )); }