Skip to content

Commit 99d125d

Browse files
authored
feat(clipboard-manager): implement clear on iOS and Android (#1462)
1 parent 03d3cc3 commit 99d125d

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

.changes/clear-clipboard.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"clipboard-manager": "minor"
3+
---
4+
5+
Add support for clearing clipboard text on iOS and Android.

plugins/clipboard-manager/android/src/main/java/ClipboardPlugin.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ class ClipboardPlugin(private val activity: Activity) : Plugin(activity) {
9292
val clipData = when (args) {
9393
is WriteOptions.PlainText -> {
9494
ClipData.newPlainText(args.label, args.text)
95+
} else -> {
96+
invoke.reject("Invalid write options provided")
97+
return
9598
}
99+
96100
}
97101

98102
manager.setPrimaryClip(clipData)
@@ -120,4 +124,12 @@ class ClipboardPlugin(private val activity: Activity) : Plugin(activity) {
120124

121125
invoke.resolveObject(data)
122126
}
127+
128+
@Command
129+
fun clear(invoke: Invoke) {
130+
if (manager.hasPrimaryClip()) {
131+
manager.clearPrimaryClip()
132+
}
133+
invoke.resolve()
134+
}
123135
}

plugins/clipboard-manager/ios/Sources/ClipboardPlugin.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class ClipboardPlugin: Plugin {
3838
invoke.reject("Clipboard is empty")
3939
}
4040
}
41+
42+
@objc public func clear(_ invoke: Invoke) throws {
43+
let clipboard = UIPasteboard.general
44+
clipboard.items = []
45+
invoke.resolve()
46+
}
4147
}
4248

4349
@_cdecl("init_plugin_clipboard")

plugins/clipboard-manager/src/mobile.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ impl<R: Runtime> Clipboard<R> {
9292
}
9393

9494
pub fn clear(&self) -> crate::Result<()> {
95-
Err(crate::Error::Clipboard(
96-
"Unsupported on this platform".to_string(),
97-
))
95+
self.0.run_mobile_plugin("clear", ()).map_err(Into::into)
9896
}
9997
}
10098

0 commit comments

Comments
 (0)