feat(clipboard-manager): implement clear on iOS and Android (#1462)

This commit is contained in:
Emin Yilmaz
2024-06-28 12:05:48 +09:00
committed by GitHub
parent 03d3cc3677
commit 99d125d86b
4 changed files with 24 additions and 3 deletions

View File

@@ -0,0 +1,5 @@
---
"clipboard-manager": "minor"
---
Add support for clearing clipboard text on iOS and Android.

View File

@@ -92,7 +92,11 @@ class ClipboardPlugin(private val activity: Activity) : Plugin(activity) {
val clipData = when (args) {
is WriteOptions.PlainText -> {
ClipData.newPlainText(args.label, args.text)
} else -> {
invoke.reject("Invalid write options provided")
return
}
}
manager.setPrimaryClip(clipData)
@@ -120,4 +124,12 @@ class ClipboardPlugin(private val activity: Activity) : Plugin(activity) {
invoke.resolveObject(data)
}
@Command
fun clear(invoke: Invoke) {
if (manager.hasPrimaryClip()) {
manager.clearPrimaryClip()
}
invoke.resolve()
}
}

View File

@@ -38,6 +38,12 @@ class ClipboardPlugin: Plugin {
invoke.reject("Clipboard is empty")
}
}
@objc public func clear(_ invoke: Invoke) throws {
let clipboard = UIPasteboard.general
clipboard.items = []
invoke.resolve()
}
}
@_cdecl("init_plugin_clipboard")

View File

@@ -92,9 +92,7 @@ impl<R: Runtime> Clipboard<R> {
}
pub fn clear(&self) -> crate::Result<()> {
Err(crate::Error::Clipboard(
"Unsupported on this platform".to_string(),
))
self.0.run_mobile_plugin("clear", ()).map_err(Into::into)
}
}