* fix: duplicate document

* fix: number cell data parser
This commit is contained in:
Nathan.fooo 2023-04-22 11:57:35 +08:00 committed by GitHub
parent d804e3ed6d
commit d3363aba0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 15 deletions

View File

@ -173,8 +173,8 @@ class CellController<T, D> extends Equatable {
void _loadData() {
_saveDataOperation?.cancel();
_loadDataOperation?.cancel();
_loadDataOperation = Timer(const Duration(milliseconds: 10), () {
_cellDataLoader.loadData().then((data) {
if (data != null) {
@ -182,7 +182,6 @@ class CellController<T, D> extends Equatable {
} else {
_cellCache.remove(_cacheKey);
}
_cellDataNotifier?.value = data;
});
});

View File

@ -55,7 +55,7 @@ class CellControllerBuilder {
case FieldType.Number:
final cellDataLoader = CellDataLoader(
cellId: _cellId,
parser: StringCellDataParser(),
parser: NumberCellDataParser(),
reloadOnFieldChanged: true,
);
return NumberCellController(

View File

@ -27,7 +27,12 @@ class CellDataLoader<T> {
(result) => result.fold(
(CellPB cell) {
try {
return parser.parserData(cell.data);
// Return null the data of the cell is empty.
if (cell.data.isEmpty) {
return null;
} else {
return parser.parserData(cell.data);
}
} catch (e, s) {
Log.error('$parser parser cellData failed, $e');
Log.error('Stack trace \n $s');
@ -51,6 +56,13 @@ class StringCellDataParser implements CellDataParser<String> {
}
}
class NumberCellDataParser implements CellDataParser<String> {
@override
String? parserData(List<int> data) {
return utf8.decode(data);
}
}
class DateCellDataParser implements CellDataParser<DateCellDataPB> {
@override
DateCellDataPB? parserData(List<int> data) {

View File

@ -1,11 +1,11 @@
import 'package:appflowy/plugins/database_view/application/cell/cell_controller_builder.dart';
import 'package:appflowy_backend/log.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'dart:async';
part 'number_cell_bloc.freezed.dart';
//
class NumberCellBloc extends Bloc<NumberCellEvent, NumberCellState> {
final NumberCellController cellController;
void Function()? _onCellChangedFn;
@ -22,17 +22,18 @@ class NumberCellBloc extends Bloc<NumberCellEvent, NumberCellState> {
didReceiveCellUpdate: (cellContent) {
emit(state.copyWith(cellContent: cellContent ?? ""));
},
updateCell: (text) {
updateCell: (text) async {
if (state.cellContent != text) {
emit(state.copyWith(cellContent: text));
cellController.saveCellData(
text,
onFinish: (result) {
result.fold(
() {},
(err) => Log.error(err),
);
},
await cellController.saveCellData(text);
// If the input content is "abc" that can't parsered as number then the data stored in the backend will be an empty string.
// So for every cell data that will be formatted in the backend.
// It needs to get the formatted data after saving.
add(
NumberCellEvent.didReceiveCellUpdate(
cellController.getCellData(),
),
);
}
},

View File

@ -66,7 +66,8 @@ impl AppFlowyDocumentEditor {
pub async fn duplicate_document(&self) -> FlowyResult<String> {
let transaction = self.document_transaction().await?;
let json = transaction.to_json()?;
let document = Document::from_transaction(transaction)?;
let json = serde_json::to_string(&document)?;
Ok(json)
}