elm-edit/src/Edit/Document.elm

52 lines
1.3 KiB
Elm

module Edit.Document exposing (Document, Segment(..), fromSegments, toSegments)
type Document attr
= Document (List (Segment attr))
fromSegments : List (Segment attr) -> Document attr
fromSegments segments =
Document (optimizeSegments segments [])
toSegments : Document attr -> List (Segment attr)
toSegments (Document segments) =
segments
{-| TODO:
- is `Segment` the best name for this?
- what about block-level elements?
- worry about duplicate attrs
-}
type Segment attr
= Text String (List attr)
optimizeSegments : List (Segment attr) -> List (Segment attr) -> List (Segment attr)
optimizeSegments segments done =
case segments of
[] ->
List.reverse done
[ anything ] ->
List.reverse (anything :: done)
((Text first firstAttrs) as firstText) :: ((Text second secondAttrs) as secondText) :: rest ->
if attrsEqual firstAttrs secondAttrs then
optimizeSegments (Text (first ++ second) firstAttrs :: rest) done
else
optimizeSegments (secondText :: rest) (firstText :: done)
{-| this isn't really completely correct... we need a better way to compare
attrs. Should be a set too.
-}
attrsEqual : List attr -> List attr -> Bool
attrsEqual a b =
a == b