feat: override block component validator (#924)
This commit is contained in:
parent
41048fed6c
commit
9580c6d0c6
12 changed files with 64 additions and 71 deletions
|
|
@ -60,7 +60,7 @@ class BulletedListBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool validate(Node node) => node.delta != null;
|
BlockComponentValidate get validate => (node) => node.delta != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
class BulletedListBlockComponentWidget extends BlockComponentStatefulWidget {
|
class BulletedListBlockComponentWidget extends BlockComponentStatefulWidget {
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ class DividerBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool validate(Node node) => node.children.isEmpty;
|
BlockComponentValidate get validate => (node) => node.children.isEmpty;
|
||||||
}
|
}
|
||||||
|
|
||||||
class DividerBlockComponentWidget extends BlockComponentStatefulWidget {
|
class DividerBlockComponentWidget extends BlockComponentStatefulWidget {
|
||||||
|
|
|
||||||
|
|
@ -63,9 +63,6 @@ class HeadingBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
bool validate(Node node) => true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class HeadingBlockComponentWidget extends BlockComponentStatefulWidget {
|
class HeadingBlockComponentWidget extends BlockComponentStatefulWidget {
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,8 @@ class ImageBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@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 {
|
class ImageBlockComponentWidget extends BlockComponentStatefulWidget {
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ class NumberedListBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool validate(Node node) => node.delta != null;
|
BlockComponentValidate get validate => (node) => node.delta != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
class NumberedListBlockComponentWidget extends BlockComponentStatefulWidget {
|
class NumberedListBlockComponentWidget extends BlockComponentStatefulWidget {
|
||||||
|
|
|
||||||
|
|
@ -60,9 +60,7 @@ class ParagraphBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool validate(Node node) {
|
BlockComponentValidate get validate => (node) => node.delta != null;
|
||||||
return node.delta != null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ParagraphBlockComponentWidget extends BlockComponentStatefulWidget {
|
class ParagraphBlockComponentWidget extends BlockComponentStatefulWidget {
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ class QuoteBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool validate(Node node) => node.delta != null;
|
BlockComponentValidate get validate => (node) => node.delta != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
class QuoteBlockComponentWidget extends BlockComponentStatefulWidget {
|
class QuoteBlockComponentWidget extends BlockComponentStatefulWidget {
|
||||||
|
|
|
||||||
|
|
@ -110,68 +110,68 @@ class TableBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool validate(Node node) {
|
BlockComponentValidate get validate => (node) {
|
||||||
// check the node is valid
|
// check the node is valid
|
||||||
if (node.attributes.isEmpty) {
|
if (node.attributes.isEmpty) {
|
||||||
AppFlowyEditorLog.editor
|
AppFlowyEditorLog.editor
|
||||||
.debug('TableBlockComponentBuilder: node is empty');
|
.debug('TableBlockComponentBuilder: node is empty');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the node has rowPosition and colPosition
|
// check the node has rowPosition and colPosition
|
||||||
if (!node.attributes.containsKey(TableBlockKeys.colsLen) ||
|
if (!node.attributes.containsKey(TableBlockKeys.colsLen) ||
|
||||||
!node.attributes.containsKey(TableBlockKeys.rowsLen)) {
|
!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) {
|
|
||||||
AppFlowyEditorLog.editor.debug(
|
AppFlowyEditorLog.editor.debug(
|
||||||
'TableBlockComponentBuilder: child($i, $j) is empty',
|
'TableBlockComponentBuilder: node has no colsLen or rowsLen',
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// should only contains one child
|
final colsLen = node.attributes[TableBlockKeys.colsLen];
|
||||||
if (child.length != 1) {
|
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(
|
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 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 {
|
class TableBlockComponentWidget extends BlockComponentStatefulWidget {
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ class TableCellBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool validate(Node node) =>
|
BlockComponentValidate get validate => (node) =>
|
||||||
node.attributes.isNotEmpty &&
|
node.attributes.isNotEmpty &&
|
||||||
node.attributes.containsKey(TableCellBlockKeys.rowPosition) &&
|
node.attributes.containsKey(TableCellBlockKeys.rowPosition) &&
|
||||||
node.attributes.containsKey(TableCellBlockKeys.colPosition);
|
node.attributes.containsKey(TableCellBlockKeys.colPosition);
|
||||||
|
|
|
||||||
|
|
@ -81,9 +81,7 @@ class TodoListBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool validate(Node node) {
|
BlockComponentValidate get validate => (node) => node.delta != null;
|
||||||
return node.delta != null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class TodoListBlockComponentWidget extends BlockComponentStatefulWidget {
|
class TodoListBlockComponentWidget extends BlockComponentStatefulWidget {
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ typedef BlockActionBuilder = Widget Function(
|
||||||
BlockComponentActionState state,
|
BlockComponentActionState state,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
typedef BlockComponentValidate = bool Function(Node node);
|
||||||
|
|
||||||
abstract class BlockComponentActionState {
|
abstract class BlockComponentActionState {
|
||||||
set alwaysShowActions(bool alwaysShowActions);
|
set alwaysShowActions(bool alwaysShowActions);
|
||||||
}
|
}
|
||||||
|
|
@ -27,11 +29,11 @@ abstract class BlockComponentBuilder with BlockComponentSelectable {
|
||||||
/// return true if the node is valid.
|
/// return true if the node is valid.
|
||||||
/// return false if the node is invalid,
|
/// return false if the node is invalid,
|
||||||
/// and the node will be displayed as a PlaceHolder widget.
|
/// and the node will be displayed as a PlaceHolder widget.
|
||||||
bool validate(Node node) => true;
|
BlockComponentValidate validate = (_) => true;
|
||||||
|
|
||||||
BlockComponentWidget build(BlockComponentContext blockComponentContext);
|
BlockComponentWidget build(BlockComponentContext blockComponentContext);
|
||||||
|
|
||||||
bool Function(Node) showActions = (_) => false;
|
bool Function(Node node) showActions = (_) => false;
|
||||||
|
|
||||||
BlockActionBuilder actionBuilder = (_, __) => const SizedBox.shrink();
|
BlockActionBuilder actionBuilder = (_, __) => const SizedBox.shrink();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -89,9 +89,6 @@ class ErrorBlockComponentBuilder extends BlockComponentBuilder {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
bool validate(Node node) => true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ErrorBlockComponentWidget extends BlockComponentStatefulWidget {
|
class ErrorBlockComponentWidget extends BlockComponentStatefulWidget {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue