Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimize the table node parse steps and add logs #875

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,66 @@
}

@override
bool validate(Node node) =>
node.attributes.isNotEmpty &&
node.attributes.containsKey(TableBlockKeys.colsLen) &&
node.attributes.containsKey(TableBlockKeys.rowsLen);
bool validate(Node node) {
// check the node is valid
if (node.attributes.isEmpty) {
Log.editor.debug('TableBlockComponentBuilder: node is empty');

Check warning on line 116 in lib/src/editor/block_component/table_block_component/table_block_component.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/block_component/table_block_component/table_block_component.dart#L116

Added line #L116 was not covered by tests
return false;
}

// check the node has rowPosition and colPosition
if (!node.attributes.containsKey(TableBlockKeys.colsLen) ||
!node.attributes.containsKey(TableBlockKeys.rowsLen)) {
Log.editor.debug(

Check warning on line 123 in lib/src/editor/block_component/table_block_component/table_block_component.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/block_component/table_block_component/table_block_component.dart#L123

Added line #L123 was not covered by tests
'TableBlockComponentBuilder: node has no colsLen or rowsLen',
);
return false;
}

final colsLen = node.attributes[TableBlockKeys.colsLen];
final rowsLen = node.attributes[TableBlockKeys.rowsLen];

// check its children
final children = node.children;
if (children.isEmpty) {
Log.editor.debug('TableBlockComponentBuilder: children is empty');

Check warning on line 135 in lib/src/editor/block_component/table_block_component/table_block_component.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/block_component/table_block_component/table_block_component.dart#L135

Added line #L135 was not covered by tests
return false;
}

if (children.length != colsLen * rowsLen) {
Log.editor.debug(
'TableBlockComponentBuilder: children length(${children.length}) is not equal to colsLen * rowsLen($colsLen * $rowsLen)',

Check warning on line 141 in lib/src/editor/block_component/table_block_component/table_block_component.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/block_component/table_block_component/table_block_component.dart#L140-L141

Added lines #L140 - L141 were not covered by tests
);
return false;
}

// all children should contain rowPosition and colPosition
for (var i = 0; i < colsLen; i++) {
for (var j = 0; j < rowsLen; j++) {
final child = children.where(
(n) =>
n.attributes[TableCellBlockKeys.colPosition] == i &&
n.attributes[TableCellBlockKeys.rowPosition] == j,
);
if (child.isEmpty) {
Log.editor.debug(
'TableBlockComponentBuilder: child($i, $j) is empty',

Check warning on line 156 in lib/src/editor/block_component/table_block_component/table_block_component.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/block_component/table_block_component/table_block_component.dart#L155-L156

Added lines #L155 - L156 were not covered by tests
);
return false;
}

// should only contains one child
if (child.length != 1) {
Log.editor.debug(
'TableBlockComponentBuilder: child($i, $j) is not unique',

Check warning on line 164 in lib/src/editor/block_component/table_block_component/table_block_component.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/block_component/table_block_component/table_block_component.dart#L163-L164

Added lines #L163 - L164 were not covered by tests
);
return false;
}
}
}

return true;
}
}

class TableBlockComponentWidget extends BlockComponentStatefulWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
Node node,
);

Node tableCellNode(String text, int rowPosition, int colPosition) {
return Node(

Check warning on line 31 in lib/src/editor/block_component/table_block_component/table_cell_block_component.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/block_component/table_block_component/table_cell_block_component.dart#L30-L31

Added lines #L30 - L31 were not covered by tests
type: TableCellBlockKeys.type,
attributes: {

Check warning on line 33 in lib/src/editor/block_component/table_block_component/table_cell_block_component.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/block_component/table_block_component/table_cell_block_component.dart#L33

Added line #L33 was not covered by tests
TableCellBlockKeys.rowPosition: rowPosition,
TableCellBlockKeys.colPosition: colPosition,
},
children: [
paragraphNode(text: text),

Check warning on line 38 in lib/src/editor/block_component/table_block_component/table_cell_block_component.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/block_component/table_block_component/table_cell_block_component.dart#L37-L38

Added lines #L37 - L38 were not covered by tests
],
);
}

class TableCellBlockComponentBuilder extends BlockComponentBuilder {
TableCellBlockComponentBuilder({
super.configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,59 @@
TableNode({
required this.node,
}) : _config = TableConfig.fromJson(node.attributes) {
assert(node.type == TableBlockKeys.type);
assert(node.attributes.containsKey(TableBlockKeys.colsLen));
assert(node.attributes[TableBlockKeys.colsLen] is int);
assert(node.attributes.containsKey(TableBlockKeys.rowsLen));
assert(node.attributes[TableBlockKeys.rowsLen] is int);

assert(node.attributes[TableBlockKeys.rowDefaultHeight] != null);
assert(node.attributes[TableBlockKeys.colMinimumWidth] != null);
assert(node.attributes[TableBlockKeys.colDefaultWidth] != null);

final int colsCount = node.attributes[TableBlockKeys.colsLen];
final int rowsCount = node.attributes[TableBlockKeys.rowsLen];
assert(node.children.length == colsCount * rowsCount);
assert(
node.children.every(
(n) =>
n.attributes.containsKey(TableCellBlockKeys.rowPosition) &&
n.attributes.containsKey(TableCellBlockKeys.colPosition),
),
);
assert(
node.children.every(
(n) =>
n.attributes.containsKey(TableCellBlockKeys.rowPosition) &&
n.attributes.containsKey(TableCellBlockKeys.colPosition),
),
);
if (node.type != TableBlockKeys.type) {
Log.editor.debug('TableNode: node is not a table');

Check warning on line 16 in lib/src/editor/block_component/table_block_component/table_node.dart

View check run for this annotation

Codecov / codecov/patch

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

Added line #L16 was not covered by tests
return;
}

final attributes = node.attributes;
final colsLen = attributes[TableBlockKeys.colsLen];
final rowsLen = attributes[TableBlockKeys.rowsLen];

if (colsLen == null ||
rowsLen == null ||
colsLen is! int ||
rowsLen is! int) {
Log.editor.debug(

Check warning on line 28 in lib/src/editor/block_component/table_block_component/table_node.dart

View check run for this annotation

Codecov / codecov/patch

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

Added line #L28 was not covered by tests
'TableNode: colsLen or rowsLen is not an integer or null',
);
return;
}

for (var i = 0; i < colsCount; i++) {
if (node.children.length != colsLen * rowsLen) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you just call validate on the node (maybe with some customization on the validate function)?

Log.editor.debug(
'TableNode: the number of children is not equal to the number of cells',
);
return;
}

// every cell should has rowPosition and colPosition to indicate its position in the table
for (final child in node.children) {
if (!child.attributes.containsKey(TableCellBlockKeys.rowPosition) ||
!child.attributes.containsKey(TableCellBlockKeys.colPosition)) {
Log.editor.debug('TableNode: cell has no rowPosition or colPosition');

Check warning on line 45 in lib/src/editor/block_component/table_block_component/table_node.dart

View check run for this annotation

Codecov / codecov/patch

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

Added line #L45 was not covered by tests
return;
}
}

for (var i = 0; i < colsLen; i++) {
_cells.add([]);
for (var j = 0; j < rowsCount; j++) {
final cell = node.children.where(
(n) =>
n.attributes[TableCellBlockKeys.colPosition] == i &&
n.attributes[TableCellBlockKeys.rowPosition] == j,
);
assert(cell.length == 1);
_cells[i].add(newCellNode(node, cell.first));
for (var j = 0; j < rowsLen; j++) {
final cell = node.children
.where(
(n) =>
n.attributes[TableCellBlockKeys.colPosition] == i &&
n.attributes[TableCellBlockKeys.rowPosition] == j,
)
.firstOrNull;

if (cell == null) {
Log.editor.debug('TableNode: cell is empty');
_cells.clear();

Check warning on line 63 in lib/src/editor/block_component/table_block_component/table_node.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/block_component/table_block_component/table_node.dart#L62-L63

Added lines #L62 - L63 were not covered by tests
return;
}

_cells[i].add(newCellNode(node, cell));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ void main() {
],
};

expect(() => TableNode.fromJson(jsonData), throwsAssertionError);
// it should not throw error
expect(() => TableNode.fromJson(jsonData), isNot(throwsFlutterError));
});

test('default constructor (from list of list of strings)', () {
Expand Down
Loading