page.cljs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. (ns frontend.handler.page
  2. (:require [cljs.reader :as reader]
  3. [clojure.string :as string]
  4. [clojure.walk :as walk]
  5. [datascript.core :as d]
  6. [frontend.commands :as commands]
  7. [frontend.config :as config]
  8. [frontend.date :as date]
  9. [frontend.db :as db]
  10. [logseq.db.schema :as db-schema]
  11. [frontend.db.model :as model]
  12. [frontend.db.utils :as db-utils]
  13. [frontend.db.conn :as conn]
  14. [frontend.fs :as fs]
  15. [frontend.handler.common :as common-handler]
  16. [frontend.handler.editor :as editor-handler]
  17. [frontend.handler.notification :as notification]
  18. [frontend.handler.route :as route-handler]
  19. [frontend.handler.ui :as ui-handler]
  20. [frontend.handler.web.nfs :as web-nfs]
  21. [frontend.handler.config :as config-handler]
  22. [frontend.handler.recent :as recent-handler]
  23. [frontend.modules.outliner.core :as outliner-core]
  24. [frontend.modules.outliner.file :as outliner-file]
  25. [frontend.modules.outliner.tree :as outliner-tree]
  26. [frontend.state :as state]
  27. [frontend.util :as util]
  28. [frontend.util.cursor :as cursor]
  29. [frontend.util.property :as property]
  30. [frontend.util.page-property :as page-property]
  31. [goog.object :as gobj]
  32. [lambdaisland.glogi :as log]
  33. [promesa.core :as p]
  34. [frontend.mobile.util :as mobile-util]
  35. [logseq.graph-parser.util :as gp-util]
  36. [logseq.graph-parser.config :as gp-config]
  37. [logseq.graph-parser.block :as gp-block]
  38. [frontend.format.block :as block]
  39. [goog.functions :refer [debounce]]))
  40. (defn- get-directory
  41. [journal?]
  42. (if journal?
  43. (config/get-journals-directory)
  44. (config/get-pages-directory)))
  45. (defn- get-file-name
  46. [journal? title]
  47. (when-let [s (if journal?
  48. (date/journal-title->default title)
  49. (gp-util/page-name-sanity (string/lower-case title)))]
  50. ;; Win10 file path has a length limit of 260 chars
  51. (gp-util/safe-subs s 0 200)))
  52. (defn get-page-file-path
  53. ([] (get-page-file-path (state/get-current-page)))
  54. ([page-name]
  55. (when page-name
  56. (let [page-name (util/page-name-sanity-lc page-name)]
  57. (when-let [page (db/entity [:block/name page-name])]
  58. (:file/path (:block/file page)))))))
  59. (defn- build-title [page]
  60. (let [original-name (:block/original-name page)]
  61. (if (string/includes? original-name ",")
  62. (util/format "\"%s\"" original-name)
  63. original-name)))
  64. (defn default-properties-block
  65. ([title format page]
  66. (default-properties-block title format page {}))
  67. ([title format page properties]
  68. (let [p (common-handler/get-page-default-properties title)
  69. ps (merge p properties)
  70. content (page-property/insert-properties format "" ps)
  71. refs (gp-block/get-page-refs-from-properties properties
  72. (db/get-db (state/get-current-repo))
  73. (state/get-date-formatter))]
  74. {:block/uuid (db/new-block-id)
  75. :block/properties ps
  76. :block/properties-order (keys ps)
  77. :block/refs refs
  78. :block/left page
  79. :block/format format
  80. :block/content content
  81. :block/parent page
  82. :block/page page})))
  83. (defn- create-title-property?
  84. [journal? page-name]
  85. (and (not journal?)
  86. (util/create-title-property? page-name)))
  87. (defn- build-page-tx [format properties page journal?]
  88. (when (:block/uuid page)
  89. (let [page-entity [:block/uuid (:block/uuid page)]
  90. create-title? (create-title-property? journal?
  91. (or
  92. (:block/original-name page)
  93. (:block/name page)))
  94. page (if (seq properties) (assoc page :block/properties properties) page)
  95. page-empty? (db/page-empty? (state/get-current-repo) (:block/name page))]
  96. (cond
  97. (not page-empty?)
  98. [page]
  99. create-title?
  100. (let [properties-block (default-properties-block (build-title page) format page-entity properties)]
  101. [page
  102. properties-block])
  103. (seq properties)
  104. [page (editor-handler/properties-block properties format page-entity)]
  105. :else
  106. [page]))))
  107. (defn create!
  108. "Create page.
  109. :redirect? - when true, redirect to the created page, otherwise return sanitized page name.
  110. :split-namespace? - when true, split hierarchical namespace into levels.
  111. :create-first-block? - when true, create an empty block if the page is empty.
  112. :uuid - when set, use this uuid instead of generating a new one."
  113. ([title]
  114. (create! title {}))
  115. ([title {:keys [redirect? create-first-block? format properties split-namespace? journal? uuid]
  116. :or {redirect? true
  117. create-first-block? true
  118. format nil
  119. properties nil
  120. split-namespace? true
  121. uuid nil}}]
  122. (let [title (string/trim title)
  123. title (gp-util/remove-boundary-slashes title)
  124. page-name (util/page-name-sanity-lc title)
  125. repo (state/get-current-repo)
  126. with-uuid? (if (uuid? uuid) uuid true)] ;; FIXME: prettier validation
  127. (when (db/page-empty? repo page-name)
  128. (let [pages (if split-namespace?
  129. (gp-util/split-namespace-pages title)
  130. [title])
  131. format (or format (state/get-preferred-format))
  132. pages (map (fn [page]
  133. ;; only apply uuid to the deepest hierarchy of page to create if provided.
  134. (-> (block/page-name->map page (if (= page title) with-uuid? true))
  135. (assoc :block/format format)))
  136. pages)
  137. txs (->> pages
  138. ;; for namespace pages, only last page need properties
  139. drop-last
  140. (mapcat #(build-page-tx format nil % journal?))
  141. (remove nil?)
  142. (remove (fn [m]
  143. (some? (db/entity [:block/name (:block/name m)])))))
  144. last-txs (build-page-tx format properties (last pages) journal?)
  145. txs (concat txs last-txs)]
  146. (when (seq txs)
  147. (db/transact! txs)))
  148. (when create-first-block?
  149. (when (or
  150. (db/page-empty? repo (:db/id (db/entity [:block/name page-name])))
  151. (create-title-property? journal? page-name))
  152. (editor-handler/api-insert-new-block! "" {:page page-name}))))
  153. (when redirect?
  154. (route-handler/redirect-to-page! page-name))
  155. page-name)))
  156. (defn delete-file!
  157. [repo page-name unlink-file?]
  158. (let [file (db/get-page-file page-name)
  159. file-path (:file/path file)]
  160. ;; delete file
  161. (when-not (string/blank? file-path)
  162. (db/transact! [[:db.fn/retractEntity [:file/path file-path]]])
  163. (when unlink-file?
  164. (-> (fs/unlink! repo (config/get-repo-path repo file-path) nil)
  165. (p/catch (fn [error] (js/console.error error))))))))
  166. (defn- compute-new-file-path
  167. [old-path new-name]
  168. (let [result (string/split old-path "/")
  169. file-name (gp-util/page-name-sanity new-name true)
  170. ext (last (string/split (last result) "."))
  171. new-file (str file-name "." ext)
  172. parts (concat (butlast result) [new-file])]
  173. (string/join "/" parts)))
  174. (defn rename-file!
  175. [file new-name ok-handler]
  176. (let [repo (state/get-current-repo)
  177. file (db/pull (:db/id file))
  178. old-path (:file/path file)
  179. new-path (compute-new-file-path old-path new-name)]
  180. ;; update db
  181. (db/transact! repo [{:db/id (:db/id file)
  182. :file/path new-path}])
  183. (->
  184. (p/let [_ (fs/rename! repo old-path new-path)]
  185. (ok-handler))
  186. (p/catch (fn [error]
  187. (println "file rename failed: " error))))))
  188. (defn- replace-page-ref!
  189. "Unsanitized names"
  190. [content old-name new-name]
  191. (let [[original-old-name original-new-name] (map string/trim [old-name new-name])
  192. [old-ref new-ref] (map #(util/format "[[%s]]" %) [old-name new-name])
  193. [old-name new-name] (map #(if (string/includes? % "/")
  194. (string/replace % "/" ".")
  195. %)
  196. [original-old-name original-new-name])
  197. old-org-ref (and (= :org (state/get-preferred-format))
  198. (:org-mode/insert-file-link? (state/get-config))
  199. (re-find
  200. (re-pattern
  201. (util/format
  202. "\\[\\[file:\\.*/.*%s\\.org\\]\\[(.*?)\\]\\]" old-name))
  203. content))]
  204. (-> (if old-org-ref
  205. (let [[old-full-ref old-label] old-org-ref
  206. new-label (if (= old-label original-old-name)
  207. original-new-name
  208. old-label)
  209. new-full-ref (-> (string/replace old-full-ref old-name new-name)
  210. (string/replace (str "[" old-label "]")
  211. (str "[" new-label "]")))]
  212. (string/replace content old-full-ref new-full-ref))
  213. content)
  214. (string/replace old-ref new-ref))))
  215. (defn- replace-tag-ref!
  216. [content old-name new-name]
  217. (let [old-tag (util/format "#%s" old-name)
  218. new-tag (if (re-find #"[\s\t]+" new-name)
  219. (util/format "#[[%s]]" new-name)
  220. (str "#" new-name))]
  221. (-> (util/replace-ignore-case content (str "^" old-tag "\\b") new-tag)
  222. (util/replace-ignore-case (str " " old-tag " ") (str " " new-tag " "))
  223. (util/replace-ignore-case (str " " old-tag "$") (str " " new-tag)))))
  224. (defn- replace-old-page!
  225. "Unsanitized names"
  226. [content old-name new-name]
  227. (when (and (string? content) (string? old-name) (string? new-name))
  228. (-> content
  229. (replace-page-ref! old-name new-name)
  230. (replace-tag-ref! old-name new-name))))
  231. (defn- walk-replace-old-page!
  232. "Unsanitized names"
  233. [form old-name new-name]
  234. (walk/postwalk (fn [f]
  235. (cond
  236. (and (vector? f)
  237. (contains? #{"Search" "Label"} (first f))
  238. (string/starts-with? (second f) (str old-name "/")))
  239. [(first f) (string/replace-first (second f)
  240. (str old-name "/")
  241. (str new-name "/"))]
  242. (string? f)
  243. (if (= f old-name)
  244. new-name
  245. (replace-old-page! f old-name new-name))
  246. :else
  247. f))
  248. form))
  249. (defn favorited?
  250. [page-name]
  251. (let [favorites (->> (:favorites (state/get-config))
  252. (filter string?)
  253. (map string/lower-case)
  254. (set))]
  255. (contains? favorites page-name)))
  256. (defn favorite-page!
  257. [page-name]
  258. (when-not (string/blank? page-name)
  259. (let [favorites (->
  260. (cons
  261. page-name
  262. (or (:favorites (state/get-config)) []))
  263. (distinct)
  264. (vec))]
  265. (config-handler/set-config! :favorites favorites))))
  266. (defn unfavorite-page!
  267. [page-name]
  268. (when-not (string/blank? page-name)
  269. (let [favorites (->> (:favorites (state/get-config))
  270. (remove #(= (string/lower-case %) (string/lower-case page-name)))
  271. (vec))]
  272. (config-handler/set-config! :favorites favorites))))
  273. (defn toggle-favorite! []
  274. ;; NOTE: in journals or settings, current-page is nil
  275. (when-let [page-name (state/get-current-page)]
  276. (let [favorites (:favorites (state/sub-graph-config))
  277. favorited? (contains? (set (map string/lower-case favorites))
  278. (string/lower-case page-name))]
  279. (if favorited?
  280. (unfavorite-page! page-name)
  281. (favorite-page! page-name)))))
  282. (defn delete!
  283. [page-name ok-handler & {:keys [delete-file?]
  284. :or {delete-file? true}}]
  285. (when page-name
  286. (when-let [repo (state/get-current-repo)]
  287. (let [page-name (util/page-name-sanity-lc page-name)
  288. blocks (db/get-page-blocks-no-cache page-name)
  289. tx-data (mapv
  290. (fn [block]
  291. [:db.fn/retractEntity [:block/uuid (:block/uuid block)]])
  292. blocks)
  293. page (db/entity [:block/name page-name])]
  294. (db/transact! tx-data)
  295. (delete-file! repo page-name delete-file?)
  296. ;; if other page alias this pagename,
  297. ;; then just remove some attrs of this entity instead of retractEntity
  298. (when-not (:block/_namespace page)
  299. (if (model/get-alias-source-page (state/get-current-repo) page-name)
  300. (when-let [id (:db/id (db/entity [:block/name page-name]))]
  301. (let [txs (mapv (fn [attribute]
  302. [:db/retract id attribute])
  303. db-schema/retract-page-attributes)]
  304. (db/transact! txs)))
  305. (db/transact! [[:db.fn/retractEntity [:block/name page-name]]])))
  306. (unfavorite-page! page-name)
  307. (when (fn? ok-handler) (ok-handler))
  308. (ui-handler/re-render-root!)))))
  309. (defn- rename-update-block-refs!
  310. [refs from-id to-id]
  311. (->> refs
  312. (remove #{{:db/id from-id}})
  313. (cons {:db/id to-id})
  314. (distinct)
  315. (vec)))
  316. (defn- rename-update-refs!
  317. "Unsanitized only"
  318. [page old-original-name new-name]
  319. ;; update all pages which have references to this page
  320. (let [repo (state/get-current-repo)
  321. to-page (db/entity [:block/name (util/page-name-sanity-lc new-name)])
  322. blocks (db/get-page-referenced-blocks-no-cache (:db/id page))
  323. page-ids (->> (map :block/page blocks)
  324. (remove nil?)
  325. (set))
  326. tx (->> (map (fn [{:block/keys [uuid content properties] :as block}]
  327. (let [content (let [content' (replace-old-page! content old-original-name new-name)]
  328. (when-not (= content' content)
  329. content'))
  330. properties (let [properties' (walk-replace-old-page! properties old-original-name new-name)]
  331. (when-not (= properties' properties)
  332. properties'))]
  333. (when (or content properties)
  334. (util/remove-nils-non-nested
  335. {:block/uuid uuid
  336. :block/content content
  337. :block/properties properties
  338. :block/refs (rename-update-block-refs! (:block/refs block) (:db/id page) (:db/id to-page))
  339. :block/path-refs (rename-update-block-refs! (:block/path-refs block) (:db/id page) (:db/id to-page))})))) blocks)
  340. (remove nil?))]
  341. (db/transact! repo tx)
  342. (doseq [page-id page-ids]
  343. (outliner-file/sync-to-file page-id))))
  344. (defn- rename-page-aux
  345. "Only accepts unsanitized page names"
  346. [old-name new-name redirect?]
  347. (let [old-page-name (util/page-name-sanity-lc old-name)
  348. new-file-name (util/file-name-sanity new-name)
  349. new-page-name (util/page-name-sanity-lc new-name)
  350. repo (state/get-current-repo)
  351. page (db/pull [:block/name old-page-name])]
  352. (when (and repo page)
  353. (let [old-original-name (:block/original-name page)
  354. file (:block/file page)
  355. journal? (:block/journal? page)
  356. properties-block (:data (outliner-tree/-get-down (outliner-core/block page)))
  357. properties-block-tx (when (and properties-block
  358. (string/includes? (util/page-name-sanity-lc (:block/content properties-block))
  359. old-page-name))
  360. (let [front-matter? (and (property/front-matter? (:block/content properties-block))
  361. (= :markdown (:block/format properties-block)))]
  362. {:db/id (:db/id properties-block)
  363. :block/content (property/insert-property (:block/format properties-block)
  364. (:block/content properties-block)
  365. :title
  366. new-name
  367. front-matter?)}))
  368. page-txs [{:db/id (:db/id page)
  369. :block/uuid (:block/uuid page)
  370. :block/name new-page-name
  371. :block/original-name new-name}]
  372. page-txs (if properties-block-tx (conj page-txs properties-block-tx) page-txs)]
  373. (d/transact! (db/get-db repo false) page-txs)
  374. ;; If page name changed after sanitization
  375. (when (or (util/create-title-property? new-page-name)
  376. (not= (gp-util/page-name-sanity new-name false) new-name))
  377. (page-property/add-property! new-page-name :title new-name))
  378. (when (and file (not journal?))
  379. (rename-file! file new-file-name (fn [] nil)))
  380. (rename-update-refs! page old-original-name new-name)
  381. (outliner-file/sync-to-file page))
  382. ;; Redirect to the newly renamed page
  383. (when redirect?
  384. (route-handler/redirect! {:to :page
  385. :push false
  386. :path-params {:name new-page-name}}))
  387. (when (favorited? old-page-name)
  388. (p/do!
  389. (unfavorite-page! old-page-name)
  390. (favorite-page! new-page-name)))
  391. (recent-handler/update-or-add-renamed-page repo old-page-name new-page-name)
  392. (ui-handler/re-render-root!))))
  393. (defn- rename-nested-pages
  394. "Unsanitized names only"
  395. [old-ns-name new-ns-name]
  396. (let [repo (state/get-current-repo)
  397. nested-page-str (util/format "[[%s]]" (util/page-name-sanity-lc old-ns-name))
  398. ns-prefix (util/format "[[%s/" (util/page-name-sanity-lc old-ns-name))
  399. nested-pages (db/get-pages-by-name-partition repo nested-page-str)
  400. nested-pages-ns (db/get-pages-by-name-partition repo ns-prefix)]
  401. (when nested-pages
  402. ;; rename page "[[obsidian]] is a tool" to "[[logseq]] is a tool"
  403. (doseq [{:block/keys [name original-name]} nested-pages]
  404. (let [old-page-title (or original-name name)
  405. new-page-title (string/replace
  406. old-page-title
  407. (util/format "[[%s]]" old-ns-name)
  408. (util/format "[[%s]]" new-ns-name))]
  409. (when (and old-page-title new-page-title)
  410. (p/do!
  411. (rename-page-aux old-page-title new-page-title false)
  412. (println "Renamed " old-page-title " to " new-page-title))))))
  413. (when nested-pages-ns
  414. ;; rename page "[[obsidian/page1]] is a tool" to "[[logseq/page1]] is a tool"
  415. (doseq [{:block/keys [name original-name]} nested-pages-ns]
  416. (let [old-page-title (or original-name name)
  417. new-page-title (string/replace
  418. old-page-title
  419. (util/format "[[%s/" old-ns-name)
  420. (util/format "[[%s/" new-ns-name))]
  421. (when (and old-page-title new-page-title)
  422. (p/do!
  423. (rename-page-aux old-page-title new-page-title false)
  424. (println "Renamed " old-page-title " to " new-page-title))))))))
  425. (defn- rename-namespace-pages!
  426. "Original names (unsanitized only)"
  427. [repo old-name new-name]
  428. (let [pages (db/get-namespace-pages repo old-name)
  429. page (db/pull [:block/name (util/page-name-sanity-lc old-name)])
  430. pages (cons page pages)]
  431. (doseq [{:block/keys [name original-name]} pages]
  432. (let [old-page-title (or original-name name)
  433. new-page-title (string/replace old-page-title old-name new-name)
  434. redirect? (= name (:block/name page))]
  435. (when (and old-page-title new-page-title)
  436. (p/let [_ (rename-page-aux old-page-title new-page-title redirect?)]
  437. (println "Renamed " old-page-title " to " new-page-title)))))))
  438. (defn merge-pages!
  439. "Only accepts sanitized page names"
  440. [from-page-name to-page-name]
  441. (when (and (db/page-exists? from-page-name)
  442. (db/page-exists? to-page-name)
  443. (not= from-page-name to-page-name))
  444. (let [to-page (db/entity [:block/name to-page-name])
  445. to-id (:db/id to-page)
  446. from-page (db/entity [:block/name from-page-name])
  447. from-id (:db/id from-page)
  448. from-first-child (some->> (db/pull from-id)
  449. (outliner-core/block)
  450. (outliner-tree/-get-down)
  451. (outliner-core/get-data))
  452. to-last-direct-child-id (model/get-block-last-direct-child (db/get-db) to-id false)
  453. repo (state/get-current-repo)
  454. conn (conn/get-db repo false)
  455. datoms (d/datoms @conn :avet :block/page from-id)
  456. block-eids (mapv :e datoms)
  457. blocks (db-utils/pull-many repo '[:db/id :block/page :block/refs :block/path-refs :block/left :block/parent] block-eids)
  458. tx-data (map (fn [block]
  459. (let [id (:db/id block)]
  460. (cond->
  461. {:db/id id
  462. :block/page {:db/id to-id}
  463. :block/path-refs (rename-update-block-refs! (:block/path-refs block) from-id to-id)
  464. :block/refs (rename-update-block-refs! (:block/refs block) from-id to-id)}
  465. (and from-first-child (= id (:db/id from-first-child)))
  466. (assoc :block/left {:db/id (or to-last-direct-child-id to-id)})
  467. (= (:block/parent block) {:db/id from-id})
  468. (assoc :block/parent {:db/id to-id})))) blocks)]
  469. (d/transact! conn tx-data)
  470. (outliner-file/sync-to-file {:db/id to-id})
  471. (rename-update-refs! from-page
  472. (util/get-page-original-name from-page)
  473. (util/get-page-original-name to-page)))
  474. (delete! from-page-name nil)
  475. (route-handler/redirect! {:to :page
  476. :push false
  477. :path-params {:name to-page-name}})))
  478. (defn rename!
  479. "Accepts unsanitized page names"
  480. [old-name new-name]
  481. (let [repo (state/get-current-repo)
  482. old-name (string/trim old-name)
  483. new-name (string/trim new-name)
  484. old-page-name (util/page-name-sanity-lc old-name)
  485. new-page-name (util/page-name-sanity-lc new-name)
  486. name-changed? (not= old-name new-name)]
  487. (if (and old-name
  488. new-name
  489. (not (string/blank? new-name))
  490. name-changed?)
  491. (do
  492. (cond
  493. (= old-page-name new-page-name)
  494. (rename-page-aux old-name new-name true)
  495. (db/pull [:block/name new-page-name])
  496. (merge-pages! old-page-name new-page-name)
  497. :else
  498. (rename-namespace-pages! repo old-name new-name))
  499. (rename-nested-pages old-name new-name))
  500. (when (string/blank? new-name)
  501. (notification/show! "Please use a valid name, empty name is not allowed!" :error)))))
  502. (defn- split-col-by-element
  503. [col element]
  504. (let [col (vec col)
  505. idx (.indexOf col element)]
  506. [(subvec col 0 (inc idx))
  507. (subvec col (inc idx))]))
  508. (defn reorder-favorites!
  509. [{:keys [to up?]}]
  510. (let [favorites (:favorites (state/get-config))
  511. from (get @state/state :favorites/dragging)]
  512. (when (and from to (not= from to))
  513. (let [[prev next] (split-col-by-element favorites to)
  514. [prev next] (mapv #(remove (fn [e] (= from e)) %) [prev next])
  515. favorites (->>
  516. (if up?
  517. (concat (drop-last prev) [from (last prev)] next)
  518. (concat prev [from] next))
  519. (remove nil?)
  520. (distinct))]
  521. (config-handler/set-config! :favorites favorites)))))
  522. (defn has-more-journals?
  523. []
  524. (let [current-length (:journals-length @state/state)]
  525. (< current-length (db/get-journals-length))))
  526. (defn load-more-journals!
  527. []
  528. (when (has-more-journals?)
  529. (state/set-journals-length! (+ (:journals-length @state/state) 7))))
  530. (defn update-public-attribute!
  531. [page-name value]
  532. (page-property/add-property! page-name :public value))
  533. (defn get-page-ref-text
  534. [page]
  535. (let [edit-block-file-path (model/get-block-file-path (state/get-edit-block))
  536. page-name (string/lower-case page)]
  537. (if (and edit-block-file-path
  538. (state/org-mode-file-link? (state/get-current-repo)))
  539. (if-let [ref-file-path (:file/path (db/get-page-file page-name))]
  540. (util/format "[[file:%s][%s]]"
  541. (util/get-relative-path edit-block-file-path ref-file-path)
  542. page)
  543. (let [journal? (date/valid-journal-title? page)
  544. ref-file-path (str
  545. (if (or (util/electron?) (mobile-util/native-platform?))
  546. (-> (config/get-repo-dir (state/get-current-repo))
  547. js/decodeURI
  548. (string/replace #"/+$" "")
  549. (str "/"))
  550. "")
  551. (get-directory journal?)
  552. "/"
  553. (get-file-name journal? page)
  554. ".org")]
  555. (create! page {:redirect? false})
  556. (util/format "[[file:%s][%s]]"
  557. (util/get-relative-path edit-block-file-path ref-file-path)
  558. page)))
  559. (util/format "[[%s]]" page))))
  560. (defn init-commands!
  561. []
  562. (commands/init-commands! get-page-ref-text))
  563. (def rebuild-slash-commands-list!
  564. (debounce init-commands! 1500))
  565. (defn template-exists?
  566. [title]
  567. (when title
  568. (let [templates (keys (db/get-all-templates))]
  569. (when (seq templates)
  570. (let [templates (map string/lower-case templates)]
  571. (contains? (set templates) (string/lower-case title)))))))
  572. (defn ls-dir-files!
  573. [ok-handler]
  574. (web-nfs/ls-dir-files-with-handler!
  575. (fn []
  576. (init-commands!)
  577. (when ok-handler (ok-handler)))))
  578. (defn get-all-pages
  579. [repo]
  580. (->> (db/get-all-pages repo)
  581. (remove (fn [p]
  582. (let [name (:block/name p)]
  583. (or (util/uuid-string? name)
  584. (gp-config/draw? name)
  585. (db/built-in-pages-names (string/upper-case name))))))
  586. (common-handler/fix-pages-timestamps)))
  587. (defn get-filters
  588. [page-name]
  589. (let [properties (db/get-page-properties page-name)
  590. properties-str (get properties :filters "{}")]
  591. (try (reader/read-string properties-str)
  592. (catch js/Error e
  593. (log/error :syntax/filters e)))))
  594. (defn save-filter!
  595. [page-name filter-state]
  596. (page-property/add-property! page-name :filters filter-state))
  597. ;; Editor
  598. (defn page-not-exists-handler
  599. [input id q current-pos]
  600. (state/set-editor-show-page-search! false)
  601. (if (state/org-mode-file-link? (state/get-current-repo))
  602. (let [page-ref-text (get-page-ref-text q)
  603. value (gobj/get input "value")
  604. old-page-ref (util/format "[[%s]]" q)
  605. new-value (string/replace value
  606. old-page-ref
  607. page-ref-text)]
  608. (state/set-edit-content! id new-value)
  609. (let [new-pos (+ current-pos
  610. (- (count page-ref-text)
  611. (count old-page-ref))
  612. 2)]
  613. (cursor/move-cursor-to input new-pos)))
  614. (let [current-selected (util/get-selected-text)]
  615. (cursor/move-cursor-forward input (+ 2 (count current-selected))))))
  616. (defn on-chosen-handler
  617. [input id _q pos format]
  618. (let [current-pos (cursor/pos input)
  619. edit-content (state/sub [:editor/content id])
  620. q (or
  621. @editor-handler/*selected-text
  622. (when (state/sub :editor/show-page-search-hashtag?)
  623. (gp-util/safe-subs edit-content pos current-pos))
  624. (when (> (count edit-content) current-pos)
  625. (gp-util/safe-subs edit-content pos current-pos)))]
  626. (if (state/sub :editor/show-page-search-hashtag?)
  627. (fn [chosen _click?]
  628. (state/set-editor-show-page-search! false)
  629. (let [wrapped? (= "[[" (gp-util/safe-subs edit-content (- pos 2) pos))
  630. chosen (if (string/starts-with? chosen "New page: ") ;; FIXME: What if a page named "New page: XXX"?
  631. (subs chosen 10)
  632. chosen)
  633. chosen (if (and (util/safe-re-find #"\s+" chosen) (not wrapped?))
  634. (util/format "[[%s]]" chosen)
  635. chosen)
  636. q (if @editor-handler/*selected-text "" q)
  637. [last-pattern forward-pos] (if wrapped?
  638. [q 3]
  639. (if (= \# (first q))
  640. [(subs q 1) 1]
  641. [q 2]))
  642. last-pattern (str "#" (when wrapped? "[[") last-pattern)]
  643. (editor-handler/insert-command! id
  644. (str "#" (when wrapped? "[[") chosen)
  645. format
  646. {:last-pattern last-pattern
  647. :end-pattern (when wrapped? "]]")
  648. :forward-pos forward-pos})))
  649. (fn [chosen _click?]
  650. (state/set-editor-show-page-search! false)
  651. (let [chosen (if (string/starts-with? chosen "New page: ")
  652. (subs chosen 10)
  653. chosen)
  654. page-ref-text (get-page-ref-text chosen)]
  655. (editor-handler/insert-command! id
  656. page-ref-text
  657. format
  658. {:last-pattern (str "[[" (if @editor-handler/*selected-text "" q))
  659. :end-pattern "]]"
  660. :postfix-fn (fn [s] (util/replace-first "]]" s ""))
  661. :forward-pos 3}))))))
  662. (defn create-today-journal!
  663. []
  664. (when-let [repo (state/get-current-repo)]
  665. (when (and (state/enable-journals? repo)
  666. (not (state/loading-files? repo)))
  667. (state/set-today! (date/today))
  668. (when (or (config/local-db? repo)
  669. (and (= "local" repo) (not (mobile-util/native-platform?))))
  670. (let [title (date/today)
  671. today-page (util/page-name-sanity-lc title)
  672. format (state/get-preferred-format repo)
  673. file-name (date/journal-title->default title)
  674. path (str (config/get-journals-directory) "/" file-name "."
  675. (config/get-file-extension format))
  676. file-path (str "/" path)
  677. repo-dir (config/get-repo-dir repo)
  678. template (state/get-default-journal-template)]
  679. (p/let [file-exists? (fs/file-exists? repo-dir file-path)
  680. file-content (when file-exists?
  681. (fs/read-file repo-dir file-path))]
  682. (when (and (db/page-empty? repo today-page)
  683. (or (not file-exists?)
  684. (and file-exists? (string/blank? file-content))))
  685. (create! title {:redirect? false
  686. :split-namespace? false
  687. :create-first-block? (not template)
  688. :journal? true})
  689. (state/pub-event! [:journal/insert-template today-page])
  690. (ui-handler/re-render-root!))))))))
  691. (defn open-today-in-sidebar
  692. []
  693. (when-let [page (db/entity [:block/name (util/page-name-sanity-lc (date/today))])]
  694. (state/sidebar-add-block!
  695. (state/get-current-repo)
  696. (:db/id page)
  697. :page)))
  698. (defn open-file-in-default-app []
  699. (when-let [file-path (and (util/electron?) (get-page-file-path))]
  700. (js/window.apis.openPath file-path)))
  701. (defn copy-current-file []
  702. (when-let [file-path (and (util/electron?) (get-page-file-path))]
  703. (util/copy-to-clipboard! file-path)))
  704. (defn open-file-in-directory []
  705. (when-let [file-path (and (util/electron?) (get-page-file-path))]
  706. (js/window.apis.showItemInFolder file-path)))