feat: override block component validator (#924)

This commit is contained in:
Lucas 2024-10-14 10:06:07 +08:00 • committed by GitHub
commit 9580c6d0c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 64 additions and 71 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -63,9 +63,6 @@ class HeadingBlockComponentBuilder extends BlockComponentBuilder {
),
);
}
@override
bool validate(Node node) => true;
}
class HeadingBlockComponentWidget extends BlockComponentStatefulWidget {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -110,68 +110,68 @@ class TableBlockComponentBuilder extends BlockComponentBuilder {
}
@override
bool validate(Node node) {
// check the node is valid
if (node.attributes.isEmpty) {
AppFlowyEditorLog.editor
.debug('TableBlockComponentBuilder: node is empty');
return false;
}
BlockComponentValidate get validate => (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;
}
// 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');
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)',
);
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',
);
return false;
}
// should only contains one child
if (child.length != 1) {
AppFlowyEditorLog.editor.debug(
'TableBlockComponentBuilder: child($i, $j) is not unique',
);
return false;
}
}
}
return true;
};
}
class TableBlockComponentWidget extends BlockComponentStatefulWidget {

View file

@ -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);

View file

@ -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 {

View file

@ -12,6 +12,8 @@ typedef BlockActionBuilder = Widget Function(
BlockComponentActionState state,
);
typedef BlockComponentValidate = bool Function(Node node);
abstract class BlockComponentActionState {
set alwaysShowActions(bool alwaysShowActions);
}
@ -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();

View file

@ -89,9 +89,6 @@ class ErrorBlockComponentBuilder extends BlockComponentBuilder {
),
);
}
@override
bool validate(Node node) => true;
}
class ErrorBlockComponentWidget extends BlockComponentStatefulWidget {