From e585bffc1e8a6e333ad5e7a59e9c44d99f4ee810 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Wed, 30 Aug 2023 16:21:37 +0800 Subject: [PATCH 01/10] fix: resizing the table won't send transaction --- .../table_block_component.dart | 4 +- .../table_col_border.dart | 2 +- .../table_block_component/table_node.dart | 43 ++++++++++++++++--- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lib/src/editor/block_component/table_block_component/table_block_component.dart b/lib/src/editor/block_component/table_block_component/table_block_component.dart index edcdb4dd2..a5308da9c 100644 --- a/lib/src/editor/block_component/table_block_component/table_block_component.dart +++ b/lib/src/editor/block_component/table_block_component/table_block_component.dart @@ -2,6 +2,7 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/src/editor/block_component/table_block_component/table_node.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; + import 'table_view.dart'; class TableBlockKeys { @@ -85,10 +86,11 @@ class TableBlockComponentBuilder extends BlockComponentBuilder { @override BlockComponentWidget build(BlockComponentContext blockComponentContext) { + final editorState = blockComponentContext.buildContext.read(); final node = blockComponentContext.node; return TableBlockComponentWidget( key: node.key, - tableNode: TableNode(node: node), + tableNode: TableNode(node: node, editorState: editorState), node: node, configuration: configuration, addIcon: addIcon, diff --git a/lib/src/editor/block_component/table_block_component/table_col_border.dart b/lib/src/editor/block_component/table_block_component/table_col_border.dart index 9e97c863d..d5b0885fc 100644 --- a/lib/src/editor/block_component/table_block_component/table_col_border.dart +++ b/lib/src/editor/block_component/table_block_component/table_col_border.dart @@ -1,6 +1,6 @@ -import 'package:flutter/material.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/src/editor/block_component/table_block_component/table_node.dart'; +import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class TableColBorder extends StatefulWidget { diff --git a/lib/src/editor/block_component/table_block_component/table_node.dart b/lib/src/editor/block_component/table_block_component/table_node.dart index e677227f8..95ba834ce 100644 --- a/lib/src/editor/block_component/table_block_component/table_node.dart +++ b/lib/src/editor/block_component/table_block_component/table_node.dart @@ -1,4 +1,5 @@ import 'dart:math'; + import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/src/editor/block_component/table_block_component/table_config.dart'; @@ -6,10 +7,12 @@ class TableNode { final TableConfig _config; final Node node; + final EditorState? editorState; final List> _cells = []; TableNode({ required this.node, + this.editorState, }) : _config = TableConfig.fromJson(node.attributes) { assert(node.type == TableBlockKeys.type); assert(node.attributes.containsKey(TableBlockKeys.colsLen)); @@ -141,20 +144,44 @@ class TableNode { ) + _config.borderWidth; + // void setColWidth(int col, double w) { + // w = w < _config.colMinimumWidth ? _config.colMinimumWidth : w; + // if (getColWidth(col) != w) { + // for (var i = 0; i < rowsLen; i++) { + // _cells[col][i].updateAttributes({TableBlockKeys.width: w}); + // } + // for (var i = 0; i < rowsLen; i++) { + // updateRowHeight(i); + // } + + // } + // } + void setColWidth(int col, double w) { + if (editorState == null) { + return; + } + final transaction = editorState!.transaction; w = w < _config.colMinimumWidth ? _config.colMinimumWidth : w; if (getColWidth(col) != w) { for (var i = 0; i < rowsLen; i++) { - _cells[col][i].updateAttributes({TableBlockKeys.width: w}); + transaction.updateNode(_cells[col][i], {TableBlockKeys.width: w}); } for (var i = 0; i < rowsLen; i++) { updateRowHeight(i); } - node.updateAttributes({}); + transaction.updateNode(node, node.attributes); } + transaction.afterSelection = transaction.beforeSelection; + editorState!.apply(transaction); } - void updateRowHeight(int row) { + Future updateRowHeight(int row) async { + if (editorState == null) { + return; + } + final transaction = editorState!.transaction; + // The extra 8 is because of paragraph padding double maxHeight = _cells .map((c) => c[row].children.first.rect.height + 8) @@ -162,12 +189,18 @@ class TableNode { if (_cells[0][row].attributes[TableBlockKeys.height] != maxHeight) { for (var i = 0; i < colsLen; i++) { - _cells[i][row].updateAttributes({TableBlockKeys.height: maxHeight}); + transaction.updateNode( + _cells[i][row], + {TableBlockKeys.height: maxHeight}, + ); } } if (node.attributes[TableBlockKeys.colsHeight] != colsHeight) { - node.updateAttributes({TableBlockKeys.colsHeight: colsHeight}); + transaction.updateNode(node, {TableBlockKeys.colsHeight: colsHeight}); } + + transaction.afterSelection = transaction.beforeSelection; + editorState!.apply(transaction); } } From 978d8819cf4c323e77c9879e7168262465a172dc Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Wed, 30 Aug 2023 16:35:06 +0800 Subject: [PATCH 02/10] fix: lost selection after inserting table block --- .../table_block_component.dart | 12 ++++++++++ .../slash_command.dart | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/src/editor/block_component/table_block_component/table_block_component.dart b/lib/src/editor/block_component/table_block_component/table_block_component.dart index a5308da9c..ffcb2f5a9 100644 --- a/lib/src/editor/block_component/table_block_component/table_block_component.dart +++ b/lib/src/editor/block_component/table_block_component/table_block_component.dart @@ -267,8 +267,20 @@ SelectionMenuItem tableMenuItem = SelectionMenuItem( transaction ..insertNode(selection.end.path, tableNode.node) ..deleteNode(currentNode); + transaction.afterSelection = Selection.collapsed( + Position( + path: selection.end.path + [0, 0], + offset: 0, + ), + ); } else { transaction.insertNode(selection.end.path.next, tableNode.node); + transaction.afterSelection = Selection.collapsed( + Position( + path: selection.end.path.next + [0, 0], + offset: 0, + ), + ); } editorState.apply(transaction); diff --git a/lib/src/editor/editor_component/service/shortcuts/character_shortcut_events/slash_command.dart b/lib/src/editor/editor_component/service/shortcuts/character_shortcut_events/slash_command.dart index 0081bb7ed..5d2b41924 100644 --- a/lib/src/editor/editor_component/service/shortcuts/character_shortcut_events/slash_command.dart +++ b/lib/src/editor/editor_component/service/shortcuts/character_shortcut_events/slash_command.dart @@ -34,6 +34,15 @@ CharacterShortcutEvent customSlashCommand( ); } +final supportSlashMenuNodeWhiteList = [ + ParagraphBlockKeys.type, + HeadingBlockKeys.type, + TodoListBlockKeys.type, + BulletedListBlockKeys.type, + NumberedListBlockKeys.type, + QuoteBlockKeys.type, +]; + SelectionMenuService? _selectionMenuService; Future _showSlashMenu( EditorState editorState, @@ -61,6 +70,13 @@ Future _showSlashMenu( return true; } + final node = editorState.getNodeAtPath(selection.start.path); + + // only enable in white-list nodes + if (node == null || !_isSupportSlashMenuNode(node)) { + return false; + } + // insert the slash character if (shouldInsertSlash) { if (kIsWeb) { @@ -94,3 +110,11 @@ Future _showSlashMenu( return true; } + +bool _isSupportSlashMenuNode(Node node) { + var result = supportSlashMenuNodeWhiteList.contains(node.type); + if (node.level > 1 && node.parent != null) { + return result && _isSupportSlashMenuNode(node.parent!); + } + return result; +} From 830bb02762816d95754c89f4ce4904ef6b4cbd87 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Wed, 30 Aug 2023 16:49:33 +0800 Subject: [PATCH 03/10] fix: test --- .../table_col_border.dart | 4 ++-- .../table_block_component/table_node.dart | 15 ++++++++----- .../table_action_test.dart | 16 +++++++------- .../table_view_test.dart | 21 ++++++++++--------- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/lib/src/editor/block_component/table_block_component/table_col_border.dart b/lib/src/editor/block_component/table_block_component/table_col_border.dart index d5b0885fc..392574f10 100644 --- a/lib/src/editor/block_component/table_block_component/table_col_border.dart +++ b/lib/src/editor/block_component/table_block_component/table_col_border.dart @@ -44,7 +44,7 @@ class _TableColBorderState extends State { child: GestureDetector( onHorizontalDragStart: (_) => setState(() => _borderDragging = true), onHorizontalDragEnd: (_) => setState(() => _borderDragging = false), - onHorizontalDragUpdate: (DragUpdateDetails details) { + onHorizontalDragUpdate: (DragUpdateDetails details) async { final RenderBox box = _borderKey.currentContext?.findRenderObject() as RenderBox; final Offset pos = box.localToGlobal(Offset.zero); @@ -58,7 +58,7 @@ class _TableColBorderState extends State { } final colWidth = widget.tableNode.getColWidth(widget.colIdx); - widget.tableNode + await widget.tableNode .setColWidth(widget.colIdx, colWidth + details.delta.dx); }, child: Container( diff --git a/lib/src/editor/block_component/table_block_component/table_node.dart b/lib/src/editor/block_component/table_block_component/table_node.dart index 95ba834ce..00e2b1b1a 100644 --- a/lib/src/editor/block_component/table_block_component/table_node.dart +++ b/lib/src/editor/block_component/table_block_component/table_node.dart @@ -157,23 +157,28 @@ class TableNode { // } // } - void setColWidth(int col, double w) { + Future setColWidth( + int col, + double w, { + EditorState? editorState, + }) async { + editorState ??= this.editorState; if (editorState == null) { return; } - final transaction = editorState!.transaction; + final transaction = editorState.transaction; w = w < _config.colMinimumWidth ? _config.colMinimumWidth : w; if (getColWidth(col) != w) { for (var i = 0; i < rowsLen; i++) { transaction.updateNode(_cells[col][i], {TableBlockKeys.width: w}); } for (var i = 0; i < rowsLen; i++) { - updateRowHeight(i); + await updateRowHeight(i); } transaction.updateNode(node, node.attributes); } transaction.afterSelection = transaction.beforeSelection; - editorState!.apply(transaction); + await editorState.apply(transaction); } Future updateRowHeight(int row) async { @@ -201,6 +206,6 @@ class TableNode { } transaction.afterSelection = transaction.beforeSelection; - editorState!.apply(transaction); + await editorState!.apply(transaction); } } diff --git a/test/new/block_component/table_block_component/table_action_test.dart b/test/new/block_component/table_block_component/table_action_test.dart index 3a47ede43..e780783a2 100644 --- a/test/new/block_component/table_block_component/table_action_test.dart +++ b/test/new/block_component/table_block_component/table_action_test.dart @@ -1,7 +1,8 @@ import 'package:appflowy_editor/appflowy_editor.dart'; -import 'package:flutter_test/flutter_test.dart'; import 'package:appflowy_editor/src/editor/block_component/table_block_component/table_node.dart'; import 'package:appflowy_editor/src/editor/block_component/table_block_component/util.dart'; +import 'package:flutter_test/flutter_test.dart'; + import '../../infra/testable_editor.dart'; void main() async { @@ -53,8 +54,8 @@ void main() async { final transaction = editor.editorState.transaction; TableActions.delete(tableNode.node, 0, transaction, TableDirection.row); - editor.editorState.apply(transaction); - await tester.pump(const Duration(milliseconds: 100)); + await editor.editorState.apply(transaction); + await tester.pumpAndSettle(const Duration(milliseconds: 200)); tableNode = TableNode(node: tableNode.node); expect(tableNode.rowsLen, 1); @@ -69,6 +70,7 @@ void main() async { } }, ); + await editor.dispose(); }); @@ -89,8 +91,8 @@ void main() async { transaction, TableDirection.col, ); - editor.editorState.apply(transaction); - await tester.pump(const Duration(milliseconds: 100)); + await editor.editorState.apply(transaction); + await tester.pumpAndSettle(const Duration(milliseconds: 100)); tableNode = TableNode(node: tableNode.node); expect(tableNode.colsLen, 3); @@ -120,8 +122,8 @@ void main() async { transaction, TableDirection.row, ); - editor.editorState.apply(transaction); - await tester.pump(const Duration(milliseconds: 100)); + await editor.editorState.apply(transaction); + await tester.pumpAndSettle(const Duration(milliseconds: 100)); tableNode = TableNode(node: tableNode.node); expect(tableNode.rowsLen, 3); diff --git a/test/new/block_component/table_block_component/table_view_test.dart b/test/new/block_component/table_block_component/table_view_test.dart index cdf7d814d..71f190d0e 100644 --- a/test/new/block_component/table_block_component/table_view_test.dart +++ b/test/new/block_component/table_block_component/table_view_test.dart @@ -1,7 +1,8 @@ -import 'package:flutter_test/flutter_test.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; -import 'package:appflowy_editor/src/editor/block_component/table_block_component/util.dart'; import 'package:appflowy_editor/src/editor/block_component/table_block_component/table_node.dart'; +import 'package:appflowy_editor/src/editor/block_component/table_block_component/util.dart'; +import 'package:flutter_test/flutter_test.dart'; + import '../../infra/testable_editor.dart'; void main() async { @@ -35,7 +36,7 @@ void main() async { ), ); await editor.ime.insertText('aaaaaaaaa'); - tableNode.updateRowHeight(0); + await tableNode.updateRowHeight(0); expect(tableNode.getRowHeight(0) != row0beforeHeight, true); expect(tableNode.getRowHeight(0), cell10.children.first.rect.height + 8); @@ -66,13 +67,13 @@ void main() async { ), ); await editor.ime.insertText('aaaaaaaaa'); - tableNode.updateRowHeight(0); + await tableNode.updateRowHeight(0); expect(tableNode.getRowHeight(0) != row0beforeHeight, true); expect(tableNode.getRowHeight(0), cell10.children.first.rect.height + 8); - tableNode.setColWidth(1, 302.5); - await tester.pump(const Duration(milliseconds: 300)); + await tableNode.setColWidth(1, 302.5, editorState: editor.editorState); + await tester.pumpAndSettle(const Duration(milliseconds: 300)); expect(tableNode.getRowHeight(0), row0beforeHeight); await editor.dispose(); @@ -90,8 +91,8 @@ void main() async { final transaction = editor.editorState.transaction; TableActions.add(tableNode.node, 2, transaction, TableDirection.col); - editor.editorState.apply(transaction); - await tester.pump(const Duration(milliseconds: 100)); + await editor.editorState.apply(transaction); + await tester.pumpAndSettle(const Duration(milliseconds: 100)); tableNode = TableNode(node: tableNode.node); expect(tableNode.colsLen, 3); @@ -118,8 +119,8 @@ void main() async { final transaction = editor.editorState.transaction; TableActions.add(tableNode.node, 2, transaction, TableDirection.row); - editor.editorState.apply(transaction); - await tester.pump(const Duration(milliseconds: 100)); + await editor.editorState.apply(transaction); + await tester.pumpAndSettle(const Duration(milliseconds: 100)); tableNode = TableNode(node: tableNode.node); expect(tableNode.rowsLen, 3); From f26025860d37f12e74eb021733d4c9a4694946aa Mon Sep 17 00:00:00 2001 From: Mohammad Zolfaghari Date: Wed, 30 Aug 2023 13:38:05 +0330 Subject: [PATCH 04/10] refactor: don't pass editorState to TableNode pass only transaction to methods directly. --- .../table_block_component.dart | 3 +- .../table_block_component/table_col.dart | 16 +++++--- .../table_col_border.dart | 12 +++++- .../table_block_component/table_node.dart | 37 ++----------------- .../table_view_test.dart | 15 ++++++-- 5 files changed, 37 insertions(+), 46 deletions(-) diff --git a/lib/src/editor/block_component/table_block_component/table_block_component.dart b/lib/src/editor/block_component/table_block_component/table_block_component.dart index ffcb2f5a9..b2966533c 100644 --- a/lib/src/editor/block_component/table_block_component/table_block_component.dart +++ b/lib/src/editor/block_component/table_block_component/table_block_component.dart @@ -86,11 +86,10 @@ class TableBlockComponentBuilder extends BlockComponentBuilder { @override BlockComponentWidget build(BlockComponentContext blockComponentContext) { - final editorState = blockComponentContext.buildContext.read(); final node = blockComponentContext.node; return TableBlockComponentWidget( key: node.key, - tableNode: TableNode(node: node, editorState: editorState), + tableNode: TableNode(node: node), node: node, configuration: configuration, addIcon: addIcon, diff --git a/lib/src/editor/block_component/table_block_component/table_col.dart b/lib/src/editor/block_component/table_block_component/table_col.dart index 2084439df..96ee3621f 100644 --- a/lib/src/editor/block_component/table_block_component/table_col.dart +++ b/lib/src/editor/block_component/table_block_component/table_col.dart @@ -41,6 +41,7 @@ class _TableColState extends State { TableColBorder( resizable: false, tableNode: widget.tableNode, + editorState: widget.editorState, colIdx: widget.colIdx, borderColor: widget.borderColor, borderHoverColor: widget.borderHoverColor, @@ -77,6 +78,7 @@ class _TableColState extends State { TableColBorder( resizable: true, tableNode: widget.tableNode, + editorState: widget.editorState, colIdx: widget.colIdx, borderColor: widget.borderColor, borderHoverColor: widget.borderHoverColor, @@ -117,9 +119,13 @@ class _TableColState extends State { } void updateRowHeightCallback(int row) => - WidgetsBinding.instance.addPostFrameCallback( - (_) => row < widget.tableNode.rowsLen - ? widget.tableNode.updateRowHeight(row) - : null, - ); + WidgetsBinding.instance.addPostFrameCallback((_) { + if (row >= widget.tableNode.rowsLen) { + return; + } + + final transaction = widget.editorState.transaction; + widget.tableNode.updateRowHeight(row, transaction); + widget.editorState.apply(transaction); + }); } diff --git a/lib/src/editor/block_component/table_block_component/table_col_border.dart b/lib/src/editor/block_component/table_block_component/table_col_border.dart index 392574f10..7b2c4e663 100644 --- a/lib/src/editor/block_component/table_block_component/table_col_border.dart +++ b/lib/src/editor/block_component/table_block_component/table_col_border.dart @@ -7,6 +7,7 @@ class TableColBorder extends StatefulWidget { const TableColBorder({ Key? key, required this.tableNode, + required this.editorState, required this.colIdx, required this.resizable, required this.borderColor, @@ -16,6 +17,7 @@ class TableColBorder extends StatefulWidget { final bool resizable; final int colIdx; final TableNode tableNode; + final EditorState editorState; final Color borderColor; final Color borderHoverColor; @@ -58,8 +60,14 @@ class _TableColBorderState extends State { } final colWidth = widget.tableNode.getColWidth(widget.colIdx); - await widget.tableNode - .setColWidth(widget.colIdx, colWidth + details.delta.dx); + + final transaction = widget.editorState.transaction; + widget.tableNode.setColWidth( + widget.colIdx, + colWidth + details.delta.dx, + transaction, + ); + await widget.editorState.apply(transaction); }, child: Container( key: _borderKey, diff --git a/lib/src/editor/block_component/table_block_component/table_node.dart b/lib/src/editor/block_component/table_block_component/table_node.dart index 00e2b1b1a..c74ac9121 100644 --- a/lib/src/editor/block_component/table_block_component/table_node.dart +++ b/lib/src/editor/block_component/table_block_component/table_node.dart @@ -7,12 +7,10 @@ class TableNode { final TableConfig _config; final Node node; - final EditorState? editorState; final List> _cells = []; TableNode({ required this.node, - this.editorState, }) : _config = TableConfig.fromJson(node.attributes) { assert(node.type == TableBlockKeys.type); assert(node.attributes.containsKey(TableBlockKeys.colsLen)); @@ -144,49 +142,21 @@ class TableNode { ) + _config.borderWidth; - // void setColWidth(int col, double w) { - // w = w < _config.colMinimumWidth ? _config.colMinimumWidth : w; - // if (getColWidth(col) != w) { - // for (var i = 0; i < rowsLen; i++) { - // _cells[col][i].updateAttributes({TableBlockKeys.width: w}); - // } - // for (var i = 0; i < rowsLen; i++) { - // updateRowHeight(i); - // } - - // } - // } - - Future setColWidth( - int col, - double w, { - EditorState? editorState, - }) async { - editorState ??= this.editorState; - if (editorState == null) { - return; - } - final transaction = editorState.transaction; + void setColWidth(int col, double w, Transaction transaction) { w = w < _config.colMinimumWidth ? _config.colMinimumWidth : w; if (getColWidth(col) != w) { for (var i = 0; i < rowsLen; i++) { transaction.updateNode(_cells[col][i], {TableBlockKeys.width: w}); } for (var i = 0; i < rowsLen; i++) { - await updateRowHeight(i); + updateRowHeight(i, transaction); } transaction.updateNode(node, node.attributes); } transaction.afterSelection = transaction.beforeSelection; - await editorState.apply(transaction); } - Future updateRowHeight(int row) async { - if (editorState == null) { - return; - } - final transaction = editorState!.transaction; - + void updateRowHeight(int row, Transaction transaction) { // The extra 8 is because of paragraph padding double maxHeight = _cells .map((c) => c[row].children.first.rect.height + 8) @@ -206,6 +176,5 @@ class TableNode { } transaction.afterSelection = transaction.beforeSelection; - await editorState!.apply(transaction); } } diff --git a/test/new/block_component/table_block_component/table_view_test.dart b/test/new/block_component/table_block_component/table_view_test.dart index 71f190d0e..f648f3a95 100644 --- a/test/new/block_component/table_block_component/table_view_test.dart +++ b/test/new/block_component/table_block_component/table_view_test.dart @@ -36,7 +36,10 @@ void main() async { ), ); await editor.ime.insertText('aaaaaaaaa'); - await tableNode.updateRowHeight(0); + + final transaction = editor.editorState.transaction; + tableNode.updateRowHeight(0, transaction); + await editor.editorState.apply(transaction); expect(tableNode.getRowHeight(0) != row0beforeHeight, true); expect(tableNode.getRowHeight(0), cell10.children.first.rect.height + 8); @@ -67,12 +70,18 @@ void main() async { ), ); await editor.ime.insertText('aaaaaaaaa'); - await tableNode.updateRowHeight(0); + + Transaction transaction = editor.editorState.transaction; + tableNode.updateRowHeight(0, transaction); + await editor.editorState.apply(transaction); expect(tableNode.getRowHeight(0) != row0beforeHeight, true); expect(tableNode.getRowHeight(0), cell10.children.first.rect.height + 8); - await tableNode.setColWidth(1, 302.5, editorState: editor.editorState); + transaction = editor.editorState.transaction; + tableNode.setColWidth(1, 302.5, transaction); + await editor.editorState.apply(transaction); + await tester.pumpAndSettle(const Duration(milliseconds: 300)); expect(tableNode.getRowHeight(0), row0beforeHeight); From bd111b4811f0ab1a06a05fe092a87c5f580c80c6 Mon Sep 17 00:00:00 2001 From: Mohammad Zolfaghari Date: Wed, 30 Aug 2023 17:01:54 +0330 Subject: [PATCH 05/10] refactor: reduce extra transactions --- .../block_component/table_block_component/table_col.dart | 5 ++++- .../table_block_component/table_col_border.dart | 1 + .../block_component/table_block_component/table_node.dart | 5 ----- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/src/editor/block_component/table_block_component/table_col.dart b/lib/src/editor/block_component/table_block_component/table_col.dart index 96ee3621f..90d12b119 100644 --- a/lib/src/editor/block_component/table_block_component/table_col.dart +++ b/lib/src/editor/block_component/table_block_component/table_col.dart @@ -126,6 +126,9 @@ class _TableColState extends State { final transaction = widget.editorState.transaction; widget.tableNode.updateRowHeight(row, transaction); - widget.editorState.apply(transaction); + if (transaction.operations.isNotEmpty) { + transaction.afterSelection = transaction.beforeSelection; + widget.editorState.apply(transaction); + } }); } diff --git a/lib/src/editor/block_component/table_block_component/table_col_border.dart b/lib/src/editor/block_component/table_block_component/table_col_border.dart index 7b2c4e663..d755002b3 100644 --- a/lib/src/editor/block_component/table_block_component/table_col_border.dart +++ b/lib/src/editor/block_component/table_block_component/table_col_border.dart @@ -67,6 +67,7 @@ class _TableColBorderState extends State { colWidth + details.delta.dx, transaction, ); + transaction.afterSelection = transaction.beforeSelection; await widget.editorState.apply(transaction); }, child: Container( diff --git a/lib/src/editor/block_component/table_block_component/table_node.dart b/lib/src/editor/block_component/table_block_component/table_node.dart index c74ac9121..fe76ba859 100644 --- a/lib/src/editor/block_component/table_block_component/table_node.dart +++ b/lib/src/editor/block_component/table_block_component/table_node.dart @@ -147,13 +147,10 @@ class TableNode { if (getColWidth(col) != w) { for (var i = 0; i < rowsLen; i++) { transaction.updateNode(_cells[col][i], {TableBlockKeys.width: w}); - } - for (var i = 0; i < rowsLen; i++) { updateRowHeight(i, transaction); } transaction.updateNode(node, node.attributes); } - transaction.afterSelection = transaction.beforeSelection; } void updateRowHeight(int row, Transaction transaction) { @@ -174,7 +171,5 @@ class TableNode { if (node.attributes[TableBlockKeys.colsHeight] != colsHeight) { transaction.updateNode(node, {TableBlockKeys.colsHeight: colsHeight}); } - - transaction.afterSelection = transaction.beforeSelection; } } From 834079f93d9b922b4a8adb0bcbfd183f5d23a6f7 Mon Sep 17 00:00:00 2001 From: Mohammad Zolfaghari Date: Wed, 30 Aug 2023 18:34:35 +0330 Subject: [PATCH 06/10] fix: mitigate Appflowy transaction adapter problem With current transaction adapter in Appflowy which creates `BlockActionPB` for `documentService` (backend). Its not possible to have multiple `InsertOperation`s separately as a single transaction (multiple `transaction.insertNode()`). The `BlockActionPB` creator (`toBlockAction`) can't figure out the `prevId` (previous node of inserting node id), The first insert operations shifts all next nodes path. Because of this situation we decided to apply the insert operations one by one. --- .../table_block_component/table_action.dart | 125 ++++++++++++------ .../table_action_menu.dart | 24 +--- .../table_block_component/table_view.dart | 8 +- .../table_action_test.dart | 26 ++-- .../table_view_test.dart | 18 ++- 5 files changed, 119 insertions(+), 82 deletions(-) diff --git a/lib/src/editor/block_component/table_block_component/table_action.dart b/lib/src/editor/block_component/table_block_component/table_action.dart index 4430024d4..ac24127c3 100644 --- a/lib/src/editor/block_component/table_block_component/table_action.dart +++ b/lib/src/editor/block_component/table_block_component/table_action.dart @@ -7,73 +7,75 @@ class TableActions { static void add( Node node, int position, - Transaction transaction, + EditorState editorState, TableDirection dir, ) { if (dir == TableDirection.col) { - _addCol(node, position, transaction); + _addCol(node, position, editorState); } else { - _addRow(node, position, transaction); + _addRow(node, position, editorState); } } static void delete( Node node, int position, - Transaction transaction, + EditorState editorState, TableDirection dir, ) { if (dir == TableDirection.col) { - _deleteCol(node, position, transaction); + _deleteCol(node, position, editorState); } else { - _deleteRow(node, position, transaction); + _deleteRow(node, position, editorState); } } static void duplicate( Node node, int position, - Transaction transaction, + EditorState editorState, TableDirection dir, ) { if (dir == TableDirection.col) { - _duplicateCol(node, position, transaction); + _duplicateCol(node, position, editorState); } else { - _duplicateRow(node, position, transaction); + _duplicateRow(node, position, editorState); } } static void clear( Node node, int position, - Transaction transaction, + EditorState editorState, TableDirection dir, ) { if (dir == TableDirection.col) { - _clearCol(node, position, transaction); + _clearCol(node, position, editorState); } else { - _clearRow(node, position, transaction); + _clearRow(node, position, editorState); } } static void setBgColor( Node node, int position, - Transaction transaction, + EditorState editorState, String? color, TableDirection dir, ) { if (dir == TableDirection.col) { - _setColBgColor(node, position, transaction, color); + _setColBgColor(node, position, editorState, color); } else { - _setRowBgColor(node, position, transaction, color); + _setRowBgColor(node, position, editorState, color); } } } -void _addCol(Node tableNode, int position, Transaction transaction) { +void _addCol(Node tableNode, int position, EditorState editorState) { assert(position >= 0); + final transaction = editorState.transaction; + List cellNodes = []; final int rowsLen = tableNode.attributes[TableBlockKeys.rowsLen], colsLen = tableNode.attributes[TableBlockKeys.colsLen]; @@ -110,23 +112,16 @@ void _addCol(Node tableNode, int position, Transaction transaction) { // way? transaction.insertNodes(insertPath, cellNodes); transaction.updateNode(tableNode, {TableBlockKeys.colsLen: colsLen + 1}); + + editorState.apply(transaction); } -void _addRow(Node tableNode, int position, Transaction transaction) { +void _addRow(Node tableNode, int position, EditorState editorState) async { assert(position >= 0); final int rowsLen = tableNode.attributes[TableBlockKeys.rowsLen], colsLen = tableNode.attributes[TableBlockKeys.colsLen]; - if (position != rowsLen) { - for (var i = position; i < rowsLen; i++) { - for (var j = 0; j < colsLen; j++) { - final node = getCellNode(tableNode, j, i)!; - transaction.updateNode(node, {TableBlockKeys.rowPosition: i + 1}); - } - } - } - for (var i = 0; i < colsLen; i++) { final node = Node( type: TableCellBlockKeys.type, @@ -143,15 +138,29 @@ void _addRow(Node tableNode, int position, Transaction transaction) { } else { insertPath = getCellNode(tableNode, i, position - 1)!.path.next; } + + final transaction = editorState.transaction; + if (position != rowsLen) { + for (var j = position; j < rowsLen; j++) { + final node = getCellNode(tableNode, i, j)!; + transaction.updateNode(node, {TableBlockKeys.rowPosition: j + 1}); + } + } transaction.insertNode( insertPath, newCellNode(tableNode, node), ); + await editorState.apply(transaction); } + + final transaction = editorState.transaction; transaction.updateNode(tableNode, {TableBlockKeys.rowsLen: rowsLen + 1}); + await editorState.apply(transaction); } -void _deleteCol(Node tableNode, int col, Transaction transaction) { +void _deleteCol(Node tableNode, int col, EditorState editorState) { + final transaction = editorState.transaction; + final int rowsLen = tableNode.attributes[TableBlockKeys.rowsLen], colsLen = tableNode.attributes[TableBlockKeys.colsLen]; List nodes = []; @@ -160,12 +169,16 @@ void _deleteCol(Node tableNode, int col, Transaction transaction) { } transaction.deleteNodes(nodes); - _updateCellPositions(tableNode, transaction, col + 1, 0, -1, 0); + _updateCellPositions(tableNode, editorState, col + 1, 0, -1, 0); transaction.updateNode(tableNode, {TableBlockKeys.colsLen: colsLen - 1}); + + editorState.apply(transaction); } -void _deleteRow(Node tableNode, int row, Transaction transaction) { +void _deleteRow(Node tableNode, int row, EditorState editorState) { + final transaction = editorState.transaction; + final int rowsLen = tableNode.attributes[TableBlockKeys.rowsLen], colsLen = tableNode.attributes[TableBlockKeys.colsLen]; List nodes = []; @@ -174,12 +187,16 @@ void _deleteRow(Node tableNode, int row, Transaction transaction) { } transaction.deleteNodes(nodes); - _updateCellPositions(tableNode, transaction, 0, row + 1, 0, -1); + _updateCellPositions(tableNode, editorState, 0, row + 1, 0, -1); transaction.updateNode(tableNode, {TableBlockKeys.rowsLen: rowsLen - 1}); + + editorState.apply(transaction); } -void _duplicateCol(Node tableNode, int col, Transaction transaction) { +void _duplicateCol(Node tableNode, int col, EditorState editorState) { + final transaction = editorState.transaction; + final int rowsLen = tableNode.attributes[TableBlockKeys.rowsLen], colsLen = tableNode.attributes[TableBlockKeys.colsLen]; List nodes = []; @@ -200,16 +217,23 @@ void _duplicateCol(Node tableNode, int col, Transaction transaction) { nodes, ); - _updateCellPositions(tableNode, transaction, col + 1, 0, 1, 0); + _updateCellPositions(tableNode, editorState, col + 1, 0, 1, 0); transaction.updateNode(tableNode, {TableBlockKeys.colsLen: colsLen + 1}); + + editorState.apply(transaction); } -void _duplicateRow(Node tableNode, int row, Transaction transaction) { +void _duplicateRow(Node tableNode, int row, EditorState editorState) async { + Transaction transaction = editorState.transaction; + _updateCellPositions(tableNode, editorState, 0, row + 1, 0, 1); + await editorState.apply(transaction); + final int rowsLen = tableNode.attributes[TableBlockKeys.rowsLen], colsLen = tableNode.attributes[TableBlockKeys.colsLen]; for (var i = 0; i < colsLen; i++) { final node = getCellNode(tableNode, i, row)!; + transaction = editorState.transaction; transaction.insertNode( node.path.next, node.copyWith( @@ -220,19 +244,22 @@ void _duplicateRow(Node tableNode, int row, Transaction transaction) { }, ), ); + await editorState.apply(transaction); } - _updateCellPositions(tableNode, transaction, 0, row + 1, 0, 1); - + transaction = editorState.transaction; transaction.updateNode(tableNode, {TableBlockKeys.rowsLen: rowsLen + 1}); + editorState.apply(transaction); } void _setColBgColor( Node tableNode, int col, - Transaction transaction, + EditorState editorState, String? color, ) { + final transaction = editorState.transaction; + final rowslen = tableNode.attributes[TableBlockKeys.rowsLen]; for (var i = 0; i < rowslen; i++) { final node = getCellNode(tableNode, col, i)!; @@ -241,14 +268,18 @@ void _setColBgColor( {TableBlockKeys.backgroundColor: color}, ); } + + editorState.apply(transaction); } void _setRowBgColor( Node tableNode, int row, - Transaction transaction, + EditorState editorState, String? color, ) { + final transaction = editorState.transaction; + final colsLen = tableNode.attributes[TableBlockKeys.colsLen]; for (var i = 0; i < colsLen; i++) { final node = getCellNode(tableNode, i, row)!; @@ -257,13 +288,17 @@ void _setRowBgColor( {TableBlockKeys.backgroundColor: color}, ); } + + editorState.apply(transaction); } void _clearCol( Node tableNode, int col, - Transaction transaction, + EditorState editorState, ) { + final transaction = editorState.transaction; + final rowsLen = tableNode.attributes[TableBlockKeys.rowsLen]; for (var i = 0; i < rowsLen; i++) { final node = getCellNode(tableNode, col, i)!; @@ -272,13 +307,17 @@ void _clearCol( paragraphNode(text: ''), ); } + + editorState.apply(transaction); } void _clearRow( Node tableNode, int row, - Transaction transaction, + EditorState editorState, ) { + final transaction = editorState.transaction; + final colsLen = tableNode.attributes[TableBlockKeys.colsLen]; for (var i = 0; i < colsLen; i++) { final node = getCellNode(tableNode, i, row)!; @@ -287,6 +326,8 @@ void _clearRow( paragraphNode(text: ''), ); } + + editorState.apply(transaction); } dynamic newCellNode(Node tableNode, n) { @@ -330,12 +371,14 @@ dynamic newCellNode(Node tableNode, n) { void _updateCellPositions( Node tableNode, - Transaction transaction, + EditorState editorState, int fromCol, int fromRow, int addToCol, int addToRow, ) { + final transaction = editorState.transaction; + final int rowsLen = tableNode.attributes[TableBlockKeys.rowsLen], colsLen = tableNode.attributes[TableBlockKeys.colsLen]; @@ -347,4 +390,6 @@ void _updateCellPositions( }); } } + + editorState.apply(transaction); } diff --git a/lib/src/editor/block_component/table_block_component/table_action_menu.dart b/lib/src/editor/block_component/table_block_component/table_action_menu.dart index f910ac65c..b2cb49356 100644 --- a/lib/src/editor/block_component/table_block_component/table_action_menu.dart +++ b/lib/src/editor/block_component/table_block_component/table_action_menu.dart @@ -39,27 +39,19 @@ void showActionMenu( height: 230, children: [ _menuItem(context, 'Add after', Icons.last_page, () { - final transaction = editorState.transaction; - TableActions.add(node, position + 1, transaction, dir); - editorState.apply(transaction); + TableActions.add(node, position + 1, editorState, dir); dismissOverlay(); }), _menuItem(context, 'Add before', Icons.first_page, () { - final transaction = editorState.transaction; - TableActions.add(node, position, transaction, dir); - editorState.apply(transaction); + TableActions.add(node, position, editorState, dir); dismissOverlay(); }), _menuItem(context, 'Remove', Icons.delete, () { - final transaction = editorState.transaction; - TableActions.delete(node, position, transaction, dir); - editorState.apply(transaction); + TableActions.delete(node, position, editorState, dir); dismissOverlay(); }), _menuItem(context, 'Duplicate', Icons.content_copy, () { - final transaction = editorState.transaction; - TableActions.duplicate(node, position, transaction, dir); - editorState.apply(transaction); + TableActions.duplicate(node, position, editorState, dir); dismissOverlay(); }), _menuItem( @@ -74,15 +66,13 @@ void showActionMenu( _showColorMenu( context, (color) { - final transaction = editorState.transaction; TableActions.setBgColor( node, position, - transaction, + editorState, color, dir, ); - editorState.apply(transaction); }, top: top, bottom: bottom, @@ -94,9 +84,7 @@ void showActionMenu( }, ), _menuItem(context, 'Clear Content', Icons.clear, () { - final transaction = editorState.transaction; - TableActions.clear(node, position, transaction, dir); - editorState.apply(transaction); + TableActions.clear(node, position, editorState, dir); dismissOverlay(); }), ], diff --git a/lib/src/editor/block_component/table_block_component/table_view.dart b/lib/src/editor/block_component/table_block_component/table_view.dart index 6852618fb..258a387ef 100644 --- a/lib/src/editor/block_component/table_block_component/table_view.dart +++ b/lib/src/editor/block_component/table_block_component/table_view.dart @@ -46,14 +46,12 @@ class _TableViewState extends State { width: 28, height: widget.tableNode.colsHeight, onPressed: () { - final transaction = widget.editorState.transaction; TableActions.add( widget.tableNode.node, widget.tableNode.colsLen, - transaction, + widget.editorState, TableDirection.col, ); - widget.editorState.apply(transaction); }, ), ], @@ -64,14 +62,12 @@ class _TableViewState extends State { height: 28, width: widget.tableNode.tableWidth, onPressed: () { - final transaction = widget.editorState.transaction; TableActions.add( widget.tableNode.node, widget.tableNode.rowsLen, - transaction, + widget.editorState, TableDirection.row, ); - widget.editorState.apply(transaction); }, ), ], diff --git a/test/new/block_component/table_block_component/table_action_test.dart b/test/new/block_component/table_block_component/table_action_test.dart index e780783a2..b0f1eda62 100644 --- a/test/new/block_component/table_block_component/table_action_test.dart +++ b/test/new/block_component/table_block_component/table_action_test.dart @@ -21,9 +21,12 @@ void main() async { await editor.startTesting(); await tester.pumpAndSettle(); - final transaction = editor.editorState.transaction; - TableActions.delete(tableNode.node, 0, transaction, TableDirection.col); - editor.editorState.apply(transaction); + TableActions.delete( + tableNode.node, + 0, + editor.editorState, + TableDirection.col, + ); await tester.pump(const Duration(milliseconds: 100)); tableNode = TableNode(node: tableNode.node); @@ -52,9 +55,12 @@ void main() async { await editor.startTesting(); await tester.pumpAndSettle(); - final transaction = editor.editorState.transaction; - TableActions.delete(tableNode.node, 0, transaction, TableDirection.row); - await editor.editorState.apply(transaction); + TableActions.delete( + tableNode.node, + 0, + editor.editorState, + TableDirection.row, + ); await tester.pumpAndSettle(const Duration(milliseconds: 200)); tableNode = TableNode(node: tableNode.node); @@ -84,14 +90,12 @@ void main() async { await editor.startTesting(); await tester.pumpAndSettle(); - final transaction = editor.editorState.transaction; TableActions.duplicate( tableNode.node, 0, - transaction, + editor.editorState, TableDirection.col, ); - await editor.editorState.apply(transaction); await tester.pumpAndSettle(const Duration(milliseconds: 100)); tableNode = TableNode(node: tableNode.node); @@ -115,14 +119,12 @@ void main() async { await editor.startTesting(); await tester.pumpAndSettle(); - final transaction = editor.editorState.transaction; TableActions.duplicate( tableNode.node, 0, - transaction, + editor.editorState, TableDirection.row, ); - await editor.editorState.apply(transaction); await tester.pumpAndSettle(const Duration(milliseconds: 100)); tableNode = TableNode(node: tableNode.node); diff --git a/test/new/block_component/table_block_component/table_view_test.dart b/test/new/block_component/table_block_component/table_view_test.dart index f648f3a95..77a43a04f 100644 --- a/test/new/block_component/table_block_component/table_view_test.dart +++ b/test/new/block_component/table_block_component/table_view_test.dart @@ -98,9 +98,12 @@ void main() async { await editor.startTesting(); await tester.pumpAndSettle(); - final transaction = editor.editorState.transaction; - TableActions.add(tableNode.node, 2, transaction, TableDirection.col); - await editor.editorState.apply(transaction); + TableActions.add( + tableNode.node, + 2, + editor.editorState, + TableDirection.col, + ); await tester.pumpAndSettle(const Duration(milliseconds: 100)); tableNode = TableNode(node: tableNode.node); @@ -126,9 +129,12 @@ void main() async { await editor.startTesting(); await tester.pumpAndSettle(); - final transaction = editor.editorState.transaction; - TableActions.add(tableNode.node, 2, transaction, TableDirection.row); - await editor.editorState.apply(transaction); + TableActions.add( + tableNode.node, + 2, + editor.editorState, + TableDirection.row, + ); await tester.pumpAndSettle(const Duration(milliseconds: 100)); tableNode = TableNode(node: tableNode.node); From ae60deb4581fe4b2e282808d9de50f8dfedfb659 Mon Sep 17 00:00:00 2001 From: Mohammad Zolfaghari Date: Wed, 30 Aug 2023 19:18:08 +0330 Subject: [PATCH 07/10] fix: table actions keep selection --- .../table_block_component/table_action.dart | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/src/editor/block_component/table_block_component/table_action.dart b/lib/src/editor/block_component/table_block_component/table_action.dart index ac24127c3..f67757459 100644 --- a/lib/src/editor/block_component/table_block_component/table_action.dart +++ b/lib/src/editor/block_component/table_block_component/table_action.dart @@ -113,7 +113,7 @@ void _addCol(Node tableNode, int position, EditorState editorState) { transaction.insertNodes(insertPath, cellNodes); transaction.updateNode(tableNode, {TableBlockKeys.colsLen: colsLen + 1}); - editorState.apply(transaction); + editorState.apply(transaction, withUpdateSelection: false); } void _addRow(Node tableNode, int position, EditorState editorState) async { @@ -150,12 +150,12 @@ void _addRow(Node tableNode, int position, EditorState editorState) async { insertPath, newCellNode(tableNode, node), ); - await editorState.apply(transaction); + await editorState.apply(transaction, withUpdateSelection: false); } final transaction = editorState.transaction; transaction.updateNode(tableNode, {TableBlockKeys.rowsLen: rowsLen + 1}); - await editorState.apply(transaction); + await editorState.apply(transaction, withUpdateSelection: false); } void _deleteCol(Node tableNode, int col, EditorState editorState) { @@ -173,7 +173,7 @@ void _deleteCol(Node tableNode, int col, EditorState editorState) { transaction.updateNode(tableNode, {TableBlockKeys.colsLen: colsLen - 1}); - editorState.apply(transaction); + editorState.apply(transaction, withUpdateSelection: false); } void _deleteRow(Node tableNode, int row, EditorState editorState) { @@ -191,7 +191,7 @@ void _deleteRow(Node tableNode, int row, EditorState editorState) { transaction.updateNode(tableNode, {TableBlockKeys.rowsLen: rowsLen - 1}); - editorState.apply(transaction); + editorState.apply(transaction, withUpdateSelection: false); } void _duplicateCol(Node tableNode, int col, EditorState editorState) { @@ -221,13 +221,13 @@ void _duplicateCol(Node tableNode, int col, EditorState editorState) { transaction.updateNode(tableNode, {TableBlockKeys.colsLen: colsLen + 1}); - editorState.apply(transaction); + editorState.apply(transaction, withUpdateSelection: false); } void _duplicateRow(Node tableNode, int row, EditorState editorState) async { Transaction transaction = editorState.transaction; _updateCellPositions(tableNode, editorState, 0, row + 1, 0, 1); - await editorState.apply(transaction); + await editorState.apply(transaction, withUpdateSelection: false); final int rowsLen = tableNode.attributes[TableBlockKeys.rowsLen], colsLen = tableNode.attributes[TableBlockKeys.colsLen]; @@ -244,12 +244,12 @@ void _duplicateRow(Node tableNode, int row, EditorState editorState) async { }, ), ); - await editorState.apply(transaction); + await editorState.apply(transaction, withUpdateSelection: false); } transaction = editorState.transaction; transaction.updateNode(tableNode, {TableBlockKeys.rowsLen: rowsLen + 1}); - editorState.apply(transaction); + editorState.apply(transaction, withUpdateSelection: false); } void _setColBgColor( @@ -269,7 +269,7 @@ void _setColBgColor( ); } - editorState.apply(transaction); + editorState.apply(transaction, withUpdateSelection: false); } void _setRowBgColor( @@ -289,7 +289,7 @@ void _setRowBgColor( ); } - editorState.apply(transaction); + editorState.apply(transaction, withUpdateSelection: false); } void _clearCol( @@ -308,7 +308,7 @@ void _clearCol( ); } - editorState.apply(transaction); + editorState.apply(transaction, withUpdateSelection: false); } void _clearRow( @@ -327,7 +327,7 @@ void _clearRow( ); } - editorState.apply(transaction); + editorState.apply(transaction, withUpdateSelection: false); } dynamic newCellNode(Node tableNode, n) { @@ -391,5 +391,5 @@ void _updateCellPositions( } } - editorState.apply(transaction); + editorState.apply(transaction, withUpdateSelection: false); } From 4be847e464f0d50bc679765a56ff6e690a35c9ff Mon Sep 17 00:00:00 2001 From: Mohammad Zolfaghari Date: Thu, 31 Aug 2023 01:47:15 +0330 Subject: [PATCH 08/10] fix: on add col|row respect row|col bg color --- .../table_block_component/table_action.dart | 22 ++- .../table_action_menu.dart | 5 +- .../table_block_component.dart | 4 +- .../table_cell_block_component.dart | 11 +- .../table_action_test.dart | 167 ++++++++++++++++++ .../table_view_test.dart | 64 ------- 6 files changed, 201 insertions(+), 72 deletions(-) diff --git a/lib/src/editor/block_component/table_block_component/table_action.dart b/lib/src/editor/block_component/table_block_component/table_action.dart index f67757459..af5d51687 100644 --- a/lib/src/editor/block_component/table_block_component/table_action.dart +++ b/lib/src/editor/block_component/table_block_component/table_action.dart @@ -98,6 +98,15 @@ void _addCol(Node tableNode, int position, EditorState editorState) { }, ); node.insert(paragraphNode()); + final firstCellInRow = getCellNode(tableNode, 0, i); + if (firstCellInRow?.attributes + .containsKey(TableBlockKeys.rowBackgroundColor) ?? + false) { + node.updateAttributes({ + TableBlockKeys.rowBackgroundColor: + firstCellInRow!.attributes[TableBlockKeys.rowBackgroundColor] + }); + } cellNodes.add(newCellNode(tableNode, node)); } @@ -131,6 +140,15 @@ void _addRow(Node tableNode, int position, EditorState editorState) async { }, ); node.insert(paragraphNode()); + final firstCellInCol = getCellNode(tableNode, i, 0); + if (firstCellInCol?.attributes + .containsKey(TableBlockKeys.colBackgroundColor) ?? + false) { + node.updateAttributes({ + TableBlockKeys.colBackgroundColor: + firstCellInCol!.attributes[TableBlockKeys.colBackgroundColor] + }); + } late Path insertPath; if (position == 0) { @@ -265,7 +283,7 @@ void _setColBgColor( final node = getCellNode(tableNode, col, i)!; transaction.updateNode( node, - {TableBlockKeys.backgroundColor: color}, + {TableBlockKeys.colBackgroundColor: color}, ); } @@ -285,7 +303,7 @@ void _setRowBgColor( final node = getCellNode(tableNode, i, row)!; transaction.updateNode( node, - {TableBlockKeys.backgroundColor: color}, + {TableBlockKeys.rowBackgroundColor: color}, ); } diff --git a/lib/src/editor/block_component/table_block_component/table_action_menu.dart b/lib/src/editor/block_component/table_block_component/table_action_menu.dart index b2cb49356..11f21f7a6 100644 --- a/lib/src/editor/block_component/table_block_component/table_action_menu.dart +++ b/lib/src/editor/block_component/table_block_component/table_action_menu.dart @@ -62,6 +62,9 @@ void showActionMenu( final cell = dir == TableDirection.col ? getCellNode(node, position, 0) : getCellNode(node, 0, position); + final key = dir == TableDirection.col + ? TableBlockKeys.colBackgroundColor + : TableBlockKeys.rowBackgroundColor; _showColorMenu( context, @@ -78,7 +81,7 @@ void showActionMenu( bottom: bottom, left: left, selectedColorHex: - cell?.attributes[TableBlockKeys.backgroundColor], + cell?.attributes[key], ); dismissOverlay(); }, diff --git a/lib/src/editor/block_component/table_block_component/table_block_component.dart b/lib/src/editor/block_component/table_block_component/table_block_component.dart index b2966533c..37993076b 100644 --- a/lib/src/editor/block_component/table_block_component/table_block_component.dart +++ b/lib/src/editor/block_component/table_block_component/table_block_component.dart @@ -32,7 +32,9 @@ class TableBlockKeys { static const String colsHeight = 'colsHeight'; - static const String backgroundColor = 'backgroundColor'; + static const String rowBackgroundColor = 'rowBackgroundColor'; + + static const String colBackgroundColor = 'colBackgroundColor'; } class TableDefaults { diff --git a/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart b/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart index f8e9cdda7..5eeeb1659 100644 --- a/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart +++ b/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart @@ -76,10 +76,13 @@ class _TableCeBlockWidgetState extends State { minHeight: context .select((Node n) => n.attributes[TableBlockKeys.height]), ), - color: context.select((Node n) { - return (n.attributes[TableBlockKeys.backgroundColor] as String?) - ?.toColor(); - }), + color: context.select( + (Node n) => + (n.attributes[TableBlockKeys.colBackgroundColor] as String?) + ?.toColor() ?? + (n.attributes[TableBlockKeys.rowBackgroundColor] as String?) + ?.toColor(), + ), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ diff --git a/test/new/block_component/table_block_component/table_action_test.dart b/test/new/block_component/table_block_component/table_action_test.dart index b0f1eda62..6e47f1a06 100644 --- a/test/new/block_component/table_block_component/table_action_test.dart +++ b/test/new/block_component/table_block_component/table_action_test.dart @@ -1,3 +1,4 @@ +import 'package:flutter/material.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/src/editor/block_component/table_block_component/table_node.dart'; import 'package:appflowy_editor/src/editor/block_component/table_block_component/util.dart'; @@ -137,5 +138,171 @@ void main() async { } await editor.dispose(); }); + + testWidgets('add column', (tester) async { + var tableNode = TableNode.fromList([ + ['', ''], + ['', ''] + ]); + final editor = tester.editor..addNode(tableNode.node); + + await editor.startTesting(); + await tester.pumpAndSettle(); + + TableActions.add( + tableNode.node, + 2, + editor.editorState, + TableDirection.col, + ); + await tester.pumpAndSettle(const Duration(milliseconds: 100)); + tableNode = TableNode(node: tableNode.node); + + expect(tableNode.colsLen, 3); + expect( + tableNode.getCell(2, 1).children.first.toJson(), + { + "type": "paragraph", + "data": {"delta": []} + }, + ); + expect(tableNode.getColWidth(2), tableNode.config.colDefaultWidth); + await editor.dispose(); + }); + + testWidgets('add row', (tester) async { + var tableNode = TableNode.fromList([ + ['', ''], + ['', ''] + ]); + final editor = tester.editor..addNode(tableNode.node); + + await editor.startTesting(); + await tester.pumpAndSettle(); + + TableActions.add( + tableNode.node, + 2, + editor.editorState, + TableDirection.row, + ); + await tester.pumpAndSettle(const Duration(milliseconds: 100)); + tableNode = TableNode(node: tableNode.node); + + expect(tableNode.rowsLen, 3); + expect( + tableNode.getCell(0, 2).children.first.toJson(), + { + "type": "paragraph", + "data": {"delta": []} + }, + ); + + var cell12 = getCellNode(tableNode.node, 1, 2)!; + expect(tableNode.getRowHeight(2), cell12.children.first.rect.height + 8); + await editor.dispose(); + }); + + testWidgets('set row bg color', (tester) async { + var tableNode = TableNode.fromList([ + ['', ''], + ['', ''] + ]); + final editor = tester.editor..addNode(tableNode.node); + + await editor.startTesting(); + await tester.pumpAndSettle(); + + final color = Colors.green.toHex(); + TableActions.setBgColor( + tableNode.node, + 0, + editor.editorState, + color, + TableDirection.row, + ); + await tester.pumpAndSettle(const Duration(milliseconds: 100)); + + for (var i = 0; i < 2; i++) { + expect( + tableNode.getCell(i, 0).attributes[TableBlockKeys.rowBackgroundColor], + color, + ); + } + await editor.dispose(); + }); + + testWidgets('add column respect row bg color', (tester) async { + var tableNode = TableNode.fromList([ + ['', ''], + ['', ''] + ]); + final editor = tester.editor..addNode(tableNode.node); + + await editor.startTesting(); + await tester.pumpAndSettle(); + + final color = Colors.green.toHex(); + TableActions.setBgColor( + tableNode.node, + 0, + editor.editorState, + color, + TableDirection.row, + ); + await tester.pumpAndSettle(const Duration(milliseconds: 100)); + + TableActions.add( + tableNode.node, + 2, + editor.editorState, + TableDirection.col, + ); + await tester.pumpAndSettle(const Duration(milliseconds: 100)); + tableNode = TableNode(node: tableNode.node); + + expect(tableNode.colsLen, 3); + expect( + tableNode.getCell(2, 0).attributes[TableBlockKeys.rowBackgroundColor], + color, + ); + await editor.dispose(); + }); + + testWidgets('add row respect column bg color', (tester) async { + var tableNode = TableNode.fromList([ + ['', ''], + ['', ''] + ]); + final editor = tester.editor..addNode(tableNode.node); + + await editor.startTesting(); + await tester.pumpAndSettle(); + + final color = Colors.green.toHex(); + TableActions.setBgColor( + tableNode.node, + 0, + editor.editorState, + color, + TableDirection.col, + ); + await tester.pumpAndSettle(const Duration(milliseconds: 100)); + + TableActions.add( + tableNode.node, + 2, + editor.editorState, + TableDirection.row, + ); + await tester.pumpAndSettle(const Duration(milliseconds: 1000)); + tableNode = TableNode(node: tableNode.node); + + expect(tableNode.rowsLen, 3); + expect( + tableNode.getCell(0, 2).attributes[TableBlockKeys.colBackgroundColor], + color, + ); + }); }); } diff --git a/test/new/block_component/table_block_component/table_view_test.dart b/test/new/block_component/table_block_component/table_view_test.dart index 77a43a04f..da549185b 100644 --- a/test/new/block_component/table_block_component/table_view_test.dart +++ b/test/new/block_component/table_block_component/table_view_test.dart @@ -87,69 +87,5 @@ void main() async { expect(tableNode.getRowHeight(0), row0beforeHeight); await editor.dispose(); }); - - testWidgets('add column', (tester) async { - var tableNode = TableNode.fromList([ - ['', ''], - ['', ''] - ]); - final editor = tester.editor..addNode(tableNode.node); - - await editor.startTesting(); - await tester.pumpAndSettle(); - - TableActions.add( - tableNode.node, - 2, - editor.editorState, - TableDirection.col, - ); - await tester.pumpAndSettle(const Duration(milliseconds: 100)); - tableNode = TableNode(node: tableNode.node); - - expect(tableNode.colsLen, 3); - expect( - tableNode.getCell(2, 1).children.first.toJson(), - { - "type": "paragraph", - "data": {"delta": []} - }, - ); - expect(tableNode.getColWidth(2), tableNode.config.colDefaultWidth); - await editor.dispose(); - }); - - testWidgets('add row', (tester) async { - var tableNode = TableNode.fromList([ - ['', ''], - ['', ''] - ]); - final editor = tester.editor..addNode(tableNode.node); - - await editor.startTesting(); - await tester.pumpAndSettle(); - - TableActions.add( - tableNode.node, - 2, - editor.editorState, - TableDirection.row, - ); - await tester.pumpAndSettle(const Duration(milliseconds: 100)); - tableNode = TableNode(node: tableNode.node); - - expect(tableNode.rowsLen, 3); - expect( - tableNode.getCell(0, 2).children.first.toJson(), - { - "type": "paragraph", - "data": {"delta": []} - }, - ); - - var cell12 = getCellNode(tableNode.node, 1, 2)!; - expect(tableNode.getRowHeight(2), cell12.children.first.rect.height + 8); - await editor.dispose(); - }); }); } From 493ec1f75fd4bf7e2fdb2fa7a612e18d185ccc26 Mon Sep 17 00:00:00 2001 From: Mohammad Zolfaghari Date: Thu, 31 Aug 2023 01:59:46 +0330 Subject: [PATCH 09/10] refactor: moving cell related keys to TableCellBlockKeys --- .../table_block_component/table_action.dart | 56 +++++++++---------- .../table_action_menu.dart | 4 +- .../table_block_component.dart | 12 ---- .../table_cell_block_component.dart | 30 +++++++--- .../table_block_component/table_col.dart | 2 +- .../table_block_component/table_commands.dart | 4 +- .../table_block_component/table_node.dart | 26 ++++----- .../table_block_component/util.dart | 4 +- .../table_action_test.dart | 6 +- .../table_node_test.dart | 32 +++++------ 10 files changed, 88 insertions(+), 88 deletions(-) diff --git a/lib/src/editor/block_component/table_block_component/table_action.dart b/lib/src/editor/block_component/table_block_component/table_action.dart index af5d51687..25213ad17 100644 --- a/lib/src/editor/block_component/table_block_component/table_action.dart +++ b/lib/src/editor/block_component/table_block_component/table_action.dart @@ -84,7 +84,7 @@ void _addCol(Node tableNode, int position, EditorState editorState) { for (var i = position; i < colsLen; i++) { for (var j = 0; j < rowsLen; j++) { final node = getCellNode(tableNode, i, j)!; - transaction.updateNode(node, {TableBlockKeys.colPosition: i + 1}); + transaction.updateNode(node, {TableCellBlockKeys.colPosition: i + 1}); } } } @@ -93,18 +93,18 @@ void _addCol(Node tableNode, int position, EditorState editorState) { final node = Node( type: TableCellBlockKeys.type, attributes: { - TableBlockKeys.colPosition: position, - TableBlockKeys.rowPosition: i, + TableCellBlockKeys.colPosition: position, + TableCellBlockKeys.rowPosition: i, }, ); node.insert(paragraphNode()); final firstCellInRow = getCellNode(tableNode, 0, i); if (firstCellInRow?.attributes - .containsKey(TableBlockKeys.rowBackgroundColor) ?? + .containsKey(TableCellBlockKeys.rowBackgroundColor) ?? false) { node.updateAttributes({ - TableBlockKeys.rowBackgroundColor: - firstCellInRow!.attributes[TableBlockKeys.rowBackgroundColor] + TableCellBlockKeys.rowBackgroundColor: + firstCellInRow!.attributes[TableCellBlockKeys.rowBackgroundColor] }); } @@ -135,18 +135,18 @@ void _addRow(Node tableNode, int position, EditorState editorState) async { final node = Node( type: TableCellBlockKeys.type, attributes: { - TableBlockKeys.colPosition: i, - TableBlockKeys.rowPosition: position, + TableCellBlockKeys.colPosition: i, + TableCellBlockKeys.rowPosition: position, }, ); node.insert(paragraphNode()); final firstCellInCol = getCellNode(tableNode, i, 0); if (firstCellInCol?.attributes - .containsKey(TableBlockKeys.colBackgroundColor) ?? + .containsKey(TableCellBlockKeys.colBackgroundColor) ?? false) { node.updateAttributes({ - TableBlockKeys.colBackgroundColor: - firstCellInCol!.attributes[TableBlockKeys.colBackgroundColor] + TableCellBlockKeys.colBackgroundColor: + firstCellInCol!.attributes[TableCellBlockKeys.colBackgroundColor] }); } @@ -161,7 +161,7 @@ void _addRow(Node tableNode, int position, EditorState editorState) async { if (position != rowsLen) { for (var j = position; j < rowsLen; j++) { final node = getCellNode(tableNode, i, j)!; - transaction.updateNode(node, {TableBlockKeys.rowPosition: j + 1}); + transaction.updateNode(node, {TableCellBlockKeys.rowPosition: j + 1}); } } transaction.insertNode( @@ -224,8 +224,8 @@ void _duplicateCol(Node tableNode, int col, EditorState editorState) { node.copyWith( attributes: { ...node.attributes, - TableBlockKeys.colPosition: col + 1, - TableBlockKeys.rowPosition: i, + TableCellBlockKeys.colPosition: col + 1, + TableCellBlockKeys.rowPosition: i, }, ), ); @@ -257,8 +257,8 @@ void _duplicateRow(Node tableNode, int row, EditorState editorState) async { node.copyWith( attributes: { ...node.attributes, - TableBlockKeys.rowPosition: row + 1, - TableBlockKeys.colPosition: i, + TableCellBlockKeys.rowPosition: row + 1, + TableCellBlockKeys.colPosition: i, }, ), ); @@ -283,7 +283,7 @@ void _setColBgColor( final node = getCellNode(tableNode, col, i)!; transaction.updateNode( node, - {TableBlockKeys.colBackgroundColor: color}, + {TableCellBlockKeys.colBackgroundColor: color}, ); } @@ -303,7 +303,7 @@ void _setRowBgColor( final node = getCellNode(tableNode, i, row)!; transaction.updateNode( node, - {TableBlockKeys.rowBackgroundColor: color}, + {TableCellBlockKeys.rowBackgroundColor: color}, ); } @@ -349,39 +349,39 @@ void _clearRow( } dynamic newCellNode(Node tableNode, n) { - final row = n.attributes[TableBlockKeys.rowPosition] as int; - final col = n.attributes[TableBlockKeys.colPosition] as int; + final row = n.attributes[TableCellBlockKeys.rowPosition] as int; + final col = n.attributes[TableCellBlockKeys.colPosition] as int; final int rowsLen = tableNode.attributes[TableBlockKeys.rowsLen]; final int colsLen = tableNode.attributes[TableBlockKeys.colsLen]; - if (!n.attributes.containsKey(TableBlockKeys.height)) { + if (!n.attributes.containsKey(TableCellBlockKeys.height)) { double nodeHeight = double.tryParse( tableNode.attributes[TableBlockKeys.rowDefaultHeight].toString(), )!; if (row < rowsLen) { nodeHeight = double.tryParse( getCellNode(tableNode, 0, row)! - .attributes[TableBlockKeys.height] + .attributes[TableCellBlockKeys.height] .toString(), ) ?? nodeHeight; } - n.updateAttributes({TableBlockKeys.height: nodeHeight}); + n.updateAttributes({TableCellBlockKeys.height: nodeHeight}); } - if (!n.attributes.containsKey(TableBlockKeys.width)) { + if (!n.attributes.containsKey(TableCellBlockKeys.width)) { double nodeWidth = double.tryParse( tableNode.attributes[TableBlockKeys.colDefaultWidth].toString(), )!; if (col < colsLen) { nodeWidth = double.tryParse( getCellNode(tableNode, col, 0)! - .attributes[TableBlockKeys.width] + .attributes[TableCellBlockKeys.width] .toString(), ) ?? nodeWidth; } - n.updateAttributes({TableBlockKeys.width: nodeWidth}); + n.updateAttributes({TableCellBlockKeys.width: nodeWidth}); } return n; @@ -403,8 +403,8 @@ void _updateCellPositions( for (var i = fromCol; i < colsLen; i++) { for (var j = fromRow; j < rowsLen; j++) { transaction.updateNode(getCellNode(tableNode, i, j)!, { - TableBlockKeys.colPosition: i + addToCol, - TableBlockKeys.rowPosition: j + addToRow, + TableCellBlockKeys.colPosition: i + addToCol, + TableCellBlockKeys.rowPosition: j + addToRow, }); } } diff --git a/lib/src/editor/block_component/table_block_component/table_action_menu.dart b/lib/src/editor/block_component/table_block_component/table_action_menu.dart index 11f21f7a6..01663c2f7 100644 --- a/lib/src/editor/block_component/table_block_component/table_action_menu.dart +++ b/lib/src/editor/block_component/table_block_component/table_action_menu.dart @@ -63,8 +63,8 @@ void showActionMenu( ? getCellNode(node, position, 0) : getCellNode(node, 0, position); final key = dir == TableDirection.col - ? TableBlockKeys.colBackgroundColor - : TableBlockKeys.rowBackgroundColor; + ? TableCellBlockKeys.colBackgroundColor + : TableCellBlockKeys.rowBackgroundColor; _showColorMenu( context, diff --git a/lib/src/editor/block_component/table_block_component/table_block_component.dart b/lib/src/editor/block_component/table_block_component/table_block_component.dart index 37993076b..7b9480d36 100644 --- a/lib/src/editor/block_component/table_block_component/table_block_component.dart +++ b/lib/src/editor/block_component/table_block_component/table_block_component.dart @@ -22,19 +22,7 @@ class TableBlockKeys { static const String rowsLen = 'rowsLen'; - static const String rowPosition = 'rowPosition'; - - static const String colPosition = 'colPosition'; - - static const String height = 'height'; - - static const String width = 'width'; - static const String colsHeight = 'colsHeight'; - - static const String rowBackgroundColor = 'rowBackgroundColor'; - - static const String colBackgroundColor = 'colBackgroundColor'; } class TableDefaults { diff --git a/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart b/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart index 5eeeb1659..5e56f3bdc 100644 --- a/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart +++ b/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart @@ -8,6 +8,18 @@ class TableCellBlockKeys { const TableCellBlockKeys._(); static const String type = 'table/cell'; + + static const String rowPosition = 'rowPosition'; + + static const String colPosition = 'colPosition'; + + static const String height = 'height'; + + static const String width = 'width'; + + static const String rowBackgroundColor = 'rowBackgroundColor'; + + static const String colBackgroundColor = 'colBackgroundColor'; } class TableCellBlockComponentBuilder extends BlockComponentBuilder { @@ -40,8 +52,8 @@ class TableCellBlockComponentBuilder extends BlockComponentBuilder { @override bool validate(Node node) => node.attributes.isNotEmpty && - node.attributes.containsKey(TableBlockKeys.rowPosition) && - node.attributes.containsKey(TableBlockKeys.colPosition); + node.attributes.containsKey(TableCellBlockKeys.rowPosition) && + node.attributes.containsKey(TableCellBlockKeys.colPosition); } class TableCelBlockWidget extends BlockComponentStatefulWidget { @@ -74,13 +86,13 @@ class _TableCeBlockWidgetState extends State { child: Container( constraints: BoxConstraints( minHeight: context - .select((Node n) => n.attributes[TableBlockKeys.height]), + .select((Node n) => n.attributes[TableCellBlockKeys.height]), ), color: context.select( (Node n) => - (n.attributes[TableBlockKeys.colBackgroundColor] as String?) + (n.attributes[TableCellBlockKeys.colBackgroundColor] as String?) ?.toColor() ?? - (n.attributes[TableBlockKeys.rowBackgroundColor] as String?) + (n.attributes[TableCellBlockKeys.rowBackgroundColor] as String?) ?.toColor(), ), child: Column( @@ -101,13 +113,13 @@ class _TableCeBlockWidgetState extends State { visible: _rowActionVisibility, node: widget.node.parent!, editorState: editorState, - position: widget.node.attributes[TableBlockKeys.rowPosition], + position: widget.node.attributes[TableCellBlockKeys.rowPosition], transform: context.select((Node n) { - final int col = n.attributes[TableBlockKeys.colPosition]; + final int col = n.attributes[TableCellBlockKeys.colPosition]; double left = -15.0; for (var i = 0; i < col; i++) { left -= getCellNode(n.parent!, i, 0) - ?.attributes[TableBlockKeys.width] as double; + ?.attributes[TableCellBlockKeys.width] as double; left -= n.parent!.attributes['borderWidth'] ?? TableDefaults.borderWidth; } @@ -116,7 +128,7 @@ class _TableCeBlockWidgetState extends State { }), alignment: Alignment.centerLeft, height: - context.select((Node n) => n.attributes[TableBlockKeys.height]), + context.select((Node n) => n.attributes[TableCellBlockKeys.height]), menuBuilder: widget.menuBuilder, dir: TableDirection.row, ), diff --git a/lib/src/editor/block_component/table_block_component/table_col.dart b/lib/src/editor/block_component/table_block_component/table_col.dart index 90d12b119..9122a501c 100644 --- a/lib/src/editor/block_component/table_block_component/table_col.dart +++ b/lib/src/editor/block_component/table_block_component/table_col.dart @@ -53,7 +53,7 @@ class _TableColState extends State { SizedBox( width: context.select( (Node n) => getCellNode(n, widget.colIdx, 0) - ?.attributes[TableBlockKeys.width], + ?.attributes[TableCellBlockKeys.width], ), child: Stack( children: [ diff --git a/lib/src/editor/block_component/table_block_component/table_commands.dart b/lib/src/editor/block_component/table_block_component/table_commands.dart index 276d99197..358037a98 100644 --- a/lib/src/editor/block_component/table_block_component/table_commands.dart +++ b/lib/src/editor/block_component/table_block_component/table_commands.dart @@ -189,8 +189,8 @@ bool _hasSelectionAndTableCell( Node? _getNextNode(Iterable nodes, int colDiff, rowDiff) { final cell = nodes.first.parent!; - final col = cell.attributes[TableBlockKeys.colPosition]; - final row = cell.attributes[TableBlockKeys.rowPosition]; + final col = cell.attributes[TableCellBlockKeys.colPosition]; + final row = cell.attributes[TableCellBlockKeys.rowPosition]; return cell.parent != null ? getCellNode(cell.parent!, col + colDiff, row + rowDiff) : null; diff --git a/lib/src/editor/block_component/table_block_component/table_node.dart b/lib/src/editor/block_component/table_block_component/table_node.dart index fe76ba859..a280c0338 100644 --- a/lib/src/editor/block_component/table_block_component/table_node.dart +++ b/lib/src/editor/block_component/table_block_component/table_node.dart @@ -28,15 +28,15 @@ class TableNode { assert( node.children.every( (n) => - n.attributes.containsKey(TableBlockKeys.rowPosition) && - n.attributes.containsKey(TableBlockKeys.colPosition), + n.attributes.containsKey(TableCellBlockKeys.rowPosition) && + n.attributes.containsKey(TableCellBlockKeys.colPosition), ), ); assert( node.children.every( (n) => - n.attributes.containsKey(TableBlockKeys.rowPosition) && - n.attributes.containsKey(TableBlockKeys.colPosition), + n.attributes.containsKey(TableCellBlockKeys.rowPosition) && + n.attributes.containsKey(TableCellBlockKeys.colPosition), ), ); @@ -45,8 +45,8 @@ class TableNode { for (var j = 0; j < rowsCount; j++) { final cell = node.children.where( (n) => - n.attributes[TableBlockKeys.colPosition] == i && - n.attributes[TableBlockKeys.rowPosition] == j, + n.attributes[TableCellBlockKeys.colPosition] == i && + n.attributes[TableCellBlockKeys.rowPosition] == j, ); assert(cell.length == 1); _cells[i].add(newCellNode(node, cell.first)); @@ -86,8 +86,8 @@ class TableNode { final cell = Node( type: TableCellBlockKeys.type, attributes: { - TableBlockKeys.colPosition: i, - TableBlockKeys.rowPosition: j + TableCellBlockKeys.colPosition: i, + TableCellBlockKeys.rowPosition: j }, ); @@ -118,7 +118,7 @@ class TableNode { double getRowHeight(int row) => double.tryParse( - _cells[0][row].attributes[TableBlockKeys.height].toString(), + _cells[0][row].attributes[TableCellBlockKeys.height].toString(), ) ?? _config.rowDefaultHeight; @@ -131,7 +131,7 @@ class TableNode { double getColWidth(int col) => double.tryParse( - _cells[col][0].attributes[TableBlockKeys.width].toString(), + _cells[col][0].attributes[TableCellBlockKeys.width].toString(), ) ?? _config.colDefaultWidth; @@ -146,7 +146,7 @@ class TableNode { w = w < _config.colMinimumWidth ? _config.colMinimumWidth : w; if (getColWidth(col) != w) { for (var i = 0; i < rowsLen; i++) { - transaction.updateNode(_cells[col][i], {TableBlockKeys.width: w}); + transaction.updateNode(_cells[col][i], {TableCellBlockKeys.width: w}); updateRowHeight(i, transaction); } transaction.updateNode(node, node.attributes); @@ -159,11 +159,11 @@ class TableNode { .map((c) => c[row].children.first.rect.height + 8) .reduce(max); - if (_cells[0][row].attributes[TableBlockKeys.height] != maxHeight) { + if (_cells[0][row].attributes[TableCellBlockKeys.height] != maxHeight) { for (var i = 0; i < colsLen; i++) { transaction.updateNode( _cells[i][row], - {TableBlockKeys.height: maxHeight}, + {TableCellBlockKeys.height: maxHeight}, ); } } diff --git a/lib/src/editor/block_component/table_block_component/util.dart b/lib/src/editor/block_component/table_block_component/util.dart index 7a9d4c1ca..7ade0b580 100644 --- a/lib/src/editor/block_component/table_block_component/util.dart +++ b/lib/src/editor/block_component/table_block_component/util.dart @@ -4,7 +4,7 @@ import 'package:collection/collection.dart'; Node? getCellNode(Node tableNode, int col, int row) { return tableNode.children.firstWhereOrNull( (n) => - n.attributes[TableBlockKeys.colPosition] == col && - n.attributes[TableBlockKeys.rowPosition] == row, + n.attributes[TableCellBlockKeys.colPosition] == col && + n.attributes[TableCellBlockKeys.rowPosition] == row, ); } diff --git a/test/new/block_component/table_block_component/table_action_test.dart b/test/new/block_component/table_block_component/table_action_test.dart index 6e47f1a06..5cbcf27c0 100644 --- a/test/new/block_component/table_block_component/table_action_test.dart +++ b/test/new/block_component/table_block_component/table_action_test.dart @@ -225,7 +225,7 @@ void main() async { for (var i = 0; i < 2; i++) { expect( - tableNode.getCell(i, 0).attributes[TableBlockKeys.rowBackgroundColor], + tableNode.getCell(i, 0).attributes[TableCellBlockKeys.rowBackgroundColor], color, ); } @@ -263,7 +263,7 @@ void main() async { expect(tableNode.colsLen, 3); expect( - tableNode.getCell(2, 0).attributes[TableBlockKeys.rowBackgroundColor], + tableNode.getCell(2, 0).attributes[TableCellBlockKeys.rowBackgroundColor], color, ); await editor.dispose(); @@ -300,7 +300,7 @@ void main() async { expect(tableNode.rowsLen, 3); expect( - tableNode.getCell(0, 2).attributes[TableBlockKeys.colBackgroundColor], + tableNode.getCell(0, 2).attributes[TableCellBlockKeys.colBackgroundColor], color, ); }); diff --git a/test/new/block_component/table_block_component/table_node_test.dart b/test/new/block_component/table_block_component/table_node_test.dart index 56ddefeaf..69132cee3 100644 --- a/test/new/block_component/table_block_component/table_node_test.dart +++ b/test/new/block_component/table_block_component/table_node_test.dart @@ -20,9 +20,9 @@ void main() { { 'type': TableCellBlockKeys.type, 'data': { - TableBlockKeys.colPosition: 0, - TableBlockKeys.rowPosition: 0, - TableBlockKeys.width: 35, + TableCellBlockKeys.colPosition: 0, + TableCellBlockKeys.rowPosition: 0, + TableCellBlockKeys.width: 35, }, 'children': [ { @@ -39,8 +39,8 @@ void main() { { 'type': TableCellBlockKeys.type, 'data': { - TableBlockKeys.colPosition: 0, - TableBlockKeys.rowPosition: 1, + TableCellBlockKeys.colPosition: 0, + TableCellBlockKeys.rowPosition: 1, }, 'children': [ { @@ -59,8 +59,8 @@ void main() { { 'type': TableCellBlockKeys.type, 'data': { - TableBlockKeys.colPosition: 1, - TableBlockKeys.rowPosition: 0, + TableCellBlockKeys.colPosition: 1, + TableCellBlockKeys.rowPosition: 0, }, 'children': [ { @@ -79,8 +79,8 @@ void main() { { 'type': TableCellBlockKeys.type, 'data': { - TableBlockKeys.colPosition: 1, - TableBlockKeys.rowPosition: 1, + TableCellBlockKeys.colPosition: 1, + TableCellBlockKeys.rowPosition: 1, }, 'children': [ { @@ -160,9 +160,9 @@ void main() { { 'type': TableCellBlockKeys.type, 'data': { - TableBlockKeys.colPosition: 0, - TableBlockKeys.rowPosition: 0, - TableBlockKeys.width: 35, + TableCellBlockKeys.colPosition: 0, + TableCellBlockKeys.rowPosition: 0, + TableCellBlockKeys.width: 35, }, 'children': [ { @@ -179,8 +179,8 @@ void main() { { 'type': TableCellBlockKeys.type, 'data': { - TableBlockKeys.colPosition: 1, - TableBlockKeys.rowPosition: 0, + TableCellBlockKeys.colPosition: 1, + TableCellBlockKeys.rowPosition: 0, }, 'children': [ { @@ -199,8 +199,8 @@ void main() { { 'type': TableCellBlockKeys.type, 'data': { - TableBlockKeys.colPosition: 1, - TableBlockKeys.rowPosition: 1, + TableCellBlockKeys.colPosition: 1, + TableCellBlockKeys.rowPosition: 1, }, 'children': [ { From 7cbf5ff123d821374b67a4e2215441155e438e41 Mon Sep 17 00:00:00 2001 From: Mohammad Zolfaghari Date: Thu, 31 Aug 2023 02:07:06 +0330 Subject: [PATCH 10/10] style: dart format --- .../table_block_component/table_action_menu.dart | 3 +-- .../table_cell_block_component.dart | 10 ++++++---- .../table_block_component/table_action_test.dart | 12 +++++++++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/src/editor/block_component/table_block_component/table_action_menu.dart b/lib/src/editor/block_component/table_block_component/table_action_menu.dart index 01663c2f7..1d42c608f 100644 --- a/lib/src/editor/block_component/table_block_component/table_action_menu.dart +++ b/lib/src/editor/block_component/table_block_component/table_action_menu.dart @@ -80,8 +80,7 @@ void showActionMenu( top: top, bottom: bottom, left: left, - selectedColorHex: - cell?.attributes[key], + selectedColorHex: cell?.attributes[key], ); dismissOverlay(); }, diff --git a/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart b/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart index 5e56f3bdc..4de9a0721 100644 --- a/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart +++ b/lib/src/editor/block_component/table_block_component/table_cell_block_component.dart @@ -90,9 +90,11 @@ class _TableCeBlockWidgetState extends State { ), color: context.select( (Node n) => - (n.attributes[TableCellBlockKeys.colBackgroundColor] as String?) + (n.attributes[TableCellBlockKeys.colBackgroundColor] + as String?) ?.toColor() ?? - (n.attributes[TableCellBlockKeys.rowBackgroundColor] as String?) + (n.attributes[TableCellBlockKeys.rowBackgroundColor] + as String?) ?.toColor(), ), child: Column( @@ -127,8 +129,8 @@ class _TableCeBlockWidgetState extends State { return Matrix4.translationValues(left, 0.0, 0.0); }), alignment: Alignment.centerLeft, - height: - context.select((Node n) => n.attributes[TableCellBlockKeys.height]), + height: context + .select((Node n) => n.attributes[TableCellBlockKeys.height]), menuBuilder: widget.menuBuilder, dir: TableDirection.row, ), diff --git a/test/new/block_component/table_block_component/table_action_test.dart b/test/new/block_component/table_block_component/table_action_test.dart index 5cbcf27c0..91a2220df 100644 --- a/test/new/block_component/table_block_component/table_action_test.dart +++ b/test/new/block_component/table_block_component/table_action_test.dart @@ -225,7 +225,9 @@ void main() async { for (var i = 0; i < 2; i++) { expect( - tableNode.getCell(i, 0).attributes[TableCellBlockKeys.rowBackgroundColor], + tableNode + .getCell(i, 0) + .attributes[TableCellBlockKeys.rowBackgroundColor], color, ); } @@ -263,7 +265,9 @@ void main() async { expect(tableNode.colsLen, 3); expect( - tableNode.getCell(2, 0).attributes[TableCellBlockKeys.rowBackgroundColor], + tableNode + .getCell(2, 0) + .attributes[TableCellBlockKeys.rowBackgroundColor], color, ); await editor.dispose(); @@ -300,7 +304,9 @@ void main() async { expect(tableNode.rowsLen, 3); expect( - tableNode.getCell(0, 2).attributes[TableCellBlockKeys.colBackgroundColor], + tableNode + .getCell(0, 2) + .attributes[TableCellBlockKeys.colBackgroundColor], color, ); });