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: changing heading from one level to another from toolbar #96

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
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
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,
);
});
});
}