-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
state.cljs
2107 lines (1738 loc) · 60.8 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
"Provides main application state, fns associated to set and state based rum
cursors"
(:require [cljs-bean.core :as bean]
[cljs.core.async :as async :refer [<!]]
[cljs.spec.alpha :as s]
[clojure.string :as string]
[dommy.core :as dom]
[electron.ipc :as ipc]
[frontend.mobile.util :as mobile-util]
[frontend.storage :as storage]
[frontend.spec.storage :as storage-spec]
[frontend.util :as util]
[frontend.util.cursor :as cursor]
[goog.dom :as gdom]
[goog.object :as gobj]
[logseq.graph-parser.config :as gp-config]
[medley.core :as medley]
[promesa.core :as p]
[rum.core :as rum]))
;; Stores main application state
(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 1000)
:db/batch-txs (async/chan 1000)
:file/writes (async/chan 10000)
:file/unlinked-dirs #{}
:reactive/custom-queries (async/chan 1000)
:notification/show? false
:notification/content nil
:repo/loading-files? {}
: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
:draw? false
:db/restoring? nil
:journals-length 3
:search/q ""
:search/mode :global ;; inner page or full graph? {:page :global}
:search/result nil
:search/graph-filters []
:search/engines {}
;; modals
:modal/dropdowns {}
:modal/id nil
:modal/label ""
:modal/show? false
:modal/panel-content nil
:modal/fullscreen? false
:modal/close-btn? nil
:modal/close-backdrop? true
:modal/subsets []
;; ui
:ui/viewport {}
;; left sidebar
:ui/navigation-item-collapsed? {}
;; 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) "light")
:ui/system-theme? ((fnil identity (or util/mac? util/win32? false)) (storage/get :ui/system-theme?))
:ui/custom-theme (or (storage/get :ui/custom-theme) {:light {:mode "light"} :dark {:mode "dark"}})
: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/show-recent? 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/scrolling? false
:document/mode? document-mode?
:config {}
:block/component-editing-mode? false
:editor/hidden-editors #{} ;; page names
:editor/draw-mode? false
:editor/action nil
:editor/action-data nil
;; With label or other data
: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 ;; click rendered block timestamp-cp to set timestamp
:editor/last-input-time nil
:editor/document-mode? document-mode?
:editor/args nil
:editor/on-paste? false
:editor/last-key-code nil
;; for audio record
:editor/record-status "NONE"
;; Whether to skip saving the current block
:editor/skip-saving-current-block? false
:editor/code-block-context {}
: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
:selection/selected-all? false
:custom-context-menu/show? false
:custom-context-menu/links nil
:custom-context-menu/position 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
:electron/server nil
;; assets
:assets/alias-enabled? (or (storage/get :assets/alias-enabled?) false)
:assets/alias-dirs (or (storage/get :assets/alias-dirs) [])
;; mobile
:mobile/container-urls nil
:mobile/show-action-bar? false
:mobile/actioned-block nil
:mobile/show-toolbar? false
:mobile/show-recording-bar? false
:mobile/show-tabbar? false
;;; Used to monitor mobile app status,
;;; value spec:
;;; {:is-active? bool, :timestamp int}
:mobile/app-state-change (atom nil)
;; plugin
:plugin/enabled (and (util/electron?)
;; true false :theme-only
((fnil identity true) (storage/get ::storage-spec/lsp-core-enabled)))
:plugin/preferences nil
:plugin/indicator-text nil
:plugin/installed-plugins {}
:plugin/installed-themes []
:plugin/installed-slash-commands {}
:plugin/installed-ui-items {}
:plugin/installed-resources {}
:plugin/installed-hooks {}
:plugin/installed-services {}
: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/system-win? false
:pdf/current nil
:pdf/ref-highlight nil
:pdf/block-highlight-colored? (or (storage/get "ls-pdf-hl-block-is-colored") true)
;; 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/graph nil
:copy/blocks 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)
#{})
:copy/export-block-text-other-options (or (storage/get :copy/export-block-text-other-options)
{})
:date-picker/date nil
:youtube/players {}
;; command palette
:command-palette/commands []
:view/components {}
:favorites/dragging nil
:srs/mode? false
:srs/cards-due-count nil
:reactive/query-dbs {}
;; login, userinfo, token, ...
:auth/refresh-token (storage/get "refresh-token")
:auth/access-token nil
:auth/id-token nil
;; file-sync
:file-sync/jstour-inst nil
:file-sync/onboarding-state (or (storage/get :file-sync/onboarding-state)
{:welcome false})
:file-sync/remote-graphs {:loading false :graphs nil}
:file-sync/set-remote-graph-password-result {}
;; graph-uuid -> {:graphs-txid {}
;; :file-sync/sync-manager {}
;; :file-sync/sync-state {}
;; ;; {file-path -> payload}
;; :file-sync/progress {}
;; :file-sync/start-time {}
;; :file-sync/last-synced-at {}}
:file-sync/graph-state {:current-graph-uuid nil
;; graph-uuid -> ...
}
:user/info {:UserGroups (storage/get :user-groups)}
:encryption/graph-parsing? false
:ui/loading? {}
:feature/enable-sync? (storage/get :logseq-sync-enabled)
:file/rename-event-chan (async/chan 100)
:ui/find-in-page nil
:graph/importing nil
:graph/importing-state {}
:whiteboard/onboarding-whiteboard? (or (storage/get :ls-onboarding-whiteboard?) false)
:whiteboard/onboarding-tour? (or (storage/get :whiteboard-onboarding-tour?) false)
:whiteboard/last-persisted-at {}
:whiteboard/pending-tx-data {}})))
;; Block ast state
;; ===============
;; 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])))
;; User configuration getters under :config (and sometimes :me)
;; ========================================
;; TODO: Refactor default config values to be data driven. Currently they are all
;; buried in getters
;; TODO: Refactor our access to be more data driven. Currently each getter
;; (re-)fetches get-current-repo needlessly
;; TODO: Add consistent validation. Only a few config options validate at get time
(def default-config
"Default config for a repo-specific, user config"
{:feature/enable-search-remove-accents? true
:default-arweave-gateway "https://arweave.net"
:ui/auto-expand-block-refs? true
;; For flushing the settings of old versions. Don't bump this value.
;; There are only two kinds of graph, one is not upgraded (:legacy) and one is upgraded (:triple-lowbar)
;; For not upgraded graphs, the config will have no key `:file/name-format`
;; Then the default value is applied
:file/name-format :legacy})
;; State that most user config is dependent on
(declare get-current-repo sub set-state!)
(defn merge-configs
"Merges user configs in given orders. All values are overridden except for maps
which are merged."
[& configs]
(->> configs
(filter map?)
(apply merge-with
(fn merge-config [current new]
(if (and (map? current) (map? new))
(merge current new)
new)))))
(defn get-config
"User config for the given repo or current repo if none given. All config fetching
should be done through this fn in order to get global config and config defaults"
([]
(get-config (get-current-repo)))
([repo-url]
(merge-configs
default-config
(get-in @state [:config ::global-config])
(get-in @state [:config repo-url]))))
(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 get-arweave-gateway
[]
(:arweave/gateway (get-config)))
(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 set-assets-alias-enabled!
[v]
(set-state! :assets/alias-enabled? (boolean v))
(storage/set :assets/alias-enabled? (boolean v)))
(defn set-assets-alias-dirs!
[dirs]
(when dirs
(set-state! :assets/alias-dirs dirs)
(storage/set :assets/alias-dirs dirs)))
(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 get-default-home
[]
(:default-home (get-config)))
(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 get-whiteboards-directory
[]
(or
(when-let [repo (get-current-repo)]
(:whiteboards-directory (get-config repo)))
"whiteboards"))
(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 get-filename-format
([] (get-filename-format (get-current-repo)))
([repo]
(:file/name-format (get-config repo))))
(defn get-date-formatter
[]
(gp-config/get-date-formatter (get-config)))
(defn shortcuts []
(:shortcuts (get-config)))
(defn get-commands
[]
(:commands (get-config)))
(defn get-scheduled-future-days
[]
(let [days (:scheduled/future-days (get-config))]
(or (when (int? days) days) 0)))
(defn get-start-of-week
[]
(or (:start-of-week (get-config))
(get-in @state [:me :settings :start-of-week])
6))
(defn get-ref-open-blocks-level
[]
(or
(when-let [value (:ref/default-open-blocks-level (get-config))]
(when (integer? value)
value))
2))
(defn get-linked-references-collapsed-threshold
[]
(or
(when-let [value (:ref/linked-references-collapsed-threshold (get-config))]
(when (integer? value)
value))
100))
(defn get-export-bullet-indentation
[]
(case (get (get-config) :export/bullet-indentation :tab)
:eight-spaces
" "
:four-spaces
" "
:two-spaces
" "
:tab
"\t"))
(defn enable-search-remove-accents?
[]
(:feature/enable-search-remove-accents? (get-config)))
;; State cursor fns for use with rum components
;; ============================================
(declare document-mode?)
(defn sub
"Creates a rum cursor, https://github.com/tonsky/rum#cursors, for use in rum components.
Similar to re-frame subscriptions"
[ks]
(if (coll? ks)
(util/react (rum/cursor-in state ks))
(util/react (rum/cursor state ks))))
(defn sub-config
"Sub equivalent to get-config which should handle all sub user-config access"
([] (sub-config (get-current-repo)))
([repo]
(let [config (sub :config)]
(merge-configs default-config
(get config ::global-config)
(get config repo)))))
(defn enable-grammarly?
[]
(true? (:feature/enable-grammarly? (sub-config))))
(defn scheduled-deadlines-disabled?
[]
(true? (:feature/disable-scheduled-and-deadline-query? (sub-config))))
(defn enable-timetracking?
[]
(not (false? (:feature/enable-timetracking? (sub-config)))))
(defn enable-fold-button-right?
[]
(let [_ (sub :ui/viewport)]
(and (util/mobile?)
(util/sm-breakpoint?))))
(defn enable-journals?
([]
(enable-journals? (get-current-repo)))
([repo]
(not (false? (:feature/enable-journals? (sub-config repo))))))
(defn enable-flashcards?
([]
(enable-flashcards? (get-current-repo)))
([repo]
(not (false? (:feature/enable-flashcards? (sub-config repo))))))
(defn enable-sync?
[]
(sub :feature/enable-sync?))
(defn enable-whiteboards?
([]
(enable-whiteboards? (get-current-repo)))
([repo]
(not (false? (:feature/enable-whiteboards? (sub-config repo))))))
(defn enable-git-auto-push?
[repo]
(not (false? (:git-auto-push (sub-config repo)))))
(defn enable-block-timestamps?
[]
(true? (:feature/enable-block-timestamps? (sub-config))))
(defn graph-settings
[]
(:graph/settings (sub-config)))
;; Enable by default
(defn show-brackets?
[]
(not (false? (:ui/show-brackets? (sub-config)))))
(defn sub-default-home-page
[]
(get-in (sub-config) [:default-home :page] ""))
(defn sub-edit-content
[id]
(sub [:editor/content id]))
(defn- get-selected-block-ids
[blocks]
(->> blocks
(remove nil?)
(keep #(when-let [id (dom/attr % "blockid")]
(uuid id)))
(distinct)))
(defn sub-block-selected?
[block-uuid]
(rum/react
(rum/derived-atom [state] [::select-block block-uuid]
(fn [state]
(contains? (set (get-selected-block-ids (:selection/blocks state)))
block-uuid)))))
(defn block-content-max-length
[repo]
(or (:block/content-max-length (sub-config repo)) 10000))
(defn mobile?
[]
(or (util/mobile?) (mobile-util/native-platform?)))
(defn enable-tooltip?
[]
(if (mobile?)
false
(get (sub-config) :ui/enable-tooltip? true)))
(defn show-command-doc?
[]
(get (sub-config) :ui/show-command-doc? true))
(defn logical-outdenting?
[]
(:editor/logical-outdenting? (sub-config)))
(defn show-full-blocks?
[]
(:ui/show-full-blocks? (sub-config)))
(defn preferred-pasting-file?
[]
(:editor/preferred-pasting-file? (sub-config)))
(defn auto-expand-block-refs?
[]
(:ui/auto-expand-block-refs? (sub-config)))
(defn doc-mode-enter-for-new-line?
[]
(and (document-mode?)
(not (:shortcut/doc-mode-enter-for-new-block? (get-config)))))
(defn user-groups
[]
(set (sub [:user/info :UserGroups])))
;; State mutation helpers
;; ======================
(defn set-state!
[path value]
(if (vector? path)
(swap! state assoc-in path value)
(swap! state assoc path value))
nil)
(defn update-state!
[path f]
(if (vector? path)
(swap! state update-in path f)
(swap! state update path f))
nil)
;; State getters and setters
;; =========================
;; These fns handle any key except :config.
;; Some state is also stored in local storage and/or sent to electron's main process
(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 whiteboard-dashboard?
[]
(= :whiteboards (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 whiteboard-route?
[]
(= :whiteboard (get-current-route)))
(defn get-current-whiteboard
[]
(when (whiteboard-route?)
(get-in (get-route-match)
[:path-params :name])))
(defn route-has-p?
[]
(get-in (get-route-match) [:query-params :p]))
(defn get-current-repo
[]
(or (:git/current-repo @state)
(when-not (mobile-util/native-platform?)
"local")))
(defn get-remote-graphs
[]
(get-in @state [:file-sync/remote-graphs :graphs]))
(defn get-remote-graph-info-by-uuid
[uuid]
(when-let [graphs (seq (get-in @state [:file-sync/remote-graphs :graphs]))]
(some #(when (= (:GraphUUID %) (str uuid)) %) graphs)))
(defn delete-remote-graph!
[repo]
(swap! state update-in [:file-sync/remote-graphs :graphs]
(fn [repos]
(remove #(and
(:GraphUUID repo)
(:GraphUUID %)
(= (:GraphUUID repo) (:GraphUUID %))) repos))))
(defn add-remote-graph!
[repo]
(swap! state update-in [:file-sync/remote-graphs :graphs]
(fn [repos]
(->> (conj repos repo)
(distinct)))))
(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 #(or (= (:url repo) (:url %))
(and
(:GraphUUID repo)
(:GraphUUID %)
(= (:GraphUUID repo) (:GraphUUID %)))) repos)
(util/distinct-by :url)))))
(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 get-cursor-range
[]
(:cursor-range @state))
(defn set-cursor-range!
[range]
(set-state! :cursor-range range))
(defn set-q!
[value]
(set-state! :search/q value))
(defn set-search-mode!
[value]
(set-state! :search/mode value))
(defn set-editor-action!
[value]
(set-state! :editor/action value))
(defn set-editor-action-data!
[value]
(set-state! :editor/action-data value))
(defn get-editor-action
[]
(:editor/action @state))
(defn get-editor-action-data
[]
(:editor/action-data @state))
(defn get-editor-show-page-search?
[]
(= (get-editor-action) :page-search))
(defn get-editor-show-page-search-hashtag?
[]
(= (get-editor-action) :page-search-hashtag))
(defn get-editor-show-block-search?
[]
(= (get-editor-action) :block-search))
(defn set-editor-show-input!
[value]
(if value
(do
(set-editor-action-data! (assoc (get-editor-action-data) :options value))
(set-editor-action! :input))
(do
(set-editor-action! nil)
(set-editor-action-data! nil))))
(defn get-editor-show-input
[]
(when (= (get-editor-action) :input)
(get @state :editor/action-data)))
(defn set-editor-show-commands!
[]
(when-not (get-editor-action) (set-editor-action! :commands)))
(defn set-editor-show-block-commands!
[]
(when-not (get-editor-action) (set-editor-action! :block-commands)))
(defn clear-editor-action!
[]
(swap! state (fn [state]
(assoc state :editor/action nil))))
(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 get-selection-start-block
[]
(get @state :selection/start-block))
(defn set-selection-start-block!
[start-block]
(when-not (get-selection-start-block)
(swap! state assoc :selection/start-block start-block)))
(defn set-selection-blocks!
([blocks]
(set-selection-blocks! blocks :down))
([blocks direction]
(when (seq blocks)
(let [blocks (util/sort-by-height (remove nil? 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
:selection/start-block nil
:selection/selected-all? false))
(defn get-selection-blocks
[]
(->> (:selection/blocks @state)
(remove nil?)))
(defn get-selection-block-ids
[]
(get-selected-block-ids (get-selection-blocks)))
(defn get-selection-start-block-or-first
[]
(or (get-selection-start-block)
(some-> (first (get-selection-blocks))
(gobj/get "id"))))
(defn in-selection-mode?
[]
(:selection/mode @state))