Files
pipelines/app/lib/features/chat/domain/chat_clipboard_target_web.dart
T

68 lines
2.3 KiB
Dart

import 'dart:js_interop';
import 'dart:js_interop_unsafe';
@JS('document')
external _Document get _document;
extension type _Document._(JSObject _) implements JSObject {
external JSObject? get activeElement;
}
extension type _Event._(JSObject _) implements JSObject {
external JSObject? get target;
external JSArray<JSObject> composedPath();
}
/// Never take a paste owned by an external editor. Flutter field ownership is
/// checked by the composer's Flutter FocusNode before this DOM check; the
/// engine's shared flt-text-editing element is not itself a composer identity.
bool chatPasteHasExternalEditable(JSObject event) {
final paste = _Event._(event);
final candidates = <JSObject>[...paste.composedPath().toDart];
if (paste.target case final target?) candidates.add(target);
var active = _document.activeElement;
for (var depth = 0; active != null && depth < 32; depth++) {
candidates.add(active);
final shadow = active.getProperty<JSObject?>('shadowRoot'.toJS);
final next = shadow?.getProperty<JSObject?>('activeElement'.toJS);
if (next == null) break;
active = next;
}
for (final node in candidates) {
final tag = node
.getProperty<JSString?>('tagName'.toJS)
?.toDart
.toUpperCase();
final editable =
tag == 'INPUT' ||
tag == 'TEXTAREA' ||
node.getProperty<JSBoolean?>('isContentEditable'.toJS)?.toDart == true;
if (editable) {
final classes =
node.getProperty<JSString?>('className'.toJS)?.toDart ?? '';
final parent = node.getProperty<JSObject?>('parentElement'.toJS);
final semanticsOwned =
parent
?.getProperty<JSString?>('tagName'.toJS)
?.toDart
.toUpperCase() ==
'FLT-SEMANTICS';
if (!classes.split(RegExp(r'\s+')).contains('flt-text-editing') &&
!semanticsOwned) {
return true;
}
}
}
// A focused embedded document or opaque custom-element editor owns its
// keyboard. Closed shadow roots cannot expose their private editable node.
if (active != null) {
final tag =
active.getProperty<JSString?>('tagName'.toJS)?.toDart.toUpperCase() ??
'';
if (tag == 'IFRAME' || (tag.contains('-') && !tag.startsWith('FLT-'))) {
return true;
}
}
return false;
}