Skip to content

Commit

Permalink
feat: support customizing the record time (#461)
Browse files Browse the repository at this point in the history
* feat: support customzing the record time

* fix: background color overflow and block selection doesn't work
  • Loading branch information
LucasXu0 authored Sep 14, 2023
1 parent 177f742 commit 3e53481
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ class _BulletedListBlockComponentWidgetState
Node get node => widget.node;

@override
Widget buildComponent(BuildContext context) {
Widget buildComponent(
BuildContext context, {
bool withBackgroundColor = true,
}) {
final textDirection = calculateTextDirection(
layoutDirection: Directionality.maybeOf(context),
);

Widget child = Container(
color: backgroundColor,
color: withBackgroundColor ? backgroundColor : null,
width: double.infinity,
alignment: alignment,
child: Row(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ class _DividerBlockComponentWidgetState
CursorStyle get cursorStyle => CursorStyle.cover;

@override
Rect getBlockRect() {
Rect getBlockRect({
bool shiftWithBaseOffset = false,
}) {
return getRectsInSelection(Selection.invalid()).first;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ class ImageBlockComponentWidgetState extends State<ImageBlockComponentWidget>
CursorStyle get cursorStyle => CursorStyle.cover;

@override
Rect getBlockRect() {
Rect getBlockRect({
bool shiftWithBaseOffset = false,
}) {
return getCursorRectInPosition(Position.invalid()) ?? Rect.zero;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,16 @@ class _NumberedListBlockComponentWidgetState
Node get node => widget.node;

@override
Widget buildComponent(BuildContext context) {
Widget buildComponent(
BuildContext context, {
bool withBackgroundColor = true,
}) {
final textDirection = calculateTextDirection(
layoutDirection: Directionality.maybeOf(context),
);

Widget child = Container(
color: backgroundColor,
color: withBackgroundColor ? backgroundColor : null,
width: double.infinity,
alignment: alignment,
child: Row(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ class _AppFlowyRichTextState extends State<AppFlowyRichText>
);

@override
Rect getBlockRect() {
Rect getBlockRect({
bool shiftWithBaseOffset = false,
}) {
throw UnimplementedError();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ mixin DefaultSelectableMixin {
return Offset.zero;
}

Rect getBlockRect() {
Rect getBlockRect({
bool shiftWithBaseOffset = false,
}) {
final parentBox = containerKey.currentContext?.findRenderObject();
final childBox = blockComponentKey.currentContext?.findRenderObject();
if (parentBox is RenderBox && childBox is RenderBox) {
final offset = childBox.localToGlobal(Offset.zero, ancestor: parentBox);
final size = parentBox.size;
return Offset.zero & (size - offset as Size);
if (shiftWithBaseOffset) {
return offset & (size - offset as Size);
}
return Offset.zero & childBox.size;
}
return Rect.zero;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ class _TableBlockComponentWidgetState extends State<TableBlockComponentWidget>
_renderBox.localToGlobal(offset);

@override
Rect getBlockRect() {
Rect getBlockRect({
bool shiftWithBaseOffset = false,
}) {
return getRectsInSelection(Selection.invalid()).first;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ class _TextBlockComponentWidgetState extends State<TextBlockComponentWidget>
}

@override
Widget buildComponent(BuildContext context) {
Widget buildComponent(
BuildContext context, {
bool withBackgroundColor = true,
}) {
final textDirection = calculateTextDirection(
layoutDirection: Directionality.maybeOf(context),
);

Widget child = Container(
color: backgroundColor,
color: withBackgroundColor ? backgroundColor : null,
width: double.infinity,
alignment: alignment,
child: Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@ class _TodoListBlockComponentWidgetState
bool get checked => widget.node.attributes[TodoListBlockKeys.checked];

@override
Widget buildComponent(BuildContext context) {
Widget buildComponent(
BuildContext context, {
bool withBackgroundColor = true,
}) {
final textDirection = calculateTextDirection(
layoutDirection: Directionality.maybeOf(context),
);

Widget child = Container(
color: backgroundColor,
color: withBackgroundColor ? backgroundColor : null,
width: double.infinity,
alignment: alignment,
child: Row(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ mixin NestedBlockComponentStatefulWidgetMixin<
super.initState();

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
final left = node.selectable?.getBlockRect().left;
final left =
node.selectable?.getBlockRect(shiftWithBaseOffset: true).left;
if (cachedLeft != left) {
setState(() => cachedLeft = left);
}
Expand All @@ -102,7 +103,7 @@ mixin NestedBlockComponentStatefulWidgetMixin<
@override
Widget build(BuildContext context) {
return node.children.isEmpty
? buildComponent(context)
? buildComponent(context, withBackgroundColor: true)
: buildComponentWithChildren(context);
}

Expand All @@ -117,7 +118,7 @@ mixin NestedBlockComponentStatefulWidgetMixin<
),
NestedListWidget(
indentPadding: indentPadding,
child: buildComponent(context),
child: buildComponent(context, withBackgroundColor: false),
children: editorState.renderer.buildList(
context,
widget.node.children,
Expand All @@ -127,5 +128,8 @@ mixin NestedBlockComponentStatefulWidgetMixin<
);
}

Widget buildComponent(BuildContext context);
Widget buildComponent(
BuildContext context, {
bool withBackgroundColor = true,
});
}
7 changes: 5 additions & 2 deletions lib/src/editor_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ enum TransactionTime {
class EditorState {
EditorState({
required this.document,
this.minHistoryItemDuration = const Duration(milliseconds: 200),
}) {
undoManager.state = this;
}
Expand All @@ -82,6 +83,9 @@ class EditorState {

final Document document;

// the minimum duration for saving the history item.
final Duration minHistoryItemDuration;

/// Whether the editor is editable.
bool editable = true;

Expand Down Expand Up @@ -477,8 +481,7 @@ class EditorState {
return;
}
_debouncedSealHistoryItemTimer?.cancel();
_debouncedSealHistoryItemTimer =
Timer(const Duration(milliseconds: 1000), () {
_debouncedSealHistoryItemTimer = Timer(minHistoryItemDuration, () {
if (undoManager.undoStack.isNonEmpty) {
Log.editor.debug('Seal history item');
final last = undoManager.undoStack.last;
Expand Down
96 changes: 80 additions & 16 deletions lib/src/l10n/intl/messages_zh-CN.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,85 @@ class MessageLookup extends MessageLookupByLibrary {

final messages = _notInlinedMessages(_notInlinedMessages);
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
"bold": MessageLookupByLibrary.simpleMessage(""),
"bulletedList": MessageLookupByLibrary.simpleMessage(""),
"checkbox": MessageLookupByLibrary.simpleMessage(""),
"embedCode": MessageLookupByLibrary.simpleMessage(""),
"heading1": MessageLookupByLibrary.simpleMessage(""),
"heading2": MessageLookupByLibrary.simpleMessage(""),
"heading3": MessageLookupByLibrary.simpleMessage(""),
"highlight": MessageLookupByLibrary.simpleMessage(""),
"image": MessageLookupByLibrary.simpleMessage(""),
"italic": MessageLookupByLibrary.simpleMessage(""),
"link": MessageLookupByLibrary.simpleMessage(""),
"numberedList": MessageLookupByLibrary.simpleMessage(""),
"quote": MessageLookupByLibrary.simpleMessage(""),
"strikethrough": MessageLookupByLibrary.simpleMessage(""),
"text": MessageLookupByLibrary.simpleMessage(""),
"underline": MessageLookupByLibrary.simpleMessage("")
"addYourLink": MessageLookupByLibrary.simpleMessage("添加链接"),
"auto": MessageLookupByLibrary.simpleMessage("自动"),
"backgroundColor": MessageLookupByLibrary.simpleMessage("背景颜色"),
"backgroundColorBlue": MessageLookupByLibrary.simpleMessage("蓝色背景"),
"backgroundColorBrown": MessageLookupByLibrary.simpleMessage("棕色背景"),
"backgroundColorDefault": MessageLookupByLibrary.simpleMessage("默认背景色"),
"backgroundColorGray": MessageLookupByLibrary.simpleMessage("灰色背景"),
"backgroundColorGreen": MessageLookupByLibrary.simpleMessage("绿色背景"),
"backgroundColorOrange": MessageLookupByLibrary.simpleMessage("橙色背景"),
"backgroundColorPink": MessageLookupByLibrary.simpleMessage("粉色背景"),
"backgroundColorPurple": MessageLookupByLibrary.simpleMessage("紫色背景"),
"backgroundColorRed": MessageLookupByLibrary.simpleMessage("红色背景"),
"backgroundColorYellow": MessageLookupByLibrary.simpleMessage("黄色背景"),
"bold": MessageLookupByLibrary.simpleMessage("粗体"),
"bulletedList": MessageLookupByLibrary.simpleMessage("无序列表"),
"cancel": MessageLookupByLibrary.simpleMessage("取消"),
"checkbox": MessageLookupByLibrary.simpleMessage("选框"),
"clearHighlightColor": MessageLookupByLibrary.simpleMessage("清除高亮颜色"),
"color": MessageLookupByLibrary.simpleMessage("颜色"),
"copyLink": MessageLookupByLibrary.simpleMessage("复制链接"),
"customColor": MessageLookupByLibrary.simpleMessage("自定义颜色"),
"done": MessageLookupByLibrary.simpleMessage("完成"),
"editLink": MessageLookupByLibrary.simpleMessage("修改链接"),
"embedCode": MessageLookupByLibrary.simpleMessage("代码块"),
"fontColorBlue": MessageLookupByLibrary.simpleMessage("蓝色"),
"fontColorBrown": MessageLookupByLibrary.simpleMessage("棕色"),
"fontColorDefault": MessageLookupByLibrary.simpleMessage("默认"),
"fontColorGray": MessageLookupByLibrary.simpleMessage("灰色"),
"fontColorGreen": MessageLookupByLibrary.simpleMessage("绿色"),
"fontColorOrange": MessageLookupByLibrary.simpleMessage("橙色"),
"fontColorPink": MessageLookupByLibrary.simpleMessage("粉红色"),
"fontColorPurple": MessageLookupByLibrary.simpleMessage("紫色"),
"fontColorRed": MessageLookupByLibrary.simpleMessage("红色"),
"fontColorYellow": MessageLookupByLibrary.simpleMessage("黄色"),
"heading1": MessageLookupByLibrary.simpleMessage("一级标题"),
"heading2": MessageLookupByLibrary.simpleMessage("二级标题"),
"heading3": MessageLookupByLibrary.simpleMessage("三级标题"),
"hexValue": MessageLookupByLibrary.simpleMessage("十六进制值"),
"highlight": MessageLookupByLibrary.simpleMessage("高亮"),
"highlightColor": MessageLookupByLibrary.simpleMessage("高亮颜色"),
"image": MessageLookupByLibrary.simpleMessage("图片"),
"italic": MessageLookupByLibrary.simpleMessage("斜体"),
"lightLightTint1": MessageLookupByLibrary.simpleMessage("紫色"),
"lightLightTint2": MessageLookupByLibrary.simpleMessage("粉红色"),
"lightLightTint3": MessageLookupByLibrary.simpleMessage("浅粉红色"),
"lightLightTint4": MessageLookupByLibrary.simpleMessage("橙色"),
"lightLightTint5": MessageLookupByLibrary.simpleMessage("黄色"),
"lightLightTint6": MessageLookupByLibrary.simpleMessage("草绿色"),
"lightLightTint7": MessageLookupByLibrary.simpleMessage("绿色"),
"lightLightTint8": MessageLookupByLibrary.simpleMessage("水蓝色"),
"lightLightTint9": MessageLookupByLibrary.simpleMessage("蓝色"),
"link": MessageLookupByLibrary.simpleMessage("链接"),
"linkAddressHint": MessageLookupByLibrary.simpleMessage("请输入URL"),
"linkText": MessageLookupByLibrary.simpleMessage("文字"),
"linkTextHint": MessageLookupByLibrary.simpleMessage("请输入文字"),
"ltr": MessageLookupByLibrary.simpleMessage("自左至右"),
"mobileHeading1": MessageLookupByLibrary.simpleMessage("一级标题"),
"mobileHeading2": MessageLookupByLibrary.simpleMessage("二级标题"),
"mobileHeading3": MessageLookupByLibrary.simpleMessage("三级标题"),
"numberedList": MessageLookupByLibrary.simpleMessage("有序列表"),
"opacity": MessageLookupByLibrary.simpleMessage("透明度"),
"openLink": MessageLookupByLibrary.simpleMessage("打开链接"),
"quote": MessageLookupByLibrary.simpleMessage("引文"),
"removeLink": MessageLookupByLibrary.simpleMessage("移除链接"),
"resetToDefaultColor": MessageLookupByLibrary.simpleMessage("重设为默认颜色"),
"rtl": MessageLookupByLibrary.simpleMessage("自右至左"),
"strikethrough": MessageLookupByLibrary.simpleMessage("删除线"),
"text": MessageLookupByLibrary.simpleMessage("文本"),
"textColor": MessageLookupByLibrary.simpleMessage("文字颜色"),
"tint1": MessageLookupByLibrary.simpleMessage("色调1"),
"tint2": MessageLookupByLibrary.simpleMessage("色调2"),
"tint3": MessageLookupByLibrary.simpleMessage("色调3"),
"tint4": MessageLookupByLibrary.simpleMessage("色调4"),
"tint5": MessageLookupByLibrary.simpleMessage("色调5"),
"tint6": MessageLookupByLibrary.simpleMessage("色调6"),
"tint7": MessageLookupByLibrary.simpleMessage("色调7"),
"tint8": MessageLookupByLibrary.simpleMessage("色调8"),
"tint9": MessageLookupByLibrary.simpleMessage("色调9"),
"underline": MessageLookupByLibrary.simpleMessage("下划线"),
"urlHint": MessageLookupByLibrary.simpleMessage("URL")
};
}
Loading

0 comments on commit 3e53481

Please sign in to comment.