|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +part of engine; |
| 6 | + |
| 7 | +/// Handles clipboard related platform messages. |
| 8 | +class ClipboardMessageHandler { |
| 9 | + /// Helper to handle copy to clipboard functionality. |
| 10 | + final CopyToClipboardStrategy _copyToClipboardStrategy = |
| 11 | + CopyToClipboardStrategy(); |
| 12 | + |
| 13 | + /// Helper to handle copy to clipboard functionality. |
| 14 | + final PasteFromClipboardStrategy _pasteFromClipboardStrategy = |
| 15 | + PasteFromClipboardStrategy(); |
| 16 | + |
| 17 | + /// Handles the platform message which stores the given text to the clipboard. |
| 18 | + void setDataMethodCall(MethodCall methodCall) { |
| 19 | + _copyToClipboardStrategy.setData(methodCall.arguments['text']); |
| 20 | + } |
| 21 | + |
| 22 | + /// Handles the platform message which retrieves text data from the clipboard. |
| 23 | + void getDataMethodCall(ui.PlatformMessageResponseCallback callback) { |
| 24 | + _pasteFromClipboardStrategy.getData().then((String data) { |
| 25 | + const MethodCodec codec = JSONMethodCodec(); |
| 26 | + final Map<String, dynamic> map = {'text': data}; |
| 27 | + callback(codec.encodeSuccessEnvelope(map)); |
| 28 | + }).catchError( |
| 29 | + (error) => print('Could not get text from clipboard: $error')); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// Provides functionality for writing text to clipboard. |
| 34 | +/// |
| 35 | +/// A concrete implementation is picked at runtime based on the available |
| 36 | +/// APIs and the browser. |
| 37 | +abstract class CopyToClipboardStrategy { |
| 38 | + factory CopyToClipboardStrategy() { |
| 39 | + return (html.window.navigator.clipboard.writeText != null) |
| 40 | + ? ClipboardAPICopyStrategy() |
| 41 | + : ExecCommandCopyStrategy(); |
| 42 | + } |
| 43 | + |
| 44 | + /// Places the text onto the browser Clipboard. |
| 45 | + void setData(String text); |
| 46 | +} |
| 47 | + |
| 48 | +/// Provides functionality for reading text from clipboard. |
| 49 | +/// |
| 50 | +/// A concrete implementation is picked at runtime based on the available |
| 51 | +/// APIs and the browser. |
| 52 | +abstract class PasteFromClipboardStrategy { |
| 53 | + factory PasteFromClipboardStrategy() { |
| 54 | + return (browserEngine == BrowserEngine.firefox || |
| 55 | + html.window.navigator.clipboard.readText == null) |
| 56 | + ? ExecCommandPasteStrategy() |
| 57 | + : ClipboardAPIPasteStrategy(); |
| 58 | + } |
| 59 | + |
| 60 | + /// Returns text from the system Clipboard. |
| 61 | + Future<String> getData(); |
| 62 | +} |
| 63 | + |
| 64 | +/// Provides copy functionality for browsers which supports ClipboardAPI. |
| 65 | +/// |
| 66 | +/// Works on Chrome and Firefox browsers. |
| 67 | +/// See: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API |
| 68 | +class ClipboardAPICopyStrategy implements CopyToClipboardStrategy { |
| 69 | + @override |
| 70 | + void setData(String text) { |
| 71 | + html.window.navigator.clipboard |
| 72 | + .writeText(text) |
| 73 | + .catchError((error) => print('Could not copy text: $error')); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Provides paste functionality for browsers which supports `clipboard.readText`. |
| 78 | +/// |
| 79 | +/// Works on Chrome. Firefox only supports `readText` if the target element is |
| 80 | +/// in content editable mode. |
| 81 | +/// See: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content |
| 82 | +/// See: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API |
| 83 | +class ClipboardAPIPasteStrategy implements PasteFromClipboardStrategy { |
| 84 | + @override |
| 85 | + Future<String> getData() async { |
| 86 | + return html.window.navigator.clipboard.readText(); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Provides a fallback strategy for browsers which does not support ClipboardAPI. |
| 91 | +class ExecCommandCopyStrategy implements CopyToClipboardStrategy { |
| 92 | + @override |
| 93 | + void setData(String text) { |
| 94 | + // TODO(nurhan): https://github.com/flutter/flutter/issues/48578 |
| 95 | + print('Clipboard is only implemented for browsers ' |
| 96 | + 'supporting Clipboard API. Use context menu for text editing.'); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// Provides a fallback strategy for browsers which does not support ClipboardAPI. |
| 101 | +class ExecCommandPasteStrategy implements PasteFromClipboardStrategy { |
| 102 | + @override |
| 103 | + Future<String> getData() { |
| 104 | + // TODO(nurhan): https://github.com/flutter/flutter/issues/48581 |
| 105 | + // TODO(nurhan): https://github.com/flutter/flutter/issues/48580 |
| 106 | + print('Paste is not implemented for this browser.'); |
| 107 | + throw UnimplementedError(); |
| 108 | + } |
| 109 | +} |
0 commit comments