-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove super_clipboard from flutter_quill_extensions and move it…
… to quill_super_clipboard (#2322)
- Loading branch information
Showing
11 changed files
with
20 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 12 additions & 140 deletions
152
...xtensions/lib/src/editor_toolbar_controller_shared/clipboard/super_clipboard_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,16 @@ | ||
import 'dart:async' show Completer; | ||
import 'dart:convert' show utf8; | ||
@Deprecated( | ||
'The super_clipboard implementation has been moved into a separate package: https://pub.dev/packages/quill_super_clipboard and this class will be removed in future releases.\n' | ||
'The new default implementation of flutter_quill is https://pub.dev/packages/quill_native_bridge and supports required features for rich text pasting.\n' | ||
'Also see https://github.com/singerdmx/flutter-quill/pull/2230 and https://pub.dev/packages/quill_native_bridge#-platform-configuration', | ||
) | ||
library; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_quill/flutter_quill_internal.dart' | ||
show ClipboardService; | ||
import 'package:meta/meta.dart' show experimental; | ||
|
||
import 'package:super_clipboard/super_clipboard.dart'; | ||
|
||
/// Implementation using the https://pub.dev/packages/super_clipboard plugin. | ||
@experimental | ||
class SuperClipboardService extends ClipboardService { | ||
/// [Null] if the Clipboard API is not supported on this platform | ||
/// https://pub.dev/packages/super_clipboard#usage | ||
SystemClipboard? _getSuperClipboard() { | ||
return SystemClipboard.instance; | ||
} | ||
|
||
SystemClipboard _getSuperClipboardOrThrow() { | ||
final clipboard = _getSuperClipboard(); | ||
if (clipboard == null) { | ||
// To avoid getting this exception, use _canProvide() | ||
throw UnsupportedError( | ||
'Clipboard API is not supported on this platform.', | ||
); | ||
} | ||
return clipboard; | ||
} | ||
|
||
Future<bool> _canProvide({required DataFormat format}) async { | ||
final clipboard = _getSuperClipboard(); | ||
if (clipboard == null) { | ||
return false; | ||
} | ||
final reader = await clipboard.read(); | ||
return reader.canProvide(format); | ||
} | ||
|
||
Future<Uint8List> _provideFileAsBytes({ | ||
required SimpleFileFormat format, | ||
}) async { | ||
final clipboard = _getSuperClipboardOrThrow(); | ||
final reader = await clipboard.read(); | ||
final completer = Completer<Uint8List>(); | ||
|
||
reader.getFile( | ||
format, | ||
(file) async { | ||
final bytes = await file.readAll(); | ||
completer.complete(bytes); | ||
}, | ||
onError: completer.completeError, | ||
); | ||
final bytes = await completer.future; | ||
return bytes; | ||
} | ||
|
||
Future<String> _provideFileAsString({ | ||
required SimpleFileFormat format, | ||
}) async { | ||
final fileBytes = await _provideFileAsBytes(format: format); | ||
final fileText = utf8.decode(fileBytes); | ||
return fileText; | ||
} | ||
|
||
/// According to super_clipboard docs, will return `null` if the value | ||
/// is not available or the data is virtual (macOS and Windows) | ||
Future<String?> _provideSimpleValueFormatAsString({ | ||
required SimpleValueFormat<String> format, | ||
}) async { | ||
final clipboard = _getSuperClipboardOrThrow(); | ||
final reader = await clipboard.read(); | ||
final value = await reader.readValue<String>(format); | ||
return value; | ||
} | ||
|
||
@override | ||
Future<String?> getHtmlText() async { | ||
if (!(await _canProvide(format: Formats.htmlText))) { | ||
return null; | ||
} | ||
return _provideSimpleValueFormatAsString(format: Formats.htmlText); | ||
} | ||
|
||
@override | ||
Future<String?> getHtmlFile() async { | ||
if (!(await _canProvide(format: Formats.htmlFile))) { | ||
return null; | ||
} | ||
return await _provideFileAsString(format: Formats.htmlFile); | ||
} | ||
|
||
@override | ||
Future<Uint8List?> getGifFile() async { | ||
if (!(await _canProvide(format: Formats.gif))) { | ||
return null; | ||
} | ||
return await _provideFileAsBytes(format: Formats.gif); | ||
} | ||
|
||
@override | ||
Future<Uint8List?> getImageFile() async { | ||
final canProvidePngFile = await _canProvide(format: Formats.png); | ||
if (canProvidePngFile) { | ||
return _provideFileAsBytes(format: Formats.png); | ||
} | ||
final canProvideJpegFile = await _canProvide(format: Formats.jpeg); | ||
if (canProvideJpegFile) { | ||
return _provideFileAsBytes(format: Formats.jpeg); | ||
} | ||
return null; | ||
} | ||
|
||
@override | ||
Future<String?> getMarkdownFile() async { | ||
// Formats.md is for markdown files | ||
if (!(await _canProvide(format: Formats.md))) { | ||
return null; | ||
} | ||
return await _provideFileAsString(format: Formats.md); | ||
} | ||
|
||
@override | ||
Future<void> copyImage(Uint8List imageBytes) async { | ||
final clipboard = SystemClipboard.instance; | ||
if (clipboard == null) { | ||
return; | ||
} | ||
final item = DataWriterItem()..add(Formats.png(imageBytes)); | ||
await clipboard.write([item]); | ||
} | ||
|
||
@override | ||
Future<bool> get hasClipboardContent async { | ||
final clipboard = _getSuperClipboard(); | ||
if (clipboard == null) { | ||
return false; | ||
} | ||
final reader = await clipboard.read(); | ||
final availablePlatformFormats = reader.platformFormats; | ||
return availablePlatformFormats.isNotEmpty; | ||
} | ||
} | ||
@Deprecated( | ||
'The super_clipboard implementation has been moved into a separate package: https://pub.dev/packages/quill_super_clipboard and this class will be removed in future releases.\n' | ||
'The new default implementation of flutter_quill is https://pub.dev/packages/quill_native_bridge and supports required features for rich text pasting.\n' | ||
'Also see https://github.com/singerdmx/flutter-quill/pull/2230 and https://pub.dev/packages/quill_native_bridge#-platform-configuration', | ||
) | ||
class SuperClipboardService {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters