Merge branch 'main' into flutter/chat-cmd-stop
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
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);
|
||||
|
||||
/// 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();
|
||||
|
||||
/// 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 never reported: it stays with the host field, so caret,
|
||||
/// selection and the draft update exactly as typing does.
|
||||
typedef ChatPasteSubscribe =
|
||||
ChatPasteUnsubscribe Function(
|
||||
ChatPasteFiles onFiles,
|
||||
ChatPasteCanAccept canAccept,
|
||||
ChatPasteFailed onError,
|
||||
);
|
||||
|
||||
/// 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,
|
||||
}) {
|
||||
final subscribe = this.subscribe;
|
||||
if (subscribe == null) throw UnsupportedError(unsupportedMessage!);
|
||||
return subscribe(onFiles, canAccept, onError);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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;
|
||||
@@ -0,0 +1,12 @@
|
||||
import 'chat_clipboard_paste.dart';
|
||||
|
||||
/// Native fallback for clipboard-file paste.
|
||||
///
|
||||
/// The composer keeps the host field's own text paste on every platform; this
|
||||
/// slice observes clipboard files on web only. A non-web host reports that
|
||||
/// missing capability rather than pretending no paste happened.
|
||||
const String chatClipboardPasteUnsupportedMessage =
|
||||
'Pasting files is not supported on this platform yet.';
|
||||
|
||||
ChatClipboardPaste get hostChatClipboardPaste =>
|
||||
const ChatClipboardPaste.unsupported(chatClipboardPasteUnsupportedMessage);
|
||||
@@ -0,0 +1,126 @@
|
||||
import 'dart:async';
|
||||
import 'dart:js_interop';
|
||||
import 'dart:js_interop_unsafe';
|
||||
|
||||
import 'chat_attachment_picker.dart';
|
||||
import 'chat_clipboard_paste.dart';
|
||||
|
||||
/// The browser's clipboard-file paste, observed at the document once per
|
||||
/// composer.
|
||||
///
|
||||
/// Declared with `dart:js_interop` rather than `package:web`, so the same source
|
||||
/// compiles for the JavaScript and WebAssembly web targets. A paste carrying no
|
||||
/// file is left completely alone: the field's own text paste keeps its caret,
|
||||
/// selection and draft behaviour. Only a paste with files the composer will
|
||||
/// accept is consumed, and its bytes are read before the files join the
|
||||
/// composer's attachment draft.
|
||||
@JS('document')
|
||||
external _Document get _document;
|
||||
|
||||
extension type _Document._(JSObject _) implements JSObject {
|
||||
external void addEventListener(JSString type, JSFunction listener);
|
||||
|
||||
external void removeEventListener(JSString type, JSFunction listener);
|
||||
}
|
||||
|
||||
extension type _ClipboardEvent._(JSObject _) implements JSObject {
|
||||
external bool get defaultPrevented;
|
||||
|
||||
external _ClipboardData? get clipboardData;
|
||||
|
||||
external void preventDefault();
|
||||
}
|
||||
|
||||
extension type _ClipboardData._(JSObject _) implements JSObject {
|
||||
external _DataTransferItemList get items;
|
||||
}
|
||||
|
||||
extension type _DataTransferItemList._(JSObject _) implements JSObject {
|
||||
external int get length;
|
||||
}
|
||||
|
||||
extension type _DataTransferItem._(JSObject _) implements JSObject {
|
||||
external JSString get kind;
|
||||
|
||||
external _ClipboardFile? getAsFile();
|
||||
}
|
||||
|
||||
extension type _ClipboardFile._(JSObject _) implements JSObject {
|
||||
external JSString get name;
|
||||
|
||||
external JSString get type;
|
||||
|
||||
external JSPromise<JSArrayBuffer> arrayBuffer();
|
||||
}
|
||||
|
||||
ChatClipboardPaste get hostChatClipboardPaste =>
|
||||
const ChatClipboardPaste(subscribe: _subscribeToPaste);
|
||||
|
||||
ChatPasteUnsubscribe _subscribeToPaste(
|
||||
ChatPasteFiles onFiles,
|
||||
ChatPasteCanAccept canAccept,
|
||||
ChatPasteFailed onError,
|
||||
) {
|
||||
var sequence = 0;
|
||||
|
||||
void handlePaste(JSObject raw) {
|
||||
final event = _ClipboardEvent._(raw);
|
||||
if (event.defaultPrevented) return;
|
||||
final data = event.clipboardData;
|
||||
if (data == null) return;
|
||||
final items = data.items;
|
||||
final files = <_ClipboardFile>[];
|
||||
for (var index = 0; index < items.length; index += 1) {
|
||||
// DataTransferItemList is indexed, not a method list like FileList.
|
||||
final raw = items.getProperty<JSObject?>('$index'.toJS);
|
||||
if (raw == null) continue;
|
||||
final item = _DataTransferItem._(raw);
|
||||
if (item.kind.toDart != 'file') continue;
|
||||
final file = item.getAsFile();
|
||||
if (file != null) files.add(file);
|
||||
}
|
||||
// A text-only clipboard keeps the field's native paste untouched.
|
||||
if (files.isEmpty || !canAccept()) return;
|
||||
event.preventDefault();
|
||||
unawaited(_readFiles(files, onFiles, onError, () => ++sequence));
|
||||
}
|
||||
|
||||
final listener = handlePaste.toJS;
|
||||
_document.addEventListener('paste'.toJS, listener);
|
||||
return () => _document.removeEventListener('paste'.toJS, listener);
|
||||
}
|
||||
|
||||
Future<void> _readFiles(
|
||||
List<_ClipboardFile> files,
|
||||
ChatPasteFiles onFiles,
|
||||
ChatPasteFailed onError,
|
||||
int Function() nextSequence,
|
||||
) async {
|
||||
final pasted = <ChatPickedFile>[];
|
||||
final failures = <Object>[];
|
||||
for (final file in files) {
|
||||
try {
|
||||
final buffer = await file.arrayBuffer().toDart;
|
||||
final mime = file.type.toDart;
|
||||
final recorded = file.name.toDart.trim();
|
||||
pasted.add(
|
||||
ChatPickedFile(
|
||||
name: recorded.isEmpty
|
||||
? chatPastedAttachmentName(mime, nextSequence())
|
||||
: recorded,
|
||||
mime: mime,
|
||||
bytes: buffer.toDart.asUint8List(),
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
// One unreadable clipboard file must not hide the others.
|
||||
failures.add(error);
|
||||
}
|
||||
}
|
||||
// Deliver the readable files first: the composer's add clears an older
|
||||
// error, so a partial failure is reported after its kept files.
|
||||
if (pasted.isNotEmpty) onFiles(pasted);
|
||||
for (final failure in failures) {
|
||||
onError(failure);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user