From b733864112fb40f7cc063175347374fee3fa8a92 Mon Sep 17 00:00:00 2001 From: Mathias Mogensen <42929161+Xazin@users.noreply.github.com> Date: Mon, 2 Oct 2023 16:55:28 +0200 Subject: [PATCH] feat: enable shift+press for selection range (#512) --- .../selection/desktop_selection_service.dart | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/src/editor/editor_component/service/selection/desktop_selection_service.dart b/lib/src/editor/editor_component/service/selection/desktop_selection_service.dart index 3bcf2ea34..fdb609d2d 100644 --- a/lib/src/editor/editor_component/service/selection/desktop_selection_service.dart +++ b/lib/src/editor/editor_component/service/selection/desktop_selection_service.dart @@ -2,6 +2,7 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/src/flutter/overlay.dart'; import 'package:appflowy_editor/src/service/selection/selection_gesture.dart'; import 'package:flutter/material.dart' hide Overlay, OverlayEntry; +import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; class DesktopSelectionServiceWidget extends StatefulWidget { @@ -198,24 +199,35 @@ class _DesktopSelectionServiceWidgetState ); if (!canTap) return; - // clear old state. - _panStartOffset = null; - final offset = details.globalPosition; final node = getNodeInOffset(offset); final selectable = node?.selectable; if (selectable == null) { - clearSelection(); - return; + // Clear old start offset + _panStartOffset = null; + return clearSelection(); } - final selection = selectable.cursorStyle == CursorStyle.verticalLine - ? Selection.collapsed( - selectable.getPositionInOffset(offset), - ) - : Selection( - start: selectable.start(), - end: selectable.end(), - ); + + Selection? selection; + if (RawKeyboard.instance.isShiftPressed && _panStartOffset != null) { + final first = getNodeInOffset(_panStartOffset!)?.selectable; + + if (first != null) { + final start = first.getSelectionInRange(_panStartOffset!, offset).start; + final end = + selectable.getSelectionInRange(_panStartOffset!, offset).end; + + selection = Selection(start: start, end: end); + } + } else { + selection = selectable.cursorStyle == CursorStyle.verticalLine + ? Selection.collapsed(selectable.getPositionInOffset(offset)) + : Selection(start: selectable.start(), end: selectable.end()); + + // Reset old start offset + _panStartOffset = offset; + } + updateSelection(selection); }