-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
block.cljs
3521 lines (3154 loc) · 140 KB
/
block.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.components.block
(:refer-clojure :exclude [range])
(:require-macros [hiccups.core])
(:require ["/frontend/utils" :as utils]
["@capacitor/share" :refer [^js Share]]
[cljs-bean.core :as bean]
[cljs.core.match :refer [match]]
[cljs.reader :as reader]
[clojure.set :as set]
[clojure.string :as string]
[clojure.walk :as walk]
[datascript.core :as d]
[dommy.core :as dom]
[frontend.commands :as commands]
[frontend.components.block.macros :as block-macros]
[frontend.components.datetime :as datetime-comp]
[frontend.components.lazy-editor :as lazy-editor]
[frontend.components.macro :as macro]
[frontend.components.plugins :as plugins]
[frontend.components.query.builder :as query-builder-component]
[frontend.components.svg :as svg]
[frontend.components.query :as query]
[frontend.config :as config]
[frontend.context.i18n :refer [t]]
[frontend.date :as date]
[frontend.db :as db]
[frontend.db.model :as model]
[frontend.db-mixins :as db-mixins]
[frontend.extensions.highlight :as highlight]
[frontend.extensions.latex :as latex]
[frontend.extensions.lightbox :as lightbox]
[frontend.extensions.pdf.assets :as pdf-assets]
[frontend.extensions.sci :as sci]
[frontend.extensions.video.youtube :as youtube]
[frontend.extensions.zotero :as zotero]
[frontend.format.block :as block]
[frontend.format.mldoc :as mldoc]
[frontend.fs :as fs]
[frontend.handler.assets :as assets-handler]
[frontend.handler.block :as block-handler]
[frontend.handler.dnd :as dnd]
[frontend.handler.editor :as editor-handler]
[frontend.handler.file-sync :as file-sync]
[frontend.handler.notification :as notification]
[frontend.handler.plugin :as plugin-handler]
[frontend.handler.repeated :as repeated]
[frontend.handler.route :as route-handler]
[frontend.handler.ui :as ui-handler]
[frontend.handler.whiteboard :as whiteboard-handler]
[frontend.handler.export.common :as export-common-handler]
[frontend.mobile.util :as mobile-util]
[frontend.modules.outliner.tree :as tree]
[frontend.security :as security]
[frontend.shui :refer [get-shui-component-version make-shui-context]]
[frontend.state :as state]
[frontend.template :as template]
[frontend.ui :as ui]
[frontend.util :as util]
[frontend.extensions.pdf.utils :as pdf-utils]
[frontend.util.clock :as clock]
[frontend.util.drawer :as drawer]
[frontend.util.property :as property]
[frontend.util.text :as text-util]
[goog.dom :as gdom]
[goog.object :as gobj]
[lambdaisland.glogi :as log]
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.config :as gp-config]
[logseq.graph-parser.mldoc :as gp-mldoc]
[logseq.graph-parser.property :as gp-property]
[logseq.graph-parser.text :as text]
[logseq.graph-parser.util :as gp-util]
[logseq.graph-parser.util.block-ref :as block-ref]
[logseq.graph-parser.util.page-ref :as page-ref]
[logseq.graph-parser.whiteboard :as gp-whiteboard]
[logseq.shui.core :as shui]
[medley.core :as medley]
[promesa.core :as p]
[reitit.frontend.easy :as rfe]
[rum.core :as rum]
[shadow.loader :as loader]
[datascript.impl.entity :as e]
[logseq.common.path :as path]))
;; local state
(defonce *dragging?
(atom false))
(defonce *dragging-block
(atom nil))
(defonce *drag-to-block
(atom nil))
(def *move-to (atom nil))
;; TODO: dynamic
(defonce max-depth-of-links 5)
(defonce *blocks-container-id (atom 0))
;; TODO:
;; add `key`
(defn- remove-nils
[col]
(remove nil? col))
(defn vec-cat
[& args]
(->> (apply concat args)
remove-nils
vec))
(defn ->elem
([elem items]
(->elem elem nil items))
([elem attrs items]
(let [elem (keyword elem)]
(if attrs
(vec
(cons elem
(cons attrs
(seq items))))
(vec
(cons elem
(seq items)))))))
(defn- join-lines
[l]
(string/trim (apply str l)))
(defn- string-of-url
[url]
(match url
["File" s]
(-> (string/replace s "file://" "")
;; "file:/Users/ll/Downloads/test.pdf" is a normal org file link
(string/replace "file:" ""))
["Complex" m]
(let [{:keys [link protocol]} m]
(if (= protocol "file")
link
(str protocol "://" link)))))
(defn- get-file-absolute-path
[config path]
(js/console.error "TODO: buggy path fn")
(let [path (string/replace path "file:" "")
block-id (:block/uuid config)
current-file (and block-id
(:file/path (:block/file (:block/page (db/entity [:block/uuid block-id])))))]
(when current-file
(let [parts (string/split current-file #"/")
parts-2 (string/split path #"/")
current-dir (util/string-join-path (drop-last 1 parts))]
(cond
(if util/win32? (utils/win32 path) (util/starts-with? path "/"))
path
(and (not (util/starts-with? path ".."))
(not (util/starts-with? path ".")))
(str current-dir "/" path)
:else
(let [parts (loop [acc []
parts (reverse parts)
col (reverse parts-2)]
(if (empty? col)
acc
(let [[part parts] (case (first col)
".."
[(first parts) (rest parts)]
"."
["" parts]
[(first col) (rest parts)])]
(recur (conj acc part)
parts
(rest col)))))
parts (remove #(string/blank? %) parts)]
(util/string-join-path (reverse parts))))))))
(rum/defcs asset-loader
< rum/reactive
(rum/local nil ::exist?)
(rum/local false ::loading?)
{:will-mount (fn [state]
(let [src (first (:rum/args state))]
(if (and (gp-config/local-protocol-asset? src)
(file-sync/current-graph-sync-on?))
(let [*exist? (::exist? state)
;; special handling for asset:// protocol
;; Capacitor uses a special URL for assets loading
asset-path (gp-config/remove-asset-protocol src)
asset-path (fs/asset-path-normalize asset-path)]
(if (string/blank? asset-path)
(reset! *exist? false)
;; FIXME(andelf): possible bug here
(p/let [exist? (fs/asset-href-exists? asset-path)]
(reset! *exist? (boolean exist?))))
(assoc state ::asset-path asset-path ::asset-file? true))
state)))
:will-update (fn [state]
(let [src (first (:rum/args state))
asset-file? (boolean (::asset-file? state))
sync-on? (file-sync/current-graph-sync-on?)
*loading? (::loading? state)
*exist? (::exist? state)]
(when (and sync-on? asset-file? (false? @*exist?))
(let [sync-state (state/get-file-sync-state (state/get-current-file-sync-graph-uuid))
downloading-files (:current-remote->local-files sync-state)
contain-url? (and (seq downloading-files)
(some #(string/ends-with? src %) downloading-files))]
(cond
(and (not @*loading?) contain-url?)
(reset! *loading? true)
(and @*loading? (not contain-url?))
(do
(reset! *exist? true)
(reset! *loading? false))))))
state)}
[state src content-fn]
(let [_ (state/sub-file-sync-state (state/get-current-file-sync-graph-uuid))
exist? @(::exist? state)
loading? @(::loading? state)
asset-file? (::asset-file? state)
sync-enabled? (boolean (file-sync/current-graph-sync-on?))
ext (keyword (util/get-file-ext src))
img? (contains? (gp-config/img-formats) ext)
audio? (contains? config/audio-formats ext)
type (cond img? "image"
audio? "audio"
:else "asset")]
(if (not sync-enabled?)
(content-fn)
(if (and asset-file? (or loading? (nil? exist?)))
[:p.text-sm.opacity-50 (ui/loading (util/format "Syncing %s ..." type))]
(if (or (not asset-file?)
(and exist? (not loading?)))
(content-fn)
[:p.text-error.text-xs [:small.opacity-80
(util/format "%s not found!" (string/capitalize type))]])))))
(defn open-lightbox
[e]
(let [images (js/document.querySelectorAll ".asset-container img")
images (to-array images)
images (if-not (= (count images) 1)
(let [^js image (.closest (.-target e) ".asset-container")
image (. image querySelector "img")]
(->> images
(sort-by (juxt #(.-y %) #(.-x %)))
(split-with (complement #{image}))
reverse
(apply concat)))
images)
images (for [^js it images] {:src (.-src it)
:w (.-naturalWidth it)
:h (.-naturalHeight it)})]
(when (seq images)
(lightbox/preview-images! images))))
(defonce *resizing-image? (atom false))
(rum/defcs ^:large-vars/cleanup-todo resizable-image <
(rum/local nil ::size)
{:will-unmount (fn [state]
(reset! *resizing-image? false)
state)}
[state config title src metadata full-text local?]
(let [size (get state ::size)
breadcrumb? (:breadcrumb? config)]
(ui/resize-provider
(ui/resize-consumer
(if (and (not (mobile-util/native-platform?))
(not breadcrumb?))
(cond->
{:className "resize image-resize"
:onSizeChanged (fn [value]
(when (and (not @*resizing-image?)
(some? @size)
(not= value @size))
(reset! *resizing-image? true))
(reset! size value))
:onMouseUp (fn []
(when (and @size @*resizing-image?)
(when-let [block-id (:block/uuid config)]
(let [size (bean/->clj @size)]
(editor-handler/resize-image! block-id metadata full-text size))))
(when @*resizing-image?
;; TODO: need a better way to prevent the clicking to edit current block
(js/setTimeout #(reset! *resizing-image? false) 200)))
:onClick (fn [e]
(when @*resizing-image? (util/stop e)))}
(and (:width metadata) (not (util/mobile?)))
(assoc :style {:width (:width metadata)}))
{})
[:div.asset-container {:key "resize-asset-container"}
[:img.rounded-sm.relative
(merge
{:loading "lazy"
:src src
:title title}
metadata)]
(when-not breadcrumb?
[:<>
[:.asset-overlay]
(let [image-src (fs/asset-path-normalize src)]
[:.asset-action-bar {:aria-hidden "true"}
;; the image path bar
(when (util/electron?)
[:button.asset-action-btn.text-left
{:title (t (if local? :asset/show-in-folder :asset/open-in-browser))
:tabIndex "-1"
:on-mouse-down util/stop
:on-click (fn [e]
(util/stop e)
(if local?
(js/window.apis.showItemInFolder image-src)
(js/window.apis.openExternal image-src)))}
image-src])
[:.flex
(when-not config/publishing?
[:button.asset-action-btn
{:title (t :asset/delete)
:tabIndex "-1"
:on-mouse-down util/stop
:on-click
(fn [e]
(when-let [block-id (:block/uuid config)]
(let [confirm-fn (ui/make-confirm-modal
{:title (t :asset/confirm-delete (.toLocaleLowerCase (t :text/image)))
:sub-title (if local? :asset/physical-delete "")
:sub-checkbox? local?
:on-confirm (fn [_e {:keys [close-fn sub-selected]}]
(close-fn)
(editor-handler/delete-asset-of-block!
{:block-id block-id
:local? local?
:delete-local? (and sub-selected (first sub-selected))
:repo (state/get-current-repo)
:href src
:title title
:full-text full-text}))})]
(util/stop e)
(state/set-modal! confirm-fn))))}
(ui/icon "trash")])
[:button.asset-action-btn
{:title (t :asset/copy)
:tabIndex "-1"
:on-mouse-down util/stop
:on-click (fn [e]
(util/stop e)
(-> (util/copy-image-to-clipboard image-src)
(p/then #(notification/show! "Copied!" :success))))}
(ui/icon "copy")]
[:button.asset-action-btn
{:title (t :asset/maximize)
:tabIndex "-1"
:on-mouse-down util/stop
:on-click open-lightbox}
(ui/icon "maximize")]]])])]))))
(rum/defc audio-cp [src]
;; Change protocol to allow media fragment uris to play
[:audio {:src (string/replace-first src gp-config/asset-protocol "file://")
:controls true
:on-touch-start #(util/stop %)}])
(rum/defcs asset-link < rum/reactive
(rum/local nil ::src)
[state config title href metadata full_text]
(let [src (::src state)
granted? (state/sub [:nfs/user-granted? (state/get-current-repo)])
href (config/get-local-asset-absolute-path href)]
(when (or granted? (util/electron?) (mobile-util/native-platform?))
(p/then (editor-handler/make-asset-url href) #(reset! src %)))
(when @src
;; NOTE(andelf): Under nfs context, src might be a bare blob:http://..../uuid URI without ext info
(let [ext (keyword (or (util/get-file-ext @src)
(util/get-file-ext href)))
repo (state/get-current-repo)
repo-dir (config/get-repo-dir repo)
path (str repo-dir href)
share-fn (fn [event]
(util/stop event)
(when (mobile-util/native-platform?)
;; File URL must be legal, so filename muse be URI-encoded
;; incoming href format: "/assets/whatever.ext"
(let [[rel-dir basename] (util/get-dir-and-basename href)
rel-dir (string/replace rel-dir #"^/+" "")
asset-url (path/path-join repo-dir rel-dir basename)]
(.share Share (clj->js {:url asset-url
:title "Open file with your favorite app"})))))]
(cond
(contains? config/audio-formats ext)
(asset-loader @src
#(audio-cp @src))
(contains? (gp-config/img-formats) ext)
(asset-loader @src
#(resizable-image config title @src metadata full_text true))
(contains? (gp-config/text-formats) ext)
[:a.asset-ref.is-plaintext {:href (rfe/href :file {:path path})
:on-click (fn [_event]
(p/let [result (fs/read-file repo-dir path)]
(db/set-file-content! repo path result)))}
title]
(= ext :pdf)
[:a.asset-ref.is-pdf {:href @src
:on-click share-fn}
title]
:else
[:a.asset-ref.is-doc {:href @src
:on-click share-fn}
title])))))
(defn ar-url->http-url
[href]
(string/replace href #"^ar://" (str (state/get-arweave-gateway) "/")))
;; TODO: safe encoding asciis
;; TODO: image link to another link
(defn image-link [config url href label metadata full_text]
(let [metadata (if (string/blank? metadata)
nil
(gp-util/safe-read-string metadata))
title (second (first label))]
(ui/catch-error
[:span.warning full_text]
(if (and (gp-config/local-asset? href)
(config/local-db? (state/get-current-repo)))
(asset-link config title href metadata full_text)
(let [href (cond
(util/starts-with? href "http")
href
(util/starts-with? href "ar")
(ar-url->http-url href)
config/publishing?
(subs href 1)
(= "Embed_data" (first url))
href
:else
(if (assets-handler/check-alias-path? href)
(assets-handler/normalize-asset-resource-url href)
(get-file-absolute-path config href)))]
(resizable-image config title href metadata full_text false))))))
(def timestamp-to-string export-common-handler/timestamp-to-string)
(defn timestamp [{:keys [active _date _time _repetition _wday] :as t} kind]
(let [prefix (case kind
"Scheduled"
[:i {:class "fa fa-calendar"
:style {:margin-right 3.5}}]
"Deadline"
[:i {:class "fa fa-calendar-times-o"
:style {:margin-right 3.5}}]
"Date"
nil
"Closed"
nil
"Started"
[:i {:class "fa fa-clock-o"
:style {:margin-right 3.5}}]
"Start"
"From: "
"Stop"
"To: "
nil)
class (when (= kind "Closed")
"line-through")]
[:span.timestamp (cond-> {:active (str active)}
class
(assoc :class class))
prefix (timestamp-to-string t)]))
(defn range [{:keys [start stop]} stopped?]
[:div {:class "timestamp-range"
:stopped stopped?}
(timestamp start "Start")
(timestamp stop "Stop")])
(declare map-inline)
(declare markup-element-cp)
(declare markup-elements-cp)
(declare page-reference)
(defn open-page-ref
[e page-name redirect-page-name page-name-in-block contents-page? whiteboard-page?]
(util/stop e)
(when (not (util/right-click? e))
(cond
(gobj/get e "shiftKey")
(when-let [page-entity (db/entity [:block/name redirect-page-name])]
(state/sidebar-add-block!
(state/get-current-repo)
(:db/id page-entity)
:page))
(and (util/meta-key? e) (whiteboard-handler/inside-portal? (.-target e)))
(whiteboard-handler/add-new-block-portal-shape!
page-name
(whiteboard-handler/closest-shape (.-target e)))
whiteboard-page?
(route-handler/redirect-to-whiteboard! page-name)
(not= redirect-page-name page-name)
(route-handler/redirect-to-page! redirect-page-name)
:else
(state/pub-event! [:page/create page-name-in-block])))
(when (and contents-page?
(util/mobile?)
(state/get-left-sidebar-open?))
(ui-handler/close-left-sidebar!)))
(rum/defcs page-inner <
(rum/local false ::mouse-down?)
"The inner div of page reference component
page-name-in-block is the overridable name of the page (legacy)
All page-names are sanitized except page-name-in-block"
[state config page-name-in-block page-name redirect-page-name page-entity contents-page? children html-export? label whiteboard-page?]
(let [*mouse-down? (::mouse-down? state)
tag? (:tag? config)
config (assoc config :whiteboard-page? whiteboard-page?)
untitled? (model/untitled-page? page-name)]
[:a
{:tabIndex "0"
:class (cond-> (if tag? "tag" "page-ref")
(:property? config)
(str " page-property-key block-property")
untitled? (str " opacity-50"))
:data-ref page-name
:draggable true
:on-drag-start (fn [e] (editor-handler/block->data-transfer! page-name-in-block e))
:on-mouse-down (fn [_e] (reset! *mouse-down? true))
:on-mouse-up (fn [e]
(when @*mouse-down?
(open-page-ref e page-name redirect-page-name page-name-in-block contents-page? whiteboard-page?)
(reset! *mouse-down? false)))
:on-key-up (fn [e] (when (and e (= (.-key e) "Enter"))
(open-page-ref e page-name redirect-page-name page-name-in-block contents-page? whiteboard-page?)))}
(if (and (coll? children) (seq children))
(for [child children]
(if (= (first child) "Label")
(last child)
(let [{:keys [content children]} (last child)
page-name (subs content 2 (- (count content) 2))]
(rum/with-key (page-reference html-export? page-name (assoc config :children children) nil) page-name))))
(cond
(and label
(string? label)
(not (string/blank? label))) ; alias
label
(coll? label)
(->elem :span (map-inline config label))
:else
(let [original-name (util/get-page-original-name page-entity)
s (cond untitled?
(t :untitled)
;; The page-name-in-block generated by the auto-complete is not page-name-sanitized
(pdf-utils/hls-file? page-name)
(pdf-utils/fix-local-asset-pagename page-name)
(not= (util/safe-page-name-sanity-lc original-name) page-name-in-block)
page-name-in-block ;; page-name-in-block might be overridden (legacy))
original-name
(util/trim-safe original-name)
:else
(util/trim-safe page-name))
_ (when-not page-entity (js/console.warn "page-inner's page-entity is nil, given page-name: " page-name
" page-name-in-block: " page-name-in-block))]
(if tag? (str "#" s) s))))]))
(rum/defc page-preview-trigger
[{:keys [children sidebar? tippy-position tippy-distance fixed-position? open? manual?] :as config} page-name]
(let [*tippy-ref (rum/create-ref)
page-name (util/page-name-sanity-lc page-name)
whiteboard-page? (model/whiteboard-page? page-name)
redirect-page-name (or (model/get-redirect-page-name page-name (:block/alias? config))
page-name)
page-original-name (model/get-page-original-name redirect-page-name)
_ #_:clj-kondo/ignore (rum/defc html-template []
(let [*el-popup (rum/use-ref nil)]
(rum/use-effect!
(fn []
(let [el-popup (rum/deref *el-popup)
cb (fn [^js e]
(when-not (:editor/editing? @state/state)
;; Esc
(and (= e.which 27)
(when-let [tp (rum/deref *tippy-ref)]
(.hideTooltip tp)))))]
(js/setTimeout #(.focus el-popup))
(.addEventListener el-popup "keyup" cb)
#(.removeEventListener el-popup "keyup" cb)))
[])
(when redirect-page-name
[:div.tippy-wrapper.overflow-y-auto.p-4.outline-none
{:ref *el-popup
:tab-index -1
:style {:width 600
:text-align "left"
:font-weight 500
:max-height 600
:padding-bottom 64}}
(if (and (string? page-original-name) (text/namespace-page? page-original-name))
[:div.my-2
(->>
(for [namespace-page (gp-util/split-namespace-pages page-original-name)]
(when (and (string? namespace-page) namespace-page)
(let [label (second (gp-util/split-last model/ns-char namespace-page))]
(page-reference false namespace-page {:preview? true} label))))
(interpose [:span.mx-2.opacity-30 model/ns-char]))]
[:h2.font-bold.text-lg (if (= page-name redirect-page-name)
page-original-name
[:span
[:span.text-sm.mr-2 "Alias:"]
page-original-name])])
(let [page (db/entity [:block/name (util/page-name-sanity-lc redirect-page-name)])]
(editor-handler/insert-first-page-block-if-not-exists! redirect-page-name {:redirect? false})
(let [page-blocks-cp (state/get-page-blocks-cp)
tldraw-preview (state/get-component :whiteboard/tldraw-preview)]
(if whiteboard-page?
(tldraw-preview page-name)
(page-blocks-cp (state/get-current-repo) page {:sidebar? sidebar? :preview? true}))))])))]
(if (or (not manual?) open?)
(ui/tippy {:ref *tippy-ref
:in-editor? true
:html html-template
:interactive true
:delay [1000, 100]
:fixed-position? fixed-position?
:position (or tippy-position "top")
:distance (or tippy-distance 10)
:popperOptions {:modifiers {:preventOverflow
{:enabled true
:boundariesElement "viewport"}}}}
children)
children)))
(rum/defc page-cp
"Component for a page. `page` argument contains :block/name which can be (un)sanitized page name.
Keys for `config`:
- `:preview?`: Is this component under preview mode? (If true, `page-preview-trigger` won't be registered to this `page-cp`)"
[{:keys [html-export? redirect-page-name label children contents-page? preview?] :as config} page]
(when-let [page-name-in-block (:block/name page)]
(let [page-name-in-block (gp-util/remove-boundary-slashes page-name-in-block)
page-name (util/page-name-sanity-lc page-name-in-block)
page-entity (db/entity [:block/name page-name])
whiteboard-page? (model/whiteboard-page? page-name)
redirect-page-name (or (and (= :org (state/get-preferred-format))
(:org-mode/insert-file-link? (state/get-config))
redirect-page-name)
(model/get-redirect-page-name page-name (:block/alias? config)))
inner (page-inner config
page-name-in-block
page-name
redirect-page-name page-entity contents-page? children html-export? label whiteboard-page?)]
(cond
(:breadcrumb? config)
(or (:block/original-name page)
(:block/name page))
(and (not (util/mobile?))
(not preview?))
(page-preview-trigger (assoc config :children inner) page-name)
:else
inner))))
(rum/defc asset-reference
[config title path]
(let [repo (state/get-current-repo)
real-path-url (cond
(gp-util/url? path)
path
(path/absolute? path)
path
:else
(assets-handler/resolve-asset-real-path-url repo path))
ext-name (util/get-file-ext path)
title-or-path (cond
(string? title)
title
(seq title)
(->elem :span (map-inline config title))
:else
path)]
[:div.asset-ref-wrap
{:data-ext ext-name}
(cond
;; https://en.wikipedia.org/wiki/HTML5_video
(contains? config/video-formats (keyword ext-name))
[:video {:src real-path-url
:controls true}]
:else
[:a.asset-ref {:target "_blank" :href real-path-url}
title-or-path])]))
(defonce excalidraw-loaded? (atom false))
(rum/defc excalidraw < rum/reactive
{:init (fn [state]
(p/let [_ (loader/load :excalidraw)]
(reset! excalidraw-loaded? true))
state)}
[file block-uuid]
(let [loaded? (rum/react excalidraw-loaded?)
draw-component (when loaded?
(resolve 'frontend.extensions.excalidraw/draw))]
(when draw-component
(draw-component {:file file :block-uuid block-uuid}))))
(rum/defc page-reference < rum/reactive
"Component for page reference"
[html-export? s {:keys [nested-link? id] :as config} label]
(let [show-brackets? (state/show-brackets?)
block-uuid (:block/uuid config)
contents-page? (= "contents" (string/lower-case (str id)))]
(if (string/ends-with? s ".excalidraw")
[:div.draw {:on-click (fn [e]
(.stopPropagation e))}
(excalidraw s block-uuid)]
[:span.page-reference
{:data-ref s}
(when (and (or show-brackets? nested-link?)
(not html-export?)
(not contents-page?))
[:span.text-gray-500.bracket page-ref/left-brackets])
(let [s (string/trim s)]
(page-cp (assoc config
:label (mldoc/plain->text label)
:contents-page? contents-page?)
{:block/name s}))
(when (and (or show-brackets? nested-link?)
(not html-export?)
(not contents-page?))
[:span.text-gray-500.bracket page-ref/right-brackets])])))
(defn- latex-environment-content
[name option content]
(if (= (string/lower-case name) "equation")
content
(util/format "\\begin%s\n%s\\end{%s}"
(str "{" name "}" option)
content
name)))
(declare blocks-container)
(defn- edit-parent-block [e config]
(when-not (state/editing?)
(.stopPropagation e)
(editor-handler/edit-block! config :max (:block/uuid config))))
(rum/defc block-embed < rum/reactive db-mixins/query
[config uuid]
(when-let [block (db/entity [:block/uuid uuid])]
(let [blocks (db/get-paginated-blocks (state/get-current-repo) (:db/id block)
{:scoped-block-id (:db/id block)})]
[:div.color-level.embed-block.bg-base-2
{:style {:z-index 2}
:on-double-click #(edit-parent-block % config)
:on-mouse-down (fn [e] (.stopPropagation e))}
[:div.px-3.pt-1.pb-2
(blocks-container blocks (assoc config
:db/id (:db/id block)
:id (str uuid)
:embed-id uuid
:embed? true
:embed-parent (:block config)
:ref? false))]])))
(rum/defc page-embed < rum/reactive db-mixins/query
[config page-name]
(let [page-name (util/page-name-sanity-lc (string/trim page-name))
current-page (state/get-current-page)
whiteboard-page? (model/whiteboard-page? page-name)]
[:div.color-level.embed.embed-page.bg-base-2
{:class (when (:sidebar? config) "in-sidebar")
:on-double-click #(edit-parent-block % config)
:on-mouse-down #(.stopPropagation %)}
[:section.flex.items-center.p-1.embed-header
[:div.mr-3 svg/page]
(page-cp config {:block/name page-name})]
(when (and
(not= (util/page-name-sanity-lc (or current-page ""))
page-name)
(not= (util/page-name-sanity-lc (get config :id ""))
page-name))
(if whiteboard-page?
((state/get-component :whiteboard/tldraw-preview) page-name)
(let [page (model/get-page page-name)
blocks (db/get-paginated-blocks (state/get-current-repo) (:db/id page))]
(blocks-container blocks (assoc config
:db/id (:db/id page)
:id page-name
:embed? true
:page-embed? true
:ref? false)))))]))
(defn- get-label-text
[label]
(when (and (= 1 (count label))
(string? (last (first label))))
(gp-util/safe-decode-uri-component (last (first label)))))
(defn- get-page
[label]
(when-let [label-text (get-label-text label)]
(db/entity [:block/name (util/page-name-sanity-lc label-text)])))
(defn- macro->text
[name arguments]
(if (and (seq arguments)
(not= arguments ["null"]))
(util/format "{{%s %s}}" name (string/join ", " arguments))
(util/format "{{%s}}" name)))
(declare block-content)
(declare block-container)
(declare breadcrumb)
(rum/defc block-reference < rum/reactive
db-mixins/query
[config id label]
(if-let [block-id (parse-uuid id)]
(let [db-id (:db/id (db/pull [:block/uuid block-id]))
block (when db-id (db/sub-block db-id))
block-type (keyword (get-in block [:block/properties :ls-type]))
hl-type (get-in block [:block/properties :hl-type])
repo (state/get-current-repo)
stop-inner-events? (= block-type :whiteboard-shape)]
(if (and block (:block/content block))
(let [title [:span.block-ref
(block-content (assoc config :block-ref? true :stop-events? stop-inner-events?)
block nil (:block/uuid block)
(:slide? config))]
inner (if label
(->elem
:span.block-ref
(map-inline config label))
title)]
[:div.block-ref-wrap.inline
{:data-type (name (or block-type :default))
:data-hl-type hl-type
:on-mouse-down
(fn [^js/MouseEvent e]
(if (util/right-click? e)
(state/set-state! :block-ref/context {:block (:block config)
:block-ref block-id})
(when (and
(or (gobj/get e "shiftKey")
(not (.. e -target (closest ".blank"))))
(not (util/right-click? e)))
(util/stop e)
(cond
(gobj/get e "shiftKey")
(state/sidebar-add-block!
(state/get-current-repo)
(:db/id block)
:block-ref)
(and (util/meta-key? e) (whiteboard-handler/inside-portal? (.-target e)))
(whiteboard-handler/add-new-block-portal-shape!
(:block/uuid block)
(whiteboard-handler/closest-shape (.-target e)))
:else
(match [block-type (util/electron?)]
;; pdf annotation
[:annotation true] (pdf-assets/open-block-ref! block)
[:whiteboard-shape true] (route-handler/redirect-to-whiteboard!
(get-in block [:block/page :block/name]) {:block-id block-id})
;; default open block page
:else (route-handler/redirect-to-page! id))))))}
(if (and (not (util/mobile?)) (not (:preview? config)) (nil? block-type))
(ui/tippy {:html (fn []
[:div.tippy-wrapper.overflow-y-auto.p-4
{:style {:width 735
:text-align "left"
:max-height 600}}
[(breadcrumb config repo block-id {:indent? true})
(blocks-container
(db/get-block-and-children repo block-id)
(assoc config :id (str id) :preview? true))]])
:interactive true
:in-editor? true
:delay [1000, 100]} inner)
inner)])
[:span.warning.mr-1 {:title "Block ref invalid"}
(block-ref/->block-ref id)]))
[:span.warning.mr-1 {:title "Block ref invalid"}
(block-ref/->block-ref id)]))
(defn inline-text
([format v]
(inline-text {} format v))
([config format v]
(when (string? v)
(let [inline-list (gp-mldoc/inline->edn v (gp-mldoc/default-config format))]
[:div.inline.mr-1 (map-inline config inline-list)]))))
(defn- render-macro
[config name arguments macro-content format]
[:div.macro {:data-macro-name name}
(if macro-content
(let [ast (->> (mldoc/->edn macro-content (gp-mldoc/default-config format))
(map first))
paragraph? (and (= 1 (count ast))
(= "Paragraph" (ffirst ast)))]
(if (and (not paragraph?)
(mldoc/block-with-title? (ffirst ast)))
(markup-elements-cp (assoc config :block/format format) ast)
(inline-text format macro-content)))
[:span.warning {:title (str "Unsupported macro name: " name)}
(macro->text name arguments)])])
(rum/defc nested-link < rum/reactive
[config html-export? link]
(let [show-brackets? (state/show-brackets?)
{:keys [content children]} link]
[:span.page-reference.nested
(when (and show-brackets?
(not html-export?)
(not (= (:id config) "contents")))
[:span.text-gray-500 page-ref/left-brackets])
(let [page-name (subs content 2 (- (count content) 2))]
(page-cp (assoc config
:children children
:nested-link? true) {:block/name page-name}))
(when (and show-brackets?
(not html-export?)
(not (= (:id config) "contents")))
[:span.text-gray-500 page-ref/right-brackets])]))
(defn- show-link?
[config metadata s full-text]
(let [media-formats (set (map name config/media-formats))
metadata-show (:show (gp-util/safe-read-string metadata))
format (get-in config [:block :block/format])]
(or
(and
(= :org format)
(or
(and
(nil? metadata-show)
(or
(gp-config/local-asset? s)
(text-util/media-link? media-formats s)))
(true? (boolean metadata-show))))
;; markdown
(string/starts-with? (string/triml full-text) "!")
;; image http link
(and (or (string/starts-with? full-text "http://")
(string/starts-with? full-text "https://"))
(text-util/media-link? media-formats s)))))