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: support multiple lines in table cell (#6931)

Lucas592390dc847e
diff --git a/frontend/appflowy_flutter/integration_test/desktop/document/document_with_simple_table_test.dart b/frontend/appflowy_flutter/integration_test/desktop/document/document_with_simple_table_test.dart
index 4a8436f8b6..3f1f53655c 100644
--- a/frontend/appflowy_flutter/integration_test/desktop/document/document_with_simple_table_test.dart
+++ b/frontend/appflowy_flutter/integration_test/desktop/document/document_with_simple_table_test.dart
@@ -1,5 +1,4 @@
import 'package:appflowy/generated/locale_keys.g.dart';
-import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/_shared_widget.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:easy_localization/easy_localization.dart';
diff --git a/frontend/appflowy_flutter/lib/plugins/document/application/document_validator.dart b/frontend/appflowy_flutter/lib/plugins/document/application/document_validator.dart
new file mode 100644
index 0000000000..a49a0f9902
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/plugins/document/application/document_validator.dart
@@ -0,0 +1,77 @@
+import 'package:appflowy_editor/appflowy_editor.dart';
+
+class DocumentValidator {
+ const DocumentValidator({
+ required this.editorState,
+ required this.rules,
+ });
+
+ final EditorState editorState;
+ final List<DocumentRule> rules;
+
+ Future<bool> validate(Transaction transaction) async {
+ // deep copy the document
+ final root = this.editorState.document.root.copyWith();
+ final dummyDocument = Document(root: root);
+ if (dummyDocument.isEmpty) {
+ return true;
+ }
+
+ final editorState = EditorState(document: dummyDocument);
+ await editorState.apply(transaction);
+
+ final iterator = NodeIterator(
+ document: editorState.document,
+ startNode: editorState.document.root,
+ );
+
+ for (final rule in rules) {
+ while (iterator.moveNext()) {
+ if (!rule.validate(iterator.current)) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+
+ Future<bool> applyTransactionInDummyDocument(Transaction transaction) async {
+ // deep copy the document
+ final root = this.editorState.document.root.copyWith();
+ final dummyDocument = Document(root: root);
+ if (dummyDocument.isEmpty) {
+ return true;
+ }
+
+ final editorState = EditorState(document: dummyDocument);
+ await editorState.apply(transaction);
+
+ final iterator = NodeIterator(
+ document: editorState.document,
+ startNode: editorState.document.root,
+ );
+
+ for (final rule in rules) {
+ while (iterator.moveNext()) {
+ if (!rule.validate(iterator.current)) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+}
+
+class DocumentRule {
+ const DocumentRule({
+ required this.type,
+ });
+
+ final String type;
+
+ bool validate(Node node) {
+ return true;
+ }
+}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_configuration.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_configuration.dart
index 7bb590079b..00682b6a52 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_configuration.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_configuration.dart
@@ -15,6 +15,24 @@ import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:universal_platform/universal_platform.dart';
+/// The node types that support slash menu.
+final Set<String> supportSlashMenuNodeTypes = {
+ ParagraphBlockKeys.type,
+ HeadingBlockKeys.type,
+
+ // Lists
+ TodoListBlockKeys.type,
+ BulletedListBlockKeys.type,
+ NumberedListBlockKeys.type,
+ QuoteBlockKeys.type,
+ ToggleListBlockKeys.type,
+
+ // Simple table
+ // SimpleTableBlockKeys.type,
+ // SimpleTableRowBlockKeys.type,
+ // SimpleTableCellBlockKeys.type,
+};
+
/// Build the block component builders.
///
/// Every block type should have a corresponding builder in the map.
@@ -125,9 +143,13 @@ void _customBlockOptionActions(
final actions = _buildOptionActions(context, entry.key);
if (UniversalPlatform.isDesktop) {
- builder.showActions = (node) =>
- node.parent?.type != TableCellBlockKeys.type &&
- node.parent?.type != SimpleTableCellBlockKeys.type;
+ builder.showActions = (node) {
+ final parentTableNode = node.parentTableNode;
+ if (node.type != SimpleTableBlockKeys.type && parentTableNode != null) {
+ return false;
+ }
+ return true;
+ };
builder.configuration = builder.configuration.copyWith(
blockSelectionAreaMargin: (_) => const EdgeInsets.symmetric(
vertical: 1,
@@ -162,6 +184,7 @@ void _customBlockOptionActions(
shouldInsertSlash: false,
deleteKeywordsByDefault: true,
style: styleCustomizer.selectionMenuStyleBuilder(),
+ supportSlashMenuNodeTypes: supportSlashMenuNodeTypes,
).handler.call(editorState)
: () {},
),
@@ -429,10 +452,19 @@ NumberedListBlockComponentBuilder _buildNumberedListBlockComponentBuilder(
return configuration.textStyle(node);
},
),
- iconBuilder: (_, node, textDirection) => NumberedListIcon(
- node: node,
- textDirection: textDirection,
- ),
+ iconBuilder: (_, node, textDirection) {
+ TextStyle? textStyle;
+ if (node.isInHeaderColumn || node.isInHeaderRow) {
+ textStyle = configuration.textStyle(node).copyWith(
+ fontWeight: FontWeight.bold,
+ );
+ }
+ return NumberedListIcon(
+ node: node,
+ textDirection: textDirection,
+ textStyle: textStyle,
+ );
+ },
);
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart
index e39c9bbd4c..bb0cf4e156 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart
@@ -122,6 +122,7 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage>
slashMenuItems,
shouldInsertSlash: false,
style: styleCustomizer.selectionMenuStyleBuilder(),
+ supportSlashMenuNodeTypes: supportSlashMenuNodeTypes,
).handler(editorState);
AFFocusManager? focusManager;
@@ -156,10 +157,6 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage>
scrollController: effectiveScrollController,
);
- // keep the previous font style when typing new text.
- supportSlashMenuNodeWhiteList.addAll([
- ToggleListBlockKeys.type,
- ]);
toolbarItemWhiteList.addAll([
ToggleListBlockKeys.type,
CalloutBlockKeys.type,
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/text_robot.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/text_robot.dart
index 68e1c8e5eb..a20ea9aec5 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/text_robot.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/base/text_robot.dart
@@ -1,7 +1,6 @@
import 'dart:async';
import 'package:appflowy_editor/appflowy_editor.dart';
-import 'package:flutter/material.dart';
import 'package:synchronized/synchronized.dart';
enum TextRobotInputType {
@@ -125,10 +124,6 @@ class TextRobot {
transaction.insertText(node, selection.endIndex, text);
await editorState.apply(transaction);
await Future.delayed(delay);
-
- debugPrint(
- 'AI insertText: path: ${selection.end.path}, index: ${selection.endIndex}, text: "$text"',
- );
}
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/numbered_list/numbered_list_icon.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/numbered_list/numbered_list_icon.dart
index 211d6740b1..3c67f7ccca 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/numbered_list/numbered_list_icon.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/numbered_list/numbered_list_icon.dart
@@ -8,17 +8,22 @@ class NumberedListIcon extends StatelessWidget {
super.key,
required this.node,
required this.textDirection,
+ this.textStyle,
});
final Node node;
final TextDirection textDirection;
+ final TextStyle? textStyle;
@override
Widget build(BuildContext context) {
- final textStyle =
+ final textStyleConfiguration =
context.read<EditorState>().editorStyle.textStyleConfiguration;
- final fontSize = textStyle.text.fontSize ?? 16.0;
- final height = textStyle.text.height ?? textStyle.lineHeight;
+ final fontSize = textStyleConfiguration.text.fontSize ?? 16.0;
+ final height =
+ textStyleConfiguration.text.height ?? textStyleConfiguration.lineHeight;
+ final combinedTextStyle = textStyle?.combine(textStyleConfiguration.text) ??
+ textStyleConfiguration.text;
final size = fontSize * height;
return Container(
constraints: BoxConstraints(
@@ -30,8 +35,8 @@ class NumberedListIcon extends StatelessWidget {
child: Center(
child: Text(
node.levelString,
- style: textStyle.text,
- strutStyle: StrutStyle.fromTextStyle(textStyle.text),
+ style: combinedTextStyle,
+ strutStyle: StrutStyle.fromTextStyle(combinedTextStyle),
textDirection: textDirection,
),
),
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 8888d3ea1b..c4affb7b69 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
@@ -1,4 +1,5 @@
import 'package:appflowy/plugins/document/application/document_bloc.dart';
+import 'package:appflowy/plugins/document/presentation/editor_configuration.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/base/format_arrow_character.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/base/page_reference_commands.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/callout/callout_block_shortcuts.dart';
@@ -38,6 +39,7 @@ List<CharacterShortcutEvent> buildCharacterShortcutEvents(
customSlashCommand(
slashMenuItems,
style: styleCustomizer.selectionMenuStyleBuilder(),
+ supportSlashMenuNodeTypes: supportSlashMenuNodeTypes,
),
customFormatGreaterEqual,
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 025aac2584..b1cd0c1712 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
@@ -47,7 +47,6 @@ List<CommandShortcutEvent> commandShortcutEvents = [
undoCommand,
redoCommand,
exitEditingCommand,
- // ...tableCommands,
].contains(shortcut),
),
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/_shared_widget.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/_shared_widget.dart
index 6f2e124fd8..2855ac6f43 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/_shared_widget.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/_shared_widget.dart
@@ -8,358 +8,6 @@ import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
-/// Only displaying the add row / add column / add column and row button
-/// when hovering on the last row / last column / last cell.
-bool _enableHoveringLogicV2 = true;
-
-class SimpleTableAddRowHoverButton extends StatefulWidget {
- const SimpleTableAddRowHoverButton({
- super.key,
- required this.editorState,
- required this.tableNode,
- });
-
- final EditorState editorState;
- final Node tableNode;
-
- @override
- State<SimpleTableAddRowHoverButton> createState() =>
- _SimpleTableAddRowHoverButtonState();
-}
-
-class _SimpleTableAddRowHoverButtonState
- extends State<SimpleTableAddRowHoverButton> {
- late final interceptorKey =
- 'simple_table_add_row_hover_button_${widget.tableNode.id}';
-
- SelectionGestureInterceptor? interceptor;
-
- @override
- void initState() {
- super.initState();
-
- interceptor = SelectionGestureInterceptor(
- key: interceptorKey,
- canTap: (details) => !_isTapInBounds(details.globalPosition),
- );
- widget.editorState.service.selectionService
- .registerGestureInterceptor(interceptor!);
- }
-
- @override
- void dispose() {
- widget.editorState.service.selectionService.unregisterGestureInterceptor(
- interceptorKey,
- );
-
- super.dispose();
- }
-
- @override
- Widget build(BuildContext context) {
- assert(widget.tableNode.type == SimpleTableBlockKeys.type);
-
- if (widget.tableNode.type != SimpleTableBlockKeys.type) {
- return const SizedBox.shrink();
- }
-
- final simpleTableContext = context.read<SimpleTableContext>();
- return ValueListenableBuilder(
- valueListenable: simpleTableContext.isHoveringOnTableArea,
- builder: (context, isHoveringOnTableArea, child) {
- return ValueListenableBuilder(
- valueListenable: simpleTableContext.hoveringTableCell,
- builder: (context, hoveringTableCell, child) {
- bool shouldShow = isHoveringOnTableArea;
- if (hoveringTableCell != null && _enableHoveringLogicV2) {
- shouldShow =
- hoveringTableCell.rowIndex + 1 == hoveringTableCell.rowLength;
- }
- return shouldShow
- ? Positioned(
- bottom: 2 * SimpleTableConstants.addRowButtonPadding,
- left: SimpleTableConstants.tableLeftPadding -
- SimpleTableConstants.cellBorderWidth,
- right: SimpleTableConstants.addRowButtonRightPadding,
- child: SimpleTableAddRowButton(
- onTap: () => widget.editorState.addRowInTable(
- widget.tableNode,
- ),
- ),
- )
- : const SizedBox.shrink();
- },
- );
- },
- );
- }
-
- bool _isTapInBounds(Offset offset) {
- final renderBox = context.findRenderObject() as RenderBox?;
- if (renderBox == null) {
- return false;
- }
-
- final localPosition = renderBox.globalToLocal(offset);
- final result = renderBox.paintBounds.contains(localPosition);
-
- return result;
- }
-}
-
-class SimpleTableAddRowButton extends StatelessWidget {
- const SimpleTableAddRowButton({
- super.key,
- this.onTap,
- });
-
- final VoidCallback? onTap;
-
- @override
- Widget build(BuildContext context) {
- return FlowyTooltip(
- message: LocaleKeys.document_plugins_simpleTable_clickToAddNewRow.tr(),
- child: GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTap: onTap,
- child: MouseRegion(
- cursor: SystemMouseCursors.click,
- child: Container(
- height: SimpleTableConstants.addRowButtonHeight,
- margin: const EdgeInsets.symmetric(
- vertical: SimpleTableConstants.addColumnButtonPadding,
- ),
- decoration: BoxDecoration(
- borderRadius: BorderRadius.circular(
- SimpleTableConstants.addRowButtonRadius,
- ),
- color: context.simpleTableMoreActionBackgroundColor,
- ),
- child: const FlowySvg(
- FlowySvgs.add_s,
- ),
- ),
- ),
- ),
- );
- }
-}
-
-class SimpleTableAddColumnHoverButton extends StatefulWidget {
- const SimpleTableAddColumnHoverButton({
- super.key,
- required this.editorState,
- required this.node,
- });
-
- final EditorState editorState;
- final Node node;
-
- @override
- State<SimpleTableAddColumnHoverButton> createState() =>
- _SimpleTableAddColumnHoverButtonState();
-}
-
-class _SimpleTableAddColumnHoverButtonState
- extends State<SimpleTableAddColumnHoverButton> {
- late final interceptorKey =
- 'simple_table_add_column_hover_button_${widget.node.id}';
-
- SelectionGestureInterceptor? interceptor;
-
- @override
- void initState() {
- super.initState();
-
- interceptor = SelectionGestureInterceptor(
- key: interceptorKey,
- canTap: (details) => !_isTapInBounds(details.globalPosition),
- );
- widget.editorState.service.selectionService
- .registerGestureInterceptor(interceptor!);
- }
-
- @override
- void dispose() {
- widget.editorState.service.selectionService.unregisterGestureInterceptor(
- interceptorKey,
- );
-
- super.dispose();
- }
-
- @override
- Widget build(BuildContext context) {
- assert(widget.node.type == SimpleTableBlockKeys.type);
-
- if (widget.node.type != SimpleTableBlockKeys.type) {
- return const SizedBox.shrink();
- }
-
- return ValueListenableBuilder(
- valueListenable: context.read<SimpleTableContext>().isHoveringOnTableArea,
- builder: (context, isHoveringOnTableArea, _) {
- return ValueListenableBuilder(
- valueListenable: context.read<SimpleTableContext>().hoveringTableCell,
- builder: (context, hoveringTableCell, _) {
- bool shouldShow = isHoveringOnTableArea;
- if (hoveringTableCell != null && _enableHoveringLogicV2) {
- shouldShow = hoveringTableCell.columnIndex + 1 ==
- hoveringTableCell.columnLength;
- }
- return Positioned(
- top: SimpleTableConstants.tableTopPadding -
- SimpleTableConstants.cellBorderWidth,
- bottom: SimpleTableConstants.addColumnButtonBottomPadding,
- right: 0,
- child: Opacity(
- opacity: shouldShow ? 1.0 : 0.0,
- child: SimpleTableAddColumnButton(
- onTap: () {
- widget.editorState.addColumnInTable(widget.node);
- },
- ),
- ),
- );
- },
- );
- },
- );
- }
-
- bool _isTapInBounds(Offset offset) {
- final renderBox = context.findRenderObject() as RenderBox?;
- if (renderBox == null) {
- return false;
- }
-
- final localPosition = renderBox.globalToLocal(offset);
- final result = renderBox.paintBounds.contains(localPosition);
-
- return result;
- }
-}
-
-class SimpleTableAddColumnButton extends StatelessWidget {
- const SimpleTableAddColumnButton({
- super.key,
- this.onTap,
- });
-
- final VoidCallback? onTap;
-
- @override
- Widget build(BuildContext context) {
- return FlowyTooltip(
- message: LocaleKeys.document_plugins_simpleTable_clickToAddNewColumn.tr(),
- child: GestureDetector(
- behavior: HitTestBehavior.translucent,
- onTap: onTap,
- child: MouseRegion(
- cursor: SystemMouseCursors.click,
- child: Container(
- width: SimpleTableConstants.addColumnButtonWidth,
- margin: const EdgeInsets.symmetric(
- horizontal: SimpleTableConstants.addColumnButtonPadding,
- ),
- decoration: BoxDecoration(
- borderRadius: BorderRadius.circular(
- SimpleTableConstants.addColumnButtonRadius,
- ),
- color: context.simpleTableMoreActionBackgroundColor,
- ),
- child: const FlowySvg(
- FlowySvgs.add_s,
- ),
- ),
- ),
- ),
- );
- }
-}
-
-class SimpleTableAddColumnAndRowHoverButton extends StatelessWidget {
- const SimpleTableAddColumnAndRowHoverButton({
- super.key,
- required this.editorState,
- required this.node,
- });
-
- final EditorState editorState;
- final Node node;
-
- @override
- Widget build(BuildContext context) {
- assert(node.type == SimpleTableBlockKeys.type);
-
- if (node.type != SimpleTableBlockKeys.type) {
- return const SizedBox.shrink();
- }
-
- return ValueListenableBuilder(
- valueListenable: context.read<SimpleTableContext>().isHoveringOnTableArea,
- builder: (context, isHoveringOnTableArea, child) {
- return ValueListenableBuilder(
- valueListenable: context.read<SimpleTableContext>().hoveringTableCell,
- builder: (context, hoveringTableCell, child) {
- bool shouldShow = isHoveringOnTableArea;
- if (hoveringTableCell != null && _enableHoveringLogicV2) {
- shouldShow = hoveringTableCell.isLastCellInTable;
- }
- return shouldShow
- ? Positioned(
- bottom:
- SimpleTableConstants.addColumnAndRowButtonBottomPadding,
- right: SimpleTableConstants.addColumnButtonPadding,
- child: SimpleTableAddColumnAndRowButton(
- onTap: () => editorState.addColumnAndRowInTable(node),
- ),
- )
- : const SizedBox.shrink();
- },
- );
- },
- );
- }
-}
-
-class SimpleTableAddColumnAndRowButton extends StatelessWidget {
- const SimpleTableAddColumnAndRowButton({
- super.key,
- this.onTap,
- });
-
- final VoidCallback? onTap;
-
- @override
- Widget build(BuildContext context) {
- return FlowyTooltip(
- message: LocaleKeys.document_plugins_simpleTable_clickToAddNewRowAndColumn
- .tr(),
- child: GestureDetector(
- behavior: HitTestBehavior.translucent,
- onTap: onTap,
- child: MouseRegion(
- cursor: SystemMouseCursors.click,
- child: Container(
- width: SimpleTableConstants.addColumnAndRowButtonWidth,
- height: SimpleTableConstants.addColumnAndRowButtonHeight,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.circular(
- SimpleTableConstants.addColumnAndRowButtonCornerRadius,
- ),
- color: context.simpleTableMoreActionBackgroundColor,
- ),
- child: const FlowySvg(
- FlowySvgs.add_s,
- ),
- ),
- ),
- ),
- );
- }
-}
-
class SimpleTableRowDivider extends StatelessWidget {
const SimpleTableRowDivider({
super.key,
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table.dart
index a4e3dabcbf..06abc4a283 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table.dart
@@ -5,3 +5,6 @@ export 'simple_table_more_action.dart';
export 'simple_table_operations/simple_table_operations.dart';
export 'simple_table_row_block_component.dart';
export 'simple_table_shortcuts/simple_table_commands.dart';
+export 'simple_table_widgets/simple_table_add_column_and_row_button.dart';
+export 'simple_table_widgets/simple_table_add_column_button.dart';
+export 'simple_table_widgets/simple_table_add_row_button.dart';
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_constants.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_constants.dart
index 8cf73a8bcb..9c0952daa4 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_constants.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_constants.dart
@@ -66,6 +66,12 @@ class SimpleTableContext {
/// This value is only available when isReordering is true
final ValueNotifier<Offset> reorderingOffset = ValueNotifier(Offset.zero);
+ /// isDraggingRow to expand the rows of the table
+ bool isDraggingRow = false;
+
+ /// isDraggingColumn to expand the columns of the table
+ bool isDraggingColumn = false;
+
bool get isReordering =>
isReorderingColumn.value.$1 || isReorderingRow.value.$1;
@@ -166,6 +172,8 @@ class SimpleTableConstants {
static const defaultColumnWidth = 120.0;
static const minimumColumnWidth = 36.0;
+ static const defaultRowHeight = 36.0;
+
static const tableTopPadding = 8.0;
static const tableLeftPadding = 8.0;
@@ -223,6 +231,13 @@ class SimpleTableConstants {
static const moreActionPadding = EdgeInsets.symmetric(vertical: 2.0);
static const moreActionHorizontalMargin =
EdgeInsets.symmetric(horizontal: 6.0);
+
+ /// Only displaying the add row / add column / add column and row button
+ /// when hovering on the last row / last column / last cell.
+ static const enableHoveringLogicV2 = true;
+
+ /// Enable the drag to expand the table
+ static const enableDragToExpandTable = false;
}
enum SimpleTableBorderRenderType {
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_delete_operation.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_delete_operation.dart
index cb9c7f28dd..6598ad5a41 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_delete_operation.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_delete_operation.dart
@@ -18,8 +18,9 @@ extension TableDeletionOperations on EditorState {
/// Row 1: | | | |
Future<void> deleteRowInTable(
Node node,
- int index,
- ) async {
+ int index, {
+ bool inMemoryUpdate = false,
+ }) async {
assert(node.type == SimpleTableBlockKeys.type);
if (node.type != SimpleTableBlockKeys.type) {
@@ -48,7 +49,12 @@ extension TableDeletionOperations on EditorState {
if (attributes != null) {
transaction.updateNode(node, attributes);
}
- await apply(transaction);
+ await apply(
+ transaction,
+ options: ApplyOptions(
+ inMemoryUpdate: inMemoryUpdate,
+ ),
+ );
}
/// Delete a column at the given index.
@@ -64,7 +70,11 @@ extension TableDeletionOperations on EditorState {
/// After:
/// Row 1: | 0 | 1 |
/// Row 2: | | |
- Future<void> deleteColumnInTable(Node node, int index) async {
+ Future<void> deleteColumnInTable(
+ Node node,
+ int index, {
+ bool inMemoryUpdate = false,
+ }) async {
assert(node.type == SimpleTableBlockKeys.type);
if (node.type != SimpleTableBlockKeys.type) {
@@ -96,6 +106,11 @@ extension TableDeletionOperations on EditorState {
if (attributes != null) {
transaction.updateNode(node, attributes);
}
- await apply(transaction);
+ await apply(
+ transaction,
+ options: ApplyOptions(
+ inMemoryUpdate: inMemoryUpdate,
+ ),
+ );
}
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_insert_operation.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_insert_operation.dart
index d430569dcf..7a92aa3c7e 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_insert_operation.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_insert_operation.dart
@@ -1,8 +1,8 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_block_component.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_cell_block_component.dart';
-import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_row_block_component.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_map_operation.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_node_extension.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_row_block_component.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
@@ -90,7 +90,11 @@ extension TableInsertionOperations on EditorState {
/// After: ↓ New column
/// Row 1: | 0 | | 1 |
/// Row 2: | | | |
- Future<void> insertColumnInTable(Node node, int index) async {
+ Future<void> insertColumnInTable(
+ Node node,
+ int index, {
+ bool inMemoryUpdate = false,
+ }) async {
assert(node.type == SimpleTableBlockKeys.type);
if (node.type != SimpleTableBlockKeys.type) {
@@ -133,7 +137,12 @@ extension TableInsertionOperations on EditorState {
if (attributes != null) {
transaction.updateNode(node, attributes);
}
- await apply(transaction);
+ await apply(
+ transaction,
+ options: ApplyOptions(
+ inMemoryUpdate: inMemoryUpdate,
+ ),
+ );
}
/// Add a row at the given index.
@@ -149,7 +158,11 @@ extension TableInsertionOperations on EditorState {
/// Row 1: | | |
/// Row 2: | | |
/// Row 3: | | | ← New row
- Future<void> insertRowInTable(Node node, int index) async {
+ Future<void> insertRowInTable(
+ Node node,
+ int index, {
+ bool inMemoryUpdate = false,
+ }) async {
assert(node.type == SimpleTableBlockKeys.type);
if (node.type != SimpleTableBlockKeys.type) {
@@ -190,6 +203,11 @@ extension TableInsertionOperations on EditorState {
if (attributes != null) {
transaction.updateNode(node, attributes);
}
- await apply(transaction);
+ await apply(
+ transaction,
+ options: ApplyOptions(
+ inMemoryUpdate: inMemoryUpdate,
+ ),
+ );
}
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_node_extension.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_node_extension.dart
index c7e1f73ae2..ab95bdcf54 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_node_extension.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_operations/simple_table_node_extension.dart
@@ -6,6 +6,7 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_tab
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_row_block_component.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
typedef TableCellPosition = (int, int);
@@ -95,6 +96,9 @@ extension TableNodeExtension on Node {
int get rowIndex {
if (type == SimpleTableCellBlockKeys.type) {
+ if (path.parent.isEmpty) {
+ return -1;
+ }
return path.parent.last;
} else if (type == SimpleTableRowBlockKeys.type) {
return path.last;
@@ -104,6 +108,9 @@ extension TableNodeExtension on Node {
int get columnIndex {
assert(type == SimpleTableCellBlockKeys.type);
+ if (path.isEmpty) {
+ return -1;
+ }
return path.last;
}
@@ -191,6 +198,18 @@ extension TableNodeExtension on Node {
return tableNode;
}
+ Node? get parentTableCellNode {
+ Node? tableCellNode;
+
+ if (type == SimpleTableCellBlockKeys.type) {
+ tableCellNode = this;
+ } else {
+ return parent?.parentTableCellNode;
+ }
+
+ return tableCellNode;
+ }
+
double get columnWidth {
final parentTableNode = this.parentTableNode;
@@ -260,7 +279,8 @@ extension TableNodeExtension on Node {
parentTableNode.type != SimpleTableBlockKeys.type) {
return false;
}
- return parentTableNode.isHeaderColumnEnabled && parent?.columnIndex == 0;
+ return parentTableNode.isHeaderColumnEnabled &&
+ parentTableCellNode?.columnIndex == 0;
}
/// Whether the current node is in the header row.
@@ -272,7 +292,8 @@ extension TableNodeExtension on Node {
parentTableNode.type != SimpleTableBlockKeys.type) {
return false;
}
- return parentTableNode.isHeaderRowEnabled && parent?.rowIndex == 0;
+ return parentTableNode.isHeaderRowEnabled &&
+ parentTableCellNode?.rowIndex == 0;
}
SimpleTableRowAlignMap get rowAligns {
@@ -529,4 +550,95 @@ extension TableNodeExtension on Node {
.join('\n');
return content;
}
+
+ /// Return the first empty row in the table from bottom to top.
+ ///
+ /// Example:
+ ///
+ /// | A | B | C |
+ /// | | | |
+ /// | E | F | G |
+ /// | H | I | J |
+ /// | | | | <--- The first empty row is the row at index 3.
+ /// | | | |
+ ///
+ /// The first empty row is the row at index 3.
+ (int, Node)? getFirstEmptyRowFromBottom() {
+ assert(type == SimpleTableBlockKeys.type);
+
+ if (type != SimpleTableBlockKeys.type) {
+ return null;
+ }
+
+ (int, Node)? result;
+
+ for (var i = children.length - 1; i >= 0; i--) {
+ final row = children[i];
+
+ // Check if all cells in this row are empty
+ final hasContent = row.children.any((cell) {
+ final content = getTableCellContent(
+ rowIndex: i,
+ columnIndex: row.children.indexOf(cell),
+ );
+ return content != null && content.isNotEmpty;
+ });
+
+ if (!hasContent) {
+ if (result != null) {
+ final (index, _) = result;
+ if (i <= index) {
+ result = (i, row);
+ }
+ } else {
+ result = (i, row);
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /// Return the first empty column in the table from right to left.
+ ///
+ /// Example:
+ /// ↓ The first empty column is the column at index 3.
+ /// | A | C | | E | | |
+ /// | B | D | | F | | |
+ ///
+ /// The first empty column is the column at index 3.
+ int? getFirstEmptyColumnFromRight() {
+ assert(type == SimpleTableBlockKeys.type);
+
+ if (type != SimpleTableBlockKeys.type) {
+ return null;
+ }
+
+ int? result;
+
+ for (var i = columnLength - 1; i >= 0; i--) {
+ bool hasContent = false;
+ for (var j = 0; j < rowLength; j++) {
+ final content = getTableCellContent(
+ rowIndex: j,
+ columnIndex: i,
+ );
+ if (content != null && content.isNotEmpty) {
+ hasContent = true;
+ }
+ }
+ if (!hasContent) {
+ if (result != null) {
+ final index = result;
+ if (i <= index) {
+ result = i;
+ }
+ } else {
+ result = i;
+ }
+ }
+ }
+
+ return result;
+ }
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_backspace_command.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_backspace_command.dart
index 0ebfd013e3..dedc59da47 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_backspace_command.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_backspace_command.dart
@@ -1,5 +1,6 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_command_extension.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart';
import 'package:flutter/material.dart';
final CommandShortcutEvent backspaceInTableCell = CommandShortcutEvent(
@@ -22,11 +23,25 @@ KeyEventResult _backspaceInTableCellHandler(EditorState editorState) {
final onlyContainsOneChild = tableCellNode.children.length == 1;
final isParagraphNode =
tableCellNode.children.first.type == ParagraphBlockKeys.type;
+ final isCodeBlock = tableCellNode.children.first.type == CodeBlockKeys.type;
if (onlyContainsOneChild &&
selection.isCollapsed &&
- selection.end.offset == 0 &&
- isParagraphNode) {
- return KeyEventResult.skipRemainingHandlers;
+ selection.end.offset == 0) {
+ if (isParagraphNode) {
+ return KeyEventResult.skipRemainingHandlers;
+ } else if (isCodeBlock) {
+ // replace the codeblock with a paragraph
+ final transaction = editorState.transaction;
+ transaction.insertNode(node.path, paragraphNode());
+ transaction.deleteNode(node);
+ transaction.afterSelection = Selection.collapsed(
+ Position(
+ path: node.path,
+ ),
+ );
+ editorState.apply(transaction);
+ return KeyEventResult.handled;
+ }
}
return KeyEventResult.ignored;
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_commands.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_commands.dart
index b52adc6334..6fca0a5275 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_commands.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_commands.dart
@@ -3,6 +3,7 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_tab
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_arrow_right_command.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_arrow_up_command.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_backspace_command.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_enter_command.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_select_all_command.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_tab_command.dart';
@@ -15,4 +16,5 @@ final simpleTableCommands = [
shiftTabInTableCell,
backspaceInTableCell,
selectAllInTableCellCommand,
+ enterInTableCell,
];
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_enter_command.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_enter_command.dart
new file mode 100644
index 0000000000..bc5a0a09c2
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_enter_command.dart
@@ -0,0 +1,24 @@
+import 'package:appflowy/plugins/document/presentation/editor_plugins/simple_table/simple_table_shortcuts/simple_table_command_extension.dart';
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:flutter/material.dart';
+
+final CommandShortcutEvent enterInTableCell = CommandShortcutEvent(
+ key: 'Press enter in table cell',
+ getDescription: () => 'Press the enter key in table cell',
+ command: 'enter',
+ handler: _enterInTableCellHandler,
+);
+
+KeyEventResult _enterInTableCellHandler(EditorState editorState) {
+ final (isInTableCell, selection, tableCellNode, node) =
+ editorState.isCurrentSelectionInTableCell();
+ if (!isInTableCell ||
+ selection == null ||
+ tableCellNode == null ||
+ node == null) {
+ return KeyEventResult.ignored;
+ }
+
+ // forward the enter command to the insertNewLine character command to support multi-line text in table cell
+ return KeyEventResult.skipRemainingHandlers;
+}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_add_column_and_row_button.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_add_column_and_row_button.dart
new file mode 100644
index 0000000000..b859895c6d
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_add_column_and_row_button.dart
@@ -0,0 +1,91 @@
+import 'package:appflowy/generated/flowy_svgs.g.dart';
+import 'package:appflowy/generated/locale_keys.g.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:easy_localization/easy_localization.dart';
+import 'package:flowy_infra_ui/flowy_infra_ui.dart';
+import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
+
+class SimpleTableAddColumnAndRowHoverButton extends StatelessWidget {
+ const SimpleTableAddColumnAndRowHoverButton({
+ super.key,
+ required this.editorState,
+ required this.node,
+ });
+
+ final EditorState editorState;
+ final Node node;
+
+ @override
+ Widget build(BuildContext context) {
+ assert(node.type == SimpleTableBlockKeys.type);
+
+ if (node.type != SimpleTableBlockKeys.type) {
+ return const SizedBox.shrink();
+ }
+
+ return ValueListenableBuilder(
+ valueListenable: context.read<SimpleTableContext>().isHoveringOnTableArea,
+ builder: (context, isHoveringOnTableArea, child) {
+ return ValueListenableBuilder(
+ valueListenable: context.read<SimpleTableContext>().hoveringTableCell,
+ builder: (context, hoveringTableCell, child) {
+ bool shouldShow = isHoveringOnTableArea;
+ if (hoveringTableCell != null &&
+ SimpleTableConstants.enableHoveringLogicV2) {
+ shouldShow = hoveringTableCell.isLastCellInTable;
+ }
+ return shouldShow
+ ? Positioned(
+ bottom:
+ SimpleTableConstants.addColumnAndRowButtonBottomPadding,
+ right: SimpleTableConstants.addColumnButtonPadding,
+ child: SimpleTableAddColumnAndRowButton(
+ onTap: () => editorState.addColumnAndRowInTable(node),
+ ),
+ )
+ : const SizedBox.shrink();
+ },
+ );
+ },
+ );
+ }
+}
+
+class SimpleTableAddColumnAndRowButton extends StatelessWidget {
+ const SimpleTableAddColumnAndRowButton({
+ super.key,
+ this.onTap,
+ });
+
+ final VoidCallback? onTap;
+
+ @override
+ Widget build(BuildContext context) {
+ return FlowyTooltip(
+ message: LocaleKeys.document_plugins_simpleTable_clickToAddNewRowAndColumn
+ .tr(),
+ child: GestureDetector(
+ behavior: HitTestBehavior.translucent,
+ onTap: onTap,
+ child: MouseRegion(
+ cursor: SystemMouseCursors.click,
+ child: Container(
+ width: SimpleTableConstants.addColumnAndRowButtonWidth,
+ height: SimpleTableConstants.addColumnAndRowButtonHeight,
+ decoration: BoxDecoration(
+ borderRadius: BorderRadius.circular(
+ SimpleTableConstants.addColumnAndRowButtonCornerRadius,
+ ),
+ color: context.simpleTableMoreActionBackgroundColor,
+ ),
+ child: const FlowySvg(
+ FlowySvgs.add_s,
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_add_column_button.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_add_column_button.dart
new file mode 100644
index 0000000000..9287b78e88
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_add_column_button.dart
@@ -0,0 +1,219 @@
+import 'package:appflowy/generated/flowy_svgs.g.dart';
+import 'package:appflowy/generated/locale_keys.g.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:easy_localization/easy_localization.dart';
+import 'package:flowy_infra_ui/flowy_infra_ui.dart';
+import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
+
+class SimpleTableAddColumnHoverButton extends StatefulWidget {
+ const SimpleTableAddColumnHoverButton({
+ super.key,
+ required this.editorState,
+ required this.tableNode,
+ });
+
+ final EditorState editorState;
+ final Node tableNode;
+
+ @override
+ State<SimpleTableAddColumnHoverButton> createState() =>
+ _SimpleTableAddColumnHoverButtonState();
+}
+
+class _SimpleTableAddColumnHoverButtonState
+ extends State<SimpleTableAddColumnHoverButton> {
+ late final interceptorKey =
+ 'simple_table_add_column_hover_button_${widget.tableNode.id}';
+
+ SelectionGestureInterceptor? interceptor;
+
+ Offset? startDraggingOffset;
+ int? initialColumnCount;
+
+ @override
+ void initState() {
+ super.initState();
+
+ interceptor = SelectionGestureInterceptor(
+ key: interceptorKey,
+ canTap: (details) => !_isTapInBounds(details.globalPosition),
+ );
+ widget.editorState.service.selectionService
+ .registerGestureInterceptor(interceptor!);
+ }
+
+ @override
+ void dispose() {
+ widget.editorState.service.selectionService.unregisterGestureInterceptor(
+ interceptorKey,
+ );
+
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ assert(widget.tableNode.type == SimpleTableBlockKeys.type);
+
+ if (widget.tableNode.type != SimpleTableBlockKeys.type) {
+ return const SizedBox.shrink();
+ }
+
+ return ValueListenableBuilder(
+ valueListenable: context.read<SimpleTableContext>().isHoveringOnTableArea,
+ builder: (context, isHoveringOnTableArea, _) {
+ return ValueListenableBuilder(
+ valueListenable: context.read<SimpleTableContext>().hoveringTableCell,
+ builder: (context, hoveringTableCell, _) {
+ bool shouldShow = isHoveringOnTableArea;
+ if (hoveringTableCell != null &&
+ SimpleTableConstants.enableHoveringLogicV2) {
+ shouldShow = hoveringTableCell.columnIndex + 1 ==
+ hoveringTableCell.columnLength;
+ }
+ return Positioned(
+ top: SimpleTableConstants.tableTopPadding -
+ SimpleTableConstants.cellBorderWidth,
+ bottom: SimpleTableConstants.addColumnButtonBottomPadding,
+ right: 0,
+ child: Opacity(
+ opacity: shouldShow ? 1.0 : 0.0,
+ child: SimpleTableAddColumnButton(
+ onTap: () =>
+ widget.editorState.addColumnInTable(widget.tableNode),
+ onHorizontalDragStart: (details) {
+ context.read<SimpleTableContext>().isDraggingColumn = true;
+ startDraggingOffset = details.globalPosition;
+ initialColumnCount = widget.tableNode.columnLength;
+ },
+ onHorizontalDragEnd: (details) {
+ context.read<SimpleTableContext>().isDraggingColumn = false;
+ },
+ onHorizontalDragUpdate: (details) {
+ _insertColumnInMemory(details);
+ },
+ ),
+ ),
+ );
+ },
+ );
+ },
+ );
+ }
+
+ bool _isTapInBounds(Offset offset) {
+ final renderBox = context.findRenderObject() as RenderBox?;
+ if (renderBox == null) {
+ return false;
+ }
+
+ final localPosition = renderBox.globalToLocal(offset);
+ final result = renderBox.paintBounds.contains(localPosition);
+
+ return result;
+ }
+
+ void _insertColumnInMemory(DragUpdateDetails details) {
+ if (!SimpleTableConstants.enableDragToExpandTable) {
+ return;
+ }
+
+ if (startDraggingOffset == null || initialColumnCount == null) {
+ return;
+ }
+
+ // calculate the horizontal offset from the start dragging offset
+ final horizontalOffset =
+ details.globalPosition.dx - startDraggingOffset!.dx;
+
+ const columnWidth = SimpleTableConstants.defaultColumnWidth;
+ final columnDelta = (horizontalOffset / columnWidth).round();
+
+ // if the change is less than 1 column, skip the operation
+ if (columnDelta.abs() < 1) {
+ return;
+ }
+
+ final firstEmptyColumnFromRight =
+ widget.tableNode.getFirstEmptyColumnFromRight();
+ if (firstEmptyColumnFromRight == null) {
+ return;
+ }
+
+ final currentColumnCount = widget.tableNode.columnLength;
+ final targetColumnCount = initialColumnCount! + columnDelta;
+
+ // There're 3 cases that we don't want to proceed:
+ // 1. targetColumnCount < 0: the table at least has 1 column
+ // 2. targetColumnCount == currentColumnCount: the table has no change
+ // 3. targetColumnCount <= initialColumnCount: the table has less columns than the initial column count
+ if (targetColumnCount <= 0 ||
+ targetColumnCount == currentColumnCount ||
+ targetColumnCount <= firstEmptyColumnFromRight) {
+ return;
+ }
+
+ if (targetColumnCount > currentColumnCount) {
+ widget.editorState.insertColumnInTable(
+ widget.tableNode,
+ targetColumnCount,
+ inMemoryUpdate: true,
+ );
+ } else {
+ widget.editorState.deleteColumnInTable(
+ widget.tableNode,
+ targetColumnCount,
+ inMemoryUpdate: true,
+ );
+ }
+ }
+}
+
+class SimpleTableAddColumnButton extends StatelessWidget {
+ const SimpleTableAddColumnButton({
+ super.key,
+ this.onTap,
+ required this.onHorizontalDragStart,
+ required this.onHorizontalDragEnd,
+ required this.onHorizontalDragUpdate,
+ });
+
+ final VoidCallback? onTap;
+ final void Function(DragStartDetails) onHorizontalDragStart;
+ final void Function(DragEndDetails) onHorizontalDragEnd;
+ final void Function(DragUpdateDetails) onHorizontalDragUpdate;
+
+ @override
+ Widget build(BuildContext context) {
+ return FlowyTooltip(
+ message: LocaleKeys.document_plugins_simpleTable_clickToAddNewColumn.tr(),
+ child: GestureDetector(
+ behavior: HitTestBehavior.translucent,
+ onTap: onTap,
+ onHorizontalDragStart: onHorizontalDragStart,
+ onHorizontalDragEnd: onHorizontalDragEnd,
+ onHorizontalDragUpdate: onHorizontalDragUpdate,
+ child: MouseRegion(
+ cursor: SystemMouseCursors.click,
+ child: Container(
+ width: SimpleTableConstants.addColumnButtonWidth,
+ margin: const EdgeInsets.symmetric(
+ horizontal: SimpleTableConstants.addColumnButtonPadding,
+ ),
+ decoration: BoxDecoration(
+ borderRadius: BorderRadius.circular(
+ SimpleTableConstants.addColumnButtonRadius,
+ ),
+ color: context.simpleTableMoreActionBackgroundColor,
+ ),
+ child: const FlowySvg(
+ FlowySvgs.add_s,
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_add_row_button.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_add_row_button.dart
new file mode 100644
index 0000000000..f0037bd0af
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_add_row_button.dart
@@ -0,0 +1,221 @@
+import 'package:appflowy/generated/flowy_svgs.g.dart';
+import 'package:appflowy/generated/locale_keys.g.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:easy_localization/easy_localization.dart';
+import 'package:flowy_infra_ui/flowy_infra_ui.dart';
+import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
+
+class SimpleTableAddRowHoverButton extends StatefulWidget {
+ const SimpleTableAddRowHoverButton({
+ super.key,
+ required this.editorState,
+ required this.tableNode,
+ });
+
+ final EditorState editorState;
+ final Node tableNode;
+
+ @override
+ State<SimpleTableAddRowHoverButton> createState() =>
+ _SimpleTableAddRowHoverButtonState();
+}
+
+class _SimpleTableAddRowHoverButtonState
+ extends State<SimpleTableAddRowHoverButton> {
+ late final interceptorKey =
+ 'simple_table_add_row_hover_button_${widget.tableNode.id}';
+
+ SelectionGestureInterceptor? interceptor;
+
+ Offset? startDraggingOffset;
+ int? initialRowCount;
+
+ @override
+ void initState() {
+ super.initState();
+
+ interceptor = SelectionGestureInterceptor(
+ key: interceptorKey,
+ canTap: (details) => !_isTapInBounds(details.globalPosition),
+ );
+ widget.editorState.service.selectionService
+ .registerGestureInterceptor(interceptor!);
+ }
+
+ @override
+ void dispose() {
+ widget.editorState.service.selectionService.unregisterGestureInterceptor(
+ interceptorKey,
+ );
+
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ assert(widget.tableNode.type == SimpleTableBlockKeys.type);
+
+ if (widget.tableNode.type != SimpleTableBlockKeys.type) {
+ return const SizedBox.shrink();
+ }
+
+ final simpleTableContext = context.read<SimpleTableContext>();
+ return ValueListenableBuilder(
+ valueListenable: simpleTableContext.isHoveringOnTableArea,
+ builder: (context, isHoveringOnTableArea, child) {
+ return ValueListenableBuilder(
+ valueListenable: simpleTableContext.hoveringTableCell,
+ builder: (context, hoveringTableCell, _) {
+ bool shouldShow = isHoveringOnTableArea;
+ if (hoveringTableCell != null &&
+ SimpleTableConstants.enableHoveringLogicV2) {
+ shouldShow =
+ hoveringTableCell.rowIndex + 1 == hoveringTableCell.rowLength;
+ }
+ if (simpleTableContext.isDraggingRow) {
+ shouldShow = true;
+ }
+ return shouldShow ? child! : const SizedBox.shrink();
+ },
+ );
+ },
+ child: Positioned(
+ bottom: 2 * SimpleTableConstants.addRowButtonPadding,
+ left: SimpleTableConstants.tableLeftPadding -
+ SimpleTableConstants.cellBorderWidth,
+ right: SimpleTableConstants.addRowButtonRightPadding,
+ child: SimpleTableAddRowButton(
+ onTap: () => widget.editorState.addRowInTable(
+ widget.tableNode,
+ ),
+ onVerticalDragStart: (details) {
+ context.read<SimpleTableContext>().isDraggingRow = true;
+ startDraggingOffset = details.globalPosition;
+ initialRowCount = widget.tableNode.children.length;
+ },
+ onVerticalDragEnd: (details) {
+ context.read<SimpleTableContext>().isDraggingRow = false;
+ },
+ onVerticalDragUpdate: (details) {
+ _insertRowInMemory(details);
+ },
+ ),
+ ),
+ );
+ }
+
+ bool _isTapInBounds(Offset offset) {
+ final renderBox = context.findRenderObject() as RenderBox?;
+ if (renderBox == null) {
+ return false;
+ }
+
+ final localPosition = renderBox.globalToLocal(offset);
+ final result = renderBox.paintBounds.contains(localPosition);
+
+ return result;
+ }
+
+ void _insertRowInMemory(DragUpdateDetails details) {
+ if (!SimpleTableConstants.enableDragToExpandTable) {
+ return;
+ }
+
+ if (startDraggingOffset == null || initialRowCount == null) {
+ return;
+ }
+
+ // calculate the vertical offset from the start dragging offset
+ final verticalOffset = details.globalPosition.dy - startDraggingOffset!.dy;
+
+ const rowHeight = SimpleTableConstants.defaultRowHeight;
+ final rowDelta = (verticalOffset / rowHeight).round();
+
+ // if the change is less than 1 row, skip the operation
+ if (rowDelta.abs() < 1) {
+ return;
+ }
+
+ final firstEmptyRowFromBottom =
+ widget.tableNode.getFirstEmptyRowFromBottom();
+ if (firstEmptyRowFromBottom == null) {
+ return;
+ }
+
+ final currentRowCount = widget.tableNode.children.length;
+ final targetRowCount = initialRowCount! + rowDelta;
+
+ // There're 3 cases that we don't want to proceed:
+ // 1. targetRowCount < 0: the table at least has 1 row
+ // 2. targetRowCount == currentRowCount: the table has no change
+ // 3. targetRowCount <= initialRowCount: the table has less rows than the initial row count
+ if (targetRowCount <= 0 ||
+ targetRowCount == currentRowCount ||
+ targetRowCount <= firstEmptyRowFromBottom.$1) {
+ return;
+ }
+
+ if (targetRowCount > currentRowCount) {
+ widget.editorState.insertRowInTable(
+ widget.tableNode,
+ targetRowCount,
+ inMemoryUpdate: true,
+ );
+ } else {
+ widget.editorState.deleteRowInTable(
+ widget.tableNode,
+ targetRowCount,
+ inMemoryUpdate: true,
+ );
+ }
+ }
+}
+
+class SimpleTableAddRowButton extends StatelessWidget {
+ const SimpleTableAddRowButton({
+ super.key,
+ this.onTap,
+ required this.onVerticalDragStart,
+ required this.onVerticalDragEnd,
+ required this.onVerticalDragUpdate,
+ });
+
+ final VoidCallback? onTap;
+ final void Function(DragStartDetails) onVerticalDragStart;
+ final void Function(DragEndDetails) onVerticalDragEnd;
+ final void Function(DragUpdateDetails) onVerticalDragUpdate;
+
+ @override
+ Widget build(BuildContext context) {
+ return FlowyTooltip(
+ message: LocaleKeys.document_plugins_simpleTable_clickToAddNewRow.tr(),
+ child: GestureDetector(
+ behavior: HitTestBehavior.opaque,
+ onTap: onTap,
+ onVerticalDragStart: onVerticalDragStart,
+ onVerticalDragEnd: onVerticalDragEnd,
+ onVerticalDragUpdate: onVerticalDragUpdate,
+ child: MouseRegion(
+ cursor: SystemMouseCursors.click,
+ child: Container(
+ height: SimpleTableConstants.addRowButtonHeight,
+ margin: const EdgeInsets.symmetric(
+ vertical: SimpleTableConstants.addColumnButtonPadding,
+ ),
+ decoration: BoxDecoration(
+ borderRadius: BorderRadius.circular(
+ SimpleTableConstants.addRowButtonRadius,
+ ),
+ color: context.simpleTableMoreActionBackgroundColor,
+ ),
+ child: const FlowySvg(
+ FlowySvgs.add_s,
+ ),
+ ),
+ ),
+ ),
+ );
+ }
+}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_column_resize_handle.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_column_resize_handle.dart
index 5555c39de4..bfa1f316d7 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_column_resize_handle.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_column_resize_handle.dart
@@ -20,12 +20,12 @@ class SimpleTableColumnResizeHandle extends StatefulWidget {
class _SimpleTableColumnResizeHandleState
extends State<SimpleTableColumnResizeHandle> {
+ late final simpleTableContext = context.read<SimpleTableContext>();
+
bool isStartDragging = false;
@override
Widget build(BuildContext context) {
- final simpleTableContext = context.read<SimpleTableContext>();
-
return MouseRegion(
cursor: SystemMouseCursors.resizeColumn,
onEnter: (_) => _onEnterHoverArea(),
@@ -57,8 +57,7 @@ class _SimpleTableColumnResizeHandleState
}
void _onEnterHoverArea() {
- context.read<SimpleTableContext>().hoveringOnResizeHandle.value =
- widget.node;
+ simpleTableContext.hoveringOnResizeHandle.value = widget.node;
}
void _onExitHoverArea() {
@@ -66,7 +65,7 @@ class _SimpleTableColumnResizeHandleState
// the onExit event will be triggered before dragging started.
// delay the hiding of the resize handle to avoid flickering.
if (!isStartDragging) {
- context.read<SimpleTableContext>().hoveringOnResizeHandle.value = null;
+ simpleTableContext.hoveringOnResizeHandle.value = null;
}
});
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_widget.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_widget.dart
index 62acf509e1..0767ec4a47 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_widget.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/simple_table/simple_table_widgets/simple_table_widget.dart
@@ -134,7 +134,7 @@ class _SimpleTableWidgetState extends State<SimpleTableWidget> {
if (widget.enableAddColumnButton)
SimpleTableAddColumnHoverButton(
editorState: editorState,
- node: widget.node,
+ tableNode: widget.node,
),
if (widget.enableAddRowButton)
SimpleTableAddRowHoverButton(
diff --git a/frontend/appflowy_flutter/pubspec.lock b/frontend/appflowy_flutter/pubspec.lock
index c8dc56a973..9e05d06614 100644
--- a/frontend/appflowy_flutter/pubspec.lock
+++ b/frontend/appflowy_flutter/pubspec.lock
@@ -61,8 +61,8 @@ packages:
dependency: "direct main"
description:
path: "."
- ref: "157ded3"
- resolved-ref: "157ded3cd321b9a54d011c0cc27e270ded35d3aa"
+ ref: f32762e
+ resolved-ref: f32762eae0a703e15ae681947ad1ff110d2e477a
url: "https://github.com/AppFlowy-IO/appflowy-editor.git"
source: git
version: "4.0.0"
diff --git a/frontend/appflowy_flutter/pubspec.yaml b/frontend/appflowy_flutter/pubspec.yaml
index f02b420acb..e452bdab82 100644
--- a/frontend/appflowy_flutter/pubspec.yaml
+++ b/frontend/appflowy_flutter/pubspec.yaml
@@ -173,7 +173,7 @@ dependency_overrides:
appflowy_editor:
git:
url: https://github.com/AppFlowy-IO/appflowy-editor.git
- ref: "157ded3"
+ ref: "f32762e"
appflowy_editor_plugins:
git: