|
| 1 | +import 'dart:async' show Completer; |
| 2 | +import 'dart:convert' show utf8; |
| 3 | + |
| 4 | +import 'package:flutter/foundation.dart'; |
| 5 | +import 'package:flutter_quill/flutter_quill_internal.dart'; |
| 6 | +import 'package:meta/meta.dart' show experimental; |
| 7 | + |
| 8 | +import 'package:super_clipboard/super_clipboard.dart'; |
| 9 | + |
| 10 | +/// Implementation using the [super_clipboard](https://pub.dev/packages/super_clipboard) plugin. |
| 11 | +@experimental |
| 12 | +class SuperClipboardService extends ClipboardService { |
| 13 | + /// [Null] if the Clipboard API is not supported on this platform |
| 14 | + /// https://pub.dev/packages/super_clipboard#usage |
| 15 | + SystemClipboard? _getSuperClipboard() { |
| 16 | + return SystemClipboard.instance; |
| 17 | + } |
| 18 | + |
| 19 | + SystemClipboard _getSuperClipboardOrThrow() { |
| 20 | + final clipboard = _getSuperClipboard(); |
| 21 | + if (clipboard == null) { |
| 22 | + // To avoid getting this exception, use _canProvide() |
| 23 | + throw UnsupportedError( |
| 24 | + 'Clipboard API is not supported on this platform.', |
| 25 | + ); |
| 26 | + } |
| 27 | + return clipboard; |
| 28 | + } |
| 29 | + |
| 30 | + Future<bool> _canProvide({required DataFormat format}) async { |
| 31 | + final clipboard = _getSuperClipboard(); |
| 32 | + if (clipboard == null) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + final reader = await clipboard.read(); |
| 36 | + return reader.canProvide(format); |
| 37 | + } |
| 38 | + |
| 39 | + Future<Uint8List> _provideFileAsBytes({ |
| 40 | + required SimpleFileFormat format, |
| 41 | + }) async { |
| 42 | + final clipboard = _getSuperClipboardOrThrow(); |
| 43 | + final reader = await clipboard.read(); |
| 44 | + final completer = Completer<Uint8List>(); |
| 45 | + |
| 46 | + reader.getFile( |
| 47 | + format, |
| 48 | + (file) async { |
| 49 | + final bytes = await file.readAll(); |
| 50 | + completer.complete(bytes); |
| 51 | + }, |
| 52 | + onError: completer.completeError, |
| 53 | + ); |
| 54 | + final bytes = await completer.future; |
| 55 | + return bytes; |
| 56 | + } |
| 57 | + |
| 58 | + Future<String> _provideFileAsString({ |
| 59 | + required SimpleFileFormat format, |
| 60 | + }) async { |
| 61 | + final fileBytes = await _provideFileAsBytes(format: format); |
| 62 | + final fileText = utf8.decode(fileBytes); |
| 63 | + return fileText; |
| 64 | + } |
| 65 | + |
| 66 | + /// According to super_clipboard docs, will return `null` if the value |
| 67 | + /// is not available or the data is virtual (macOS and Windows) |
| 68 | + Future<String?> _provideSimpleValueFormatAsString({ |
| 69 | + required SimpleValueFormat<String> format, |
| 70 | + }) async { |
| 71 | + final clipboard = _getSuperClipboardOrThrow(); |
| 72 | + final reader = await clipboard.read(); |
| 73 | + final value = await reader.readValue<String>(format); |
| 74 | + return value; |
| 75 | + } |
| 76 | + |
| 77 | + @override |
| 78 | + Future<String?> getHtmlText() async { |
| 79 | + if (!(await _canProvide(format: Formats.htmlText))) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + return _provideSimpleValueFormatAsString(format: Formats.htmlText); |
| 83 | + } |
| 84 | + |
| 85 | + @override |
| 86 | + Future<String?> getHtmlFile() async { |
| 87 | + if (!(await _canProvide(format: Formats.htmlFile))) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + return await _provideFileAsString(format: Formats.htmlFile); |
| 91 | + } |
| 92 | + |
| 93 | + @override |
| 94 | + Future<Uint8List?> getGifFile() async { |
| 95 | + if (!(await _canProvide(format: Formats.gif))) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + return await _provideFileAsBytes(format: Formats.gif); |
| 99 | + } |
| 100 | + |
| 101 | + @override |
| 102 | + Future<Uint8List?> getImageFile() async { |
| 103 | + final canProvidePngFile = await _canProvide(format: Formats.png); |
| 104 | + if (canProvidePngFile) { |
| 105 | + return _provideFileAsBytes(format: Formats.png); |
| 106 | + } |
| 107 | + final canProvideJpegFile = await _canProvide(format: Formats.jpeg); |
| 108 | + if (canProvideJpegFile) { |
| 109 | + return _provideFileAsBytes(format: Formats.jpeg); |
| 110 | + } |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + @override |
| 115 | + Future<String?> getMarkdownFile() async { |
| 116 | + // Formats.md is for markdown files |
| 117 | + if (!(await _canProvide(format: Formats.md))) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + return await _provideFileAsString(format: Formats.md); |
| 121 | + } |
| 122 | + |
| 123 | + @override |
| 124 | + Future<void> copyImageToClipboard(Uint8List imageBytes) async { |
| 125 | + final clipboard = SystemClipboard.instance; |
| 126 | + if (clipboard == null) { |
| 127 | + return; |
| 128 | + } |
| 129 | + final item = DataWriterItem()..add(Formats.png(imageBytes)); |
| 130 | + await clipboard.write([item]); |
| 131 | + } |
| 132 | + |
| 133 | + @override |
| 134 | + Future<bool> get hasClipboardContent async { |
| 135 | + final clipboard = _getSuperClipboard(); |
| 136 | + if (clipboard == null) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + final reader = await clipboard.read(); |
| 140 | + final availablePlatformFormats = reader.platformFormats; |
| 141 | + return availablePlatformFormats.isNotEmpty; |
| 142 | + } |
| 143 | +} |
0 commit comments