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: Add contentInsertionConfiguration to editor and text input service #691

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies:
highlight: ^0.7.0
http: ^1.1.0
show_fps: ^1.0.6
path: ^1.8.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it because of the "depend_on_referenced_packages" lint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


dev_dependencies:
flutter_test:
Expand Down
5 changes: 5 additions & 0 deletions lib/src/editor/editor_component/service/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class AppFlowyEditor extends StatefulWidget {
List<CharacterShortcutEvent>? characterShortcutEvents,
List<CommandShortcutEvent>? commandShortcutEvents,
List<List<ContextMenuItem>>? contextMenuItems,
this.contentInsertionConfiguration,
this.editable = true,
this.autoFocus = false,
this.focusedSelection,
Expand Down Expand Up @@ -157,6 +158,9 @@ class AppFlowyEditor extends StatefulWidget {
/// only works on iOS or Android.
final bool showMagnifier;

/// {@macro flutter.widgets.editableText.contentInsertionConfiguration}
final ContentInsertionConfiguration? contentInsertionConfiguration;

@override
State<AppFlowyEditor> createState() => _AppFlowyEditorState();
}
Expand Down Expand Up @@ -260,6 +264,7 @@ class _AppFlowyEditorState extends State<AppFlowyEditor> {
characterShortcutEvents: widget.characterShortcutEvents,
commandShortcutEvents: widget.commandShortcutEvents,
focusNode: widget.focusNode,
contentInsertionConfiguration: widget.contentInsertionConfiguration,
child: child,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class NonDeltaTextInputService extends TextInputService with TextInputClient {
required super.onReplace,
required super.onNonTextUpdate,
required super.onPerformAction,
super.contentInsertionConfiguration,
super.onFloatingCursor,
});

Expand Down Expand Up @@ -171,7 +172,12 @@ class NonDeltaTextInputService extends TextInputService with TextInputClient {
}

@override
void insertContent(KeyboardInsertedContent content) {}
void insertContent(KeyboardInsertedContent content) {
assert(contentInsertionConfiguration?.allowedMimeTypes
.contains(content.mimeType) ??
false);
contentInsertionConfiguration?.onContentInserted.call(content);
}

void _updateComposing(TextEditingDelta delta) {
if (delta is! TextEditingDeltaNonTextUpdate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ abstract class TextInputService {
required this.onNonTextUpdate,
required this.onPerformAction,
this.onFloatingCursor,
this.contentInsertionConfiguration,
});

Future<void> Function(TextEditingDeltaInsertion insertion) onInsert;
Expand All @@ -19,6 +20,8 @@ abstract class TextInputService {
Future<void> Function(TextInputAction action) onPerformAction;
Future<void> Function(RawFloatingCursorPoint point)? onFloatingCursor;

final ContentInsertionConfiguration? contentInsertionConfiguration;

TextRange? get composingTextRange;
bool get attached;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class KeyboardServiceWidget extends StatefulWidget {
this.commandShortcutEvents = const [],
this.characterShortcutEvents = const [],
this.focusNode,
this.contentInsertionConfiguration,
required this.child,
});

final ContentInsertionConfiguration? contentInsertionConfiguration;
final FocusNode? focusNode;
final List<CommandShortcutEvent> commandShortcutEvents;
final List<CharacterShortcutEvent> characterShortcutEvents;
Expand Down Expand Up @@ -86,6 +88,7 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
point,
editorState,
),
contentInsertionConfiguration: widget.contentInsertionConfiguration,
);

focusNode = widget.focusNode ?? FocusNode(debugLabel: 'keyboard service');
Expand Down Expand Up @@ -236,6 +239,9 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
textCapitalization: TextCapitalization.sentences,
inputAction: TextInputAction.newline,
keyboardAppearance: Theme.of(context).brightness,
allowedMimeTypes: widget.contentInsertionConfiguration == null
? const <String>[]
: widget.contentInsertionConfiguration!.allowedMimeTypes,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
allowedMimeTypes: widget.contentInsertionConfiguration == null
? const <String>[]
: widget.contentInsertionConfiguration!.allowedMimeTypes,
allowedMimeTypes:
widget.contentInsertionConfiguration?.allowedMimeTypes ?? [],

),
);
// disable shortcuts when the IME active
Expand Down
28 changes: 28 additions & 0 deletions test/editor/editor_component/ime/non_delta_input_service_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:async';

import 'package:appflowy_editor/src/editor/editor_component/service/ime/non_delta_input_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

Expand Down Expand Up @@ -75,5 +78,30 @@ void main() {
inputService.performAction(TextInputAction.newline);
expect(onPerformAction, true);
});

test('content insertion configuration is handled', () {
final completer = Completer<bool>();
final config = ContentInsertionConfiguration(
allowedMimeTypes: ['mimeType'],
onContentInserted: (value) => completer.complete(true),
);
final inputService = NonDeltaTextInputService(
onInsert: (_) async {},
onDelete: (_) async {},
onReplace: (_) async {},
onNonTextUpdate: (_) async {},
onPerformAction: (_) async {},
contentInsertionConfiguration: config,
);

inputService.insertContent(
const KeyboardInsertedContent(
mimeType: 'mimeType',
uri: 'uri',
),
);

expect(completer.future, completion(true));
});
});
}
Loading