Skip to content

Commit

Permalink
feat: add contentInsertionConfiguration to editor and text input serv…
Browse files Browse the repository at this point in the history
…ice (#691)

* Add contentInsertionConfiguration to editor and text input service

* add unit tests

* feat(editor): add contentInsertionConfiguration
  • Loading branch information
stevenosse authored Jan 30, 2024
1 parent 2c5d5c7 commit 0d8f755
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
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));
});
});
}

0 comments on commit 0d8f755

Please sign in to comment.