-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
state.cljs
1713 lines (1418 loc) · 47.5 KB
/
state.cljs
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
(ns frontend.state
(:require [cljs-bean.core :as bean]
[cljs-time.core :as t]
[cljs-time.format :as tf]
[cljs.core.async :as async]
[clojure.string :as string]
[cljs.spec.alpha :as s]
[dommy.core :as dom]
[medley.core :as medley]
[electron.ipc :as ipc]
[frontend.storage :as storage]
[frontend.util :as util]
[frontend.util.cursor :as cursor]
[goog.dom :as gdom]
[goog.object :as gobj]
[lambdaisland.glogi :as log]
[promesa.core :as p]
[rum.core :as rum]
[frontend.mobile.util :as mobile-util]))
(defonce ^:large-vars/data-var state
(let [document-mode? (or (storage/get :document/mode?) false)
current-graph (let [graph (storage/get :git/current-repo)]
(when graph (ipc/ipc "setCurrentGraph" graph))
graph)]
(atom
{:route-match nil
:today nil
:system/events (async/chan 100)
:db/batch-txs (async/chan 100)
:file/writes (async/chan 100)
:reactive/custom-queries (async/chan 100)
:notification/show? false
:notification/content nil
:repo/cloning? false
;; :repo/loading-files? is only for github repos
:repo/loading-files? {}
:repo/changed-files nil
:nfs/user-granted? {}
:nfs/refreshing? nil
:instrument/disabled? (storage/get "instrument-disabled")
;; TODO: how to detect the network reliably?
:network/online? true
:indexeddb/support? true
:me nil
:git/current-repo current-graph
:git/status {}
:format/loading {}
:draw? false
:db/restoring? nil
:journals-length 3
:search/q ""
:search/mode :global
:search/result nil
:search/graph-filters []
;; modals
:modal/id nil
:modal/label ""
:modal/show? false
:modal/panel-content nil
:modal/fullscreen? false
:modal/close-btn? nil
:modal/subsets []
;; right sidebar
:ui/fullscreen? false
:ui/settings-open? false
:ui/sidebar-open? false
:ui/left-sidebar-open? (boolean (storage/get "ls-left-sidebar-open?"))
:ui/theme (or (storage/get :ui/theme) (if (mobile-util/is-native-platform?) "light" "dark"))
:ui/system-theme? ((fnil identity (or util/mac? util/win32? false)) (storage/get :ui/system-theme?))
:ui/wide-mode? (storage/get :ui/wide-mode)
;; ui/collapsed-blocks is to separate the collapse/expand state from db for:
;; 1. right sidebar
;; 2. zoom-in view
;; 3. queries
;; 4. references
;; graph => {:block-id bool}
:ui/collapsed-blocks {}
:ui/sidebar-collapsed-blocks {}
:ui/root-component nil
:ui/file-component nil
:ui/custom-query-components {}
:ui/show-recent? false
:ui/command-palette-open? false
:ui/developer-mode? (or (= (storage/get "developer-mode") "true")
false)
;; remember scroll positions of visited paths
:ui/paths-scroll-positions {}
:ui/shortcut-tooltip? (if (false? (storage/get :ui/shortcut-tooltip?))
false
true)
:ui/visual-viewport-pending? false
:ui/visual-viewport-state nil
:document/mode? document-mode?
:github/contents {}
:config {}
:block/component-editing-mode? false
:editor/draw-mode? false
:editor/show-page-search? false
:editor/show-page-search-hashtag? false
:editor/show-date-picker? false
;; With label or other data
:editor/show-input nil
:editor/show-zotero false
:editor/last-saved-cursor nil
:editor/editing? nil
:editor/in-composition? false
:editor/content {}
:editor/block nil
:editor/block-dom-id nil
:editor/set-timestamp-block nil
:editor/last-input-time nil
:editor/document-mode? document-mode?
:editor/args nil
:editor/on-paste? false
:editor/last-key-code nil
:editor/editing-page-title? false
;; for audio record
:editor/record-status "NONE"
:db/last-transact-time {}
;; whether database is persisted
:db/persisted? {}
:cursor-range nil
:selection/mode false
;; Warning: blocks order is determined when setting this attribute
:selection/blocks []
:selection/start-block nil
;; either :up or :down, defaults to down
;; used to determine selection direction when two or more blocks are selected
:selection/direction :down
:custom-context-menu/show? false
:custom-context-menu/links nil
;; pages or blocks in the right sidebar
;; It is a list of `[repo db-id block-type block-data]` 4-tuple
:sidebar/blocks '()
:preferred-language (storage/get :preferred-language)
;; electron
:electron/auto-updater-downloaded false
:electron/updater-pending? false
:electron/updater {}
:electron/user-cfgs nil
;; plugin
:plugin/enabled (and (util/electron?)
;; true false :theme-only
((fnil identity true) (storage/get :lsp-core-enabled)))
:plugin/indicator-text nil
:plugin/installed-plugins {}
:plugin/installed-themes []
:plugin/installed-commands {}
:plugin/installed-ui-items {}
:plugin/simple-commands {}
:plugin/selected-theme nil
:plugin/selected-unpacked-pkg nil
:plugin/marketplace-pkgs nil
:plugin/marketplace-stats nil
:plugin/installing nil
:plugin/active-readme nil
:plugin/updates-pending {}
:plugin/updates-coming {}
:plugin/updates-downloading? false
:plugin/updates-unchecked #{}
:plugin/navs-settings? true
:plugin/focused-settings nil ;; plugin id
;; pdf
:pdf/current nil
:pdf/ref-highlight nil
;; all notification contents as k-v pairs
:notification/contents {}
:graph/syncing? false
;; graph -> state
:graph/parsing-state {}
;; copied blocks
:copy/blocks {:copy/content nil :copy/block-ids nil}
:copy/export-block-text-indent-style (or (storage/get :copy/export-block-text-indent-style)
"dashes")
:copy/export-block-text-remove-options (or (storage/get :copy/export-block-text-remove-options)
#{})
:date-picker/date nil
:youtube/players {}
;; command palette
:command-palette/commands []
:view/components {}
:debug/write-acks {}
:encryption/graph-parsing? false
:favorites/dragging nil
:srs/mode? false
:srs/cards-due-count nil
:reactive/query-dbs {}
;; login, userinfo, token, ...
:auth/refresh-token nil
:auth/access-token nil
:auth/id-token nil
;; file-sync
:file-sync/sync-manager nil
:file-sync/sync-state-manager nil
:file-sync/sync-state nil
:file-sync/sync-uploading-files nil
:file-sync/sync-downloading-files nil
})))
;; block uuid -> {content(String) -> ast}
(def blocks-ast-cache (atom {}))
(defn add-block-ast-cache!
[block-uuid content ast]
(when (and block-uuid content ast)
(let [new-value (assoc-in @blocks-ast-cache [block-uuid content] ast)
new-value (if (> (count new-value) 10000)
(into {} (take 5000 new-value))
new-value)]
(reset! blocks-ast-cache new-value))))
(defn get-block-ast
[block-uuid content]
(when (and block-uuid content)
(get-in @blocks-ast-cache [block-uuid content])))
(defn sub
[ks]
(if (coll? ks)
(util/react (rum/cursor-in state ks))
(util/react (rum/cursor state ks))))
(defn get-route-match
[]
(:route-match @state))
(defn get-current-route
[]
(get-in (get-route-match) [:data :name]))
(defn home?
[]
(= :home (get-current-route)))
(defn setups-picker?
[]
(= :repo-add (get-current-route)))
(defn get-current-page
[]
(when (= :page (get-current-route))
(get-in (get-route-match)
[:path-params :name])))
(defn route-has-p?
[]
(get-in (get-route-match) [:query-params :p]))
(defn set-state!
[path value]
(if (vector? path)
(swap! state assoc-in path value)
(swap! state assoc path value)))
(defn update-state!
[path f]
(if (vector? path)
(swap! state update-in path f)
(swap! state update path f)))
(defn get-current-repo
[]
(or (:git/current-repo @state)
(when-not (mobile-util/is-native-platform?)
"local")))
(defn get-config
([]
(get-config (get-current-repo)))
([repo-url]
(get-in @state [:config repo-url])))
(def default-arweave-gateway "https://arweave.net")
(defn get-arweave-gateway
[]
(:arweave/gateway (get-config) default-arweave-gateway))
(defonce built-in-macros
{"img" "[:img.$4 {:src \"$1\" :style {:width $2 :height $3}}]"})
(defn get-macros
[]
(merge
built-in-macros
(:macros (get-config))))
(defn sub-config
[]
(sub :config))
(defn get-custom-css-link
[]
(:custom-css-url (get-config)))
(defn get-custom-js-link
[]
(:custom-js-url (get-config)))
(defn get-default-journal-template
[]
(when-let [template (get-in (get-config) [:default-templates :journals])]
(when-not (string/blank? template)
(string/trim template))))
(defn all-pages-public?
[]
(let [value (:publishing/all-pages-public? (get-config))
value (if (some? value) value (:all-pages-public? (get-config)))]
(true? value)))
(defn enable-grammarly?
[]
(true? (:feature/enable-grammarly?
(get (sub-config) (get-current-repo)))))
;; (defn store-block-id-in-file?
;; []
;; (true? (:block/store-id-in-file? (get-config))))
(defn scheduled-deadlines-disabled?
[]
(true? (:feature/disable-scheduled-and-deadline-query?
(get (sub-config) (get-current-repo)))))
(defn enable-timetracking?
[]
(not (false? (:feature/enable-timetracking?
(get (sub-config) (get-current-repo))))))
(defn enable-journals?
[repo]
(not (false? (:feature/enable-journals?
(get (sub-config) repo)))))
(defn export-heading-to-list?
[]
(not (false? (:export/heading-to-list?
(get (sub-config) (get-current-repo))))))
(defn enable-encryption?
[repo]
(:feature/enable-encryption?
(get (sub-config) repo)))
(defn enable-git-auto-push?
[repo]
(not (false? (:git-auto-push
(get (sub-config) repo)))))
(defn enable-block-timestamps?
[]
(true? (:feature/enable-block-timestamps?
(get (sub-config) (get-current-repo)))))
(defn sub-graph-config
[]
(get (sub-config) (get-current-repo)))
(defn sub-graph-config-settings
[]
(:graph/settings (sub-graph-config)))
;; Enable by default
(defn show-brackets?
[]
(not (false? (:ui/show-brackets?
(get (sub-config) (get-current-repo))))))
(defn get-default-home
[]
(:default-home (get-config)))
(defn sub-default-home-page
[]
(get-in (sub-config) [(get-current-repo) :default-home :page] ""))
(defn custom-home-page?
[]
(some? (:page (get-default-home))))
(defn get-preferred-format
([]
(get-preferred-format (get-current-repo)))
([repo-url]
(keyword
(or
(when-let [fmt (:preferred-format (get-config repo-url))]
(string/lower-case (name fmt)))
(get-in @state [:me :preferred_format] "markdown")))))
;; TODO: consider adding a pane in Settings to set this through the GUI (rather
;; than having to go through the config.edn file)
(defn get-editor-command-trigger
([] (get-editor-command-trigger (get-current-repo)))
([repo-url]
(or
(:editor/command-trigger (get-config repo-url)) ;; Get from user config
"/"))) ;; Set the default
(defn markdown?
[]
(= (keyword (get-preferred-format))
:markdown))
(defn get-pages-directory
[]
(or
(when-let [repo (get-current-repo)]
(:pages-directory (get-config repo)))
"pages"))
(defn get-journals-directory
[]
(or
(when-let [repo (get-current-repo)]
(:journals-directory (get-config repo)))
"journals"))
(defn org-mode-file-link?
[repo]
(:org-mode/insert-file-link? (get-config repo)))
(defn get-journal-file-name-format
[]
(when-let [repo (get-current-repo)]
(:journal/file-name-format (get-config repo))))
(defn get-preferred-workflow
[]
(keyword
(or
(when-let [workflow (:preferred-workflow (get-config))]
(let [workflow (name workflow)]
(if (util/safe-re-find #"now|NOW" workflow)
:now
:todo)))
(get-in @state [:me :preferred_workflow] :now))))
(defn get-preferred-todo
[]
(if (= (get-preferred-workflow) :now)
"LATER"
"TODO"))
(defn page-name-order
"Decide whether to use file name or :title as page name. If it returns \"file\", use the file
name unless it is missing."
[]
(:page-name-order (get-config)))
(defn get-repos
[]
(get-in @state [:me :repos]))
(defn set-repos!
[repos]
(set-state! [:me :repos] repos))
(defn add-repo!
[repo]
(when (not (string/blank? repo))
(update-state! [:me :repos]
(fn [repos]
(->> (conj repos repo)
(distinct))))))
(defn set-current-repo!
[repo]
(swap! state assoc :git/current-repo repo)
(if repo
(storage/set :git/current-repo repo)
(storage/remove :git/current-repo))
(ipc/ipc "setCurrentGraph" repo))
(defn set-preferred-format!
[format]
(swap! state assoc-in [:me :preferred_format] (name format)))
(defn set-preferred-workflow!
[workflow]
(swap! state assoc-in [:me :preferred_workflow] (name workflow)))
(defn set-preferred-language!
[language]
(set-state! :preferred-language (name language))
(storage/set :preferred-language (name language)))
(defn delete-repo!
[repo]
(swap! state update-in [:me :repos]
(fn [repos]
(->> (remove #(= (:url repo)
(:url %))
repos)
(util/distinct-by :url))))
(when (= (get-current-repo) (:url repo))
(set-current-repo! (:url (first (get-repos))))))
(defn set-timestamp-block!
[value]
(set-state! :editor/set-timestamp-block value))
(defn get-timestamp-block
[]
(:editor/set-timestamp-block @state))
(defn set-edit-content!
([input-id value] (set-edit-content! input-id value true))
([input-id value set-input-value?]
(when input-id
(when set-input-value?
(when-let [input (gdom/getElement input-id)]
(util/set-change-value input value)))
(update-state! :editor/content (fn [m]
(assoc m input-id value))))))
(defn get-edit-input-id
[]
(ffirst (:editor/editing? @state)))
(defn get-input
[]
(when-let [id (get-edit-input-id)]
(gdom/getElement id)))
(defn editing?
[]
(let [input (get-input)]
(and input (= input (.-activeElement js/document)))))
(defn get-edit-content
[]
(get (:editor/content @state) (get-edit-input-id)))
(defn sub-edit-content
[]
(sub [:editor/content (get-edit-input-id)]))
(defn append-current-edit-content!
[append-text]
(when-not (string/blank? append-text)
(when-let [input-id (get-edit-input-id)]
(when-let [input (gdom/getElement input-id)]
(let [value (gobj/get input "value")
new-value (str value append-text)
new-value (if (or (= (last value) " ")
(= (last value) "\n"))
new-value
(str "\n" new-value))]
(js/document.execCommand "insertText" false append-text)
(update-state! :editor/content (fn [m]
(assoc m input-id new-value))))))))
(defn get-cursor-range
[]
(:cursor-range @state))
(defn set-cursor-range!
[range]
(set-state! :cursor-range range))
(defn set-cloning!
[value]
(set-state! :repo/cloning? value))
(defn set-q!
[value]
(set-state! :search/q value))
(defn set-search-mode!
[value]
(set-state! :search/mode value))
(defn set-editor-show-page-search!
[value]
(set-state! :editor/show-page-search? value))
(defn get-editor-show-page-search?
[]
(get @state :editor/show-page-search?))
(defn set-editor-show-page-search-hashtag!
[value]
(set-state! :editor/show-page-search? value)
(set-state! :editor/show-page-search-hashtag? value))
(defn get-editor-show-page-search-hashtag?
[]
(get @state :editor/show-page-search-hashtag?))
(defn set-editor-show-block-search!
[value]
(set-state! :editor/show-block-search? value))
(defn get-editor-show-block-search?
[]
(get @state :editor/show-block-search?))
(defn set-editor-show-template-search!
[value]
(set-state! :editor/show-template-search? value))
(defn get-editor-show-template-search?
[]
(get @state :editor/show-template-search?))
(defn set-editor-show-date-picker!
[value]
(set-state! :editor/show-date-picker? value))
(defn get-editor-show-date-picker?
[]
(get @state :editor/show-date-picker?))
(defn set-editor-show-input!
[value]
(set-state! :editor/show-input value))
(defn get-editor-show-input
[]
(get @state :editor/show-input))
(defn set-editor-show-zotero!
[value]
(set-state! :editor/show-zotero value))
;; TODO: refactor, use one state
(defn clear-editor-show-state!
[]
(swap! state (fn [state]
(assoc state
:editor/show-input nil
:editor/show-zotero false
:editor/show-date-picker? false
:editor/show-block-search? false
:editor/show-template-search? false
:editor/show-page-search? false
:editor/show-page-search-hashtag? false))))
(defn set-edit-input-id!
[input-id]
(swap! state update :editor/editing?
(fn [_m]
(and input-id {input-id true}))))
(defn get-edit-pos
[]
(when-let [input (get-input)]
(util/get-selection-start input)))
(defn set-selection-start-block!
[start-block]
(swap! state assoc :selection/start-block start-block))
(defn get-selection-start-block
[]
(get @state :selection/start-block))
(defn set-selection-blocks!
([blocks]
(set-selection-blocks! blocks :down))
([blocks direction]
(when (seq blocks)
(let [blocks (util/sort-by-height blocks)]
(swap! state assoc
:selection/mode true
:selection/blocks blocks
:selection/direction direction)))))
(defn into-selection-mode!
[]
(swap! state assoc :selection/mode true))
(defn clear-selection!
[]
(swap! state assoc
:selection/mode false
:selection/blocks nil
:selection/direction :down))
(defn get-selection-blocks
[]
(:selection/blocks @state))
(defn get-selection-block-ids
[]
(->> (sub :selection/blocks)
(keep #(when-let [id (dom/attr % "blockid")]
(uuid id)))
(distinct)))
(defn in-selection-mode?
[]
(:selection/mode @state))
(defn selection?
"True sense of selection mode with valid selected block"
[]
(and (in-selection-mode?) (seq (get-selection-blocks))))
(defn conj-selection-block!
[block direction]
(dom/add-class! block "selected noselect")
(swap! state assoc
:selection/mode true
:selection/blocks (-> (conj (vec (:selection/blocks @state)) block)
(util/sort-by-height))
:selection/direction direction))
(defn drop-last-selection-block!
[]
(let [last-block (peek (vec (:selection/blocks @state)))]
(swap! state assoc
:selection/mode true
:selection/blocks (pop (vec (:selection/blocks @state))))
last-block))
(defn get-selection-direction
[]
(:selection/direction @state))
(defn show-custom-context-menu!
[links]
(swap! state assoc
:custom-context-menu/show? true
:custom-context-menu/links links))
(defn hide-custom-context-menu!
[]
(swap! state assoc
:custom-context-menu/show? false
:custom-context-menu/links nil))
(defn set-github-installation-tokens!
[tokens]
(when (seq tokens)
(let [tokens (medley/index-by :installation_id tokens)
repos (get-repos)]
(when (seq repos)
(let [set-token-f
(fn [{:keys [installation_id] :as repo}]
(let [{:keys [token] :as m} (get tokens installation_id)]
(if (string? token)
;; GitHub API returns a expires_at key which is a timestamp (expires after 60 minutes at present),
;; however, user's system time may be inaccurate. Here, based on the client system time, we use
;; 40-minutes interval to deal with some critical conditions, for e.g. http request time consume.
(let [formatter (tf/formatters :date-time-no-ms)
expires-at (->> (t/plus (t/now) (t/minutes 40))
(tf/unparse formatter))]
(merge repo {:token token :expires_at expires-at}))
(do
(when (and
(:url repo)
(string/starts-with? (:url repo) "https://"))
(log/error :token/cannot-set-token {:repo-m repo :token-m m}))
repo))))
repos (mapv set-token-f repos)]
(swap! state assoc-in [:me :repos] repos))))))
(defn get-github-token
[repo]
(when repo
(let [repos (get-repos)]
(some #(when (= repo (:url %)) %) repos))))
(defn toggle-sidebar-open?!
[]
(swap! state update :ui/sidebar-open? not))
(defn open-right-sidebar!
[]
(swap! state assoc :ui/sidebar-open? true))
(defn hide-right-sidebar!
[]
(swap! state assoc :ui/sidebar-open? false))
(defn sidebar-add-block!
[repo db-id block-type block-data]
(when (not (util/sm-breakpoint?))
(when db-id
(update-state! :sidebar/blocks (fn [blocks]
(->> (remove #(= (second %) db-id) blocks)
(cons [repo db-id block-type block-data])
(distinct))))
(open-right-sidebar!)
(when-let [elem (gdom/getElementByClass "cp__right-sidebar-scrollable")]
(util/scroll-to elem 0)))))
(defn sidebar-remove-block!
[idx]
(update-state! :sidebar/blocks (fn [blocks]
(if (string? idx)
(remove #(= (second %) idx) blocks)
(util/drop-nth idx blocks))))
(when (empty? (:sidebar/blocks @state))
(hide-right-sidebar!)))
(defn sidebar-block-exists?
[idx]
(some #(= (second %) idx) (:sidebar/blocks @state)))
(defn clear-sidebar-blocks!
[]
(set-state! :sidebar/blocks '()))
(defn sidebar-block-toggle-collapse!
[db-id]
(when db-id
(update-state! [:ui/sidebar-collapsed-blocks db-id] not)))
(defn get-edit-block
[]
(get @state :editor/block))
(defn get-last-edit-block
[]
(:editor/last-edit-block @state))
(defn get-current-edit-block-and-position
[]
(let [edit-input-id (get-edit-input-id)
edit-block (get-edit-block)
block-element (when edit-input-id (gdom/getElement (string/replace edit-input-id "edit-block" "ls-block")))
container (when block-element
(util/get-block-container block-element))]
(when container
{:last-edit-block edit-block
:container (gobj/get container "id")
:pos (cursor/pos (gdom/getElement edit-input-id))})))
(defonce publishing? (atom nil))
(defn publishing-enable-editing?
[]
(and @publishing? (:publishing/enable-editing? (get-config))))
(defn enable-editing?
[]
(or (not @publishing?) (:publishing/enable-editing? (get-config))))
(defn set-editing!
([edit-input-id content block cursor-range]
(set-editing! edit-input-id content block cursor-range true))
([edit-input-id content block cursor-range move-cursor?]
(when (and edit-input-id block
(or
(publishing-enable-editing?)
(not @publishing?)))
(let [block-element (gdom/getElement (string/replace edit-input-id "edit-block" "ls-block"))
container (util/get-block-container block-element)
block (if container
(assoc block
:block/container (gobj/get container "id"))
block)
content (string/trim (or content ""))]
(swap! state
(fn [state]
(-> state
(assoc-in [:editor/content edit-input-id] content)
(assoc
:editor/block block
:editor/editing? {edit-input-id true}
:editor/last-edit-block block
:editor/last-key-code nil
:cursor-range cursor-range))))
(when-let [input (gdom/getElement edit-input-id)]
(let [pos (count cursor-range)]
(when content
(util/set-change-value input content))
(when move-cursor?
(cursor/move-cursor-to input pos))
(when (or (util/mobile?) (mobile-util/is-native-platform?))
(util/make-el-center-if-near-top input))))))))
(defn clear-edit!
[]
(swap! state merge {:editor/editing? nil
:editor/block nil
:cursor-range nil
:editor/last-saved-cursor nil}))
(defn into-code-editor-mode!
[]
(swap! state merge {:editor/editing? nil
:cursor-range nil
:editor/code-mode? true}))
(defn set-editor-last-pos!
[new-pos]
(set-state! :editor/last-saved-cursor new-pos))
(defn clear-editor-last-pos!
[]
(set-state! :editor/last-saved-cursor nil))
(defn get-editor-last-pos
[]
(:editor/last-saved-cursor @state))
(defn set-block-content-and-last-pos!
[edit-input-id content new-pos]
(when edit-input-id
(set-edit-content! edit-input-id content)
(set-state! :editor/last-saved-cursor new-pos)))
(defn set-theme!
[theme]
(set-state! :ui/theme theme)
(when (mobile-util/native-ios?)
(if (= theme "light")
(util/set-theme-light)
(util/set-theme-dark)))
(storage/set :ui/theme theme))
(defn sync-system-theme!
[]
(let [system-dark? (.-matches (js/window.matchMedia "(prefers-color-scheme: dark)"))]
(set-theme! (if system-dark? "dark" "light"))
(set-state! :ui/system-theme? true)
(storage/set :ui/system-theme? true)))
(defn use-theme-mode!
[theme-mode]
(if-not (= theme-mode "system")
(do
(set-theme! theme-mode)
(set-state! :ui/system-theme? false)
(storage/set :ui/system-theme? false))
(sync-system-theme!)))
(defn set-editing-block-dom-id!
[block-dom-id]
(set-state! :editor/block-dom-id block-dom-id))
(defn get-editing-block-dom-id
[]
(:editor/block-dom-id @state))
(defn toggle-theme!
[]
(let [theme (:ui/theme @state)
theme' (if (= theme "dark") "light" "dark")]
(use-theme-mode! theme')))
(defn set-root-component!
[component]
(set-state! :ui/root-component component))
(defn get-root-component
[]
(get @state :ui/root-component))
(defn load-app-user-cfgs
([] (load-app-user-cfgs false))
([refresh?]
(p/let [cfgs (if (or refresh? (nil? (:electron/user-cfgs @state)))
(ipc/ipc "userAppCfgs")
(:electron/user-cfgs @state))
cfgs (if (object? cfgs) (bean/->clj cfgs) cfgs)]
(set-state! :electron/user-cfgs cfgs))))
(defn setup-electron-updater!
[]
(when (util/electron?)
(js/window.apis.setUpdatesCallback
(fn [_ args]
(let [data (bean/->clj args)
pending? (not= (:type data) "completed")]
(set-state! :electron/updater-pending? pending?)
(when pending? (set-state! :electron/updater data))
nil)))))
(defn set-file-component!
[component]