-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clip.elm
149 lines (113 loc) · 2.98 KB
/
Clip.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
module Clip exposing (Clip, Word, font, initial, lineWidth, maxWidth, minSpace, update)
import String
import Video exposing (Video)
font : String
font =
"106px Mod"
maxWidth : Int
maxWidth =
640
maxLines : Int
maxLines =
8
minSpace : Int
minSpace =
30
showProgress : Bool
showProgress =
False
type alias Word =
{ text : String
, width : Int
}
type alias Clip =
{ video : String
, title : String
, cover : String
, caption : String
, word : Word
, line : List Word
, lines : List (List Word)
}
initial : Video -> ( Clip, Maybe String )
initial { createdTime, title } =
start
{ video = "/videos/" ++ String.fromInt createdTime ++ ".mp4"
, cover = "/videos/" ++ String.fromInt createdTime ++ ".jpg"
, caption = title ++ " "
, title = title
, word = Word "" 0
, line = []
, lines = []
}
lineWidth : List Word -> Int
lineWidth line =
let
len =
List.length line - 1
in
line
|> List.map .width
|> List.sum
|> (+)
(if len > 0 then
len * minSpace
else
0
)
start : Clip -> ( Clip, Maybe String )
start clip =
case String.uncons clip.caption of
Just ( char, rest ) ->
( { clip
| caption = rest ++ String.fromChar char
}
, Just (String.fromChar char)
)
Nothing ->
( clip, Nothing )
addWord : Word -> List Word -> List Word
addWord word line =
if word.text == "" then
line
else
line ++ [ word ]
update : Word -> Clip -> ( Clip, Maybe String )
update ({ text, width } as newWord) clip =
if List.length clip.lines == maxLines then
( clip, Nothing )
else if lineWidth (addWord newWord clip.line) >= maxWidth then
( { clip
| lines = clip.lines ++ [ addWord clip.word clip.line ]
, line =
[]
-- start new line
, word =
Word "" 0
-- start new word
}
, Just (String.dropLeft (String.length text - 1) text)
)
else
case String.uncons clip.caption of
Just ( ' ', rest ) ->
-- got a space, end of word
update (Word "" 0)
{ clip
| line =
addWord newWord clip.line
-- add new word
, caption = rest ++ " "
, word =
Word "" 0
-- start new word
}
Just ( char, rest ) ->
( { clip
| word = newWord
, caption = rest ++ String.fromChar char
}
, Just (text ++ String.fromChar char)
)
Nothing ->
( clip, Nothing )