summaryrefslogtreecommitdiff
path: root/elm/DocEdit.elm
blob: b9d706220dcad26a246e7787bd1381c1a54f44d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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.TextPreview as TP
import Lib.Api as Api
import Lib.Ffi as Ffi
import Lib.Editsum as Editsum
import Gen.Api as GApi
import Gen.DocEdit as GD


main : Program GD.Recv 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     : TP.Model
  , id          : Int
  }


init : GD.Recv -> Model
init d =
  { state       = Api.Normal
  , editsum     = { authmod = True, editsum = TP.bbcode d.editsum, locked = d.locked, hidden = d.hidden }
  , title       = d.title
  , content     = TP.markdown d.content
  , id          = d.id
  }


encode : Model -> GD.Send
encode model =
  { editsum     = model.editsum.editsum.data
  , hidden      = model.editsum.hidden
  , locked      = model.editsum.locked
  , title       = model.title
  , content     = model.content.data
  }


type Msg
  = Editsum Editsum.Msg
  | Submit
  | Submitted GApi.Response
  | Title String
  | Content TP.Msg


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Editsum m  -> let (nm,nc) = Editsum.update m model.editsum in ({ model | editsum = nm }, Cmd.map Editsum nc)
    Title s   -> ({ model | title   = s }, Cmd.none)
    Content m -> let (nm,nc) = TP.update m model.content in ({ model | content = nm }, Cmd.map Content nc)

    Submit ->
      let
        path = "/d" ++ String.fromInt model.id ++ "/edit"
        body = GD.encode (encode model)
      in ({ model | state = Api.Loading }, Api.post path body Submitted)

    Submitted (GApi.Changed id rev) -> (model, load <| "/d" ++ String.fromInt id ++ "." ++ String.fromInt rev)
    Submitted r -> ({ model | state = Api.Error r }, Cmd.none)


view : Model -> Html Msg
view model =
  form_ Submit (model.state == Api.Loading)
    [ div [ class "mainbox" ]
      [ h1 [] [ text <| "Edit d" ++ String.fromInt model.id ]
      , table [ class "formtable" ]
        [ formField "title::Title" [ inputText "title" model.title Title (style "width" "300px" :: GD.valTitle) ]
        , formField "none"
          [ br_ 1
          , b [] [ text "Contents" ]
          , br_ 1
          , text "HTML and MultiMarkdown supported, which is "
          , a [ href "https://daringfireball.net/projects/markdown/basics", target "_blank" ] [ text "Markdown" ]
          , text " with some "
          , a [ href "http://fletcher.github.io/MultiMarkdown-5/syntax.html", target "_blank" ][ text "extensions" ]
          , text "."
          , TP.view "content" model.content Content 850 ([rows 50, cols 90] ++ GD.valContent)
          ]
        ]
      ]
    , div [ class "mainbox" ]
      [ fieldset [ class "submit" ]
        [ Html.map Editsum (Editsum.view model.editsum)
        , submitButton "Submit" model.state True
        ]
      ]
    ]