Skip to content

Commit

Permalink
refactor: rename copyHTMLToClipboard() and getClipboardHTML() to copy…
Browse files Browse the repository at this point in the history
…HtmlToClipboard() and getClipboardHtml()
  • Loading branch information
EchoEllet committed Sep 26, 2024
1 parent 17845c8 commit d3e9357
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import 'clipboard_service.dart';
class DefaultClipboardService extends ClipboardService {
@override
Future<String?> getHtmlText() async {
if (QuillNativeBridgePlatformFeature.getClipboardHTML.isUnsupported) {
if (QuillNativeBridgePlatformFeature.getClipboardHtml.isUnsupported) {
return null;
}
return await QuillNativeBridge.getClipboardHTML();
return await QuillNativeBridge.getClipboardHtml();
}

@override
Expand Down
4 changes: 3 additions & 1 deletion pubspec_overrides.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# TODO: Remove this file completely once https://github.com/singerdmx/flutter-quill/pull/2230 is complete before publishing
dependency_overrides:
quill_native_bridge:
path: ./quill_native_bridge/quill_native_bridge
path: ./quill_native_bridge/quill_native_bridge
quill_native_bridge_platform_interface:
path: ./quill_native_bridge/quill_native_bridge_platform_interface
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,41 @@ void main() {
);
});

group('getClipboardHTML and copyHTMLToClipbaord', () {
group('getClipboardHtml and copyHtmlToClipbaord', () {
test('copying HTML to the clipboard should make it accessible', () async {
const htmlToCopy =
'<div class="container"><h1>Test Document</h1><p>This is a <strong>sample</strong> paragraph with <a href="https://example.com">a link</a> and some <span style="color:red;">red text</span>.</p><ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul><footer>Footer content here</footer></div>';
await QuillNativeBridge.copyHTMLToClipboard(htmlToCopy);
final clipboardHTML = await QuillNativeBridge.getClipboardHTML();
expect(htmlToCopy, clipboardHTML);
await QuillNativeBridge.copyHtmlToClipboard(htmlToCopy);
final clipboardHtml = await QuillNativeBridge.getClipboardHtml();
expect(htmlToCopy, clipboardHtml);
});

test('copying HTML should return the HTML that was recently copied',
() async {
const html1 = '<pre style="font-family: monospace;">HTML</pre>';
const html2 = '<div style="border: 1px solid;">HTML Div</div>';

await QuillNativeBridge.copyHTMLToClipboard(html1);
await QuillNativeBridge.copyHTMLToClipboard(html2);
await QuillNativeBridge.copyHtmlToClipboard(html1);
await QuillNativeBridge.copyHtmlToClipboard(html2);

final clipboardHTML = await QuillNativeBridge.getClipboardHTML();
expect(clipboardHTML, isNot(html1));
expect(clipboardHTML, html2);
final clipboardHtml = await QuillNativeBridge.getClipboardHtml();
expect(clipboardHtml, isNot(html1));
expect(clipboardHtml, html2);
});
// TODO: See if there is a need for writing a similar test for getClipboardImage
test(
'getClipboardHTML should return the HTML content after copying HTML, '
'getClipboardHtml should return the HTML content after copying HTML, '
'and should no longer return HTML once an image (or any non-HTML item) '
'has been copied to the clipboard after that.',
() async {
const html = '<pre style="font-family: monospace;">HTML</pre>';

// Copy HTML to clipboard before copying an image

await QuillNativeBridge.copyHTMLToClipboard(html);
await QuillNativeBridge.copyHtmlToClipboard(html);

expect(
await QuillNativeBridge.getClipboardHTML(),
await QuillNativeBridge.getClipboardHtml(),
html,
);

Expand All @@ -91,16 +91,16 @@ void main() {
await QuillNativeBridge.copyImageToClipboard(imageBytes);

expect(
await QuillNativeBridge.getClipboardHTML(),
await QuillNativeBridge.getClipboardHtml(),
null,
);

// Copy HTML to clipboard before copying plain text

await QuillNativeBridge.copyHTMLToClipboard(html);
await QuillNativeBridge.copyHtmlToClipboard(html);

expect(
await QuillNativeBridge.getClipboardHTML(),
await QuillNativeBridge.getClipboardHtml(),
html,
);

Expand All @@ -116,7 +116,7 @@ void main() {
);

expect(
await QuillNativeBridge.getClipboardHTML(),
await QuillNativeBridge.getClipboardHtml(),
null,
);
},
Expand All @@ -129,8 +129,8 @@ void main() {
() async {
const exampleHtml = '<div style="border: 1px solid;">HTML Div</div>';

await QuillNativeBridge.copyHTMLToClipboard(exampleHtml);
final clipboardHtml = await QuillNativeBridge.getClipboardHTML();
await QuillNativeBridge.copyHtmlToClipboard(exampleHtml);
final clipboardHtml = await QuillNativeBridge.getClipboardHtml();

if (clipboardHtml == null) {
fail(
Expand Down
12 changes: 6 additions & 6 deletions quill_native_bridge/quill_native_bridge/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ class Buttons extends StatelessWidget {
),
ElevatedButton.icon(
onPressed: () => _onButtonClick(
QuillNativeBridgePlatformFeature.getClipboardHTML,
QuillNativeBridgePlatformFeature.getClipboardHtml,
context: context,
),
label: const Text('Get HTML from Clipboard'),
icon: const Icon(Icons.html),
),
ElevatedButton.icon(
onPressed: () => _onButtonClick(
QuillNativeBridgePlatformFeature.copyHTMLToClipboard,
QuillNativeBridgePlatformFeature.copyHtmlToClipboard,
context: context,
),
label: const Text('Copy HTML to Clipboard'),
Expand Down Expand Up @@ -114,7 +114,7 @@ class Buttons extends StatelessWidget {
? "You're running the app on iOS simulator"
: "You're running the app on real iOS device.");
break;
case QuillNativeBridgePlatformFeature.getClipboardHTML:
case QuillNativeBridgePlatformFeature.getClipboardHtml:
if (isFeatureUnsupported) {
scaffoldMessenger.showText(
isFeatureWebUnsupported
Expand All @@ -123,7 +123,7 @@ class Buttons extends StatelessWidget {
);
return;
}
final result = await QuillNativeBridge.getClipboardHTML();
final result = await QuillNativeBridge.getClipboardHtml();
if (result == null) {
scaffoldMessenger.showText(
'The HTML is not available on the clipboard.',
Expand All @@ -135,7 +135,7 @@ class Buttons extends StatelessWidget {
);
debugPrint('HTML from the clipboard: $result');
break;
case QuillNativeBridgePlatformFeature.copyHTMLToClipboard:
case QuillNativeBridgePlatformFeature.copyHtmlToClipboard:
if (isFeatureUnsupported) {
scaffoldMessenger.showText(
isFeatureWebUnsupported
Expand All @@ -151,7 +151,7 @@ class Buttons extends StatelessWidget {
<span style="color:red;">Red text</span>
<span style="background-color:yellow;">Highlighted text</span>
''';
await QuillNativeBridge.copyHTMLToClipboard(html);
await QuillNativeBridge.copyHtmlToClipboard(html);
scaffoldMessenger.showText(
'HTML copied to the clipboard: $html',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class QuillNativeBridge {
/// permission for pasting (on some platforms such as iOS).
///
/// Currently only supports **Android**, **iOS**, **macOS**, **Windows**, **Linux**, and the **Web**.
static Future<String?> getClipboardHTML() => _platform.getClipboardHTML();
static Future<String?> getClipboardHtml() => _platform.getClipboardHtml();

/// Copy the [html] to the clipboard to be pasted on other apps.
///
Expand All @@ -46,8 +46,8 @@ class QuillNativeBridge {
/// such as the [copy_event](https://developer.mozilla.org/en-US/docs/Web/API/Element/copy_event).
///
/// Currently only supports **Android**, **iOS**, **macOS**, **Linux**, and the **Web**.
static Future<void> copyHTMLToClipboard(String html) =>
_platform.copyHTMLToClipboard(html);
static Future<void> copyHtmlToClipboard(String html) =>
_platform.copyHtmlToClipboard(html);

/// Copy the [imageBytes] to Clipboard to be pasted on other apps.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class QuillNativeBridgeAndroid extends QuillNativeBridgePlatform {
}

@override
Future<String?> getClipboardHTML() async => _hostApi.getClipboardHtml();
Future<String?> getClipboardHtml() async => _hostApi.getClipboardHtml();

@override
Future<void> copyHTMLToClipboard(String html) =>
Future<void> copyHtmlToClipboard(String html) =>
_hostApi.copyHtmlToClipboard(html);

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class QuillNativeBridgeIos extends QuillNativeBridgePlatform {
Future<bool> isIOSSimulator() => _hostApi.isIosSimulator();

@override
Future<String?> getClipboardHTML() => _hostApi.getClipboardHtml();
Future<String?> getClipboardHtml() => _hostApi.getClipboardHtml();

@override
Future<void> copyHTMLToClipboard(String html) =>
Future<void> copyHtmlToClipboard(String html) =>
_hostApi.copyHtmlToClipboard(html);

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class QuillNativeBridgeLinux extends QuillNativeBridgePlatform {
}

@override
Future<String?> getClipboardHTML() async {
Future<String?> getClipboardHtml() async {
final xclipFile = await extractBinaryFromAsset(kXclipAssetFile);
try {
// TODO: Write a test case where copying an image and then retrieving HTML
Expand Down Expand Up @@ -102,7 +102,7 @@ class QuillNativeBridgeLinux extends QuillNativeBridgePlatform {
}

@override
Future<void> copyHTMLToClipboard(String html) async {
Future<void> copyHtmlToClipboard(String html) async {
final xclipFile = await extractBinaryFromAsset(kXclipAssetFile);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class QuillNativeBridgeMacOS extends QuillNativeBridgePlatform {
);

@override
Future<String?> getClipboardHTML() => _hostApi.getClipboardHtml();
Future<String?> getClipboardHtml() => _hostApi.getClipboardHtml();

@override
Future<void> copyHTMLToClipboard(String html) =>
Future<void> copyHtmlToClipboard(String html) =>
_hostApi.copyHtmlToClipboard(html);

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ abstract class QuillNativeBridgePlatform extends PlatformInterface {
Future<bool> isIOSSimulator() =>
throw UnimplementedError('isIOSSimulator() has not been implemented.');

// TODO: rename getClipboardHTML() and their related usages to getClipboardHtml()
/// Return HTML from the Clipboard.
Future<String?> getClipboardHTML() =>
throw UnimplementedError('getClipboardHTML() has not been implemented.');
Future<String?> getClipboardHtml() =>
throw UnimplementedError('getClipboardHtml() has not been implemented.');

/// Copy the [html] to the clipboard to be pasted on other apps.
Future<void> copyHTMLToClipboard(String html) => throw UnimplementedError(
'copyHTMLToClipboard() has not been implemented.');
Future<void> copyHtmlToClipboard(String html) => throw UnimplementedError(
'copyHtmlToClipboard() has not been implemented.');

/// Copy the [imageBytes] to Clipboard to be pasted on other apps.
Future<void> copyImageToClipboard(Uint8List imageBytes) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'package:flutter/foundation.dart'
/// The features/methods provided by the plugin
enum QuillNativeBridgePlatformFeature {
isIOSSimulator(hasWebSupport: false),
getClipboardHTML(hasWebSupport: true),
copyHTMLToClipboard(hasWebSupport: true),
getClipboardHtml(hasWebSupport: true),
copyHtmlToClipboard(hasWebSupport: true),
copyImageToClipboard(hasWebSupport: true),
getClipboardImage(hasWebSupport: true),
getClipboardGif(hasWebSupport: false);
Expand Down Expand Up @@ -45,14 +45,14 @@ enum QuillNativeBridgePlatformFeature {
return switch (this) {
QuillNativeBridgePlatformFeature.isIOSSimulator =>
!kIsWeb && defaultTargetPlatform == TargetPlatform.iOS,
QuillNativeBridgePlatformFeature.getClipboardHTML => {
QuillNativeBridgePlatformFeature.getClipboardHtml => {
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
TargetPlatform.windows,
TargetPlatform.linux,
}.contains(defaultTargetPlatform),
QuillNativeBridgePlatformFeature.copyHTMLToClipboard => {
QuillNativeBridgePlatformFeature.copyHtmlToClipboard => {
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,32 @@ class MethodChannelQuillNativeBridge implements QuillNativeBridgePlatform {
}

@override
Future<String?> getClipboardHTML() async {
Future<String?> getClipboardHtml() async {
assert(() {
if (QuillNativeBridgePlatformFeature.getClipboardHTML.isUnsupported) {
if (QuillNativeBridgePlatformFeature.getClipboardHtml.isUnsupported) {
throw FlutterError(
'getClipboardHTML() is currently not supported on $defaultTargetPlatform.',
'getClipboardHtml() is currently not supported on $defaultTargetPlatform.',
);
}
return true;
}());
final htmlText =
await _methodChannel.invokeMethod<String?>('getClipboardHTML');
await _methodChannel.invokeMethod<String?>('getClipboardHtml');
return htmlText;
}

@override
Future<void> copyHTMLToClipboard(String html) async {
Future<void> copyHtmlToClipboard(String html) async {
assert(() {
if (QuillNativeBridgePlatformFeature.copyHTMLToClipboard.isUnsupported) {
if (QuillNativeBridgePlatformFeature.copyHtmlToClipboard.isUnsupported) {
throw FlutterError(
'copyHTMLToClipboard() is currently not supported on $defaultTargetPlatform.',
'copyHtmlToClipboard() is currently not supported on $defaultTargetPlatform.',
);
}
return true;
}());
await _methodChannel.invokeMethod<void>(
'copyHTMLToClipboard',
'copyHtmlToClipboard',
html,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class MockQuillNativeBridgePlatform
Future<bool> isIOSSimulator() async => false;

@override
Future<String?> getClipboardHTML() async {
Future<String?> getClipboardHtml() async {
return '<center>Invalid HTML</center>';
}

String? primaryHTMLClipbaord;

@override
Future<void> copyHTMLToClipboard(String html) async {
Future<void> copyHtmlToClipboard(String html) async {
primaryHTMLClipbaord = html;
}

Expand Down Expand Up @@ -55,9 +55,9 @@ void main() {
expect(await QuillNativeBridgePlatform.instance.isIOSSimulator(), false);
});

test('getClipboardHTML()', () async {
test('getClipboardHtml()', () async {
expect(
await QuillNativeBridgePlatform.instance.getClipboardHTML(),
await QuillNativeBridgePlatform.instance.getClipboardHtml(),
'<center>Invalid HTML</center>',
);
});
Expand All @@ -75,13 +75,13 @@ void main() {
);
});

test('copyHTMLToClipboard()', () async {
test('copyHtmlToClipboard()', () async {
const html = '<pre>HTML</pre>';
expect(
fakePlatform.primaryHTMLClipbaord,
null,
);
await QuillNativeBridgePlatform.instance.copyHTMLToClipboard(html);
await QuillNativeBridgePlatform.instance.copyHtmlToClipboard(html);
expect(
fakePlatform.primaryHTMLClipbaord,
html,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class QuillNativeBridgeWeb extends QuillNativeBridgePlatform {
}

@override
Future<String?> getClipboardHTML() async {
Future<String?> getClipboardHtml() async {
if (isClipbaordApiUnsupported) {
throw UnsupportedError(
'Could not retrieve HTML from the clipboard.\n'
Expand All @@ -51,7 +51,7 @@ class QuillNativeBridgeWeb extends QuillNativeBridgePlatform {
}

@override
Future<void> copyHTMLToClipboard(String html) async {
Future<void> copyHtmlToClipboard(String html) async {
if (isClipbaordApiUnsupported) {
throw UnsupportedError(
'Could not copy HTML to the clipboard.\n'
Expand Down
Loading

0 comments on commit d3e9357

Please sign in to comment.