editor.cljs 25 KB

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