-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompact.cirru
More file actions
3123 lines (3122 loc) · 146 KB
/
Copy pathcompact.cirru
File metadata and controls
3123 lines (3122 loc) · 146 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{} (:about "|Machine-generated snapshot. AI AGENTS: never edit this file directly — changes will be overwritten on recompile. Inspect via `cr query`; modify via `cr edit` / `cr tree`. MANDATORY first step: run `cr docs agents --full`.") (:package |respo)
:configs $ {} (:init-fn |respo.main/main!) (:reload-fn |respo.main/reload!) (:version |0.16.39)
:modules $ [] |memof/ |calcit-test/
:entries $ {}
:files $ {}
|respo.app.comp.container $ %{} :FileEntry
:defs $ {}
|comp-container $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-container (store)
let
states $ :states store
div
{} (; :class-name highlight-defcomp) (:class-name style-global)
comp-todolist states $ :tasks store
div
{} $ :style style-states
<> $ str "|states: "
to-lispy-string $ :states store
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Component)
:args $ [] :dynamic
|style-global $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle style-global $ {}
|& $ {} (:font-family |Avenir,Verdana)
|& $ {} ('contained "|@media only screen and (max-width: 600px)")
:background-color $ hsl 0 0 90
:examples $ []
|style-states $ %{} :CodeEntry (:doc |) (:schema :map)
:code $ quote
def style-states $ {} (:padding 8)
:examples $ []
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.app.comp.container $ :require
respo.core :refer $ defcomp div span <> >> a
respo.util.format :refer $ hsl
respo.css :refer $ defstyle
respo.app.comp.todolist :refer $ comp-todolist
respo.comp.space :refer $ =<
respo.comp.inspect :refer $ highlight-defcomp
|respo.app.comp.task $ %{} :FileEntry
:defs $ {}
|comp-task $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-task (states task)
let
cursor $ :cursor states
state $ either (:data states) |
[] (effect-log task)
div
{} $ :class-name style-task
comp-inspect |Task task $ {} (:left 200)
button $ {} (:class-name style-done)
:style $ {}
:background-color $ if (:done? task) (hsl 200 20 80) (hsl 200 80 70)
:on-click $ fn (e d!)
d! $ :: :toggle (:id task)
=< 8 0
input $ {}
:value $ :text task
:class-name widget/style-input
:on-input $ fn (e d!)
let
task-id $ :id task
text $ :value e
d! $ %:: Op :update task-id text
=< 8 0
input $ {} (:value state) (:class-name widget/style-input)
:on-input $ fn (e d!)
d! cursor $ :value e
=< 8 0
div
{} (:class-name widget/style-button)
:on-click $ fn (e d!)
d! $ %:: Op :remove (:id task)
<> |Remove
=< 8 0
div ({}) (<> state)
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Component)
:args $ [] :dynamic :dynamic
|effect-log $ %{} :CodeEntry (:doc |)
:code $ quote
defeffect effect-log (task) (action parent at-place?) (; js/console.log "|Task effect" action at-place?)
case-default action nil
:mount $ let
x0 $ js/Math.random
; println |Stored x0
, nil
:update (; println |read) nil
:unmount (; println |read) nil
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Effect)
:args $ [] :dynamic
|style-done $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle style-done $ {}
:& $ {} (:width 32) (:height 32) (:outline :none) (:border :none) (:vertical-align :middle) (:cursor :pointer)
:examples $ []
|style-task $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle style-task $ {}
|& $ {} (:display :flex) (:padding "|4px 0px")
:examples $ []
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.app.comp.task $ :require
respo.core :refer $ defcomp div input span button <> defeffect
respo.util.format :refer $ hsl
respo.comp.space :refer $ =<
respo.comp.inspect :refer $ comp-inspect
respo.app.style.widget :as widget
respo.css :refer $ defstyle
respo.app.schema :refer $ Op
|respo.app.comp.todolist $ %{} :FileEntry
:defs $ {}
|comp-todolist $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-todolist (states tasks)
let
cursor $ either (:cursor states) ([])
state $ either (:data states)
{} (:draft |) (:locked? false) (:message "|Press Ctrl+M to change message")
[] (on-keydown cursor state) (effect-focus |#draft-input)
div
{} (:class-name style-todo-root) (:data-name |todolist)
comp-inspect |States state $ {} (:left |80px)
div
{} $ :style style-panel
input $ {} (:placeholder |Text) (:id |draft-input)
:value $ :draft state
:class-name widget/style-input
:style $ {}
:width $ &max 200
+ 24 $ text-width (:draft state) 16 |BlinkMacSystemFont
:on-input $ fn (e d!)
d! $ %:: Op :states-merge cursor state
{} $ :draft (:value e)
:on-focus on-focus
=< 8 0
span
{} (:class-name widget/style-button)
:on-click $ fn (e d!)
d! $ %:: Op :add (:draft state)
d! cursor $ assoc state :draft |
span $ {} (:on-click nil) (:inner-text |Add)
=< 8 0
span $ {} (:inner-text |Clear) (:class-name widget/style-button)
:on-click $ fn (e d!)
d! $ %:: Op :clear
=< 8 0
div ({})
div
{} (:class-name widget/style-button) (:on-click on-test)
<> "|heavy tasks" style-bold!
list->
{} (:class-name |task-list) (:style style-list)
-> tasks .to-list .reverse $ map
fn (task)
let
task-id $ :id task
[] task-id $ memof1-call-by task-id comp-task (>> states task-id) task
if
> (count tasks) 0
div
{} (:spell-check true) (:class-name style-toolbar)
div
{} (:class-name widget/style-button)
:on-click $ if
not $ :locked? state
fn (e d!)
d! $ %:: Op :clear
<> |Clear2
=< 8 0
div
{} (:class-name widget/style-button)
:on-click $ fn (e d!)
d! cursor $ update state :locked? not
<>
str-spaced |Lock? $ :locked? state
{} $ :font-size 13
=< 8 0
comp-wrap $ comp-zero
comp-inspect |Tasks tasks $ {} (:left 500) (:top 20)
div
{} $ :style
{} (:padding |8px) (:font-size 12) (:color |#999) (:margin-top |16px)
<> $ :message state
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Component)
:args $ [] :dynamic :dynamic
|effect-focus $ %{} :CodeEntry (:doc |)
:code $ quote
defeffect effect-focus (pattern) (action parent at-place?)
when (= action :mount)
if-let
target $ js/document.querySelector pattern
.!select target
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Effect)
:args $ [] :string
|make-keydown-listener $ %{} :CodeEntry (:doc "|DEPRECATED: Factory function approach for creating listeners. This was an experimental approach that did not work due to Record serialization issues. Use on-keydown function instead.")
:code $ quote
defn make-keydown-listener (cursor state)
%{} respo.schema/RespoListener (:name :on-keydown)
:handler $ fn (event dispatch!)
match event $
:keydown info
when
and
= |m $ :key info
:ctrl info
do (js/console.log "|[7] on-keydown with cursor:" cursor |state: state)
dispatch! cursor $ assoc state :message "|Message changed by Ctrl+M!"
js/window.setTimeout
fn () $ dispatch! cursor (assoc state :message "|Press Ctrl+M to change message")
, 2000
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/RespoListener)
:args $ [] :list :map
|number-order $ %{} :CodeEntry (:doc |)
:code $ quote
defn number-order (a b)
if (&< a b) -1 $ if (&> a b) 1 0
:examples $ []
:schema $ :: :fn
{} (:return :number)
:args $ [] :number :number
|on-focus $ %{} :CodeEntry (:doc |)
:code $ quote
defn on-focus (e dispatch!) (println "|Just focused~")
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ [] 'respo.schema/RespoEvent :fn
|on-keydown $ %{} :CodeEntry (:doc "|Creates a keyboard listener for Ctrl+M shortcut. This function demonstrates how to create component-local listeners that can access component state through closures. Returns a RespoListener that updates the message state when Ctrl+M is pressed.")
:code $ quote
defn on-keydown (cursor state)
%{} respo.schema/RespoListener (:name :on-keydown)
:handler $ fn (event dispatch!)
match event $
:keydown info
when
and
= |m $ :key info
:ctrl info
do
dispatch! $ %:: Op :states cursor (assoc state :message "|Message changed by Ctrl+M!")
js/window.setTimeout
fn () $ dispatch!
%:: Op :states cursor $ assoc state :message "|Press Ctrl+M to change message"
, 2000
:examples $ []
quote $ on-keydown cursor state
:schema $ :: :fn
{} (:return 'respo.schema/RespoListener)
:args $ [] :list :map
|on-test $ %{} :CodeEntry (:doc |)
:code $ quote
defn on-test (e dispatch!) (println "|trigger test!")
try-test! dispatch! $ []
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ [] 'respo.schema/RespoEvent :fn
|style-bold! $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle style-bold! $ {}
|& $ {} (:font-weight "|bold !important")
:examples $ []
|style-list $ %{} :CodeEntry (:doc |) (:schema :map)
:code $ quote
def style-list $ {} (:color :black)
:background-color $ hsl 120 20 98
:examples $ []
|style-panel $ %{} :CodeEntry (:doc |) (:schema :map)
:code $ quote
def style-panel $ {} (:display :flex) (:margin-bottom 4)
:examples $ []
|style-todo-root $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle style-todo-root $ {}
|& $ {} (:color :black)
:background-color $ hsl 120 20 98
:line-height |24px
|font-size 16
:padding 10
:font-family "|\"微软雅黑\", Verdana"
:examples $ []
|style-toolbar $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle style-toolbar $ {}
|& $ {} (:display :flex) (:flex-direction :row) (:justify-content :start) (:padding "|4px 0") (:white-space :nowrap)
:examples $ []
|try-test! $ %{} :CodeEntry (:doc |)
:code $ quote
defn try-test! (dispatch! acc)
let
started $ js/Date.now
dispatch! $ %:: Op :clear
loop
x 20
dispatch! $ %:: Op :add |empty
if (> x 0)
recur $ dec x
loop
x 20
dispatch! $ %:: Op :hit-first (js/Math.random)
if (> x 0)
recur $ dec x
dispatch! $ %:: Op :clear
loop
x 10
dispatch! $ %:: Op :add "|only 10 items"
if (> x 0)
recur $ dec x
let
cost $ - (js/Date.now) started
if
< (count acc) 40
js/setTimeout
fn () $ try-test! dispatch! (conj acc cost)
, 0
println |result: $ sort acc number-order
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ [] :fn (:: :list :number)
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.app.comp.todolist $ :require
respo.core :refer $ defcomp div span input <> list-> defeffect >> a
respo.util.format :refer $ hsl
respo.app.comp.task :refer $ comp-task
respo.comp.space :refer $ =<
respo.comp.inspect :refer $ comp-inspect
respo.app.comp.zero :refer $ comp-zero
respo.app.comp.wrap :refer $ comp-wrap
respo.util.dom :refer $ text-width
respo.app.style.widget :as widget
memof.once :refer $ memof1-call-by
respo.css :refer $ defstyle
respo.app.schema :refer $ Op
|respo.app.comp.wrap $ %{} :FileEntry
:defs $ {}
|comp-wrap $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-wrap (x)
div ({}) x
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Component)
:args $ [] :dynamic
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.app.comp.wrap $ :require
respo.core :refer $ defcomp div
|respo.app.comp.zero $ %{} :FileEntry
:defs $ {}
|comp-zero $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-zero () $ div
{} $ :inner-text 0
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Component)
:args $ []
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.app.comp.zero $ :require
respo.core :refer $ defcomp div
|respo.app.core $ %{} :FileEntry
:defs $ {}
|*store $ %{} :CodeEntry (:doc "|Global state storage Atom for the Respo application.\n\nThis is an atom containing all application state data, initialized with the structure defined by schema/store.\nIn Respo applications, all component states are stored in this global store and updated through the dispatch mechanism.") (:schema :ref)
:code $ quote (defatom *store schema/store)
:examples $ []
|dispatch! $ %{} :CodeEntry (:doc |)
:code $ quote
defn dispatch! (op ? op-data)
if dev? $ js/console.log op op-data
let
store $ updater @*store op (generate-id!)
reset! *store store
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ [] 'respo.app.schema/Op (:: :optional :dynamic)
|handle-ssr! $ %{} :CodeEntry (:doc |)
:code $ quote
defn handle-ssr! (mount-target)
realize-ssr! mount-target (comp-container @*store) dispatch!
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ [] :dynamic
|new-fn $ %{} :CodeEntry (:doc |)
:code $ quote
defn new-fn () $ println |hello
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ []
|render-app! $ %{} :CodeEntry (:doc |)
:code $ quote
defn render-app! (mount-target)
render! mount-target (comp-container @*store) dispatch!
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ [] :dynamic
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.app.core $ :require
respo.app.comp.container :refer $ comp-container
respo.core :refer $ render! realize-ssr!
respo.schema :refer $ dev?
respo.app.schema :as schema
respo.app.updater :refer $ updater
|respo.app.schema $ %{} :FileEntry
:defs $ {}
|Op $ %{} :CodeEntry (:doc |) (:schema :dynamic)
:code $ quote
defenum Op (:states :list :dynamic) (:states-kv :list :dynamic :dynamic) (:states-merge :list :map :map) (:add :string) (:remove :string) (:clear) (:update :string :string) (:hit-first :dynamic) (:toggle :string)
:examples $ []
|store $ %{} :CodeEntry (:doc |) (:schema :map)
:code $ quote
def store $ {}
:tasks $ []
:states $ {}
:cursor $ []
:examples $ []
|task $ %{} :CodeEntry (:doc |) (:schema :map)
:code $ quote
def task $ {} (:id nil) (:text |) (:done? false)
:examples $ []
:ns $ %{} :NsEntry (:doc |)
:code $ quote (ns respo.app.schema)
|respo.app.style.widget $ %{} :FileEntry
:defs $ {}
|button $ %{} :CodeEntry (:doc |) (:schema :map)
:code $ quote
def button $ {} (:display :inline-block) (:padding "|0 6px 0 6px") (:font-family |Avenir,Verdana) (:cursor :pointer)
:background-color $ hsl 0 80 70.9
:color $ hsl 0 0 100
:height 28
:line-height |28px
:transition-duration |200ms
:examples $ []
|style-button $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle style-button $ {} (:& button)
|&:hover $ {} (:transform "|scale(1.04)")
:examples $ []
|style-input $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle style-input $ {}
|& $ {} (:font-size |16px) (:line-height |24px) (:padding "|0px 8px") (:outline :none) (:min-width |300px)
:background-color $ hsl 0 0 94
:border :none
:examples $ []
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.app.style.widget $ :require
respo.util.format :refer $ hsl
respo.css :refer $ defstyle
|respo.app.updater $ %{} :FileEntry
:defs $ {}
|updater $ %{} :CodeEntry (:doc |)
:code $ quote
defn updater (store op op-id) (; println store op)
match op
:states cursor s
update-states store cursor s
(:states-kv cursor k v) (update-states-kv store cursor k v)
(:states-merge cursor s o) (update-states-merge store cursor s o)
(:add text)
update store :tasks $ fn (tasks)
conj tasks $ {} (:text text) (:id op-id) (:done? false)
(:remove task-id)
update store :tasks $ fn (tasks)
-> tasks $ filter
fn (task)
not $ = (:id task) task-id
(:clear)
assoc store :tasks $ []
(:update task-id text)
update store :tasks $ fn (tasks)
-> tasks $ map
fn (task)
if
= (:id task) task-id
assoc task :text text
, task
(:hit-first rd)
-> store $ update-in ([] :tasks 0)
fn (task) (assoc task :text rd)
(:toggle task-id)
update store :tasks $ fn (tasks)
-> tasks $ map
fn (task)
if
= (:id task) task-id
update task :done? not
, task
_ $ do (eprintln "|Unknown op:" op) store
:examples $ []
:schema $ :: :fn
{} (:return :map)
:args $ [] :map 'respo.app.schema/Op :string
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.app.updater $ :require
respo.cursor :refer $ update-states update-states-kv update-states-merge
|respo.comp.global-keydown $ %{} :FileEntry
:defs $ {}
|comp-global-keydown $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-global-keydown (options on-event) (; "|dirty solution: proxy window keydown event to a `<span/>`, comes with some restrictions. however Respo does not allow effects to modify states.")
[] (effect-listen-keyboard options |keydown)
span $ {}
:on-keydown $ fn (e d!) (on-event e d!)
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Component)
:args $ [] :dynamic :dynamic
|comp-global-keyup $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-global-keyup (options on-event) (; "|dirty solution: proxy window keydown event to a `<span/>`, comes with some restrictions. however Respo does not allow effects to modify states.")
[] (effect-listen-keyboard options |keyup)
span $ {}
:on-keyup $ fn (e d!) (on-event e d!)
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Component)
:args $ [] :dynamic :dynamic
|dirty-field $ %{} :CodeEntry (:doc "|Constant string key for the global keyboard listener.") (:schema :string)
:code $ quote (def dirty-field |_global_listener)
:examples $ []
|effect-listen-keyboard $ %{} :CodeEntry (:doc "|Effect for listening to global keyboard events on the window object.")
:code $ quote
defeffect effect-listen-keyboard (options event-name) (action el at?)
cond
or (= action :mount) (= action :update)
let
disabled-commands $ noted "|copied event does not support `event.preventDefault()`, so we need to pass a set of configs"
either (:disabled-commands options) (#{} |p |s)
handler $ fn (event)
if
and
.includes? disabled-commands $ .-key event
or (.-ctrlKey event) (.-metaKey event)
.!preventDefault event
.!dispatchEvent el $ new js/KeyboardEvent (.-type event) event
if-let
prev-listener $ aget el dirty-field
js/window.removeEventListener event-name prev-listener
aset el dirty-field handler
js/window.addEventListener event-name handler
(= action :unmount)
let
handler $ aget el dirty-field
js/window.removeEventListener event-name handler
js-delete el dirty-field
true nil
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Effect)
:args $ [] :dynamic :string
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.comp.global-keydown $ :require
respo.core :refer $ defcomp defeffect <> >> div button textarea span input a list->
|respo.comp.inspect $ %{} :FileEntry
:defs $ {}
|comp-inspect $ %{} :CodeEntry (:doc "|put a label in tag with absolute position, click it to print data.\n\n3 parameters are,\n- `tip` a string of comment,\n- `data` Calcit data to inspect, which will be printed in Console,\n- `style` string of className, or hashmap of styles")
:code $ quote
defcomp comp-inspect (tip data style)
let
class-name $ if (string? style) style
style-map $ if (map? style) style
pre $ {}
:class-name $ str-spaced style-data class-name
:inner-text $ str tip "|: " (grab-info data)
:style style-map
:on-click $ fn (e d!)
if (some? js/window.devtoolsFormatters) (js/console.log data)
js/console.log $ to-js-data data
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Component)
:args $ [] :dynamic :dynamic :dynamic
|grab-info $ %{} :CodeEntry (:doc |)
:code $ quote
defn grab-info (data)
cond
map? data
str |Map/ $ count data
(list? data)
str |List/ $ count data
(set? data)
str |Set/ $ count data
(nil? data) |nil
(number? data) (str data)
(tag? data) (str data)
(bool? data) (str data)
(fn? data) |Fn
true $ to-lispy-string data
:examples $ []
:schema $ :: :fn
{} (:return :string)
:args $ [] :dynamic
|highlight-defcomp $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle highlight-defcomp $ {}
"|& *" $ {}
:outline $ str "|1px dashed " (hsl 200 40 50 0.5)
:examples $ []
|style-data $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle style-data $ {}
|& $ {} (:position :absolute) (:background-color "|hsl(240,100%,0%)") (:color :white) (:opacity 0.2) (:font-size |12px) (:font-family |Avenir,Verdana) (:line-height |1.4em) (:padding "|2px 6px") (:border-radius |4px) (:max-width 160) (:max-height 32) (:white-space :normal) (:text-overflow :ellipsis) (:cursor :default)
:examples $ []
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.comp.inspect $ :require
respo.core :refer $ defcomp pre <>
respo.css :refer $ defstyle
respo.util.format :refer $ hsl
|respo.comp.space $ %{} :FileEntry
:defs $ {}
|=< $ %{} :CodeEntry (:doc "|insert a tiny space, horizontally or verticaly.\n\n- `8 nil` for horizontal width 8px,\n- `nil 8` for vertical height 8px.\n")
:code $ quote
defn =< (w x) (comp-space w x)
:examples $ []
quote $ =< 8 nil
quote $ =< nil 16
quote $ =< 12 nil
:schema $ :: :fn
{} (:return 'respo.schema/Component)
:args $ [] (:: :optional :dynamic) (:: :optional :dynamic)
|comp-space $ %{} :CodeEntry (:doc "|A simple spacer component. Creates a div with specified width or height.")
:code $ quote
defcomp comp-space (w h)
div $ {} (:class-name style-space)
:style $ if (some? w) (&{} :width w) (&{} :height h)
:examples $ []
quote $ comp-space 10 nil
quote $ comp-space nil 16px
:schema $ :: :fn
{} (:return 'respo.schema/Component)
:args $ [] :dynamic :dynamic
|style-space $ %{} :CodeEntry (:doc |) (:schema :string)
:code $ quote
defstyle style-space $ {}
:& $ {} (:height 1) (:width 1) (:display :inline-block)
:examples $ []
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.comp.space $ :require
respo.core :refer $ div defcomp
respo.css :refer $ defstyle
|respo.controller.client $ %{} :FileEntry
:defs $ {}
|activate-instance! $ %{} :CodeEntry (:doc "|Initializes the application by mounting the root element to the DOM.")
:code $ quote
defn activate-instance! (entire-dom mount-point deliver-event)
let
listener-builder $ fn (event-name) (build-listener event-name deliver-event)
set! (.-innerHTML mount-point) |
.!appendChild mount-point $ make-element entire-dom listener-builder ([])
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ [] 'respo.schema/Component :dynamic :fn
|build-listener $ %{} :CodeEntry (:doc "|Creates a DOM event listener that converts events and dispatches them to Respo.")
:code $ quote
defn build-listener (event-name deliver-event)
fn (event coord)
let
simple-event $ event->edn event
deliver-event coord event-name simple-event
:examples $ []
:schema $ :: :fn
{} (:return :fn)
:args $ [] :tag :fn
|patch-instance! $ %{} :CodeEntry (:doc "|Applies collected changes to the DOM and updates event listeners.")
:code $ quote
defn patch-instance! (changes mount-point deliver-event)
let
listener-builder $ fn (event-name) (build-listener event-name deliver-event)
apply-dom-changes changes mount-point listener-builder
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ [] :list :dynamic :fn
|send-to-component! $ %{} :CodeEntry (:doc |)
:code $ quote
defn send-to-component! (event-tuple)
let
dispatch! $ wrap-dispatch *dispatch-fn
tree @*global-element
traverse-and-call tree event-tuple dispatch!
:examples $ []
:schema $ :: :fn
{} (:return :tag)
:args $ [] :tuple
|traverse-and-call $ %{} :CodeEntry (:doc |)
:code $ quote
defn traverse-and-call (element event-tuple dispatch!)
when (some? element)
when (component? element)
let
listeners $ &record:get element :listeners
tree $ &record:get element :tree
each listeners $ fn (listener)
let
handler $ &record:get listener :handler
handler event-tuple dispatch!
traverse-and-call tree event-tuple dispatch!
when (element? element)
each (&record:get element :children)
fn (pair)
let
child $ get pair 1
traverse-and-call child event-tuple dispatch!
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ [] :dynamic :tuple
:: :fn $ {} (:return :unit)
:args $ [] :tuple
|wrap-dispatch $ %{} :CodeEntry (:doc "|Wraps a raw dispatch function to automatically handle different operation types (list, tag, or direct).")
:code $ quote
defn wrap-dispatch (*dispatch-fn)
fn (op ? data)
let
dispatch! $ deref *dispatch-fn
if (list? op)
dispatch! $ : states op data
if (tag? op)
dispatch! $ :: op data
dispatch! op
:examples $ []
:schema $ :: :fn
{} (:return :fn)
:args $ [] :ref
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.controller.client $ :require
respo.render.patch :refer $ apply-dom-changes
respo.util.format :refer $ event->edn
respo.render.dom :refer $ make-element
respo.core :refer $ *dispatch-fn *global-element
respo.util.detect :refer $ component? element?
respo.controller.resolve :refer $ extract-listeners
|respo.controller.resolve $ %{} :FileEntry
:defs $ {}
|build-deliver-event $ %{} :CodeEntry (:doc "|Creates a function to dispatch events from the DOM to Respo's event handling system.")
:code $ quote
defn build-deliver-event (*global-element *dispatch-fn)
fn (coord event-name simple-event) (; echo "|event coord" coord)
let
target-element $ find-event-target @*global-element coord event-name
target-listener $ if (some? target-element)
get (:event target-element) event-name
do (js/console.warn "|found no element" coord event-name) nil
dispatch-wrap $ wrap-dispatch *dispatch-fn
if (some? target-listener)
do (; println "|listener found:" coord event-name) (target-listener simple-event dispatch-wrap)
; println "|found no listener:" coord event-name
:examples $ []
:schema $ :: :fn
{} (:return :fn)
:args $ [] :ref :ref
|extract-listeners $ %{} :CodeEntry (:doc |)
:code $ quote
defn extract-listeners (component-result)
if (list? component-result)
let
listeners $ filter component-result listener?
elements $ filter component-result
fn (x)
not $ listener? x
{} (:listeners listeners)
:element $ first elements
{}
:listeners $ []
:element component-result
:examples $ []
:schema $ :: :fn
{} (:return :map)
:args $ [] :dynamic
|find-event-target $ %{} :CodeEntry (:doc "|Traverses the virtual DOM to find the element that should handle a specific event.")
:code $ quote
defn find-event-target (element coord event-name) (; echo "|looking for" coord event-name)
assert "|element cannot be nil" $ some? element
assert "|coord cannot be nil" $ some? coord
let
target-element $ loop
m $ get-markup-at element coord
if (component? m)
recur $ :tree m
, m
element-exists? $ some? target-element
; println "|target element:" $ to-lispy-string event-name
if
and element-exists? $ some?
get (:event target-element) event-name
, target-element $ if (empty? coord) nil
if element-exists?
recur element
slice coord 0 $ - (count coord) 1
, event-name
, nil
:examples $ []
:schema $ :: :fn
{} (:return :dynamic)
:args $ [] :dynamic :list :tag
|get-markup-at $ %{} :CodeEntry (:doc "|Retrieves the virtual DOM element at the specified coordinate.")
:code $ quote
defn get-markup-at (markup coord)
; println |markup: $ to-lispy-string coord
list-match coord
() markup
(coord-head cs)
if (component? markup)
recur (:tree markup) cs
let
child-pair $ find (:children markup)
fn (child-entry)
= (get child-entry 0) coord-head
if (some? child-pair)
get-markup-at (get child-pair 1) cs
raise $ str "|child not found:" coord
map (:children markup) first
:examples $ []
:schema $ :: :fn
{} (:return :dynamic)
:args $ [] :dynamic :list
:ns $ %{} :NsEntry (:doc |)
:code $ quote
ns respo.controller.resolve $ :require
respo.util.detect :refer $ component? element? listener?
respo.controller.client :refer $ wrap-dispatch
|respo.core $ %{} :FileEntry
:defs $ {}
|*changes-logger $ %{} :CodeEntry (:doc "|Atom to hold a logging function for observing changes during rerenders. Function signature: (old-tree new-tree changes).") (:schema :ref)
:code $ quote (defatom *changes-logger nil)
:examples $ []
quote $ reset! *changes-logger
fn (old new changes) (println changes)
|*dispatch-fn $ %{} :CodeEntry (:doc "|internal atom storing the dispatch function. used to handle events and state updates throughout the application.") (:schema :ref)
:code $ quote (defatom *dispatch-fn nil)
:examples $ []
|*global-element $ %{} :CodeEntry (:doc "|internal atom storing the current virtual DOM tree. used by render! to track and update the application state.") (:schema :ref)
:code $ quote (defatom *global-element nil)
:examples $ []
|<> $ %{} :CodeEntry (:doc "|create a text node using span element. first argument is the text content. optional second argument is style (hashmap) or class-name (string).")
:code $ quote
defn <> (content ? style)
if (string? style)
span $ {} (:inner-text content) (:class-name style)
span $ {} (:inner-text content) (:style style)
:examples $ []
:schema $ :: :fn
{} (:return 'respo.schema/Element)
:args $ [] :string (:: :optional :dynamic)
|>> $ %{} :CodeEntry (:doc "|Navigates to a sub-state cursor. Used for managing nested component states.")
:code $ quote
defn >> (states k)
let
parent-cursor $ either (:cursor states) ([])
branch $ either (get states k) ({})
assoc branch :cursor $ conj parent-cursor k
:examples $ []
quote $ >> states :task-a
:schema $ :: :fn
{} (:return :map)
:args $ [] :map :tag
|a $ %{} :CodeEntry (:doc "|Creates HTML link element (anchor tag).\n\nParameters:\n props - Attribute map, can include standard HTML attributes like href, target, class-name, etc.\n & children - Variable arguments for child elements, typically link display text or other elements\n\nReturns:\n Created link element component\n\nUsed to create hyperlinks, supports all standard HTML link attributes.")
:code $ quote
defn a (props & children) (create-element :a props & children)
:examples $ []
quote $ a
{} (:href |https://example.com) (:inner-text "|Visit Example")
:schema $ :: :fn
{} (:rest :dynamic) (:return 'respo.schema/Element)
:args $ [] (:: :optional 'respo.schema/DomProps)
|blockquote $ %{} :CodeEntry (:doc |)
:code $ quote
defn blockquote (props & children) (create-element :blockquote props & children)
:examples $ []
:schema $ :: :fn
{} (:rest :dynamic) (:return 'respo.schema/Element)
:args $ [] (:: :optional 'respo.schema/DomProps)
|body $ %{} :CodeEntry (:doc "|create a body element with properties and children. first argument is a hashmap for properties, rest arguments are children elements.")
:code $ quote
defn body (props & children) (create-element :body props & children)
:examples $ []
quote $ body ({})
div ({}) (<> |Content)
quote $ body
{} $ :style
{} $ :margin |0
:schema $ :: :fn
{} (:rest :dynamic) (:return 'respo.schema/Element)
:args $ [] (:: :optional 'respo.schema/DomProps)
|button $ %{} :CodeEntry (:doc "|Renders a <button> element. Wrapper around create-element.")
:code $ quote
defn button (props & children)
create-element :button props & $ map children confirm-child
:examples $ []
quote $ button
{} $ :on-click
fn (e d!)
d! $ :: :click
<> "|Click me"
:schema $ :: :fn
{} (:rest :dynamic) (:return 'respo.schema/Element)
:args $ [] (:: :optional 'respo.schema/DomProps)
|clear-cache! $ %{} :CodeEntry (:doc |)
:code $ quote
defn clear-cache! () $ reset-memof1-caches!
:examples $ []
:schema $ :: :fn
{} (:return :unit)
:args $ []
|code $ %{} :CodeEntry (:doc |)
:code $ quote
defn code (props & children) (create-element :code props & children)
:examples $ []
:schema $ :: :fn
{} (:rest :dynamic) (:return 'respo.schema/Element)
:args $ [] (:: :optional 'respo.schema/DomProps)
|confirm-child $ %{} :CodeEntry (:doc "|Validates if the item is a valid Respo node (element, component, or nil). Returns the item.")
:code $ quote
defn confirm-child (x)
assert "|Invalid data in elements tree: " $ or (nil? x) (element? x) (component? x)
, x
:examples $ []
:schema $ :: :fn
{}
:args $ [] :dynamic
:return $ :: :optional 'respo.schema/Element
|confirm-child-pair $ %{} :CodeEntry (:doc |)
:code $ quote
defn confirm-child-pair (pair)
assert "|expected pair" $ and (list? pair)
&= 2 $ count pair
&let
x $ nth pair 1
assert "|Invalid data in elements tree: " $ or (nil? x) (element? x) (component? x)
, pair
:examples $ []
:schema $ :: :fn
{}
:args $ [] :dynamic
:return $ :: :optional 'respo.schema/Element
|create-element $ %{} :CodeEntry (:doc "|create a virtual DOM element with tag name, properties and children. used internally by element macros like div, span, etc.")
:code $ quote
defn create-element (tag-name props & children)
; assert
str "|For rendering lists, please use list-> , got: " $ to-lispy-string children
and
&> (count children) 0
not $ any? list? children
let
props-map $ if (record? props) (&record:to-map props) props
attrs $ pick-attrs props-map
styles $ ->
either (get props-map :style) ({})
&map:to-list
sort $ fn (x y)
&compare (nth x 0) (nth y 0)
event $ pick-event props-map
children-nodes $ -> children
map-indexed $ fn (idx item) (confirm-child item) ([] idx item)
filter val-exists?
%{} schema/Element (:name tag-name) (:coord nil) (:attrs attrs) (:style styles) (:event event) (:children children-nodes)
:examples $ []
quote $ create-element :div ({})
quote $ create-element :span
{} $ :class-name |text
quote $ create-element :a
{} $ :href |/home
<> |Home
:schema $ :: :fn
{} (:rest :dynamic) (:return 'respo.schema/Element)
:args $ [] :tag (:: :optional 'respo.schema/DomProps)
|create-list-element $ %{} :CodeEntry (:doc "|Creates a virtual DOM element for list rendering. Arguments: tag-name, props, child-map (map of key -> child).")
:code $ quote