diff --git a/app/lib/features/editor/views/editor_page.dart b/app/lib/features/editor/views/editor_page.dart index ed0e205..7c0970c 100644 --- a/app/lib/features/editor/views/editor_page.dart +++ b/app/lib/features/editor/views/editor_page.dart @@ -37,12 +37,26 @@ import '../widgets/brush_panel.dart'; import '../widgets/dot_grid_painter.dart'; /// 手账编辑器页面 -class EditorPage extends StatelessWidget { +class EditorPage extends StatefulWidget { final String? journalId; final String? templateId; const EditorPage({super.key, this.journalId, this.templateId}); + @override + State createState() => _EditorPageState(); +} + +class _EditorPageState extends State { + /// 跟踪已保存的日记 ID — 新建日记首次保存后赋值 + String? _savedJournalId; + + @override + void initState() { + super.initState(); + _savedJournalId = widget.journalId; + } + @override Widget build(BuildContext context) { // 从 Provider 树获取 JournalRepository(IsarJournalRepository) @@ -50,10 +64,6 @@ class EditorPage extends StatelessWidget { // 从 Provider 树获取 SyncEngine(同步到后端) final syncEngine = context.read(); - // 可变闭包变量:跟踪已保存的日记 ID - // 新建日记首次保存后赋值,后续自动更新使用此 ID - String? savedJournalId = journalId; - return BlocProvider( create: (_) => EditorBloc( onSave: (state) async { @@ -66,7 +76,7 @@ class EditorPage extends StatelessWidget { } await _persistState( - repo, state, (id) => savedJournalId = id, savedJournalId, + repo, state, (id) => _savedJournalId = id, _savedJournalId, syncEngine: syncEngine, authorId: authorId, ); @@ -76,12 +86,12 @@ class EditorPage extends StatelessWidget { }, ), child: _EditorView( - journalId: journalId, - templateId: templateId, - savedJournalId: savedJournalId, + journalId: widget.journalId, + templateId: widget.templateId, + savedJournalId: _savedJournalId, repo: repo, onSaveComplete: () { - _showShareSheetAndNavigate(context, repo, savedJournalId); + _showShareSheetAndNavigate(context, repo, _savedJournalId); }, ), ); @@ -137,7 +147,14 @@ class EditorPage extends StatelessWidget { // --- 更新已有日记 --- final existing = await repo.getJournal(savedJournalId); if (existing != null) { - await repo.updateJournal(existing); + // 将编辑器当前状态合并到已有日记中 + final updated = existing.copyWith( + title: state.title.isNotEmpty ? state.title : existing.title, + mood: state.selectedMood, + tags: state.tags.isNotEmpty ? state.tags : existing.tags, + updatedAt: now, + ); + await repo.updateJournal(updated); // 入队 SyncEngine 等待同步到后端 syncEngine.enqueue(PendingOperation(