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 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
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,14 @@ 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,8 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
textCapitalization: TextCapitalization.sentences,
inputAction: TextInputAction.newline,
keyboardAppearance: Theme.of(context).brightness,
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