Skip to content

Commit

Permalink
test: add copy/paste/cut test
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Dec 20, 2024
1 parent 2ca4482 commit 7844518
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ class SimpleTableCellBlockWidgetState extends State<SimpleTableCellBlockWidget>
),
if (node.columnIndex == 0 && node.rowIndex == 0)
Positioned(
// todo: don't use hardcoded value
left: 2,
top: 0,
child: _buildTableActionMenu(),
Expand Down Expand Up @@ -371,9 +370,10 @@ class SimpleTableCellBlockWidgetState extends State<SimpleTableCellBlockWidget>
simpleTableContext?.isEditingCell.value = null;
} else if (selection.isCollapsed) {
// if the selection is collapsed, check if the selection is in the cell.
final node = editorState.getNodesInSelection(selection).firstOrNull;
if (node != null) {
final tableNode = node.parentTableNode;
final selectedNode =
editorState.getNodesInSelection(selection).firstOrNull;
if (selectedNode != null) {
final tableNode = selectedNode.parentTableNode;
if (tableNode == null || tableNode.id != node.parentTableNode?.id) {
simpleTableContext?.isEditingCell.value = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/mobile/presentation/bottom_sheet/show_mobile_bottom_sheet.dart';
Expand Down Expand Up @@ -330,12 +332,30 @@ class _SimpleTableMoreActionMenuState extends State<SimpleTableMoreActionMenu> {
valueListenable: isShowingMenu,
builder: (context, isShowingMenu, child) {
return ValueListenableBuilder(
valueListenable: isEditingCellNotifier,
valueListenable: simpleTableContext.isEditingCell,
builder: (context, isEditingCell, child) {
if (!isEditingCell && !isShowingMenu) {
if (isShowingMenu) {
return child!;
}

if (isEditingCell == null) {
return const SizedBox.shrink();
}

final columnIndex = isEditingCell.columnIndex;
final rowIndex = isEditingCell.rowIndex;

switch (widget.type) {
case SimpleTableMoreActionType.column:
if (columnIndex != widget.index) {
return const SizedBox.shrink();
}
case SimpleTableMoreActionType.row:
if (rowIndex != widget.index) {
return const SizedBox.shrink();
}
}

return child!;
},
child: SimpleTableMobileDraggableReorderButton(
Expand Down Expand Up @@ -378,17 +398,27 @@ class SimpleTableActionMenu extends StatelessWidget {

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => _showTableActionBottomSheet(context),
child: Container(
width: 20,
height: 20,
alignment: Alignment.center,
child: const FlowySvg(
FlowySvgs.drag_element_s,
size: Size.square(18.0),
),
),
final simpleTableContext = context.read<SimpleTableContext>();
return ValueListenableBuilder<Node?>(
valueListenable: simpleTableContext.isEditingCell,
builder: (context, isEditingCell, child) {
if (isEditingCell == null) {
return const SizedBox.shrink();
}

return GestureDetector(
onTap: () => _showTableActionBottomSheet(context),
child: Container(
width: 20,
height: 20,
alignment: Alignment.center,
child: const FlowySvg(
FlowySvgs.drag_element_s,
size: Size.square(18.0),
),
),
);
},
);
}

Expand All @@ -401,6 +431,21 @@ class SimpleTableActionMenu extends StatelessWidget {
return;
}

unawaited(
editorState.updateSelectionWithReason(
Selection.collapsed(
Position(
path: tableNode.path,
),
),
customSelectionType: SelectionType.block,
),
);

if (!context.mounted) {
return;
}

final simpleTableContext = context.read<SimpleTableContext>();

simpleTableContext.isSelectingTable.value = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ extension TableContentOperation on EditorState {
///
/// If the [clearContent] is true, the content of the column will be cleared after
/// copying.
Future<void> copyColumn({
Future<ClipboardServiceData?> copyColumn({
required Node tableNode,
required int columnIndex,
bool clearContent = false,
}) async {
assert(tableNode.type == SimpleTableBlockKeys.type);

if (tableNode.type != SimpleTableBlockKeys.type) {
return;
return null;
}

if (columnIndex < 0 || columnIndex >= tableNode.columnLength) {
Log.warn('copy column: index out of range: $columnIndex');
return;
return null;
}

// the plain text content of the column
Expand All @@ -137,10 +137,18 @@ extension TableContentOperation on EditorState {
final cell = columnIndex >= row.children.length
? row.children.last
: row.children[columnIndex];
final startNode = cell.getFirstChildIndex();
final endNode = cell.getLastChildIndex();
if (startNode == null || endNode == null) {
continue;
}
final plainText = getTextInSelection(
Selection(
start: Position(path: cell.path),
end: Position(path: cell.path.next),
start: Position(path: startNode.path),
end: Position(
path: endNode.path,
offset: endNode.delta?.length ?? 0,
),
),
);
content.add(plainText.join('\n'));
Expand All @@ -150,39 +158,37 @@ extension TableContentOperation on EditorState {
final plainText = content.join('\n');
final document = Document.blank()..insert([0], cells);

await getIt<ClipboardService>().setData(
ClipboardServiceData(
plainText: plainText,
tableJson: jsonEncode(document.toJson()),
),
);

if (clearContent) {
await clearContentAtColumnIndex(
tableNode: tableNode,
columnIndex: columnIndex,
);
}

return ClipboardServiceData(
plainText: plainText,
tableJson: jsonEncode(document.toJson()),
);
}

/// Copy the selected row to the clipboard.
///
/// If the [clearContent] is true, the content of the row will be cleared after
/// copying.
Future<void> copyRow({
Future<ClipboardServiceData?> copyRow({
required Node tableNode,
required int rowIndex,
bool clearContent = false,
}) async {
assert(tableNode.type == SimpleTableBlockKeys.type);

if (tableNode.type != SimpleTableBlockKeys.type) {
return;
return null;
}

if (rowIndex < 0 || rowIndex >= tableNode.rowLength) {
Log.warn('copy row: index out of range: $rowIndex');
return;
return null;
}

// the plain text content of the row
Expand All @@ -194,10 +200,18 @@ extension TableContentOperation on EditorState {
final row = tableNode.children[rowIndex];
for (var i = 0; i < row.children.length; i++) {
final cell = row.children[i];
final startNode = cell.getFirstChildIndex();
final endNode = cell.getLastChildIndex();
if (startNode == null || endNode == null) {
continue;
}
final plainText = getTextInSelection(
Selection(
start: Position(path: cell.path),
end: Position(path: cell.path.next),
start: Position(path: startNode.path),
end: Position(
path: endNode.path,
offset: endNode.delta?.length ?? 0,
),
),
);
content.add(plainText.join('\n'));
Expand All @@ -207,30 +221,28 @@ extension TableContentOperation on EditorState {
final plainText = content.join('\n');
final document = Document.blank()..insert([0], cells);

await getIt<ClipboardService>().setData(
ClipboardServiceData(
plainText: plainText,
tableJson: jsonEncode(document.toJson()),
),
);

if (clearContent) {
await clearContentAtRowIndex(
tableNode: tableNode,
rowIndex: rowIndex,
);
}

return ClipboardServiceData(
plainText: plainText,
tableJson: jsonEncode(document.toJson()),
);
}

/// Copy the selected table to the clipboard.
Future<void> copyTable({
Future<ClipboardServiceData?> copyTable({
required Node tableNode,
bool clearContent = false,
}) async {
assert(tableNode.type == SimpleTableBlockKeys.type);

if (tableNode.type != SimpleTableBlockKeys.type) {
return;
return null;
}

// the plain text content of the table
Expand Down Expand Up @@ -265,16 +277,14 @@ extension TableContentOperation on EditorState {
final plainText = content.join('\n');
final document = Document.blank()..insert([0], cells);

await getIt<ClipboardService>().setData(
ClipboardServiceData(
plainText: plainText,
tableJson: jsonEncode(document.toJson()),
),
);

if (clearContent) {
await clearAllContent(tableNode: tableNode);
}

return ClipboardServiceData(
plainText: plainText,
tableJson: jsonEncode(document.toJson()),
);
}

/// Paste the clipboard content to the table column.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,38 +111,50 @@ class SimpleTableCellQuickActions extends ISimpleTableBottomSheetActions {
Navigator.of(context).pop();
}

void _onCut(Node tableNode) {
Future<void> _onCut(Node tableNode) async {
ClipboardServiceData? data;

switch (type) {
case SimpleTableMoreActionType.column:
editorState.copyColumn(
data = await editorState.copyColumn(
tableNode: tableNode,
columnIndex: cellNode.columnIndex,
clearContent: true,
);
case SimpleTableMoreActionType.row:
editorState.copyRow(
data = await editorState.copyRow(
tableNode: tableNode,
rowIndex: cellNode.rowIndex,
clearContent: true,
);
}

if (data != null) {
await getIt<ClipboardService>().setData(data);
}
}

void _onCopy(
Future<void> _onCopy(
Node tableNode,
) {
) async {
ClipboardServiceData? data;

switch (type) {
case SimpleTableMoreActionType.column:
editorState.copyColumn(
data = await editorState.copyColumn(
tableNode: tableNode,
columnIndex: cellNode.columnIndex,
);
case SimpleTableMoreActionType.row:
editorState.copyRow(
data = await editorState.copyRow(
tableNode: tableNode,
rowIndex: cellNode.rowIndex,
);
}

if (data != null) {
await getIt<ClipboardService>().setData(data);
}
}

void _onPaste(Node tableNode) {
Expand Down Expand Up @@ -1231,14 +1243,24 @@ class SimpleTableQuickActions extends StatelessWidget {
Navigator.of(context).pop();
}

void _onCut(Node tableNode) => editorState.copyTable(
tableNode: tableNode,
clearContent: true,
);
Future<void> _onCut(Node tableNode) async {
final data = await editorState.copyTable(
tableNode: tableNode,
clearContent: true,
);
if (data != null) {
await getIt<ClipboardService>().setData(data);
}
}

void _onCopy(Node tableNode) => editorState.copyTable(
tableNode: tableNode,
);
Future<void> _onCopy(Node tableNode) async {
final data = await editorState.copyTable(
tableNode: tableNode,
);
if (data != null) {
await getIt<ClipboardService>().setData(data);
}
}

void _onPaste(Node tableNode) => editorState.pasteTable(
tableNode: tableNode,
Expand Down
Loading

0 comments on commit 7844518

Please sign in to comment.