96 lines
3.6 KiB
Dart
96 lines
3.6 KiB
Dart
import 'chat_attachment_picker.dart';
|
|
import 'chat_clipboard_paste_stub.dart'
|
|
if (dart.library.js_interop) 'chat_clipboard_paste_web.dart'
|
|
as impl;
|
|
|
|
/// Receives the files one paste carried, in clipboard order.
|
|
typedef ChatPasteFiles = void Function(List<ChatPickedFile> files);
|
|
typedef ChatPasteText = void Function(String text);
|
|
|
|
/// Whether the composer would take a pasted file right now. The host asks this
|
|
/// before it suppresses the paste's default behaviour.
|
|
typedef ChatPasteCanAccept = bool Function();
|
|
typedef ChatPasteCanAcceptText = bool Function();
|
|
|
|
/// Reports a clipboard file whose bytes could not be read.
|
|
typedef ChatPasteFailed = void Function(Object error);
|
|
|
|
/// Stops observing paste events for one composer.
|
|
typedef ChatPasteUnsubscribe = void Function();
|
|
|
|
/// Registers [onFiles] for file pastes the composer may take and returns the
|
|
/// cancellation for that registration.
|
|
///
|
|
/// A text-only paste is reported when the active chat pane owns it; otherwise
|
|
/// it stays with the host field, so unrelated editable controls keep native
|
|
/// caret and selection behaviour.
|
|
typedef ChatPasteSubscribe =
|
|
ChatPasteUnsubscribe Function(
|
|
ChatPasteFiles onFiles,
|
|
ChatPasteCanAccept canAccept,
|
|
ChatPasteFailed onError,
|
|
ChatPasteText onText,
|
|
ChatPasteCanAcceptText canAcceptText,
|
|
);
|
|
|
|
/// The clipboard seam for one host.
|
|
///
|
|
/// The web host observes the browser's own `paste` event. A paste carrying at
|
|
/// least one file is consumed ([ChatPasteCanAccept] permitting) and handed to
|
|
/// the composer as [ChatPickedFile] rows, so the landed attachment draft owns
|
|
/// every size decision and refusal. A clipboard file the browser recorded
|
|
/// without a name takes the web client's pasted name. A host without
|
|
/// clipboard-file paste constructs [ChatClipboardPaste.unsupported] and the
|
|
/// composer simply keeps its native text behaviour. Tests inject a fake
|
|
/// [subscribe].
|
|
class ChatClipboardPaste {
|
|
const ChatClipboardPaste({required ChatPasteSubscribe this.subscribe})
|
|
: unsupportedMessage = null;
|
|
|
|
const ChatClipboardPaste.unsupported(String message)
|
|
: subscribe = null,
|
|
unsupportedMessage = message;
|
|
|
|
/// The host's paste listener, or null when this host cannot observe files.
|
|
final ChatPasteSubscribe? subscribe;
|
|
|
|
/// Why this host cannot observe pasted files, when [subscribe] is null.
|
|
final String? unsupportedMessage;
|
|
|
|
bool get isSupported => subscribe != null;
|
|
|
|
/// Starts observing the host's paste event. A host without the capability
|
|
/// throws [UnsupportedError] with [unsupportedMessage].
|
|
ChatPasteUnsubscribe listen({
|
|
required ChatPasteFiles onFiles,
|
|
required ChatPasteCanAccept canAccept,
|
|
required ChatPasteFailed onError,
|
|
ChatPasteText? onText,
|
|
ChatPasteCanAcceptText? canAcceptText,
|
|
}) {
|
|
final subscribe = this.subscribe;
|
|
if (subscribe == null) throw UnsupportedError(unsupportedMessage!);
|
|
return subscribe(
|
|
onFiles,
|
|
canAccept,
|
|
onError,
|
|
onText ?? (_) {},
|
|
canAcceptText ?? () => false,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The web composer's recorded name for a clipboard file that arrived without
|
|
/// one: the MIME subtype becomes the extension, with the web client's `jpeg`
|
|
/// to `jpg` mapping and `bin` fallback.
|
|
String chatPastedAttachmentName(String mime, int sequence) {
|
|
final subtype = mime.split('/').last.toLowerCase();
|
|
final extension = subtype == 'jpeg'
|
|
? 'jpg'
|
|
: subtype.replaceAll(RegExp('[^a-z0-9]'), '');
|
|
return 'pasted-$sequence.${extension.isEmpty ? 'bin' : extension}';
|
|
}
|
|
|
|
/// The clipboard seam for the running host.
|
|
ChatClipboardPaste get hostChatClipboardPaste => impl.hostChatClipboardPaste;
|