editor.cljs 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  1. (ns frontend.handler.editor
  2. (:require [frontend.state :as state]
  3. [frontend.handler.common :as common-handler]
  4. [frontend.handler.route :as route-handler]
  5. [frontend.handler.git :as git-handler]
  6. [frontend.handler.ui :as ui-handler]
  7. [frontend.handler.repo :as repo-handler]
  8. [frontend.handler.notification :as notification]
  9. [frontend.handler.draw :as draw]
  10. [frontend.handler.expand :as expand]
  11. [frontend.format :as format]
  12. [frontend.format.block :as block]
  13. [frontend.image :as image]
  14. [cljs-time.core :as t]
  15. [cljs-time.coerce :as tc]
  16. [frontend.db :as db]
  17. [goog.object :as gobj]
  18. [goog.dom :as gdom]
  19. [goog.dom.classes :as gdom-classes]
  20. [clojure.string :as string]
  21. [frontend.util :as util :refer-macros [profile]]
  22. [frontend.config :as config]
  23. [dommy.core :as dom]
  24. [frontend.utf8 :as utf8]
  25. [frontend.fs :as fs]
  26. [promesa.core :as p]
  27. [dommy.core :as d]
  28. [frontend.diff :as diff]
  29. [frontend.search :as search]
  30. [frontend.handler.image :as image-handler]
  31. [frontend.commands :as commands
  32. :refer [*show-commands
  33. *matched-commands
  34. *slash-caret-pos
  35. *angle-bracket-caret-pos
  36. *matched-block-commands
  37. *show-block-commands]]
  38. [frontend.extensions.html-parser :as html-parser]
  39. [medley.core :as medley]
  40. [frontend.text :as text]
  41. [frontend.date :as date]
  42. [frontend.handler.repeated :as repeated]
  43. [clojure.core.async :as async]))
  44. ;; FIXME: should support multiple images concurrently uploading
  45. (defonce *image-uploading? (atom false))
  46. (defonce *image-uploading-process (atom 0))
  47. (defonce *selected-text (atom nil))
  48. (defn modified-time-tx
  49. [page file]
  50. (let [modified-at (tc/to-long (t/now))]
  51. [[:db/add (:db/id page) :page/last-modified-at modified-at]
  52. [:db/add (:db/id file) :file/last-modified-at modified-at]]))
  53. (defn- get-selection-and-format
  54. []
  55. (when-let [block (state/get-edit-block)]
  56. (when-let [id (:block/uuid block)]
  57. (when-let [edit-id (state/get-edit-input-id)]
  58. (when-let [input (gdom/getElement edit-id)]
  59. {:selection-start (gobj/get input "selectionStart")
  60. :selection-end (gobj/get input "selectionEnd")
  61. :format (:block/format block)
  62. :value (gobj/get input "value")
  63. :block block
  64. :edit-id edit-id
  65. :input input})))))
  66. (defn- format-text!
  67. [pattern-fn]
  68. (when-let [m (get-selection-and-format)]
  69. (let [{:keys [selection-start selection-end format value block edit-id input]} m
  70. empty-selection? (= selection-start selection-end)
  71. pattern (pattern-fn format)
  72. new-value (str
  73. (subs value 0 selection-start)
  74. pattern
  75. (subs value selection-start selection-end)
  76. pattern
  77. (subs value selection-end))]
  78. (state/set-edit-content! edit-id new-value)
  79. (when empty-selection?
  80. (util/cursor-move-back input (count pattern))))))
  81. (defn bold-format! []
  82. (format-text! config/get-bold))
  83. (defn italics-format! []
  84. (format-text! config/get-italic))
  85. (defn highlight-format! []
  86. (format-text! config/get-highlight))
  87. (defn html-link-format! []
  88. (when-let [m (get-selection-and-format)]
  89. (let [{:keys [selection-start selection-end format value block edit-id input]} m
  90. cur-pos (:pos (util/get-caret-pos input))
  91. empty-selection? (= selection-start selection-end)
  92. selection (subs value selection-start selection-end)
  93. selection-link? (and selection (or (util/starts-with? selection "http://")
  94. (util/starts-with? selection "https://")))
  95. [content forward-pos] (cond
  96. empty-selection?
  97. (config/get-empty-link-and-forward-pos format)
  98. selection-link?
  99. (config/with-default-link format selection)
  100. :else
  101. (config/with-default-label format selection))
  102. new-value (str
  103. (subs value 0 selection-start)
  104. content
  105. (subs value selection-end))
  106. cur-pos (or selection-start cur-pos)]
  107. (state/set-edit-content! edit-id new-value)
  108. (util/move-cursor-to input (+ cur-pos forward-pos)))))
  109. (defn focus-on-block!
  110. [block-id]
  111. (when block-id
  112. (route-handler/redirect! {:to :page
  113. :path-params {:name (str block-id)}})))
  114. (defn open-block-in-sidebar!
  115. [block-id]
  116. (when block-id
  117. (when-let [block (db/pull [:block/uuid block-id])]
  118. (state/sidebar-add-block!
  119. (state/get-current-repo)
  120. (:db/id block)
  121. :block
  122. block))))
  123. (defn reset-cursor-range!
  124. [node]
  125. (when node
  126. (state/set-cursor-range! (util/caret-range node))))
  127. (defn restore-cursor-pos!
  128. ([id markup]
  129. (restore-cursor-pos! id markup false))
  130. ([id markup dummy?]
  131. (when-let [node (gdom/getElement (str id))]
  132. (when-let [cursor-range (state/get-cursor-range)]
  133. (when-let [range (string/trim cursor-range)]
  134. (let [pos (inc (diff/find-position markup range))]
  135. (util/set-caret-pos! node pos)))))))
  136. (defn block-content-join-newlines
  137. [prefix value postfix]
  138. (str
  139. (if (or (= "" prefix)
  140. (= "\n" (last prefix)))
  141. ""
  142. "\n")
  143. (string/trim value)
  144. (if (= "\n" (first postfix))
  145. ""
  146. "\n")))
  147. (defn new-file-content
  148. [{:block/keys [content meta dummy?] :as block} file-content value]
  149. (let [utf8-content (utf8/encode file-content)
  150. prefix (utf8/substring utf8-content 0 (:start-pos meta))
  151. postfix (let [end-pos (if dummy?
  152. (:start-pos meta)
  153. (:end-pos meta))]
  154. (utf8/substring utf8-content end-pos))
  155. value (block-content-join-newlines prefix value postfix)]
  156. [(str prefix value postfix)
  157. value]))
  158. (defn get-block-new-value
  159. [{:block/keys [content meta dummy?] :as block} file-content value]
  160. (let [utf8-content (utf8/encode file-content)
  161. prefix (utf8/substring utf8-content 0 (:start-pos meta))
  162. postfix (let [end-pos (if dummy?
  163. (:start-pos meta)
  164. (:end-pos meta))]
  165. (utf8/substring utf8-content end-pos))]
  166. (block-content-join-newlines prefix value postfix)))
  167. (defn new-file-content-indent-outdent
  168. [{:block/keys [content meta dummy?] :as block} file-content value block-with-children-content last-child-end-pos indent-left?]
  169. (let [utf8-content (utf8/encode file-content)
  170. prefix (utf8/substring utf8-content 0 (:start-pos meta))
  171. last-child-end-pos (if (some? indent-left?) last-child-end-pos nil)
  172. end-pos (or
  173. last-child-end-pos
  174. (if dummy?
  175. (:start-pos meta)
  176. (:end-pos meta)))
  177. postfix (utf8/substring utf8-content end-pos)
  178. block-children-value (block-content-join-newlines prefix block-with-children-content postfix)]
  179. (str prefix block-children-value postfix)))
  180. (defn- with-block-meta
  181. [repo block]
  182. (if (:block/dummy? block)
  183. (if-let [page-id (:db/id (:block/page block))]
  184. (let [page-name (:page/name (db/entity repo page-id))
  185. end-pos (db/get-block-page-end-pos repo page-name)]
  186. (assoc block :block/meta {:start-pos end-pos
  187. :end-pos end-pos}))
  188. block)
  189. (if-let [meta (:block/meta (db/entity repo [:block/uuid (:block/uuid block)]))]
  190. (assoc block :block/meta meta)
  191. block)))
  192. (defn highlight-block!
  193. [block-uuid]
  194. (let [blocks (array-seq (js/document.getElementsByClassName (str block-uuid)))]
  195. (doseq [block blocks]
  196. (dom/add-class! block "block-highlight"))))
  197. (defn unhighlight-block!
  198. []
  199. (let [blocks (some->> (array-seq (js/document.getElementsByClassName "block-highlight"))
  200. (repeat 2)
  201. (apply concat))]
  202. (doseq [block blocks]
  203. (gdom-classes/remove block "block-highlight"))))
  204. (defn rebuild-blocks-meta
  205. [start-pos blocks]
  206. (let [last-start-pos (atom start-pos)]
  207. (mapv
  208. (fn [{:block/keys [uuid meta] :as block}]
  209. (let [old-start-pos (:start-pos meta)
  210. old-end-pos (:end-pos meta)
  211. new-end-pos (if old-end-pos
  212. (+ @last-start-pos (- old-end-pos old-start-pos)))
  213. new-meta {:start-pos @last-start-pos
  214. :end-pos new-end-pos}]
  215. (reset! last-start-pos new-end-pos)
  216. {:block/uuid uuid
  217. :block/meta new-meta}))
  218. blocks)))
  219. (defn rebuild-after-blocks
  220. [repo file before-end-pos new-end-pos]
  221. (let [file-id (:db/id file)
  222. after-blocks (db/get-file-after-blocks-meta repo file-id before-end-pos)]
  223. (rebuild-blocks-meta new-end-pos after-blocks)))
  224. (defn rebuild-after-blocks-indent-outdent
  225. [repo file block before-end-pos new-end-pos indent-left?]
  226. (let [file-id (:db/id file)
  227. after-blocks (db/get-file-after-blocks-meta repo file-id before-end-pos true)
  228. last-start-pos (atom new-end-pos)
  229. block-level (:block/level block)
  230. next-leq-level? (atom false)
  231. format (:block/format block)
  232. block-and-children-content (atom (:block/content block))
  233. last-child-end-pos (atom before-end-pos)
  234. after-blocks (mapv
  235. (fn [{:block/keys [uuid meta level content] :as block}]
  236. (let [old-start-pos (:start-pos meta)
  237. old-end-pos (:end-pos meta)]
  238. (when (<= level block-level)
  239. (reset! next-leq-level? true))
  240. (let [[new-content offset] (cond
  241. (and (not @next-leq-level?) (true? indent-left?))
  242. [(subs content 1) -1]
  243. (and (not @next-leq-level?) (false? indent-left?))
  244. [(str (config/get-block-pattern format) content) 1]
  245. :else
  246. [nil 0])
  247. new-end-pos (if old-end-pos
  248. (+ @last-start-pos
  249. (- old-end-pos old-start-pos)
  250. offset))
  251. new-meta {:start-pos @last-start-pos
  252. :end-pos new-end-pos}]
  253. (reset! last-start-pos new-end-pos)
  254. (when-not @next-leq-level?
  255. (do
  256. (reset! block-and-children-content (str @block-and-children-content new-content))
  257. (reset! last-child-end-pos old-end-pos)))
  258. (cond->
  259. {:block/uuid uuid
  260. :block/meta new-meta}
  261. (and (some? indent-left?) (not @next-leq-level?))
  262. (assoc :block/level (if indent-left? (dec level) (inc level)))
  263. (and new-content (not @next-leq-level?))
  264. (assoc :block/content new-content)))))
  265. after-blocks)]
  266. [after-blocks @block-and-children-content @last-child-end-pos]))
  267. (defn compute-retract-refs
  268. "Computes old references to be retracted."
  269. [eid {:block/keys [ref-pages ref-blocks]} old-ref-pages old-ref-blocks]
  270. (let [ref-pages-id (map #(:db/id (db/get-page (:page/name %))) ref-pages)
  271. retracted-pages (reduce (fn [done current]
  272. (if (some #(= (:db/id current) %) ref-pages-id)
  273. done
  274. (conj done (:db/id current))))
  275. [] old-ref-pages)
  276. ref-blocks-id (map #(:db/id (db/get-page (str (last %)))) ref-blocks)
  277. retracted-blocks (reduce (fn [done current]
  278. (if (some #(= (:db/id current) %) ref-blocks-id)
  279. done
  280. (conj done (:db/id current))))
  281. [] old-ref-blocks)]
  282. ;; removes retracted pages and blocks
  283. (into
  284. (mapv (fn [ref] [:db/retract eid :block/ref-pages ref]) retracted-pages)
  285. (mapv (fn [ref] [:db/retract eid :block/ref-blocks ref]) retracted-blocks))))
  286. (defn- rebuild-block-content
  287. "We'll create an empty heading if the first parsed ast element is not a paragraph, definition list or some hiccup."
  288. [content format]
  289. (let [content-without-level-spaces (text/remove-level-spaces content format)
  290. first-block (-> content-without-level-spaces
  291. (format/to-edn format)
  292. ffirst)]
  293. (if (or (block/heading-block? first-block)
  294. (block/paragraph-block? first-block)
  295. (block/hiccup-block? first-block)
  296. (block/definition-list-block? first-block))
  297. content
  298. (text/append-newline-after-level-spaces content format))))
  299. (defn- get-edit-input-id-with-block-id
  300. [block-id]
  301. (when-let [first-block (util/get-first-block-by-id block-id)]
  302. (string/replace (gobj/get first-block "id")
  303. "ls-block"
  304. "edit-block")))
  305. ;; id: block dom id, "ls-block-counter-uuid"
  306. (defn edit-block!
  307. ([block pos format id]
  308. (edit-block! block pos format id nil))
  309. ([block pos format id {:keys [custom-content custom-properties tail-len]
  310. :or {tail-len 0}}]
  311. (when-not config/publishing?
  312. (when-let [block-id (:block/uuid block)]
  313. (let [edit-input-id (if (uuid? id)
  314. (get-edit-input-id-with-block-id id)
  315. (str (subs id 0 (- (count id) 36)) block-id))
  316. block (or
  317. block
  318. (db/pull [:block/uuid block-id])
  319. ;; dummy?
  320. {:block/uuid block-id
  321. :block/content ""})
  322. {:block/keys [content properties]} block
  323. content (or custom-content content)
  324. content (string/trim (text/remove-level-spaces content format))
  325. properties (or custom-properties properties)
  326. content (if (and (seq properties) (text/properties-hidden? properties))
  327. (text/remove-properties! content)
  328. content)
  329. content-length (count content)
  330. text-range (cond
  331. (and (> tail-len 0) (>= (count content) tail-len))
  332. (subs content 0 (- (count content) tail-len))
  333. (or (= :max pos) (<= content-length pos))
  334. content
  335. :else
  336. (subs content 0 pos))]
  337. (state/set-editing! edit-input-id content block text-range))))))
  338. (defn edit-last-block-for-new-page!
  339. [last-block pos]
  340. (when-let [first-block (util/get-first-block-by-id (:block/uuid last-block))]
  341. (edit-block!
  342. last-block
  343. pos
  344. (:block/format last-block)
  345. (string/replace (gobj/get first-block "id")
  346. "ls-block"
  347. "edit-block"))))
  348. (defn save-block-if-changed!
  349. ([block value]
  350. (save-block-if-changed! block value nil))
  351. ([{:block/keys [uuid content meta file page dummy? format repo pre-block? content ref-pages ref-blocks] :as block}
  352. value
  353. {:keys [indent-left? custom-properties rebuild-content?]
  354. :or {rebuild-content? true
  355. custom-properties nil}}]
  356. (let [value value
  357. repo (or repo (state/get-current-repo))
  358. e (db/entity repo [:block/uuid uuid])
  359. block (assoc (with-block-meta repo block)
  360. :block/properties (:block/properties e))
  361. format (or format (state/get-preferred-format))
  362. page (db/entity repo (:db/id page))
  363. [old-properties new-properties] (when pre-block?
  364. [(:page/properties (db/entity (:db/id page)))
  365. (db/parse-properties value format)])
  366. page-tags (when-let [tags (:tags new-properties)]
  367. (util/->tags tags))
  368. page-alias (when-let [alias (:alias new-properties)]
  369. (map
  370. (fn [alias]
  371. {:page/name (string/lower-case alias)})
  372. (remove #{(:page/name page)} alias)))
  373. permalink-changed? (when (and pre-block? (:permalink old-properties))
  374. (not= (:permalink old-properties)
  375. (:permalink new-properties)))
  376. value (if permalink-changed?
  377. (db/add-properties! format value {:old_permalink (:permalink old-properties)})
  378. value)
  379. new-properties (if permalink-changed?
  380. (assoc new-properties :old_permalink (:permalink old-properties))
  381. new-properties)
  382. value (cond
  383. (some? custom-properties)
  384. (text/re-construct-block-properties block value custom-properties)
  385. (and (seq (:block/properties block))
  386. (text/properties-hidden? (:block/properties block)))
  387. (text/re-construct-block-properties block value (:block/properties block))
  388. :else
  389. value)]
  390. (cond
  391. (not= (string/trim content) (string/trim value)) ; block content changed
  392. (let [file (db/entity repo (:db/id file))]
  393. (cond
  394. ;; Page was referenced but no related file
  395. ;; TODO: replace with handler.page/create!
  396. (and page (not file))
  397. (let [format (name format)
  398. title (string/capitalize (:page/name page))
  399. journal-page? (date/valid-journal-title? title)
  400. path (str
  401. (if journal-page?
  402. config/default-journals-directory
  403. (config/get-pages-directory))
  404. "/"
  405. (if journal-page?
  406. (date/journal-title->default title)
  407. (-> (:page/name page)
  408. (util/page-name-sanity))) "."
  409. (if (= format "markdown") "md" format))
  410. file-path (str "/" path)
  411. dir (util/get-repo-dir repo)]
  412. (p/let [exists? (fs/file-exists? dir file-path)]
  413. (if exists?
  414. (notification/show!
  415. [:p.content
  416. (util/format "File %s already exists!" file-path)]
  417. :error)
  418. ;; create the file
  419. (let [content (str (util/default-content-with-title format
  420. (or (:page/original-name page)
  421. (:page/name page)))
  422. (text/remove-level-spaces value (keyword format)))]
  423. (p/let [_ (fs/create-if-not-exists dir file-path content)]
  424. (db/reset-file! repo path content)
  425. (git-handler/git-add repo path)
  426. (ui-handler/re-render-root!)
  427. ;; Continue to edit the last block
  428. (let [blocks (db/get-page-blocks repo (:page/name page))
  429. last-block (last blocks)]
  430. (edit-last-block-for-new-page! last-block :max)))))))
  431. (and file page)
  432. (let [file (db/entity repo (:db/id file))
  433. file-path (:file/path file)
  434. format (format/get-format file-path)
  435. file-content (db/get-file repo file-path)
  436. value (get-block-new-value block file-content value)
  437. value (if rebuild-content?
  438. (rebuild-block-content value format)
  439. value)
  440. block (assoc block :block/content value)
  441. {:keys [blocks pages start-pos end-pos]} (if pre-block?
  442. (let [new-end-pos (utf8/length (utf8/encode value))]
  443. {:blocks [(assoc-in block [:block/meta :end-pos] new-end-pos)]
  444. :pages []
  445. :start-pos 0
  446. :end-pos new-end-pos})
  447. (block/parse-block block format))
  448. block-retracted-attrs (when-not pre-block?
  449. (when-let [id (:db/id block)]
  450. [[:db/retract id :block/priority]
  451. [:db/retract id :block/deadline]
  452. [:db/retract id :block/deadline-ast]
  453. [:db/retract id :block/scheduled]
  454. [:db/retract id :block/scheduled-ast]
  455. [:db/retract id :block/marker]
  456. [:db/retract id :block/tags]
  457. [:db/retract id :block/repeated?]]))
  458. [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?)
  459. retract-refs (compute-retract-refs (:db/id e) (first blocks) ref-pages ref-blocks)
  460. page-id (:db/id page)
  461. modified-time (let [modified-at (tc/to-long (t/now))]
  462. [[:db/add page-id :page/last-modified-at modified-at]
  463. [:db/add (:db/id file) :file/last-modified-at modified-at]])
  464. page-properties (when pre-block?
  465. (if (seq new-properties)
  466. [[:db/retract page-id :page/properties]
  467. {:db/id page-id
  468. :page/properties new-properties}]
  469. [[:db/retract page-id :page/properties]]))
  470. pages (if (seq page-tags)
  471. (let [tag-pages (map
  472. (fn [page]
  473. {:page/original-name page
  474. :page/name page})
  475. (map :tag/name page-tags))]
  476. (concat pages tag-pages))
  477. pages)
  478. page-tags (when (and pre-block? (seq page-tags))
  479. (if (seq page-tags)
  480. [[:db/retract page-id :page/tags]
  481. {:db/id page-id
  482. :page/tags page-tags}]
  483. [[:db/retract page-id :page/tags]]))
  484. page-alias (when (and pre-block? (seq page-alias))
  485. (if (seq page-alias)
  486. [[:db/retract page-id :page/alias]
  487. {:db/id page-id
  488. :page/alias page-alias}]
  489. [[:db/retract page-id :page/alias]]))]
  490. (profile
  491. "Save block: "
  492. (repo-handler/transact-react-and-alter-file!
  493. repo
  494. (concat
  495. pages
  496. block-retracted-attrs
  497. blocks
  498. retract-refs
  499. page-properties
  500. page-tags
  501. page-alias
  502. after-blocks
  503. modified-time)
  504. {:key :block/change
  505. :data (map (fn [block] (assoc block :block/page page)) blocks)}
  506. (let [new-content (new-file-content-indent-outdent block file-content value block-children-content new-end-pos indent-left?)]
  507. [[file-path new-content]])))
  508. (when (or (seq retract-refs) pre-block?)
  509. (ui-handler/re-render-root!))
  510. (repo-handler/push-if-auto-enabled! repo))
  511. :else
  512. nil))
  513. (seq (state/get-changed-files))
  514. (repo-handler/push-if-auto-enabled! repo)
  515. :else
  516. nil))))
  517. (defn insert-new-block-aux!
  518. [{:block/keys [uuid content meta file dummy? level repo page format properties collapsed?] :as block}
  519. value
  520. {:keys [create-new-block? ok-handler with-level? new-level current-page blocks-container-id]}]
  521. (let [value (or value "")
  522. block-page? (and current-page (util/uuid-string? current-page))
  523. block-self? (= uuid (and block-page? (medley/uuid current-page)))
  524. input (gdom/getElement (state/get-edit-input-id))
  525. pos (if new-level
  526. (dec (count value))
  527. (util/get-input-pos input))
  528. repo (or repo (state/get-current-repo))
  529. block-has-children? (seq (:block/children block))
  530. fst-block-text (subs value 0 pos)
  531. snd-block-text (string/triml (subs value pos))
  532. fst-block-text (string/trim (if with-level? fst-block-text (block/with-levels fst-block-text format block)))
  533. snd-block-text-level (cond
  534. new-level
  535. new-level
  536. (or block-self? block-has-children?)
  537. (inc level)
  538. :else
  539. level)
  540. snd-block-text (if (and snd-block-text
  541. (re-find (re-pattern (util/format "^[%s]+\\s+" (config/get-block-pattern format))) snd-block-text))
  542. snd-block-text
  543. (rebuild-block-content
  544. (str (config/default-empty-block format snd-block-text-level) " " snd-block-text)
  545. format))
  546. block (with-block-meta repo block)
  547. original-id (:block/uuid block)
  548. format (:block/format block)
  549. page (db/entity repo (:db/id page))
  550. file (db/entity repo (:db/id file))
  551. insert-block (fn [block file-path file-content]
  552. (let [value (if create-new-block?
  553. (str fst-block-text "\n" snd-block-text)
  554. value)
  555. value (text/re-construct-block-properties block value properties)
  556. value (rebuild-block-content value format)
  557. [new-content value] (new-file-content block file-content value)
  558. parse-result (block/parse-block (assoc block :block/content value) format)
  559. id-conflict? (some #(= original-id (:block/uuid %)) (next (:blocks parse-result)))
  560. {:keys [blocks pages start-pos end-pos]}
  561. (if id-conflict?
  562. (let [new-value (string/replace
  563. value
  564. (re-pattern (str "(?i):custom_id: " original-id))
  565. "")]
  566. (block/parse-block (assoc block :block/content new-value) format))
  567. parse-result)
  568. after-blocks (rebuild-after-blocks repo file (:end-pos meta) end-pos)
  569. transact-fn (fn []
  570. (repo-handler/transact-react-and-alter-file!
  571. repo
  572. (concat
  573. pages
  574. blocks
  575. after-blocks)
  576. {:key :block/change
  577. :data (map (fn [block] (assoc block :block/page page)) blocks)}
  578. [[file-path new-content]])
  579. (state/set-editor-op! nil))]
  580. ;; Replace with batch transactions
  581. (state/add-tx! transact-fn)
  582. (let [blocks (remove (fn [block]
  583. (nil? (:block/content block))) blocks)
  584. page-blocks-atom (db/get-page-blocks-cache-atom repo (:db/id page))
  585. first-block-id (:block/uuid (first blocks))
  586. [before-part after-part] (and page-blocks-atom
  587. (split-with
  588. #(not= first-block-id (:block/uuid %))
  589. @page-blocks-atom))
  590. after-part (rest after-part)
  591. blocks-container-id (and blocks-container-id
  592. (util/uuid-string? blocks-container-id)
  593. (medley/uuid blocks-container-id))]
  594. ;; update page blocks cache if exists
  595. (when page-blocks-atom
  596. (reset! page-blocks-atom (->> (concat before-part blocks after-part)
  597. (remove nil?))))
  598. ;; update block children cache if exists
  599. (when blocks-container-id
  600. (let [blocks-atom (db/get-block-blocks-cache-atom repo blocks-container-id)
  601. [before-part after-part] (and blocks-atom
  602. (split-with
  603. #(not= first-block-id (:block/uuid %))
  604. @blocks-atom))
  605. after-part (rest after-part)]
  606. (and blocks-atom
  607. (reset! blocks-atom (->> (concat before-part blocks after-part)
  608. (remove nil?))))))
  609. (when ok-handler
  610. (let [first-block (first blocks)
  611. last-block (last blocks)]
  612. (ok-handler [first-block last-block snd-block-text]))))))]
  613. (cond
  614. (and (not file) page)
  615. ;; TODO: replace with handler.page/create!
  616. (let [format (name format)
  617. title (string/capitalize (:page/name page))
  618. journal-page? (date/valid-journal-title? title)
  619. path (str
  620. (if journal-page?
  621. config/default-journals-directory
  622. (config/get-pages-directory))
  623. "/"
  624. (if journal-page?
  625. (date/journal-title->default title)
  626. (-> (:page/name page)
  627. (util/page-name-sanity)))
  628. "."
  629. (if (= format "markdown") "md" format))
  630. file-path (str "/" path)
  631. dir (util/get-repo-dir repo)]
  632. (p/let [exists? (fs/file-exists? dir file-path)]
  633. (if exists?
  634. (notification/show!
  635. [:p.content
  636. (util/format "File %s already exists!"
  637. file-path)]
  638. :error)
  639. ;; create the file
  640. (let [content (util/default-content-with-title format (or
  641. (:page/original-name page)
  642. (:page/name page)))]
  643. (p/let [_ (fs/create-if-not-exists dir file-path content)]
  644. (db/reset-file! repo path
  645. (str content
  646. (text/remove-level-spaces value (keyword format))
  647. "\n"
  648. snd-block-text))
  649. (git-handler/git-add repo path)
  650. (ui-handler/re-render-root!)
  651. ;; Continue to edit the last block
  652. (let [blocks (db/get-page-blocks repo (:page/name page))
  653. last-block (last blocks)]
  654. (edit-last-block-for-new-page! last-block 0))))))
  655. (state/set-editor-op! nil))
  656. file
  657. (let [file-path (:file/path file)
  658. file-content (db/get-file repo file-path)]
  659. (insert-block block file-path file-content))
  660. :else
  661. nil)))
  662. (defn clear-when-saved!
  663. []
  664. (state/set-editor-show-input nil)
  665. (state/set-editor-show-date-picker false)
  666. (state/set-editor-show-page-search false)
  667. (state/set-editor-show-block-search false)
  668. (state/set-editor-show-template-search false)
  669. (commands/restore-state true))
  670. (defn get-state
  671. [state]
  672. (let [[{:keys [on-hide block block-id block-parent-id dummy? format sidebar?]} id config] (:rum/args state)
  673. node (gdom/getElement id)
  674. value (gobj/get node "value")
  675. pos (gobj/get node "selectionStart")]
  676. {:config config
  677. :on-hide on-hide
  678. :dummy? dummy?
  679. :sidebar? sidebar?
  680. :format format
  681. :id id
  682. :block block
  683. :block-id block-id
  684. :block-parent-id block-parent-id
  685. :node node
  686. :value value
  687. :pos pos}))
  688. (defn- with-timetracking-properties
  689. [block value]
  690. (let [new-marker (first (re-find format/bare-marker-pattern (or value "")))
  691. new-marker (if new-marker (string/lower-case (string/trim new-marker)))
  692. properties (into {} (:block/properties block))]
  693. (if (and
  694. new-marker
  695. (not= new-marker (string/lower-case (or (:block/marker block) "")))
  696. (state/enable-timetracking?))
  697. (assoc properties new-marker (util/time-ms))
  698. properties)))
  699. (defn insert-new-block!
  700. [state]
  701. (when (and (not config/publishing?)
  702. ;; skip this operation if it's inserting
  703. (not= :insert (state/get-editor-op)))
  704. (state/set-editor-op! :insert)
  705. (let [{:keys [block value format id config]} (get-state state)
  706. block-id (:block/uuid block)
  707. block (or (db/pull [:block/uuid block-id])
  708. block)
  709. collapsed? (:block/collapsed? block)
  710. repo (or (:block/repo block) (state/get-current-repo))
  711. last-child (and collapsed?
  712. (last (db/get-block-and-children-no-cache repo (:block/uuid block))))
  713. last-child (when (not= (:block/uuid last-child)
  714. (:block/uuid block))
  715. last-child)
  716. new-block (or last-child block)
  717. new-value (if last-child (:block/content last-child) value)
  718. properties (with-timetracking-properties new-block new-value)]
  719. ;; save the current block and insert a new block
  720. (insert-new-block-aux!
  721. (assoc new-block :block/properties properties)
  722. new-value
  723. {:create-new-block? true
  724. :ok-handler
  725. (fn [[_first-block last-block _new-block-content]]
  726. (let [last-id (:block/uuid last-block)]
  727. (edit-block! last-block 0 format id)
  728. (clear-when-saved!)))
  729. :with-level? (if last-child true false)
  730. :new-level (and last-child (:block/level block))
  731. :blocks-container-id (:id config)
  732. :current-page (state/get-current-page)}))))
  733. (defn insert-new-block-without-save-previous!
  734. [config last-block]
  735. (let [format (:block/format last-block)
  736. id (:id config)
  737. new-level (if (util/uuid-string? id)
  738. (inc (:block/level (db/entity [:block/uuid (medley/uuid id)])))
  739. 2)]
  740. (insert-new-block-aux!
  741. last-block
  742. (:block/content last-block)
  743. {:create-new-block? true
  744. :ok-handler
  745. (fn [[_first-block last-block _new-block-content]]
  746. (js/setTimeout #(edit-last-block-for-new-page! last-block :max) 50))
  747. :with-level? true
  748. :new-level new-level
  749. :blocks-container-id (:id config)
  750. :current-page (state/get-current-page)})))
  751. (defn update-timestamps-content!
  752. [{:block/keys [repeated? scheduled-ast deadline-ast marker]} content]
  753. (if repeated?
  754. (let [content (some->> (filter repeated/repeated? [scheduled-ast deadline-ast])
  755. (map (fn [ts]
  756. [(repeated/timestamp->text ts)
  757. (repeated/next-timestamp-text ts)]))
  758. (reduce (fn [content [old new]]
  759. (string/replace content old new))
  760. content))]
  761. (when content
  762. (str (string/trimr content)
  763. "\n"
  764. (util/format "- %s -> DONE [%s]"
  765. marker
  766. (date/get-local-date-time-string)))))
  767. content))
  768. (defn- with-marker-time
  769. [block marker]
  770. (let [properties (:block/properties block)
  771. properties (into {} properties)]
  772. (if (state/enable-timetracking?)
  773. (assoc properties
  774. (string/lower-case marker)
  775. (util/time-ms))
  776. properties)))
  777. (defn check
  778. [{:block/keys [uuid marker content meta file dummy? repeated?] :as block}]
  779. (let [new-content (string/replace-first content marker "DONE")
  780. new-content (if repeated?
  781. (update-timestamps-content! block content)
  782. new-content)]
  783. (save-block-if-changed! block new-content
  784. {:custom-properties (with-marker-time block "DONE")})))
  785. (defn uncheck
  786. [{:block/keys [uuid marker content meta file dummy?] :as block}]
  787. (let [marker (if (= :now (state/get-preferred-workflow))
  788. "LATER"
  789. "TODO")
  790. new-content (string/replace-first content "DONE" marker)]
  791. (save-block-if-changed! block new-content
  792. {:custom-properties (with-marker-time block marker)})))
  793. (defn cycle-todo!
  794. []
  795. (when-let [block (state/get-edit-block)]
  796. (let [edit-input-id (state/get-edit-input-id)
  797. current-input (gdom/getElement edit-input-id)
  798. content (state/get-edit-content)
  799. [new-content marker] (cond
  800. (util/starts-with? content "TODO")
  801. [(string/replace-first content "TODO" "DOING") "DOING"]
  802. (util/starts-with? content "DOING")
  803. [(string/replace-first content "DOING" "DONE") "DONE"]
  804. (util/starts-with? content "LATER")
  805. [(string/replace-first content "LATER" "NOW") "NOW"]
  806. (util/starts-with? content "NOW")
  807. [(string/replace-first content "NOW" "DONE") "DONE"]
  808. (util/starts-with? content "DONE")
  809. [(string/replace-first content "DONE" "") nil]
  810. :else
  811. (let [marker (if (= :now (state/get-preferred-workflow))
  812. "LATER"
  813. "TODO")]
  814. [(str marker " " (string/triml content)) marker]))
  815. new-content (string/triml new-content)]
  816. (let [new-pos (commands/compute-pos-delta-when-change-marker
  817. current-input content new-content marker (util/get-input-pos current-input))]
  818. (state/set-edit-content! edit-input-id new-content)
  819. (util/set-caret-pos! current-input new-pos)))))
  820. (defn set-marker
  821. [{:block/keys [uuid marker content meta file dummy? properties] :as block} new-marker]
  822. (let [new-content (string/replace-first content marker new-marker)]
  823. (save-block-if-changed! block new-content
  824. (with-marker-time block marker))))
  825. (defn set-priority
  826. [{:block/keys [uuid marker priority content meta file dummy?] :as block} new-priority]
  827. (let [new-content (string/replace-first content
  828. (util/format "[#%s]" priority)
  829. (util/format "[#%s]" new-priority))]
  830. (save-block-if-changed! block new-content)))
  831. (defn- get-prev-block-non-collapsed
  832. [block]
  833. (let [id (gobj/get block "id")
  834. prefix (re-find #"ls-block-[\d]+" id)]
  835. (when-let [blocks (d/by-class "ls-block")]
  836. (when-let [index (.indexOf blocks block)]
  837. (loop [idx (dec index)]
  838. (when (>= idx 0)
  839. (let [block (nth blocks idx)
  840. collapsed? (= "none" (d/style block "display"))
  841. prefix-match? (util/starts-with? (gobj/get block "id") prefix)]
  842. (if (or collapsed?
  843. ;; might be embed blocks
  844. (not prefix-match?))
  845. (recur (dec idx))
  846. block))))))))
  847. (defn- get-next-block-non-collapsed
  848. [block]
  849. (let [id (gobj/get block "id")
  850. prefix (re-find #"ls-block-[\d]+" id)]
  851. (when-let [blocks (d/by-class "ls-block")]
  852. (when-let [index (.indexOf blocks block)]
  853. (loop [idx (inc index)]
  854. (when (>= (count blocks) idx)
  855. (when-let [block (util/nth-safe blocks idx)]
  856. (let [collapsed? (= "none" (d/style block "display"))
  857. prefix-match? (util/starts-with? (gobj/get block "id") prefix)]
  858. (if (or collapsed?
  859. ;; might be embed blocks
  860. (not prefix-match?))
  861. (recur (inc idx))
  862. block)))))))))
  863. (defn delete-block-aux!
  864. [{:block/keys [uuid meta content file repo ref-pages ref-blocks] :as block} dummy?]
  865. (when-not dummy?
  866. (let [repo (or repo (state/get-current-repo))
  867. block (db/pull repo '[*] [:block/uuid uuid])]
  868. (when block
  869. (let [file-path (:file/path (db/entity repo (:db/id file)))
  870. file-content (db/get-file repo file-path)
  871. after-blocks (rebuild-after-blocks repo file (:end-pos meta) (:start-pos meta))
  872. new-content (utf8/delete! file-content (:start-pos meta) (:end-pos meta))]
  873. (repo-handler/transact-react-and-alter-file!
  874. repo
  875. (concat
  876. [[:db.fn/retractEntity [:block/uuid uuid]]]
  877. after-blocks)
  878. {:key :block/change
  879. :data [block]}
  880. [[file-path new-content]])
  881. (when (or (seq ref-pages) (seq ref-blocks))
  882. (ui-handler/re-render-root!)))))))
  883. (defn delete-block!
  884. [state repo e]
  885. (let [{:keys [id block-id block-parent-id dummy? value pos format]} (get-state state)]
  886. (when block-id
  887. (when-let [page-id (:db/id (:block/page (db/entity [:block/uuid block-id])))]
  888. (let [page-blocks-count (db/get-page-blocks-count repo page-id)
  889. page (db/entity page-id)]
  890. (when (> page-blocks-count 1)
  891. (util/stop e)
  892. ;; delete block, edit previous block
  893. (let [block (db/pull [:block/uuid block-id])
  894. block-parent (gdom/getElement block-parent-id)
  895. sibling-block (get-prev-block-non-collapsed block-parent)]
  896. (delete-block-aux! block dummy?)
  897. (when (and repo sibling-block)
  898. (when-let [sibling-block-id (d/attr sibling-block "blockid")]
  899. (when-let [block (db/pull repo '[*] [:block/uuid (uuid sibling-block-id)])]
  900. (let [original-content (util/trim-safe (:block/content block))
  901. new-value (str original-content " " (string/triml value))
  902. tail-len (count (string/triml value))
  903. pos (max
  904. (if original-content
  905. (utf8/length (utf8/encode (text/remove-level-spaces original-content format)))
  906. 0)
  907. 0)]
  908. (edit-block! block pos format id
  909. {:custom-content new-value
  910. :tail-len tail-len}))))))))))))
  911. (defn delete-blocks!
  912. [repo block-uuids]
  913. (when (seq block-uuids)
  914. (let [current-page (state/get-current-page)
  915. top-block-id (and current-page
  916. (util/uuid-string? current-page)
  917. (medley/uuid current-page))
  918. top-block? (and top-block-id
  919. (= top-block-id (first block-uuids)))]
  920. (let [blocks (db/pull-many repo '[*] (mapv (fn [id]
  921. [:block/uuid id])
  922. block-uuids))
  923. page (db/entity repo (:db/id (:block/page (first blocks))))
  924. first-block (first blocks)
  925. last-block (last blocks)
  926. file (db/entity repo (:db/id (:block/file first-block)))
  927. file-path (:file/path file)
  928. file-content (db/get-file repo file-path)
  929. start-pos (:start-pos (:block/meta first-block))
  930. end-pos (:end-pos (:block/meta last-block))
  931. after-blocks (rebuild-after-blocks repo file end-pos start-pos)
  932. new-content (utf8/delete! file-content start-pos end-pos)
  933. retract-blocks-tx (mapv
  934. (fn [uuid]
  935. [:db.fn/retractEntity [:block/uuid uuid]])
  936. block-uuids)
  937. tx-data (concat
  938. retract-blocks-tx
  939. after-blocks
  940. [{:file/path file-path}])]
  941. (repo-handler/transact-react-and-alter-file!
  942. repo
  943. tx-data
  944. {:key :block/change
  945. :data blocks}
  946. [[file-path new-content]])
  947. (when top-block?
  948. (route-handler/redirect! {:to :page
  949. :path-params {:name (:page/name page)}})
  950. (ui-handler/re-render-root!))
  951. (repo-handler/push-if-auto-enabled! repo)))))
  952. (defn remove-block-property!
  953. [block-id key]
  954. (let [block-id (if (string? block-id) (uuid block-id) block-id)
  955. key (string/lower-case (str key))]
  956. (when-let [block (db/pull [:block/uuid block-id])]
  957. (let [{:block/keys [content properties]} block]
  958. (when (get properties key)
  959. (save-block-if-changed! block content
  960. {:custom-properties (dissoc properties key)}))))))
  961. (defn set-block-property!
  962. [block-id key value]
  963. (let [block-id (if (string? block-id) (uuid block-id) block-id)
  964. key (string/lower-case (str key))
  965. value (str value)]
  966. (when-let [block (db/pull [:block/uuid block-id])]
  967. (when-not (:block/pre-block? block)
  968. (let [{:block/keys [content properties]} block]
  969. (cond
  970. (and (get properties key)
  971. (= (string/trim (get properties key)) value))
  972. nil
  973. :else
  974. (let [properties (:block/properties block)
  975. properties' (if (seq properties)
  976. (assoc properties key value)
  977. {key value})]
  978. (save-block-if-changed! block content
  979. {:custom-properties properties'
  980. :rebuild-content? false}))))))))
  981. (defn set-block-timestamp!
  982. [block-id key value]
  983. (let [key (string/lower-case key)
  984. scheduled? (= key "scheduled")
  985. deadline? (= key "deadline")
  986. block-id (if (string? block-id) (uuid block-id) block-id)
  987. key (string/lower-case (str key))
  988. value (str value)]
  989. (when-let [block (db/pull [:block/uuid block-id])]
  990. (let [{:block/keys [content scheduled deadline format]} block
  991. content (or (when-let [edit-content (state/get-edit-content)]
  992. (block/with-levels edit-content format block))
  993. content)
  994. new-line (str (string/upper-case key) ": " value)
  995. new-content (cond
  996. ;; update
  997. (or
  998. (and deadline deadline?)
  999. (and scheduled scheduled?))
  1000. (let [lines (string/split-lines content)
  1001. body (map (fn [line]
  1002. (if (string/starts-with? (string/lower-case line) key)
  1003. new-line
  1004. line))
  1005. (rest lines))]
  1006. (->> (cons (first lines) body)
  1007. (string/join "\n")))
  1008. ;; insert
  1009. (or deadline? scheduled?)
  1010. (let [[title body] (if (string/includes? content "\n")
  1011. (util/split-first "\n" content)
  1012. [content ""])]
  1013. (str title "\n"
  1014. new-line
  1015. "\n" (util/trim-only-newlines body)))
  1016. :else
  1017. content)]
  1018. (when (not= content new-content)
  1019. (if-let [input-id (state/get-edit-input-id)]
  1020. (state/set-edit-content! input-id new-content)
  1021. (save-block-if-changed! block new-content)))))))
  1022. (defn copy-block-ref!
  1023. [block-id]
  1024. (let [block (db/entity [:block/uuid block-id])]
  1025. (when-not (:block/pre-block? block)
  1026. (set-block-property! block-id "custom_id" (str block-id))))
  1027. (util/copy-to-clipboard! (str block-id)))
  1028. (defn clear-selection!
  1029. [_e]
  1030. (when (state/in-selection-mode?)
  1031. (doseq [block (state/get-selection-blocks)]
  1032. (dom/remove-class! block "selected")
  1033. (dom/remove-class! block "noselect"))
  1034. (state/clear-selection!))
  1035. ;; (when e
  1036. ;; (when-not (util/input? (gobj/get e "target"))
  1037. ;; (util/clear-selection!)))
  1038. )
  1039. (defn clear-selection-blocks!
  1040. []
  1041. (when (state/in-selection-mode?)
  1042. (doseq [block (state/get-selection-blocks)]
  1043. (dom/remove-class! block "selected")
  1044. (dom/remove-class! block "noselect"))
  1045. (state/clear-selection-blocks!)))
  1046. (defn select-all-blocks!
  1047. []
  1048. (when-let [current-input-id (state/get-edit-input-id)]
  1049. (let [input (gdom/getElement current-input-id)
  1050. blocks-container (util/rec-get-blocks-container input)
  1051. blocks (dom/by-class blocks-container "ls-block")]
  1052. (doseq [block blocks]
  1053. (dom/add-class! block "selected noselect"))
  1054. (state/set-selection-blocks! blocks))))
  1055. (defn- get-selected-blocks-with-children
  1056. []
  1057. (when-let [blocks (seq (get @state/state :selection/blocks))]
  1058. (mapcat (fn [block]
  1059. (cons block
  1060. (array-seq (dom/by-class block "ls-block"))))
  1061. blocks)))
  1062. (defn copy-selection-blocks
  1063. []
  1064. (when-let [blocks (seq (get-selected-blocks-with-children))]
  1065. (let [repo (dom/attr (first blocks) "repo")
  1066. ids (->> (distinct (map #(when-let [id (dom/attr % "blockid")]
  1067. (uuid id)) blocks))
  1068. (remove nil?))
  1069. up? (state/selection-up?)
  1070. content (some->> (db/get-blocks-contents repo ids)
  1071. (map :block/content))
  1072. content (if (false? up?) (reverse content) content)
  1073. content (string/join "" content)]
  1074. (when-not (string/blank? content)
  1075. (common-handler/copy-to-clipboard-without-id-property! content)))))
  1076. (defn cut-selection-blocks
  1077. []
  1078. (copy-selection-blocks)
  1079. (when-let [blocks (seq (get-selected-blocks-with-children))]
  1080. (let [repo (dom/attr (first blocks) "repo")
  1081. ids (distinct (map #(uuid (dom/attr % "blockid")) blocks))]
  1082. (delete-blocks! repo ids))))
  1083. (defn- get-nearest-page
  1084. []
  1085. (when-let [block (state/get-edit-block)]
  1086. (when-let [id (:block/uuid block)]
  1087. (when-let [edit-id (state/get-edit-input-id)]
  1088. (when-let [input (gdom/getElement edit-id)]
  1089. (when-let [pos (util/get-input-pos input)]
  1090. (let [value (gobj/get input "value")
  1091. page-pattern #"\[\[([^\]]+)]]"
  1092. block-pattern #"\(\(([^\)]+)\)\)"
  1093. page-matches (util/re-pos page-pattern value)
  1094. block-matches (util/re-pos block-pattern value)
  1095. matches (->> (concat page-matches block-matches)
  1096. (remove nil?))
  1097. [_ page] (first (sort-by
  1098. (fn [[start-pos content]]
  1099. (let [end-pos (+ start-pos (count content))]
  1100. (cond
  1101. (< pos start-pos)
  1102. (- pos start-pos)
  1103. (> pos end-pos)
  1104. (- end-pos pos)
  1105. :else
  1106. 0)))
  1107. >
  1108. matches))]
  1109. (when page
  1110. (subs page 2 (- (count page) 2))))))))))
  1111. (defn follow-link-under-cursor!
  1112. []
  1113. (when-let [page (get-nearest-page)]
  1114. (let [page-name (string/lower-case page)]
  1115. (state/clear-edit!)
  1116. (route-handler/redirect! {:to :page
  1117. :path-params {:name page-name}}))))
  1118. (defn open-link-in-sidebar!
  1119. []
  1120. (when-let [page (get-nearest-page)]
  1121. (let [page-name (string/lower-case page)
  1122. block? (util/uuid-string? page-name)]
  1123. (when-let [page (db/get-page page-name)]
  1124. (if block?
  1125. (state/sidebar-add-block!
  1126. (state/get-current-repo)
  1127. (:db/id page)
  1128. :block
  1129. page)
  1130. (state/sidebar-add-block!
  1131. (state/get-current-repo)
  1132. (:db/id page)
  1133. :page
  1134. {:page page}))))))
  1135. (defn zoom-in! []
  1136. (if-let [block (state/get-edit-block)]
  1137. (when-let [id (:block/uuid block)]
  1138. (route-handler/redirect! {:to :page
  1139. :path-params {:name (str id)}}))
  1140. (js/window.history.forward)))
  1141. (defn zoom-out! []
  1142. (when-let [page (state/get-current-page)]
  1143. (let [block-id (and
  1144. (string? page)
  1145. (util/uuid-string? page)
  1146. (medley/uuid page))
  1147. repo (state/get-current-repo)
  1148. block-parent (db/get-block-parent repo block-id)]
  1149. (if-let [id (:block/uuid block-parent)]
  1150. (route-handler/redirect! {:to :page
  1151. :path-params {:name (str id)}})
  1152. (let [page-id (-> (db/entity [:block/uuid block-id])
  1153. :block/page
  1154. :db/id)]
  1155. (when-let [page-name (:page/name (db/entity repo page-id))]
  1156. (route-handler/redirect! {:to :page
  1157. :path-params {:name page-name}})))))))
  1158. (defn cut-block!
  1159. [block-id]
  1160. (when-let [block (db/pull [:block/uuid block-id])]
  1161. (let [content (:block/content block)]
  1162. (common-handler/copy-to-clipboard-without-id-property! content)
  1163. (delete-block-aux! block false))))
  1164. (defonce select-start-block-state (atom nil))
  1165. (defn clear-last-selected-block!
  1166. []
  1167. (let [first-block (state/pop-selection-block!)]
  1168. (dom/remove-class! first-block "selected")
  1169. (dom/remove-class! first-block "noselect")))
  1170. (defn input-start-or-end?
  1171. ([input]
  1172. (input-start-or-end? input nil))
  1173. ([input up?]
  1174. (let [value (gobj/get input "value")
  1175. start (gobj/get input "selectionStart")
  1176. end (gobj/get input "selectionEnd")]
  1177. (if (nil? up?)
  1178. (or (= start 0) (= end (count value)))
  1179. (or (and (= start 0) up?)
  1180. (and (= end (count value)) (not up?)))))))
  1181. (defn highlight-selection-area!
  1182. [end-block]
  1183. (when-let [start-block (:selection/start-block @state/state)]
  1184. (clear-selection-blocks!)
  1185. (let [blocks (util/get-nodes-between-two-nodes start-block end-block "ls-block")]
  1186. (doseq [block blocks]
  1187. (dom/add-class! block "selected noselect"))
  1188. (state/set-selection-blocks! blocks))))
  1189. (defn on-select-block
  1190. [state e up?]
  1191. (when (and
  1192. (gobj/get e "shiftKey")
  1193. (not (gobj/get e "altKey"))
  1194. (or (state/in-selection-mode?)
  1195. (when-let [input-id (state/get-edit-input-id)]
  1196. (when-let [input (gdom/getElement input-id)]
  1197. (input-start-or-end? input up?)))))
  1198. (state/clear-edit!)
  1199. (let [{:keys [id block-id block block-parent-id dummy? value pos format] :as block-state} @select-start-block-state
  1200. element (gdom/getElement block-parent-id)
  1201. selected-blocks (state/get-selection-blocks)
  1202. selected-blocks-count (count selected-blocks)
  1203. first-block (first selected-blocks)
  1204. selection-up? (state/selection-up?)]
  1205. (when block-id
  1206. (util/stop e)
  1207. (when-let [element (if-not (state/in-selection-mode?)
  1208. element
  1209. (let [f (if up? util/get-prev-block util/get-next-block)]
  1210. (f first-block)))]
  1211. (if (and (not (nil? selection-up?)) (not= up? selection-up?))
  1212. (cond
  1213. (>= selected-blocks-count 2) ; back to the start block
  1214. (do
  1215. (when (= 2 selected-blocks-count) (state/set-selection-up? nil))
  1216. (clear-last-selected-block!))
  1217. :else
  1218. nil)
  1219. (state/conj-selection-block! element up?)))))))
  1220. (defn save-block!
  1221. [{:keys [format block id repo dummy?] :as state} value]
  1222. (when (or (:db/id (db/entity repo [:block/uuid (:block/uuid block)]))
  1223. dummy?)
  1224. (let [value (text/remove-level-spaces value format true)
  1225. new-value (block/with-levels value format block)
  1226. properties (with-timetracking-properties block value)]
  1227. ;; FIXME: somehow frontend.components.editor's will-unmount event will loop forever
  1228. ;; maybe we shouldn't save the block/file in "will-unmount" event?
  1229. (save-block-if-changed! block new-value
  1230. {:custom-properties properties}))))
  1231. (defn on-up-down
  1232. [state e up?]
  1233. (let [{:keys [id block-id block block-parent-id dummy? value pos format] :as block-state} (get-state state)]
  1234. (if (gobj/get e "shiftKey")
  1235. (reset! select-start-block-state block-state)
  1236. (let [element (gdom/getElement id)
  1237. line-height (util/get-textarea-line-height element)]
  1238. (when (and block-id
  1239. (or (and up? (util/textarea-cursor-first-row? element line-height))
  1240. (and (not up?) (util/textarea-cursor-end-row? element line-height))))
  1241. (util/stop e)
  1242. (let [f (if up? get-prev-block-non-collapsed get-next-block-non-collapsed)
  1243. sibling-block (f (gdom/getElement block-parent-id))]
  1244. (when sibling-block
  1245. (when-let [sibling-block-id (d/attr sibling-block "blockid")]
  1246. (let [state (get-state state)
  1247. content (:block/content block)
  1248. value (:value state)]
  1249. (when (not= (-> content
  1250. (text/remove-level-spaces format)
  1251. text/remove-properties!
  1252. string/trim)
  1253. (string/trim value))
  1254. (save-block! state (:value state))))
  1255. (let [block (db/pull (state/get-current-repo) '[*] [:block/uuid (uuid sibling-block-id)])]
  1256. (edit-block! block pos format id))))))))))
  1257. (defn insert-command!
  1258. [id command-output format {:keys [restore?]
  1259. :or {restore? true}
  1260. :as option}]
  1261. (cond
  1262. ;; replace string
  1263. (string? command-output)
  1264. (commands/insert! id command-output option)
  1265. ;; steps
  1266. (vector? command-output)
  1267. (commands/handle-steps command-output format)
  1268. :else
  1269. nil)
  1270. (when restore?
  1271. (let [restore-slash-caret-pos? (if (= :editor/click-hidden-file-input
  1272. (ffirst command-output))
  1273. false
  1274. true)]
  1275. (commands/restore-state restore-slash-caret-pos?))))
  1276. (defn upload-image
  1277. [id files format uploading? drop?]
  1278. (image/upload
  1279. files
  1280. (fn [file file-name file-type]
  1281. (image-handler/request-presigned-url
  1282. file file-name file-type
  1283. uploading?
  1284. (fn [signed-url]
  1285. (insert-command! id
  1286. (util/format "[[%s][%s]]"
  1287. signed-url
  1288. file-name)
  1289. format
  1290. {:last-pattern (if drop? "" commands/slash)
  1291. :restore? false})
  1292. (reset! *image-uploading-process 0))
  1293. (fn [e]
  1294. (let [process (* (/ (gobj/get e "loaded")
  1295. (gobj/get e "total"))
  1296. 100)]
  1297. (reset! *image-uploading-process process)))))))
  1298. ;; Editor should track some useful information, like editor modes.
  1299. ;; For example:
  1300. ;; 1. Which file format is it, markdown or org mode?
  1301. ;; 2. Is it in the properties area? Then we can enable the ":" autopair
  1302. (def autopair-map
  1303. {"[" "]"
  1304. "{" "}"
  1305. "(" ")"
  1306. "`" "`"
  1307. "~" "~"
  1308. "*" "*"
  1309. ;; "_" "_"
  1310. ;; ":" ":" ; TODO: only properties editing and org mode tag
  1311. "^" "^"})
  1312. (def reversed-autopair-map
  1313. (zipmap (vals autopair-map)
  1314. (keys autopair-map)))
  1315. (def delete-map
  1316. (assoc autopair-map
  1317. "$" "$"
  1318. ":" ":"))
  1319. (def reversed-delete-map
  1320. (zipmap (vals delete-map)
  1321. (keys delete-map)))
  1322. (defn autopair
  1323. [input-id prefix format {:keys [restore?]
  1324. :or {restore? true}
  1325. :as option}]
  1326. (let [value (get autopair-map prefix)
  1327. selected (util/get-selected-text)
  1328. postfix (str selected value)
  1329. value (str prefix postfix)
  1330. input (gdom/getElement input-id)]
  1331. (when value
  1332. (when-not (string/blank? selected) (reset! *selected-text selected))
  1333. (let [[prefix pos] (commands/simple-replace! input-id value selected
  1334. {:backward-pos (count postfix)
  1335. :check-fn (fn [new-value prefix-pos]
  1336. (when (>= prefix-pos 0)
  1337. [(subs new-value prefix-pos (+ prefix-pos 2))
  1338. (+ prefix-pos 2)]))})]
  1339. (case prefix
  1340. "[["
  1341. (do
  1342. (commands/handle-step [:editor/search-page])
  1343. (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
  1344. "(("
  1345. (do
  1346. (commands/handle-step [:editor/search-block :reference])
  1347. (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
  1348. nil)))))
  1349. (defn surround-by?
  1350. [input before after]
  1351. (when input
  1352. (let [value (gobj/get input "value")
  1353. pos (util/get-input-pos input)
  1354. start-pos (if (= :start before) 0 (- pos (count before)))
  1355. end-pos (if (= :end after) (count value) (+ pos (count after)))]
  1356. (when (>= (count value) end-pos)
  1357. (= (cond
  1358. (and (= :end after) (= :start before))
  1359. ""
  1360. (= :end after)
  1361. before
  1362. (= :start before)
  1363. after
  1364. :else
  1365. (str before after))
  1366. (subs value start-pos end-pos))))))
  1367. (defn get-matched-pages
  1368. [q]
  1369. (let [block (state/get-edit-block)
  1370. editing-page (and block
  1371. (when-let [page-id (:db/id (:block/page block))]
  1372. (:page/name (db/entity page-id))))]
  1373. (let [pages (db/get-pages (state/get-current-repo))
  1374. pages (if editing-page
  1375. ;; To prevent self references
  1376. (remove (fn [p] (= (string/lower-case p) editing-page)) pages)
  1377. pages)]
  1378. (filter
  1379. (fn [page]
  1380. (string/index-of
  1381. (string/lower-case page)
  1382. (string/lower-case q)))
  1383. pages))))
  1384. (defn get-matched-blocks
  1385. [q]
  1386. ;; remove current block
  1387. (let [current-block (state/get-edit-block)]
  1388. (remove
  1389. (fn [h]
  1390. (= (:block/uuid current-block)
  1391. (:block/uuid h)))
  1392. (search/search q 21))))
  1393. (defn get-matched-templates
  1394. [q]
  1395. (search/template-search q))
  1396. (defn get-matched-commands
  1397. [input]
  1398. (try
  1399. (let [edit-content (gobj/get input "value")
  1400. pos (util/get-input-pos input)
  1401. last-slash-caret-pos (:pos @*slash-caret-pos)
  1402. last-command (and last-slash-caret-pos (subs edit-content last-slash-caret-pos pos))]
  1403. (when (> pos 0)
  1404. (or
  1405. (and (= \/ (util/nth-safe edit-content (dec pos)))
  1406. (commands/commands-map))
  1407. (and last-command
  1408. (commands/get-matched-commands last-command)))))
  1409. (catch js/Error e
  1410. nil)))
  1411. (defn get-matched-block-commands
  1412. [input]
  1413. (try
  1414. (let [edit-content (gobj/get input "value")
  1415. pos (util/get-input-pos input)
  1416. last-command (subs edit-content
  1417. (:pos @*angle-bracket-caret-pos)
  1418. pos)]
  1419. (when (> pos 0)
  1420. (or
  1421. (and (= \< (util/nth-safe edit-content (dec pos)))
  1422. (commands/block-commands-map))
  1423. (and last-command
  1424. (commands/get-matched-commands
  1425. last-command
  1426. (commands/block-commands-map))))))
  1427. (catch js/Error e
  1428. nil)))
  1429. (defn in-auto-complete?
  1430. [input]
  1431. (or @*show-commands
  1432. @*show-block-commands
  1433. (state/get-editor-show-input)
  1434. (state/get-editor-show-page-search)
  1435. (state/get-editor-show-block-search)
  1436. (state/get-editor-show-template-search)
  1437. (state/get-editor-show-date-picker)))
  1438. (defn get-previous-input-char
  1439. [input]
  1440. (when-let [pos (util/get-input-pos input)]
  1441. (let [value (gobj/get input "value")]
  1442. (when (and (>= (count value) pos)
  1443. (>= pos 1))
  1444. (util/nth-safe value (- pos 1))))))
  1445. (defn get-previous-input-chars
  1446. [input length]
  1447. (when-let [pos (util/get-input-pos input)]
  1448. (let [value (gobj/get input "value")]
  1449. (when (and (>= (count value) pos)
  1450. (>= pos 1))
  1451. (subs value (- pos length) pos)))))
  1452. (defn get-current-input-char
  1453. [input]
  1454. (when-let [pos (util/get-input-pos input)]
  1455. (let [value (gobj/get input "value")]
  1456. (when (and (>= (count value) (inc pos))
  1457. (>= pos 1))
  1458. (util/nth-safe value pos)))))
  1459. (defn- get-previous-block-level
  1460. [current-id]
  1461. (when-let [input (gdom/getElement current-id)]
  1462. (when-let [prev-block (util/get-prev-block input)]
  1463. (util/parse-int (d/attr prev-block "level")))))
  1464. (defn adjust-block-level!
  1465. [state direction]
  1466. (state/set-editor-op! :indent-outdent)
  1467. (let [{:keys [block block-parent-id value config]} (get-state state)
  1468. start-level (:start-level config)
  1469. format (:block/format block)
  1470. block-pattern (config/get-block-pattern format)
  1471. level (:block/level block)
  1472. previous-level (or (get-previous-block-level block-parent-id) 1)
  1473. [add? remove?] (case direction
  1474. :left [false true]
  1475. :right [true false]
  1476. [(<= level previous-level)
  1477. (and (> level previous-level)
  1478. (> level 2))])
  1479. final-level (cond
  1480. add? (inc level)
  1481. remove? (if (> level 2)
  1482. (dec level)
  1483. level)
  1484. :else level)
  1485. new-value (block/with-levels value format (assoc block :block/level final-level))]
  1486. (when (and
  1487. (not (and (= direction :left)
  1488. (and
  1489. (get config :id)
  1490. (util/uuid-string? (get config :id)))
  1491. (<= final-level start-level)))
  1492. (<= (- final-level previous-level) 1))
  1493. (save-block-if-changed! block new-value
  1494. {:indent-left? (= direction :left)})))
  1495. (state/set-editor-op! nil))
  1496. (defn adjust-blocks-level!
  1497. [blocks direction])
  1498. (defn append-paste-doc!
  1499. [format event]
  1500. (let [[html text] (util/get-clipboard-as-html event)]
  1501. (when-not (util/starts-with? (string/trim text) "http")
  1502. (let [doc-text (html-parser/parse format html)]
  1503. (when-not (string/blank? doc-text)
  1504. (util/stop event)
  1505. (state/append-current-edit-content! doc-text))))))
  1506. (defn- block-and-children-content
  1507. [block-children]
  1508. (-> (map :block/content block-children)
  1509. string/join))
  1510. (defn move-up-down
  1511. [e up?]
  1512. (when-let [block-id (:block/uuid (state/get-edit-block))]
  1513. (let [block-parent-id (state/get-editing-block-dom-id)
  1514. block (db/entity [:block/uuid block-id])
  1515. meta (:block/meta block)
  1516. page (:block/page block)
  1517. block-dom-node (gdom/getElement block-parent-id)
  1518. prev-block (get-prev-block-non-collapsed block-dom-node)
  1519. next-block (get-next-block-non-collapsed block-dom-node)
  1520. repo (state/get-current-repo)
  1521. move-upwards-to-parent? (and up? prev-block (< (d/attr prev-block "level") (:block/level block)))
  1522. move-down-to-higher-level? (and (not up?) next-block (< (d/attr next-block "level") (:block/level block)))]
  1523. (when-let [sibling-block (cond
  1524. move-upwards-to-parent?
  1525. prev-block
  1526. move-down-to-higher-level?
  1527. next-block
  1528. :else
  1529. (let [f (if up? util/get-prev-block-with-same-level util/get-next-block-with-same-level)]
  1530. (f block-dom-node)))]
  1531. (when-let [sibling-block-id (d/attr sibling-block "blockid")]
  1532. (when-let [sibling-block (db/pull-block (medley/uuid sibling-block-id))]
  1533. (let [sibling-meta (:block/meta sibling-block)
  1534. hc1 (db/get-block-and-children-no-cache repo (:block/uuid block))
  1535. hc2 (if (or move-upwards-to-parent? move-down-to-higher-level?)
  1536. [sibling-block]
  1537. (db/get-block-and-children-no-cache repo (:block/uuid sibling-block)))]
  1538. ;; Same page and next to the other
  1539. (when (and
  1540. (= (:db/id (:block/page block))
  1541. (:db/id (:block/page sibling-block)))
  1542. (or
  1543. (and up? (= (:end-pos (:block/meta (last hc2))) (:start-pos (:block/meta (first hc1)))))
  1544. (and (not up?) (= (:end-pos (:block/meta (last hc1))) (:start-pos (:block/meta (first hc2)))))))
  1545. (let [hc1-content (block-and-children-content hc1)
  1546. hc2-content (block-and-children-content hc2)
  1547. file (db/get-block-file (:block/uuid block))
  1548. file-path (:file/path file)
  1549. old-file-content (db/get-file file-path)
  1550. [start-pos end-pos new-content blocks] (if up?
  1551. [(:start-pos sibling-meta)
  1552. (get-in (last hc1) [:block/meta :end-pos])
  1553. (str hc1-content hc2-content)
  1554. (concat hc1 hc2)]
  1555. [(:start-pos meta)
  1556. (get-in (last hc2) [:block/meta :end-pos])
  1557. (str hc2-content hc1-content)
  1558. (concat hc2 hc1)])]
  1559. (when (and start-pos end-pos)
  1560. (let [new-file-content (utf8/insert! old-file-content start-pos end-pos new-content)
  1561. modified-time (modified-time-tx page file)
  1562. blocks-meta (rebuild-blocks-meta start-pos blocks)]
  1563. (profile
  1564. (str "Move block " (if up? "up: " "down: "))
  1565. (repo-handler/transact-react-and-alter-file!
  1566. repo
  1567. (concat
  1568. blocks-meta
  1569. modified-time)
  1570. {:key :block/change
  1571. :data (map (fn [block] (assoc block :block/page page)) blocks)}
  1572. [[file-path new-file-content]])))))))))))))
  1573. (defn expand!
  1574. []
  1575. (when-let [current-block (state/get-edit-block)]
  1576. (expand/expand! current-block)
  1577. (state/set-collapsed-state! (:block/uuid current-block)
  1578. false)))
  1579. (defn collapse!
  1580. []
  1581. (when-let [current-block (state/get-edit-block)]
  1582. (expand/collapse! current-block)
  1583. (state/set-collapsed-state! (:block/uuid current-block)
  1584. true)))
  1585. (defn cycle-collapse!
  1586. [_state e]
  1587. (when (and
  1588. ;; not input, t
  1589. (nil? (state/get-edit-input-id))
  1590. (not (state/get-editor-show-input))
  1591. (string/blank? (:search/q @state/state)))
  1592. (util/stop e)
  1593. (expand/cycle!)))
  1594. (defn on-tab
  1595. [direction]
  1596. (fn [state e]
  1597. (when-let [repo (state/get-current-repo)]
  1598. (let [blocks (seq (state/get-selection-blocks))]
  1599. (if (seq blocks)
  1600. (let [ids (map (fn [block] (when-let [id (dom/attr block "blockid")]
  1601. (medley/uuid id))) blocks)
  1602. ids (->> (mapcat #(let [children (vec (db/get-block-children-ids repo %))]
  1603. (cons % children)) ids)
  1604. (distinct))
  1605. blocks (db/pull-many '[*] (map (fn [id] [:block/uuid id]) ids))
  1606. block (first blocks)
  1607. format (:block/format block)
  1608. start-pos (get-in block [:block/meta :start-pos])
  1609. old-end-pos (get-in (last blocks) [:block/meta :end-pos])
  1610. pattern (config/get-block-pattern format)
  1611. last-start-pos (atom start-pos)
  1612. blocks (doall
  1613. (map (fn [block]
  1614. (let [content (:block/content block)
  1615. level (:block/level block)
  1616. content' (if (= :left direction)
  1617. (subs content 1)
  1618. (str pattern content))
  1619. end-pos (+ @last-start-pos (utf8/length (utf8/encode content')))
  1620. block (assoc block
  1621. :block/content content'
  1622. :block/level (if (= direction :left)
  1623. (dec level)
  1624. (inc level))
  1625. :block/meta (merge
  1626. (:block/meta block)
  1627. {:start-pos @last-start-pos
  1628. :end-pos end-pos}))]
  1629. (reset! last-start-pos end-pos)
  1630. block))
  1631. blocks))
  1632. file-id (:db/id (:block/file block))
  1633. file (db/entity file-id)
  1634. page (:block/page block)
  1635. after-blocks (rebuild-after-blocks repo file old-end-pos @last-start-pos)
  1636. ;; _ (prn {:blocks (map (fn [h] (select-keys h [:block/content :block/meta])) blocks)
  1637. ;; :after-blocks after-blocks
  1638. ;; :last-start-pos @last-start-pos})
  1639. file-path (:file/path file)
  1640. file-content (db/get-file file-path)
  1641. new-content (utf8/insert! file-content start-pos old-end-pos (apply str (map :block/content blocks)))
  1642. modified-time (modified-time-tx page file)]
  1643. (profile
  1644. "Indent/outdent: "
  1645. (repo-handler/transact-react-and-alter-file!
  1646. repo
  1647. (concat
  1648. blocks
  1649. after-blocks
  1650. modified-time)
  1651. {:key :block/change
  1652. :data (map (fn [block] (assoc block :block/page page)) blocks)}
  1653. [[file-path new-content]])))
  1654. (cycle-collapse! state e))))))
  1655. (defn bulk-make-todos
  1656. [state e]
  1657. (when-let [repo (state/get-current-repo)]
  1658. (let [blocks (seq (state/get-selection-blocks))]
  1659. (if (seq blocks)
  1660. (let [ids (map (fn [block] (when-let [id (dom/attr block "blockid")]
  1661. (medley/uuid id))) blocks)
  1662. ids (->> (mapcat #(let [children (vec (db/get-block-children-ids repo %))]
  1663. (cons % children)) ids)
  1664. (distinct))
  1665. blocks (db/pull-many '[*] (map (fn [id] [:block/uuid id]) ids))
  1666. block (first blocks)
  1667. format (:block/format block)
  1668. start-pos (get-in block [:block/meta :start-pos])
  1669. old-end-pos (get-in (last blocks) [:block/meta :end-pos])
  1670. pattern (config/get-block-pattern format)
  1671. last-start-pos (atom start-pos)
  1672. blocks (doall
  1673. (map (fn [block]
  1674. (let [content (:block/content block)
  1675. [prefix content] (if-let [col (util/split-first " " content)]
  1676. col
  1677. [content ""])
  1678. level (:block/level block)
  1679. new-marker (state/get-preferred-todo)
  1680. content' (string/replace-first content
  1681. format/marker-pattern
  1682. (str new-marker " "))
  1683. content' (str prefix " " content')
  1684. end-pos (+ @last-start-pos (utf8/length (utf8/encode content')))
  1685. block (assoc block
  1686. :block/marker new-marker
  1687. :block/content content'
  1688. :block/meta (merge
  1689. (:block/meta block)
  1690. {:start-pos @last-start-pos
  1691. :end-pos end-pos}))]
  1692. (reset! last-start-pos end-pos)
  1693. block))
  1694. blocks))
  1695. file-id (:db/id (:block/file block))
  1696. file (db/entity file-id)
  1697. page (:block/page block)
  1698. after-blocks (rebuild-after-blocks repo file old-end-pos @last-start-pos)
  1699. file-path (:file/path file)
  1700. file-content (db/get-file file-path)
  1701. new-content (utf8/insert! file-content start-pos old-end-pos (apply str (map :block/content blocks)))
  1702. modified-time (modified-time-tx page file)]
  1703. (profile
  1704. "Indent/outdent: "
  1705. (repo-handler/transact-react-and-alter-file!
  1706. repo
  1707. (concat
  1708. blocks
  1709. after-blocks
  1710. modified-time)
  1711. {:key :block/change
  1712. :data (map (fn [block] (assoc block :block/page page)) blocks)}
  1713. [[file-path new-content]])))
  1714. (cycle-collapse! state e)))))
  1715. (defn handle-command-input
  1716. [command id format m pos]
  1717. (case command
  1718. :link
  1719. (let [{:keys [link label]} m]
  1720. (if (and (string/blank? link)
  1721. (string/blank? label))
  1722. nil
  1723. (insert-command! id
  1724. (util/format "[[%s][%s]]"
  1725. (or link "")
  1726. (or label ""))
  1727. format
  1728. {:last-pattern (str commands/slash "link")})))
  1729. :draw
  1730. (when-not (string/blank? (:title m))
  1731. (let [file (draw/title->file-name (:title m))
  1732. value (util/format
  1733. "[[%s]]\n<iframe class=\"draw-iframe\" src=\"/draw?file=%s\" width=\"100%\" height=\"400\" frameborder=\"0\" allowfullscreen></iframe>"
  1734. file
  1735. file)]
  1736. (insert-command! id
  1737. value
  1738. format
  1739. {:last-pattern (str commands/slash "draw ")})
  1740. (draw/create-draw-with-default-content
  1741. file
  1742. (fn []
  1743. (let [input (gdom/getElement "download")]
  1744. (.click input))))))
  1745. nil)
  1746. (state/set-editor-show-input nil)
  1747. (when-let [saved-cursor (get @state/state :editor/last-saved-cursor)]
  1748. (when-let [input (gdom/getElement id)]
  1749. (.focus input)
  1750. (util/move-cursor-to input saved-cursor))))
  1751. (defn set-block-as-a-heading!
  1752. [block-id value]
  1753. (set-block-property! block-id "heading" value))
  1754. ;; Should preserve the cursor too.
  1755. (defn open-last-block!
  1756. [journal?]
  1757. (let [edit-id (state/get-edit-input-id)
  1758. last-pos (state/get-edit-pos)
  1759. block-id (when edit-id (subs edit-id (- (count edit-id) 36)))]
  1760. (let [last-edit-block (first (array-seq (js/document.getElementsByClassName block-id)))
  1761. first-block (first (array-seq (js/document.getElementsByClassName "ls-block")))
  1762. node (or last-edit-block
  1763. (and (not journal?) first-block))]
  1764. (when node
  1765. (let [block-id (and node (d/attr node "blockid"))
  1766. edit-block-id (string/replace (gobj/get node "id") "ls-block" "edit-block")
  1767. block-id (medley/uuid block-id)]
  1768. (when-let [block (db/entity [:block/uuid block-id])]
  1769. (edit-block! block
  1770. (or (and last-edit-block last-pos)
  1771. :max)
  1772. (:block/format block)
  1773. edit-block-id)))))))
  1774. (defn get-search-q
  1775. []
  1776. (when-let [id (state/get-edit-input-id)]
  1777. (when-let [input (gdom/getElement id)]
  1778. (let [current-pos (:pos (util/get-caret-pos input))
  1779. pos (:editor/last-saved-cursor @state/state)
  1780. edit-content (state/sub [:editor/content id])]
  1781. (or
  1782. @*selected-text
  1783. (util/safe-subs edit-content pos current-pos))))))
  1784. (defn close-autocomplete-if-outside
  1785. [input]
  1786. (when (or (state/get-editor-show-page-search)
  1787. (state/get-editor-show-page-search-hashtag)
  1788. (state/get-editor-show-block-search))
  1789. (when-let [q (get-search-q)]
  1790. (let [value (gobj/get input "value")
  1791. pos (:editor/last-saved-cursor @state/state)
  1792. current-pos (:pos (util/get-caret-pos input))]
  1793. (when (or (< current-pos pos)
  1794. (string/includes? q "]")
  1795. (string/includes? q ")"))
  1796. (state/set-editor-show-block-search false)
  1797. (state/set-editor-show-page-search false)
  1798. (state/set-editor-show-page-search-hashtag false))))))