From 7fea2f9dbd834f612b5cf7c16654551f527df344 Mon Sep 17 00:00:00 2001 From: Maciek Korsan Date: Tue, 19 Mar 2024 23:33:06 +0100 Subject: [PATCH] Copy data to clipboard --- .../elm/src/Page/ShowEvent.elm | 37 +++++++++++++------ .../elm/src/Page/ShowStream.elm | 3 +- ruby_event_store-browser/public/bootstrap.js | 6 ++- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/ruby_event_store-browser/elm/src/Page/ShowEvent.elm b/ruby_event_store-browser/elm/src/Page/ShowEvent.elm index 0b6fef48b4..111eb1ded1 100644 --- a/ruby_event_store-browser/elm/src/Page/ShowEvent.elm +++ b/ruby_event_store-browser/elm/src/Page/ShowEvent.elm @@ -1,4 +1,4 @@ -module Page.ShowEvent exposing (Model, Msg(..), initCmd, initModel, showJsonTree, update, view) +port module Page.ShowEvent exposing (Model, Msg(..), initCmd, initModel, showJsonTree, update, view) import Api import BrowserTime @@ -12,7 +12,7 @@ import Maybe import Maybe.Extra exposing (values) import Pagination import Route -import Svg exposing (svg, path) +import Svg exposing (path, svg) import Svg.Attributes as SvgAttr import Url @@ -54,14 +54,23 @@ initModel flags eventId = +-- PORT + + +port copyToClipboard : String -> Cmd msg + + + -- UPDATE + type Msg = ChangeOpenedEventDataTreeState JsonTree.State | ChangeOpenedEventMetadataTreeState JsonTree.State | EventFetched (Result Http.Error Api.Event) | CausedEventsFetched (Result Http.Error (Api.PaginatedList Api.Event)) | CausedStreamFetched (Result Http.Error Api.Stream) + | Copy String initCmd : Flags -> String -> Cmd Msg @@ -88,6 +97,9 @@ update msg model = _ -> ( model, Cmd.none ) + Copy content -> + ( model, copyToClipboard content ) + EventFetched (Ok result) -> let event = @@ -201,9 +213,6 @@ view_ model = ] [ text "Loading..." ] ] - - - Api.Loaded event -> showEvent model.flags.rootUrl event model.causedEvents @@ -234,18 +243,24 @@ showEvent baseUrl event maybeCausedEvents = [ class "w-full text-left grid md:grid-cols-3 gap-8 overflow-hidden" ] [ section [ class "space-y-4" ] - [ header [ class "flex justify-between border-gray-400 border-b text-xs pb-2" ] [ - h2 [ class "text-gray-500 uppercase font-bold " ] [ text "Event ID" ], - button [ class "text-red-700 no-underline" ] [ text "Copy" ] - ] + [ header [ class "flex justify-between border-gray-400 border-b text-xs pb-2" ] + [ h2 [ class "text-gray-500 uppercase font-bold " ] [ text "Event ID" ] + , button [ class "text-red-700 no-underline", onClick (Copy event.eventId) ] [ text "Copy" ] + ] , div [ class "text-sm font-medium font-mono" ] [ text event.eventId ] ] , section [ class "space-y-4" ] - [ h2 [ class "border-gray-400 border-b text-gray-500 uppercase font-bold text-xs pb-2" ] [ text "Raw Data" ] + [ header [ class "flex justify-between border-gray-400 border-b text-xs pb-2" ] + [ h2 [ class "text-gray-500 uppercase font-bold" ] [ text "Raw Data" ] + , button [ class "text-red-700 no-underline", onClick (Copy event.rawData) ] [ text "Copy" ] + ] , div [ class "overflow-auto w-full" ] [ showJsonTree event.rawData event.dataTreeState (\s -> ChangeOpenedEventDataTreeState s) ] ] , section [ class "space-y-4" ] - [ h2 [ class "border-gray-400 border-b text-gray-500 uppercase font-bold text-xs pb-2" ] [ text "Raw Metadata" ] + [ header [ class "flex justify-between border-gray-400 border-b text-xs pb-2" ] + [ h2 [ class "text-gray-500 uppercase font-bold" ] [ text "Raw Metadata" ] + , button [ class "text-red-700 no-underline", onClick (Copy event.rawMetadata) ] [ text "Copy" ] + ] , div [ class "overflow-auto w-full" ] [ showJsonTree event.rawMetadata event.metadataTreeState (\s -> ChangeOpenedEventMetadataTreeState s) ] ] ] diff --git a/ruby_event_store-browser/elm/src/Page/ShowStream.elm b/ruby_event_store-browser/elm/src/Page/ShowStream.elm index 2c0808a641..5abe375526 100644 --- a/ruby_event_store-browser/elm/src/Page/ShowStream.elm +++ b/ruby_event_store-browser/elm/src/Page/ShowStream.elm @@ -4,8 +4,7 @@ import Api import BrowserTime import Flags exposing (Flags) import Html exposing (..) -import Html.Attributes exposing (class, disabled, href) -import Html.Events exposing (onClick) +import Html.Attributes exposing (class, href) import Http import Pagination import Route diff --git a/ruby_event_store-browser/public/bootstrap.js b/ruby_event_store-browser/public/bootstrap.js index 66f3f55b40..d3ebd46adf 100644 --- a/ruby_event_store-browser/public/bootstrap.js +++ b/ruby_event_store-browser/public/bootstrap.js @@ -1,3 +1,7 @@ -Elm.Main.init({ +const app = Elm.Main.init({ flags: JSON.parse(document.querySelector("meta[name='ruby-event-store-browser-settings']").getAttribute("content")), }); + +app.ports.copyToClipboard.subscribe(function(message) { + navigator.clipboard.writeText(message); +}); \ No newline at end of file