Skip to content

Commit b429ae6

Browse files
authored
fix: better column resize performance (#434)
* fix: table padding when it gets scrolled The handler was getting out of region when table width was wider than editor size. * fix: modify scroll view padding * fix: better column resize performance
1 parent 9143c1f commit b429ae6

File tree

4 files changed

+70
-38
lines changed

4 files changed

+70
-38
lines changed

lib/src/editor/block_component/table_block_component/table_col.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class TableCol extends StatefulWidget {
3333
class _TableColState extends State<TableCol> {
3434
bool _colActionVisiblity = false;
3535

36+
Map<String, void Function()> listeners = {};
37+
3638
@override
3739
Widget build(BuildContext context) {
3840
List<Widget> children = [];
@@ -98,10 +100,9 @@ class _TableColState extends State<TableCol> {
98100

99101
for (var i = 0; i < rowsLen; i++) {
100102
final node = widget.tableNode.getCell(widget.colIdx, i);
101-
102103
updateRowHeightCallback(i);
103-
node.addListener(() => updateRowHeightCallback(i));
104-
node.children.first.addListener(() => updateRowHeightCallback(i));
104+
addListener(node, i);
105+
addListener(node.children.first, i);
105106

106107
cells.addAll([
107108
widget.editorState.renderer.build(
@@ -118,14 +119,23 @@ class _TableColState extends State<TableCol> {
118119
];
119120
}
120121

122+
void addListener(Node node, int row) {
123+
if (listeners.containsKey(node.id)) {
124+
return;
125+
}
126+
127+
listeners[node.id] = () => updateRowHeightCallback(row);
128+
node.addListener(listeners[node.id]!);
129+
}
130+
121131
void updateRowHeightCallback(int row) =>
122132
WidgetsBinding.instance.addPostFrameCallback((_) {
123133
if (row >= widget.tableNode.rowsLen) {
124134
return;
125135
}
126136

127137
final transaction = widget.editorState.transaction;
128-
widget.tableNode.updateRowHeight(row, transaction);
138+
widget.tableNode.updateRowHeight(row, transaction: transaction);
129139
if (transaction.operations.isNotEmpty) {
130140
transaction.afterSelection = transaction.beforeSelection;
131141
widget.editorState.apply(transaction);

lib/src/editor/block_component/table_block_component/table_col_border.dart

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class _TableColBorderState extends State<TableColBorder> {
3131
bool _borderHovering = false;
3232
bool _borderDragging = false;
3333

34+
Offset initialOffset = const Offset(0, 0);
35+
3436
@override
3537
Widget build(BuildContext context) {
3638
return widget.resizable
@@ -44,31 +46,26 @@ class _TableColBorderState extends State<TableColBorder> {
4446
onEnter: (_) => setState(() => _borderHovering = true),
4547
onExit: (_) => setState(() => _borderHovering = false),
4648
child: GestureDetector(
47-
onHorizontalDragStart: (_) => setState(() => _borderDragging = true),
48-
onHorizontalDragEnd: (_) => setState(() => _borderDragging = false),
49-
onHorizontalDragUpdate: (DragUpdateDetails details) async {
50-
final RenderBox box =
51-
_borderKey.currentContext?.findRenderObject() as RenderBox;
52-
final Offset pos = box.localToGlobal(Offset.zero);
53-
final double colsHeight = widget.tableNode.colsHeight;
54-
final int direction = details.delta.dx > 0 ? 1 : -1;
55-
if ((details.globalPosition.dx - pos.dx - (direction * 90)).abs() >
56-
110 ||
57-
(details.globalPosition.dy - pos.dy) > colsHeight + 50 ||
58-
(details.globalPosition.dy - pos.dy) < -50) {
59-
return;
60-
}
61-
62-
final colWidth = widget.tableNode.getColWidth(widget.colIdx);
63-
49+
onHorizontalDragStart: (DragStartDetails details) {
50+
setState(() => _borderDragging = true);
51+
initialOffset = details.globalPosition;
52+
},
53+
onHorizontalDragEnd: (_) {
6454
final transaction = widget.editorState.transaction;
6555
widget.tableNode.setColWidth(
6656
widget.colIdx,
67-
colWidth + details.delta.dx,
68-
transaction,
57+
widget.tableNode.getColWidth(widget.colIdx),
58+
transaction: transaction,
59+
force: true,
6960
);
7061
transaction.afterSelection = transaction.beforeSelection;
71-
await widget.editorState.apply(transaction);
62+
widget.editorState.apply(transaction);
63+
setState(() => _borderDragging = false);
64+
},
65+
onHorizontalDragUpdate: (DragUpdateDetails details) {
66+
final colWidth = widget.tableNode.getColWidth(widget.colIdx);
67+
widget.tableNode
68+
.setColWidth(widget.colIdx, colWidth + details.delta.dx);
7269
},
7370
child: Container(
7471
key: _borderKey,

lib/src/editor/block_component/table_block_component/table_node.dart

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,34 +142,59 @@ class TableNode {
142142
) +
143143
_config.borderWidth;
144144

145-
void setColWidth(int col, double w, Transaction transaction) {
145+
void setColWidth(
146+
int col,
147+
double w, {
148+
Transaction? transaction,
149+
bool force = false,
150+
}) {
146151
w = w < _config.colMinimumWidth ? _config.colMinimumWidth : w;
147-
if (getColWidth(col) != w) {
152+
if (getColWidth(col) != w || force) {
148153
for (var i = 0; i < rowsLen; i++) {
149-
transaction.updateNode(_cells[col][i], {TableCellBlockKeys.width: w});
150-
updateRowHeight(i, transaction);
154+
if (transaction != null) {
155+
transaction.updateNode(_cells[col][i], {TableCellBlockKeys.width: w});
156+
} else {
157+
_cells[col][i].updateAttributes({TableCellBlockKeys.width: w});
158+
}
159+
updateRowHeight(i, transaction: transaction);
160+
}
161+
if (transaction != null) {
162+
transaction.updateNode(node, node.attributes);
163+
} else {
164+
node.updateAttributes(node.attributes);
151165
}
152-
transaction.updateNode(node, node.attributes);
153166
}
154167
}
155168

156-
void updateRowHeight(int row, Transaction transaction) {
169+
void updateRowHeight(
170+
int row, {
171+
Transaction? transaction,
172+
}) {
157173
// The extra 8 is because of paragraph padding
158174
double maxHeight = _cells
159175
.map<double>((c) => c[row].children.first.rect.height + 8)
160176
.reduce(max);
161177

162178
if (_cells[0][row].attributes[TableCellBlockKeys.height] != maxHeight) {
163179
for (var i = 0; i < colsLen; i++) {
164-
transaction.updateNode(
165-
_cells[i][row],
166-
{TableCellBlockKeys.height: maxHeight},
167-
);
180+
if (transaction != null) {
181+
transaction.updateNode(
182+
_cells[i][row],
183+
{TableCellBlockKeys.height: maxHeight},
184+
);
185+
} else {
186+
_cells[i][row]
187+
.updateAttributes({TableCellBlockKeys.height: maxHeight});
188+
}
168189
}
169190
}
170191

171192
if (node.attributes[TableBlockKeys.colsHeight] != colsHeight) {
172-
transaction.updateNode(node, {TableBlockKeys.colsHeight: colsHeight});
193+
if (transaction != null) {
194+
transaction.updateNode(node, {TableBlockKeys.colsHeight: colsHeight});
195+
} else {
196+
node.updateAttributes({TableBlockKeys.colsHeight: colsHeight});
197+
}
173198
}
174199
}
175200
}

test/new/block_component/table_block_component/table_view_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void main() async {
3838
await editor.ime.insertText('aaaaaaaaa');
3939

4040
final transaction = editor.editorState.transaction;
41-
tableNode.updateRowHeight(0, transaction);
41+
tableNode.updateRowHeight(0, transaction: transaction);
4242
await editor.editorState.apply(transaction);
4343

4444
expect(tableNode.getRowHeight(0) != row0beforeHeight, true);
@@ -72,14 +72,14 @@ void main() async {
7272
await editor.ime.insertText('aaaaaaaaa');
7373

7474
Transaction transaction = editor.editorState.transaction;
75-
tableNode.updateRowHeight(0, transaction);
75+
tableNode.updateRowHeight(0, transaction: transaction);
7676
await editor.editorState.apply(transaction);
7777

7878
expect(tableNode.getRowHeight(0) != row0beforeHeight, true);
7979
expect(tableNode.getRowHeight(0), cell10.children.first.rect.height + 8);
8080

8181
transaction = editor.editorState.transaction;
82-
tableNode.setColWidth(1, 302.5, transaction);
82+
tableNode.setColWidth(1, 302.5, transaction: transaction);
8383
await editor.editorState.apply(transaction);
8484

8585
await tester.pumpAndSettle(const Duration(milliseconds: 300));

0 commit comments

Comments
 (0)