Browse Source

make Document opaque

main
Brian Hicks 2 years ago
parent
commit
7f7a9d55d6
  1. 9
      src/Edit/Delta.elm
  2. 45
      src/Edit/Document.elm
  3. 19
      src/Main.elm

9
src/Edit/Delta.elm

@ -1,6 +1,6 @@
module Edit.Delta exposing (Delta(..), Edit(..), apply)
import Edit.Document exposing (Document(..), Segment(..))
import Edit.Document as Document exposing (Document(..), Segment(..))
type Delta attrs
@ -18,8 +18,11 @@ type Edit attrs
{-| TODO: not tail-recursive.
-}
apply : Delta attrs -> Document attrs -> Document attrs
apply (Delta edits) (Document segments) =
Document (applyHelp edits segments)
apply (Delta edits) document =
document
|> Document.toSegments
|> applyHelp edits
|> Document.fromSegments
applyHelp : List (Edit attrs) -> List (Segment attrs) -> List (Segment attrs)

45
src/Edit/Document.elm

@ -1,8 +1,18 @@
module Edit.Document exposing (Document(..), Segment(..))
module Edit.Document exposing (Document, Segment(..), fromSegments, toSegments)
type Document attrs
= Document (List (Segment attrs))
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:
@ -12,5 +22,30 @@ type Document attrs
- worry about duplicate attrs
-}
type Segment attrs
= Text String (List 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

19
src/Main.elm

@ -34,12 +34,12 @@ type Msg
init : Flags -> ( Model, Cmd Msg )
init _ =
( { editor =
Document
[ Document.Text "Hey" [ Bold ]
, Document.Text " " []
, Document.Text "there" [ Italic ]
, Document.Text "!" []
]
[ Document.Text "Hey" [ Bold ]
, Document.Text " " []
, Document.Text "there" [ Italic ]
, Document.Text "!" []
]
|> Document.fromSegments
|> Edit.init
}
, Cmd.none
@ -57,12 +57,9 @@ update msg model =
view : Model -> Html Msg
view model =
let
(Document segments) =
model.editor.document
in
Html.main_ []
[ segments
[ model.editor.document
|> Document.toSegments
|> List.map viewSegment
|> Html.p []
|> Edit.view

Loading…
Cancel
Save