diff options
author | Yorhel <git@yorhel.nl> | 2019-07-25 14:30:04 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2019-07-25 14:36:21 +0200 |
commit | f296495a912ce759df11c43e78b4552788bdbff2 (patch) | |
tree | 0c10802de65fb7c8475722e12234bff5eb980628 /elm3/DocEdit.elm | |
parent | 0f3cfeb85caec6424bcbea47142eefbf8011636b (diff) |
Merge the v3 branch into separate namespace + fix Docker stuff (again)
I was getting tired of having to keep two branches up-to-date with the
latest developments, so decided to throw v3 into the same branch - just
different files (...which will get mostly rewritten again soon). The two
versions aren't very different in terms of dependencies, build system
and support code, so they can now properly share files. Added a section
to the README to avoid confusion.
This merge also makes it easier to quickly switch between the different
versions, which is handy for development. It's even possible to run both
at the same time, but my scripts use the same port so that needs a
workaround.
And it's amazing how often I break the Docker scripts.
Diffstat (limited to 'elm3/DocEdit.elm')
-rw-r--r-- | elm3/DocEdit.elm | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/elm3/DocEdit.elm b/elm3/DocEdit.elm new file mode 100644 index 00000000..09729a3d --- /dev/null +++ b/elm3/DocEdit.elm @@ -0,0 +1,107 @@ +module DocEdit exposing (main) + +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) +import Browser +import Browser.Navigation exposing (load) +import Json.Encode as JE +import Lib.Html exposing (..) +import Lib.Gen exposing (..) +import Lib.Api as Api +import Lib.Ffi as Ffi +import Lib.Editsum as Editsum + + +main : Program DocEdit Model Msg +main = Browser.element + { init = \e -> (init e, Cmd.none) + , view = view + , update = update + , subscriptions = always Sub.none + } + + +type alias Model = + { state : Api.State + , editsum : Editsum.Model + , title : String + , content : String + , id : Int + , preview : String + } + + +init : DocEdit -> Model +init d = + { state = Api.Normal + , editsum = { authmod = True, editsum = d.editsum, locked = d.locked, hidden = d.hidden } + , title = d.title + , content = d.content + , id = d.id + , preview = "" + } + + +encode : Model -> DocEditSend +encode model = + { editsum = model.editsum.editsum + , hidden = model.editsum.hidden + , locked = model.editsum.locked + , title = model.title + , content = model.content + } + + +type Msg + = Editsum Editsum.Msg + | Submit + | Submitted Api.Response + | Title String + | Content String + | Preview + | HandlePreview Api.Response + + +update : Msg -> Model -> (Model, Cmd Msg) +update msg model = + case msg of + Editsum e -> ({ model | editsum = Editsum.update e model.editsum }, Cmd.none) + Title s -> ({ model | title = s }, Cmd.none) + Content s -> ({ model | content = s }, Cmd.none) + + Submit -> + let + path = "/d" ++ String.fromInt model.id ++ "/edit" + body = doceditSendEncode (encode model) + in ({ model | state = Api.Loading }, Api.post path body Submitted) + + Submitted (Api.Changed id rev) -> (model, load <| "/d" ++ String.fromInt id ++ "." ++ String.fromInt rev) + Submitted r -> ({ model | state = Api.Error r }, Cmd.none) + + Preview -> + ( { model | state = Api.Loading, preview = "" } + , Api.post "/js/markdown.json" (JE.object [("content", JE.string model.content)]) HandlePreview + ) + + HandlePreview (Api.Content s) -> ({ model | state = Api.Normal, preview = s }, Cmd.none) + HandlePreview r -> ({ model | state = Api.Error r }, Cmd.none) + + +view : Model -> Html Msg +view model = + form_ Submit (model.state == Api.Loading) + [ card "general" "General" [] <| formGroups + [ [ label [ for "title" ] [ text "Title" ] + , inputText "title" model.title Title [required True, maxlength 200] + ] + , [ label [ for "content" ] [ text "Content" ] + , inputTextArea "content" model.content Content [rows 100, required True] + ] + , [ button [ type_ "button", class "btn", onClick Preview ] [ text "Preview" ] + , div [ class "doc", Ffi.innerHtml model.preview ] [] + ] + ] + , Html.map Editsum (Editsum.view model.editsum) + , submitButton "Submit" model.state True False + ] |