0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-23 23:18:48 -05:00

Add the ability to rename an image (on images dashboard page).

This commit is contained in:
Andrey Antukh 2016-11-10 18:41:20 +01:00
parent 351db464a5
commit 477d31ce53
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
2 changed files with 54 additions and 14 deletions

View file

@ -234,30 +234,30 @@
;; --- Image Updated ;; --- Image Updated
(defrecord ImageUpdated [id data] (defrecord ImagePersisted [id data]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:images id] data))) (assoc-in state [:images id] data)))
(defn image-updated (defn image-persisted
[{:keys [id] :as data}] [{:keys [id] :as data}]
{:pre [(map? data)]} {:pre [(map? data) (uuid? id)]}
(ImageUpdated. id data)) (ImagePersisted. id data))
;; --- Update Image ;; --- Update Image
(defrecord UpdateImage [id] (defrecord PersistImage [id]
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state stream] (-apply-watch [_ state stream]
(let [image (get-in state [:images id])] (let [image (get-in state [:images id])]
(->> (rp/req :update/image image) (->> (rp/req :update/image image)
(rx/map :payload) (rx/map :payload)
(rx/map image-updated))))) (rx/map image-persisted)))))
(defn update-image (defn persist-image
[id] [id]
{:pre [(uuid? id)]} {:pre [(uuid? id)]}
(UpdateImage. id)) (PersistImage. id))
;; --- Images Fetched ;; --- Images Fetched
@ -342,6 +342,22 @@
{:pre [(uuid? id)]} {:pre [(uuid? id)]}
(DeleteImage. id)) (DeleteImage. id))
;; --- Rename Image
(defrecord RenameImage [id name]
rs/UpdateEvent
(-apply-update [_ state]
(assoc-in state [:images id :name] name))
rs/WatchEvent
(-apply-watch [_ state stream]
(rx/of (persist-image id))))
(defn rename-image
[id name]
{:pre [(uuid? id) (string? name)]}
(RenameImage. id name))
;; --- Select image ;; --- Select image
(defrecord SelectImage [id] (defrecord SelectImage [id]
@ -407,7 +423,7 @@
(let [selected (get-in state [:dashboard :images :selected])] (let [selected (get-in state [:dashboard :images :selected])]
(rx/merge (rx/merge
(->> (rx/from-coll selected) (->> (rx/from-coll selected)
(rx/map update-image)) (rx/map persist-image))
(->> (rx/from-coll selected) (->> (rx/from-coll selected)
(rx/map deselect-image)))))) (rx/map deselect-image))))))

View file

@ -295,15 +295,32 @@
:on-select on-copy)) :on-select on-copy))
i/organize]])]))) i/organize]])])))
(mx/defc grid-item (mx/defcs grid-item
[{:keys [id created-at] :as image} selected?] {:mixins [mx/static (mx/local)]}
[{:keys [rum/local] :as own}
{:keys [id created-at] :as image} selected?]
(letfn [(toggle-selection [event] (letfn [(toggle-selection [event]
(rs/emit! (di/toggle-image-selection id))) (rs/emit! (di/toggle-image-selection id)))
(toggle-selection-shifted [event] (toggle-selection-shifted [event]
(when (kbd/shift? event) (when (kbd/shift? event)
(toggle-selection event)))] (toggle-selection event)))
(on-key-down [event]
(when (kbd/enter? event)
(on-blur event)))
(on-blur [event]
(let [target (dom/event->target event)
name (dom/get-value target)
id (:id image)]
(swap! local assoc :edition false)
(rs/emit! (di/rename-image id name))))
(on-edit [event]
(dom/stop-propagation event)
(dom/prevent-default event)
(swap! local assoc :edition true))]
[:div.grid-item.images-th [:div.grid-item.images-th
{:on-click toggle-selection-shifted} {:on-click toggle-selection-shifted
:on-double-click on-edit}
[:div.grid-item-th [:div.grid-item-th
{:style {:background-image (str "url('" (:thumbnail image) "')")}} {:style {:background-image (str "url('" (:thumbnail image) "')")}}
[:div.input-checkbox.check-primary [:div.input-checkbox.check-primary
@ -313,7 +330,14 @@
:checked selected?}] :checked selected?}]
[:label {:for (:id image)}]]] [:label {:for (:id image)}]]]
[:div.item-info [:div.item-info
[:h3 (:name image)] (if (:edition @local)
[:input {:type "text"
:auto-focus true
:on-key-down on-key-down
:on-blur on-blur
:on-click on-edit
:default-value (:name image)}]
[:h3 (:name image)])
[:span.date [:span.date
(str "Uploaded at " (dt/format created-at "L"))]]])) (str "Uploaded at " (dt/format created-at "L"))]]]))