editor.cljs 87 KB

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