editor.cljs 89 KB

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