Skip to content
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

Add copy to clipboard buttons to nixos configuration and nix-shell tabs #801

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ repl-temp-*
result
src-url
.direnv/
.sl
8 changes: 7 additions & 1 deletion frontend/src/Page/Flakes.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Page.Flakes exposing
port module Page.Flakes exposing
( Model(..)
, Msg(..)
, init
Expand Down Expand Up @@ -41,6 +41,9 @@ import Search
)


port copyToClipboard : String -> Cmd msg



-- MODEL

Expand Down Expand Up @@ -140,6 +143,9 @@ update navKey msg model nixosChannels =
in
( newModel, Cmd.map Page.Packages.SearchMsg newCmd ) |> Tuple.mapBoth PackagesModel (Cmd.map PackagesMsg)

CopyToClipboard text ->
( model, Cmd.none )

_ ->
( model, Cmd.none )

Expand Down
37 changes: 32 additions & 5 deletions frontend/src/Page/Packages.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Page.Packages exposing
port module Page.Packages exposing
( Model
, Msg(..)
, decodeResultAggregations
Expand All @@ -17,6 +17,7 @@ import Html
exposing
( Html
, a
, button
, code
, div
, em
Expand All @@ -35,6 +36,7 @@ import Html.Attributes
, classList
, href
, id
, style
, target
)
import Html.Events exposing (onClick)
Expand All @@ -54,6 +56,9 @@ import Search
import Utils


port copyToClipboard : String -> Cmd msg



-- MODEL

Expand Down Expand Up @@ -192,6 +197,7 @@ platforms =

type Msg
= SearchMsg (Search.Msg ResultItemSource ResultAggregations)
| CopyToClipboard String


update :
Expand All @@ -214,6 +220,9 @@ update navKey msg model nixosChannels =
in
( newModel, Cmd.map SearchMsg newCmd )

CopyToClipboard text ->
( model, copyToClipboard text )



-- VIEW
Expand Down Expand Up @@ -651,7 +660,7 @@ viewResultItem nixosChannels channel showInstallDetails show item =
, class "tab-pane"
, id "package-details-nixpkgs"
]
[ pre [ class "code-block shell-command" ]
[ pre [ class "code-block shell-command", style "display" "block" ]
[ text "# without flakes:\nnix-env -iA nixpkgs."
, strong [] [ text item.source.attr_name ]
, text "\n# with flakes:\nnix profile install nixpkgs#"
Expand All @@ -677,9 +686,23 @@ viewResultItem nixosChannels channel showInstallDetails show item =
, id "package-details-nixpkgs"
]
[ pre [ class "code-block" ]
[ text <| " environment.systemPackages = [\n pkgs."
, strong [] [ text item.source.attr_name ]
, text <| "\n ];"
[ div []
[ text <| " environment.systemPackages = [\n pkgs."
, strong [] [ text item.source.attr_name ]
, text <| "\n ];"
]
, button
[ class "copy-to-clipboard-button"
, onClick <|
CopyToClipboard
(" environment.systemPackages = [\n pkgs."
++ item.source.attr_name
++ "\n ];"
)
]
[ text
"📋"
]
]
]
, div
Expand Down Expand Up @@ -707,6 +730,10 @@ viewResultItem nixosChannels channel showInstallDetails show item =
[ pre [ class "code-block shell-command" ]
[ text "nix-shell -p "
, strong [] [ text item.source.attr_name ]
, button [ class "copy-to-clipboard-button", onClick <| CopyToClipboard ("nix-shell -p " ++ item.source.attr_name) ]
[ text
"📋"
]
]
]
]
Expand Down
27 changes: 26 additions & 1 deletion frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require("elm-keyboard-shortcut")

const {Elm} = require('./Main');

Elm.Main.init({
const app = Elm.Main.init({
flags: {
elasticsearchMappingSchemaVersion: parseInt(process.env.ELASTICSEARCH_MAPPING_SCHEMA_VERSION),
elasticsearchUrl: process.env.ELASTICSEARCH_URL || '/backend',
Expand All @@ -14,3 +14,28 @@ Elm.Main.init({
nixosChannels : JSON.parse(process.env.NIXOS_CHANNELS)
}
});

app.ports.copyToClipboard.subscribe(fallbackCopyTextToClipboard)

function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement('textarea')
textArea.value = text

// avoid scrolling to bottom
textArea.style.top = '0'
textArea.style.left = '0'
textArea.style.position = 'fixed'

document.body.appendChild(textArea)
textArea.focus()
textArea.select()

try {
return document.execCommand('copy')
} catch(err) {
console.error('fallback: oops, unable to copy', err)
return false
} finally {
document.body.removeChild(textArea)
}
}
8 changes: 7 additions & 1 deletion frontend/src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,14 @@ body {
}

.code-block {
display: block;
display: flex;
cursor: text;
align-items: center;
}

.copy-to-clipboard-button {
height: max-content;
margin-left: auto;
}

.shell-command:before {
Expand Down
Loading