-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
common.cljs
105 lines (93 loc) · 3.26 KB
/
common.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
(ns frontend.handler.common
"Common fns for handlers"
(:require [cljs-bean.core :as bean]
[cljs.reader :as reader]
[clojure.string :as string]
[frontend.date :as date]
[frontend.state :as state]
[frontend.util :as util]
[frontend.util.property :as property]
[goog.object :as gobj]
["ignore" :as Ignore]))
(defn copy-to-clipboard-without-id-property!
[format raw-text html blocks]
(util/copy-to-clipboard! (property/remove-id-property format raw-text)
:html html
:blocks blocks))
(defn config-with-document-mode
[config]
(assoc config
:document/mode? (state/sub [:document/mode?])))
(defn ignore-files
[pattern paths]
(-> (Ignore)
(.add pattern)
(.filter (bean/->js paths))
(bean/->clj)))
(defn- hidden?
[path patterns]
(let [path (if (and (string? path)
(= \/ (first path)))
(subs path 1)
path)]
(some (fn [pattern]
(let [pattern (if (and (string? pattern)
(not= \/ (first pattern)))
(str "/" pattern)
pattern)]
(string/starts-with? (str "/" path) pattern))) patterns)))
(defn remove-hidden-files
[files config get-path-fn]
(if-let [patterns (seq (:hidden config))]
(remove (fn [file]
(let [path (get-path-fn file)]
(hidden? path patterns))) files)
files))
(defn safe-read-string
[content error-message-or-handler]
(try
(reader/read-string content)
(catch :default e
(js/console.error e)
(if (fn? error-message-or-handler)
(error-message-or-handler e)
(println error-message-or-handler))
{})))
(defn get-page-default-properties
[page-name]
{:title page-name
;; :date (date/get-date-time-string)
})
(defn fix-pages-timestamps
[pages]
(map (fn [{:block/keys [created-at updated-at journal-day] :as p}]
(cond->
p
(nil? created-at)
(assoc :block/created-at
(if journal-day
(date/journal-day->ts journal-day)
(util/time-ms)))
(nil? updated-at)
(assoc :block/updated-at
;; Not exact true
(if journal-day
(date/journal-day->ts journal-day)
(util/time-ms)))))
pages))
(defn show-custom-context-menu! [e context-menu-content]
(util/stop e)
(let [position [(gobj/get e "clientX") (gobj/get e "clientY")]]
(state/show-custom-context-menu! context-menu-content position)))
(defn listen-to-scroll!
[element]
(let [*scroll-timer (atom nil)]
(.addEventListener element "scroll"
(fn []
(when @*scroll-timer
(js/clearTimeout @*scroll-timer))
(state/set-state! :ui/scrolling? true)
(state/save-scroll-position! (util/scroll-top))
(reset! *scroll-timer (js/setTimeout
(fn [] (state/set-state! :ui/scrolling? false)) 500)))
false)))