editor.cljs 26 KB

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