editor.cljs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. (ns frontend.components.editor
  2. (:require [rum.core :as rum]
  3. [frontend.components.svg :as svg]
  4. [frontend.config :as config]
  5. [frontend.handler.editor :as editor-handler :refer [get-state]]
  6. [frontend.util :as util :refer-macros [profile]]
  7. [frontend.handler.file :as file]
  8. [frontend.handler.block :as block-handler]
  9. [frontend.handler.page :as page-handler]
  10. [frontend.handler.editor.keyboards :as keyboards-handler]
  11. [frontend.components.datetime :as datetime-comp]
  12. [promesa.core :as p]
  13. [frontend.state :as state]
  14. [frontend.mixins :as mixins]
  15. [frontend.ui :as ui]
  16. [frontend.db :as db]
  17. [frontend.config :as config]
  18. [frontend.handler.web.nfs :as nfs]
  19. [dommy.core :as d]
  20. [goog.object :as gobj]
  21. [goog.dom :as gdom]
  22. [clojure.string :as string]
  23. [clojure.set :as set]
  24. [cljs.core.match :refer-macros [match]]
  25. [frontend.commands :as commands
  26. :refer [*show-commands
  27. *matched-commands
  28. *slash-caret-pos
  29. *angle-bracket-caret-pos
  30. *matched-block-commands
  31. *show-block-commands]]
  32. [medley.core :as medley]
  33. [cljs-drag-n-drop.core :as dnd]
  34. [frontend.text :as text]
  35. ["/frontend/utils" :as utils]))
  36. (rum/defc commands < rum/reactive
  37. [id format]
  38. (when (and (util/react *show-commands)
  39. @*slash-caret-pos
  40. (not (state/sub :editor/show-page-search?))
  41. (not (state/sub :editor/show-block-search?))
  42. (not (state/sub :editor/show-template-search?))
  43. (not (state/sub :editor/show-input))
  44. (not (state/sub :editor/show-date-picker?)))
  45. (let [matched (util/react *matched-commands)]
  46. (ui/auto-complete
  47. (map first matched)
  48. {:on-chosen (fn [chosen]
  49. (reset! commands/*current-command chosen)
  50. (let [command-steps (get (into {} matched) chosen)
  51. restore-slash? (or
  52. (contains? #{"Today" "Yesterday" "Tomorrow"} chosen)
  53. (and
  54. (not (fn? command-steps))
  55. (not (contains? (set (map first command-steps)) :editor/input))
  56. (not (contains? #{"Date Picker" "Template" "Deadline" "Scheduled" "Upload an image"} chosen))))]
  57. (editor-handler/insert-command! id command-steps
  58. format
  59. {:restore? restore-slash?})))
  60. :class "black"}))))
  61. (rum/defc block-commands < rum/reactive
  62. [id format]
  63. (when (and (util/react *show-block-commands)
  64. @*angle-bracket-caret-pos)
  65. (let [matched (util/react *matched-block-commands)]
  66. (ui/auto-complete
  67. (map first matched)
  68. {:on-chosen (fn [chosen]
  69. (editor-handler/insert-command! id (get (into {} matched) chosen)
  70. format
  71. {:last-pattern commands/angle-bracket}))
  72. :class "black"}))))
  73. (rum/defc page-search < rum/reactive
  74. {:will-unmount (fn [state] (reset! editor-handler/*selected-text nil) state)}
  75. [id format]
  76. (when (state/sub :editor/show-page-search?)
  77. (let [pos (:editor/last-saved-cursor @state/state)
  78. input (gdom/getElement id)]
  79. (when input
  80. (let [current-pos (:pos (util/get-caret-pos input))
  81. edit-content (state/sub [:editor/content id])
  82. edit-block (state/sub :editor/block)
  83. q (or
  84. @editor-handler/*selected-text
  85. (when (state/sub :editor/show-page-search-hashtag?)
  86. (util/safe-subs edit-content pos current-pos))
  87. (when (> (count edit-content) current-pos)
  88. (util/safe-subs edit-content pos current-pos)))
  89. matched-pages (when-not (string/blank? q)
  90. (editor-handler/get-matched-pages q))
  91. chosen-handler (if (state/sub :editor/show-page-search-hashtag?)
  92. (fn [chosen _click?]
  93. (state/set-editor-show-page-search! false)
  94. (let [chosen (if (re-find #"\s+" chosen)
  95. (util/format "[[%s]]" chosen)
  96. chosen)]
  97. (editor-handler/insert-command! id
  98. (str "#" chosen)
  99. format
  100. {:last-pattern (str "#" (if @editor-handler/*selected-text "" q))})))
  101. (fn [chosen _click?]
  102. (state/set-editor-show-page-search! false)
  103. (let [page-ref-text (page-handler/get-page-ref-text chosen)]
  104. (editor-handler/insert-command! id
  105. page-ref-text
  106. format
  107. {:last-pattern (str "[[" (if @editor-handler/*selected-text "" q))
  108. :postfix-fn (fn [s] (util/replace-first "]]" s ""))}))))
  109. non-exist-page-handler (fn [_state]
  110. (state/set-editor-show-page-search! false)
  111. (if (state/org-mode-file-link? (state/get-current-repo))
  112. (let [page-ref-text (page-handler/get-page-ref-text q)
  113. value (gobj/get input "value")
  114. old-page-ref (util/format "[[%s]]" q)
  115. new-value (string/replace value
  116. old-page-ref
  117. page-ref-text)]
  118. (state/set-edit-content! id new-value)
  119. (let [new-pos (+ current-pos
  120. (- (count page-ref-text)
  121. (count old-page-ref))
  122. 2)]
  123. (util/move-cursor-to input new-pos)))
  124. (util/cursor-move-forward input 2)))]
  125. (ui/auto-complete
  126. matched-pages
  127. {:on-chosen chosen-handler
  128. :on-enter non-exist-page-handler
  129. :empty-div [:div.text-gray-500.pl-4.pr-4 "Search for a page"]
  130. :class "black"}))))))
  131. (rum/defc block-search < rum/reactive
  132. {:will-unmount (fn [state] (reset! editor-handler/*selected-text nil) state)}
  133. [id format]
  134. (when (state/sub :editor/show-block-search?)
  135. (let [pos (:editor/last-saved-cursor @state/state)
  136. input (gdom/getElement id)]
  137. (when input
  138. (let [current-pos (:pos (util/get-caret-pos input))
  139. edit-content (state/sub [:editor/content id])
  140. edit-block (state/get-edit-block)
  141. q (or
  142. @editor-handler/*selected-text
  143. (when (> (count edit-content) current-pos)
  144. (subs edit-content pos current-pos)))
  145. matched-blocks (when-not (string/blank? q)
  146. (editor-handler/get-matched-blocks q (:block/uuid edit-block)))
  147. chosen-handler (fn [chosen _click?]
  148. (state/set-editor-show-block-search! false)
  149. (let [uuid-string (str (:block/uuid chosen))]
  150. ;; block reference
  151. (editor-handler/insert-command! id
  152. (util/format "((%s))" uuid-string)
  153. format
  154. {:last-pattern (str "((" (if @editor-handler/*selected-text "" q))
  155. :postfix-fn (fn [s] (util/replace-first "))" s ""))})
  156. ;; Save it so it'll be parsed correctly in the future
  157. (editor-handler/set-block-property! (:block/uuid chosen)
  158. "ID"
  159. uuid-string)
  160. (when-let [input (gdom/getElement id)]
  161. (.focus input))))
  162. non-exist-block-handler (fn [_state]
  163. (state/set-editor-show-block-search! false)
  164. (util/cursor-move-forward input 2))]
  165. (ui/auto-complete
  166. matched-blocks
  167. {:on-chosen chosen-handler
  168. :on-enter non-exist-block-handler
  169. :empty-div [:div.text-gray-500.pl-4.pr-4 "Search for a block"]
  170. :item-render (fn [{:block/keys [content]}]
  171. (subs content 0 64))
  172. :class "black"}))))))
  173. (rum/defc template-search < rum/reactive
  174. {:will-unmount (fn [state] (reset! editor-handler/*selected-text nil) state)}
  175. [id format]
  176. (when (state/sub :editor/show-template-search?)
  177. (let [pos (:editor/last-saved-cursor @state/state)
  178. input (gdom/getElement id)]
  179. (when input
  180. (let [current-pos (:pos (util/get-caret-pos input))
  181. edit-content (state/sub [:editor/content id])
  182. edit-block (state/sub :editor/block)
  183. q (or
  184. (when (>= (count edit-content) current-pos)
  185. (subs edit-content pos current-pos))
  186. "")
  187. matched-templates (editor-handler/get-matched-templates q)
  188. chosen-handler (fn [[template db-id] _click?]
  189. (if-let [block (db/entity db-id)]
  190. (let [new-level (:block/level edit-block)
  191. template-parent-level (:block/level block)
  192. pattern (config/get-block-pattern format)
  193. content
  194. (block-handler/get-block-full-content
  195. (state/get-current-repo)
  196. (:block/uuid block)
  197. (fn [{:block/keys [level content properties] :as block}]
  198. (let [new-level (+ new-level (- level template-parent-level))
  199. properties' (dissoc (into {} properties) "id" "custom_id" "template")]
  200. (-> content
  201. (string/replace-first (apply str (repeat level pattern))
  202. (apply str (repeat new-level pattern)))
  203. text/remove-properties!
  204. (text/rejoin-properties properties')))))
  205. content (if (string/includes? (string/trim edit-content) "\n")
  206. content
  207. (text/remove-level-spaces content format))]
  208. (state/set-editor-show-template-search! false)
  209. (editor-handler/insert-command! id
  210. content
  211. format
  212. {})))
  213. (when-let [input (gdom/getElement id)]
  214. (.focus input)))
  215. non-exist-handler (fn [_state]
  216. (state/set-editor-show-template-search! false))]
  217. (ui/auto-complete
  218. matched-templates
  219. {:on-chosen chosen-handler
  220. :on-enter non-exist-handler
  221. :empty-div [:div.text-gray-500.pl-4.pr-4 "Search for a template"]
  222. :item-render (fn [[template _block-db-id]]
  223. template)
  224. :class "black"}))))))
  225. (rum/defc mobile-bar < rum/reactive
  226. [parent-state parent-id]
  227. [:div#mobile-editor-toolbar.bg-base-2.fix-ios-fixed-bottom
  228. [:button.bottom-action
  229. {:on-click #(editor-handler/adjust-block-level! parent-state :right)}
  230. svg/indent-block]
  231. [:button.bottom-action
  232. {:on-click #(editor-handler/adjust-block-level! parent-state :left)}
  233. svg/outdent-block]
  234. [:button.bottom-action
  235. {:on-click #(editor-handler/move-up-down % true)}
  236. svg/move-up-block]
  237. [:button.bottom-action
  238. {:on-click #(editor-handler/move-up-down % false)}
  239. svg/move-down-block]
  240. [:button.bottom-action
  241. {:on-click #(commands/simple-insert! parent-id "\n" {})}
  242. svg/multi-line-input]
  243. [:button.bottom-action
  244. {:on-click #(commands/insert-before! parent-id "TODO " {})}
  245. svg/checkbox]
  246. [:button.font-extrabold.bottom-action.-mt-1
  247. {:on-click #(commands/simple-insert!
  248. parent-id "[[]]"
  249. {:backward-pos 2
  250. :check-fn (fn [_ _ new-pos]
  251. (reset! commands/*slash-caret-pos new-pos)
  252. (commands/handle-step [:editor/search-page]))})}
  253. "[[]]"]
  254. [:button.font-extrabold.bottom-action.-mt-1
  255. {:on-click #(commands/simple-insert!
  256. parent-id "(())"
  257. {:backward-pos 2
  258. :check-fn (fn [_ _ new-pos]
  259. (reset! commands/*slash-caret-pos new-pos)
  260. (commands/handle-step [:editor/search-block]))})}
  261. "(())"]
  262. [:button.font-extrabold.bottom-action.-mt-1
  263. {:on-click #(commands/simple-insert! parent-id "/" {})}
  264. "/"]])
  265. (rum/defcs input < rum/reactive
  266. (rum/local {} ::input-value)
  267. (mixins/event-mixin
  268. (fn [state]
  269. (mixins/on-key-down
  270. state
  271. {;; enter
  272. 13 (fn [state e]
  273. (let [input-value (get state ::input-value)
  274. input-option (get @state/state :editor/show-input)]
  275. (when (seq @input-value)
  276. ;; no new line input
  277. (util/stop e)
  278. (let [[_id on-submit] (:rum/args state)
  279. {:keys [pos]} @*slash-caret-pos
  280. command (:command (first input-option))]
  281. (on-submit command @input-value pos))
  282. (reset! input-value nil))))})))
  283. {:did-update
  284. (fn [state]
  285. (when-let [show-input (state/get-editor-show-input)]
  286. (let [id (str "modal-input-"
  287. (name (:id (first show-input))))
  288. first-input (gdom/getElement id)]
  289. (when (and first-input
  290. (not (d/has-class? first-input "focused")))
  291. (.focus first-input)
  292. (d/add-class! first-input "focused"))))
  293. state)}
  294. [state id on-submit]
  295. (when-let [input-option (state/sub :editor/show-input)]
  296. (let [{:keys [pos]} (util/react *slash-caret-pos)
  297. input-value (get state ::input-value)]
  298. (when (seq input-option)
  299. (let [command (:command (first input-option))]
  300. [:div.p-2.mt-2.rounded-md.shadow-sm.bg-base-2
  301. (for [{:keys [id placeholder type] :as input-item} input-option]
  302. [:div.my-3
  303. [:input.form-input.block.w-full.pl-2.sm:text-sm.sm:leading-5
  304. (merge
  305. (cond->
  306. {:key (str "modal-input-" (name id))
  307. :id (str "modal-input-" (name id))
  308. :type (or type "text")
  309. :on-change (fn [e]
  310. (swap! input-value assoc id (util/evalue e)))
  311. :auto-complete (if (util/chrome?) "chrome-off" "off")}
  312. placeholder
  313. (assoc :placeholder placeholder))
  314. (dissoc input-item :id))]])
  315. (ui/button
  316. "Submit"
  317. :on-click
  318. (fn [e]
  319. (util/stop e)
  320. (on-submit command @input-value pos)))])))))
  321. (rum/defc absolute-modal < rum/static
  322. [cp set-default-width? {:keys [top left rect]}]
  323. (let [max-height 500
  324. max-width 300
  325. offset-top 24
  326. vw-height js/window.innerHeight
  327. vw-width js/window.innerWidth
  328. to-max-height (if (and (seq rect) (> vw-height max-height))
  329. (let [delta-height (- vw-height (+ (:top rect) top offset-top))]
  330. (if (< delta-height max-height)
  331. (- (max (* 2 offset-top) delta-height) 16)
  332. max-height))
  333. max-height)
  334. x-overflow? (if (and (seq rect) (> vw-width max-width))
  335. (let [delta-width (- vw-width (+ (:left rect) left))]
  336. (< delta-width (* max-width 0.5))))] ;; FIXME: for translateY layer
  337. [:div.absolute.rounded-md.shadow-lg.absolute-modal
  338. {:class (if x-overflow? "is-overflow-vw-x" "")
  339. :style (merge
  340. {:top (+ top offset-top)
  341. :max-height to-max-height
  342. :z-index 11}
  343. (if set-default-width?
  344. {:width max-width})
  345. (if config/mobile?
  346. {:left 0}
  347. {:left left}))}
  348. cp]))
  349. (rum/defc transition-cp < rum/reactive
  350. [cp set-default-width? pos]
  351. (when pos
  352. (when-let [pos (rum/react pos)]
  353. (ui/css-transition
  354. {:class-names "fade"
  355. :timeout {:enter 500
  356. :exit 300}}
  357. (absolute-modal cp set-default-width? pos)))))
  358. (rum/defc image-uploader < rum/reactive
  359. {:did-mount (fn [state]
  360. (let [[id format] (:rum/args state)]
  361. (add-watch editor-handler/*asset-pending-file ::pending-asset
  362. (fn [_ _ _ f]
  363. (reset! *slash-caret-pos (util/get-caret-pos (gdom/getElement id)))
  364. (editor-handler/upload-asset id #js[f] format editor-handler/*asset-uploading? true))))
  365. state)
  366. :will-unmount (fn [state]
  367. (remove-watch editor-handler/*asset-pending-file ::pending-asset))}
  368. [id format]
  369. [:div.image-uploader
  370. [:input
  371. {:id "upload-file"
  372. :type "file"
  373. :on-change (fn [e]
  374. (let [files (.-files (.-target e))]
  375. (editor-handler/upload-asset id files format editor-handler/*asset-uploading? false)))
  376. :hidden true}]
  377. (when-let [uploading? (util/react editor-handler/*asset-uploading?)]
  378. (let [processing (util/react editor-handler/*asset-uploading-process)]
  379. (transition-cp
  380. [:div.flex.flex-row.align-center.rounded-md.shadow-sm.bg-base-2.px-1.py-1
  381. (ui/loading
  382. (util/format "Uploading %s%" (util/format "%2d" processing)))]
  383. false
  384. *slash-caret-pos)))])
  385. (rum/defcs box < rum/reactive
  386. (mixins/event-mixin
  387. (fn [state]
  388. (let [{:keys [id format block]} (get-state state)
  389. input-id id
  390. input (gdom/getElement input-id)
  391. repo (:block/repo block)]
  392. (mixins/on-key-down
  393. state
  394. {;; enter
  395. 13 (fn [state e]
  396. (when (and (not (gobj/get e "ctrlKey"))
  397. (not (gobj/get e "metaKey"))
  398. (not (editor-handler/in-auto-complete? input)))
  399. (let [{:keys [block config]} (get-state state)]
  400. (when (and block
  401. (not (:ref? config))
  402. (not (:custom-query? config))) ; in reference section
  403. (let [content (state/get-edit-content)]
  404. (if (and
  405. (> (:block/level block) 2)
  406. (string/blank? content))
  407. (do
  408. (util/stop e)
  409. (editor-handler/adjust-block-level! state :left))
  410. (let [shortcut (state/get-new-block-shortcut)
  411. insert? (cond
  412. config/mobile?
  413. true
  414. (and (= shortcut "alt+enter") (not (gobj/get e "altKey")))
  415. false
  416. (gobj/get e "shiftKey")
  417. false
  418. :else
  419. true)]
  420. (when (and
  421. insert?
  422. (not (editor-handler/in-auto-complete? input)))
  423. (util/stop e)
  424. (profile
  425. "Insert block"
  426. (editor-handler/insert-new-block! state))))))))))
  427. ;; up
  428. 38 (fn [state e]
  429. (when (and
  430. (not (gobj/get e "ctrlKey"))
  431. (not (gobj/get e "metaKey"))
  432. (not (editor-handler/in-auto-complete? input)))
  433. (editor-handler/on-up-down state e true)))
  434. ;; down
  435. 40 (fn [state e]
  436. (when (and
  437. (not (gobj/get e "ctrlKey"))
  438. (not (gobj/get e "metaKey"))
  439. (not (editor-handler/in-auto-complete? input)))
  440. (editor-handler/on-up-down state e false)))
  441. ;; backspace
  442. 8 (fn [state e]
  443. (let [node (gdom/getElement input-id)
  444. current-pos (:pos (util/get-caret-pos node))
  445. value (gobj/get node "value")
  446. deleted (and (> current-pos 0)
  447. (util/nth-safe value (dec current-pos)))
  448. selected-start (gobj/get node "selectionStart")
  449. selected-end (gobj/get node "selectionEnd")
  450. block-id (:block-id (first (:rum/args state)))
  451. page (state/get-current-page)]
  452. (cond
  453. (not= selected-start selected-end)
  454. nil
  455. (and (zero? current-pos)
  456. ;; not the top block in a block page
  457. (not (and page
  458. (util/uuid-string? page)
  459. (= (medley/uuid page) block-id))))
  460. (editor-handler/delete-block! state repo e)
  461. (and (> current-pos 1)
  462. (= (util/nth-safe value (dec current-pos)) commands/slash))
  463. (do
  464. (reset! *slash-caret-pos nil)
  465. (reset! *show-commands false))
  466. (and (> current-pos 1)
  467. (= (util/nth-safe value (dec current-pos)) commands/angle-bracket))
  468. (do
  469. (reset! *angle-bracket-caret-pos nil)
  470. (reset! *show-block-commands false))
  471. ;; pair
  472. (and
  473. deleted
  474. (contains?
  475. (set (keys editor-handler/delete-map))
  476. deleted)
  477. (>= (count value) (inc current-pos))
  478. (= (util/nth-safe value current-pos)
  479. (get editor-handler/delete-map deleted)))
  480. (do
  481. (util/stop e)
  482. (commands/delete-pair! id)
  483. (cond
  484. (and (= deleted "[") (state/get-editor-show-page-search?))
  485. (state/set-editor-show-page-search! false)
  486. (and (= deleted "(") (state/get-editor-show-block-search?))
  487. (state/set-editor-show-block-search! false)
  488. :else
  489. nil))
  490. ;; deleting hashtag
  491. (and (= deleted "#") (state/get-editor-show-page-search-hashtag?))
  492. (state/set-editor-show-page-search-hashtag! false)
  493. :else
  494. nil)))
  495. ;; tab
  496. 9 (fn [state e]
  497. (let [input-id (state/get-edit-input-id)
  498. input (and input-id (gdom/getElement id))
  499. pos (and input (:pos (util/get-caret-pos input)))]
  500. (when (and (not (state/get-editor-show-input))
  501. (not (state/get-editor-show-date-picker?))
  502. (not (state/get-editor-show-template-search?)))
  503. (util/stop e)
  504. (let [direction (if (gobj/get e "shiftKey") ; shift+tab move to left
  505. :left
  506. :right)]
  507. (p/let [_ (editor-handler/adjust-block-level! state direction)]
  508. (and input pos (js/setTimeout #(when-let [input (gdom/getElement input-id)]
  509. (util/move-cursor-to input pos))
  510. 0)))))))}
  511. {:not-matched-handler
  512. (fn [e key-code]
  513. (let [key (gobj/get e "key")
  514. value (gobj/get input "value")
  515. ctrlKey (gobj/get e "ctrlKey")
  516. metaKey (gobj/get e "metaKey")
  517. pos (util/get-input-pos input)]
  518. (cond
  519. (or ctrlKey metaKey)
  520. nil
  521. (or
  522. (and (= key "#")
  523. (and
  524. (> pos 0)
  525. (= "#" (util/nth-safe value (dec pos)))))
  526. (and (= key " ")
  527. (state/get-editor-show-page-search-hashtag?)))
  528. (state/set-editor-show-page-search-hashtag! false)
  529. (or
  530. (editor-handler/surround-by? input "#" " ")
  531. (editor-handler/surround-by? input "#" :end)
  532. (= key "#"))
  533. (do
  534. (commands/handle-step [:editor/search-page-hashtag])
  535. (state/set-last-pos! (:pos (util/get-caret-pos input)))
  536. (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
  537. (and
  538. (= key " ")
  539. (state/get-editor-show-page-search-hashtag?))
  540. (state/set-editor-show-page-search-hashtag! false)
  541. (and
  542. (contains? (set/difference (set (keys editor-handler/reversed-autopair-map))
  543. #{"`"})
  544. key)
  545. (= (editor-handler/get-current-input-char input) key))
  546. (do
  547. (util/stop e)
  548. (util/cursor-move-forward input 1))
  549. (contains? (set (keys editor-handler/autopair-map)) key)
  550. (do
  551. (util/stop e)
  552. (editor-handler/autopair input-id key format nil)
  553. (cond
  554. (editor-handler/surround-by? input "[[" "]]")
  555. (do
  556. (commands/handle-step [:editor/search-page])
  557. (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
  558. (editor-handler/surround-by? input "((" "))")
  559. (do
  560. (commands/handle-step [:editor/search-block :reference])
  561. (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
  562. :else
  563. nil))
  564. (let [sym "$"]
  565. (and (= key sym)
  566. (>= (count value) 1)
  567. (> pos 0)
  568. (= (nth value (dec pos)) sym)
  569. (if (> (count value) pos)
  570. (not= (nth value pos) sym)
  571. true)))
  572. (commands/simple-insert! input-id "$$" {:backward-pos 2})
  573. (let [sym "^"]
  574. (and (= key sym)
  575. (>= (count value) 1)
  576. (> pos 0)
  577. (= (nth value (dec pos)) sym)
  578. (if (> (count value) pos)
  579. (not= (nth value pos) sym)
  580. true)))
  581. (commands/simple-insert! input-id "^^" {:backward-pos 2})
  582. :else
  583. nil)))})
  584. (mixins/on-key-up
  585. state
  586. {}
  587. (fn [e key-code]
  588. (let [k (gobj/get e "key")
  589. format (:format (get-state state))]
  590. (when-not (state/get-editor-show-input)
  591. (when (and @*show-commands (not= key-code 191)) ; not /
  592. (let [matched-commands (editor-handler/get-matched-commands input)]
  593. (if (seq matched-commands)
  594. (do
  595. (reset! *show-commands true)
  596. (reset! *matched-commands matched-commands))
  597. (reset! *show-commands false))))
  598. (when (and @*show-block-commands (not= key-code 188)) ; not <
  599. (let [matched-block-commands (editor-handler/get-matched-block-commands input)]
  600. (if (seq matched-block-commands)
  601. (cond
  602. (= key-code 9) ;tab
  603. (when @*show-block-commands
  604. (util/stop e)
  605. (editor-handler/insert-command! input-id
  606. (last (first matched-block-commands))
  607. format
  608. {:last-pattern commands/angle-bracket}))
  609. :else
  610. (reset! *matched-block-commands matched-block-commands))
  611. (reset! *show-block-commands false))))
  612. (editor-handler/close-autocomplete-if-outside input))))))))
  613. {:did-mount (fn [state]
  614. (let [[{:keys [dummy? format block-parent-id]} id] (:rum/args state)
  615. content (get-in @state/state [:editor/content id])
  616. input (gdom/getElement id)]
  617. (when block-parent-id
  618. (state/set-editing-block-dom-id! block-parent-id))
  619. (if (= :indent-outdent (state/get-editor-op))
  620. (when input
  621. (when-let [pos (state/get-edit-pos)]
  622. (util/set-caret-pos! input pos)))
  623. (editor-handler/restore-cursor-pos! id content dummy?))
  624. (when input
  625. (dnd/subscribe!
  626. input
  627. :upload-images
  628. {:drop (fn [e files]
  629. (editor-handler/upload-asset id files format editor-handler/*asset-uploading? true))}))
  630. ;; Here we delay this listener, otherwise the click to edit event will trigger a outside click event,
  631. ;; which will hide the editor so no way for editing.
  632. (js/setTimeout #(keyboards-handler/esc-save! state) 100)
  633. (when-let [element (gdom/getElement id)]
  634. (.focus element)))
  635. state)
  636. :did-remount (fn [_old-state state]
  637. (keyboards-handler/esc-save! state)
  638. state)
  639. :will-unmount (fn [state]
  640. (let [{:keys [id value format block repo dummy? config]} (get-state state)
  641. file? (:file? config)]
  642. (when-let [input (gdom/getElement id)]
  643. ;; (.removeEventListener input "paste" (fn [event]
  644. ;; (append-paste-doc! format event)))
  645. (let [s (str "cljs-drag-n-drop." :upload-images)
  646. a (gobj/get input s)
  647. timer (:timer a)]
  648. (and timer
  649. (dnd/unsubscribe!
  650. input
  651. :upload-images))))
  652. (editor-handler/clear-when-saved!)
  653. (if file?
  654. (let [path (:file-path config)
  655. content (db/get-file-no-sub path)
  656. value (some-> (gdom/getElement path)
  657. (gobj/get "value"))]
  658. (when (and
  659. (not (string/blank? value))
  660. (not= (string/trim value) (string/trim content)))
  661. (let [old-page-name (db/get-file-page path false)]
  662. (page-handler/rename-when-alter-title-property! old-page-name path format content value)
  663. (file/alter-file (state/get-current-repo) path (string/trim value)
  664. {:re-render-root? true}))))
  665. (when-not (contains? #{:insert :indent-outdent :auto-save} (state/get-editor-op))
  666. (editor-handler/save-block! (get-state state) value))))
  667. state)}
  668. [state {:keys [on-hide dummy? node format block block-parent-id]
  669. :or {dummy? false}
  670. :as option} id config]
  671. (let [content (state/get-edit-content)]
  672. [:div.editor-inner {:class (if block "block-editor" "non-block-editor")}
  673. (when config/mobile? (mobile-bar state id))
  674. (ui/ls-textarea
  675. {:id id
  676. :cacheMeasurements true
  677. :default-value (or content "")
  678. :minRows (if (state/enable-grammarly?) 2 1)
  679. :on-click (fn [_e]
  680. (let [input (gdom/getElement id)
  681. current-pos (:pos (util/get-caret-pos input))]
  682. (state/set-edit-pos! current-pos)
  683. (editor-handler/close-autocomplete-if-outside input)))
  684. :on-change (fn [e]
  685. (let [value (util/evalue e)
  686. current-pos (:pos (util/get-caret-pos (gdom/getElement id)))]
  687. (state/set-edit-content! id value false)
  688. (state/set-edit-pos! current-pos)
  689. (when-let [repo (or (:block/repo block)
  690. (state/get-current-repo))]
  691. (state/set-editor-last-input-time! repo (util/time-ms))
  692. (db/clear-repo-persistent-job! repo))
  693. (let [input (gdom/getElement id)
  694. native-e (gobj/get e "nativeEvent")
  695. last-input-char (util/nth-safe value (dec current-pos))]
  696. (case last-input-char
  697. "/"
  698. ;; TODO: is it cross-browser compatible?
  699. (when (not= (gobj/get native-e "inputType") "insertFromPaste")
  700. (when-let [matched-commands (seq (editor-handler/get-matched-commands input))]
  701. (reset! *slash-caret-pos (util/get-caret-pos input))
  702. (reset! *show-commands true)))
  703. "<"
  704. (when-let [matched-commands (seq (editor-handler/get-matched-block-commands input))]
  705. (reset! *angle-bracket-caret-pos (util/get-caret-pos input))
  706. (reset! *show-block-commands true))
  707. nil))))
  708. :on-paste (fn [e]
  709. (when-let [handled
  710. (let [pick-one-allowed-item
  711. (fn [items]
  712. (if (util/electron?)
  713. (let [existed-file-path (js/window.apis.getFilePathFromClipboard)
  714. existed-file-path (if (and
  715. (string? existed-file-path)
  716. (not util/mac?)
  717. (not util/win32?)) ; FIXME: linuxcx
  718. (when (re-find #"^(/[^/ ]*)+/?$" existed-file-path)
  719. existed-file-path)
  720. existed-file-path)
  721. has-file-path? (not (string/blank? existed-file-path))
  722. has-image? (js/window.apis.isClipboardHasImage)]
  723. (if (or has-image? has-file-path?)
  724. [:asset (js/File. #js[] (if has-file-path? existed-file-path "image.png"))]))
  725. (when (and items (.-length items))
  726. (let [files (. (js/Array.from items) (filter #(= (.-kind %) "file")))
  727. it (gobj/get files 0) ;;; TODO: support multiple files
  728. mime (and it (.-type it))]
  729. (cond
  730. (contains? #{"image/jpeg" "image/png" "image/jpg" "image/gif"} mime) [:asset (. it getAsFile)])))))
  731. clipboard-data (gobj/get e "clipboardData")
  732. items (or (.-items clipboard-data)
  733. (.-files clipboard-data))
  734. picked (pick-one-allowed-item items)]
  735. (if (get picked 1)
  736. (match picked
  737. [:asset file] (editor-handler/set-asset-pending-file file))))]
  738. (util/stop e)))
  739. :auto-focus false})
  740. ;; TODO: how to render the transitions asynchronously?
  741. (transition-cp
  742. (commands id format)
  743. true
  744. *slash-caret-pos)
  745. (transition-cp
  746. (block-commands id format)
  747. true
  748. *angle-bracket-caret-pos)
  749. (transition-cp
  750. (page-search id format)
  751. true
  752. *slash-caret-pos)
  753. (transition-cp
  754. (block-search id format)
  755. true
  756. *slash-caret-pos)
  757. (transition-cp
  758. (template-search id format)
  759. true
  760. *slash-caret-pos)
  761. (transition-cp
  762. (datetime-comp/date-picker id format nil)
  763. false
  764. *slash-caret-pos)
  765. (transition-cp
  766. (input id
  767. (fn [command m pos]
  768. (editor-handler/handle-command-input command id format m pos)))
  769. true
  770. *slash-caret-pos)
  771. (when format
  772. (image-uploader id format))]))