-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
editor.cljs
2230 lines (2064 loc) · 96.7 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 [frontend.state :as state]
[lambdaisland.glogi :as log]
[frontend.db.model :as db-model]
[frontend.db.utils :as db-utils]
[frontend.handler.common :as common-handler]
[frontend.handler.route :as route-handler]
[frontend.handler.git :as git-handler]
[frontend.handler.ui :as ui-handler]
[frontend.handler.repo :as repo-handler]
[frontend.handler.file :as file-handler]
[frontend.handler.notification :as notification]
[frontend.handler.draw :as draw]
[frontend.handler.expand :as expand]
[frontend.format.mldoc :as mldoc]
[frontend.format :as format]
[frontend.format.block :as block]
[frontend.image :as image]
[cljs-time.core :as t]
[cljs-time.coerce :as tc]
[frontend.db :as db]
[goog.object :as gobj]
[goog.dom :as gdom]
[goog.dom.classes :as gdom-classes]
[clojure.string :as string]
[clojure.set :as set]
[frontend.util :as util :refer-macros [profile]]
[frontend.config :as config]
[dommy.core :as dom]
[frontend.utf8 :as utf8]
[frontend.fs :as fs]
[promesa.core :as p]
[dommy.core :as d]
[frontend.diff :as diff]
[frontend.search :as search]
[frontend.handler.image :as image-handler]
[frontend.commands :as commands
:refer [*show-commands
*slash-caret-pos
*angle-bracket-caret-pos
*show-block-commands]]
[frontend.extensions.html-parser :as html-parser]
[medley.core :as medley]
[frontend.text :as text]
[frontend.date :as date]
[frontend.handler.repeated :as repeated]
[clojure.core.async :as async]
[lambdaisland.glogi :as log]))
;; FIXME: should support multiple images concurrently uploading
(defonce *image-pending-file (atom nil))
(defonce *image-uploading? (atom false))
(defonce *image-uploading-process (atom 0))
(defonce *selected-text (atom nil))
(defn modified-time-tx
[page file]
(let [modified-at (tc/to-long (t/now))]
[[:db/add (:db/id page) :page/last-modified-at modified-at]
[:db/add (:db/id file) :file/last-modified-at modified-at]]))
(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)
new-value (str
(subs value 0 selection-start)
pattern
(subs value selection-start selection-end)
pattern
(subs value selection-end))]
(state/set-edit-content! edit-id new-value)
(when empty-selection?
(util/cursor-move-back input (count pattern))))))
(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 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 (:pos (util/get-caret-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)
(util/move-cursor-to input (+ cur-pos forward-pos)))))
(defn focus-on-block!
[block-id]
(when block-id
(route-handler/redirect! {:to :page
:path-params {:name (str block-id)}})))
(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]
(restore-cursor-pos! id markup false))
([id markup dummy?]
(when-let [node (gdom/getElement (str id))]
(when-let [cursor-range (state/get-cursor-range)]
(when-let [range (string/trim cursor-range)]
(let [pos (diff/find-position markup range)]
(util/set-caret-pos! node pos)))))))
(defn block-content-join-newlines
[prefix value postfix]
(str
(if (or (= "" prefix)
(= "\n" (last prefix)))
""
"\n")
(string/trim value)
(if (= "\n" (first postfix))
""
"\n")))
(defn get-current-input-node
[]
(let [edit-input-id (state/get-edit-input-id)]
(and edit-input-id (gdom/getElement edit-input-id))))
(defn get-current-input-value
[]
(let [edit-input-id (state/get-edit-input-id)
input (and edit-input-id (gdom/getElement edit-input-id))]
(when input
(gobj/get input "value"))))
(defn new-file-content
[{:block/keys [content meta dummy?] :as block} file-content value]
(let [utf8-content (utf8/encode file-content)
prefix (utf8/substring utf8-content 0 (:start-pos meta))
postfix (let [end-pos (if dummy?
(:start-pos meta)
(:end-pos meta))]
(utf8/substring utf8-content end-pos))
value (block-content-join-newlines prefix value postfix)]
[(str prefix value postfix)
value]))
(defn get-block-new-value
[{:block/keys [content meta dummy?] :as block} file-content value]
(let [utf8-content (utf8/encode file-content)
prefix (utf8/substring utf8-content 0 (:start-pos meta))
postfix (let [end-pos (if dummy?
(:start-pos meta)
(:end-pos meta))]
(utf8/substring utf8-content end-pos))]
(block-content-join-newlines prefix value postfix)))
(defn new-file-content-indent-outdent
[{:block/keys [content meta dummy?] :as block} file-content value block-with-children-content last-child-end-pos indent-left?]
(let [utf8-content (utf8/encode file-content)
prefix (utf8/substring utf8-content 0 (:start-pos meta))
last-child-end-pos (if (some? indent-left?) last-child-end-pos nil)
end-pos (or
last-child-end-pos
(if dummy?
(:start-pos meta)
(:end-pos meta)))
postfix (utf8/substring utf8-content end-pos)
block-children-value (block-content-join-newlines prefix block-with-children-content postfix)]
(str prefix block-children-value postfix)))
(defn- with-block-meta
[repo block]
(if (:block/dummy? block)
(if-let [page-id (:db/id (:block/page block))]
(let [page-name (:page/name (db/entity repo page-id))
end-pos (db/get-block-page-end-pos repo page-name)]
(assoc block :block/meta {:start-pos end-pos
:end-pos end-pos}))
block)
(if-let [meta (:block/meta (db/entity repo [:block/uuid (:block/uuid block)]))]
(assoc block :block/meta meta)
block)))
(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-block!
[]
(let [blocks (some->> (array-seq (js/document.getElementsByClassName "block-highlight"))
(repeat 2)
(apply concat))]
(doseq [block blocks]
(gdom-classes/remove block "block-highlight"))))
(defn rebuild-blocks-meta
[start-pos blocks]
(let [last-start-pos (atom start-pos)]
(mapv
(fn [{:block/keys [uuid meta] :as block}]
(let [old-start-pos (:start-pos meta)
old-end-pos (:end-pos meta)
new-end-pos (if old-end-pos
(+ @last-start-pos (- old-end-pos old-start-pos)))
new-meta {:start-pos @last-start-pos
:end-pos new-end-pos}]
(reset! last-start-pos new-end-pos)
{:block/uuid uuid
:block/meta new-meta}))
blocks)))
(defn rebuild-after-blocks
[repo file before-end-pos new-end-pos]
(let [file-id (:db/id file)
after-blocks (db/get-file-after-blocks-meta repo file-id before-end-pos)]
(rebuild-blocks-meta new-end-pos after-blocks)))
(defn rebuild-after-blocks-indent-outdent
[repo file block before-end-pos new-end-pos indent-left?]
(let [file-id (:db/id file)
after-blocks (db/get-file-after-blocks-meta repo file-id before-end-pos true)
last-start-pos (atom new-end-pos)
block-level (:block/level block)
next-leq-level? (atom false)
format (:block/format block)
block-and-children-content (atom (:block/content block))
last-child-end-pos (atom before-end-pos)
after-blocks (mapv
(fn [{:block/keys [uuid meta level content] :as block}]
(let [old-start-pos (:start-pos meta)
old-end-pos (:end-pos meta)]
(when (<= level block-level)
(reset! next-leq-level? true))
(let [[new-content offset] (cond
(and (not @next-leq-level?) (true? indent-left?))
[(subs content 1) -1]
(and (not @next-leq-level?) (false? indent-left?))
[(str (config/get-block-pattern format) content) 1]
:else
[nil 0])
new-end-pos (if old-end-pos
(+ @last-start-pos
(- old-end-pos old-start-pos)
offset))
new-meta {:start-pos @last-start-pos
:end-pos new-end-pos}]
(reset! last-start-pos new-end-pos)
(when-not @next-leq-level?
(do
(reset! block-and-children-content (str @block-and-children-content new-content))
(reset! last-child-end-pos old-end-pos)))
(cond->
{:block/uuid uuid
:block/meta new-meta}
(and (some? indent-left?) (not @next-leq-level?))
(assoc :block/level (if indent-left? (dec level) (inc level)))
(and new-content (not @next-leq-level?))
(assoc :block/content new-content)))))
after-blocks)]
[after-blocks @block-and-children-content @last-child-end-pos]))
(defn compute-retract-refs
"Computes old references to be retracted."
[eid {:block/keys [ref-pages ref-blocks]} old-ref-pages old-ref-blocks]
(let [ref-pages-id (map #(:db/id (db/get-page (:page/name %))) ref-pages)
retracted-pages (reduce (fn [done current]
(if (some #(= (:db/id current) %) ref-pages-id)
done
(conj done (:db/id current))))
[] old-ref-pages)
ref-blocks-id (map #(:db/id (db/get-page (str (last %)))) ref-blocks)
retracted-blocks (reduce (fn [done current]
(if (some #(= (:db/id current) %) ref-blocks-id)
done
(conj done (:db/id current))))
[] old-ref-blocks)]
;; removes retracted pages and blocks
(into
(mapv (fn [ref] [:db/retract eid :block/ref-pages ref]) retracted-pages)
(mapv (fn [ref] [:db/retract eid :block/ref-blocks ref]) retracted-blocks))))
(defn- block-with-title
[content format]
(let [content-without-level-spaces (-> (text/remove-level-spaces content format))
content-without-level-spaces (str (when (= \n (first content-without-level-spaces))
"\n")
(string/trim content-without-level-spaces))
first-block (-> content-without-level-spaces
(format/to-edn format)
ffirst)]
(or (block/heading-block? first-block)
(block/paragraph-block? first-block)
(block/hiccup-block? first-block)
(block/definition-list-block? first-block))))
(defn- rebuild-block-content
"We'll create an empty heading if the first parsed ast element is not a paragraph, definition list or some hiccup."
[content format]
(if (block-with-title content format)
content
(text/append-newline-after-level-spaces content format)))
(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")))
;; 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 custom-properties tail-len]
:or {tail-len 0}}]
(when-not config/publishing?
(when-let [block-id (:block/uuid block)]
(let [edit-input-id (if (uuid? id)
(get-edit-input-id-with-block-id id)
(str (subs id 0 (- (count id) 36)) block-id))
block (or
(db/pull [:block/uuid block-id])
block
;; dummy?
{:block/uuid block-id
:block/content ""})
{:block/keys [content properties]} block
content (or custom-content content)
content (string/trim (text/remove-level-spaces content format))
properties (or custom-properties properties)
content (if (and (seq properties) (text/properties-hidden? properties))
(text/remove-properties! content)
content)
content-length (count content)
text-range (cond
(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))]
(state/set-editing! edit-input-id content block text-range))))))
(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- with-time-properties
[block properties]
(if (and (state/enable-block-time?)
(not (:block/pre-block? block))
(not= "Src" (ffirst (:block/body block))))
(let [time (util/time-ms)
props (into {} (:block/properties block))]
(merge properties
(if-let [created-at (get props "created_at")]
{"created_at" created-at
"last_modified_at" time}
{"created_at" time
"last_modified_at" time})))
properties))
(defn- block-text-with-time
([block format value]
(block-text-with-time block format value {}))
([block format value properties]
(let [properties (with-time-properties block properties)
block-with-title? (boolean (block-with-title value format))]
(text/re-construct-block-properties format value properties
block-with-title?))))
(defn save-block-if-changed!
([block value]
(save-block-if-changed! block value nil))
([{:block/keys [uuid content meta file page dummy? format repo pre-block? content ref-pages ref-blocks] :as block}
value
{:keys [indent-left? custom-properties remove-properties rebuild-content? chan chan-callback]
:or {rebuild-content? true
custom-properties nil
remove-properties nil}
:as opts}]
(let [repo (or repo (state/get-current-repo))
e (db/entity repo [:block/uuid uuid])
block (assoc (with-block-meta repo block)
:block/properties (into {} (:block/properties e)))
format (or format (state/get-preferred-format))
page (db/entity repo (:db/id page))
;; page properties
[old-properties new-properties] (when pre-block?
[(:page/properties (db/entity (:db/id page)))
(mldoc/parse-properties value format)])
page-tags (when-let [tags (:tags new-properties)]
(mapv (fn [tag] {:page/name (string/lower-case tag)
:page/original-name tag}) tags))
page-alias (when-let [alias (:alias new-properties)]
(map
(fn [alias]
{:page/original-name alias
:page/name (string/lower-case alias)})
(remove #{(:page/name page)} alias)))
permalink-changed? (when (and pre-block? (:permalink old-properties))
(not= (:permalink old-properties)
(:permalink new-properties)))
value (if permalink-changed?
(db/add-properties! format value {:old_permalink (:permalink old-properties)})
value)
new-properties (if permalink-changed?
(assoc new-properties :old_permalink (:permalink old-properties))
new-properties)
text-properties (text/extract-properties value)
old-hidden-properties (select-keys (:block/properties block) text/hidden-properties)
properties (merge old-hidden-properties
custom-properties
text-properties)
remove-properties (->
(set/difference (set (keys (:block/properties block)))
(set (keys text-properties))
text/hidden-properties)
(set/union (set remove-properties)))
properties (medley/remove-keys (fn [k] (contains? remove-properties k)) properties)
value (block-text-with-time block format value properties)
content-changed? (not= (text/remove-timestamp-property! (string/trim content))
(text/remove-timestamp-property! (string/trim value)))]
(cond
content-changed?
(let [file (db/entity repo (:db/id file))]
(cond
;; Page was referenced but no related file
;; TODO: replace with handler.page/create!
(and page (not file))
(let [format (name format)
title (string/capitalize (:page/name page))
journal-page? (date/valid-journal-title? title)
path (str
(if journal-page?
config/default-journals-directory
(config/get-pages-directory))
"/"
(if journal-page?
(date/journal-title->default title)
(-> (:page/name page)
(util/page-name-sanity))) "."
(if (= format "markdown") "md" format))
file-path (str "/" path)
dir (config/get-repo-dir repo)]
(p/let [exists? (fs/file-exists? dir file-path)]
(if exists?
(notification/show!
[:p.content
(util/format "File %s already exists!" file-path)]
:error)
;; create the file
(let [value (block-text-with-time nil format value)
content (str (util/default-content-with-title format
(or (:page/original-name page)
(:page/name page)))
value)]
(p/let [_ (fs/create-if-not-exists repo dir file-path content)
_ (git-handler/git-add repo path)]
(file-handler/reset-file! repo path content)
(ui-handler/re-render-root!)
;; Continue to edit the last block
(let [blocks (db/get-page-blocks repo (:page/name page))
last-block (last blocks)]
(edit-last-block-for-new-page! last-block :max)))))))
(and file page)
(let [file (db/entity repo (:db/id file))
file-path (:file/path file)
format (format/get-format file-path)
file-content (db/get-file repo file-path)
value (get-block-new-value block file-content value)
value (if rebuild-content?
(rebuild-block-content value format)
value)
block (assoc block :block/content value)
{:keys [blocks pages start-pos end-pos]} (if pre-block?
(let [new-end-pos (utf8/length (utf8/encode value))]
{:blocks [(assoc-in block [:block/meta :end-pos] new-end-pos)]
:pages []
:start-pos 0
:end-pos new-end-pos})
(block/parse-block block format))
block-retracted-attrs (when-not pre-block?
;; TODO: should we retract the whole block instead?
(when-let [id (:db/id block)]
[[:db/retract id :block/properties]
[:db/retract id :block/priority]
[:db/retract id :block/deadline]
[:db/retract id :block/deadline-ast]
[:db/retract id :block/scheduled]
[:db/retract id :block/scheduled-ast]
[:db/retract id :block/marker]
[:db/retract id :block/repeated?]]))
[after-blocks block-children-content new-end-pos] (rebuild-after-blocks-indent-outdent repo file block (:end-pos (:block/meta block)) end-pos indent-left?)
retract-refs (compute-retract-refs (:db/id e) (first blocks) ref-pages ref-blocks)
page-id (:db/id page)
modified-time (let [modified-at (tc/to-long (t/now))]
[[:db/add page-id :page/last-modified-at modified-at]
[:db/add (:db/id file) :file/last-modified-at modified-at]])
page-properties (when pre-block?
(if (seq new-properties)
[[:db/retract page-id :page/properties]
{:db/id page-id
:page/properties new-properties}]
[[:db/retract page-id :page/properties]]))
pages (if (seq page-tags)
(concat pages page-tags)
pages)
pages (remove
(fn [page]
(string/blank? (:page/name page)))
pages)
page-tags (when (and pre-block? (seq page-tags))
(if (seq page-tags)
[[:db/retract page-id :page/tags]
{:db/id page-id
:page/tags page-tags}]
[[:db/retract page-id :page/tags]]))
page-alias (when (and pre-block? (seq page-alias))
(if (seq page-alias)
[[:db/retract page-id :page/alias]
{:db/id page-id
:page/alias page-alias}]
[[:db/retract page-id :page/alias]]))]
(profile
"Save block: "
(repo-handler/transact-react-and-alter-file!
repo
(concat
pages
block-retracted-attrs
(mapv (fn [b] {:block/uuid (:block/uuid b)}) blocks)
blocks
retract-refs
page-properties
page-tags
page-alias
after-blocks
modified-time)
{:key :block/change
:data (map (fn [block] (assoc block :block/page page)) blocks)}
(let [new-content (new-file-content-indent-outdent block file-content value block-children-content new-end-pos indent-left?)]
[[file-path new-content]])
(when chan {:chan chan
:chan-callback chan-callback})))
;; fix editing template with multiple headings
(when (> (count blocks) 1)
(let [new-value (-> (text/remove-level-spaces (:block/content (first blocks)) (:block/format (first blocks)))
(string/trim-newline))
edit-input-id (state/get-edit-input-id)]
(when edit-input-id
(state/set-edit-content! edit-input-id new-value))))
(when (or (seq retract-refs) pre-block?)
(ui-handler/re-render-root!))
(repo-handler/push-if-auto-enabled! repo))
:else
nil))
(seq (state/get-changed-files))
(repo-handler/push-if-auto-enabled! repo)
:else
nil))))
(defn insert-new-block-aux!
[{:block/keys [uuid content meta file dummy? level repo page format properties collapsed? pre-block?] :as block}
value
{:keys [create-new-block? ok-handler with-level? new-level current-page blocks-container-id]}]
(let [value (or value "")
block-page? (and current-page (util/uuid-string? current-page))
block-self? (= uuid (and block-page? (medley/uuid current-page)))
input (gdom/getElement (state/get-edit-input-id))
pos (if new-level
(dec (count value))
(util/get-input-pos input))
repo (or repo (state/get-current-repo))
block-has-children? (seq (:block/children block))
fst-block-text (subs value 0 pos)
snd-block-text (string/triml (subs value pos))
fst-block-text (string/trim (if with-level? fst-block-text (block/with-levels fst-block-text format block)))
edit-self? (and block-has-children? (zero? pos))
snd-block-text-level (cond
new-level
new-level
(or block-self?
(and block-has-children?
(not (zero? pos))))
(inc level)
:else
level)
snd-block-text (if (and snd-block-text
(re-find (re-pattern (util/format "^[%s]+\\s+" (config/get-block-pattern format))) snd-block-text))
snd-block-text
(rebuild-block-content
(str (config/default-empty-block format snd-block-text-level) " " snd-block-text)
format))
block (with-block-meta repo block)
original-id (:block/uuid block)
format (:block/format block)
page (db/entity repo (:db/id page))
file (db/entity repo (:db/id file))
insert-block (fn [block file-path file-content]
(let [value (if create-new-block?
(str fst-block-text "\n" snd-block-text)
value)
snd-block-text (text/remove-id-property snd-block-text)
text-properties (if (zero? pos)
{}
(text/extract-properties fst-block-text))
old-hidden-properties (select-keys (:block/properties block) text/hidden-properties)
properties (merge old-hidden-properties
text-properties)
value (if create-new-block?
(str
(->
(block-text-with-time block format fst-block-text properties)
(string/trimr))
"\n"
(string/triml snd-block-text))
(block-text-with-time block format value properties))
value (rebuild-block-content value format)
[new-content value] (new-file-content block file-content value)
parse-result (block/parse-block (assoc block :block/content value) format)
id-conflict? (some #(= original-id (:block/uuid %)) (next (:blocks parse-result)))
{:keys [blocks pages start-pos end-pos]}
(if id-conflict?
(let [new-value (string/replace
value
(re-pattern (str "(?i):(custom_)?id: " original-id))
"")]
(block/parse-block (assoc block :block/content new-value) format))
parse-result)
after-blocks (rebuild-after-blocks repo file (:end-pos meta) end-pos)
files [[file-path new-content]]
block-retracted-attrs (when-not pre-block?
;; TODO: should we retract the whole block instead?
(when-let [id (:db/id block)]
[[:db/retract id :block/properties]
[:db/retract id :block/priority]
[:db/retract id :block/deadline]
[:db/retract id :block/deadline-ast]
[:db/retract id :block/scheduled]
[:db/retract id :block/scheduled-ast]
[:db/retract id :block/marker]
[:db/retract id :block/repeated?]]))
transact-fn (fn []
(repo-handler/transact-react-and-alter-file!
repo
(concat
block-retracted-attrs
pages
(mapv (fn [b] {:block/uuid (:block/uuid b)}) blocks)
blocks
after-blocks)
{:key :block/insert
:data (map (fn [block] (assoc block :block/page page)) blocks)}
files)
(state/set-editor-op! nil))]
;; Replace with batch transactions
(state/add-tx! transact-fn)
(let [blocks (remove (fn [block]
(nil? (:block/content block))) blocks)
page-blocks-atom (db/get-page-blocks-cache-atom repo (:db/id page))
first-block-id (:block/uuid (first blocks))
[before-part after-part] (and page-blocks-atom
(split-with
#(not= first-block-id (:block/uuid %))
@page-blocks-atom))
after-part (rest after-part)
blocks-container-id (and blocks-container-id
(util/uuid-string? blocks-container-id)
(medley/uuid blocks-container-id))]
; WORKAROUND: The block won't refresh itself even if the content is empty.
(when edit-self?
(gobj/set input "value" ""))
(when ok-handler
(ok-handler
(if edit-self? (first blocks) (last blocks))))
;; update page blocks cache if exists
(when page-blocks-atom
(reset! page-blocks-atom (->> (concat before-part blocks after-part)
(remove nil?))))
;; update block children cache if exists
(when blocks-container-id
(let [blocks-atom (db/get-block-blocks-cache-atom repo blocks-container-id)
[before-part after-part] (and blocks-atom
(split-with
#(not= first-block-id (:block/uuid %))
@blocks-atom))
after-part (rest after-part)]
(and blocks-atom
(reset! blocks-atom (->> (concat before-part blocks after-part)
(remove nil?)))))))))]
(cond
(and (not file) page)
;; TODO: replace with handler.page/create!
(let [format (name format)
title (string/capitalize (:page/name page))
journal-page? (date/valid-journal-title? title)
path (str
(if journal-page?
config/default-journals-directory
(config/get-pages-directory))
"/"
(if journal-page?
(date/journal-title->default title)
(-> (:page/name page)
(util/page-name-sanity)))
"."
(if (= format "markdown") "md" format))
file-path (str "/" path)
dir (config/get-repo-dir repo)]
(p/let [exists? (fs/file-exists? dir file-path)]
(if exists?
(do (notification/show!
[:p.content
(util/format "File %s already exists!"
file-path)]
:error)
(state/set-editor-op! nil))
;; create the file
(let [content (util/default-content-with-title format (or
(:page/original-name page)
(:page/name page)))]
(p/let [_ (fs/create-if-not-exists repo dir file-path content)
_ (git-handler/git-add repo path)]
(let [value (block-text-with-time nil format value)]
(file-handler/reset-file! repo path
(str content
value
"\n"
snd-block-text)))
(ui-handler/re-render-root!)
;; Continue to edit the last block
(let [blocks (db/get-page-blocks repo (:page/name page))
last-block (last blocks)]
(edit-last-block-for-new-page! last-block 0))
(state/set-editor-op! nil))))))
file
(let [file-path (:file/path file)
file-content (db/get-file repo file-path)]
(insert-block block file-path file-content))
:else
nil)))
(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
[state]
(let [[{:keys [on-hide block block-id block-parent-id dummy? format sidebar?]} id config] (:rum/args state)
node (gdom/getElement id)
value (gobj/get node "value")
pos (gobj/get node "selectionStart")]
{:config config
:on-hide on-hide
:dummy? dummy?
:sidebar? sidebar?
:format format
:id id
: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 (re-find format/bare-marker-pattern (or value "")))
new-marker (if new-marker (string/lower-case (string/trim new-marker)))
time-properties (if (and
new-marker
(not= new-marker (string/lower-case (or (:block/marker block) "")))
(state/enable-timetracking?))
{new-marker (util/time-ms)}
{})]
(merge (:block/properties block)
time-properties)))
(defn insert-new-block!
[state]
(when (and (not config/publishing?)
;; skip this operation if it's inserting
(not= :insert (state/get-editor-op)))
(state/set-editor-op! :insert)
(let [{:keys [block value format id config]} (get-state state)
block-id (:block/uuid block)
block (or (db/pull [:block/uuid block-id])
block)
collapsed? (:block/collapsed? block)
repo (or (:block/repo block) (state/get-current-repo))
last-child (and collapsed?
(last (db/get-block-and-children-no-cache repo (:block/uuid block))))
last-child (when (not= (:block/uuid last-child)
(:block/uuid block))
last-child)
new-block (or last-child block)
new-value (if last-child (:block/content last-child) value)
properties (with-timetracking-properties new-block new-value)]
;; save the current block and insert a new block
(insert-new-block-aux!
(assoc new-block :block/properties properties)
new-value
{:create-new-block? true
:ok-handler
(fn [last-block]
(let [last-id (:block/uuid last-block)]
(edit-block! last-block 0 format id)
(clear-when-saved!)))
:with-level? (if last-child true false)
:new-level (and last-child (:block/level block))
:blocks-container-id (:id config)
:current-page (state/get-current-page)}))))
(defn insert-new-block-without-save-previous!
[config last-block]
(let [format (:block/format last-block)
id (:id config)
new-level (if (util/uuid-string? id)
(inc (:block/level (db/entity [:block/uuid (medley/uuid id)])))
2)]
(insert-new-block-aux!
last-block
(:block/content last-block)
{:create-new-block? true
:ok-handler
(fn [last-block]
(js/setTimeout #(edit-last-block-for-new-page! last-block :max) 50))
:with-level? true
:new-level new-level
:blocks-container-id (:id config)
:current-page (state/get-current-page)})))
(defn update-timestamps-content!
[{:block/keys [repeated? scheduled-ast deadline-ast marker]} content]
(if repeated?
(let [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 "- %s -> DONE [%s]"
marker
(date/get-local-date-time-string)))))
content))
(defn- with-marker-time
[block marker]
(if (state/enable-timetracking?)
(let [marker (string/lower-case marker)]
{marker (util/time-ms)})
{}))
(defn check
[{:block/keys [uuid marker content meta file dummy? repeated?] :as block}]
(let [new-content (string/replace-first content marker "DONE")
new-content (if repeated?
(update-timestamps-content! block content)
new-content)]
(save-block-if-changed! block new-content
{:custom-properties (with-marker-time block "DONE")})))
(defn uncheck
[{:block/keys [uuid marker content meta file dummy?] :as block}]
(let [marker (if (= :now (state/get-preferred-workflow))
"LATER"
"TODO")
new-content (string/replace-first content "DONE" marker)]
(save-block-if-changed! block new-content
{:custom-properties (with-marker-time block marker)})))
(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)
[new-content marker] (cond
(util/starts-with? content "TODO")
[(string/replace-first content "TODO" "DOING") "DOING"]
(util/starts-with? content "DOING")
[(string/replace-first content "DOING" "DONE") "DONE"]
(util/starts-with? content "LATER")
[(string/replace-first content "LATER" "NOW") "NOW"]
(util/starts-with? content "NOW")
[(string/replace-first content "NOW" "DONE") "DONE"]
(util/starts-with? content "DONE")
[(string/replace-first content "DONE" "") nil]
:else
(let [marker (if (= :now (state/get-preferred-workflow))
"LATER"
"TODO")]
[(str marker " " (string/triml content)) marker]))
new-content (string/triml new-content)]
(let [new-pos (commands/compute-pos-delta-when-change-marker
current-input content new-content marker (util/get-input-pos current-input))]
(state/set-edit-content! edit-input-id new-content)
(util/set-caret-pos! current-input new-pos)))))
(defn set-marker
[{:block/keys [uuid marker content meta file dummy? properties] :as block} new-marker]
(let [new-content (string/replace-first content marker new-marker)]
(save-block-if-changed! block new-content
(with-marker-time block marker))))
(defn set-priority
[{:block/keys [uuid marker priority content meta file dummy?] :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)))