Skip to content

Commit

Permalink
fix: changing heading from one level to another from toolbar (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored Apr 24, 2023
1 parent d4ab376 commit 91a6060
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
6 changes: 6 additions & 0 deletions lib/src/core/document/attributes.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import 'package:flutter/foundation.dart';

/// Attributes is used to describe the Node's information.
///
/// Please note: The keywords in [BuiltInAttributeKey] are reserved.
typedef Attributes = Map<String, dynamic>;

bool isAttributesEqual(Attributes? a, Attributes? b) {
return mapEquals(a, b);
}

Attributes? composeAttributes(
Attributes? base,
Attributes? other, {
Expand Down
24 changes: 12 additions & 12 deletions lib/src/service/default_text_operations/format_rich_text_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,21 @@ bool formatTextNodes(EditorState editorState, Attributes attributes) {

for (final textNode in textNodes) {
var newAttributes = {...textNode.attributes};
for (final globalStyleKey in BuiltInAttributeKey.globalStyleKeys) {
if (newAttributes.keys.contains(globalStyleKey)) {
newAttributes[globalStyleKey] = null;
if (isAttributesEqual(newAttributes, attributes)) {
for (final key in attributes.keys) {
newAttributes[key] = null;
}
}

// if an attribute already exists in the node, it should be removed instead
for (final entry in attributes.entries) {
if (textNode.attributes.containsKey(entry.key) &&
textNode.attributes[entry.key] == entry.value) {
// attribute is not added to the node new attributes
} else {
newAttributes.addEntries([entry]);
} else {
for (final globalStyleKey in BuiltInAttributeKey.globalStyleKeys) {
if (newAttributes.keys.contains(globalStyleKey)) {
newAttributes[globalStyleKey] = null;
}
}

// if an attribute already exists in the node, it should be removed instead
newAttributes.addAll(attributes);
}

transaction
..updateNode(
textNode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/render/rich_text/heading_text.dart';
import 'package:appflowy_editor/src/render/rich_text/quoted_text.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../infra/test_editor.dart';
Expand Down Expand Up @@ -31,5 +32,36 @@ void main() async {
await tester.pumpAndSettle();
expect(find.byType(QuotedTextNodeWidget), findsNothing);
});

testWidgets('formatHeading', (tester) async {
const text = 'Welcome to Appflowy 😁';
final editor = tester.editor
..insertTextNode(
text,
attributes: {
BuiltInAttributeKey.subtype: BuiltInAttributeKey.heading,
BuiltInAttributeKey.heading: BuiltInAttributeKey.h2,
},
);
await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: 0, endOffset: text.length),
);

// format the text to h1
formatHeading(editor.editorState, BuiltInAttributeKey.h1);
await tester.pumpAndSettle(const Duration(milliseconds: 100));
expect(find.byType(HeadingTextNodeWidget), findsOneWidget);

final textNode = editor.nodeAtPath([0])!;
expect(
textNode.attributes[BuiltInAttributeKey.subtype],
BuiltInAttributeKey.heading,
);
expect(
textNode.attributes[BuiltInAttributeKey.heading],
BuiltInAttributeKey.h1,
);
});
});
}

0 comments on commit 91a6060

Please sign in to comment.