-
Notifications
You must be signed in to change notification settings - Fork 51
/
App.elm
368 lines (284 loc) · 8.78 KB
/
App.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
module App where
import Dreamwriter exposing (..)
import Dreamwriter.Mailboxes exposing (signals, addresses)
import Dreamwriter.Mailboxes as Mailboxes
import Component.Page as Page
import Component.LeftSidebar as LeftSidebar
import Component.RightSidebar as RightSidebar
import Component.Editor as Editor
import String
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.Lazy exposing (..)
import Signal
import Signal exposing (Signal)
import Time exposing (Time, since)
import List exposing (..)
import Maybe
import Window
import Dict
import Set
debounce : Time -> Signal a -> Signal a
debounce wait signal =
Signal.sampleOn (since wait signal |> Signal.dropRepeats) signal
-- UPDATES --
type Update
= NoOp
| LoadAsCurrentDoc Doc
| OpenDocId Identifier
| ListDocs (List Doc)
| OpenNoteId Identifier
| ListNotes (List Note)
| SetCurrentNote (Maybe Note)
| SetChapters (List Chapter)
| UpdateChapter Chapter
| SetTitle (String, Int)
| SetDescription (String, Int)
| SetFullscreen FullscreenState
| PutSnapshot Snapshot
| SetPage Page.Model
-- updates from user input
updates : Signal.Mailbox Update
updates =
Signal.mailbox NoOp
type alias AppState =
{ page : Page.Model
, fullscreen : FullscreenState
, currentDoc : Maybe Doc
, currentDocId : Maybe Identifier
, currentNote : Maybe Note
, docs : List Doc
, notes : List Note
, snapshots : Dict.Dict Identifier Snapshot
}
initialState : AppState
initialState =
{ page = Page.initialModel
, fullscreen = False
, currentDoc = Nothing
, currentDocId = Nothing
, currentNote = Nothing
, docs = []
, notes = []
, snapshots = Dict.empty
}
transition : Update -> AppState -> AppState
transition action state =
case action of
NoOp ->
state
OpenDocId id ->
let
initialPage =
Page.initialModel
page =
{ initialPage
| currentDocId <- Just id
, currentDoc <- state.currentDoc
, docs <- state.docs
, notes <- state.notes
, fullscreen <- state.fullscreen
}
in
{ state
| currentDocId <- Just id
, page <- page
}
LoadAsCurrentDoc doc ->
let
stateAfterOpenDocId =
transition (OpenDocId doc.id) state
newState =
{ stateAfterOpenDocId | currentDoc <- Just doc }
in
updateCurrentDoc (always doc) newState
|> pruneSnapshots
ListDocs docs ->
{ state | docs <- docs }
ListNotes notes ->
{ state | notes <- notes }
SetCurrentNote currentNote ->
{ state | currentNote <- currentNote }
SetChapters chapters ->
let
updateDoc doc =
{ doc | chapters <- chapters }
in
updateCurrentDoc updateDoc state
|> pruneSnapshots
UpdateChapter chapter ->
let
updateDoc doc =
{ doc
| chapters <- List.map (preferById chapter) doc.chapters
}
in
updateCurrentDoc updateDoc state
|> pruneSnapshots
SetTitle (title, words) ->
let
updateDoc doc =
{ doc | title <- title, titleWords <- words }
in
updateCurrentDoc updateDoc state
SetDescription (description, words) ->
let
updateDoc doc =
{ doc
| description <- description
, descriptionWords <- words
}
in
updateCurrentDoc updateDoc state
SetFullscreen enabled ->
{ state | fullscreen <- enabled }
PutSnapshot snapshot ->
{ state
| snapshots <- Dict.insert snapshot.id snapshot state.snapshots
}
SetPage model ->
{ state
| page <- model
, currentDocId <- model.currentDocId
, currentNote <- model.currentNote
}
-- Throw out any snapshots that are no longer relevant, so they can be GC'd.
pruneSnapshots : AppState -> AppState
pruneSnapshots state =
case state.currentDoc of
Nothing ->
state
Just currentDoc ->
let
allSnapshotIds =
currentDoc.chapters
|> List.map .snapshotId
|> Set.fromList
newSnapshots =
state.snapshots
|> Dict.filter (\id _ -> Set.member id allSnapshotIds)
in
{ state | snapshots <- newSnapshots }
updateCurrentDoc : (Doc -> Doc) -> AppState -> AppState
updateCurrentDoc transformation state =
case state.currentDoc of
Nothing ->
state
Just currentDoc ->
let
newCurrentDoc =
transformation currentDoc
newDocs =
List.map (preferById newCurrentDoc) state.docs
in
{ state
| currentDoc <- Just newCurrentDoc
, docs <- newDocs
}
preferById : { a | id : b } -> { a | id : b } -> { a | id : b }
preferById preferred given =
if preferred.id == given.id then
preferred
else
given
main : Signal Html
main =
Signal.map scene state
userInput : Signal Update
userInput =
Signal.mergeMany
[ Signal.map LoadAsCurrentDoc loadAsCurrentDoc
, Signal.map ListDocs listDocs
, Signal.map ListNotes listNotes
, Signal.map SetChapters setChapters
, Signal.map UpdateChapter updateChapter
, Signal.map SetTitle setTitle
, Signal.map SetDescription setDescription
, Signal.map SetFullscreen setFullscreen
, Signal.map PutSnapshot putSnapshot
, Signal.map SetCurrentNote setCurrentNote
, updates.signal
]
generalizePageUpdate : AppState -> Page.Update -> Update
generalizePageUpdate state pageUpdate =
SetPage (Page.transition pageUpdate state.page)
modelPage : AppState -> Page.Model
modelPage state =
{ leftSidebar = state.page.leftSidebar
, rightSidebar = state.page.rightSidebar
, editor = state.page.editor
, fullscreen = state.fullscreen
, currentDocId = state.currentDocId
, currentDoc = state.currentDoc
, currentNote = state.currentNote
, docs = state.docs
, notes = state.notes
}
scene : AppState -> Html
scene state =
let
pageUpdate =
Signal.forwardTo updates.address (generalizePageUpdate state)
addresses =
Mailboxes.addresses
in
Page.view { addresses | update = pageUpdate } (modelPage state)
-- manage the state of our application over time
state : Signal AppState
state =
Signal.foldp transition initialState userInput
-- PORTS --
port loadAsCurrentDoc : Signal Doc
port setChapters : Signal (List Chapter)
port updateChapter : Signal Chapter
port setTitle : Signal (String, Int)
port setDescription : Signal (String, Int)
port setFullscreen : Signal Bool
port setCurrentNote : Signal (Maybe Note)
port listDocs : Signal (List Doc)
port listNotes : Signal (List Note)
port putSnapshot : Signal Snapshot
port setCurrentDocId : Signal (Maybe Identifier)
port setCurrentDocId =
Signal.map .currentDocId state
port newDoc : Signal ()
port newDoc =
signals.newDoc
port openFromFile : Signal ()
port openFromFile =
signals.openFromFile
port downloadDoc : Signal DownloadOptions
port downloadDoc =
signals.download
port printDoc : Signal ()
port printDoc =
signals.print
port navigateToChapterId : Signal Identifier
port navigateToChapterId =
signals.navigateToChapterId
port navigateToTitle : Signal ()
port navigateToTitle =
signals.navigateToTitle
port newNote : Signal ()
port newNote =
signals.newNote
port openNoteId : Signal Identifier
port openNoteId =
signals.openNoteId
port newChapter : Signal ()
port newChapter =
signals.newChapter
port searchNotes : Signal String
port searchNotes =
signals.searchNotes
|> debounce 500
port fullscreen : Signal Bool
port fullscreen =
signals.fullscreen
port execCommand : Signal String
port execCommand =
signals.execCommand
port remoteSync : Signal ()
port remoteSync =
signals.remoteSync