editor.cljs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. (ns frontend.components.editor
  2. (:require [clojure.string :as string]
  3. [goog.string :as gstring]
  4. [frontend.commands :as commands
  5. :refer [*angle-bracket-caret-pos *first-command-group *matched-block-commands *matched-commands *show-block-commands *show-commands *slash-caret-pos]]
  6. [frontend.components.block :as block]
  7. [frontend.components.datetime :as datetime-comp]
  8. [frontend.components.search :as search]
  9. [frontend.components.svg :as svg]
  10. [frontend.mobile.camera :as mobile-camera]
  11. [frontend.mobile.util :as mobile-util]
  12. [frontend.config :as config]
  13. [frontend.db :as db]
  14. [frontend.db.model :as db-model]
  15. [frontend.extensions.zotero :as zotero]
  16. [frontend.handler.editor :as editor-handler :refer [get-state]]
  17. [frontend.handler.editor.lifecycle :as lifecycle]
  18. [frontend.handler.page :as page-handler]
  19. [frontend.mixins :as mixins]
  20. [frontend.modules.shortcut.core :as shortcut]
  21. [frontend.state :as state]
  22. [frontend.ui :as ui]
  23. [frontend.util :as util]
  24. [frontend.util.cursor :as cursor]
  25. [frontend.util.keycode :as keycode]
  26. [goog.dom :as gdom]
  27. [promesa.core :as p]
  28. [rum.core :as rum]
  29. [frontend.handler.history :as history]))
  30. (rum/defc commands < rum/reactive
  31. [id format]
  32. (let [matched (util/react *matched-commands)]
  33. (when (util/react *show-commands)
  34. (ui/auto-complete
  35. matched
  36. {:get-group-name
  37. (fn [item]
  38. (get *first-command-group (first item)))
  39. :item-render
  40. (fn [item]
  41. (let [command-name (first item)
  42. command-doc (get item 2)
  43. doc (when (state/show-command-doc?) command-doc)]
  44. (cond
  45. (string? doc)
  46. [:div {:title doc}
  47. command-name]
  48. (vector? doc)
  49. [:div.has-help
  50. command-name
  51. (ui/tippy
  52. {:html doc
  53. :interactive true
  54. :fixed-position? true
  55. :position "right"}
  56. [:small (svg/help-circle)])]
  57. :else
  58. [:div command-name])))
  59. :on-chosen
  60. (fn [chosen-item]
  61. (let [command (first chosen-item)]
  62. (reset! commands/*current-command command)
  63. (let [command-steps (get (into {} matched) command)
  64. restore-slash? (or
  65. (contains? #{"Today" "Yesterday" "Tomorrow" "Current time"} command)
  66. (and
  67. (not (fn? command-steps))
  68. (not (contains? (set (map first command-steps)) :editor/input))
  69. (not (contains? #{"Date picker" "Template" "Deadline" "Scheduled" "Upload an image"} command))))]
  70. (editor-handler/insert-command! id command-steps
  71. format
  72. {:restore? restore-slash?})
  73. (state/pub-event! [:instrument {:type :editor/command-triggered
  74. :payload {:command command}}]))))
  75. :class
  76. "black"}))))
  77. (rum/defc block-commands < rum/reactive
  78. [id format]
  79. (when (util/react *show-block-commands)
  80. (let [matched (util/react *matched-block-commands)]
  81. (ui/auto-complete
  82. (map first matched)
  83. {:on-chosen (fn [chosen]
  84. (editor-handler/insert-command! id (get (into {} matched) chosen)
  85. format
  86. {:last-pattern commands/angle-bracket}))
  87. :class "black"}))))
  88. (defn- in-sidebar? [el]
  89. (not (.contains (.getElementById js/document "left-container") el)))
  90. (rum/defc page-search < rum/reactive
  91. {:will-unmount (fn [state] (reset! editor-handler/*selected-text nil) state)}
  92. "Embedded page searching popup"
  93. [id format]
  94. (when (state/sub :editor/show-page-search?)
  95. (let [pos (:editor/last-saved-cursor @state/state)
  96. input (gdom/getElement id)]
  97. (when input
  98. (let [current-pos (cursor/pos input)
  99. edit-content (or (state/sub [:editor/content id]) "")
  100. sidebar? (in-sidebar? input)
  101. q (or
  102. @editor-handler/*selected-text
  103. (when (state/sub :editor/show-page-search-hashtag?)
  104. (util/safe-subs edit-content pos current-pos))
  105. (when (> (count edit-content) current-pos)
  106. (util/safe-subs edit-content pos current-pos))
  107. "")
  108. matched-pages (when-not (string/blank? q)
  109. (editor-handler/get-matched-pages q))
  110. matched-pages (cond
  111. (contains? (set (map util/page-name-sanity-lc matched-pages)) (util/page-name-sanity-lc (string/trim q))) ;; if there's a page name fully matched
  112. matched-pages
  113. (string/blank? q)
  114. nil
  115. (empty? matched-pages)
  116. (cons (str "New page: " q) matched-pages)
  117. ;; reorder, shortest and starts-with first.
  118. :else
  119. (let [matched-pages (remove nil? matched-pages)
  120. matched-pages (sort-by
  121. (fn [m]
  122. [(not (gstring/caseInsensitiveStartsWith m q)) (count m)])
  123. matched-pages)]
  124. (if (gstring/caseInsensitiveStartsWith (first matched-pages) q)
  125. (cons (first matched-pages)
  126. (cons (str "New page: " q) (rest matched-pages)))
  127. (cons (str "New page: " q) matched-pages))))]
  128. (ui/auto-complete
  129. matched-pages
  130. {:on-chosen (page-handler/on-chosen-handler input id q pos format)
  131. :on-enter #(page-handler/page-not-exists-handler input id q current-pos)
  132. :item-render (fn [page-name chosen?]
  133. [:div.preview-trigger-wrapper
  134. (block/page-preview-trigger
  135. {:children [:div (search/highlight-exact-query page-name q)]
  136. :open? chosen?
  137. :manual? true
  138. :fixed-position? true
  139. :tippy-distance 24
  140. :tippy-position (if sidebar? "left" "right")}
  141. page-name)])
  142. :empty-placeholder [:div.text-gray-500.text-sm.px-4.py-2 "Search for a page"]
  143. :class "black"}))))))
  144. (rum/defcs block-search-auto-complete < rum/reactive
  145. {:init (fn [state]
  146. (assoc state ::result (atom nil)))
  147. :did-update (fn [state]
  148. (let [result (::result state)
  149. [edit-block _ _ q] (:rum/args state)]
  150. (p/let [matched-blocks (when-not (string/blank? q)
  151. (editor-handler/get-matched-blocks q (:block/uuid edit-block)))]
  152. (reset! result matched-blocks)))
  153. state)}
  154. [state _edit-block input id q format]
  155. (let [result (rum/react (get state ::result))
  156. chosen-handler (editor-handler/block-on-chosen-handler input id q format)
  157. non-exist-block-handler (editor-handler/block-non-exist-handler input)]
  158. (when result
  159. (ui/auto-complete
  160. result
  161. {:on-chosen chosen-handler
  162. :on-enter non-exist-block-handler
  163. :empty-placeholder [:div.text-gray-500.pl-4.pr-4 "Search for a block"]
  164. :item-render (fn [{:block/keys [page uuid]}] ;; content returned from search engine is normalized
  165. (let [page (or (:block/original-name page)
  166. (:block/name page))
  167. repo (state/sub :git/current-repo)
  168. format (db/get-page-format page)
  169. block (db-model/query-block-by-uuid uuid)
  170. content (:block/content block)]
  171. [:.py-2 (search/block-search-result-item repo uuid format content q :block)]))
  172. :class "black"}))))
  173. (rum/defcs block-search < rum/reactive
  174. {:will-unmount (fn [state]
  175. (reset! editor-handler/*selected-text nil)
  176. (state/clear-search-result!)
  177. state)}
  178. [state id _format]
  179. (when (state/sub :editor/show-block-search?)
  180. (let [pos (:editor/last-saved-cursor @state/state)
  181. input (gdom/getElement id)
  182. [id format] (:rum/args state)
  183. current-pos (cursor/pos input)
  184. edit-content (state/sub [:editor/content id])
  185. edit-block (state/get-edit-block)
  186. q (or
  187. @editor-handler/*selected-text
  188. (when (> (count edit-content) current-pos)
  189. (subs edit-content pos current-pos)))]
  190. (when input
  191. (block-search-auto-complete edit-block input id q format)))))
  192. (rum/defc template-search < rum/reactive
  193. {:will-unmount (fn [state] (reset! editor-handler/*selected-text nil) state)}
  194. [id _format]
  195. (when (state/sub :editor/show-template-search?)
  196. (let [pos (:editor/last-saved-cursor @state/state)
  197. input (gdom/getElement id)]
  198. (when input
  199. (let [current-pos (cursor/pos input)
  200. edit-content (state/sub [:editor/content id])
  201. q (or
  202. (when (>= (count edit-content) current-pos)
  203. (subs edit-content pos current-pos))
  204. "")
  205. matched-templates (editor-handler/get-matched-templates q)
  206. non-exist-handler (fn [_state]
  207. (state/set-editor-show-template-search! false))]
  208. (ui/auto-complete
  209. matched-templates
  210. {:on-chosen (editor-handler/template-on-chosen-handler id)
  211. :on-enter non-exist-handler
  212. :empty-placeholder [:div.text-gray-500.px-4.py-2.text-sm "Search for a template"]
  213. :item-render (fn [[template _block-db-id]]
  214. template)
  215. :class "black"}))))))
  216. (rum/defc mobile-bar-indent-outdent [indent? icon]
  217. [:button.bottom-action
  218. {:on-mouse-down (fn [e]
  219. (util/stop e)
  220. (state/set-state! :editor/pos (cursor/pos (state/get-input)))
  221. (editor-handler/indent-outdent indent?))}
  222. (ui/icon icon {:style {:fontSize ui/icon-size}})])
  223. (rum/defc mobile-bar-command [command-handler icon]
  224. [:button.bottom-action
  225. {:on-mouse-down (fn [e]
  226. (util/stop e)
  227. (command-handler))}
  228. (ui/icon icon {:style {:fontSize ui/icon-size}})])
  229. (rum/defc mobile-bar-command-with-event [command-handler icon]
  230. [:button.bottom-action
  231. {:on-mouse-down (fn [e]
  232. (util/stop e)
  233. (command-handler e))}
  234. (ui/icon icon {:style {:fontSize ui/icon-size}})])
  235. (rum/defc mobile-bar < rum/reactive
  236. [_parent-state parent-id]
  237. (let [vw-state (state/sub :ui/visual-viewport-state)
  238. vw-pending? (state/sub :ui/visual-viewport-pending?)
  239. ;; TODO: should we add this focus step to `simple-insert!`?
  240. viewport-fn (fn [] (when-let [input (gdom/getElement parent-id)]
  241. (util/make-el-cursor-position-into-center-viewport input)
  242. (.focus input)))]
  243. [:div#mobile-editor-toolbar.bg-base-2
  244. {:style {:bottom (if vw-state
  245. (- (.-clientHeight js/document.documentElement)
  246. (:height vw-state)
  247. (:offset-top vw-state))
  248. 0)}
  249. :class (util/classnames [{:is-vw-pending (boolean vw-pending?)}])}
  250. [:div (mobile-bar-indent-outdent true "arrow-bar-right")]
  251. [:div (mobile-bar-indent-outdent false "arrow-bar-left")]
  252. [:div (mobile-bar-command (editor-handler/move-up-down true) "arrow-bar-to-up")]
  253. [:div (mobile-bar-command (editor-handler/move-up-down false) "arrow-bar-to-down")]
  254. [:div (mobile-bar-command #(commands/simple-insert! parent-id "\n" {}) "arrow-back")]
  255. [:div (mobile-bar-command editor-handler/cycle-todo! "checkbox")]
  256. [:div (mobile-bar-command #(editor-handler/toggle-page-reference-embed parent-id) "brackets")]
  257. [:div (mobile-bar-command #(editor-handler/toggle-block-reference-embed parent-id) "parentheses")]
  258. [:div (mobile-bar-command #(do (viewport-fn) (commands/simple-insert! parent-id "/" {})) "command")]
  259. [:div (mobile-bar-command #(do (viewport-fn) (commands/simple-insert! parent-id "#" {})) "tag")]
  260. [:div (mobile-bar-command editor-handler/cycle-priority! "a-b")]
  261. [:div (mobile-bar-command editor-handler/toggle-list! "list")]
  262. [:div (mobile-bar-command #(mobile-camera/embed-photo parent-id) "camera")]
  263. [:div (mobile-bar-command commands/insert-youtube-timestamp "brand-youtube")]
  264. [:div (mobile-bar-command editor-handler/html-link-format! "link")]
  265. [:div (mobile-bar-command-with-event history/undo! "rotate")]
  266. [:div (mobile-bar-command-with-event history/redo! "rotate-clockwise")]
  267. [:div (mobile-bar-command #(do (viewport-fn) (commands/simple-insert! parent-id "<" {})) "code")]
  268. [:div (mobile-bar-command editor-handler/bold-format! "bold")]
  269. [:div (mobile-bar-command editor-handler/italics-format! "italic")]
  270. [:div (mobile-bar-command editor-handler/strike-through-format! "strikethrough")]
  271. [:div (mobile-bar-command editor-handler/highlight-format! "paint")]]))
  272. (rum/defcs input < rum/reactive
  273. (rum/local {} ::input-value)
  274. (mixins/event-mixin
  275. (fn [state]
  276. (mixins/on-key-down
  277. state
  278. { ;; enter
  279. 13 (fn [state e]
  280. (let [input-value (get state ::input-value)
  281. input-option (get @state/state :editor/show-input)]
  282. (when (seq @input-value)
  283. ;; no new line input
  284. (util/stop e)
  285. (let [[_id on-submit] (:rum/args state)
  286. {:keys [pos]} @*slash-caret-pos
  287. command (:command (first input-option))]
  288. (on-submit command @input-value pos))
  289. (reset! input-value nil))))})))
  290. [state _id on-submit]
  291. (when-let [input-option (state/sub :editor/show-input)]
  292. (let [{:keys [pos]} (util/react *slash-caret-pos)
  293. input-value (get state ::input-value)]
  294. (when (seq input-option)
  295. (let [command (:command (first input-option))]
  296. [:div.p-2.rounded-md.shadow-lg
  297. (for [{:keys [id placeholder type autoFocus] :as input-item} input-option]
  298. [:div.my-3 {:key id}
  299. [:input.form-input.block.w-full.pl-2.sm:text-sm.sm:leading-5
  300. (merge
  301. (cond->
  302. {:key (str "modal-input-" (name id))
  303. :id (str "modal-input-" (name id))
  304. :type (or type "text")
  305. :on-change (fn [e]
  306. (swap! input-value assoc id (util/evalue e)))
  307. :auto-complete (if (util/chrome?) "chrome-off" "off")}
  308. placeholder
  309. (assoc :placeholder placeholder)
  310. autoFocus
  311. (assoc :auto-focus true))
  312. (dissoc input-item :id))]])
  313. (ui/button
  314. "Submit"
  315. :on-click
  316. (fn [e]
  317. (util/stop e)
  318. (on-submit command @input-value pos)))])))))
  319. (rum/defc absolute-modal < rum/static
  320. [cp set-default-width? {:keys [top left rect]}]
  321. (let [max-height 370
  322. max-width 300
  323. offset-top 24
  324. vw-height js/window.innerHeight
  325. vw-width js/window.innerWidth
  326. to-max-height (if (and (seq rect) (> vw-height max-height))
  327. (let [delta-height (- vw-height (+ (:top rect) top offset-top))]
  328. (if (< delta-height max-height)
  329. (- (max (* 2 offset-top) delta-height) 16)
  330. max-height))
  331. max-height)
  332. right-sidebar? (:ui/sidebar-open? @state/state)
  333. editing-key (first (keys (:editor/editing? @state/state)))
  334. *el (rum/use-ref nil)
  335. _ (rum/use-effect! (fn []
  336. (when-let [^js/HTMLElement cnt
  337. (and right-sidebar? editing-key
  338. (js/document.querySelector "#main-content-container"))]
  339. (when (.contains cnt (js/document.querySelector (str "#" editing-key)))
  340. (let [el (rum/deref *el)
  341. ofx (- (.-scrollWidth cnt) (.-clientWidth cnt))]
  342. (when (> ofx 0)
  343. (set! (.-transform (.-style el)) (str "translateX(-" (+ ofx 20) "px)")))))))
  344. [right-sidebar? editing-key])
  345. ;; FIXME: for translateY layer
  346. x-overflow-vw? (when (and (seq rect) (> vw-width max-width))
  347. (let [delta-width (- vw-width (+ (:left rect) left))]
  348. (< delta-width (* max-width 0.5))))
  349. pos-rect (when (and (seq rect) editing-key)
  350. (:rect (cursor/get-caret-pos (state/get-input))))
  351. y-diff (when pos-rect (- (:height pos-rect) (:height rect)))]
  352. [:div.absolute.rounded-md.shadow-lg.absolute-modal
  353. {:ref *el
  354. :class (if x-overflow-vw? "is-overflow-vw-x" "")
  355. :on-mouse-down (fn [e]
  356. (.stopPropagation e))
  357. :style (merge
  358. {:top (+ top offset-top (if (int? y-diff) y-diff 0))
  359. :max-height to-max-height
  360. :max-width 700
  361. ;; TODO: auto responsive fixed size
  362. :width "fit-content"
  363. :z-index 11}
  364. (when set-default-width?
  365. {:width max-width})
  366. (if config/mobile?
  367. {:left 0}
  368. {:left (if (and y-diff (= y-diff 0)) left 0)}))}
  369. cp]))
  370. (rum/defc transition-cp < rum/reactive
  371. [cp set-default-width? pos]
  372. (when pos
  373. (when-let [pos (rum/react pos)]
  374. (ui/css-transition
  375. {:class-names "fade"
  376. :timeout {:enter 500
  377. :exit 300}}
  378. (absolute-modal cp set-default-width? pos)))))
  379. (rum/defc image-uploader < rum/reactive
  380. [id format]
  381. [:div.image-uploader
  382. [:input
  383. {:id "upload-file"
  384. :type "file"
  385. :on-change (fn [e]
  386. (let [files (.-files (.-target e))]
  387. (editor-handler/upload-asset id files format editor-handler/*asset-uploading? false)))
  388. :hidden true}]
  389. #_:clj-kondo/ignore
  390. (when-let [uploading? (util/react editor-handler/*asset-uploading?)]
  391. (let [processing (util/react editor-handler/*asset-uploading-process)]
  392. (transition-cp
  393. [:div.flex.flex-row.align-center.rounded-md.shadow-sm.bg-base-2.px-1.py-1
  394. (ui/loading
  395. (util/format "Uploading %s%" (util/format "%2d" processing)))]
  396. false
  397. *slash-caret-pos)))])
  398. (defn- set-up-key-down!
  399. [state format]
  400. (mixins/on-key-down
  401. state
  402. {}
  403. {:not-matched-handler (editor-handler/keydown-not-matched-handler format)}))
  404. (defn- set-up-key-up!
  405. [state input input-id search-timeout]
  406. (mixins/on-key-up
  407. state
  408. {}
  409. (editor-handler/keyup-handler state input input-id search-timeout)))
  410. (def search-timeout (atom nil))
  411. (defn- setup-key-listener!
  412. [state]
  413. (let [{:keys [id format]} (get-state)
  414. input-id id
  415. input (gdom/getElement input-id)]
  416. (set-up-key-down! state format)
  417. (set-up-key-up! state input input-id search-timeout)))
  418. (def starts-with? clojure.string/starts-with?)
  419. (defn get-editor-style-class
  420. "Get textarea css class according to it's content"
  421. [content format]
  422. (let [content (if content (str content) "")]
  423. ;; as the function is binding to the editor content, optimization is welcome
  424. (str
  425. (if (or (> (.-length content) 1000)
  426. (string/includes? content "\n"))
  427. "multiline-block"
  428. "uniline-block")
  429. " "
  430. (case format
  431. :markdown
  432. (cond
  433. (starts-with? content "# ") "h1"
  434. (starts-with? content "## ") "h2"
  435. (starts-with? content "### ") "h3"
  436. (starts-with? content "#### ") "h4"
  437. (starts-with? content "##### ") "h5"
  438. (starts-with? content "###### ") "h6"
  439. (and (starts-with? content "---\n") (.endsWith content "\n---")) "page-properties"
  440. :else "normal-block")
  441. ;; other formats
  442. (cond
  443. (and (starts-with? content "---\n") (.endsWith content "\n---")) "page-properties"
  444. :else "normal-block")))))
  445. (defn editor-row-height-unchanged?
  446. "Check if the row height of editor textarea is changed, which happens when font-size changed"
  447. []
  448. ;; FIXME: assuming enter key is the only trigger of the height changing (under markdown editing of headlines)
  449. ;; FIXME: looking for an elegant & robust way to track the change of font-size, or wait for our own WYSIWYG text area
  450. (let [last-key (state/get-last-key-code)]
  451. (and (not= keycode/enter (:key-code last-key))
  452. (not= keycode/enter-code (:code last-key)))))
  453. (rum/defc mock-textarea <
  454. rum/static
  455. {:did-update
  456. (fn [state]
  457. (when-not (:editor/on-paste? @state/state)
  458. (try (editor-handler/handle-last-input)
  459. (catch js/Error _e
  460. nil)))
  461. (state/set-state! :editor/on-paste? false)
  462. state)}
  463. [content]
  464. [:div#mock-text
  465. {:style {:width "100%"
  466. :height "100%"
  467. :position "absolute"
  468. :visibility "hidden"
  469. :top 0
  470. :left 0}}
  471. (let [content (str content "0")]
  472. (for [[idx c] (map-indexed
  473. vector
  474. (string/split content ""))]
  475. (if (= c "\n")
  476. [:span {:id (str "mock-text_" idx)
  477. :key idx} "0" [:br]]
  478. [:span {:id (str "mock-text_" idx)
  479. :key idx} c])))])
  480. (rum/defc mock-textarea-wrapper < rum/reactive
  481. []
  482. (let [content (state/sub-edit-content)]
  483. (mock-textarea content)))
  484. (defn animated-modal
  485. [key component set-default-width? *pos]
  486. (when *pos
  487. (ui/css-transition
  488. {:key key
  489. :class-names {:enter "origin-top-left opacity-0 transform scale-95"
  490. :enter-done "origin-top-left transition opacity-100 transform scale-100"
  491. :exit "origin-top-left transition opacity-0 transform scale-95"}
  492. :timeout {:enter 0
  493. :exit 150}}
  494. (fn [_]
  495. (absolute-modal
  496. component
  497. set-default-width?
  498. *pos)))))
  499. (rum/defc modals < rum/reactive
  500. "React to atom changes, find and render the correct modal"
  501. [id format]
  502. (ui/transition-group
  503. (cond
  504. (and (util/react *show-commands)
  505. (not (state/sub :editor/show-page-search?))
  506. (not (state/sub :editor/show-block-search?))
  507. (not (state/sub :editor/show-template-search?))
  508. (not (state/sub :editor/show-input))
  509. (not (state/sub :editor/show-zotero))
  510. (not (state/sub :editor/show-date-picker?)))
  511. (animated-modal "commands" (commands id format) true (util/react *slash-caret-pos))
  512. (and (util/react *show-block-commands) @*angle-bracket-caret-pos)
  513. (animated-modal "block-commands" (block-commands id format) true (util/react *angle-bracket-caret-pos))
  514. (state/sub :editor/show-page-search?)
  515. (animated-modal "page-search" (page-search id format) true (util/react *slash-caret-pos))
  516. (state/sub :editor/show-block-search?)
  517. (animated-modal "block-search" (block-search id format) false (util/react *slash-caret-pos))
  518. (state/sub :editor/show-template-search?)
  519. (animated-modal "template-search" (template-search id format) true (util/react *slash-caret-pos))
  520. (state/sub :editor/show-date-picker?)
  521. (animated-modal "date-picker" (datetime-comp/date-picker id format nil) false (util/react *slash-caret-pos))
  522. (state/sub :editor/show-input)
  523. (animated-modal "input" (input id
  524. (fn [command m _pos]
  525. (editor-handler/handle-command-input command id format m)))
  526. true (util/react *slash-caret-pos))
  527. (state/sub :editor/show-zotero)
  528. (animated-modal "zotero-search" (zotero/zotero-search id) false (util/react *slash-caret-pos))
  529. :else
  530. nil)))
  531. (rum/defcs box < rum/reactive
  532. {:init (fn [state]
  533. (assoc state ::heading-level (:heading-level (first (:rum/args state)))))
  534. :did-mount (fn [state]
  535. (state/set-editor-args! (:rum/args state))
  536. state)}
  537. (mixins/event-mixin setup-key-listener!)
  538. (shortcut/mixin :shortcut.handler/block-editing-only)
  539. lifecycle/lifecycle
  540. [state {:keys [format block]} id _config]
  541. (let [content (state/sub-edit-content)
  542. heading-class (get-editor-style-class content format)]
  543. [:div.editor-inner {:class (if block "block-editor" "non-block-editor")}
  544. (when (or (mobile-util/is-native-platform?) config/mobile?)
  545. (mobile-bar state id))
  546. (ui/ls-textarea
  547. {:id id
  548. :cacheMeasurements (editor-row-height-unchanged?) ;; check when content updated (as the content variable is binded)
  549. :default-value (or content "")
  550. :minRows (if (state/enable-grammarly?) 2 1)
  551. :on-click (editor-handler/editor-on-click! id)
  552. :on-change (editor-handler/editor-on-change! block id search-timeout)
  553. :on-paste (editor-handler/editor-on-paste! id)
  554. :on-height-change (editor-handler/editor-on-height-change! id)
  555. :auto-focus false
  556. :class heading-class})
  557. (mock-textarea-wrapper)
  558. (modals id format)
  559. (when format
  560. (image-uploader id format))]))