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.

fix: v0.8.2 launch review issues (#7266)

Lucas862e5629e325
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_html.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_html.dart
index 893c12cad1..6eff666991 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_html.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_html.dart
@@ -1,5 +1,6 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy/shared/markdown_to_document.dart';
+import 'package:appflowy/shared/patterns/common_patterns.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:html2md/html2md.dart' as html2md;
@@ -43,11 +44,13 @@ extension PasteFromHtml on EditorState {
// The table from Google Docs will contain the flag 'Google Table'
const googleDocsFlag = 'docs-internal-guid-';
final isPasteFromGoogleDocs = html.contains(googleDocsFlag);
+ final isPasteFromAppleNotes = appleNotesRegex.hasMatch(html);
final containsTable = nodes.any(
(node) =>
[TableBlockKeys.type, SimpleTableBlockKeys.type].contains(node.type),
);
- if (nodes.isEmpty || isPasteFromGoogleDocs || containsTable) {
+ if ((nodes.isEmpty || isPasteFromGoogleDocs || containsTable) &&
+ !isPasteFromAppleNotes) {
// fallback to the markdown parser
final markdown = html2md.convert(html);
nodes = customMarkdownToDocument(markdown, tableWidth: 200)
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_in_app_json.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_in_app_json.dart
index 70948cd962..d4db86d80c 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_in_app_json.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_in_app_json.dart
@@ -1,5 +1,6 @@
import 'dart:convert';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
@@ -7,6 +8,17 @@ extension PasteFromInAppJson on EditorState {
Future<bool> pasteInAppJson(String inAppJson) async {
try {
final nodes = Document.fromJson(jsonDecode(inAppJson)).root.children;
+
+ // skip pasting a table block to another table block
+ final containsTable =
+ nodes.any((node) => node.type == SimpleTableBlockKeys.type);
+ if (containsTable) {
+ final selectedNodes = getSelectedNodes(withCopy: false);
+ if (selectedNodes.any((node) => node.parentTableNode != null)) {
+ return false;
+ }
+ }
+
if (nodes.isEmpty) {
Log.info('pasteInAppJson: nodes is empty');
return false;
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/command_shortcuts.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/command_shortcuts.dart
index 8b168bcd30..1f409255c6 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/command_shortcuts.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/command_shortcuts.dart
@@ -1,6 +1,7 @@
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/align_toolbar_item/custom_text_align_command.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/shortcuts/custom_delete_command.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/undo_redo/custom_undo_redo_commands.dart';
import 'package:appflowy/workspace/presentation/settings/widgets/emoji_picker/emoji_picker.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
@@ -37,6 +38,8 @@ List<CommandShortcutEvent> commandShortcutEvents = [
...customTextAlignCommands,
+ customDeleteCommand,
+
// remove standard shortcuts for copy, cut, paste, todo
...standardCommandShortcutEvents
..removeWhere(
@@ -50,6 +53,7 @@ List<CommandShortcutEvent> commandShortcutEvents = [
redoCommand,
exitEditingCommand,
...tableCommands,
+ deleteCommand,
].contains(shortcut),
),
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/custom_delete_command.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/custom_delete_command.dart
new file mode 100644
index 0000000000..e0663c2bcf
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/custom_delete_command.dart
@@ -0,0 +1,115 @@
+import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_block_component.dart';
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:flutter/material.dart';
+
+/// Delete key event.
+///
+/// - support
+/// - desktop
+/// - web
+///
+final CommandShortcutEvent customDeleteCommand = CommandShortcutEvent(
+ key: 'Delete Key',
+ getDescription: () => AppFlowyEditorL10n.current.cmdDeleteRight,
+ command: 'delete, shift+delete',
+ handler: _deleteCommandHandler,
+);
+
+CommandShortcutEventHandler _deleteCommandHandler = (editorState) {
+ final selection = editorState.selection;
+ final selectionType = editorState.selectionType;
+ if (selection == null) {
+ return KeyEventResult.ignored;
+ }
+ if (selectionType == SelectionType.block) {
+ return _deleteInBlockSelection(editorState);
+ } else if (selection.isCollapsed) {
+ return _deleteInCollapsedSelection(editorState);
+ } else {
+ return _deleteInNotCollapsedSelection(editorState);
+ }
+};
+
+/// Handle delete key event when selection is collapsed.
+CommandShortcutEventHandler _deleteInCollapsedSelection = (editorState) {
+ final selection = editorState.selection;
+ if (selection == null || !selection.isCollapsed) {
+ return KeyEventResult.ignored;
+ }
+
+ final position = selection.start;
+ final node = editorState.getNodeAtPath(position.path);
+ final delta = node?.delta;
+ if (node == null || delta == null) {
+ return KeyEventResult.ignored;
+ }
+
+ final transaction = editorState.transaction;
+
+ if (position.offset == delta.length) {
+ final Node? tableParent =
+ node.findParent((element) => element.type == SimpleTableBlockKeys.type);
+ Node? nextTableParent;
+ final next = node.findDownward((element) {
+ nextTableParent = element
+ .findParent((element) => element.type == SimpleTableBlockKeys.type);
+ // break if only one is in a table or they're in different tables
+ return tableParent != nextTableParent ||
+ // merge the next node with delta
+ element.delta != null;
+ });
+ // table nodes should be deleted using the table menu
+ // in-table paragraphs should only be deleted inside the table
+ if (next != null && tableParent == nextTableParent) {
+ if (next.children.isNotEmpty) {
+ final path = node.path + [node.children.length];
+ transaction.insertNodes(path, next.children);
+ }
+ transaction
+ ..deleteNode(next)
+ ..mergeText(
+ node,
+ next,
+ );
+ editorState.apply(transaction);
+ return KeyEventResult.handled;
+ }
+ } else {
+ final nextIndex = delta.nextRunePosition(position.offset);
+ if (nextIndex <= delta.length) {
+ transaction.deleteText(
+ node,
+ position.offset,
+ nextIndex - position.offset,
+ );
+ editorState.apply(transaction);
+ return KeyEventResult.handled;
+ }
+ }
+
+ return KeyEventResult.ignored;
+};
+
+/// Handle delete key event when selection is not collapsed.
+CommandShortcutEventHandler _deleteInNotCollapsedSelection = (editorState) {
+ final selection = editorState.selection;
+ if (selection == null || selection.isCollapsed) {
+ return KeyEventResult.ignored;
+ }
+ editorState.deleteSelection(selection);
+ return KeyEventResult.handled;
+};
+
+CommandShortcutEventHandler _deleteInBlockSelection = (editorState) {
+ final selection = editorState.selection;
+ if (selection == null || editorState.selectionType != SelectionType.block) {
+ return KeyEventResult.ignored;
+ }
+ final transaction = editorState.transaction;
+ transaction.deleteNodesAtPath(selection.start.path);
+ editorState
+ .apply(transaction)
+ .then((value) => editorState.selectionType = null);
+
+ return KeyEventResult.handled;
+};
diff --git a/frontend/appflowy_flutter/lib/shared/patterns/common_patterns.dart b/frontend/appflowy_flutter/lib/shared/patterns/common_patterns.dart
index 15c2cdb3fe..862f9c4778 100644
--- a/frontend/appflowy_flutter/lib/shared/patterns/common_patterns.dart
+++ b/frontend/appflowy_flutter/lib/shared/patterns/common_patterns.dart
@@ -51,3 +51,7 @@ final localPathRegex = RegExp(_localPathPattern, caseSensitive: false);
const _wordPattern = r"\S+";
final wordRegex = RegExp(_wordPattern);
+
+const _appleNotesPattern =
+ r'<meta\s+name="Generator"\s+content="Cocoa HTML Writer"\s*>\s*<meta\s+name="CocoaVersion"\s+content="\d+"\s*>';
+final appleNotesRegex = RegExp(_appleNotesPattern);
diff --git a/frontend/appflowy_flutter/test/unit_test/document/html/_html_samples.dart b/frontend/appflowy_flutter/test/unit_test/document/html/_html_samples.dart
index 579e42a3ca..41dea6e01c 100644
--- a/frontend/appflowy_flutter/test/unit_test/document/html/_html_samples.dart
+++ b/frontend/appflowy_flutter/test/unit_test/document/html/_html_samples.dart
@@ -478,3 +478,64 @@ const tableFromChatGPT = '''
By following the guidelines above, you can work toward achieving a healthier and happier life.
</p>
''';
+
+// | Month | Savings |
+// | -------- | ------- |
+// | January | $250 |
+// | February | $80 |
+// | March | $420 |
+const tableFromAppleNotes = '''
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<meta http-equiv="Content-Style-Type" content="text/css">
+<title></title>
+<meta name="Generator" content="Cocoa HTML Writer">
+<meta name="CocoaVersion" content="2566">
+<style type="text/css">
+p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px '.AppleSystemUIFont'}
+table.t1 {border-collapse: collapse}
+td.td1 {border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #9a9a9a #9a9a9a #9a9a9a #9a9a9a; padding: 1.0px 5.0px 1.0px 5.0px}
+</style>
+</head>
+<body>
+<table cellspacing="0" cellpadding="0" class="t1">
+<tbody>
+<tr>
+<td valign="top" class="td1">
+<p class="p1">Month</p>
+</td>
+<td valign="top" class="td1">
+<p class="p1">Savings</p>
+</td>
+</tr>
+<tr>
+<td valign="top" class="td1">
+<p class="p1">January</p>
+</td>
+<td valign="top" class="td1">
+<p class="p1">\$250</p>
+</td>
+</tr>
+<tr>
+<td valign="top" class="td1">
+<p class="p1">February</p>
+</td>
+<td valign="top" class="td1">
+<p class="p1">\$80</p>
+</td>
+</tr>
+<tr>
+<td valign="top" class="td1">
+<p class="p1">March</p>
+</td>
+<td valign="top" class="td1">
+<p class="p1">\$420</p>
+</td>
+</tr>
+</tbody>
+</table>
+</body>
+</html>
+''';
diff --git a/frontend/appflowy_flutter/test/unit_test/document/html/paste_from_html_test.dart b/frontend/appflowy_flutter/test/unit_test/document/html/paste_from_html_test.dart
index d9f59adabb..a36474ef3f 100644
--- a/frontend/appflowy_flutter/test/unit_test/document/html/paste_from_html_test.dart
+++ b/frontend/appflowy_flutter/test/unit_test/document/html/paste_from_html_test.dart
@@ -46,6 +46,10 @@ void main() {
nodes.where((node) => node.type == DividerBlockKeys.type);
expect(dividers.length, 5);
});
+
+ test('sample 5 - paste table from Apple Notes', () {
+ checkTable(tableFromAppleNotes);
+ });
});
}