11import 'dart:convert' ;
2+ import 'package:appflowy/plugins/document/presentation/plugins/cover/cover_node_widget.dart' ;
23import 'package:appflowy/plugins/trash/application/trash_service.dart' ;
34import 'package:appflowy/user/application/user_service.dart' ;
45import 'package:appflowy/workspace/application/view/view_listener.dart' ;
56import 'package:appflowy/plugins/document/application/doc_service.dart' ;
7+ import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart' ;
68import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pbserver.dart' ;
79import 'package:appflowy_editor/appflowy_editor.dart'
8- show EditorState, Document, Transaction;
10+ show EditorState, Document, Transaction, Node ;
911import 'package:appflowy_backend/protobuf/flowy-folder/trash.pb.dart' ;
1012import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart' ;
1113import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart' ;
1214import 'package:appflowy_backend/log.dart' ;
15+ import 'package:flutter/foundation.dart' ;
1316import 'package:flutter_bloc/flutter_bloc.dart' ;
1417import 'package:freezed_annotation/freezed_annotation.dart' ;
1518import 'package:dartz/dartz.dart' ;
@@ -78,29 +81,27 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
7881 Future <void > _initial (Initial value, Emitter <DocumentState > emit) async {
7982 final userProfile = await UserBackendService .getCurrentUserProfile ();
8083 if (userProfile.isRight ()) {
81- emit (
84+ return emit (
8285 state.copyWith (
8386 loadingState: DocumentLoadingState .finish (
8487 right (userProfile.asRight ()),
8588 ),
8689 ),
8790 );
88- return ;
8991 }
9092 final result = await _documentService.openDocument (view: view);
91- result.fold (
92- (documentData) {
93- final document = Document .fromJson (jsonDecode (documentData.content));
94- editorState = EditorState (document: document);
95- _listenOnDocumentChange ();
96- emit (
97- state.copyWith (
98- loadingState: DocumentLoadingState .finish (left (unit)),
99- userProfilePB: userProfile.asLeft (),
100- ),
101- );
93+ return result.fold (
94+ (documentData) async {
95+ await _initEditorState (documentData).whenComplete (() {
96+ emit (
97+ state.copyWith (
98+ loadingState: DocumentLoadingState .finish (left (unit)),
99+ userProfilePB: userProfile.asLeft (),
100+ ),
101+ );
102+ });
102103 },
103- (err) {
104+ (err) async {
104105 emit (
105106 state.copyWith (
106107 loadingState: DocumentLoadingState .finish (right (err)),
@@ -127,8 +128,13 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
127128 );
128129 }
129130
130- void _listenOnDocumentChange () {
131- _subscription = editorState? .transactionStream.listen ((transaction) {
131+ Future <void > _initEditorState (DocumentDataPB documentData) async {
132+ final document = Document .fromJson (jsonDecode (documentData.content));
133+ final editorState = EditorState (document: document);
134+ this .editorState = editorState;
135+
136+ // listen on document change
137+ _subscription = editorState.transactionStream.listen ((transaction) {
132138 final json = jsonEncode (TransactionAdaptor (transaction).toJson ());
133139 _documentService
134140 .applyEdit (docId: view.id, operations: json)
@@ -139,6 +145,15 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
139145 );
140146 });
141147 });
148+ // log
149+ if (kDebugMode) {
150+ editorState.logConfiguration.handler = (log) {
151+ Log .debug (log);
152+ };
153+ }
154+ // migration
155+ final migration = DocumentMigration (editorState: editorState);
156+ await migration.apply ();
142157 }
143158}
144159
@@ -215,3 +230,33 @@ class TransactionAdaptor {
215230 return json;
216231 }
217232}
233+
234+ class DocumentMigration {
235+ const DocumentMigration ({
236+ required this .editorState,
237+ });
238+
239+ final EditorState editorState;
240+
241+ /// Migrate the document to the latest version.
242+ Future <void > apply () async {
243+ final transaction = editorState.transaction;
244+
245+ // A temporary solution to migrate the document to the latest version.
246+ // Once the editor is stable, we can remove this.
247+
248+ // cover plugin
249+ if (editorState.document.nodeAtPath ([0 ])? .type != kCoverType) {
250+ transaction.insertNode (
251+ [0 ],
252+ Node (type: kCoverType),
253+ );
254+ }
255+
256+ transaction.afterSelection = null ;
257+
258+ if (transaction.operations.isNotEmpty) {
259+ editorState.apply (transaction);
260+ }
261+ }
262+ }
0 commit comments