0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-19 13:25:59 -05:00

♻️ Refactor icons dashboard page.

This commit is contained in:
Andrey Antukh 2019-08-06 09:06:07 +02:00
parent c948082e0a
commit c9e88cfdb7
9 changed files with 326 additions and 334 deletions

View file

@ -110,6 +110,11 @@
justify-content: center; justify-content: center;
} }
.grid-item-icon {
width:90px;
height:90px;
}
.item-info { .item-info {
bottom: 0; bottom: 0;
display: flex; display: flex;

View file

@ -19,25 +19,14 @@
;; --- Initialize ;; --- Initialize
(declare fetch-icons) (defrecord Initialize []
(declare fetch-collections)
(declare collections-fetched?)
(defrecord Initialize [type id]
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(-> state (assoc-in state [:dashboard :icons] {:selected #{}})))
(assoc-in [:dashboard :icons] {:selected #{}})
(assoc-in [:dashboard :section] :dashboard/icons)))
ptk/WatchEvent
(watch [_ state s]
(rx/merge (rx/of (fetch-collections))
(rx/of (fetch-icons id)))))
(defn initialize (defn initialize
[type id] []
(Initialize. type id)) (Initialize.))
;; --- Select a Collection ;; --- Select a Collection
@ -236,7 +225,7 @@
;; TODO Keep the name of the original icon ;; TODO Keep the name of the original icon
:name (str "Icon " (gensym "i")) :name (str "Icon " (gensym "i"))
:metadata metadata})] :metadata metadata})]
(->> (rx/from-coll (jscoll->vec files)) (->> (rx/from-coll files)
(rx/filter allowed?) (rx/filter allowed?)
(rx/flat-map parse) (rx/flat-map parse)
(rx/map prepare) (rx/map prepare)

View file

@ -44,7 +44,7 @@
"ds.your-icons-title" "YOUR ICONS" "ds.your-icons-title" "YOUR ICONS"
"ds.store-icons-title" "ICONS STORE" "ds.store-icons-title" "ICONS STORE"
"ds.icons-collection.new" "+ New collection" "ds.icons-collection.new" "+ New collection"
"ds.icon.new" "+ New icon" "ds.icon-new" "+ New icon"
"ds.images" "IMAGES" "ds.images" "IMAGES"
"ds.num-images" ["No images" "ds.num-images" ["No images"

View file

@ -44,7 +44,7 @@
"ds.your-icons-title" "VOS ICÔNES" "ds.your-icons-title" "VOS ICÔNES"
"ds.store-icons-title" "BOUTIQUE" "ds.store-icons-title" "BOUTIQUE"
"ds.icons-collection.new" "+ Nouvelle collection" "ds.icons-collection.new" "+ Nouvelle collection"
"ds.icon.new" "+ Nouvel icône" "ds.icon-new" "+ Nouvel icône"
"ds.images" "IMAGES" "ds.images" "IMAGES"
"ds.num-images" ["Nb images" "ds.num-images" ["Nb images"

View file

@ -9,23 +9,19 @@
(:require (:require
[cuerdas.core :as str] [cuerdas.core :as str]
[lentes.core :as l] [lentes.core :as l]
[potok.core :as ptk]
[rumext.core :as mx]
[rumext.alpha :as mf] [rumext.alpha :as mf]
[uxbox.builtins.icons :as i] [uxbox.builtins.icons :as i]
[uxbox.main.data.icons :as di] [uxbox.main.data.icons :as di]
[uxbox.main.data.lightbox :as udl]
[uxbox.main.store :as st] [uxbox.main.store :as st]
[uxbox.main.ui.dashboard.header :refer (header)] [uxbox.main.ui.confirm :refer [confirm-dialog]]
[uxbox.main.ui.dashboard.common :as common]
[uxbox.main.ui.keyboard :as kbd] [uxbox.main.ui.keyboard :as kbd]
[uxbox.main.ui.lightbox :as lbx] [uxbox.main.ui.modal :as modal]
[uxbox.main.ui.shapes.icon :as icon] [uxbox.main.ui.shapes.icon :as icon]
[uxbox.util.data :refer (read-string)] [uxbox.util.components :refer [chunked-list]]
[uxbox.util.data :refer [read-string jscoll->vec seek]]
[uxbox.util.dom :as dom] [uxbox.util.dom :as dom]
[uxbox.util.forms :as sc] [uxbox.util.i18n :as t :refer [tr]]
[uxbox.util.i18n :as t :refer (tr)]
[uxbox.util.i18n :refer (tr)]
[uxbox.util.lens :as ul]
[uxbox.util.router :as rt] [uxbox.util.router :as rt]
[uxbox.util.time :as dt])) [uxbox.util.time :as dt]))
@ -57,111 +53,67 @@
;; --- Refs ;; --- Refs
(def collections-ref (def collections-iref
(-> (l/key :icons-collections) (-> (l/key :icons-collections)
(l/derive st/state))) (l/derive st/state)))
(def opts-ref (def opts-iref
(-> (l/in [:dashboard :icons]) (-> (l/in [:dashboard :icons])
(l/derive st/state))) (l/derive st/state)))
;; TODO: remove when sidebar is refactored
(def icons-ref
(-> (l/key :icons)
(l/derive st/state)))
;; --- Page Title ;; --- Page Title
(mf/def page-title (mf/defc grid-header
:mixins [(mf/local) mf/memo] [{:keys [coll] :as props}]
(letfn [(on-change [name]
:render (st/emit! (di/rename-collection (:id coll) name)))
(fn [{:keys [::mf/local] :as own} {:keys [id type] :as coll}]
(let [own? (= :own (:type coll))
edit? (:edit @local)]
(letfn [(on-save [e]
(let [dom (mx/ref-node own "input")
name (.-innerText dom)]
(st/emit! (di/rename-collection id (str/trim name)))
(swap! local assoc :edit false)))
(on-cancel [e]
(swap! local assoc :edit false))
(on-edit [e]
(swap! local assoc :edit true))
(on-input-keydown [e]
(cond
(kbd/esc? e) (on-cancel e)
(kbd/enter? e)
(do
(dom/prevent-default e)
(dom/stop-propagation e)
(on-save e))))
(delete [] (delete []
(st/emit! (di/delete-collection id))) (st/emit!
(di/delete-collection (:id coll))
(rt/nav :dashboard/icons nil {:type (:type coll)})))
(on-delete [] (on-delete []
(udl/open! :confirm {:on-accept delete}))] (modal/show! confirm-dialog {:on-accept delete}))]
[:div.dashboard-title [:& common/grid-header {:value (:name coll)
[:h2 :on-change on-change
(if edit? :on-delete on-delete}]))
[:div.dashboard-title-field
[:span.edit
{:content-editable true
:ref "input"
:on-key-down on-input-keydown}
(:name coll)]
[:span.close {:on-click on-cancel} i/close]]
(if own?
[:span.dashboard-title-field
{:on-double-click on-edit}
(:name coll)]
[:span.dashboard-title-field
(:name coll "Storage")]))]
(when (and own? coll)
[:div.edition
(if edit?
[:span {:on-click on-save} i/save]
[:span {:on-click on-edit} i/pencil])
[:span {:on-click on-delete} i/trash]])]))))
;; --- Nav ;; --- Nav
(defn num-icons-ref (defn- make-num-icons-iref
[id] [id]
(let [selector (fn [icons] (count (filter #(= id (:collection %)) (vals icons))))] (letfn [(selector [icons]
(->> (vals icons)
(filter #(= id (:collection %)))
(count)))]
(-> (comp (l/key :icons) (-> (comp (l/key :icons)
(l/lens selector)) (l/lens selector))
(l/derive st/state)))) (l/derive st/state))))
(mf/def nav-item (mf/defc nav-item
:key-fn :id [{:keys [coll selected?] :as props}]
:mixins [(mf/local) mf/memo mf/reactive] (let [local (mf/use-state {})
{:keys [id type name num-icons]} coll
:init ;; TODO: recalculate the num-icons on crud operations for
(fn [own {:keys [id] :as props}] ;; avod doing this on UI.
(assoc own ::num-icons-ref (num-icons-ref id))) ;; num-icons-iref (mf/use-memo {:deps #js [id]
;; :init #(make-num-icons-iref (:id coll))})
:render ;; num-icons (mf/deref num-icons-iref)
(fn [{:keys [::mf/local] :as own}
{:keys [id type name num-icons ::selected?] :as coll}]
(let [num-icons (or num-icons (mf/react (::num-icons-ref own)))
editable? (= type :own)] editable? (= type :own)]
(letfn [(on-click [event] (letfn [(on-click [event]
(let [type (or type :own)] (let [type (or type :own)]
(st/emit! (rt/navigate :dashboard/icons {} {:type type :id id})))) (st/emit! (rt/nav :dashboard/icons {} {:type type :id id}))))
(on-input-change [event] (on-input-change [event]
(let [value (dom/get-target event) (-> (dom/get-target event)
value (dom/get-value value)] (dom/get-value)
(swap! local assoc :name value))) (swap! local assoc :name)))
(on-cancel [event] (on-cancel [event]
(swap! local dissoc :name) (swap! local dissoc :name :edit))
(swap! local dissoc :edit))
(on-double-click [event] (on-double-click [event]
(when editable? (when editable?
(swap! local assoc :edit true))) (swap! local assoc :edit true)))
(on-input-keyup [event] (on-input-keyup [event]
(when (kbd/enter? event) (when (kbd/enter? event)
(let [value (dom/get-target event) (let [value (-> (dom/get-target event) (dom/get-value))]
value (dom/get-value value)]
(st/emit! (di/rename-collection id (str/trim (:name @local)))) (st/emit! (di/rename-collection id (str/trim (:name @local))))
(swap! local assoc :edit false))))] (swap! local assoc :edit false))))]
[:li {:on-click on-click [:li {:on-click on-click
@ -169,31 +121,21 @@
:class-name (when selected? "current")} :class-name (when selected? "current")}
(if (:edit @local) (if (:edit @local)
[:div [:div
[:input.element-title [:input.element-title {:value (if (:name @local)
{:value (if (:name @local) (:name @local) (if id name "Storage")) (:name @local)
(if id name "Storage"))
:on-change on-input-change :on-change on-input-change
:on-key-down on-input-keyup}] :on-key-down on-input-keyup}]
[:span.close {:on-click on-cancel} i/close]] [:span.close {:on-click on-cancel} i/close]]
[:span.element-title [:span.element-title (if id name "Storage")])
(if id name "Storage")]) [:span.element-subtitle (tr "ds.num-elements" (t/c num-icons))]])))
[:span.element-subtitle
(tr "ds.num-elements" (t/c num-icons))]]))))
(mf/def nav
:mixins [mf/memo mf/reactive]
:render (mf/defc nav
(fn [own {:keys [id type] :as props}] [{:keys [id type colls selected-coll] :as props}]
(let [own? (= type :own) (let [own? (= type :own)
builtin? (= type :builtin) builtin? (= type :builtin)
colls (mf/react collections-ref) select-tab #(st/emit! (rt/nav :dashboard/icons nil {:type %}))]
select-tab (fn [type]
(if-let [coll (->> (vals colls)
(filter #(= type (:type %)))
(sort-by :created-at)
(first))]
(st/emit! (rt/nav :dashboard/icons nil {:type type :id (:id coll)}))
(st/emit! (rt/nav :dashboard/icons nil {:type type}))))]
[:div.library-bar [:div.library-bar
[:div.library-bar-inside [:div.library-bar-inside
[:ul.library-tabs [:ul.library-tabs
@ -203,40 +145,41 @@
[:li {:class-name (when builtin? "current") [:li {:class-name (when builtin? "current")
:on-click (partial select-tab :builtin)} :on-click (partial select-tab :builtin)}
(tr "ds.store-icons-title")]] (tr "ds.store-icons-title")]]
[:ul.library-elements [:ul.library-elements
(when own? (when own?
[:li [:li
[:a.btn-primary {:on-click #(st/emit! (di/create-collection))} [:a.btn-primary {:on-click #(st/emit! (di/create-collection))}
(tr "ds.icons-collection.new")]]) (tr "ds.icons-collection.new")]])
(when own? (when own?
(nav-item {::selected? (nil? id)})) [:& nav-item {:selected? (nil? id)}])
(for [coll (cond->> (vals colls) (for [item colls]
own? (filter #(= :own (:type %))) [:& nav-item {:coll item
builtin? (filter #(= :builtin (:type %))) :selected? (= (:id item) id)
true (sort-by :name))] :key (:id item)}])]]]))
(let [selected? (= (:id coll) id)]
(nav-item (assoc coll ::selected? selected?))))]]])))
;; --- Grid ;; --- Grid
(mx/defcs grid-form (mf/defc grid-form
{:mixins [mx/static]} [{:keys [id type uploading?] :as props}]
[own coll-id] (let [input (mf/use-ref* nil)
(letfn [(forward-click [event] on-click #(dom/click (mf/ref-node input))
(dom/click (mx/ref-node own "file-input"))) on-select #(st/emit! (->> (dom/get-event-files %)
(on-file-selected [event] (jscoll->vec)
(let [files (dom/get-event-files event)] (di/create-icons id)))]
(st/emit! (di/create-icons coll-id files))))] [:div.grid-item.add-project {:on-click on-click}
[:div.grid-item.small-item.add-project {:on-click forward-click} (if uploading?
[:span (tr "ds.icon.new")] [:div i/loader-pencil]
[:input.upload-image-input [:span (tr "ds.icon-new")])
[:input.upload-icon-input
{:style {:display "none"} {:style {:display "none"}
:multiple true :multiple true
:ref "file-input" :ref input
:value "" :value ""
:accept "image/svg+xml" :accept "icon/svg+xml"
:type "file" :type "file"
:on-change on-file-selected}]])) :on-change on-select}]]))
(mf/def grid-options-tooltip (mf/def grid-options-tooltip
:mixins [mf/reactive mf/memo] :mixins [mf/reactive mf/memo]
@ -246,7 +189,7 @@
{:pre [(uuid? selected) {:pre [(uuid? selected)
(fn? on-select) (fn? on-select)
(string? title)]} (string? title)]}
(let [colls (mf/react collections-ref) (let [colls (mf/react collections-iref)
colls (->> (vals colls) colls (->> (vals colls)
(filter #(= :own (:type %))) (filter #(= :own (:type %)))
(remove #(= selected (:id %))) (remove #(= selected (:id %)))
@ -272,7 +215,7 @@
(letfn [(delete [] (letfn [(delete []
(st/emit! (di/delete-selected))) (st/emit! (di/delete-selected)))
(on-delete [event] (on-delete [event]
(udl/open! :confirm {:on-accept delete})) (modal/show! confirm-dialog {:on-accept delete}))
(on-toggle-copy [event] (on-toggle-copy [event]
(swap! local update :show-copy-tooltip not)) (swap! local update :show-copy-tooltip not))
(on-toggle-move [event] (on-toggle-move [event]
@ -333,14 +276,12 @@
:on-select on-copy})) :on-select on-copy}))
i/organize]])]))) i/organize]])])))
(mf/def grid-item ;; --- Grid Item
:key-fn :id
:mixins [mf/memo] (mf/defc grid-item
:render [{:keys [icon selected? edition?] :as props}]
(fn [own {:keys [id created-at ::selected? ::edition?] :as icon}]
(letfn [(toggle-selection [event] (letfn [(toggle-selection [event]
(prn "toggle-selection") (st/emit! (di/toggle-icon-selection (:id icon))))
(st/emit! (di/toggle-icon-selection id)))
(on-key-down [event] (on-key-down [event]
(when (kbd/enter? event) (when (kbd/enter? event)
(on-blur event))) (on-blur event)))
@ -348,22 +289,22 @@
(let [target (dom/event->target event) (let [target (dom/event->target event)
name (dom/get-value target)] name (dom/get-value target)]
(st/emit! (di/update-opts :edition false) (st/emit! (di/update-opts :edition false)
(di/rename-icon id name)))) (di/rename-icon (:id icon) name))))
(ignore-click [event] (ignore-click [event]
(dom/stop-propagation event) (dom/stop-propagation event)
(dom/prevent-default event)) (dom/prevent-default event))
(on-edit [event] (on-edit [event]
(dom/stop-propagation event) (dom/stop-propagation event)
(dom/prevent-default event) (dom/prevent-default event)
(st/emit! (di/update-opts :edition id)))] (st/emit! (di/update-opts :edition (:id icon))))]
[:div.grid-item.small-item.project-th {:id (str "grid-item-" id)} [:div.grid-item.small-item.project-th
[:div.input-checkbox.check-primary [:div.input-checkbox.check-primary
[:input {:type "checkbox" [:input {:type "checkbox"
:id (:id icon) :id (:id icon)
:on-change toggle-selection :on-change toggle-selection
:checked selected?}] :checked selected?}]
[:label {:for (:id icon)}]] [:label {:for (:id icon)}]]
[:span.grid-item-image (icon/icon-svg icon)] [:span.grid-item-icon (icon/icon-svg icon)]
[:div.item-info {:on-click ignore-click} [:div.item-info {:on-click ignore-click}
(if edition? (if edition?
[:input.element-name {:type "text" [:input.element-name {:type "text"
@ -374,47 +315,47 @@
:default-value (:name icon)}] :default-value (:name icon)}]
[:h3 {:on-double-click on-edit} [:h3 {:on-double-click on-edit}
(:name icon)]) (:name icon)])
(str (tr "ds.uploaded-at" (dt/format created-at "DD/MM/YYYY")))]]))) (str (tr "ds.uploaded-at" (dt/format (:created-at icon) "DD/MM/YYYY")))]]))
(mf/def grid ;; --- Grid
:mixins [mf/reactive]
:init (defn- make-icons-iref
(fn [own {:keys [id] :as props}] [id]
(let [selector (fn [icons] (-> (comp (l/key :icons)
(l/lens (fn [icons]
(->> (vals icons) (->> (vals icons)
(filter #(= id (:collection %)))))] (filter #(= id (:collection %)))))))
(assoc own ::icons-ref (-> (comp (l/key :icons) (l/derive st/state)))
(l/lens selector))
(l/derive st/state)))))
:render (mf/defc grid
(fn [own {:keys [selected edition id type] :as props}] [{:keys [id type coll opts] :as props}]
(let [editable? (or (= type :own) (nil? id)) (let [editable? (or (= type :own) (nil? id))
icons (->> (mf/react (::icons-ref own)) icons-iref (mf/use-memo {:deps #js [id] :init #(make-icons-iref id)})
(filter-icons-by (:filter props "")) icons (->> (mf/deref icons-iref)
(sort-icons-by (:order props :name)))] (filter-icons-by (:filter opts ""))
(sort-icons-by (:order opts :name)))]
[:div.dashboard-grid-content [:div.dashboard-grid-content
[:div.dashboard-grid-row [:div.dashboard-grid-row
(when editable? (grid-form id)) (when editable?
(for [icon icons] [:& grid-form {:id id :type type :uploading? (:uploading opts)}])
(let [edition? (= edition (:id icon))
selected? (contains? selected (:id icon))] [:& chunked-list {:items icons
(grid-item (assoc icon ::selected? selected? ::edition? edition?))))]]))) :initial-size 30
:chunk-size 30
:key (str type id (count icons))}
(fn [icon]
[:& grid-item {:icon icon
:key (:id icon)
:selected (contains? (:selected opts) (:id icon))
:edition? (= (:edition opts) (:id icon))}])]]]))
;; --- Menu ;; --- Menu
(mf/def menu (mf/defc menu
:mixins [mf/memo mf/reactive] [{:keys [opts coll] :as props}]
(let [ordering (:order opts :name)
:init filtering (:filter opts "")
(fn [own {:keys [id] :as props}] icount (count (:icons coll))]
(assoc own ::num-icons-ref (num-icons-ref id)))
:render
(fn [own props]
(let [{:keys [id num-icons] :as coll} (::coll props)
num-icons (or num-icons (mf/react (::num-icons-ref own)))]
(letfn [(on-term-change [event] (letfn [(on-term-change [event]
(let [term (-> (dom/get-target event) (let [term (-> (dom/get-target event)
(dom/get-value))] (dom/get-value))]
@ -427,16 +368,18 @@
(st/emit! (di/update-opts :filter "")))] (st/emit! (di/update-opts :filter "")))]
[:section.dashboard-bar.library-gap [:section.dashboard-bar.library-gap
[:div.dashboard-info [:div.dashboard-info
;; Counter ;; Counter
[:span.dashboard-icons (tr "ds.num-icons" (t/c num-icons))] [:span.dashboard-icons (tr "ds.num-icons" (t/c icount))]
;; Sorting ;; Sorting
[:div [:div
[:span (tr "ds.ordering")] [:span (tr "ds.ordering")]
[:select.input-select {:on-change on-ordering-change [:select.input-select {:on-change on-ordering-change
:value (pr-str (:order props :name))} :value (pr-str ordering)}
(for [[key value] (seq +ordering-options+)] (for [[key value] (seq +ordering-options+)]
[:option {:key key :value (pr-str key)} (tr value)])]] [:option {:key key :value (pr-str key)} (tr value)])]]
;; Search ;; Search
[:form.dashboard-search [:form.dashboard-search
[:input.input-text {:key :icons-search-box [:input.input-text {:key :icons-search-box
@ -444,48 +387,53 @@
:on-change on-term-change :on-change on-term-change
:auto-focus true :auto-focus true
:placeholder (tr "ds.search.placeholder") :placeholder (tr "ds.search.placeholder")
:value (:filter props "")}] :value filtering}]
[:div.clear-search {:on-click on-clear} [:div.clear-search {:on-click on-clear} i/close]]]])))
i/close]]]]))))
(mf/def content ;; --- Content
:mixins [mf/reactive mf/memo]
:init (mf/defc content
(fn [own {:keys [id] :as props}] [{:keys [id type coll] :as props}]
(assoc own ::coll-ref (-> (l/in [:icons-collections id]) (let [opts (mf/deref opts-iref)]
(l/derive st/state))))
:render
(fn [own props]
(let [opts (mf/react opts-ref)
coll (mf/react (::coll-ref own))
props (merge opts props)]
[:* [:*
(menu (assoc props ::coll coll)) [:& menu {:opts opts :coll coll}]
[:section.dashboard-grid.library [:section.dashboard-grid.library
(page-title coll) (when coll
(grid props) [:& grid-header {:coll coll}])
[:& grid {:id id
:key [id type]
:type type
:coll coll
:opts opts}]
(when (seq (:selected opts)) (when (seq (:selected opts))
(grid-options props))]]))) [:& grid-options {:id id :type type :selected (:selected opts)}])]]))
;; --- Icons Page ;; --- Icons Page
(mf/def icons-page (mf/defc icons-page
:key-fn identity [{:keys [id type] :as props}]
:mixins #{mf/memo mf/reactive}
:init
(fn [own props]
(let [{:keys [type id]} (::mf/props own)]
(st/emit! (di/initialize type id))
own))
:render
(fn [own {:keys [type] :as props}]
(let [type (or type :own) (let [type (or type :own)
props (assoc props :type type)] colls (mf/deref collections-iref)
colls (cond->> (vals colls)
(= type :own) (filter #(= :own (:type %)))
(= type :builtin) (filter #(= :builtin (:type %)))
true (sort-by :created-at))
selected-coll (cond
(and (= type :own) (nil? id)) nil
(uuid? id) (seek #(= id (:id %)) colls)
:else (first colls))
id (:id selected-coll)]
(mf/use-effect {:init #(st/emit! (di/fetch-collections))})
(mf/use-effect {:init #(st/emit! (di/initialize)
(di/fetch-icons id))
:deps #js [id type]})
[:section.dashboard-content [:section.dashboard-content
(nav props) [:& nav {:type type
(content props)]))) :id id
:colls colls
:selected-coll selected-coll}]
[:& content {:type type
:id id
:coll selected-coll}]]))

View file

@ -15,7 +15,6 @@
[uxbox.main.data.images :as di] [uxbox.main.data.images :as di]
[uxbox.main.data.lightbox :as udl] [uxbox.main.data.lightbox :as udl]
[uxbox.main.store :as st] [uxbox.main.store :as st]
[uxbox.main.ui.dashboard.header :refer [header]]
[uxbox.main.ui.dashboard.common :as common] [uxbox.main.ui.dashboard.common :as common]
[uxbox.main.ui.keyboard :as kbd] [uxbox.main.ui.keyboard :as kbd]
[uxbox.main.ui.lightbox :as lbx] [uxbox.main.ui.lightbox :as lbx]
@ -53,11 +52,11 @@
;; --- Refs ;; --- Refs
(def collections-ref (def collections-iref
(-> (l/key :images-collections) (-> (l/key :images-collections)
(l/derive st/state))) (l/derive st/state)))
(def opts-ref (def opts-iref
(-> (l/in [:dashboard :images]) (-> (l/in [:dashboard :images])
(l/derive st/state))) (l/derive st/state)))
@ -71,7 +70,7 @@
(delete [] (delete []
(st/emit! (st/emit!
(di/delete-collection (:id coll)) (di/delete-collection (:id coll))
(rt/nav :dashboard/mages nil {:type (:type coll)}))) (rt/nav :dashboard/images nil {:type (:type coll)})))
(on-delete [] (on-delete []
(modal/show! confirm-dialog {:on-accept delete}))] (modal/show! confirm-dialog {:on-accept delete}))]
@ -166,7 +165,7 @@
{:pre [(uuid? selected) {:pre [(uuid? selected)
(fn? on-select) (fn? on-select)
(string? title)]} (string? title)]}
(let [colls (mf/deref collections-ref) (let [colls (mf/deref collections-iref)
colls (->> (vals colls) colls (->> (vals colls)
(filter #(= :own (:type %))) (filter #(= :own (:type %)))
(remove #(= selected (:id %))) (remove #(= selected (:id %)))
@ -190,7 +189,7 @@
(letfn [(delete [] (letfn [(delete []
(st/emit! (di/delete-selected))) (st/emit! (di/delete-selected)))
(on-delete [event] (on-delete [event]
(udl/open! :confirm {:on-accept delete})) (modal/show! confirm-dialog {:on-accept delete}))
(on-toggle-copy [event] (on-toggle-copy [event]
(swap! local update :show-copy-tooltip not)) (swap! local update :show-copy-tooltip not))
(on-toggle-move [event] (on-toggle-move [event]
@ -283,6 +282,8 @@
[:span.date (str (tr "ds.uploaded-at" [:span.date (str (tr "ds.uploaded-at"
(dt/format (:created-at image) "DD/MM/YYYY")))]]])) (dt/format (:created-at image) "DD/MM/YYYY")))]]]))
;; --- Grid Form
(mf/defc grid-form (mf/defc grid-form
[{:keys [id type uploading?] :as props}] [{:keys [id type uploading?] :as props}]
(let [input (mf/use-ref* nil) (let [input (mf/use-ref* nil)
@ -303,6 +304,8 @@
:type "file" :type "file"
:on-change on-select}]])) :on-change on-select}]]))
;; --- Grid
(defn- make-images-iref (defn- make-images-iref
[id] [id]
(-> (comp (l/key :images) (-> (comp (l/key :images)
@ -371,7 +374,7 @@
(mf/defc content (mf/defc content
[{:keys [id type coll] :as props}] [{:keys [id type coll] :as props}]
(let [opts (mf/deref opts-ref)] (let [opts (mf/deref opts-iref)]
[:* [:*
[:& menu {:opts opts :coll coll}] [:& menu {:opts opts :coll coll}]
[:section.dashboard-grid.library [:section.dashboard-grid.library
@ -389,7 +392,7 @@
(mf/defc images-page (mf/defc images-page
[{:keys [id type] :as props}] [{:keys [id type] :as props}]
(let [type (or type :own) (let [type (or type :own)
colls (mf/deref collections-ref) colls (mf/deref collections-iref)
colls (cond->> (vals colls) colls (cond->> (vals colls)
(= type :own) (filter #(= :own (:type %))) (= type :own) (filter #(= :own (:type %)))
(= type :builtin) (filter #(= :builtin (:type %))) (= type :builtin) (filter #(= :builtin (:type %)))

View file

@ -50,7 +50,7 @@
{:mixins [mx/static mx/reactive] {:mixins [mx/static mx/reactive]
:init icons-toolbox-init} :init icons-toolbox-init}
[] []
(let [drawing (mx/react drawing-shape-ref) #_(let [drawing (mx/react drawing-shape-ref)
selected (mx/react icons-toolbox-ref) selected (mx/react icons-toolbox-ref)
colls (mx/react icons/collections-ref) colls (mx/react icons/collections-ref)
selected-coll (get colls selected) selected-coll (get colls selected)

View file

@ -0,0 +1,40 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2019 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.util.components
"A collection of general purpose utility components."
(:require
[beicon.core :as rx]
[rumext.alpha :as mf]
[uxbox.util.timers :refer [schedule-on-idle]]))
(mf/defc chunked-list
[{:keys [items children initial-size chunk-size]
:or {initial-size 30 chunk-size 5}
:as props}]
(letfn [(initial-state []
(let [total (count items)
size (if (> total initial-size) initial-size total)
current (take size items)
pending (drop size items)]
{:current (vec current)
:pending pending
:pending-num (- total size)}))
(update-state [{:keys [current pending pending-num] :as state}]
(let [chunk-size (if (> pending-num chunk-size) chunk-size pending-num)]
{:current (into current (take chunk-size pending))
:pending (drop chunk-size pending)
:pending-num (- pending-num chunk-size)}))]
(let [initial (mf/use-memo {:init initial-state})
state (mf/use-state initial)]
(mf/use-effect {:deps true
:init #(when (pos? (:pending-num @state))
(swap! state update-state))
:end #(when % (rx/cancel! %))})
(for [item (:current @state)]
(children item)))))

View file

@ -2,7 +2,7 @@
;; License, v. 2.0. If a copy of the MPL was not distributed with this ;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/. ;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; ;;
;; Copyright (c) 2016-2017 Andrey Antukh <niwi@niwi.nz> ;; Copyright (c) 2016-2019 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.util.timers (ns uxbox.util.timers
(:require [beicon.core :as rx])) (:require [beicon.core :as rx]))
@ -20,3 +20,10 @@
(reify rx/ICancellable (reify rx/ICancellable
(-cancel [_] (-cancel [_]
(js/clearInterval sem))))) (js/clearInterval sem)))))
(defn schedule-on-idle
[func]
(let [sem (js/requestIdleCallback #(func))]
(reify rx/ICancellable
(-cancel [_]
(js/cancelIdleCallback sem)))))