/
Theater.elm
46 lines (37 loc) · 1.07 KB
/
Theater.elm
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
module Theater exposing
(Frame, init, focus, replace, undoAndReplace, hardUndo)
import Ports
import Replacement exposing (Replacement)
import Substring
import Json.Encode as Encode
type alias Frame =
{ text : String
, selectionStart : Int
, selectionEnd : Int
}
init : Frame -> Cmd msg
init frame =
Ports.initTheater
( Encode.object
[ ( "text", Encode.string frame.text )
, ( "selectionStart", Encode.int frame.selectionStart )
, ( "selectionEnd", Encode.int frame.selectionEnd )
]
)
focus : Cmd msg
focus =
Ports.focusTheater ()
replace : Replacement -> Cmd msg
replace = Ports.replace << encodeReplacement
undoAndReplace : Replacement -> Cmd msg
undoAndReplace = Ports.undoAndReplace << encodeReplacement
hardUndo : Cmd msg
hardUndo =
Ports.hardUndo ()
encodeReplacement : Replacement -> Encode.Value
encodeReplacement replacement =
Encode.object
[ ( "selectionStart", Encode.int replacement.old.i )
, ( "selectionEnd", Encode.int (Substring.stop replacement.old) )
, ( "text", Encode.string replacement.new )
]