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

fix search integration issues #511

Merged
merged 3 commits into from
Oct 1, 2023
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
1 change: 1 addition & 0 deletions lib/appflowy_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export 'src/editor/block_component/rich_text/appflowy_rich_text_keys.dart';
export 'src/editor/block_component/rich_text/default_selectable_mixin.dart';
// editor part, including editor component, block component, etc.
export 'src/editor/editor.dart';
export 'src/editor/find_replace_menu/find_and_replace.dart';
export 'src/editor/selection_menu/selection_menu.dart';
// editor state
export 'src/editor_state.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class _DesktopScrollServiceState extends State<DesktopScrollService>

@override
void jumpTo(int index) {
throw UnimplementedError();
editorScrollController.jumpTo(offset: index.toDouble());
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ class EditorScrollController {
);
}

scrollOffsetController.jumpTo(offset: max(0, offset));
itemScrollController.jumpTo(
index: max(0, offset.toInt()),
alignment: 0.5,
);
}

void jumpToTop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ class FindReplaceStyle {
FindReplaceStyle({
this.selectedHighlightColor = const Color(0xFFFFB931),
this.unselectedHighlightColor = const Color(0x60ECBC5F),
this.findMenuBuilder,
});

//selected highlight color is used as background color on the selected found pattern.
final Color selectedHighlightColor;
//unselected highlight color is used on every other found pattern which can be selected.
final Color unselectedHighlightColor;

// find menu builder
Widget Function(
BuildContext context,
EditorState editorState,
FindReplaceLocalizations? localizations,
FindReplaceStyle style,
bool showReplaceMenu,
VoidCallback onDismiss,
)? findMenuBuilder;
}

class FindReplaceLocalizations {
Expand Down Expand Up @@ -108,7 +119,7 @@ KeyEventResult _showFindAndReplaceDialog(
_findReplaceService = FindReplaceMenu(
context: context,
editorState: editorState,
replaceFlag: openReplace,
showReplaceMenu: openReplace,
localizations: localizations,
style: style,
);
Expand Down
1 change: 1 addition & 0 deletions lib/src/editor/find_replace_menu/find_and_replace.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'search_service_v2.dart';
54 changes: 31 additions & 23 deletions lib/src/editor/find_replace_menu/find_menu_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class FindReplaceMenu implements FindReplaceService {
FindReplaceMenu({
required this.context,
required this.editorState,
required this.replaceFlag,
required this.showReplaceMenu,
this.localizations,
required this.style,
});

final BuildContext context;
final EditorState editorState;
final bool replaceFlag;
final bool showReplaceMenu;
final FindReplaceLocalizations? localizations;
final FindReplaceStyle style;

Expand Down Expand Up @@ -68,29 +68,37 @@ class FindReplaceMenu implements FindReplaceService {
return Positioned(
top: topOffset,
right: rightOffset,
child: Material(
borderRadius: BorderRadius.circular(8.0),
child: DecoratedBox(
decoration: BoxDecoration(
color: editorState.editorStyle.selectionColor,
boxShadow: [
BoxShadow(
blurRadius: 5,
spreadRadius: 1,
color: Colors.black.withOpacity(0.1),
child: style.findMenuBuilder?.call(
context,
editorState,
localizations,
style,
showReplaceMenu,
dismiss,
) ??
Material(
borderRadius: BorderRadius.circular(8.0),
child: DecoratedBox(
decoration: BoxDecoration(
color: editorState.editorStyle.selectionColor,
boxShadow: [
BoxShadow(
blurRadius: 5,
spreadRadius: 1,
color: Colors.black.withOpacity(0.1),
),
],
borderRadius: BorderRadius.circular(6.0),
),
],
borderRadius: BorderRadius.circular(6.0),
),
child: FindAndReplaceMenuWidget(
onDismiss: dismiss,
editorState: editorState,
showReplaceMenu: replaceFlag,
localizations: localizations,
style: style,
child: FindAndReplaceMenuWidget(
onDismiss: dismiss,
editorState: editorState,
showReplaceMenu: showReplaceMenu,
localizations: localizations,
style: style,
),
),
),
),
),
);
},
);
Expand Down
1 change: 0 additions & 1 deletion lib/src/editor/find_replace_menu/find_replace_widget.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/find_replace_menu/find_replace_menu_icon_button.dart';
import 'package:appflowy_editor/src/editor/find_replace_menu/search_service_v2.dart';
import 'package:flutter/material.dart';

const double _iconButtonSize = 30;
Expand Down
16 changes: 16 additions & 0 deletions lib/src/editor/find_replace_menu/search_service_v2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ class SearchServiceV2 {
int _selectedIndex = 0;
int get selectedIndex => _selectedIndex;
set selectedIndex(int index) {
_prevSelectedIndex = _selectedIndex;
_selectedIndex = matchedPositions.value.isEmpty
? -1
: index.clamp(0, matchedPositions.value.length - 1);
currentSelectedIndex.value = _selectedIndex;
}

// only used for scrolling to the first or the last match.
int _prevSelectedIndex = 0;

ValueNotifier<int> currentSelectedIndex = ValueNotifier(0);

void dispose() {
Expand Down Expand Up @@ -101,6 +105,18 @@ class SearchServiceV2 {
path: start.path,
offset: start.offset + pattern.length,
);

// https://github.com/google/flutter.widgets/issues/151
// there's a bug in the scrollable_positioned_list package
// we can't scroll to the index without animation.
// so we just scroll the position if the index is the first or the last.
final length = matchedPositions.value.length - 1;
if (_prevSelectedIndex != selectedIndex &&
((_prevSelectedIndex == length && selectedIndex == 0) ||
(_prevSelectedIndex == 0 && selectedIndex == length))) {
editorState.scrollService?.jumpTo(start.path.first);
}

editorState.updateSelectionWithReason(
Selection(start: start, end: end),
extraInfo: {
Expand Down
1 change: 0 additions & 1 deletion lib/src/editor/toolbar/desktop/floating_toolbar.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:math';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/find_replace_menu/search_service_v2.dart';
import 'package:flutter/material.dart';

class FloatingToolbarStyle {
Expand Down