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: override block component validator #924

Merged
merged 1 commit into from
Oct 14, 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 @@ -60,7 +60,7 @@ class BulletedListBlockComponentBuilder extends BlockComponentBuilder {
}

@override
bool validate(Node node) => node.delta != null;
BlockComponentValidate get validate => (node) => node.delta != null;
}

class BulletedListBlockComponentWidget extends BlockComponentStatefulWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DividerBlockComponentBuilder extends BlockComponentBuilder {
}

@override
bool validate(Node node) => node.children.isEmpty;
BlockComponentValidate get validate => (node) => node.children.isEmpty;
}

class DividerBlockComponentWidget extends BlockComponentStatefulWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ class HeadingBlockComponentBuilder extends BlockComponentBuilder {
),
);
}

@override
bool validate(Node node) => true;
}

class HeadingBlockComponentWidget extends BlockComponentStatefulWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class ImageBlockComponentBuilder extends BlockComponentBuilder {
}

@override
bool validate(Node node) => node.delta == null && node.children.isEmpty;
BlockComponentValidate get validate =>
(node) => node.delta == null && node.children.isEmpty;
}

class ImageBlockComponentWidget extends BlockComponentStatefulWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class NumberedListBlockComponentBuilder extends BlockComponentBuilder {
}

@override
bool validate(Node node) => node.delta != null;
BlockComponentValidate get validate => (node) => node.delta != null;
}

class NumberedListBlockComponentWidget extends BlockComponentStatefulWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ class ParagraphBlockComponentBuilder extends BlockComponentBuilder {
}

@override
bool validate(Node node) {
return node.delta != null;
}
BlockComponentValidate get validate => (node) => node.delta != null;
}

class ParagraphBlockComponentWidget extends BlockComponentStatefulWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class QuoteBlockComponentBuilder extends BlockComponentBuilder {
}

@override
bool validate(Node node) => node.delta != null;
BlockComponentValidate get validate => (node) => node.delta != null;
}

class QuoteBlockComponentWidget extends BlockComponentStatefulWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,68 +110,68 @@
}

@override
bool validate(Node node) {
// check the node is valid
if (node.attributes.isEmpty) {
AppFlowyEditorLog.editor
.debug('TableBlockComponentBuilder: node is empty');
return false;
}

// check the node has rowPosition and colPosition
if (!node.attributes.containsKey(TableBlockKeys.colsLen) ||
!node.attributes.containsKey(TableBlockKeys.rowsLen)) {
AppFlowyEditorLog.editor.debug(
'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) {
AppFlowyEditorLog.editor
.debug('TableBlockComponentBuilder: children is empty');
return false;
}

if (children.length != colsLen * rowsLen) {
AppFlowyEditorLog.editor.debug(
'TableBlockComponentBuilder: children length(${children.length}) is not equal to colsLen * rowsLen($colsLen * $rowsLen)',
);
return false;
}
BlockComponentValidate get validate => (node) {
// check the node is valid
if (node.attributes.isEmpty) {
AppFlowyEditorLog.editor
.debug('TableBlockComponentBuilder: node is empty');

Check warning on line 117 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-L117

Added lines #L116 - L117 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) {
// check the node has rowPosition and colPosition
if (!node.attributes.containsKey(TableBlockKeys.colsLen) ||
!node.attributes.containsKey(TableBlockKeys.rowsLen)) {
AppFlowyEditorLog.editor.debug(
'TableBlockComponentBuilder: child($i, $j) is empty',
'TableBlockComponentBuilder: node has no colsLen or rowsLen',
);
return false;
}

// should only contains one child
if (child.length != 1) {
final colsLen = node.attributes[TableBlockKeys.colsLen];
final rowsLen = node.attributes[TableBlockKeys.rowsLen];

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

Check warning on line 137 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#L136-L137

Added lines #L136 - L137 were not covered by tests
return false;
}

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

Check warning on line 143 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#L143

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

return true;
}
// 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) {
AppFlowyEditorLog.editor.debug(
'TableBlockComponentBuilder: child($i, $j) is empty',

Check warning on line 158 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#L157-L158

Added lines #L157 - L158 were not covered by tests
);
return false;
}

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

Check warning on line 166 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#L165-L166

Added lines #L165 - L166 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 @@ -68,7 +68,7 @@ class TableCellBlockComponentBuilder extends BlockComponentBuilder {
}

@override
bool validate(Node node) =>
BlockComponentValidate get validate => (node) =>
node.attributes.isNotEmpty &&
node.attributes.containsKey(TableCellBlockKeys.rowPosition) &&
node.attributes.containsKey(TableCellBlockKeys.colPosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ class TodoListBlockComponentBuilder extends BlockComponentBuilder {
}

@override
bool validate(Node node) {
return node.delta != null;
}
BlockComponentValidate get validate => (node) => node.delta != null;
}

class TodoListBlockComponentWidget extends BlockComponentStatefulWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ typedef BlockActionBuilder = Widget Function(
BlockComponentActionState state,
);

typedef BlockComponentValidate = bool Function(Node node);

abstract class BlockComponentActionState {
set alwaysShowActions(bool alwaysShowActions);
}
Expand All @@ -27,11 +29,11 @@ abstract class BlockComponentBuilder with BlockComponentSelectable {
/// return true if the node is valid.
/// return false if the node is invalid,
/// and the node will be displayed as a PlaceHolder widget.
bool validate(Node node) => true;
BlockComponentValidate validate = (_) => true;

BlockComponentWidget build(BlockComponentContext blockComponentContext);

bool Function(Node) showActions = (_) => false;
bool Function(Node node) showActions = (_) => false;

BlockActionBuilder actionBuilder = (_, __) => const SizedBox.shrink();

Expand Down
3 changes: 0 additions & 3 deletions test/customer/custom_error_block_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ class ErrorBlockComponentBuilder extends BlockComponentBuilder {
),
);
}

@override
bool validate(Node node) => true;
}

class ErrorBlockComponentWidget extends BlockComponentStatefulWidget {
Expand Down
Loading