appflowy 3 years ago
parent
commit
a4338652e0

+ 35 - 16
app_flowy/lib/workspace/application/doc/doc_bloc.dart

@@ -1,36 +1,55 @@
+import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
+import 'package:flutter_bloc/flutter_bloc.dart';
 import 'package:app_flowy/workspace/domain/i_doc.dart';
 import 'package:freezed_annotation/freezed_annotation.dart';
-import 'package:flutter_bloc/flutter_bloc.dart';
-
+import 'package:flowy_editor/flowy_editor.dart';
+import 'dart:convert';
 part 'doc_bloc.freezed.dart';
 
 class DocBloc extends Bloc<DocEvent, DocState> {
   final IDoc iDocImpl;
 
-  DocBloc(this.iDocImpl) : super(DocState.initial());
+  DocBloc({
+    required this.iDocImpl,
+  }) : super(const DocState.loading());
 
   @override
   Stream<DocState> mapEventToState(DocEvent event) async* {
     yield* event.map(
-      initial: (e) async* {},
-      close: (Close value) async* {},
+      started: (_) async* {
+        yield* _readDoc();
+      },
     );
   }
+
+  Stream<DocState> _readDoc() async* {
+    final docOrFail = await iDocImpl.readDoc();
+    yield docOrFail.fold(
+      (doc) {
+        final flowyDoc = FlowyDoc(doc: doc, data: _decodeToDocument(doc.data));
+        return DocState.loadDoc(flowyDoc);
+      },
+      (error) {
+        return DocState.loadFail(error);
+      },
+    );
+  }
+
+  Document _decodeToDocument(String text) {
+    final json = jsonDecode(text);
+    final document = Document.fromJson(json);
+    return document;
+  }
 }
 
 @freezed
-abstract class DocEvent with _$DocEvent {
-  const factory DocEvent.initial() = Initial;
-  const factory DocEvent.close() = Close;
+class DocEvent with _$DocEvent {
+  const factory DocEvent.started() = Started;
 }
 
 @freezed
-abstract class DocState implements _$DocState {
-  const factory DocState({
-    required bool isSaving,
-  }) = _DocState;
-
-  factory DocState.initial() => const DocState(
-        isSaving: false,
-      );
+class DocState with _$DocState {
+  const factory DocState.loading() = Loading;
+  const factory DocState.loadDoc(FlowyDoc doc) = LoadDoc;
+  const factory DocState.loadFail(WorkspaceError error) = LoadFail;
 }

+ 309 - 135
app_flowy/lib/workspace/application/doc/doc_bloc.freezed.dart

@@ -16,12 +16,8 @@ final _privateConstructorUsedError = UnsupportedError(
 class _$DocEventTearOff {
   const _$DocEventTearOff();
 
-  Initial initial() {
-    return const Initial();
-  }
-
-  Close close() {
-    return const Close();
+  Started started() {
+    return const Started();
   }
 }
 
@@ -32,27 +28,23 @@ const $DocEvent = _$DocEventTearOff();
 mixin _$DocEvent {
   @optionalTypeArgs
   TResult when<TResult extends Object?>({
-    required TResult Function() initial,
-    required TResult Function() close,
+    required TResult Function() started,
   }) =>
       throw _privateConstructorUsedError;
   @optionalTypeArgs
   TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? initial,
-    TResult Function()? close,
+    TResult Function()? started,
     required TResult orElse(),
   }) =>
       throw _privateConstructorUsedError;
   @optionalTypeArgs
   TResult map<TResult extends Object?>({
-    required TResult Function(Initial value) initial,
-    required TResult Function(Close value) close,
+    required TResult Function(Started value) started,
   }) =>
       throw _privateConstructorUsedError;
   @optionalTypeArgs
   TResult maybeMap<TResult extends Object?>({
-    TResult Function(Initial value)? initial,
-    TResult Function(Close value)? close,
+    TResult Function(Started value)? started,
     required TResult orElse(),
   }) =>
       throw _privateConstructorUsedError;
@@ -74,34 +66,34 @@ class _$DocEventCopyWithImpl<$Res> implements $DocEventCopyWith<$Res> {
 }
 
 /// @nodoc
-abstract class $InitialCopyWith<$Res> {
-  factory $InitialCopyWith(Initial value, $Res Function(Initial) then) =
-      _$InitialCopyWithImpl<$Res>;
+abstract class $StartedCopyWith<$Res> {
+  factory $StartedCopyWith(Started value, $Res Function(Started) then) =
+      _$StartedCopyWithImpl<$Res>;
 }
 
 /// @nodoc
-class _$InitialCopyWithImpl<$Res> extends _$DocEventCopyWithImpl<$Res>
-    implements $InitialCopyWith<$Res> {
-  _$InitialCopyWithImpl(Initial _value, $Res Function(Initial) _then)
-      : super(_value, (v) => _then(v as Initial));
+class _$StartedCopyWithImpl<$Res> extends _$DocEventCopyWithImpl<$Res>
+    implements $StartedCopyWith<$Res> {
+  _$StartedCopyWithImpl(Started _value, $Res Function(Started) _then)
+      : super(_value, (v) => _then(v as Started));
 
   @override
-  Initial get _value => super._value as Initial;
+  Started get _value => super._value as Started;
 }
 
 /// @nodoc
 
-class _$Initial implements Initial {
-  const _$Initial();
+class _$Started implements Started {
+  const _$Started();
 
   @override
   String toString() {
-    return 'DocEvent.initial()';
+    return 'DocEvent.started()';
   }
 
   @override
   bool operator ==(dynamic other) {
-    return identical(this, other) || (other is Initial);
+    return identical(this, other) || (other is Started);
   }
 
   @override
@@ -110,21 +102,19 @@ class _$Initial implements Initial {
   @override
   @optionalTypeArgs
   TResult when<TResult extends Object?>({
-    required TResult Function() initial,
-    required TResult Function() close,
+    required TResult Function() started,
   }) {
-    return initial();
+    return started();
   }
 
   @override
   @optionalTypeArgs
   TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? initial,
-    TResult Function()? close,
+    TResult Function()? started,
     required TResult orElse(),
   }) {
-    if (initial != null) {
-      return initial();
+    if (started != null) {
+      return started();
     }
     return orElse();
   }
@@ -132,59 +122,130 @@ class _$Initial implements Initial {
   @override
   @optionalTypeArgs
   TResult map<TResult extends Object?>({
-    required TResult Function(Initial value) initial,
-    required TResult Function(Close value) close,
+    required TResult Function(Started value) started,
   }) {
-    return initial(this);
+    return started(this);
   }
 
   @override
   @optionalTypeArgs
   TResult maybeMap<TResult extends Object?>({
-    TResult Function(Initial value)? initial,
-    TResult Function(Close value)? close,
+    TResult Function(Started value)? started,
     required TResult orElse(),
   }) {
-    if (initial != null) {
-      return initial(this);
+    if (started != null) {
+      return started(this);
     }
     return orElse();
   }
 }
 
-abstract class Initial implements DocEvent {
-  const factory Initial() = _$Initial;
+abstract class Started implements DocEvent {
+  const factory Started() = _$Started;
 }
 
 /// @nodoc
-abstract class $CloseCopyWith<$Res> {
-  factory $CloseCopyWith(Close value, $Res Function(Close) then) =
-      _$CloseCopyWithImpl<$Res>;
+class _$DocStateTearOff {
+  const _$DocStateTearOff();
+
+  Loading loading() {
+    return const Loading();
+  }
+
+  LoadDoc loadDoc(FlowyDoc doc) {
+    return LoadDoc(
+      doc,
+    );
+  }
+
+  LoadFail loadFail(WorkspaceError error) {
+    return LoadFail(
+      error,
+    );
+  }
 }
 
 /// @nodoc
-class _$CloseCopyWithImpl<$Res> extends _$DocEventCopyWithImpl<$Res>
-    implements $CloseCopyWith<$Res> {
-  _$CloseCopyWithImpl(Close _value, $Res Function(Close) _then)
-      : super(_value, (v) => _then(v as Close));
+const $DocState = _$DocStateTearOff();
+
+/// @nodoc
+mixin _$DocState {
+  @optionalTypeArgs
+  TResult when<TResult extends Object?>({
+    required TResult Function() loading,
+    required TResult Function(FlowyDoc doc) loadDoc,
+    required TResult Function(WorkspaceError error) loadFail,
+  }) =>
+      throw _privateConstructorUsedError;
+  @optionalTypeArgs
+  TResult maybeWhen<TResult extends Object?>({
+    TResult Function()? loading,
+    TResult Function(FlowyDoc doc)? loadDoc,
+    TResult Function(WorkspaceError error)? loadFail,
+    required TResult orElse(),
+  }) =>
+      throw _privateConstructorUsedError;
+  @optionalTypeArgs
+  TResult map<TResult extends Object?>({
+    required TResult Function(Loading value) loading,
+    required TResult Function(LoadDoc value) loadDoc,
+    required TResult Function(LoadFail value) loadFail,
+  }) =>
+      throw _privateConstructorUsedError;
+  @optionalTypeArgs
+  TResult maybeMap<TResult extends Object?>({
+    TResult Function(Loading value)? loading,
+    TResult Function(LoadDoc value)? loadDoc,
+    TResult Function(LoadFail value)? loadFail,
+    required TResult orElse(),
+  }) =>
+      throw _privateConstructorUsedError;
+}
+
+/// @nodoc
+abstract class $DocStateCopyWith<$Res> {
+  factory $DocStateCopyWith(DocState value, $Res Function(DocState) then) =
+      _$DocStateCopyWithImpl<$Res>;
+}
+
+/// @nodoc
+class _$DocStateCopyWithImpl<$Res> implements $DocStateCopyWith<$Res> {
+  _$DocStateCopyWithImpl(this._value, this._then);
+
+  final DocState _value;
+  // ignore: unused_field
+  final $Res Function(DocState) _then;
+}
+
+/// @nodoc
+abstract class $LoadingCopyWith<$Res> {
+  factory $LoadingCopyWith(Loading value, $Res Function(Loading) then) =
+      _$LoadingCopyWithImpl<$Res>;
+}
+
+/// @nodoc
+class _$LoadingCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res>
+    implements $LoadingCopyWith<$Res> {
+  _$LoadingCopyWithImpl(Loading _value, $Res Function(Loading) _then)
+      : super(_value, (v) => _then(v as Loading));
 
   @override
-  Close get _value => super._value as Close;
+  Loading get _value => super._value as Loading;
 }
 
 /// @nodoc
 
-class _$Close implements Close {
-  const _$Close();
+class _$Loading implements Loading {
+  const _$Loading();
 
   @override
   String toString() {
-    return 'DocEvent.close()';
+    return 'DocState.loading()';
   }
 
   @override
   bool operator ==(dynamic other) {
-    return identical(this, other) || (other is Close);
+    return identical(this, other) || (other is Loading);
   }
 
   @override
@@ -193,21 +254,23 @@ class _$Close implements Close {
   @override
   @optionalTypeArgs
   TResult when<TResult extends Object?>({
-    required TResult Function() initial,
-    required TResult Function() close,
+    required TResult Function() loading,
+    required TResult Function(FlowyDoc doc) loadDoc,
+    required TResult Function(WorkspaceError error) loadFail,
   }) {
-    return close();
+    return loading();
   }
 
   @override
   @optionalTypeArgs
   TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? initial,
-    TResult Function()? close,
+    TResult Function()? loading,
+    TResult Function(FlowyDoc doc)? loadDoc,
+    TResult Function(WorkspaceError error)? loadFail,
     required TResult orElse(),
   }) {
-    if (close != null) {
-      return close();
+    if (loading != null) {
+      return loading();
     }
     return orElse();
   }
@@ -215,150 +278,261 @@ class _$Close implements Close {
   @override
   @optionalTypeArgs
   TResult map<TResult extends Object?>({
-    required TResult Function(Initial value) initial,
-    required TResult Function(Close value) close,
+    required TResult Function(Loading value) loading,
+    required TResult Function(LoadDoc value) loadDoc,
+    required TResult Function(LoadFail value) loadFail,
   }) {
-    return close(this);
+    return loading(this);
   }
 
   @override
   @optionalTypeArgs
   TResult maybeMap<TResult extends Object?>({
-    TResult Function(Initial value)? initial,
-    TResult Function(Close value)? close,
+    TResult Function(Loading value)? loading,
+    TResult Function(LoadDoc value)? loadDoc,
+    TResult Function(LoadFail value)? loadFail,
     required TResult orElse(),
   }) {
-    if (close != null) {
-      return close(this);
+    if (loading != null) {
+      return loading(this);
     }
     return orElse();
   }
 }
 
-abstract class Close implements DocEvent {
-  const factory Close() = _$Close;
+abstract class Loading implements DocState {
+  const factory Loading() = _$Loading;
 }
 
 /// @nodoc
-class _$DocStateTearOff {
-  const _$DocStateTearOff();
+abstract class $LoadDocCopyWith<$Res> {
+  factory $LoadDocCopyWith(LoadDoc value, $Res Function(LoadDoc) then) =
+      _$LoadDocCopyWithImpl<$Res>;
+  $Res call({FlowyDoc doc});
+}
 
-  _DocState call({required bool isSaving}) {
-    return _DocState(
-      isSaving: isSaving,
-    );
+/// @nodoc
+class _$LoadDocCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res>
+    implements $LoadDocCopyWith<$Res> {
+  _$LoadDocCopyWithImpl(LoadDoc _value, $Res Function(LoadDoc) _then)
+      : super(_value, (v) => _then(v as LoadDoc));
+
+  @override
+  LoadDoc get _value => super._value as LoadDoc;
+
+  @override
+  $Res call({
+    Object? doc = freezed,
+  }) {
+    return _then(LoadDoc(
+      doc == freezed
+          ? _value.doc
+          : doc // ignore: cast_nullable_to_non_nullable
+              as FlowyDoc,
+    ));
   }
 }
 
 /// @nodoc
-const $DocState = _$DocStateTearOff();
 
-/// @nodoc
-mixin _$DocState {
-  bool get isSaving => throw _privateConstructorUsedError;
+class _$LoadDoc implements LoadDoc {
+  const _$LoadDoc(this.doc);
+
+  @override
+  final FlowyDoc doc;
+
+  @override
+  String toString() {
+    return 'DocState.loadDoc(doc: $doc)';
+  }
+
+  @override
+  bool operator ==(dynamic other) {
+    return identical(this, other) ||
+        (other is LoadDoc &&
+            (identical(other.doc, doc) ||
+                const DeepCollectionEquality().equals(other.doc, doc)));
+  }
+
+  @override
+  int get hashCode =>
+      runtimeType.hashCode ^ const DeepCollectionEquality().hash(doc);
 
   @JsonKey(ignore: true)
-  $DocStateCopyWith<DocState> get copyWith =>
-      throw _privateConstructorUsedError;
-}
+  @override
+  $LoadDocCopyWith<LoadDoc> get copyWith =>
+      _$LoadDocCopyWithImpl<LoadDoc>(this, _$identity);
 
-/// @nodoc
-abstract class $DocStateCopyWith<$Res> {
-  factory $DocStateCopyWith(DocState value, $Res Function(DocState) then) =
-      _$DocStateCopyWithImpl<$Res>;
-  $Res call({bool isSaving});
-}
+  @override
+  @optionalTypeArgs
+  TResult when<TResult extends Object?>({
+    required TResult Function() loading,
+    required TResult Function(FlowyDoc doc) loadDoc,
+    required TResult Function(WorkspaceError error) loadFail,
+  }) {
+    return loadDoc(doc);
+  }
 
-/// @nodoc
-class _$DocStateCopyWithImpl<$Res> implements $DocStateCopyWith<$Res> {
-  _$DocStateCopyWithImpl(this._value, this._then);
+  @override
+  @optionalTypeArgs
+  TResult maybeWhen<TResult extends Object?>({
+    TResult Function()? loading,
+    TResult Function(FlowyDoc doc)? loadDoc,
+    TResult Function(WorkspaceError error)? loadFail,
+    required TResult orElse(),
+  }) {
+    if (loadDoc != null) {
+      return loadDoc(doc);
+    }
+    return orElse();
+  }
 
-  final DocState _value;
-  // ignore: unused_field
-  final $Res Function(DocState) _then;
+  @override
+  @optionalTypeArgs
+  TResult map<TResult extends Object?>({
+    required TResult Function(Loading value) loading,
+    required TResult Function(LoadDoc value) loadDoc,
+    required TResult Function(LoadFail value) loadFail,
+  }) {
+    return loadDoc(this);
+  }
 
   @override
-  $Res call({
-    Object? isSaving = freezed,
+  @optionalTypeArgs
+  TResult maybeMap<TResult extends Object?>({
+    TResult Function(Loading value)? loading,
+    TResult Function(LoadDoc value)? loadDoc,
+    TResult Function(LoadFail value)? loadFail,
+    required TResult orElse(),
   }) {
-    return _then(_value.copyWith(
-      isSaving: isSaving == freezed
-          ? _value.isSaving
-          : isSaving // ignore: cast_nullable_to_non_nullable
-              as bool,
-    ));
+    if (loadDoc != null) {
+      return loadDoc(this);
+    }
+    return orElse();
   }
 }
 
+abstract class LoadDoc implements DocState {
+  const factory LoadDoc(FlowyDoc doc) = _$LoadDoc;
+
+  FlowyDoc get doc => throw _privateConstructorUsedError;
+  @JsonKey(ignore: true)
+  $LoadDocCopyWith<LoadDoc> get copyWith => throw _privateConstructorUsedError;
+}
+
 /// @nodoc
-abstract class _$DocStateCopyWith<$Res> implements $DocStateCopyWith<$Res> {
-  factory _$DocStateCopyWith(_DocState value, $Res Function(_DocState) then) =
-      __$DocStateCopyWithImpl<$Res>;
-  @override
-  $Res call({bool isSaving});
+abstract class $LoadFailCopyWith<$Res> {
+  factory $LoadFailCopyWith(LoadFail value, $Res Function(LoadFail) then) =
+      _$LoadFailCopyWithImpl<$Res>;
+  $Res call({WorkspaceError error});
 }
 
 /// @nodoc
-class __$DocStateCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res>
-    implements _$DocStateCopyWith<$Res> {
-  __$DocStateCopyWithImpl(_DocState _value, $Res Function(_DocState) _then)
-      : super(_value, (v) => _then(v as _DocState));
+class _$LoadFailCopyWithImpl<$Res> extends _$DocStateCopyWithImpl<$Res>
+    implements $LoadFailCopyWith<$Res> {
+  _$LoadFailCopyWithImpl(LoadFail _value, $Res Function(LoadFail) _then)
+      : super(_value, (v) => _then(v as LoadFail));
 
   @override
-  _DocState get _value => super._value as _DocState;
+  LoadFail get _value => super._value as LoadFail;
 
   @override
   $Res call({
-    Object? isSaving = freezed,
+    Object? error = freezed,
   }) {
-    return _then(_DocState(
-      isSaving: isSaving == freezed
-          ? _value.isSaving
-          : isSaving // ignore: cast_nullable_to_non_nullable
-              as bool,
+    return _then(LoadFail(
+      error == freezed
+          ? _value.error
+          : error // ignore: cast_nullable_to_non_nullable
+              as WorkspaceError,
     ));
   }
 }
 
 /// @nodoc
 
-class _$_DocState implements _DocState {
-  const _$_DocState({required this.isSaving});
+class _$LoadFail implements LoadFail {
+  const _$LoadFail(this.error);
 
   @override
-  final bool isSaving;
+  final WorkspaceError error;
 
   @override
   String toString() {
-    return 'DocState(isSaving: $isSaving)';
+    return 'DocState.loadFail(error: $error)';
   }
 
   @override
   bool operator ==(dynamic other) {
     return identical(this, other) ||
-        (other is _DocState &&
-            (identical(other.isSaving, isSaving) ||
-                const DeepCollectionEquality()
-                    .equals(other.isSaving, isSaving)));
+        (other is LoadFail &&
+            (identical(other.error, error) ||
+                const DeepCollectionEquality().equals(other.error, error)));
   }
 
   @override
   int get hashCode =>
-      runtimeType.hashCode ^ const DeepCollectionEquality().hash(isSaving);
+      runtimeType.hashCode ^ const DeepCollectionEquality().hash(error);
 
   @JsonKey(ignore: true)
   @override
-  _$DocStateCopyWith<_DocState> get copyWith =>
-      __$DocStateCopyWithImpl<_DocState>(this, _$identity);
-}
+  $LoadFailCopyWith<LoadFail> get copyWith =>
+      _$LoadFailCopyWithImpl<LoadFail>(this, _$identity);
 
-abstract class _DocState implements DocState {
-  const factory _DocState({required bool isSaving}) = _$_DocState;
+  @override
+  @optionalTypeArgs
+  TResult when<TResult extends Object?>({
+    required TResult Function() loading,
+    required TResult Function(FlowyDoc doc) loadDoc,
+    required TResult Function(WorkspaceError error) loadFail,
+  }) {
+    return loadFail(error);
+  }
 
   @override
-  bool get isSaving => throw _privateConstructorUsedError;
+  @optionalTypeArgs
+  TResult maybeWhen<TResult extends Object?>({
+    TResult Function()? loading,
+    TResult Function(FlowyDoc doc)? loadDoc,
+    TResult Function(WorkspaceError error)? loadFail,
+    required TResult orElse(),
+  }) {
+    if (loadFail != null) {
+      return loadFail(error);
+    }
+    return orElse();
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult map<TResult extends Object?>({
+    required TResult Function(Loading value) loading,
+    required TResult Function(LoadDoc value) loadDoc,
+    required TResult Function(LoadFail value) loadFail,
+  }) {
+    return loadFail(this);
+  }
+
   @override
+  @optionalTypeArgs
+  TResult maybeMap<TResult extends Object?>({
+    TResult Function(Loading value)? loading,
+    TResult Function(LoadDoc value)? loadDoc,
+    TResult Function(LoadFail value)? loadFail,
+    required TResult orElse(),
+  }) {
+    if (loadFail != null) {
+      return loadFail(this);
+    }
+    return orElse();
+  }
+}
+
+abstract class LoadFail implements DocState {
+  const factory LoadFail(WorkspaceError error) = _$LoadFail;
+
+  WorkspaceError get error => throw _privateConstructorUsedError;
   @JsonKey(ignore: true)
-  _$DocStateCopyWith<_DocState> get copyWith =>
+  $LoadFailCopyWith<LoadFail> get copyWith =>
       throw _privateConstructorUsedError;
 }

+ 43 - 0
app_flowy/lib/workspace/application/doc/doc_edit_bloc.dart

@@ -0,0 +1,43 @@
+import 'package:app_flowy/workspace/domain/i_doc.dart';
+import 'package:freezed_annotation/freezed_annotation.dart';
+import 'package:flutter_bloc/flutter_bloc.dart';
+
+part 'doc_edit_bloc.freezed.dart';
+
+class DocEditBloc extends Bloc<DocEditEvent, DocEditState> {
+  final IDoc iDocImpl;
+
+  DocEditBloc(this.iDocImpl) : super(DocEditState.initial());
+
+  @override
+  Stream<DocEditState> mapEventToState(DocEditEvent event) async* {
+    yield* event.map(
+        initial: (e) async* {},
+        close: (Close value) async* {},
+        changeset: (Changeset changeset) async* {
+          iDocImpl.updateWithChangeset(text: changeset.data);
+        },
+        save: (Save save) async* {
+          iDocImpl.saveDoc(text: save.data);
+        });
+  }
+}
+
+@freezed
+abstract class DocEditEvent with _$DocEditEvent {
+  const factory DocEditEvent.initial() = Initial;
+  const factory DocEditEvent.changeset(String data) = Changeset;
+  const factory DocEditEvent.save(String data) = Save;
+  const factory DocEditEvent.close() = Close;
+}
+
+@freezed
+abstract class DocEditState implements _$DocEditState {
+  const factory DocEditState({
+    required bool isSaving,
+  }) = _DocEditState;
+
+  factory DocEditState.initial() => const DocEditState(
+        isSaving: false,
+      );
+}

+ 646 - 0
app_flowy/lib/workspace/application/doc/doc_edit_bloc.freezed.dart

@@ -0,0 +1,646 @@
+// GENERATED CODE - DO NOT MODIFY BY HAND
+// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target
+
+part of 'doc_edit_bloc.dart';
+
+// **************************************************************************
+// FreezedGenerator
+// **************************************************************************
+
+T _$identity<T>(T value) => value;
+
+final _privateConstructorUsedError = UnsupportedError(
+    'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more informations: https://github.com/rrousselGit/freezed#custom-getters-and-methods');
+
+/// @nodoc
+class _$DocEditEventTearOff {
+  const _$DocEditEventTearOff();
+
+  Initial initial() {
+    return const Initial();
+  }
+
+  Changeset changeset(String data) {
+    return Changeset(
+      data,
+    );
+  }
+
+  Save save(String data) {
+    return Save(
+      data,
+    );
+  }
+
+  Close close() {
+    return const Close();
+  }
+}
+
+/// @nodoc
+const $DocEditEvent = _$DocEditEventTearOff();
+
+/// @nodoc
+mixin _$DocEditEvent {
+  @optionalTypeArgs
+  TResult when<TResult extends Object?>({
+    required TResult Function() initial,
+    required TResult Function(String data) changeset,
+    required TResult Function(String data) save,
+    required TResult Function() close,
+  }) =>
+      throw _privateConstructorUsedError;
+  @optionalTypeArgs
+  TResult maybeWhen<TResult extends Object?>({
+    TResult Function()? initial,
+    TResult Function(String data)? changeset,
+    TResult Function(String data)? save,
+    TResult Function()? close,
+    required TResult orElse(),
+  }) =>
+      throw _privateConstructorUsedError;
+  @optionalTypeArgs
+  TResult map<TResult extends Object?>({
+    required TResult Function(Initial value) initial,
+    required TResult Function(Changeset value) changeset,
+    required TResult Function(Save value) save,
+    required TResult Function(Close value) close,
+  }) =>
+      throw _privateConstructorUsedError;
+  @optionalTypeArgs
+  TResult maybeMap<TResult extends Object?>({
+    TResult Function(Initial value)? initial,
+    TResult Function(Changeset value)? changeset,
+    TResult Function(Save value)? save,
+    TResult Function(Close value)? close,
+    required TResult orElse(),
+  }) =>
+      throw _privateConstructorUsedError;
+}
+
+/// @nodoc
+abstract class $DocEditEventCopyWith<$Res> {
+  factory $DocEditEventCopyWith(
+          DocEditEvent value, $Res Function(DocEditEvent) then) =
+      _$DocEditEventCopyWithImpl<$Res>;
+}
+
+/// @nodoc
+class _$DocEditEventCopyWithImpl<$Res> implements $DocEditEventCopyWith<$Res> {
+  _$DocEditEventCopyWithImpl(this._value, this._then);
+
+  final DocEditEvent _value;
+  // ignore: unused_field
+  final $Res Function(DocEditEvent) _then;
+}
+
+/// @nodoc
+abstract class $InitialCopyWith<$Res> {
+  factory $InitialCopyWith(Initial value, $Res Function(Initial) then) =
+      _$InitialCopyWithImpl<$Res>;
+}
+
+/// @nodoc
+class _$InitialCopyWithImpl<$Res> extends _$DocEditEventCopyWithImpl<$Res>
+    implements $InitialCopyWith<$Res> {
+  _$InitialCopyWithImpl(Initial _value, $Res Function(Initial) _then)
+      : super(_value, (v) => _then(v as Initial));
+
+  @override
+  Initial get _value => super._value as Initial;
+}
+
+/// @nodoc
+
+class _$Initial implements Initial {
+  const _$Initial();
+
+  @override
+  String toString() {
+    return 'DocEditEvent.initial()';
+  }
+
+  @override
+  bool operator ==(dynamic other) {
+    return identical(this, other) || (other is Initial);
+  }
+
+  @override
+  int get hashCode => runtimeType.hashCode;
+
+  @override
+  @optionalTypeArgs
+  TResult when<TResult extends Object?>({
+    required TResult Function() initial,
+    required TResult Function(String data) changeset,
+    required TResult Function(String data) save,
+    required TResult Function() close,
+  }) {
+    return initial();
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult maybeWhen<TResult extends Object?>({
+    TResult Function()? initial,
+    TResult Function(String data)? changeset,
+    TResult Function(String data)? save,
+    TResult Function()? close,
+    required TResult orElse(),
+  }) {
+    if (initial != null) {
+      return initial();
+    }
+    return orElse();
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult map<TResult extends Object?>({
+    required TResult Function(Initial value) initial,
+    required TResult Function(Changeset value) changeset,
+    required TResult Function(Save value) save,
+    required TResult Function(Close value) close,
+  }) {
+    return initial(this);
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult maybeMap<TResult extends Object?>({
+    TResult Function(Initial value)? initial,
+    TResult Function(Changeset value)? changeset,
+    TResult Function(Save value)? save,
+    TResult Function(Close value)? close,
+    required TResult orElse(),
+  }) {
+    if (initial != null) {
+      return initial(this);
+    }
+    return orElse();
+  }
+}
+
+abstract class Initial implements DocEditEvent {
+  const factory Initial() = _$Initial;
+}
+
+/// @nodoc
+abstract class $ChangesetCopyWith<$Res> {
+  factory $ChangesetCopyWith(Changeset value, $Res Function(Changeset) then) =
+      _$ChangesetCopyWithImpl<$Res>;
+  $Res call({String data});
+}
+
+/// @nodoc
+class _$ChangesetCopyWithImpl<$Res> extends _$DocEditEventCopyWithImpl<$Res>
+    implements $ChangesetCopyWith<$Res> {
+  _$ChangesetCopyWithImpl(Changeset _value, $Res Function(Changeset) _then)
+      : super(_value, (v) => _then(v as Changeset));
+
+  @override
+  Changeset get _value => super._value as Changeset;
+
+  @override
+  $Res call({
+    Object? data = freezed,
+  }) {
+    return _then(Changeset(
+      data == freezed
+          ? _value.data
+          : data // ignore: cast_nullable_to_non_nullable
+              as String,
+    ));
+  }
+}
+
+/// @nodoc
+
+class _$Changeset implements Changeset {
+  const _$Changeset(this.data);
+
+  @override
+  final String data;
+
+  @override
+  String toString() {
+    return 'DocEditEvent.changeset(data: $data)';
+  }
+
+  @override
+  bool operator ==(dynamic other) {
+    return identical(this, other) ||
+        (other is Changeset &&
+            (identical(other.data, data) ||
+                const DeepCollectionEquality().equals(other.data, data)));
+  }
+
+  @override
+  int get hashCode =>
+      runtimeType.hashCode ^ const DeepCollectionEquality().hash(data);
+
+  @JsonKey(ignore: true)
+  @override
+  $ChangesetCopyWith<Changeset> get copyWith =>
+      _$ChangesetCopyWithImpl<Changeset>(this, _$identity);
+
+  @override
+  @optionalTypeArgs
+  TResult when<TResult extends Object?>({
+    required TResult Function() initial,
+    required TResult Function(String data) changeset,
+    required TResult Function(String data) save,
+    required TResult Function() close,
+  }) {
+    return changeset(data);
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult maybeWhen<TResult extends Object?>({
+    TResult Function()? initial,
+    TResult Function(String data)? changeset,
+    TResult Function(String data)? save,
+    TResult Function()? close,
+    required TResult orElse(),
+  }) {
+    if (changeset != null) {
+      return changeset(data);
+    }
+    return orElse();
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult map<TResult extends Object?>({
+    required TResult Function(Initial value) initial,
+    required TResult Function(Changeset value) changeset,
+    required TResult Function(Save value) save,
+    required TResult Function(Close value) close,
+  }) {
+    return changeset(this);
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult maybeMap<TResult extends Object?>({
+    TResult Function(Initial value)? initial,
+    TResult Function(Changeset value)? changeset,
+    TResult Function(Save value)? save,
+    TResult Function(Close value)? close,
+    required TResult orElse(),
+  }) {
+    if (changeset != null) {
+      return changeset(this);
+    }
+    return orElse();
+  }
+}
+
+abstract class Changeset implements DocEditEvent {
+  const factory Changeset(String data) = _$Changeset;
+
+  String get data => throw _privateConstructorUsedError;
+  @JsonKey(ignore: true)
+  $ChangesetCopyWith<Changeset> get copyWith =>
+      throw _privateConstructorUsedError;
+}
+
+/// @nodoc
+abstract class $SaveCopyWith<$Res> {
+  factory $SaveCopyWith(Save value, $Res Function(Save) then) =
+      _$SaveCopyWithImpl<$Res>;
+  $Res call({String data});
+}
+
+/// @nodoc
+class _$SaveCopyWithImpl<$Res> extends _$DocEditEventCopyWithImpl<$Res>
+    implements $SaveCopyWith<$Res> {
+  _$SaveCopyWithImpl(Save _value, $Res Function(Save) _then)
+      : super(_value, (v) => _then(v as Save));
+
+  @override
+  Save get _value => super._value as Save;
+
+  @override
+  $Res call({
+    Object? data = freezed,
+  }) {
+    return _then(Save(
+      data == freezed
+          ? _value.data
+          : data // ignore: cast_nullable_to_non_nullable
+              as String,
+    ));
+  }
+}
+
+/// @nodoc
+
+class _$Save implements Save {
+  const _$Save(this.data);
+
+  @override
+  final String data;
+
+  @override
+  String toString() {
+    return 'DocEditEvent.save(data: $data)';
+  }
+
+  @override
+  bool operator ==(dynamic other) {
+    return identical(this, other) ||
+        (other is Save &&
+            (identical(other.data, data) ||
+                const DeepCollectionEquality().equals(other.data, data)));
+  }
+
+  @override
+  int get hashCode =>
+      runtimeType.hashCode ^ const DeepCollectionEquality().hash(data);
+
+  @JsonKey(ignore: true)
+  @override
+  $SaveCopyWith<Save> get copyWith =>
+      _$SaveCopyWithImpl<Save>(this, _$identity);
+
+  @override
+  @optionalTypeArgs
+  TResult when<TResult extends Object?>({
+    required TResult Function() initial,
+    required TResult Function(String data) changeset,
+    required TResult Function(String data) save,
+    required TResult Function() close,
+  }) {
+    return save(data);
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult maybeWhen<TResult extends Object?>({
+    TResult Function()? initial,
+    TResult Function(String data)? changeset,
+    TResult Function(String data)? save,
+    TResult Function()? close,
+    required TResult orElse(),
+  }) {
+    if (save != null) {
+      return save(data);
+    }
+    return orElse();
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult map<TResult extends Object?>({
+    required TResult Function(Initial value) initial,
+    required TResult Function(Changeset value) changeset,
+    required TResult Function(Save value) save,
+    required TResult Function(Close value) close,
+  }) {
+    return save(this);
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult maybeMap<TResult extends Object?>({
+    TResult Function(Initial value)? initial,
+    TResult Function(Changeset value)? changeset,
+    TResult Function(Save value)? save,
+    TResult Function(Close value)? close,
+    required TResult orElse(),
+  }) {
+    if (save != null) {
+      return save(this);
+    }
+    return orElse();
+  }
+}
+
+abstract class Save implements DocEditEvent {
+  const factory Save(String data) = _$Save;
+
+  String get data => throw _privateConstructorUsedError;
+  @JsonKey(ignore: true)
+  $SaveCopyWith<Save> get copyWith => throw _privateConstructorUsedError;
+}
+
+/// @nodoc
+abstract class $CloseCopyWith<$Res> {
+  factory $CloseCopyWith(Close value, $Res Function(Close) then) =
+      _$CloseCopyWithImpl<$Res>;
+}
+
+/// @nodoc
+class _$CloseCopyWithImpl<$Res> extends _$DocEditEventCopyWithImpl<$Res>
+    implements $CloseCopyWith<$Res> {
+  _$CloseCopyWithImpl(Close _value, $Res Function(Close) _then)
+      : super(_value, (v) => _then(v as Close));
+
+  @override
+  Close get _value => super._value as Close;
+}
+
+/// @nodoc
+
+class _$Close implements Close {
+  const _$Close();
+
+  @override
+  String toString() {
+    return 'DocEditEvent.close()';
+  }
+
+  @override
+  bool operator ==(dynamic other) {
+    return identical(this, other) || (other is Close);
+  }
+
+  @override
+  int get hashCode => runtimeType.hashCode;
+
+  @override
+  @optionalTypeArgs
+  TResult when<TResult extends Object?>({
+    required TResult Function() initial,
+    required TResult Function(String data) changeset,
+    required TResult Function(String data) save,
+    required TResult Function() close,
+  }) {
+    return close();
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult maybeWhen<TResult extends Object?>({
+    TResult Function()? initial,
+    TResult Function(String data)? changeset,
+    TResult Function(String data)? save,
+    TResult Function()? close,
+    required TResult orElse(),
+  }) {
+    if (close != null) {
+      return close();
+    }
+    return orElse();
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult map<TResult extends Object?>({
+    required TResult Function(Initial value) initial,
+    required TResult Function(Changeset value) changeset,
+    required TResult Function(Save value) save,
+    required TResult Function(Close value) close,
+  }) {
+    return close(this);
+  }
+
+  @override
+  @optionalTypeArgs
+  TResult maybeMap<TResult extends Object?>({
+    TResult Function(Initial value)? initial,
+    TResult Function(Changeset value)? changeset,
+    TResult Function(Save value)? save,
+    TResult Function(Close value)? close,
+    required TResult orElse(),
+  }) {
+    if (close != null) {
+      return close(this);
+    }
+    return orElse();
+  }
+}
+
+abstract class Close implements DocEditEvent {
+  const factory Close() = _$Close;
+}
+
+/// @nodoc
+class _$DocEditStateTearOff {
+  const _$DocEditStateTearOff();
+
+  _DocEditState call({required bool isSaving}) {
+    return _DocEditState(
+      isSaving: isSaving,
+    );
+  }
+}
+
+/// @nodoc
+const $DocEditState = _$DocEditStateTearOff();
+
+/// @nodoc
+mixin _$DocEditState {
+  bool get isSaving => throw _privateConstructorUsedError;
+
+  @JsonKey(ignore: true)
+  $DocEditStateCopyWith<DocEditState> get copyWith =>
+      throw _privateConstructorUsedError;
+}
+
+/// @nodoc
+abstract class $DocEditStateCopyWith<$Res> {
+  factory $DocEditStateCopyWith(
+          DocEditState value, $Res Function(DocEditState) then) =
+      _$DocEditStateCopyWithImpl<$Res>;
+  $Res call({bool isSaving});
+}
+
+/// @nodoc
+class _$DocEditStateCopyWithImpl<$Res> implements $DocEditStateCopyWith<$Res> {
+  _$DocEditStateCopyWithImpl(this._value, this._then);
+
+  final DocEditState _value;
+  // ignore: unused_field
+  final $Res Function(DocEditState) _then;
+
+  @override
+  $Res call({
+    Object? isSaving = freezed,
+  }) {
+    return _then(_value.copyWith(
+      isSaving: isSaving == freezed
+          ? _value.isSaving
+          : isSaving // ignore: cast_nullable_to_non_nullable
+              as bool,
+    ));
+  }
+}
+
+/// @nodoc
+abstract class _$DocEditStateCopyWith<$Res>
+    implements $DocEditStateCopyWith<$Res> {
+  factory _$DocEditStateCopyWith(
+          _DocEditState value, $Res Function(_DocEditState) then) =
+      __$DocEditStateCopyWithImpl<$Res>;
+  @override
+  $Res call({bool isSaving});
+}
+
+/// @nodoc
+class __$DocEditStateCopyWithImpl<$Res> extends _$DocEditStateCopyWithImpl<$Res>
+    implements _$DocEditStateCopyWith<$Res> {
+  __$DocEditStateCopyWithImpl(
+      _DocEditState _value, $Res Function(_DocEditState) _then)
+      : super(_value, (v) => _then(v as _DocEditState));
+
+  @override
+  _DocEditState get _value => super._value as _DocEditState;
+
+  @override
+  $Res call({
+    Object? isSaving = freezed,
+  }) {
+    return _then(_DocEditState(
+      isSaving: isSaving == freezed
+          ? _value.isSaving
+          : isSaving // ignore: cast_nullable_to_non_nullable
+              as bool,
+    ));
+  }
+}
+
+/// @nodoc
+
+class _$_DocEditState implements _DocEditState {
+  const _$_DocEditState({required this.isSaving});
+
+  @override
+  final bool isSaving;
+
+  @override
+  String toString() {
+    return 'DocEditState(isSaving: $isSaving)';
+  }
+
+  @override
+  bool operator ==(dynamic other) {
+    return identical(this, other) ||
+        (other is _DocEditState &&
+            (identical(other.isSaving, isSaving) ||
+                const DeepCollectionEquality()
+                    .equals(other.isSaving, isSaving)));
+  }
+
+  @override
+  int get hashCode =>
+      runtimeType.hashCode ^ const DeepCollectionEquality().hash(isSaving);
+
+  @JsonKey(ignore: true)
+  @override
+  _$DocEditStateCopyWith<_DocEditState> get copyWith =>
+      __$DocEditStateCopyWithImpl<_DocEditState>(this, _$identity);
+}
+
+abstract class _DocEditState implements DocEditState {
+  const factory _DocEditState({required bool isSaving}) = _$_DocEditState;
+
+  @override
+  bool get isSaving => throw _privateConstructorUsedError;
+  @override
+  @JsonKey(ignore: true)
+  _$DocEditStateCopyWith<_DocEditState> get copyWith =>
+      throw _privateConstructorUsedError;
+}

+ 0 - 44
app_flowy/lib/workspace/application/view/doc_watch_bloc.dart

@@ -1,44 +0,0 @@
-import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
-import 'package:flutter_bloc/flutter_bloc.dart';
-import 'package:app_flowy/workspace/domain/i_doc.dart';
-import 'package:freezed_annotation/freezed_annotation.dart';
-part 'doc_watch_bloc.freezed.dart';
-
-class DocWatchBloc extends Bloc<DocWatchEvent, DocWatchState> {
-  final IDoc iDocImpl;
-
-  DocWatchBloc({
-    required this.iDocImpl,
-  }) : super(const DocWatchState.loading());
-
-  @override
-  Stream<DocWatchState> mapEventToState(DocWatchEvent event) async* {
-    yield* event.map(
-      started: (_) async* {
-        yield* _readDoc();
-      },
-    );
-  }
-
-  Stream<DocWatchState> _readDoc() async* {
-    final docOrFail = await iDocImpl.readDoc();
-    yield docOrFail.fold(
-      (doc) => DocWatchState.loadDoc(doc),
-      (error) {
-        return DocWatchState.loadFail(error);
-      },
-    );
-  }
-}
-
-@freezed
-class DocWatchEvent with _$DocWatchEvent {
-  const factory DocWatchEvent.started() = Started;
-}
-
-@freezed
-class DocWatchState with _$DocWatchState {
-  const factory DocWatchState.loading() = Loading;
-  const factory DocWatchState.loadDoc(FlowyDoc doc) = LoadDoc;
-  const factory DocWatchState.loadFail(WorkspaceError error) = LoadFail;
-}

+ 0 - 542
app_flowy/lib/workspace/application/view/doc_watch_bloc.freezed.dart

@@ -1,542 +0,0 @@
-// GENERATED CODE - DO NOT MODIFY BY HAND
-// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target
-
-part of 'doc_watch_bloc.dart';
-
-// **************************************************************************
-// FreezedGenerator
-// **************************************************************************
-
-T _$identity<T>(T value) => value;
-
-final _privateConstructorUsedError = UnsupportedError(
-    'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more informations: https://github.com/rrousselGit/freezed#custom-getters-and-methods');
-
-/// @nodoc
-class _$DocWatchEventTearOff {
-  const _$DocWatchEventTearOff();
-
-  Started started() {
-    return const Started();
-  }
-}
-
-/// @nodoc
-const $DocWatchEvent = _$DocWatchEventTearOff();
-
-/// @nodoc
-mixin _$DocWatchEvent {
-  @optionalTypeArgs
-  TResult when<TResult extends Object?>({
-    required TResult Function() started,
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? started,
-    required TResult orElse(),
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult map<TResult extends Object?>({
-    required TResult Function(Started value) started,
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult maybeMap<TResult extends Object?>({
-    TResult Function(Started value)? started,
-    required TResult orElse(),
-  }) =>
-      throw _privateConstructorUsedError;
-}
-
-/// @nodoc
-abstract class $DocWatchEventCopyWith<$Res> {
-  factory $DocWatchEventCopyWith(
-          DocWatchEvent value, $Res Function(DocWatchEvent) then) =
-      _$DocWatchEventCopyWithImpl<$Res>;
-}
-
-/// @nodoc
-class _$DocWatchEventCopyWithImpl<$Res>
-    implements $DocWatchEventCopyWith<$Res> {
-  _$DocWatchEventCopyWithImpl(this._value, this._then);
-
-  final DocWatchEvent _value;
-  // ignore: unused_field
-  final $Res Function(DocWatchEvent) _then;
-}
-
-/// @nodoc
-abstract class $StartedCopyWith<$Res> {
-  factory $StartedCopyWith(Started value, $Res Function(Started) then) =
-      _$StartedCopyWithImpl<$Res>;
-}
-
-/// @nodoc
-class _$StartedCopyWithImpl<$Res> extends _$DocWatchEventCopyWithImpl<$Res>
-    implements $StartedCopyWith<$Res> {
-  _$StartedCopyWithImpl(Started _value, $Res Function(Started) _then)
-      : super(_value, (v) => _then(v as Started));
-
-  @override
-  Started get _value => super._value as Started;
-}
-
-/// @nodoc
-
-class _$Started implements Started {
-  const _$Started();
-
-  @override
-  String toString() {
-    return 'DocWatchEvent.started()';
-  }
-
-  @override
-  bool operator ==(dynamic other) {
-    return identical(this, other) || (other is Started);
-  }
-
-  @override
-  int get hashCode => runtimeType.hashCode;
-
-  @override
-  @optionalTypeArgs
-  TResult when<TResult extends Object?>({
-    required TResult Function() started,
-  }) {
-    return started();
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? started,
-    required TResult orElse(),
-  }) {
-    if (started != null) {
-      return started();
-    }
-    return orElse();
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult map<TResult extends Object?>({
-    required TResult Function(Started value) started,
-  }) {
-    return started(this);
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeMap<TResult extends Object?>({
-    TResult Function(Started value)? started,
-    required TResult orElse(),
-  }) {
-    if (started != null) {
-      return started(this);
-    }
-    return orElse();
-  }
-}
-
-abstract class Started implements DocWatchEvent {
-  const factory Started() = _$Started;
-}
-
-/// @nodoc
-class _$DocWatchStateTearOff {
-  const _$DocWatchStateTearOff();
-
-  Loading loading() {
-    return const Loading();
-  }
-
-  LoadDoc loadDoc(FlowyDoc doc) {
-    return LoadDoc(
-      doc,
-    );
-  }
-
-  LoadFail loadFail(WorkspaceError error) {
-    return LoadFail(
-      error,
-    );
-  }
-}
-
-/// @nodoc
-const $DocWatchState = _$DocWatchStateTearOff();
-
-/// @nodoc
-mixin _$DocWatchState {
-  @optionalTypeArgs
-  TResult when<TResult extends Object?>({
-    required TResult Function() loading,
-    required TResult Function(FlowyDoc doc) loadDoc,
-    required TResult Function(WorkspaceError error) loadFail,
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? loading,
-    TResult Function(FlowyDoc doc)? loadDoc,
-    TResult Function(WorkspaceError error)? loadFail,
-    required TResult orElse(),
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult map<TResult extends Object?>({
-    required TResult Function(Loading value) loading,
-    required TResult Function(LoadDoc value) loadDoc,
-    required TResult Function(LoadFail value) loadFail,
-  }) =>
-      throw _privateConstructorUsedError;
-  @optionalTypeArgs
-  TResult maybeMap<TResult extends Object?>({
-    TResult Function(Loading value)? loading,
-    TResult Function(LoadDoc value)? loadDoc,
-    TResult Function(LoadFail value)? loadFail,
-    required TResult orElse(),
-  }) =>
-      throw _privateConstructorUsedError;
-}
-
-/// @nodoc
-abstract class $DocWatchStateCopyWith<$Res> {
-  factory $DocWatchStateCopyWith(
-          DocWatchState value, $Res Function(DocWatchState) then) =
-      _$DocWatchStateCopyWithImpl<$Res>;
-}
-
-/// @nodoc
-class _$DocWatchStateCopyWithImpl<$Res>
-    implements $DocWatchStateCopyWith<$Res> {
-  _$DocWatchStateCopyWithImpl(this._value, this._then);
-
-  final DocWatchState _value;
-  // ignore: unused_field
-  final $Res Function(DocWatchState) _then;
-}
-
-/// @nodoc
-abstract class $LoadingCopyWith<$Res> {
-  factory $LoadingCopyWith(Loading value, $Res Function(Loading) then) =
-      _$LoadingCopyWithImpl<$Res>;
-}
-
-/// @nodoc
-class _$LoadingCopyWithImpl<$Res> extends _$DocWatchStateCopyWithImpl<$Res>
-    implements $LoadingCopyWith<$Res> {
-  _$LoadingCopyWithImpl(Loading _value, $Res Function(Loading) _then)
-      : super(_value, (v) => _then(v as Loading));
-
-  @override
-  Loading get _value => super._value as Loading;
-}
-
-/// @nodoc
-
-class _$Loading implements Loading {
-  const _$Loading();
-
-  @override
-  String toString() {
-    return 'DocWatchState.loading()';
-  }
-
-  @override
-  bool operator ==(dynamic other) {
-    return identical(this, other) || (other is Loading);
-  }
-
-  @override
-  int get hashCode => runtimeType.hashCode;
-
-  @override
-  @optionalTypeArgs
-  TResult when<TResult extends Object?>({
-    required TResult Function() loading,
-    required TResult Function(FlowyDoc doc) loadDoc,
-    required TResult Function(WorkspaceError error) loadFail,
-  }) {
-    return loading();
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? loading,
-    TResult Function(FlowyDoc doc)? loadDoc,
-    TResult Function(WorkspaceError error)? loadFail,
-    required TResult orElse(),
-  }) {
-    if (loading != null) {
-      return loading();
-    }
-    return orElse();
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult map<TResult extends Object?>({
-    required TResult Function(Loading value) loading,
-    required TResult Function(LoadDoc value) loadDoc,
-    required TResult Function(LoadFail value) loadFail,
-  }) {
-    return loading(this);
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeMap<TResult extends Object?>({
-    TResult Function(Loading value)? loading,
-    TResult Function(LoadDoc value)? loadDoc,
-    TResult Function(LoadFail value)? loadFail,
-    required TResult orElse(),
-  }) {
-    if (loading != null) {
-      return loading(this);
-    }
-    return orElse();
-  }
-}
-
-abstract class Loading implements DocWatchState {
-  const factory Loading() = _$Loading;
-}
-
-/// @nodoc
-abstract class $LoadDocCopyWith<$Res> {
-  factory $LoadDocCopyWith(LoadDoc value, $Res Function(LoadDoc) then) =
-      _$LoadDocCopyWithImpl<$Res>;
-  $Res call({FlowyDoc doc});
-}
-
-/// @nodoc
-class _$LoadDocCopyWithImpl<$Res> extends _$DocWatchStateCopyWithImpl<$Res>
-    implements $LoadDocCopyWith<$Res> {
-  _$LoadDocCopyWithImpl(LoadDoc _value, $Res Function(LoadDoc) _then)
-      : super(_value, (v) => _then(v as LoadDoc));
-
-  @override
-  LoadDoc get _value => super._value as LoadDoc;
-
-  @override
-  $Res call({
-    Object? doc = freezed,
-  }) {
-    return _then(LoadDoc(
-      doc == freezed
-          ? _value.doc
-          : doc // ignore: cast_nullable_to_non_nullable
-              as FlowyDoc,
-    ));
-  }
-}
-
-/// @nodoc
-
-class _$LoadDoc implements LoadDoc {
-  const _$LoadDoc(this.doc);
-
-  @override
-  final FlowyDoc doc;
-
-  @override
-  String toString() {
-    return 'DocWatchState.loadDoc(doc: $doc)';
-  }
-
-  @override
-  bool operator ==(dynamic other) {
-    return identical(this, other) ||
-        (other is LoadDoc &&
-            (identical(other.doc, doc) ||
-                const DeepCollectionEquality().equals(other.doc, doc)));
-  }
-
-  @override
-  int get hashCode =>
-      runtimeType.hashCode ^ const DeepCollectionEquality().hash(doc);
-
-  @JsonKey(ignore: true)
-  @override
-  $LoadDocCopyWith<LoadDoc> get copyWith =>
-      _$LoadDocCopyWithImpl<LoadDoc>(this, _$identity);
-
-  @override
-  @optionalTypeArgs
-  TResult when<TResult extends Object?>({
-    required TResult Function() loading,
-    required TResult Function(FlowyDoc doc) loadDoc,
-    required TResult Function(WorkspaceError error) loadFail,
-  }) {
-    return loadDoc(doc);
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? loading,
-    TResult Function(FlowyDoc doc)? loadDoc,
-    TResult Function(WorkspaceError error)? loadFail,
-    required TResult orElse(),
-  }) {
-    if (loadDoc != null) {
-      return loadDoc(doc);
-    }
-    return orElse();
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult map<TResult extends Object?>({
-    required TResult Function(Loading value) loading,
-    required TResult Function(LoadDoc value) loadDoc,
-    required TResult Function(LoadFail value) loadFail,
-  }) {
-    return loadDoc(this);
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeMap<TResult extends Object?>({
-    TResult Function(Loading value)? loading,
-    TResult Function(LoadDoc value)? loadDoc,
-    TResult Function(LoadFail value)? loadFail,
-    required TResult orElse(),
-  }) {
-    if (loadDoc != null) {
-      return loadDoc(this);
-    }
-    return orElse();
-  }
-}
-
-abstract class LoadDoc implements DocWatchState {
-  const factory LoadDoc(FlowyDoc doc) = _$LoadDoc;
-
-  FlowyDoc get doc => throw _privateConstructorUsedError;
-  @JsonKey(ignore: true)
-  $LoadDocCopyWith<LoadDoc> get copyWith => throw _privateConstructorUsedError;
-}
-
-/// @nodoc
-abstract class $LoadFailCopyWith<$Res> {
-  factory $LoadFailCopyWith(LoadFail value, $Res Function(LoadFail) then) =
-      _$LoadFailCopyWithImpl<$Res>;
-  $Res call({WorkspaceError error});
-}
-
-/// @nodoc
-class _$LoadFailCopyWithImpl<$Res> extends _$DocWatchStateCopyWithImpl<$Res>
-    implements $LoadFailCopyWith<$Res> {
-  _$LoadFailCopyWithImpl(LoadFail _value, $Res Function(LoadFail) _then)
-      : super(_value, (v) => _then(v as LoadFail));
-
-  @override
-  LoadFail get _value => super._value as LoadFail;
-
-  @override
-  $Res call({
-    Object? error = freezed,
-  }) {
-    return _then(LoadFail(
-      error == freezed
-          ? _value.error
-          : error // ignore: cast_nullable_to_non_nullable
-              as WorkspaceError,
-    ));
-  }
-}
-
-/// @nodoc
-
-class _$LoadFail implements LoadFail {
-  const _$LoadFail(this.error);
-
-  @override
-  final WorkspaceError error;
-
-  @override
-  String toString() {
-    return 'DocWatchState.loadFail(error: $error)';
-  }
-
-  @override
-  bool operator ==(dynamic other) {
-    return identical(this, other) ||
-        (other is LoadFail &&
-            (identical(other.error, error) ||
-                const DeepCollectionEquality().equals(other.error, error)));
-  }
-
-  @override
-  int get hashCode =>
-      runtimeType.hashCode ^ const DeepCollectionEquality().hash(error);
-
-  @JsonKey(ignore: true)
-  @override
-  $LoadFailCopyWith<LoadFail> get copyWith =>
-      _$LoadFailCopyWithImpl<LoadFail>(this, _$identity);
-
-  @override
-  @optionalTypeArgs
-  TResult when<TResult extends Object?>({
-    required TResult Function() loading,
-    required TResult Function(FlowyDoc doc) loadDoc,
-    required TResult Function(WorkspaceError error) loadFail,
-  }) {
-    return loadFail(error);
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeWhen<TResult extends Object?>({
-    TResult Function()? loading,
-    TResult Function(FlowyDoc doc)? loadDoc,
-    TResult Function(WorkspaceError error)? loadFail,
-    required TResult orElse(),
-  }) {
-    if (loadFail != null) {
-      return loadFail(error);
-    }
-    return orElse();
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult map<TResult extends Object?>({
-    required TResult Function(Loading value) loading,
-    required TResult Function(LoadDoc value) loadDoc,
-    required TResult Function(LoadFail value) loadFail,
-  }) {
-    return loadFail(this);
-  }
-
-  @override
-  @optionalTypeArgs
-  TResult maybeMap<TResult extends Object?>({
-    TResult Function(Loading value)? loading,
-    TResult Function(LoadDoc value)? loadDoc,
-    TResult Function(LoadFail value)? loadFail,
-    required TResult orElse(),
-  }) {
-    if (loadFail != null) {
-      return loadFail(this);
-    }
-    return orElse();
-  }
-}
-
-abstract class LoadFail implements DocWatchState {
-  const factory LoadFail(WorkspaceError error) = _$LoadFail;
-
-  WorkspaceError get error => throw _privateConstructorUsedError;
-  @JsonKey(ignore: true)
-  $LoadFailCopyWith<LoadFail> get copyWith =>
-      throw _privateConstructorUsedError;
-}

+ 4 - 2
app_flowy/lib/workspace/domain/i_doc.dart

@@ -8,10 +8,12 @@ class FlowyDoc {
   final Document data;
 
   FlowyDoc({required this.doc, required this.data});
+  String get id => doc.id;
 }
 
 abstract class IDoc {
-  Future<Either<FlowyDoc, WorkspaceError>> readDoc();
-  Future<Either<Unit, WorkspaceError>> updateDoc({String? text});
+  Future<Either<Doc, WorkspaceError>> readDoc();
+  Future<Either<Unit, WorkspaceError>> saveDoc({String? text});
+  Future<Either<Unit, WorkspaceError>> updateWithChangeset({String? text});
   Future<Either<Unit, WorkspaceError>> closeDoc();
 }

+ 5 - 5
app_flowy/lib/workspace/infrastructure/deps_resolver.dart

@@ -1,10 +1,10 @@
 import 'package:app_flowy/workspace/application/app/app_bloc.dart';
 import 'package:app_flowy/workspace/application/app/app_watch_bloc.dart';
 import 'package:app_flowy/workspace/application/doc/doc_bloc.dart';
+import 'package:app_flowy/workspace/application/doc/doc_edit_bloc.dart';
 import 'package:app_flowy/workspace/application/menu/menu_bloc.dart';
 import 'package:app_flowy/workspace/application/menu/menu_user_bloc.dart';
 import 'package:app_flowy/workspace/application/menu/menu_watch.dart';
-import 'package:app_flowy/workspace/application/view/doc_watch_bloc.dart';
 import 'package:app_flowy/workspace/application/view/view_bloc.dart';
 import 'package:app_flowy/workspace/application/view/view_list_bloc.dart';
 import 'package:app_flowy/workspace/application/workspace/welcome_bloc.dart';
@@ -82,11 +82,11 @@ class HomeDepsResolver {
     getIt.registerFactoryParam<ViewBloc, String, void>(
         (viewId, _) => ViewBloc(iViewImpl: getIt<IView>(param1: viewId)));
 
-    getIt.registerFactoryParam<DocWatchBloc, String, void>(
-        (docId, _) => DocWatchBloc(iDocImpl: getIt<IDoc>(param1: docId)));
-
     getIt.registerFactoryParam<DocBloc, String, void>(
-        (docId, _) => DocBloc(getIt<IDoc>(param1: docId)));
+        (docId, _) => DocBloc(iDocImpl: getIt<IDoc>(param1: docId)));
+
+    getIt.registerFactoryParam<DocEditBloc, String, void>(
+        (docId, _) => DocEditBloc(getIt<IDoc>(param1: docId)));
 
     // editor
     getIt.registerFactoryParam<EditorPersistence, String, void>(

+ 10 - 10
app_flowy/lib/workspace/infrastructure/i_doc_impl.dart

@@ -4,6 +4,8 @@ import 'package:dartz/dartz.dart';
 import 'package:flowy_editor/flowy_editor.dart';
 import 'package:app_flowy/workspace/domain/i_doc.dart';
 import 'package:app_flowy/workspace/infrastructure/repos/doc_repo.dart';
+import 'package:flowy_infra/flowy_logger.dart';
+import 'package:flowy_sdk/protobuf/flowy-document/doc.pb.dart';
 import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
 
 class IDocImpl extends IDoc {
@@ -17,24 +19,21 @@ class IDocImpl extends IDoc {
   }
 
   @override
-  Future<Either<FlowyDoc, WorkspaceError>> readDoc() async {
+  Future<Either<Doc, WorkspaceError>> readDoc() async {
     final docOrFail = await repo.readDoc();
-
-    return docOrFail.fold((doc) {
-      return left(FlowyDoc(doc: doc, data: _decodeToDocument(doc.data)));
-    }, (error) => right(error));
+    return docOrFail;
   }
 
   @override
-  Future<Either<Unit, WorkspaceError>> updateDoc({String? text}) {
+  Future<Either<Unit, WorkspaceError>> saveDoc({String? text}) {
+    Log.debug("Saving doc");
     final json = jsonEncode(text ?? "");
     return repo.updateDoc(text: json);
   }
 
-  Document _decodeToDocument(String text) {
-    final json = jsonDecode(text);
-    final document = Document.fromJson(json);
-    return document;
+  @override
+  Future<Either<Unit, WorkspaceError>> updateWithChangeset({String? text}) {
+    return repo.updateWithChangeset(text: text);
   }
 }
 
@@ -46,6 +45,7 @@ class EditorPersistenceImpl extends EditorPersistence {
 
   @override
   Future<bool> save(List<dynamic> jsonList) async {
+    Log.debug("Saving doc");
     final json = jsonEncode(jsonList);
     return repo.updateDoc(text: json).then((result) {
       return result.fold(

+ 4 - 0
app_flowy/lib/workspace/infrastructure/repos/doc_repo.dart

@@ -23,6 +23,10 @@ class DocRepository {
     return WorkspaceEventUpdateViewData(request).send();
   }
 
+  Future<Either<Unit, WorkspaceError>> updateWithChangeset({String? text}) {
+    throw UnimplementedError();
+  }
+
   Future<Either<Unit, WorkspaceError>> closeDoc(
       {String? name, String? desc, String? text}) {
     throw UnimplementedError();

+ 5 - 1
app_flowy/lib/workspace/presentation/doc/doc_page.dart → app_flowy/lib/workspace/presentation/doc/doc_stack_page.dart

@@ -2,6 +2,7 @@ import 'package:app_flowy/startup/startup.dart';
 import 'package:app_flowy/workspace/application/view/doc_watch_bloc.dart';
 import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart';
 import 'package:app_flowy/workspace/presentation/doc/editor_page.dart';
+import 'package:flowy_infra/flowy_logger.dart';
 import 'package:flowy_infra_ui/widget/error_page.dart';
 import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
 import 'package:flutter/material.dart';
@@ -32,7 +33,10 @@ class _DocPageState extends State<DocPage> {
         return state.map(
           loading: (_) => const FlowyProgressIndicator(),
           loadDoc: (s) => EditorPage(doc: s.doc),
-          loadFail: (s) => FlowyErrorPage(s.error.toString()),
+          loadFail: (s) {
+            Log.error("$s");
+            return FlowyErrorPage(s.error.toString());
+          },
         );
       }),
     );

+ 4 - 4
app_flowy/lib/workspace/presentation/doc/editor_page.dart

@@ -1,7 +1,7 @@
 import 'dart:io';
 
 import 'package:app_flowy/startup/startup.dart';
-import 'package:app_flowy/workspace/application/doc/doc_bloc.dart';
+import 'package:app_flowy/workspace/application/doc/doc_edit_bloc.dart';
 import 'package:app_flowy/workspace/domain/i_doc.dart';
 import 'package:flowy_editor/flowy_editor.dart';
 import 'package:flutter/material.dart';
@@ -17,15 +17,15 @@ class EditorPage extends StatelessWidget {
     controller = EditorController(
       document: doc.data,
       selection: const TextSelection.collapsed(offset: 0),
-      persistence: getIt<EditorPersistence>(param1: doc.doc.id),
+      persistence: getIt<EditorPersistence>(param1: doc.id),
     );
   }
 
   @override
   Widget build(BuildContext context) {
     return BlocProvider(
-      create: (context) => getIt<DocBloc>(param1: doc.doc.id),
-      child: BlocBuilder<DocBloc, DocState>(
+      create: (context) => getIt<DocEditBloc>(param1: doc.id),
+      child: BlocBuilder<DocEditBloc, DocEditState>(
         builder: (ctx, state) {
           return Column(
             mainAxisAlignment: MainAxisAlignment.spaceBetween,