Skip to content

Commit

Permalink
ai: add 'Generate with AI' to template selection as Features.AINewDoc…
Browse files Browse the repository at this point in the history
…ument
  • Loading branch information
AdrianoFerrari committed Nov 1, 2024
1 parent 200ac80 commit 28cf366
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/elm/Doc/UI.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Browser.Dom exposing (Element)
import Coders exposing (treeToMarkdownString)
import Doc.TreeStructure as TreeStructure exposing (defaultTree)
import Doc.TreeUtils as TreeUtils exposing (..)
import Feature
import Features exposing (Feature(..))
import GlobalData exposing (GlobalData)
import Html exposing (Html, a, div, h2, h3, h5, hr, input, li, pre, span, textarea)
import Html.Attributes exposing (..)
Expand All @@ -18,6 +20,7 @@ import Markdown.Renderer exposing (Renderer)
import Octicons as Icon exposing (defaultOptions)
import Regex exposing (Regex, replace)
import Route
import Session exposing (LoggedIn)
import SharedUI exposing (ctrlOrCmdText, modalWrapper)
import Svg exposing (g, svg)
import Svg.Attributes exposing (d, fill, fontFamily, fontSize, fontWeight, preserveAspectRatio, stroke, strokeDasharray, strokeDashoffset, strokeLinecap, strokeLinejoin, strokeMiterlimit, strokeWidth, textAnchor, version, viewBox)
Expand Down Expand Up @@ -189,23 +192,33 @@ viewDocumentLoadingSpinner =


viewTemplateSelector :
Language
LoggedIn
-> Language
->
{ modalClosed : msg
, aiNewClicked : msg
, importBulkClicked : msg
, importTextClicked : msg
, importOpmlRequested : msg
, importJSONRequested : msg
}
-> List (Html msg)
viewTemplateSelector language msgs =
viewTemplateSelector session language msgs =
[ div [ id "templates-block" ]
[ h2 [] [ text language New ]
, div [ class "template-row" ]
[ a [ id "template-new", class "template-item", href (Route.toString Route.DocNew) ]
[ div [ classList [ ( "template-thumbnail", True ), ( "new", True ) ] ] []
, div [ class "template-title" ] [ text language HomeBlank ]
]
, if Feature.enabled AINewDocument session then
div [ id "template-ai-new", class "template-item", onClick msgs.aiNewClicked ]
[ div [ classList [ ( "template-thumbnail", True ) ] ] [ Icon.circuitBoard (Icon.defaultOptions |> Icon.size 48) ]
, div [ class "template-title" ] [ textNoTr "Generate with AI" ]
]

else
textNoTr ""
]
, h2 [] [ text language ImportSectionTitle ]
, div [ class "template-row" ]
Expand Down
43 changes: 43 additions & 0 deletions src/elm/Doc/UIStyled.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Doc.UIStyled exposing (viewAINewPrompt)

import Css exposing (..)
import Html exposing (Html)
import Html.Styled exposing (div, label, text, textarea, toUnstyled)
import Html.Styled.Attributes exposing (autofocus, css, for, id, rows)
import Html.Styled.Events exposing (onInput)
import SharedUI exposing (modalWrapper)
import Styles exposing (colors)
import Translation exposing (Language)


viewAINewPrompt : Language -> { modalClosed : msg, promptInput : String -> msg } -> List (Html msg)
viewAINewPrompt lang msgs =
[ div [ css [ displayFlex, flexDirection column, property "gap" "10px" ] ]
[ label [ for "ai-new-prompt", css [ fontWeight bold ] ] [ text "Prompt" ]
, textarea
[ id "ai-new-prompt"
, css [ fontSize (px 16), padding (px 10) ]
, onInput msgs.promptInput
, autofocus True
, rows 8
]
[]
, div [ css [ displayFlex, justifyContent flexEnd ] ]
[ div
[ css
[ fontWeight bold
, color colors.nearWhite
, backgroundColor colors.green
, padding (px 10)
, borderRadius (px 6)
, cursor pointer
, hover [ boxShadow4 zero (px 2) (px 3) (rgba 0 0 0 0.2) ]
, active [ marginTop (px 1) ]
]
]
[ text "Generate" ]
]
]
]
|> List.map toUnstyled
|> modalWrapper msgs.modalClosed Nothing Nothing "Generate Document"
4 changes: 4 additions & 0 deletions src/elm/Features.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Feature
= VotingAppLinkInMenu
| VotingAppLinkInSidebar
| AIPromptFeature
| AINewDocument


decoder : Decoder (List Feature)
Expand All @@ -30,5 +31,8 @@ maybeFeature str =
"ai-prompt-feature" ->
Just AIPromptFeature

"ai-new-document" ->
Just AINewDocument

_ ->
Nothing
21 changes: 20 additions & 1 deletion src/elm/Page/App.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Doc.Metadata as Metadata exposing (Metadata)
import Doc.Switcher
import Doc.TreeStructure as TreeStructure exposing (defaultTree)
import Doc.UI as UI
import Doc.UIStyled as UIStyled
import Doc.VideoViewer as VideoViewer
import Feature
import Features exposing (Feature(..))
Expand Down Expand Up @@ -113,6 +114,7 @@ type ModalState
| MigrateModal
| SidebarContextMenu String ( Float, Float )
| TemplateSelector
| AINewPrompt String
| HelpScreen
| VideoViewer VideoViewer.Model
| Wordcount Page.Doc.Model
Expand Down Expand Up @@ -406,6 +408,7 @@ type Msg
| ImportJSONIdGenerated Tree String String
| ImportSingleCompleted String
-- AI
| AINewClicked
| AIButtonClicked
| AIPromptFieldChanged String
-- Misc UI
Expand Down Expand Up @@ -1572,11 +1575,19 @@ update msg model =
( model, Route.pushUrl model.navKey (Route.DocUntitled docId) )

-- AI
AINewClicked ->
( { model | modalState = AINewPrompt "" }
, Task.attempt (always NoOp) (Browser.Dom.focus "ai-new-prompt")
)

AIButtonClicked ->
applyParentMsg OpenAIPrompt ( model, Cmd.none )

AIPromptFieldChanged newField ->
case model.modalState of
AINewPrompt _ ->
( { model | modalState = AINewPrompt newField }, Cmd.none )

AIPrompt isWaiting _ ->
( { model | modalState = AIPrompt isWaiting newField }, Cmd.none )

Expand Down Expand Up @@ -2437,14 +2448,22 @@ viewModal globalData session modalState =
]

TemplateSelector ->
UI.viewTemplateSelector language
UI.viewTemplateSelector session
language
{ modalClosed = ModalClosed
, aiNewClicked = AINewClicked
, importBulkClicked = ImportBulkClicked
, importTextClicked = ImportTextClicked
, importOpmlRequested = ImportOpmlRequested
, importJSONRequested = ImportJSONRequested
}

AINewPrompt _ ->
UIStyled.viewAINewPrompt language
{ modalClosed = ModalClosed
, promptInput = AIPromptFieldChanged
}

HelpScreen ->
HelpScreen.view language
(GlobalData.isMac globalData)
Expand Down
10 changes: 10 additions & 0 deletions src/elm/Styles.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Styles exposing (colors)

import Css exposing (hsl, rgb, rgba)


colors =
{ green = hsl 86 0.54 0.51
, nearWhite = rgba 255 255 255 0.95
, white = rgb 255 255 255
}

0 comments on commit 28cf366

Please sign in to comment.