generated from b-conf/hestory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompact.cirru
418 lines (417 loc) · 18.6 KB
/
compact.cirru
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
{} (:package |app)
:configs $ {} (:init-fn |app.main/main!) (:reload-fn |app.main/reload!) (:version |0.0.1)
:modules $ [] |respo.calcit/compact.cirru |lilac/compact.cirru |memof/compact.cirru |respo-ui.calcit/compact.cirru |respo-markdown.calcit/compact.cirru |reel.calcit/compact.cirru |respo-feather.calcit/
:entries $ {}
:files $ {}
|app.comp.container $ %{} :FileEntry
:defs $ {}
|at-pattern $ %{} :CodeEntry (:doc |)
:code $ quote
def at-pattern $ new js/RegExp "\"@"
|comp-avatar $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-avatar (label)
[] (effect-render-icon label)
div $ {}
:style $ {} (:width 40) (:height 40)
:border $ str "\"1px solid " (hsl 0 0 90)
:border-radius "\"2px"
|comp-container $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-container (reel)
let
store $ :store reel
states $ :states store
cursor $ either (:cursor states) ([])
state $ either (:data states)
{} (:content "\"") (:voice? false)
div
{} $ :style
merge ui/global ui/fullscreen ui/row $ {} (:background-color :white) (:font-size 16)
div
{} $ :style
merge ui/column $ {} (:width "\"28%")
:background-color $ hsl 0 0 94
memof1-call comp-menu $ :voice? state
div
{} $ :style
merge ui/row-parted $ {} (:padding "\"0 8px") (:user-select :none)
a $ {}
:style $ {} (:font-size 14)
:href "\"https://github.com/fp-china/functor-purity"
:target "\"_blank"
:inner-text "\"源码查看 GitHub."
span $ {} (:inner-text "\"Voice")
:style $ {}
:color $ if (:voice? state) (hsl 240 60 60) (hsl 0 0 80)
:cursor :pointer
:font-family ui/font-fancy
:on-click $ fn (e d!)
d! cursor $ update state :voice? not
div
{} $ :style (merge ui/expand ui/column)
memof1-call comp-header
comp-messages $ :messages store
memof1-call comp-input $ >> states :input
when dev? $ comp-reel (>> states :reel) reel ({})
|comp-header $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-header () $ div
{} $ :style
merge ui/row-parted $ {} (:padding "\"4px 6px") (:font-weight 300) (:font-size 16)
:background-color $ hsl 0 0 97
:border-bottom $ str "\"1px solid " (hsl 0 0 90)
span nil
span
{} $ :on-click
fn (e d!) (js/document.body.requestFullscreen)
<> "\"Functor&Purity" $ {} (:font-family ui/font-fancy)
comp-icon :trash
{} (:font-size 20)
:color $ hsl 320 80 70
:line-height "\"20px"
:vertical-align :middle
:cursor :pointer
fn (e d!) (d! :clear nil)
let
xs $ js/document.querySelectorAll "\"audio"
.!forEach xs $ fn (x i ? n) (.!remove x)
js/window.speechSynthesis.cancel
|comp-input $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-input (states)
let
cursor $ :cursor states
state $ either (:data states)
{} $ :content "\""
div
{} $ :style
merge ui/row-middle $ {} (:padding "\"6px 10px")
:background-color $ hsl 0 0 97
:border-top $ str "\"1px solid " (hsl 0 0 90)
textarea $ {}
:value $ :content state
:placeholder "\"Reply..."
:style $ merge ui/textarea ui/expand
{} (:height 40) (:line-height "\"24px") (:border :none)
:on-input $ fn (e d!)
d! cursor $ assoc state :content (:value e)
:autofocus true
:on-keydown $ fn (e d!)
let
event $ :event e
when
= "\"Enter" $ .-key event
.!preventDefault $ :event e
d! :message $ {} (:author "\"Me")
:text $ .-value
.-target $ :event e
d! cursor $ assoc state :content "\""
scroll-view!
|comp-menu $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-menu (voice?)
div
{} $ :style
merge ui/expand $ {} (:padding "\"16px 0") (:line-height "\"36px")
list-> ({})
-> reading-list $ map
fn (info)
[] (:idx info)
div
{} (:class-name "\"hover-item")
:style $ merge ui/row-middle
{} (:cursor :pointer) (:padding "\"0 8px")
:on-click $ fn (e d!) (js/window.speechSynthesis.cancel)
if voice?
read-content (:messages info) 0 d!
swap-messages (:messages info) d!
comp-icon :link
{} (:font-size 14)
:color $ hsl 230 70 70
:line-height "\"14px"
, nil
=< 2 nil
<> (:idx info)
{}
:color $ hsl 0 0 70
:font-size 12
:font-family ui/font-code
=< 8 nil
<> $ :title info
=< 8 nil
<>
str $ count (:messages info)
{} (:font-size 12)
:background-color $ hsl 200 60 85
:color :white
:padding "\"0px 5px"
:border-radius "\"8px"
:line-height "\"16px"
=< nil 80
|comp-message $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-message (content)
div
{} $ :style
merge ui/row $ {} (:width "\"98%") (:padding "\"4px 10px")
comp-avatar $ :author content
=< 8 nil
div
{} $ :style ui/flex
div
{} $ :style
{}
:color $ hsl 0 0 70
:font-size 12
:line-height "\"18px"
div
{} $ :style ui/row-parted
<> $ :author content
<>
str "\"#" $ or (:floor content) "\"_"
{} (:font-size 10)
:color $ hsl 0 0 80
:font-family ui/font-code
div
{} $ :style
{}
:color $ hsl 0 0 40
:font-size 16
:line-height "\"24px"
comp-md $ :text content
|comp-messages $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-messages (ms)
div
{} $ :style ui/expand
=< nil 8
list->
{} $ :id "\"message-area"
-> ms
or $ []
.map-indexed $ fn (idx m)
[] idx $ comp-message m
if (empty? ms)
div
{} $ :style
merge ui/center $ {} (:padding "\"40px")
<> "\"Cleared." $ {} (:font-family ui/font-fancy) (:font-weight 500)
:color $ hsl 0 0 70
:font-style :italic
=< nil 80
|effect-render-icon $ %{} :CodeEntry (:doc |)
:code $ quote
defeffect effect-render-icon (label) (action el at?)
let
svg-ns "\"http://www.w3.org/2000/svg"
svg $ js/document.createElementNS svg-ns "\"svg"
.!appendChild el svg
jdenticon/update svg label
|read-content $ %{} :CodeEntry (:doc |)
:code $ quote
defn read-content (messages idx d!)
when
not $ empty? messages
let
msg $ first messages
text $ if (:code? msg) "\"Code" (:text msg)
d! :message $ assoc msg :floor idx
; println "\"read" text
case-default (w-log api-target)
speech! (santinize-voice text)
fn () $ read-content (rest messages) (inc idx) d!
"\"xunfei" $ speakXunfei (santinize-voice text)
fn () $ read-content (rest messages) (inc idx) d!
"\"azure" $ synthesizeAzureSpeech (santinize-voice text) (get-env "\"azure-key")
fn $
fn () $ read-content (rest messages) (inc idx) d!
"\"audio" $ requestAudioSpeech (get-env "\"audio-host") (santinize-voice text)
fn () $ read-content (rest messages) (inc idx) d!
scroll-view!
|reading-list $ %{} :CodeEntry (:doc |)
:code $ quote
def reading-list $ [] (slurp-data "\"data/009-ghc-wasm.cirru") (slurp-data "\"data/008-haskell-generics.cirru") (slurp-data "\"data/007-immutable-channel.cirru") (slurp-data "\"data/006-database-perf-usage.cirru") (slurp-data "\"data/005-talk-cat.cirru") (slurp-data "\"data/004-web-frontend.cirru") (slurp-data "\"data/003-tutorial-learning.cirru") (slurp-data "\"data/002-haskell-hiring.cirru") (slurp-data "\"data/001-haskell-tiobe.cirru")
|santinize-voice $ %{} :CodeEntry (:doc |)
:code $ quote
defn santinize-voice (text)
-> text (.!replace at-pattern "\" at ")
.!replace url-pattern $ fn (target & args)
let
url $ new js/URL target
if (some? url)
str "\" link to "
.!replace (.-host url) "\"www." "\""
, "\" "
, "\"link. "
|scroll-view! $ %{} :CodeEntry (:doc |)
:code $ quote
defn scroll-view! () $ js/setTimeout
fn () $ let
target $ js/document.querySelector "\"#message-area"
last-child $ if (some? target) (.-lastElementChild target)
if (some? last-child)
if
some? $ .-scrollIntoViewIfNeeded last-child
.!scrollIntoViewIfNeeded last-child
.!scrollIntoView last-child
js/console.warn "\"no target"
, 100
|slurp-data $ %{} :CodeEntry (:doc |)
:code $ quote
defmacro slurp-data (path) (; println "\"reading path" path)
&data-to-code $ parse-cirru-edn (read-file path)
|speech! $ %{} :CodeEntry (:doc |)
:code $ quote
defn speech! (text cb)
let
t $ new js/window.SpeechSynthesisUtterance text
set! (.-lang t) "\"zh-cn"
set! (.-rate t) 1.2
let
v0 $ js/window.speechSynthesis.getVoices
vs $ .!filter v0
fn (v i a)
.!includes (.-lang v) "\"zh"
if
some? $ aget vs 3
set! (.-voice t) (aget vs 3)
js/console.warn "\"no voice:" v0
js/window.speechSynthesis.speak t
set! (.-onend t)
fn (event) (js/setTimeout cb 400)
; set! (.-onerror t)
fn (event) (js/console.log "\"speech error:" event) (js/setTimeout cb 400)
; set! (.-onboundary t)
fn (event) (js/console.log "\"speech boundary:" event) (js/setTimeout cb 1000)
; set! (.-onpause t)
fn (event) (js/console.log "\"speech pause:" event) (js/setTimeout cb 1000)
|swap-messages $ %{} :CodeEntry (:doc |)
:code $ quote
defn swap-messages (messages d!) (d! :swap-messages messages)
|url-pattern $ %{} :CodeEntry (:doc |)
:code $ quote
def url-pattern $ new js/RegExp "\"https?:[\\w\\d\\/_#\\.\\=\\?\\-]+"
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.comp.container $ :require (respo-ui.core :as ui)
respo-ui.core :refer $ hsl
respo.core :refer $ defcomp defeffect <> >> a div button textarea span input list-> create-element
respo.comp.space :refer $ =<
reel.comp.reel :refer $ comp-reel
respo-md.comp.md :refer $ comp-md
app.config :refer $ dev? api-target
memof.once :refer $ memof1-call
"\"jdenticon" :as jdenticon
"\"../xunfei/sdk" :refer $ speakXunfei
"\"../assets/play-azure.mjs" :refer $ synthesizeAzureSpeech
"\"../assets/play-audio.mjs" :refer $ requestAudioSpeech
feather.core :refer $ comp-icon comp-i
|app.config $ %{} :FileEntry
:defs $ {}
|api-target $ %{} :CodeEntry (:doc |)
:code $ quote
def api-target $ get-env "\"api-target"
|dev? $ %{} :CodeEntry (:doc |)
:code $ quote
def dev? $ = "\"dev" (get-env "\"mode")
|site $ %{} :CodeEntry (:doc |)
:code $ quote
def site $ {} (:storage-key "\"hestory")
:ns $ %{} :CodeEntry (:doc |)
:code $ quote (ns app.config)
|app.main $ %{} :FileEntry
:defs $ {}
|*reel $ %{} :CodeEntry (:doc |)
:code $ quote
defatom *reel $ -> reel-schema/reel (assoc :base schema/store) (assoc :store schema/store)
|dispatch! $ %{} :CodeEntry (:doc |)
:code $ quote
defn dispatch! (op)
when
and config/dev? $ not= (nth op 0) :states
println "\"Dispatch:" op
reset! *reel $ reel-updater updater @*reel op
|main! $ %{} :CodeEntry (:doc |)
:code $ quote
defn main! ()
if (= config/dev? "\"dev") (load-console-formatter!)
println "\"Running mode:" $ if config/dev? "\"dev" "\"release"
js/window.speechSynthesis.getVoices
render-app!
add-watch *reel :changes $ fn (reel prev) (render-app!)
listen-devtools! |k dispatch!
.!addEventListener js/window |beforeunload $ fn (event) (; persist-storage!) (js/window.speechSynthesis.cancel)
; repeat! 60 persist-storage!
; let
raw $ .!getItem js/localStorage (:storage-key config/site)
when (some? raw)
dispatch! :hydrate-storage $ parse-cirru-edn raw
println "|App started."
|mount-target $ %{} :CodeEntry (:doc |)
:code $ quote
def mount-target $ .!querySelector js/document |.app
|persist-storage! $ %{} :CodeEntry (:doc |)
:code $ quote
defn persist-storage! () $ .!setItem js/localStorage (:storage-key config/site)
format-cirru-edn $ :store @*reel
|reload! $ %{} :CodeEntry (:doc |)
:code $ quote
defn reload! () $ if (nil? build-errors)
do (remove-watch *reel :changes) (clear-cache!)
add-watch *reel :changes $ fn (reel prev) (render-app!)
reset! *reel $ refresh-reel @*reel schema/store updater
hud! "\"ok~" "\"Ok"
hud! "\"error" build-errors
|render-app! $ %{} :CodeEntry (:doc |)
:code $ quote
defn render-app! () $ render! mount-target (comp-container @*reel) dispatch!
|repeat! $ %{} :CodeEntry (:doc |)
:code $ quote
defn repeat! (duration cb)
js/setTimeout
fn () (cb)
repeat! (* 1000 duration) cb
* 1000 duration
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.main $ :require
respo.core :refer $ render! clear-cache!
app.comp.container :refer $ comp-container
app.updater :refer $ updater
app.schema :as schema
reel.util :refer $ listen-devtools!
reel.core :refer $ reel-updater refresh-reel
reel.schema :as reel-schema
app.config :as config
"\"./calcit.build-errors" :default build-errors
"\"bottom-tip" :default hud!
|app.schema $ %{} :FileEntry
:defs $ {}
|store $ %{} :CodeEntry (:doc |)
:code $ quote
def store $ {}
:states $ {}
:cursor $ []
:messages $ []
:ns $ %{} :CodeEntry (:doc |)
:code $ quote (ns app.schema)
|app.updater $ %{} :FileEntry
:defs $ {}
|updater $ %{} :CodeEntry (:doc |)
:code $ quote
defn updater (store op op-id op-time)
tag-match op
:states cursor s
update-states store cursor s
(:hydrate-storage data) data
(:message data)
update store :messages $ fn (xs) (conj xs data)
(:clear)
assoc store :messages $ []
(:swap-messages data) (assoc store :messages data)
_ $ do (eprintln "\"unknown op:" op) store
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.updater $ :require
respo.cursor :refer $ update-states