From afe11a4f914b78f1962deed49326e65fd917f485 Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Thu, 11 Aug 2022 13:47:49 +0800 Subject: [PATCH] fix: selection state mismatch --- .../flowy_editor/lib/src/editor_state.dart | 12 ++++++---- .../lib/src/service/selection_service.dart | 23 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/editor_state.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/editor_state.dart index 82725b5255..31a321a3c0 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/src/editor_state.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/src/editor_state.dart @@ -21,6 +21,11 @@ class ApplyOptions { }); } +enum CursorUpdateReason { + uiEvent, + others, +} + class EditorState { final StateTree document; @@ -37,11 +42,10 @@ class EditorState { } /// add the set reason in the future, don't use setter - updateCursorSelection(Selection? cursorSelection) { + updateCursorSelection(Selection? cursorSelection, + [CursorUpdateReason reason = CursorUpdateReason.others]) { // broadcast to other users here - if (cursorSelection == null) { - service.selectionService.clearSelection(); - } else { + if (reason != CursorUpdateReason.uiEvent) { service.selectionService.updateSelection(cursorSelection); } _cursorSelection = cursorSelection; diff --git a/frontend/app_flowy/packages/flowy_editor/lib/src/service/selection_service.dart b/frontend/app_flowy/packages/flowy_editor/lib/src/service/selection_service.dart index 4a4fd6002c..552e9eaf69 100644 --- a/frontend/app_flowy/packages/flowy_editor/lib/src/service/selection_service.dart +++ b/frontend/app_flowy/packages/flowy_editor/lib/src/service/selection_service.dart @@ -52,7 +52,7 @@ abstract class FlowySelectionService { /// The editor will update selection area and toolbar area /// if the [selection] is not collapsed, /// otherwise, will update the cursor area. - void updateSelection(Selection selection); + void updateSelection(Selection? selection); /// Clears the selection area, cursor area and the popup list area. void clearSelection(); @@ -180,21 +180,24 @@ class _FlowySelectionState extends State } @override - void updateSelection(Selection selection) { + void updateSelection(Selection? selection) { selectionRects.clear(); clearSelection(); - if (selection.isCollapsed) { - /// updates cursor area. - debugPrint('updating cursor'); - _updateCursorAreas(selection.start); - } else { - // updates selection area. - debugPrint('updating selection'); - _updateSelectionAreas(selection); + if (selection != null) { + if (selection.isCollapsed) { + /// updates cursor area. + debugPrint('updating cursor'); + _updateCursorAreas(selection.start); + } else { + // updates selection area. + debugPrint('updating selection'); + _updateSelectionAreas(selection); + } } currentSelection.value = selection; + editorState.updateCursorSelection(selection, CursorUpdateReason.uiEvent); } @override