editor_toolbar.cljs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. (ns capacitor.components.editor-toolbar
  2. "Mobile editor toolbar"
  3. (:require [capacitor.init :as init]
  4. [frontend.commands :as commands]
  5. [frontend.handler.editor :as editor-handler]
  6. [frontend.mobile.camera :as mobile-camera]
  7. [frontend.mobile.haptics :as haptics]
  8. [frontend.state :as state]
  9. [frontend.ui :as ui]
  10. [frontend.util :as util]
  11. [frontend.util.cursor :as cursor]
  12. [goog.dom :as gdom]
  13. [logseq.common.util.page-ref :as page-ref]
  14. [rum.core :as rum]))
  15. (defn- blur-if-compositing
  16. "Call blur on the textarea if it is in composition mode, let the IME commit the composing text"
  17. []
  18. (when-let [edit-input-id (and (state/editor-in-composition?)
  19. (state/get-edit-input-id))]
  20. (let [textarea-el (gdom/getElement edit-input-id)]
  21. (.blur textarea-el))))
  22. (rum/defc indent-outdent [indent? icon]
  23. [:div
  24. [:button.bottom-action
  25. {:on-pointer-down (fn [e]
  26. (util/stop e)
  27. (haptics/haptics)
  28. (blur-if-compositing)
  29. (editor-handler/indent-outdent indent?))}
  30. (ui/icon icon {:size ui/icon-size})]])
  31. (rum/defc command
  32. [command-handler {:keys [icon class]} & [event?]]
  33. [:div
  34. [:button.bottom-action
  35. {:on-pointer-down (fn [e]
  36. (util/stop e)
  37. (haptics/haptics)
  38. (if event?
  39. (command-handler e)
  40. (command-handler)))}
  41. (ui/icon icon {:size ui/icon-size :class class})]])
  42. (defn- insert-text
  43. [text opts]
  44. (when-let [parent-id (state/get-edit-input-id)]
  45. (let [input (gdom/getElement parent-id)
  46. pos (cursor/pos input)
  47. c (when (> pos 0)
  48. (str (nth (.-value input) (dec pos))))
  49. text' (if (and c (not= c " "))
  50. (str " " text)
  51. text)]
  52. (commands/simple-insert! parent-id text' opts))))
  53. (defn commands
  54. []
  55. [(command #(insert-text "#" {}) {:icon "hash"} true)
  56. (command #(insert-text page-ref/left-and-right-brackets
  57. {:backward-pos 2
  58. :check-fn (fn [_ _ _]
  59. (let [input (state/get-input)
  60. new-pos (cursor/get-caret-pos input)]
  61. (state/set-editor-action-data! {:pos new-pos})
  62. (commands/handle-step [:editor/search-page])))})
  63. {:icon "brackets"} true)
  64. (command #(insert-text "/" {}) {:icon "command"} true)])
  65. (rum/defc mobile-bar < rum/reactive
  66. []
  67. (when (and (util/mobile?)
  68. (or (state/sub :editor/editing?)
  69. (= "app-keep-keyboard-open-input" (some-> js/document.activeElement (.-id)))))
  70. (let [commands' (commands)]
  71. [:div#mobile-editor-toolbar
  72. [:div.toolbar-commands
  73. ;; (command (editor-handler/move-up-down true) {:icon "arrow-bar-to-up"})
  74. ;; (command (editor-handler/move-up-down false) {:icon "arrow-bar-to-down"})
  75. (command #(do
  76. (blur-if-compositing)
  77. (editor-handler/cycle-todo!))
  78. {:icon "checkbox"} true)
  79. (indent-outdent false "arrow-left-to-arc")
  80. (indent-outdent true "arrow-right-to-arc")
  81. ;; (command history/undo! {:icon "rotate" :class "rotate-180"} true)
  82. ;; (command history/redo! {:icon "rotate-clockwise" :class "rotate-180"} true)
  83. ;; (timestamp-submenu parent-id)
  84. (for [command' commands']
  85. command')
  86. (command #(let [parent-id (state/get-edit-input-id)]
  87. (mobile-camera/embed-photo parent-id)) {:icon "camera"} true)]
  88. [:div.toolbar-hide-keyboard
  89. (command #(do
  90. (state/clear-edit!)
  91. (init/keyboard-hide)) {:icon "keyboard-show"})]])))