Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

feat: use undo to revert the autotypograph (#7472)

Lucas7f3469a0f2bf
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/format_arrow_character.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/format_arrow_character.dart
index 379c7c61ba..8548b9354c 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/format_arrow_character.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/format_arrow_character.dart
@@ -7,6 +7,9 @@ const _equals = '=';
const _equalGreater = '⇒';
const _dashGreater = '→';
+const _hyphen = '-';
+const _emDash = '—'; // This is an em dash — not a single dash - !!
+
/// format '=' + '>' into an ⇒
///
/// - support
@@ -43,6 +46,24 @@ final CharacterShortcutEvent customFormatDashGreater = CharacterShortcutEvent(
),
);
+/// format two hyphens into an em dash
+///
+/// - support
+/// - desktop
+/// - mobile
+/// - web
+///
+final CharacterShortcutEvent customFormatDoubleHyphenEmDash =
+ CharacterShortcutEvent(
+ key: 'format double hyphen into an em dash',
+ character: _hyphen,
+ handler: (editorState) async => _handleDoubleCharacterReplacement(
+ editorState: editorState,
+ character: _hyphen,
+ replacement: _emDash,
+ ),
+);
+
/// If [prefixCharacter] is null or empty, [character] is used
Future<bool> _handleDoubleCharacterReplacement({
required EditorState editorState,
@@ -81,11 +102,29 @@ Future<bool> _handleDoubleCharacterReplacement({
return false;
}
+ // insert the greater character first and convert it to the replacement character to support undo
+ final insert = editorState.transaction
+ ..insertText(
+ node,
+ selection.end.offset,
+ character,
+ );
+
+ await editorState.apply(
+ insert,
+ skipHistoryDebounce: true,
+ );
+
+ final afterSelection = editorState.selection;
+ if (afterSelection == null) {
+ return false;
+ }
+
final replace = editorState.transaction
..replaceText(
node,
- selection.end.offset - 1,
- 1,
+ afterSelection.end.offset - 2,
+ 2,
replacement,
);
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/character_shortcuts.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/character_shortcuts.dart
index 158d14f48e..49178ca12b 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/character_shortcuts.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/character_shortcuts.dart
@@ -44,6 +44,7 @@ List<CharacterShortcutEvent> buildCharacterShortcutEvents(
customFormatGreaterEqual,
customFormatDashGreater,
+ customFormatDoubleHyphenEmDash,
customFormatNumberToNumberedList,
customFormatSignToHeading,
@@ -55,6 +56,7 @@ List<CharacterShortcutEvent> buildCharacterShortcutEvents(
formatGreaterEqual, // Overridden by customFormatGreaterEqual
formatNumberToNumberedList, // Overridden by customFormatNumberToNumberedList
formatSignToHeading, // Overridden by customFormatSignToHeading
+ formatDoubleHyphenEmDash, // Overridden by customFormatDoubleHyphenEmDash
].contains(shortcut),
),
diff --git a/frontend/appflowy_flutter/test/unit_test/document/shortcuts/format_shortcut_test.dart b/frontend/appflowy_flutter/test/unit_test/document/shortcuts/format_shortcut_test.dart
index c5cb1c524d..21011df540 100644
--- a/frontend/appflowy_flutter/test/unit_test/document/shortcuts/format_shortcut_test.dart
+++ b/frontend/appflowy_flutter/test/unit_test/document/shortcuts/format_shortcut_test.dart
@@ -33,6 +33,12 @@ void main() {
final node = editorState.document.root.children[0];
expect(node.delta!.toPlainText(), '⇒');
+ // use undo to revert the change
+ undoCommand.execute(editorState);
+ expect(editorState.document.root.children.length, 1);
+ final nodeAfterUndo = editorState.document.root.children[0];
+ expect(nodeAfterUndo.delta!.toPlainText(), '=>');
+
editorState.dispose();
});
@@ -56,6 +62,41 @@ void main() {
final node = editorState.document.root.children[0];
expect(node.delta!.toPlainText(), '→');
+ // use undo to revert the change
+ undoCommand.execute(editorState);
+ expect(editorState.document.root.children.length, 1);
+ final nodeAfterUndo = editorState.document.root.children[0];
+ expect(nodeAfterUndo.delta!.toPlainText(), '->');
+
+ editorState.dispose();
+ });
+
+ test('turn -- into —', () async {
+ final document = Document.blank()
+ ..insert([
+ 0,
+ ], [
+ paragraphNode(text: '-'),
+ ]);
+
+ final editorState = EditorState(document: document);
+ editorState.selection = Selection.collapsed(
+ Position(path: [0], offset: 1),
+ );
+
+ final result = await customFormatDoubleHyphenEmDash.execute(editorState);
+ expect(result, true);
+
+ expect(editorState.document.root.children.length, 1);
+ final node = editorState.document.root.children[0];
+ expect(node.delta!.toPlainText(), '—');
+
+ // use undo to revert the change
+ undoCommand.execute(editorState);
+ expect(editorState.document.root.children.length, 1);
+ final nodeAfterUndo = editorState.document.root.children[0];
+ expect(nodeAfterUndo.delta!.toPlainText(), '--');
+
editorState.dispose();
});
});