-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: first commit, part of singerdmx/flutter-quill#2230
- Loading branch information
0 parents
commit d3de671
Showing
10 changed files
with
305 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Miscellaneous | ||
*.class | ||
*.log | ||
*.pyc | ||
*.swp | ||
.DS_Store | ||
.atom/ | ||
.buildlog/ | ||
.history | ||
.svn/ | ||
migrate_working_dir/ | ||
|
||
# IntelliJ related | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
|
||
# The .vscode folder contains launch configuration and tasks you configure in | ||
# VS Code which you may wish to be included in version control, so this line | ||
# is commented out by default. | ||
#.vscode/ | ||
|
||
# Flutter/Dart/Pub related | ||
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. | ||
/pubspec.lock | ||
**/doc/api/ | ||
.dart_tool/ | ||
build/ | ||
.flutter-plugins | ||
.flutter-plugins-dependencies |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: "2663184aa79047d0a33a14a3b607954f8fdd8730" | ||
channel: "stable" | ||
|
||
project_type: package |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 0.0.1 | ||
|
||
* initial release. |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Flutter Quill project and open source contributors. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 🪶 Quill Super Clipboard | ||
|
||
A package for integrating the package [`super_clipboard`](https://pub.dev/packages/super_clipboard) with [`flutter_quill`](https://pub.dev/packages/flutter_quill) | ||
to provide seamless access to the system clipboard for rich text operations. | ||
|
||
> **Caution** | ||
> | ||
> The support of this package might be discontinued in the future versions of [flutter_quill](https://pub.dev/packages/flutter_quill). | ||
## Usage | ||
|
||
To use the `super_clipboard` implementation with this package: | ||
|
||
```dart | ||
import 'package:quill_super_clipboard/quill_super_clipboard.dart'; | ||
QuillSuperClipboard.use(); | ||
``` | ||
|
||
> **Note** | ||
> | ||
> The default clipboard implementation for `flutter_quill` is now [`quill_native_bridge`](https://pub.dev/packages/quill_native_bridge). It provides full support for all system clipboard features used by `flutter_quill`. |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include: package:flutter_lints/flutter.yaml | ||
|
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/// A library for integrating the package [`super_clipboard`](https://pub.dev/packages/super_clipboard) with [`flutter_quill`](https://pub.dev/packages/flutter_quill) | ||
/// to provide seamless access to the system clipboard for rich text operations. | ||
@experimental | ||
library; | ||
|
||
import 'package:flutter_quill/flutter_quill_internal.dart'; | ||
import 'package:meta/meta.dart' show experimental; | ||
|
||
import 'src/super_clipboard_service.dart'; | ||
|
||
/// Provides integration of the [`super_clipboard`](https://pub.dev/packages/super_clipboard) | ||
/// package with [`flutter_quill`](https://pub.dev/packages/flutter_quill) package | ||
/// to the system clipboard for rich text operations. | ||
@experimental | ||
class QuillSuperClipboard { | ||
/// Private constructor | ||
QuillSuperClipboard._(); | ||
|
||
/// Use the [`super_clipboard`](https://pub.dev/packages/super_clipboard) | ||
/// plugin as clipboard implementation for [`flutter_quill`](https://pub.dev/packages/flutter_quill) | ||
/// to access the system clipboard for rich text operations. | ||
/// | ||
/// See also: [`quill_native_bridge`](https://pub.dev/packages/quill_native_bridge) | ||
/// which is the default clipboard implementation for [flutter_quill](https://pub.dev/packages/flutter_quill). | ||
@experimental | ||
static void use() { | ||
ClipboardServiceProvider.setInstance(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import 'dart:async' show Completer; | ||
import 'dart:convert' show utf8; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_quill/flutter_quill_internal.dart'; | ||
import 'package:meta/meta.dart' show experimental; | ||
|
||
import 'package:super_clipboard/super_clipboard.dart'; | ||
|
||
/// Implementation using the [super_clipboard](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> copyImageToClipboard(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; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: quill_super_clipboard | ||
description: "A package for integrating the package super_clipboard with flutter_quill to provide access to the system clipboard." | ||
version: 0.0.1 | ||
homepage: https://github.com/FlutterQuill/quill-super-clipboard | ||
repository: https://github.com/FlutterQuill/quill-super-clipboard | ||
issue_tracker: https://github.com/FlutterQuill/quill-super-clipboard/issues | ||
documentation: https://github.com/FlutterQuill/quill-super-clipboard | ||
|
||
environment: | ||
sdk: '>=3.0.0 <4.0.0' | ||
flutter: ">=3.0.0" | ||
|
||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
super_clipboard: ^0.8.24 | ||
# TODO: Publish the package and use latest version of flutter_quill once https://github.com/singerdmx/flutter-quill/pull/2230 is published | ||
flutter_quill: | ||
path: /Users/ellet/Developer/playground/framework_based/flutter/flutter-quill/ | ||
meta: ^1.10.0 | ||
|
||
dev_dependencies: | ||
test: ^1.25.8 | ||
flutter_lints: ^5.0.0 | ||
|
||
flutter: |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:flutter_quill/flutter_quill_internal.dart'; | ||
import 'package:quill_super_clipboard/quill_super_clipboard.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'package:quill_super_clipboard/src/super_clipboard_service.dart'; | ||
|
||
void main() { | ||
test( | ||
'Calling QuillSuperClipboard.use() should set the instance to SuperClipboardService', | ||
() { | ||
QuillSuperClipboard.use(); | ||
expect( | ||
ClipboardServiceProvider.instance, | ||
isA<SuperClipboardService>(), | ||
); | ||
}, | ||
); | ||
} |