0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-12 10:09:03 -05:00

Improve autoselect collection with icons.

This commit is contained in:
Andrey Antukh 2017-01-09 20:23:29 +01:00
parent dfa8814887
commit 8352cc75d9
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
3 changed files with 74 additions and 46 deletions

View file

@ -57,7 +57,10 @@
;; --- Collections Fetched
(defrecord CollectionsFetched [items]
(deftype CollectionsFetched [items]
cljs.core/IDeref
(-deref [_] items)
ptk/UpdateEvent
(update [_ state]
(reduce (fn [state {:keys [id user] :as item}]

View file

@ -16,6 +16,7 @@
[uxbox.main.data.projects :as dp]
[uxbox.main.data.pages :as udp]
[uxbox.main.data.shapes :as uds]
[uxbox.main.data.icons :as udi]
[uxbox.main.data.shapes-impl :as shimpl]
[uxbox.main.data.lightbox :as udl]
[uxbox.main.data.history :as udh]
@ -143,6 +144,54 @@
[flag]
(ToggleFlag. flag))
;; --- Icons Toolbox
(deftype SelectIconsToolboxCollection [id]
ptk/UpdateEvent
(update [_ state]
(assoc-in state [:workspace :icons-toolbox] id))
ptk/WatchEvent
(watch [_ state stream]
(rx/of (udi/fetch-icons id))))
(defn select-icons-toolbox-collection
[id]
{:pre [(or (nil? id) (uuid? id))]}
(SelectIconsToolboxCollection. id))
(deftype InitializeIconsToolbox []
ptk/UpdateEvent
(update [_ state]
state)
ptk/WatchEvent
(watch [_ state stream]
(letfn [(get-first-with-icons [colls]
(->> (sort-by :name colls)
(filter #(> (:num-icons %) 0))
(first)
(:id)))
(on-fetched [event]
(let [coll (get-first-with-icons @event)]
(println "first" coll)
(select-icons-toolbox-collection coll)))]
(rx/merge
(rx/of (udi/fetch-collections)
(udi/fetch-icons nil))
;; Only perform the autoselection if it is not
;; previously already selected by the user.
(when-not (contains? (:workspace state) :icons-toolbox)
(->> stream
(rx/filter udi/collections-fetched?)
(rx/take 1)
(rx/map on-fetched)))))))
(defn initialize-icons-toolbox
[]
(InitializeIconsToolbox.))
;; --- Copy to Clipboard
(defrecord CopyToClipboard []

View file

@ -11,7 +11,7 @@
[potok.core :as ptk]
[uxbox.store :as st]
[uxbox.main.lenses :as ul]
[uxbox.main.data.workspace :as dw]
[uxbox.main.data.workspace :as udw]
[uxbox.main.data.icons :as udi]
[uxbox.main.ui.shapes.icon :as icon]
[uxbox.main.ui.workspace.base :as wb]
@ -30,6 +30,10 @@
change."
(l/derive ul/selected-drawing st/state))
(def ^:private icons-toolbox-ref
(-> (l/in [:workspace :icons-toolbox])
(l/derive st/state)))
;; --- Icons (Component)
(mx/defc icon-wrapper
@ -39,58 +43,30 @@
(defn- icons-toolbox-will-mount
[own]
(let [local (:rum/local own)]
(st/emit! (udi/fetch-collections))
(st/emit! (udi/fetch-icons nil))
(add-watch local ::key (fn [_ _ _ {:keys [id]}]
(st/emit! (udi/fetch-icons id))))
own))
(st/emit! (udw/initialize-icons-toolbox))
own)
(defn- icons-toolbox-will-unmount
[own]
(let [local (:rum/local own)]
(remove-watch local ::key)
own))
(defn- get-first-with-icons
"Get a first collection with icons."
[colls-map]
(->> (vals colls-map)
(sort-by :name)
(filter #(> (:num-icons %) 0))
(first)))
(defn- get-or-select-coll
[local colls]
{:pre [(map? colls)]}
(let [selected (:id @local)]
(if selected
(get colls selected)
(let [coll (get-first-with-icons colls)]
(swap! local assoc :id (:d coll))
coll))))
(mx/defcs icons-toolbox
{:mixins [(mx/local) mx/reactive]
:will-mount icons-toolbox-will-mount
:will-unmount icons-toolbox-will-unmount}
[{:keys [rum/local] :as own}]
(mx/defc icons-toolbox
{:mixins [mx/static mx/reactive]
:will-mount icons-toolbox-will-mount}
[]
(let [drawing (mx/react drawing-shape-ref)
colls-map (mx/react icons/collections-ref)
selected-coll (get-or-select-coll local colls-map)
colls (->> (vals colls-map)
selected (mx/react icons-toolbox-ref)
colls (mx/react icons/collections-ref)
selected-coll (get colls selected)
colls (->> (vals (mx/react icons/collections-ref))
(sort-by :name))
icons (->> (vals (mx/react icons/icons-ref))
(filter #(= (:id selected-coll) (:collection %))))]
(letfn [(on-close [event]
(st/emit! (dw/toggle-flag :icons)))
(st/emit! (udw/toggle-flag :icons)))
(on-select [icon event]
(st/emit! (dw/select-for-drawing icon)))
(st/emit! (udw/select-for-drawing icon)))
(on-change [event]
(let [value (-> (dom/event->value event)
(read-string))]
(swap! local assoc :id value)
(st/emit! (dw/select-for-drawing nil))))]
(let [value (read-string (dom/event->value event))]
(st/emit! (udw/select-for-drawing nil)
(udw/select-icons-toolbox-collection value))))]
[:div#form-figures.tool-window
[:div.tool-window-bar
[:div.tool-window-icon i/icon-set]