Support unfocused chat composer paste

This commit is contained in:
2026-11-07 22:32:07 +00:00
parent f6ca558302
commit 63758cd176
12 changed files with 619 additions and 35 deletions
@@ -27,7 +27,7 @@ void main() {
var listens = 0;
var stops = 0;
final paste = ChatClipboardPaste(
subscribe: (onFiles, canAccept, onError) {
subscribe: (onFiles, canAccept, onError, onText, canAcceptText) {
listens += 1;
return () => stops += 1;
},
@@ -0,0 +1,170 @@
@TestOn('browser')
library;
import 'dart:js_interop';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart';
@JS('Function')
external JSFunction _function(JSString body);
JSAny? _js(String body) => _function(body.toJS).callAsFunction(null);
bool _paste(String target, {String? filename}) =>
(_js('''
const target = $target;
const data = new DataTransfer();
${filename != null ? "data.items.add(new File([new Uint8Array([104,105])], '$filename', {type:'${filename.endsWith('.png') ? 'image/png' : 'text/plain'}'}));" : "data.setData('text/plain', 'browser text');"}
const event = new ClipboardEvent('paste', {clipboardData:data, bubbles:true, composed:true, cancelable:true});
target.dispatchEvent(event);
return event.defaultPrevented;
''')
as JSBoolean)
.toDart;
void main() {
late List<ChatPickedFile> files;
late List<String> texts;
late List<Object> errors;
late ChatPasteUnsubscribe stop;
var accepts = true, acceptsText = true;
setUp(() {
files = [];
texts = [];
errors = [];
accepts = true;
acceptsText = true;
stop = hostChatClipboardPaste.listen(
onFiles: files.addAll,
canAccept: () => accepts,
onError: errors.add,
onText: texts.add,
canAcceptText: () => acceptsText,
);
});
tearDown(() {
stop();
_js("document.getElementById('paste-test-host')?.remove();");
});
void editingElement({
bool flutter = false,
bool shadow = false,
bool closed = false,
}) {
_js('''
const host = document.createElement('${shadow ? 'paste-test-editor' : 'div'}'); host.id='paste-test-host'; host.tabIndex=0;
const input = document.createElement('textarea');
${flutter ? "input.className='flt-text-editing';" : ''}
const root = ${shadow ? "host.attachShadow({mode:'${closed ? 'closed' : 'open'}'})" : 'host'};
root.append(input); document.body.append(host); input.focus();
globalThis.pasteTestTarget=input;
''');
}
Future<void> filesRead() async {
for (var i = 0; i < 50 && files.isEmpty && errors.isEmpty; i++) {
await Future<void>.delayed(const Duration(milliseconds: 10));
}
}
test(
'production bridge consumes unfocused text and clipboard files/images',
() async {
expect(_paste('document.body'), isTrue);
expect(texts, ['browser text']);
expect(_paste('document.body', filename: 'clipboard.txt'), isTrue);
await filesRead();
expect(files.single.name, 'clipboard.txt');
expect(files.single.bytes, [104, 105]);
files.clear();
expect(_paste('document.body', filename: 'clipboard.png'), isTrue);
await filesRead();
expect(files.single.mime, 'image/png');
expect(errors, isEmpty);
},
);
test(
'semantics-owned unclassed editor preserves native text and accepts files',
() async {
_js(
"const host=document.createElement('flt-semantics');host.id='paste-test-host';const input=document.createElement('textarea');host.append(input);document.body.append(host);input.focus();globalThis.pasteTestTarget=input;",
);
acceptsText = false;
expect(_paste('globalThis.pasteTestTarget'), isFalse);
expect(
_paste('globalThis.pasteTestTarget', filename: 'clipboard.txt'),
isTrue,
);
await filesRead();
expect(files.single.name, 'clipboard.txt');
accepts = false;
expect(
_paste('globalThis.pasteTestTarget', filename: 'clipboard.txt'),
isFalse,
);
},
);
test(
'external DOM and open-shadow editors keep both text and files',
() async {
for (final shadow in [false, true]) {
editingElement(shadow: shadow);
expect(_paste('globalThis.pasteTestTarget'), isFalse);
expect(
_paste('globalThis.pasteTestTarget', filename: 'clipboard.png'),
isFalse,
);
_js("document.getElementById('paste-test-host').remove();");
}
expect(texts, isEmpty);
expect(files, isEmpty);
},
);
test('closed external shadow editor is conservatively left native', () {
editingElement(shadow: true, closed: true);
expect(_paste('globalThis.pasteTestTarget'), isFalse);
expect(texts, isEmpty);
});
test(
'Flutter focus eligibility rejects unrelated editor sharing engine class',
() {
accepts = false;
acceptsText = false;
editingElement(flutter: true, shadow: true);
expect(_paste('globalThis.pasteTestTarget'), isFalse);
expect(
_paste('globalThis.pasteTestTarget', filename: 'clipboard.png'),
isFalse,
);
expect(texts, isEmpty);
expect(files, isEmpty);
},
);
test('inactive Flutter editing target is consumed before target listeners', () {
editingElement(flutter: true, shadow: true);
_js(
"globalThis.pasteAtTarget=null; pasteTestTarget.addEventListener('paste',e=>globalThis.pasteAtTarget=e.defaultPrevented);",
);
expect(_paste('globalThis.pasteTestTarget'), isTrue);
expect(texts, ['browser text']);
expect(
(_js('return globalThis.pasteAtTarget;') as JSBoolean).toDart,
isTrue,
);
});
test(
'focused composer native text path is untouched while file path remains available',
() async {
acceptsText = false;
editingElement(flutter: true, shadow: true);
expect(_paste('globalThis.pasteTestTarget'), isFalse);
expect(texts, isEmpty);
expect(
_paste('globalThis.pasteTestTarget', filename: 'clipboard.txt'),
isTrue,
);
await filesRead();
expect(files.single.name, 'clipboard.txt');
},
);
}