From cd17460704e227e0d25c68e6a92dcdf9efa6cece Mon Sep 17 00:00:00 2001 From: Shagen Ogandzhanian Date: Wed, 20 Mar 2024 12:05:36 +0100 Subject: [PATCH] Minor refactoring of ClipboardEventsHandler --- .../foundation/text/ClipboardEventsHandler.js.kt | 11 ++++------- .../foundation/text/ClipboardEventsHandler.wasm.kt | 12 ++++++------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/compose/foundation/foundation/src/jsMain/kotlin/androidx/compose/foundation/text/ClipboardEventsHandler.js.kt b/compose/foundation/foundation/src/jsMain/kotlin/androidx/compose/foundation/text/ClipboardEventsHandler.js.kt index 9a4d61e99444c..d61d0da04e28f 100644 --- a/compose/foundation/foundation/src/jsMain/kotlin/androidx/compose/foundation/text/ClipboardEventsHandler.js.kt +++ b/compose/foundation/foundation/src/jsMain/kotlin/androidx/compose/foundation/text/ClipboardEventsHandler.js.kt @@ -35,7 +35,7 @@ internal actual inline fun rememberClipboardEventsHandler( if (isFocused) { DisposableEffect(textFieldSelectionManager) { - val onCopy = EventListenerWrapper { event -> + val onCopy = EventListener { event -> val textToCopy = textFieldSelectionManager.onCopyWithResult() if (textToCopy != null && event is ClipboardEvent) { event.clipboardData?.setData("text/plain", textToCopy) @@ -43,7 +43,7 @@ internal actual inline fun rememberClipboardEventsHandler( } } - val onPaste = EventListenerWrapper { event -> + val onPaste = EventListener { event -> if (event is ClipboardEvent) { val textToPaste = event.clipboardData?.getData("text/plain") ?: "" event.preventDefault() @@ -51,7 +51,7 @@ internal actual inline fun rememberClipboardEventsHandler( } } - val onCut = EventListenerWrapper { event -> + val onCut = EventListener { event -> if (event is ClipboardEvent) { val cutText = textFieldSelectionManager.onCutWithResult() event.clipboardData?.setData("text/plain", cutText ?: "") @@ -63,7 +63,7 @@ internal actual inline fun rememberClipboardEventsHandler( document.addEventListener("paste", onPaste) document.addEventListener("cut", onCut) - return@DisposableEffect onDispose { + onDispose { document.removeEventListener("copy", onCopy) document.removeEventListener("paste", onPaste) document.removeEventListener("cut", onCut) @@ -71,6 +71,3 @@ internal actual inline fun rememberClipboardEventsHandler( } } } - -private fun EventListenerWrapper(handler: (Event) -> Unit): EventListener = - EventListener { handler(it) } diff --git a/compose/foundation/foundation/src/wasmJsMain/kotlin/androidx/compose/foundation/text/ClipboardEventsHandler.wasm.kt b/compose/foundation/foundation/src/wasmJsMain/kotlin/androidx/compose/foundation/text/ClipboardEventsHandler.wasm.kt index 9bcc7ef324f91..be052153b395f 100644 --- a/compose/foundation/foundation/src/wasmJsMain/kotlin/androidx/compose/foundation/text/ClipboardEventsHandler.wasm.kt +++ b/compose/foundation/foundation/src/wasmJsMain/kotlin/androidx/compose/foundation/text/ClipboardEventsHandler.wasm.kt @@ -24,7 +24,7 @@ import androidx.compose.ui.text.AnnotatedString import kotlinx.browser.document import org.w3c.dom.clipboard.ClipboardEvent import org.w3c.dom.events.Event -import org.w3c.dom.events.EventListener +import org.w3c.dom.events.EventListener as EventListenerInterface @Composable @NonRestartableComposable @@ -35,7 +35,7 @@ internal actual inline fun rememberClipboardEventsHandler( if (isFocused) { DisposableEffect(textFieldSelectionManager) { - val onCopy = EventListenerWrapper { event -> + val onCopy = EventListener { event -> val textToCopy = textFieldSelectionManager.onCopyWithResult() if (textToCopy != null && event is ClipboardEvent) { event.clipboardData?.setData("text/plain", textToCopy) @@ -43,7 +43,7 @@ internal actual inline fun rememberClipboardEventsHandler( } } - val onPaste = EventListenerWrapper { event -> + val onPaste = EventListener { event -> if (event is ClipboardEvent) { val textToPaste = event.clipboardData?.getData("text/plain") ?: "" event.preventDefault() @@ -51,7 +51,7 @@ internal actual inline fun rememberClipboardEventsHandler( } } - val onCut = EventListenerWrapper { event -> + val onCut = EventListener { event -> if (event is ClipboardEvent) { val cutText = textFieldSelectionManager.onCutWithResult() event.clipboardData?.setData("text/plain", cutText ?: "") @@ -63,7 +63,7 @@ internal actual inline fun rememberClipboardEventsHandler( document.addEventListener("paste", onPaste) document.addEventListener("cut", onCut) - return@DisposableEffect onDispose { + onDispose { document.removeEventListener("copy", onCopy) document.removeEventListener("paste", onPaste) document.removeEventListener("cut", onCut) @@ -72,5 +72,5 @@ internal actual inline fun rememberClipboardEventsHandler( } } -private fun EventListenerWrapper(handler: (Event) -> Unit): EventListener = +private fun EventListener(handler: (Event) -> Unit): EventListenerInterface = js("(event) => { handler(event) }")