-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
editor.cljs
3097 lines (2802 loc) · 120 KB
/
editor.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.handler.editor
(:require [cljs.core.match :refer [match]]
[clojure.set :as set]
[clojure.string :as string]
[clojure.walk :as w]
[clojure.zip :as zip]
[dommy.core :as dom]
[frontend.commands :as commands
:refer [*angle-bracket-caret-pos *show-block-commands *show-commands *slash-caret-pos]]
[frontend.config :as config]
[frontend.date :as date]
[frontend.db :as db]
[frontend.db-schema :as db-schema]
[frontend.db.model :as db-model]
[frontend.db.utils :as db-utils]
[frontend.diff :as diff]
[frontend.extensions.html-parser :as html-parser]
[frontend.format.block :as block]
[frontend.format.mldoc :as mldoc]
[frontend.fs :as fs]
[frontend.handler.block :as block-handler]
[frontend.handler.common :as common-handler]
[frontend.handler.image :as image-handler]
[frontend.handler.notification :as notification]
[frontend.handler.repeated :as repeated]
[frontend.handler.repo :as repo-handler]
[frontend.handler.route :as route-handler]
[frontend.handler.ui :as ui-handler]
[frontend.image :as image]
[frontend.modules.outliner.core :as outliner-core]
[frontend.modules.outliner.tree :as tree]
[frontend.search :as search]
[frontend.state :as state]
[frontend.template :as template]
[frontend.text :as text]
[frontend.utf8 :as utf8]
[frontend.util :as util :refer [profile]]
[frontend.util.cursor :as cursor]
[frontend.util.marker :as marker]
[frontend.util.property :as property]
[frontend.util.thingatpt :as thingatpt]
[goog.dom :as gdom]
[goog.dom.classes :as gdom-classes]
[goog.object :as gobj]
[lambdaisland.glogi :as log]
[medley.core :as medley]
[promesa.core :as p]))
;; FIXME: should support multiple images concurrently uploading
(defonce *asset-pending-file (atom nil))
(defonce *asset-uploading? (atom false))
(defonce *asset-uploading-process (atom 0))
(defonce *selected-text (atom nil))
(defn- get-selection-and-format
[]
(when-let [block (state/get-edit-block)]
(when-let [id (:block/uuid block)]
(when-let [edit-id (state/get-edit-input-id)]
(when-let [input (gdom/getElement edit-id)]
{:selection-start (gobj/get input "selectionStart")
:selection-end (gobj/get input "selectionEnd")
:format (:block/format block)
:value (gobj/get input "value")
:block block
:edit-id edit-id
:input input})))))
(defn- format-text!
[pattern-fn]
(when-let [m (get-selection-and-format)]
(let [{:keys [selection-start selection-end format value block edit-id input]} m
empty-selection? (= selection-start selection-end)
pattern (pattern-fn format)
pattern-count (count pattern)
pattern-prefix (subs value (max 0 (- selection-start pattern-count)) selection-start)
pattern-suffix (subs value selection-end (min (count value) (+ selection-end pattern-count)))
already-wrapped? (= pattern pattern-prefix pattern-suffix)
prefix (if already-wrapped?
(subs value 0 (- selection-start pattern-count))
(subs value 0 selection-start))
postfix (if already-wrapped?
(subs value (+ selection-end pattern-count))
(subs value selection-end))
inner-value (cond-> (subs value selection-start selection-end)
(not already-wrapped?) (#(str pattern % pattern)))
new-value (str prefix inner-value postfix)]
(state/set-edit-content! edit-id new-value)
(cond
already-wrapped? (cursor/set-selection-to input (- selection-start pattern-count) (- selection-end pattern-count))
empty-selection? (cursor/move-cursor-to input (+ selection-end pattern-count))
:else (cursor/set-selection-to input (+ selection-start pattern-count) (+ selection-end pattern-count))))))
(defn bold-format! []
(format-text! config/get-bold))
(defn italics-format! []
(format-text! config/get-italic))
(defn highlight-format! []
(format-text! config/get-highlight))
(defn strike-through-format! []
(format-text! config/get-strike-through))
(defn html-link-format! []
(when-let [m (get-selection-and-format)]
(let [{:keys [selection-start selection-end format value block edit-id input]} m
cur-pos (cursor/pos input)
empty-selection? (= selection-start selection-end)
selection (subs value selection-start selection-end)
selection-link? (and selection (or (util/starts-with? selection "http://")
(util/starts-with? selection "https://")))
[content forward-pos] (cond
empty-selection?
(config/get-empty-link-and-forward-pos format)
selection-link?
(config/with-default-link format selection)
:else
(config/with-default-label format selection))
new-value (str
(subs value 0 selection-start)
content
(subs value selection-end))
cur-pos (or selection-start cur-pos)]
(state/set-edit-content! edit-id new-value)
(cursor/move-cursor-to input (+ cur-pos forward-pos)))))
(defn open-block-in-sidebar!
[block-id]
(when block-id
(when-let [block (db/pull [:block/uuid block-id])]
(state/sidebar-add-block!
(state/get-current-repo)
(:db/id block)
:block
block))))
(defn reset-cursor-range!
[node]
(when node
(state/set-cursor-range! (util/caret-range node))))
(defn restore-cursor-pos!
[id markup]
(when-let [node (gdom/getElement (str id))]
(when-let [cursor-range (state/get-cursor-range)]
(when-let [range cursor-range]
(let [pos (diff/find-position markup range)]
(cursor/move-cursor-to node pos))))))
(defn highlight-block!
[block-uuid]
(let [blocks (array-seq (js/document.getElementsByClassName (str block-uuid)))]
(doseq [block blocks]
(dom/add-class! block "block-highlight"))))
(defn unhighlight-blocks!
[]
(let [blocks (some->> (array-seq (js/document.getElementsByClassName "block-highlight"))
(repeat 2)
(apply concat))]
(doseq [block blocks]
(gdom-classes/remove block "block-highlight"))))
;; FIXME: children' :block/path-ref-pages
(defn compute-retract-refs
"Computes old references to be retracted."
[eid {:block/keys [refs]} old-refs]
;; TODO:
)
(defn- get-edit-input-id-with-block-id
[block-id]
(when-let [first-block (util/get-first-block-by-id block-id)]
(string/replace (gobj/get first-block "id")
"ls-block"
"edit-block")))
(defn clear-selection!
[_e]
(util/select-unhighlight! (dom/by-class "selected"))
(state/clear-selection!))
(defn- text-range-by-lst-fst-line [content [direction pos]]
(case direction
:up
(let [last-new-line (or (string/last-index-of content \newline) -1)
end (+ last-new-line pos 1)]
(subs content 0 end))
:down
(-> (string/split-lines content)
first
(or "")
(subs 0 pos))))
;; id: block dom id, "ls-block-counter-uuid"
(defn edit-block!
([block pos format id]
(edit-block! block pos format id nil))
([block pos format id {:keys [custom-content tail-len move-cursor?]
:or {tail-len 0
move-cursor? true}}]
(when-not config/publishing?
(when-let [block-id (:block/uuid block)]
(let [block (or (db/pull [:block/uuid block-id]) block)
edit-input-id (if (uuid? id)
(get-edit-input-id-with-block-id id)
(str (subs id 0 (- (count id) 36)) block-id))
content (or custom-content (:block/content block) "")
content-length (count content)
text-range (cond
(vector? pos)
(text-range-by-lst-fst-line content pos)
(and (> tail-len 0) (>= (count content) tail-len))
(subs content 0 (- (count content) tail-len))
(or (= :max pos) (<= content-length pos))
content
:else
(subs content 0 pos))
content (property/remove-built-in-properties (:block/format block)
content)]
(clear-selection! nil)
(state/set-editing! edit-input-id content block text-range move-cursor?))))))
(defn edit-last-block-for-new-page!
[last-block pos]
(when-let [first-block (util/get-first-block-by-id (:block/uuid last-block))]
(edit-block!
last-block
pos
(:block/format last-block)
(string/replace (gobj/get first-block "id")
"ls-block"
"edit-block"))))
(defn- another-block-with-same-id-exists?
[current-id block-id]
(and (string? block-id)
(util/uuid-string? block-id)
(not= current-id (cljs.core/uuid block-id))
(db/entity [:block/uuid (cljs.core/uuid block-id)])))
(defn- attach-page-properties-if-exists!
[block]
(if (and (:block/pre-block? block)
(seq (:block/properties block)))
(let [page-properties (:block/properties block)
str->page (fn [n] (block/page-name->map n true))
refs (->> page-properties
(filter (fn [[_ v]] (coll? v)))
(vals)
(apply concat)
(set)
(map str->page)
(concat (:block/refs block))
(util/distinct-by :block/name))
{:keys [tags alias]} page-properties
page-tx (let [id (:db/id (:block/page block))
retract-attributes (mapv (fn [attribute]
[:db/retract id attribute])
[:block/properties :block/tags :block/alias])
tx (cond-> {:db/id id
:block/properties page-properties}
(seq tags)
(assoc :block/tags (map str->page tags))
(seq alias)
(assoc :block/alias (map str->page alias)))]
(conj retract-attributes tx))]
(assoc block
:block/refs refs
:db/other-tx page-tx))
block))
(defn- remove-non-existed-refs!
[refs]
(remove (fn [x] (and (vector? x)
(= :block/uuid (first x))
(nil? (db/entity x)))) refs))
(defn wrap-parse-block
[{:block/keys [content format parent left page uuid pre-block? level] :as block}]
(let [block (or (and (:db/id block) (db/pull (:db/id block))) block)
properties (:block/properties block)
real-content (:block/content block)
content (if (and (seq properties) real-content (not= real-content content))
(property/with-built-in-properties properties content format)
content)
first-block? (= left page)
ast (mldoc/->edn (string/trim content) (mldoc/default-config format))
first-elem-type (first (ffirst ast))
first-elem-meta (second (ffirst ast))
properties? (contains? #{"Property_Drawer" "Properties"} first-elem-type)
markdown-heading? (and (= format :markdown)
(= "Heading" first-elem-type)
(nil? (:size first-elem-meta)))
block-with-title? (mldoc/block-with-title? first-elem-type)
content (string/triml content)
content (string/replace content (util/format "((%s))" (str uuid)) "")
[content content'] (cond
(and first-block? properties?)
[content content]
markdown-heading?
[content content]
:else
(let [content' (str (config/get-block-pattern format) (if block-with-title? " " "\n") content)]
[content content']))
block (assoc block :block/content content')
block (apply dissoc block (remove #{:block/pre-block?} db-schema/retract-attributes))
block (block/parse-block block)
block (if (and first-block? (:block/pre-block? block))
block
(dissoc block :block/pre-block?))
block (update block :block/refs remove-non-existed-refs!)
block (attach-page-properties-if-exists! block)
new-properties (merge
(select-keys properties property/built-in-properties)
(:block/properties block))]
(-> block
(dissoc :block/top?
:block/block-refs-count)
(assoc :block/content content
:block/properties new-properties)
(merge (if level {:block/level level} {})))))
(defn- save-block-inner!
[repo block value {:keys [refresh?]
:or {refresh? true}}]
(let [block (assoc block :block/content value)
block (apply dissoc block db-schema/retract-attributes)]
(profile
"Save block: "
(let [block (wrap-parse-block block)]
(-> (outliner-core/block block)
(outliner-core/save-node))
(when refresh?
(let [opts {:key :block/change
:data [block]}]
(db/refresh! repo opts)))))
(repo-handler/push-if-auto-enabled! repo)))
(defn save-block-if-changed!
([block value]
(save-block-if-changed! block value nil))
([block value
{:keys [force?]
:as opts}]
(let [{:block/keys [uuid file page format repo content properties]} block
repo (or repo (state/get-current-repo))
e (db/entity repo [:block/uuid uuid])
format (or format (state/get-preferred-format))
page (db/entity repo (:db/id page))
block-id (when (map? properties) (get properties :id))
content (property/remove-built-in-properties format content)]
(cond
(another-block-with-same-id-exists? uuid block-id)
(notification/show!
[:p.content
(util/format "Block with the id % already exists!" block-id)]
:error)
force?
(save-block-inner! repo block value opts)
:else
(let [content-changed? (not= (string/trim content) (string/trim value))]
(when (and content-changed? page)
(save-block-inner! repo block value opts)))))))
(defn- compute-fst-snd-block-text
[value pos]
(let [fst-block-text (subs value 0 pos)
snd-block-text (string/triml (subs value pos))]
[fst-block-text snd-block-text]))
(defn outliner-insert-block!
[config current-block new-block sibling?]
(let [ref-top-block? (and (:ref? config)
(not (:ref-child? config)))
skip-save-current-block? (:skip-save-current-block? config)
[current-node new-node]
(mapv outliner-core/block [current-block new-block])
has-children? (db/has-children? (state/get-current-repo)
(tree/-get-id current-node))
sibling? (cond
ref-top-block?
false
(boolean? sibling?)
sibling?
(:collapsed (:block/properties current-block))
true
:else
(not has-children?))]
(let [*blocks (atom [current-node])]
(when-not skip-save-current-block?
(outliner-core/save-node current-node))
(outliner-core/insert-node new-node current-node sibling? {:blocks-atom *blocks
:skip-transact? false})
{:blocks @*blocks
:sibling? sibling?})))
(defn- block-self-alone-when-insert?
[config uuid]
(let [current-page (state/get-current-page)
block-id (or
(and (:id config)
(util/uuid-string? (:id config))
(:id config))
(and current-page
(util/uuid-string? current-page)
current-page))]
(= uuid (and block-id (medley/uuid block-id)))))
;; FIXME: painful
(defn update-cache-for-block-insert!
"Currently, this only affects current editor container to improve the performance."
[repo config {:block/keys [page uuid] :as block} blocks]
(let [blocks (map :data blocks)
[first-block last-block right-block] blocks
child? (= (first (:block/parent last-block))
(:block/uuid first-block))
blocks-container-id (when-let [id (:id config)]
(and (util/uuid-string? id) (medley/uuid id)))]
(let [new-last-block (let [first-block-id {:db/id (:db/id first-block)}]
(assoc last-block
:block/left first-block-id
:block/parent (if child?
first-block-id
;; sibling
(:block/parent first-block))))
blocks [first-block new-last-block]
blocks-atom (if blocks-container-id
(db/get-block-blocks-cache-atom repo blocks-container-id)
(db/get-page-blocks-cache-atom repo (:db/id page)))
[before-part after-part] (and blocks-atom
(split-with
#(not= uuid (:block/uuid %))
@blocks-atom))
after-part (rest after-part)
blocks (concat before-part blocks after-part)
blocks (if right-block
(map (fn [block]
(if (= (:block/uuid right-block) (:block/uuid block))
(assoc block :block/left (:block/left right-block))
block)) blocks)
blocks)]
(when blocks-atom
(reset! blocks-atom blocks)))))
(defn insert-new-block-before-block-aux!
[config
{:block/keys [uuid content repo format page]
db-id :db/id
:as block}
value
{:keys [ok-handler]
:as opts}]
(let [input (gdom/getElement (state/get-edit-input-id))
pos (cursor/pos input)
repo (or repo (state/get-current-repo))
[fst-block-text snd-block-text] (compute-fst-snd-block-text value pos)
current-block (assoc block :block/content snd-block-text)
current-block (apply dissoc current-block db-schema/retract-attributes)
current-block (wrap-parse-block current-block)
new-m {:block/uuid (db/new-block-id)
:block/content fst-block-text}
prev-block (-> (merge (select-keys block [:block/parent :block/left :block/format
:block/page :block/file :block/journal?]) new-m)
(wrap-parse-block))
left-block (db/pull (:db/id (:block/left block)))
_ (outliner-core/save-node (outliner-core/block current-block))
sibling? (not= (:db/id left-block) (:db/id (:block/parent block)))
{:keys [sibling? blocks]} (profile
"outliner insert block"
(outliner-insert-block! config left-block prev-block sibling?))]
(db/refresh! repo {:key :block/insert :data [prev-block left-block current-block]})
(profile "ok handler" (ok-handler prev-block))))
(defn insert-new-block-aux!
[config
{:block/keys [uuid content repo format page]
db-id :db/id
:as block}
value
{:keys [ok-handler]
:as opts}]
(let [block-self? (block-self-alone-when-insert? config uuid)
input (gdom/getElement (state/get-edit-input-id))
pos (cursor/pos input)
repo (or repo (state/get-current-repo))
[fst-block-text snd-block-text] (compute-fst-snd-block-text value pos)
current-block (assoc block :block/content fst-block-text)
current-block (apply dissoc current-block db-schema/retract-attributes)
current-block (wrap-parse-block current-block)
zooming? (when-let [id (:id config)]
(and (string? id) (util/uuid-string? id)))
new-m {:block/uuid (db/new-block-id)
:block/content snd-block-text}
next-block (-> (merge (select-keys block [:block/parent :block/left :block/format
:block/page :block/file :block/journal?]) new-m)
(wrap-parse-block))
sibling? (when block-self? false)
{:keys [sibling? blocks]} (profile
"outliner insert block"
(outliner-insert-block! config current-block next-block sibling?))
refresh-fn (fn []
(let [opts {:key :block/insert
:data [current-block next-block]}]
(db/refresh! repo opts)))]
(do
(if (or (:ref? config)
(not sibling?)
zooming?)
(refresh-fn)
(do
(profile "update cache " (update-cache-for-block-insert! repo config block blocks))
(state/add-tx! refresh-fn)))
;; WORKAROUND: The block won't refresh itself even if the content is empty.
(when block-self?
(gobj/set input "value" ""))
(profile "ok handler" (ok-handler next-block)))))
(defn clear-when-saved!
[]
(state/set-editor-show-input! nil)
(state/set-editor-show-date-picker! false)
(state/set-editor-show-page-search! false)
(state/set-editor-show-block-search! false)
(state/set-editor-show-template-search! false)
(commands/restore-state true))
(defn get-state
[]
(let [[{:keys [on-hide block block-id block-parent-id format sidebar?]} id config] (state/get-editor-args)
node (gdom/getElement id)]
(when node
(let [value (gobj/get node "value")
pos (gobj/get node "selectionStart")]
{:config config
:on-hide on-hide
:sidebar? sidebar?
:format format
:id id
:block (or (db/pull [:block/uuid (:block/uuid block)]) block)
:block-id block-id
:block-parent-id block-parent-id
:node node
:value value
:pos pos}))))
(defn- with-timetracking-properties
[block value]
(let [new-marker (first (util/safe-re-find marker/bare-marker-pattern (or value "")))
new-marker (if new-marker (string/lower-case (string/trim new-marker)))
new-marker? (and
new-marker
(not= new-marker (string/lower-case (or (:block/marker block) "")))
(state/enable-timetracking?))
ts (util/time-ms)
properties (if new-marker?
(assoc (:block/properties block) new-marker ts)
(:block/properties block))
value (or
(when new-marker?
(property/insert-property (:block/format block)
value
new-marker
ts))
value)]
[properties value]))
(defn insert-new-block!
([state]
(insert-new-block! state nil))
([state block-value]
(when (and (not config/publishing?)
(not= :insert (state/get-editor-op)))
(state/set-editor-op! :insert)
(when-let [state (get-state)]
(let [{:keys [block value format id config]} state
value (if (string? block-value) block-value value)
block-id (:block/uuid block)
block (or (db/pull [:block/uuid block-id])
block)
repo (or (:block/repo block) (state/get-current-repo))
[properties value] (with-timetracking-properties block value)
block-self? (block-self-alone-when-insert? config block-id)
input (gdom/getElement (state/get-edit-input-id))
pos (cursor/pos input)
repo (or repo (state/get-current-repo))
[fst-block-text snd-block-text] (compute-fst-snd-block-text value pos)
insert-fn (match (mapv boolean [block-self? (seq fst-block-text) (seq snd-block-text)])
[true _ _] insert-new-block-aux!
[_ false true] insert-new-block-before-block-aux!
[_ _ _] insert-new-block-aux!)]
(insert-fn
config
(assoc block :block/properties properties)
value
{:ok-handler
(fn [last-block]
(edit-block! last-block 0 format id)
(clear-when-saved!))}))))
(state/set-editor-op! nil)))
(defn api-insert-new-block!
[content {:keys [page block-uuid sibling? before? properties]
:or {sibling? false
before? false}}]
(when (or page block-uuid)
(let [before? (if page false before?)
sibling? (if before? true (if page false sibling?))
block (if page
(db/entity [:block/name (string/lower-case page)])
(db/entity [:block/uuid block-uuid]))]
(when block
(let [repo (state/get-current-repo)
last-block (when (not sibling?)
(let [children (:block/_parent block)
blocks (db/sort-by-left children block)
last-block-id (:db/id (last blocks))]
(when last-block-id
(db/pull last-block-id))))
format (or
(:block/format block)
(db/get-page-format (:db/id block))
:markdown)
content (if (seq properties)
(property/insert-properties format content properties)
content)
new-block (-> (select-keys block [:block/page :block/file :block/journal?
:block/journal-day])
(assoc :block/content content
:block/format format)
(wrap-parse-block)
(assoc :block/uuid (db/new-block-id)))
new-block (if (:block/page new-block)
(assoc new-block :block/page (:db/id (:block/page new-block)))
(assoc new-block :block/page (:db/id block)))
new-block (if-let [db-id (:db/id (:block/file block))]
(assoc new-block :block/file db-id)
new-block)]
(let [[block-m sibling?] (cond
before?
(let [block (db/pull (:db/id (:block/left block)))
sibling? (if (:block/name block) false sibling?)]
[block sibling?])
sibling?
[(db/pull (:db/id block)) sibling?]
last-block
[last-block true]
block
[(db/pull (:db/id block)) sibling?]
;; FIXME: assert
:else
nil)]
(when block-m
(outliner-insert-block! {:skip-save-current-block? true} block-m new-block sibling?)
(ui-handler/re-render-root!)
new-block)))))))
(defn insert-first-page-block-if-not-exists!
[page-name]
(when (string? page-name)
(when-let [page (db/entity [:block/name (string/lower-case page-name)])]
(when (db/page-empty? (state/get-current-repo) (:db/id page))
(api-insert-new-block! "" {:page page-name})))))
(defn default-properties-block
[title format page]
(let [properties (common-handler/get-page-default-properties title)
content (property/build-properties-str format properties)]
{:block/pre-block? true
:block/uuid (db/new-block-id)
:block/properties properties
:block/left page
:block/format format
:block/content content
:block/parent page
:block/unordered true
:block/page page}))
(defn add-default-title-property-if-needed!
[page-name]
(when (string? page-name)
(when-let [page (db/entity [:block/name (string/lower-case page-name)])]
(when (db/page-empty? (state/get-current-repo) (:db/id page))
(let [title (or (:block/original-name page)
(:block/name page))
format (get page :block/format :markdown)
create-title-property? (util/create-title-property? title)]
(when create-title-property?
(let [default-properties (default-properties-block title format (:db/id page))
new-empty-block (-> (dissoc default-properties :block/pre-block? :block/uuid :block/left :block/properties)
(assoc :block/uuid (db/new-block-id)
:block/content ""
:block/left [:block/uuid (:block/uuid default-properties)]))]
(db/transact! [default-properties new-empty-block])
true)))))))
(defn update-timestamps-content!
[{:block/keys [repeated? marker format] :as block} content]
(if repeated?
(let [scheduled-ast (block-handler/get-scheduled-ast block)
deadline-ast (block-handler/get-deadline-ast block)
content (some->> (filter repeated/repeated? [scheduled-ast deadline-ast])
(map (fn [ts]
[(repeated/timestamp->text ts)
(repeated/next-timestamp-text ts)]))
(reduce (fn [content [old new]]
(string/replace content old new))
content))]
(when content
(str (string/trimr content)
"\n"
(util/format (str "*" " %s -> DONE [%s]")
marker
(date/get-local-date-time-string)))))
content))
(defn- with-marker-time
[content format marker]
(if (state/enable-timetracking?)
(let [marker (string/lower-case marker)]
(property/insert-property format content marker (util/time-ms)))
content))
(defn check
[{:block/keys [uuid marker content format repeated?] :as block}]
(let [new-content (string/replace-first content marker "DONE")
new-content (->
(if repeated?
(update-timestamps-content! block content)
new-content)
(with-marker-time format "DONE"))]
(save-block-if-changed! block new-content)))
(defn uncheck
[{:block/keys [uuid marker content format] :as block}]
(let [marker (if (= :now (state/get-preferred-workflow))
"LATER"
"TODO")
new-content (-> (string/replace-first content "DONE" marker)
(with-marker-time format marker))]
(save-block-if-changed! block new-content)))
(defn cycle-todo!
[]
(when-let [block (state/get-edit-block)]
(let [edit-input-id (state/get-edit-input-id)
current-input (gdom/getElement edit-input-id)
content (state/get-edit-content)
format (or (db/get-page-format (state/get-current-page))
(state/get-preferred-format))
[new-content marker] (marker/cycle-marker content format (state/get-preferred-workflow))
new-content (string/triml new-content)]
(let [new-pos (commands/compute-pos-delta-when-change-marker
current-input content new-content marker (cursor/pos current-input))]
(state/set-edit-content! edit-input-id new-content)
(cursor/move-cursor-to current-input new-pos)))))
(defn set-marker
[{:block/keys [uuid marker content format properties] :as block} new-marker]
(let [new-content (-> (string/replace-first content (re-pattern (str "^" marker)) new-marker)
(with-marker-time format new-marker))]
(save-block-if-changed! block new-content)))
(defn set-priority
[{:block/keys [uuid marker priority content] :as block} new-priority]
(let [new-content (string/replace-first content
(util/format "[#%s]" priority)
(util/format "[#%s]" new-priority))]
(save-block-if-changed! block new-content)))
(defn delete-block-aux!
[{:block/keys [uuid content repo refs] :as block} children?]
(let [repo (or repo (state/get-current-repo))
block (db/pull repo '[*] [:block/uuid uuid])]
(when block
(->
(outliner-core/block block)
(outliner-core/delete-node children?))
(db/refresh! repo {:key :block/change :data [block]}))))
(defn delete-block!
([repo e]
(delete-block! repo e true))
([repo e delete-children?]
(state/set-editor-op! :delete)
(let [{:keys [id block-id block-parent-id value format]} (get-state)]
(when block-id
(let [page-id (:db/id (:block/page (db/entity [:block/uuid block-id])))
page-blocks-count (and page-id (db/get-page-blocks-count repo page-id))]
(when (> page-blocks-count 1)
(let [block (db/entity [:block/uuid block-id])
has-children? (seq (:block/_parent block))
block (db/pull (:db/id block))
left (tree/-get-left (outliner-core/block block))
left-has-children? (and left
(when-let [block-id (:block/uuid (:data left))]
(let [block (db/entity [:block/uuid block-id])]
(seq (:block/_parent block)))))]
(when-not (and has-children? left-has-children?)
(when block-parent-id
(let [block-parent (gdom/getElement block-parent-id)
sibling-block (util/get-prev-block-non-collapsed block-parent)]
(delete-block-aux! block delete-children?)
(when (and repo sibling-block)
(when-let [sibling-block-id (dom/attr sibling-block "blockid")]
(when-let [block (db/pull repo '[*] [:block/uuid (uuid sibling-block-id)])]
(let [original-content (util/trim-safe (:block/content block))
new-value (str original-content " " (string/triml value))
tail-len (count (string/triml value))
pos (max
(if original-content
(utf8/length (utf8/encode original-content))
0)
0)]
(edit-block! block pos format id
{:custom-content new-value
:tail-len tail-len
:move-cursor? false})))))))))))))
(state/set-editor-op! nil)))
(defn- get-end-block-parent
[end-block blocks]
(if-let [parent (let [id (:db/id (:block/parent end-block))]
(some (fn [block] (when (= (:db/id block) id) block)) blocks))]
(recur parent blocks)
end-block))
(defn- get-top-level-end-node
[blocks]
(let [end-block (last blocks)
end-block-parent (get-end-block-parent end-block blocks)]
(outliner-core/block end-block-parent)))
(defn- reorder-blocks
[blocks]
(if (<= (count blocks) 1)
blocks
(let [[f s & others] blocks]
(if (= (or (:block/left s)
(:block/parent s))
{:db/id (:db/id f)})
blocks
(reverse blocks)))))
(defn delete-blocks!
[repo block-uuids]
(when (seq block-uuids)
(let [lookup-refs (map (fn [id] [:block/uuid id]) block-uuids)
blocks (db/pull-many repo '[*] lookup-refs)
blocks (reorder-blocks blocks)
start-node (outliner-core/block (first blocks))
end-node (get-top-level-end-node blocks)]
(if (= start-node end-node)
(delete-block-aux! (first blocks) true)
(when (outliner-core/delete-nodes start-node end-node lookup-refs)
(let [opts {:key :block/change
:data blocks}]
(db/refresh! repo opts)
(ui-handler/re-render-root!)))))))
(defn- block-property-aux!
[block-id key value]
(let [block-id (if (string? block-id) (uuid block-id) block-id)
repo (state/get-current-repo)]
(when repo
(when-let [block (db/entity [:block/uuid block-id])]
(let [format (:block/format block)
content (:block/content block)
properties (:block/properties block)
properties (if (nil? value)
(dissoc properties key)
(assoc properties key value))
content (if (nil? value)
(property/remove-property format key content)
(property/insert-property format content key value))
block (outliner-core/block {:block/uuid block-id
:block/properties properties
:block/content content})
input-pos (or (state/get-edit-pos) :max)]
(outliner-core/save-node block)
(db/refresh! (state/get-current-repo)
{:key :block/change
:data [(db/pull [:block/uuid block-id])]})
;; update editing input content
(when-let [editing-block (state/get-edit-block)]
(when (= (:block/uuid editing-block) block-id)
(edit-block! editing-block
input-pos
format
(state/get-edit-input-id)))))))))
(defn remove-block-property!
[block-id key]
(let [key (keyword key)]
(block-property-aux! block-id key nil)))
(defn set-block-property!
[block-id key value]
(let [key (keyword key)]
(block-property-aux! block-id key value)))
(defn set-block-query-properties!
[block-id all-properties key add?]
(when-let [block (db/entity [:block/uuid block-id])]
(let [query-properties (-> (get-in block [:block/properties :query-properties] "")
(common-handler/safe-read-string "Failed to parse query properties"))
query-properties (if (seq query-properties)
query-properties
all-properties)
query-properties (if add?
(distinct (conj query-properties key))
(remove #{key} query-properties))
query-properties (vec query-properties)]
(if (seq query-properties)
(set-block-property! block-id :query-properties (str query-properties))
(remove-block-property! block-id :query-properties)))))
(defn set-block-timestamp!
[block-id key value]
(let [key (string/lower-case key)
block-id (if (string? block-id) (uuid block-id) block-id)
key (string/lower-case (str key))
value (str value)]
(when-let [block (db/pull [:block/uuid block-id])]
(let [{:block/keys [content scheduled deadline format]} block
content (or (state/get-edit-content) content)
new-line (str (string/upper-case key) ": " value)
new-content (let [lines (string/split-lines content)
new-lines (map (fn [line]
(if (string/starts-with? (string/lower-case line) key)
new-line
line))
lines)
new-lines (if (not= lines new-lines)
new-lines
(cons (first new-lines) ;; title
(cons
new-line
(rest new-lines))))]
(string/join "\n" new-lines))]
(when (not= content new-content)
(if-let [input-id (state/get-edit-input-id)]
(state/set-edit-content! input-id new-content)
(save-block-if-changed! block new-content)))))))
(defn copy-block-ref!
([block-id] (copy-block-ref! block-id #(str %)))
([block-id tap-clipboard]
(let [block (db/entity [:block/uuid block-id])]
(when-not (:block/pre-block? block)
(set-block-property! block-id "id" (str block-id))))
(util/copy-to-clipboard! (tap-clipboard block-id))))
(defn select-block!
[block-uuid]
(when-let [block (-> (str block-uuid)
(js/document.getElementsByClassName)
first)]
(state/exit-editing-and-set-selected-blocks! [block])))
(defn- get-selected-blocks-with-children
[]
(when-let [blocks (seq (state/get-selection-blocks))]
(->> (mapcat (fn [block]
(cons block
(array-seq (dom/by-class block "ls-block"))))
blocks)
distinct)))
(defn- blocks-with-level
[blocks]
(let [level-blocks (mapv #(assoc % :level 1) blocks)
level-blocks-map (into {} (mapv (fn [b] [(:db/id b) b]) level-blocks))
[level-blocks-map _]
(reduce (fn [[r state] [id block]]
(if-let [parent-level (get-in state [(:db/id (:block/parent block)) :level])]