Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support updating cursor position via spacebar #655

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'dart:io';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/editor_component/service/selection/mobile_selection_service.dart';
import 'package:appflowy_editor/src/render/selection/mobile_basic_handle.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Offset? _cursorOffset;

Future<void> onFloatingCursorUpdate(

Check warning on line 11 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L11

Added line #L11 was not covered by tests
RawFloatingCursorPoint point,
EditorState editorState,
) async {
Log.input.debug('onFloatingCursorUpdate: ${point.state}, ${point.offset}');

Check warning on line 15 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L15

Added line #L15 was not covered by tests

// support updating the cursor position via the space bar on iOS.
if (!Platform.isIOS) {

Check warning on line 18 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L18

Added line #L18 was not covered by tests
return;
}

final selectionService = editorState.service.selectionService;

Check warning on line 22 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L22

Added line #L22 was not covered by tests

switch (point.state) {
case FloatingCursorDragState.Start:
final collapsedCursor = HandleType.collapsed.key;
final context = collapsedCursor.currentContext;

Check warning on line 27 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L24-L27

Added lines #L24 - L27 were not covered by tests
if (context == null) {
return;
}

// get global offset of the cursor.
final renderBox = context.findRenderObject() as RenderBox;
final offset = renderBox.localToGlobal(Offset.zero);
final size = renderBox.size;
_cursorOffset = offset + Offset(size.width / 2, size.height / 2);

Check warning on line 36 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L33-L36

Added lines #L33 - L36 were not covered by tests
disableMagnifier = true;
selectionService.onPanStart(
DragStartDetails(

Check warning on line 39 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L38-L39

Added lines #L38 - L39 were not covered by tests
globalPosition: _cursorOffset!,
localPosition: Offset.zero,
),
MobileSelectionDragMode.cursor,
);
break;
case FloatingCursorDragState.Update:

Check warning on line 46 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L46

Added line #L46 was not covered by tests
disableMagnifier = true;
selectionService.onPanUpdate(
DragUpdateDetails(
globalPosition: _cursorOffset! + point.offset!,

Check warning on line 50 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L48-L50

Added lines #L48 - L50 were not covered by tests
),
MobileSelectionDragMode.cursor,
);
break;
case FloatingCursorDragState.End:

Check warning on line 55 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L55

Added line #L55 was not covered by tests
_cursorOffset = null;
disableMagnifier = false;
selectionService.onPanEnd(
DragEndDetails(),

Check warning on line 59 in lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart#L58-L59

Added lines #L58 - L59 were not covered by tests
MobileSelectionDragMode.cursor,
);
break;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
required super.onReplace,
required super.onNonTextUpdate,
required super.onPerformAction,
super.onFloatingCursor,
});

@override
Expand Down Expand Up @@ -134,11 +135,14 @@

@override
Future<void> performAction(TextInputAction action) async {
Log.editor.debug('performAction: $action');
return onPerformAction(action);
}

@override
void performPrivateCommand(String action, Map<String, dynamic> data) {}
void performPrivateCommand(String action, Map<String, dynamic> data) {
Log.editor.debug('performPrivateCommand: $action, $data');

Check warning on line 144 in lib/src/editor/editor_component/service/ime/non_delta_input_service.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/non_delta_input_service.dart#L144

Added line #L144 was not covered by tests
}

@override
void removeTextPlaceholder() {}
Expand All @@ -150,7 +154,9 @@
void showToolbar() {}

@override
void updateFloatingCursor(RawFloatingCursorPoint point) {}
void updateFloatingCursor(RawFloatingCursorPoint point) {
onFloatingCursor?.call(point);

Check warning on line 158 in lib/src/editor/editor_component/service/ime/non_delta_input_service.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/non_delta_input_service.dart#L158

Added line #L158 was not covered by tests
}

@override
void didChangeInputControl(
Expand All @@ -159,7 +165,9 @@
) {}

@override
void performSelector(String selectorName) {}
void performSelector(String selectorName) {
Log.editor.debug('performSelector: $selectorName');

Check warning on line 169 in lib/src/editor/editor_component/service/ime/non_delta_input_service.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/non_delta_input_service.dart#L169

Added line #L169 was not covered by tests
}

@override
void insertContent(KeyboardInsertedContent content) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ abstract class TextInputService {
required this.onReplace,
required this.onNonTextUpdate,
required this.onPerformAction,
this.onFloatingCursor,
});

Future<void> Function(TextEditingDeltaInsertion insertion) onInsert;
Expand All @@ -16,6 +17,7 @@ abstract class TextInputService {
Future<void> Function(TextEditingDeltaNonTextUpdate nonTextUpdate)
onNonTextUpdate;
Future<void> Function(TextInputAction action) onPerformAction;
Future<void> Function(RawFloatingCursorPoint point)? onFloatingCursor;

TextRange? get composingTextRange;
bool get attached;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/editor_component/service/ime/delta_input_on_floating_cursor_update.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -81,6 +82,10 @@
action,
editorState,
),
onFloatingCursor: (point) => onFloatingCursorUpdate(

Check warning on line 85 in lib/src/editor/editor_component/service/keyboard_service_widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/keyboard_service_widget.dart#L85

Added line #L85 was not covered by tests
point,
editorState,

Check warning on line 87 in lib/src/editor/editor_component/service/keyboard_service_widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/keyboard_service_widget.dart#L87

Added line #L87 was not covered by tests
),
);

focusNode = widget.focusNode ?? FocusNode(debugLabel: 'keyboard service');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

// the value type is MobileSelectionDragMode
const String selectionDragModeKey = 'selection_drag_mode';
bool disableMagnifier = false;

class MobileSelectionServiceWidget extends StatefulWidget {
const MobileSelectionServiceWidget({
Expand Down Expand Up @@ -113,8 +114,6 @@
onDoubleTapUp: _onDoubleTapUp,
onTripleTapUp: _onTripleTapUp,
onLongPressStart: _onLongPressStart,
// onLongPressMoveUpdate: _onLongPressMoveUpdate,
// onLongPressEnd: _onLongPressEnd,
child: Stack(
children: [
widget.child,
Expand All @@ -135,7 +134,7 @@
return ValueListenableBuilder(
valueListenable: _lastPanOffset,
builder: (_, offset, __) {
if (offset == null) {
if (offset == null || disableMagnifier) {
return const SizedBox.shrink();
}
final renderBox = context.findRenderObject() as RenderBox;
Expand Down Expand Up @@ -260,6 +259,8 @@
reason: SelectionUpdateReason.uiEvent,
extraInfo: {
selectionDragModeKey: dragMode,
selectionExtraInfoDoNotAttachTextService:
dragMode == MobileSelectionDragMode.cursor,

Check warning on line 263 in lib/src/editor/editor_component/service/selection/mobile_selection_service.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/selection/mobile_selection_service.dart#L263

Added line #L263 was not covered by tests
},
);
}
Expand Down Expand Up @@ -490,7 +491,9 @@
editorState.updateSelectionWithReason(
editorState.selection,
reason: SelectionUpdateReason.uiEvent,
extraInfo: null,
extraInfo: {

Check warning on line 494 in lib/src/editor/editor_component/service/selection/mobile_selection_service.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/selection/mobile_selection_service.dart#L494

Added line #L494 was not covered by tests
selectionExtraInfoDoNotAttachTextService: false,
},
);
}

Expand Down
18 changes: 18 additions & 0 deletions lib/src/render/selection/mobile_basic_handle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

GlobalKey _leftHandleKey = GlobalKey();
GlobalKey _rightHandleKey = GlobalKey();
GlobalKey _collapsedHandleKey = GlobalKey();

Check warning on line 13 in lib/src/render/selection/mobile_basic_handle.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/render/selection/mobile_basic_handle.dart#L11-L13

Added lines #L11 - L13 were not covered by tests

enum HandleType {
none,
left,
Expand Down Expand Up @@ -39,6 +43,19 @@
return CrossAxisAlignment.center;
}
}

GlobalKey get key {

Check warning on line 47 in lib/src/render/selection/mobile_basic_handle.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/render/selection/mobile_basic_handle.dart#L47

Added line #L47 was not covered by tests
switch (this) {
case HandleType.none:
throw UnsupportedError('Unsupported handle type');
case HandleType.left:
return _leftHandleKey;
case HandleType.right:
return _rightHandleKey;
case HandleType.collapsed:
return _collapsedHandleKey;

Check warning on line 56 in lib/src/render/selection/mobile_basic_handle.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/render/selection/mobile_basic_handle.dart#L49-L56

Added lines #L49 - L56 were not covered by tests
}
}
}

abstract class _IDragHandle extends StatelessWidget {
Expand Down Expand Up @@ -142,6 +159,7 @@
Widget child;
if (handleType == HandleType.collapsed) {
child = Container(
key: handleType.key,

Check warning on line 162 in lib/src/render/selection/mobile_basic_handle.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/render/selection/mobile_basic_handle.dart#L162

Added line #L162 was not covered by tests
width: handleWidth,
color: handleColor,
height: handleHeight,
Expand Down