-
Notifications
You must be signed in to change notification settings - Fork 0
feature: Add Clipboard API support to uni-dom #404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Add Clipboard object for reading and writing to the system clipboard: - writeText/readText for async clipboard operations - writeTextRx for reactive clipboard writing - onCopy/onCut/onPaste event handlers - copyAs/cutAs for customizing clipboard content - copyOnClick/copyOnClickDynamic for one-click copy buttons - Fallback to execCommand for older browsers - isSupported check for feature detection Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary of ChangesHello @xerial, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a comprehensive Clipboard API to uni-dom, providing both asynchronous Future-based and reactive Rx-based methods for interacting with the system clipboard. It includes support for reading and writing text, intercepting clipboard events (copy, cut, paste), and provides a fallback mechanism using execCommand for older browsers. The changes are well-structured and include a new test suite. My review includes a couple of suggestions to improve the implementation of the reactive API and reduce code duplication for better maintainability.
| def writeTextRx(text: String): Rx[Boolean] = | ||
| val result = Rx.variable(false) | ||
| writeText(text).onComplete { | ||
| case Success(_) => | ||
| result := true | ||
| case Failure(_) => | ||
| result := false | ||
| } | ||
| result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of writeTextRx can be ambiguous. It returns an RxVar that is initialized to false, and then updated to true on success or false again on failure. This makes it difficult for a subscriber to distinguish between the initial "in-progress" state and a "failure" state.
A more idiomatic and clearer approach would be to return an Rx that only emits a value when the write operation is complete. Using Rx.future would achieve this and make the stream's behavior more predictable.
def writeTextRx(text: String): Rx[Boolean] =
Rx.future {
writeText(text)
.map(_ => true)
.recover { case _ => false }
}| def copyOnClick( | ||
| text: String, | ||
| onSuccess: () => Unit = () => (), | ||
| onFailure: Throwable => Unit = _ => () | ||
| ): DomNode = | ||
| val onclick = HtmlTags.handler[dom.MouseEvent]("onclick") | ||
| onclick { (_: dom.MouseEvent) => | ||
| writeText(text).onComplete { | ||
| case Success(_) => | ||
| onSuccess() | ||
| case Failure(e) => | ||
| onFailure(e) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create a click handler that copies dynamic text to clipboard. | ||
| * | ||
| * @param getText | ||
| * Function that returns the text to copy | ||
| * @param onSuccess | ||
| * Optional callback when copy succeeds | ||
| * @param onFailure | ||
| * Optional callback when copy fails | ||
| * @return | ||
| * DomNode modifier | ||
| */ | ||
| def copyOnClickDynamic( | ||
| getText: () => String, | ||
| onSuccess: () => Unit = () => (), | ||
| onFailure: Throwable => Unit = _ => () | ||
| ): DomNode = | ||
| val onclick = HtmlTags.handler[dom.MouseEvent]("onclick") | ||
| onclick { (_: dom.MouseEvent) => | ||
| writeText(getText()).onComplete { | ||
| case Success(_) => | ||
| onSuccess() | ||
| case Failure(e) => | ||
| onFailure(e) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The methods copyOnClick and copyOnClickDynamic have very similar implementations. This code duplication can be reduced by extracting the common logic into a private helper method. This will improve maintainability and reduce the chance of bugs if this logic needs to be updated in the future.
private def createCopyHandler(
getText: () => String,
onSuccess: () => Unit,
onFailure: Throwable => Unit
): DomNode = {
val onclick = HtmlTags.handler[dom.MouseEvent]("onclick")
onclick { (_: dom.MouseEvent) =>
writeText(getText()).onComplete {
case Success(_) => onSuccess()
case Failure(e) => onFailure(e)
}
}
}
def copyOnClick(
text: String,
onSuccess: () => Unit = () => (),
onFailure: Throwable => Unit = _ => ()
): DomNode =
createCopyHandler(() => text, onSuccess, onFailure)
/**
* Create a click handler that copies dynamic text to clipboard.
*
* @param getText
* Function that returns the text to copy
* @param onSuccess
* Optional callback when copy succeeds
* @param onFailure
* Optional callback when copy fails
* @return
* DomNode modifier
*/
def copyOnClickDynamic(
getText: () => String,
onSuccess: () => Unit = () => (),
onFailure: Throwable => Unit = _ => ()
): DomNode =
createCopyHandler(getText, onSuccess, onFailure)- Change writeTextRx to return Rx[Option[Boolean]] (None initially, Some on complete) - Fix onCopy/onCut to handle input/textarea selections via selectionStart/selectionEnd Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary
Clipboardobject for reading and writing to the system clipboardexecCommandfor older browsersFeatures
API
writeText(text)readText()writeTextRx(text)onCopy(handler)onCut(handler)onPaste(handler)copyAs(getText)cutAs(getText)copyOnClick(text)copyOnClickDynamic(getText)isSupportedTest plan
🤖 Generated with Claude Code