2017-04-22 04:16:14 -04:00
|
|
|
module ViewHelper
|
|
|
|
exposing
|
|
|
|
( formatContent
|
|
|
|
, getMentionForLink
|
|
|
|
, onClickWithPreventAndStop
|
|
|
|
, toVirtualDom
|
|
|
|
)
|
|
|
|
|
|
|
|
import Html exposing (..)
|
|
|
|
import Html.Attributes exposing (..)
|
|
|
|
import Html.Events exposing (onWithOptions)
|
|
|
|
import HtmlParser
|
|
|
|
import Json.Decode as Decode
|
2017-04-27 10:34:27 -04:00
|
|
|
import String.Extra exposing (replace)
|
|
|
|
import Mastodon.Model
|
2017-04-27 12:39:14 -04:00
|
|
|
import Model exposing (Msg(LoadUserAccount))
|
2017-04-22 04:16:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
-- Custom Events
|
|
|
|
|
|
|
|
|
|
|
|
onClickWithPreventAndStop : msg -> Attribute msg
|
|
|
|
onClickWithPreventAndStop msg =
|
|
|
|
onWithOptions
|
|
|
|
"click"
|
|
|
|
{ preventDefault = True, stopPropagation = True }
|
|
|
|
(Decode.succeed msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Views
|
|
|
|
|
|
|
|
|
2017-04-27 10:34:27 -04:00
|
|
|
formatContent : String -> List Mastodon.Model.Mention -> List (Html Msg)
|
2017-04-22 04:16:14 -04:00
|
|
|
formatContent content mentions =
|
|
|
|
content
|
2017-04-27 10:34:27 -04:00
|
|
|
|> replace " ?" " ?"
|
|
|
|
|> replace " !" " !"
|
|
|
|
|> replace " :" " :"
|
2017-04-22 04:16:14 -04:00
|
|
|
|> HtmlParser.parse
|
|
|
|
|> toVirtualDom mentions
|
|
|
|
|
|
|
|
|
|
|
|
{-| Converts nodes to virtual dom nodes.
|
|
|
|
-}
|
2017-04-27 10:34:27 -04:00
|
|
|
toVirtualDom : List Mastodon.Model.Mention -> List HtmlParser.Node -> List (Html Msg)
|
2017-04-22 04:16:14 -04:00
|
|
|
toVirtualDom mentions nodes =
|
|
|
|
List.map (toVirtualDomEach mentions) nodes
|
|
|
|
|
|
|
|
|
2017-04-27 10:34:27 -04:00
|
|
|
createLinkNode : List ( String, String ) -> List HtmlParser.Node -> List Mastodon.Model.Mention -> Html Msg
|
2017-04-22 04:16:14 -04:00
|
|
|
createLinkNode attrs children mentions =
|
|
|
|
let
|
|
|
|
maybeMention =
|
|
|
|
getMentionForLink attrs mentions
|
|
|
|
in
|
|
|
|
case maybeMention of
|
|
|
|
Just mention ->
|
|
|
|
Html.node "a"
|
|
|
|
((List.map toAttribute attrs)
|
2017-04-27 12:39:14 -04:00
|
|
|
++ [ onClickWithPreventAndStop (LoadUserAccount mention.id) ]
|
2017-04-22 04:16:14 -04:00
|
|
|
)
|
|
|
|
(toVirtualDom mentions children)
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Html.node "a" (List.map toAttribute attrs) (toVirtualDom mentions children)
|
|
|
|
|
|
|
|
|
|
|
|
getHrefLink : List ( String, String ) -> Maybe String
|
|
|
|
getHrefLink attrs =
|
|
|
|
attrs
|
|
|
|
|> List.filter
|
|
|
|
(\( name, value ) -> (name == "href"))
|
|
|
|
|> List.map
|
|
|
|
(\( name, value ) -> value)
|
|
|
|
|> List.head
|
|
|
|
|
|
|
|
|
2017-04-27 10:34:27 -04:00
|
|
|
getMentionForLink : List ( String, String ) -> List Mastodon.Model.Mention -> Maybe Mastodon.Model.Mention
|
2017-04-22 04:16:14 -04:00
|
|
|
getMentionForLink attrs mentions =
|
|
|
|
case getHrefLink attrs of
|
|
|
|
Just href ->
|
|
|
|
mentions
|
|
|
|
|> List.filter (\m -> m.url == href)
|
|
|
|
|> List.head
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
Nothing
|
|
|
|
|
|
|
|
|
2017-04-27 10:34:27 -04:00
|
|
|
toVirtualDomEach : List Mastodon.Model.Mention -> HtmlParser.Node -> Html Msg
|
2017-04-22 04:16:14 -04:00
|
|
|
toVirtualDomEach mentions node =
|
|
|
|
case node of
|
|
|
|
HtmlParser.Element "a" attrs children ->
|
|
|
|
createLinkNode attrs children mentions
|
|
|
|
|
|
|
|
HtmlParser.Element name attrs children ->
|
|
|
|
Html.node name (List.map toAttribute attrs) (toVirtualDom mentions children)
|
|
|
|
|
|
|
|
HtmlParser.Text s ->
|
|
|
|
text s
|
|
|
|
|
|
|
|
HtmlParser.Comment _ ->
|
|
|
|
text ""
|
|
|
|
|
|
|
|
|
|
|
|
toAttribute : ( String, String ) -> Attribute msg
|
|
|
|
toAttribute ( name, value ) =
|
|
|
|
attribute name value
|