Skip to content

Commit

Permalink
redo copy_to_clipboard_button to run javascript after hitting the ser…
Browse files Browse the repository at this point in the history
…ver first
  • Loading branch information
nikochiko committed Sep 23, 2024
1 parent 222f5c0 commit fdee7fc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
15 changes: 9 additions & 6 deletions daras_ai_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ def _render_share_button(self):

def _render_copy_link_button(
self, label: str = "", className: str = "mb-0 ms-lg-2"
):
copy_to_clipboard_button(
) -> bool:
return copy_to_clipboard_button(
label=f"{icons.link} {label}".strip(),
value=self.current_app_url(self.tab),
type="secondary",
Expand Down Expand Up @@ -539,12 +539,15 @@ def _render_share_modal(self, dialog: gui.AlertDialogRef):
)

with gui.div(className="d-flex justify-content-between"):
self._render_copy_link_button(label="Copy Link", className="py-2 px-3 m-0")
if gui.button(
pressed_copy_link = self._render_copy_link_button(
label="Copy Link", className="py-2 px-3 m-0"
)
pressed_done = gui.button(
"Done",
type="primary",
className="py-2 px-5 m-0",
):
)
if pressed_done or pressed_copy_link:
if self.current_pr.visibility != published_run_visibility:
self.current_pr.add_version(
user=self.request.user,
Expand All @@ -553,7 +556,7 @@ def _render_share_modal(self, dialog: gui.AlertDialogRef):
notes=self.current_pr.notes,
visibility=PublishedRunVisibility(published_run_visibility),
)

if pressed_done:
dialog.set_open(False)
gui.rerun()

Expand Down
34 changes: 19 additions & 15 deletions daras_ai_v2/copy_to_clipboard_button_widget.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import typing
from html import escape

Expand All @@ -23,20 +22,25 @@ def copy_to_clipboard_button(
label: str,
*,
value: str,
style: str = "",
style: dict[str, str] | None = None,
className: str = "",
type: typing.Literal["primary", "secondary", "tertiary", "link"] = "primary",
):
return gui.html(
# language="html"
f"""
<button
type="button"
class="btn btn-theme btn-{type} {className}"
onclick="copyToClipboard(this);"
style="{style}"
data-clipboard-text="{escape(value)}">
{label}
</button>
""",
key: str | None = None,
) -> bool:
key = key or "copy-to-clipboard-btn"
pressed = gui.button(
label,
id=key,
className=className,
style=style,
type=type,
**{"data-clipboard-text": value},
)
if pressed:
gui.js(
# language="javascript"
f"""
copyToClipboard(document.getElementById("{escape(key)}"));
"""
)
return pressed
2 changes: 1 addition & 1 deletion daras_ai_v2/manage_api_keys_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _generate_new_key_doc() -> dict:
copy_to_clipboard_button(
"📎 Copy Secret Key",
value=new_api_key,
style="height: 3.2rem",
style={"height": "3.2rem"},
)

return {
Expand Down

0 comments on commit fdee7fc

Please sign in to comment.