property_v2.cljs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. (ns frontend.components.property-v2
  2. (:require [frontend.components.icon :as icon-component]
  3. [frontend.handler.db-based.property :as db-property-handler]
  4. [frontend.db :as db]
  5. [frontend.util :as util]
  6. [logseq.shui.ui :as shui]
  7. [logseq.shui.popup.core :as popup-core]
  8. [promesa.core :as p]
  9. [goog.dom :as gdom]
  10. [rum.core :as rum]))
  11. (rum/defc name-edit-pane
  12. [property]
  13. (let [title (:block/title property)
  14. icon (:logseq.property/icon property)
  15. *input-ref (rum/use-ref nil)]
  16. (rum/use-effect!
  17. (fn []
  18. (js/console.log "==>>>> name editor SET-UP!", property)
  19. #(js/console.log "==>>>> name editor BYE!"))
  20. [])
  21. [:div.ls-property-name-edit-pane
  22. [:div.flex.items-center.input-wrap
  23. (icon-component/icon-picker icon {:on-chosen (fn [_e icon]
  24. (db-property-handler/upsert-property!
  25. (:db/ident property)
  26. (:block/schema property)
  27. {:properties {:logseq.property/icon icon}}))
  28. :popup-opts {:align "start"}})
  29. (shui/input {:ref *input-ref :size "sm" :default-value title})]
  30. [:div.pt-2 (shui/textarea {:placeholder "description"})]
  31. [:div.pt-2.flex.justify-end
  32. (shui/button {:size "sm" :disabled true
  33. :variant :secondary} "Save")]]))
  34. (defn restore-root-highlight-item!
  35. [id]
  36. (js/setTimeout
  37. #(some-> (gdom/getElement id) (.focus)) 32))
  38. (rum/defc dropdown-editor-menuitem
  39. [{:keys [id icon title desc submenu-content item-props sub-content-props disabled? toggle-checked? on-toggle-checked-change]}]
  40. (let [[sub-open? set-sub-open!] (rum/use-state false)
  41. toggle? (boolean? toggle-checked?)
  42. id1 (str (or id icon (random-uuid)))
  43. id2 (str "d2-" id1)
  44. or-close-menu-sub! (fn []
  45. (when-not (popup-core/get-popup :ls-icon-picker)
  46. (set-sub-open! false)
  47. (restore-root-highlight-item! id1)))
  48. wrap-menuitem (if submenu-content
  49. #(shui/dropdown-menu-sub
  50. {:open sub-open?
  51. :on-open-change (fn [v] (if v (set-sub-open! true) (or-close-menu-sub!)))}
  52. (shui/dropdown-menu-sub-trigger (merge {:id id1} item-props) %)
  53. (shui/dropdown-menu-portal
  54. (shui/dropdown-menu-sub-content
  55. (merge {:hideWhenDetached true
  56. :onEscapeKeyDown or-close-menu-sub!} sub-content-props)
  57. (if (fn? submenu-content)
  58. (submenu-content {:set-sub-open! set-sub-open! :id id1}) submenu-content))))
  59. #(shui/dropdown-menu-item
  60. (merge {:on-select (fn []
  61. (when toggle?
  62. (some-> (gdom/getElement id2) (.click))))
  63. :id id1}
  64. item-props) %))]
  65. (wrap-menuitem
  66. [:div.inner-wrap
  67. {:class (util/classnames [{:disabled disabled?}])}
  68. [:strong
  69. (some-> icon (name) (shui/tabler-icon))
  70. [:span title]]
  71. (if (fn? desc) (desc)
  72. (if (boolean? toggle-checked?)
  73. [:span.scale-90.flex.items-center
  74. (shui/switch {:id id2 :size "sm" :default-checked toggle-checked?
  75. :disabled disabled? :on-click #(util/stop-propagation %)
  76. :on-checked-change (or on-toggle-checked-change identity)})]
  77. [:small [:span desc]
  78. (when disabled? (shui/tabler-icon "forbid-2" {:size 15}))]))])))
  79. (rum/defc choices-sub-pane
  80. [_property]
  81. [:div.ls-property-choices-sub-pane
  82. (dropdown-editor-menuitem {})])
  83. (rum/defc ui-position-sub-pane
  84. [_property {:keys [id set-sub-open!]}]
  85. (let [handle-select! (fn [^js e]
  86. (shui/toast! (.-innerText (.-target e)))
  87. (set-sub-open! false)
  88. (restore-root-highlight-item! id))
  89. item-props {:on-select handle-select!}]
  90. [:div.ls-property-dropdown-editor.ls-property-ui-position-sub-pane
  91. (dropdown-editor-menuitem {:icon :layout-distribute-horizontal :title "Block properties" :item-props item-props})
  92. (dropdown-editor-menuitem {:icon :layout-align-right :title "Beginning of the block" :item-props item-props})
  93. (dropdown-editor-menuitem {:icon :layout-align-left :title "End of the block" :item-props item-props})
  94. (dropdown-editor-menuitem {:icon :layout-align-top :title "Below of the block" :item-props item-props})]))
  95. (rum/defc dropdown-editor-impl
  96. "popup-id: dropdown popup id
  97. property: block entity"
  98. [popup-id property]
  99. (let [title (:block/title property)
  100. icon (:logseq.property/icon property)
  101. icon (when icon (icon-component/icon icon {:size 15}))]
  102. [:<>
  103. (dropdown-editor-menuitem {:icon :edit :title "Property name" :desc [:span.flex.items-center.gap-1 icon title]
  104. :submenu-content (fn [] (name-edit-pane property))})
  105. (dropdown-editor-menuitem {:icon :hash :title "Schema type" :desc "Date" :disabled? true})
  106. (dropdown-editor-menuitem {:icon :list :title "Available choices" :desc "4 choices"
  107. :submenu-content (fn [] (choices-sub-pane property))})
  108. (dropdown-editor-menuitem {:icon :checks :title "Multiple values" :toggle-checked? true :disabled? true
  109. :on-toggle-checked-change (fn [v] (shui/toast! (str title ": " v)))})
  110. (shui/dropdown-menu-separator)
  111. (dropdown-editor-menuitem {:icon :float-left :title "UI position" :desc "beginning of the block"
  112. :item-props {:class "ui__position-trigger-item"}
  113. :submenu-content (fn [ops] (ui-position-sub-pane property ops))})
  114. (dropdown-editor-menuitem {:icon :eye-off :title "Hide by default" :toggle-checked? false
  115. :on-toggle-checked-change (fn [v] (shui/toast! (str title ": " v)))})
  116. (shui/dropdown-menu-separator)
  117. (dropdown-editor-menuitem
  118. {:id :remove-property :icon :square-x :title "Remove property" :desc "" :disabled? false
  119. :item-props {:class "opacity-50 focus:opacity-100 focus:!text-red-rx-08"
  120. :on-select (fn [^js e]
  121. (util/stop e)
  122. (-> (shui/dialog-confirm! "remove?")
  123. (p/then (fn [] (shui/popup-hide-all!)))
  124. (p/catch (fn [] (restore-root-highlight-item! :remove-property)))))}})]))
  125. (rum/defc dropdown-editor < rum/reactive
  126. [popup-id property]
  127. (let [property1 (db/sub-block (:db/id property))]
  128. (dropdown-editor-impl popup-id property1)))