Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions lib/web_ui/lib/src/engine/clipboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ClipboardMessageHandler {
/// APIs and the browser.
abstract class CopyToClipboardStrategy {
factory CopyToClipboardStrategy() {
return (html.window.navigator.clipboard.writeText != null)
return (html.window.navigator.clipboard?.writeText != null)
? ClipboardAPICopyStrategy()
: ExecCommandCopyStrategy();
}
Expand All @@ -52,7 +52,7 @@ abstract class CopyToClipboardStrategy {
abstract class PasteFromClipboardStrategy {
factory PasteFromClipboardStrategy() {
return (browserEngine == BrowserEngine.firefox ||
html.window.navigator.clipboard.readText == null)
html.window.navigator.clipboard?.readText == null)
? ExecCommandPasteStrategy()
: ClipboardAPIPasteStrategy();
}
Expand Down Expand Up @@ -91,9 +91,43 @@ class ClipboardAPIPasteStrategy implements PasteFromClipboardStrategy {
class ExecCommandCopyStrategy implements CopyToClipboardStrategy {
@override
void setData(String text) {
// TODO(nurhan): https://github.com/flutter/flutter/issues/48578
print('Clipboard is only implemented for browsers '
'supporting Clipboard API. Use context menu for text editing.');
// Copy content to clipboard with execCommand.
// See: https://developers.google.com/web/updates/2015/04/cut-and-copy-commands
final html.TextAreaElement tempTextArea = _appendTemporaryTextArea();
tempTextArea.value = text;
tempTextArea.focus();
tempTextArea.select();
try {
final bool result = html.document.execCommand('copy');
if (!result) {
print('copy is not successful');
}
} catch (e) {
print('copy is not successful ${e.message}');
} finally {
_removeTemporaryTextArea(tempTextArea);
}
}

html.TextAreaElement _appendTemporaryTextArea() {
final html.TextAreaElement tempElement = html.TextAreaElement();
final html.CssStyleDeclaration elementStyle = tempElement.style;
elementStyle
..position = 'absolute'
..top = '-99999px'
..left = '-99999px'
..opacity = '0'
..color = 'transparent'
..backgroundColor = 'transparent'
..background = 'transparent';

html.document.body.append(tempElement);

return tempElement;
}

void _removeTemporaryTextArea(html.HtmlElement element) {
element?.remove();
}
}

Expand Down