155 lines
4.5 KiB
Dart
155 lines
4.5 KiB
Dart
import 'dart:async';
|
|
import 'dart:js_interop';
|
|
import 'dart:js_interop_unsafe';
|
|
|
|
import 'chat_attachment_picker.dart';
|
|
import 'chat_clipboard_paste.dart';
|
|
import 'chat_clipboard_target_web.dart';
|
|
|
|
/// Browser clipboard paste observed at the document for each mounted composer.
|
|
/// Focused fields retain native text insertion. An eligible active composer can
|
|
/// receive unfocused text or files; external editors retain their own paste.
|
|
/// Uses dart:js_interop so the bridge supports JavaScript and WebAssembly builds.
|
|
@JS('document')
|
|
external _Document get _document;
|
|
|
|
extension type _Document._(JSObject _) implements JSObject {
|
|
external void addEventListener(
|
|
JSString type,
|
|
JSFunction listener,
|
|
JSBoolean capture,
|
|
);
|
|
|
|
external void removeEventListener(
|
|
JSString type,
|
|
JSFunction listener,
|
|
JSBoolean capture,
|
|
);
|
|
|
|
external JSObject? get activeElement;
|
|
}
|
|
|
|
extension type _ClipboardEvent._(JSObject _) implements JSObject {
|
|
external bool get defaultPrevented;
|
|
|
|
external JSObject? get target;
|
|
|
|
external _ClipboardData? get clipboardData;
|
|
|
|
external void preventDefault();
|
|
}
|
|
|
|
extension type _ClipboardData._(JSObject _) implements JSObject {
|
|
external _DataTransferItemList get items;
|
|
|
|
external JSString getData(JSString format);
|
|
}
|
|
|
|
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,
|
|
ChatPasteText onText,
|
|
ChatPasteCanAcceptText canAcceptText,
|
|
) {
|
|
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);
|
|
}
|
|
if (chatPasteHasExternalEditable(raw)) return;
|
|
if (files.isNotEmpty) {
|
|
// An active chat pane may take over from an inactive chat composer, but
|
|
// an unrelated editable field keeps the browser's native paste.
|
|
if (!canAccept()) {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
unawaited(_readFiles(files, onFiles, onError, () => ++sequence));
|
|
return;
|
|
}
|
|
// A focused composer owns its native paste. An eligible active pane may
|
|
// take over when the focused editable is an inactive chat composer.
|
|
if (!canAcceptText()) {
|
|
return;
|
|
}
|
|
final text = data.getData('text/plain'.toJS).toDart;
|
|
if (text.isEmpty) return;
|
|
event.preventDefault();
|
|
onText(text);
|
|
}
|
|
|
|
final listener = handlePaste.toJS;
|
|
_document.addEventListener('paste'.toJS, listener, true.toJS);
|
|
return () => _document.removeEventListener('paste'.toJS, listener, true.toJS);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|