Allows drag-drop files into dashboard

This commit is contained in:
alonso.torres 2021-07-06 16:43:13 +02:00
parent 1894fc7cfa
commit 60009476d6
10 changed files with 169 additions and 52 deletions

View file

@ -319,7 +319,9 @@
create-child create-child
(fn [file child] (fn [file child]
(-> file (-> file
(create-svg-raw (assoc data :content child)) (create-svg-raw (assoc data
:id (uuid/next)
:content child))
(close-svg-raw)))] (close-svg-raw)))]
;; First :content is the the shape attribute, the other content is the ;; First :content is the the shape attribute, the other content is the

View file

@ -6,6 +6,7 @@
.dashboard-grid { .dashboard-grid {
font-size: $fs14; font-size: $fs14;
height: 100%;
.grid-row { .grid-row {
display: flex; display: flex;

View file

@ -86,9 +86,9 @@
(mf/fnc svg-raw-wrapper (mf/fnc svg-raw-wrapper
[{:keys [shape frame] :as props}] [{:keys [shape frame] :as props}]
(let [childs (mapv #(get objects %) (:shapes shape))] (let [childs (mapv #(get objects %) (:shapes shape))]
(if (and (contains? shape :svg-attrs) (if (and (map? (:content shape))
(map? (:content shape)) (or (= :svg (get-in shape [:content :tag]))
(not= :svg (get-in shape [:content :tag]))) (contains? shape :svg-attrs)))
[:> shape-container {:shape shape} [:> shape-container {:shape shape}
[:& svg-raw-shape {:frame frame [:& svg-raw-shape {:frame frame
:shape shape :shape shape

View file

@ -6,6 +6,7 @@
(ns app.main.ui.dashboard.file-menu (ns app.main.ui.dashboard.file-menu
(:require (:require
[app.common.data :as d]
[app.main.data.dashboard :as dd] [app.main.data.dashboard :as dd]
[app.main.data.messages :as dm] [app.main.data.messages :as dm]
[app.main.data.modal :as modal] [app.main.data.modal :as modal]
@ -155,12 +156,22 @@
:on-accept del-shared}))) :on-accept del-shared})))
on-export-files on-export-files
(mf/use-callback
(mf/deps files current-team-id)
(fn [_] (fn [_]
(->> (rx/from files)
(rx/flat-map
(fn [file]
(->> (rp/query :file-libraries {:file-id (:id file)})
(rx/map #(assoc file :has-libraries? (d/not-empty? %))))))
(rx/reduce conj [])
(rx/subs
(fn [files]
(st/emit! (st/emit!
(modal/show (modal/show
{:type :export {:type :export
:team-id current-team-id :team-id current-team-id
:files (->> files (mapv :id))})))] :files (->> files (mapv :id))})))))))]
(mf/use-effect (mf/use-effect
(fn [] (fn []

View file

@ -44,7 +44,14 @@
(mf/deps project) (mf/deps project)
(fn [event] (fn [event]
(dom/prevent-default event) (dom/prevent-default event)
(st/emit! (dd/create-file {:project-id (:id project)}))))] (st/emit! (dd/create-file {:project-id (:id project)}))))
on-import
(mf/use-callback
(mf/deps (:id project))
(fn []
(st/emit! (dd/fetch-files {:project-id (:id project)})
(dd/clear-selected-files))))]
[:header.dashboard-header [:header.dashboard-header
@ -65,7 +72,8 @@
:left (- (:x (:menu-pos @local)) 180) :left (- (:x (:menu-pos @local)) 180)
:top (:y (:menu-pos @local)) :top (:y (:menu-pos @local))
:on-edit on-edit :on-edit on-edit
:on-menu-close on-menu-close}]])) :on-menu-close on-menu-close
:on-import on-import}]]))
[:div.dashboard-header-actions [:div.dashboard-header-actions
[:a.btn-secondary.btn-small {:on-click on-create-clicked} [:a.btn-secondary.btn-small {:on-click on-create-clicked}
(tr "dashboard.new-file")] (tr "dashboard.new-file")]
@ -102,6 +110,6 @@
[:* [:*
[:& header {:team team :project project}] [:& header {:team team :project project}]
[:section.dashboard-container [:section.dashboard-container
[:& grid {:id (:id project) [:& grid {:project-id (:id project)
:files files}]]])) :files files}]]]))

View file

@ -13,6 +13,7 @@
[app.main.refs :as refs] [app.main.refs :as refs]
[app.main.store :as st] [app.main.store :as st]
[app.main.ui.dashboard.file-menu :refer [file-menu]] [app.main.ui.dashboard.file-menu :refer [file-menu]]
[app.main.ui.dashboard.import :refer [use-import-file]]
[app.main.ui.dashboard.inline-edition :refer [inline-edition]] [app.main.ui.dashboard.inline-edition :refer [inline-edition]]
[app.main.ui.icons :as i] [app.main.ui.icons :as i]
[app.main.worker :as wrk] [app.main.worker :as wrk]
@ -210,14 +211,60 @@
[:div.text (tr "dashboard.loading-files")]]) [:div.text (tr "dashboard.loading-files")]])
(mf/defc grid (mf/defc grid
[{:keys [files] :as props}] [{:keys [files project-id] :as props}]
[:section.dashboard-grid (let [dragging? (mf/use-state false)
on-finish-import
(mf/use-callback
(fn []
(st/emit! (dd/fetch-files {:project-id project-id})
(dd/clear-selected-files))))
import-files (use-import-file project-id on-finish-import)
on-drag-enter
(mf/use-callback
(fn [e]
(when (or (dnd/has-type? e "Files")
(dnd/has-type? e "application/x-moz-file"))
(dom/prevent-default e)
(reset! dragging? true))))
on-drag-over
(mf/use-callback
(fn [e]
(when (or (dnd/has-type? e "Files")
(dnd/has-type? e "application/x-moz-file"))
(dom/prevent-default e))))
on-drag-leave
(mf/use-callback
(fn [e]
(when-not (dnd/from-child? e)
(reset! dragging? false))))
on-drop
(mf/use-callback
(fn [e]
(when (or (dnd/has-type? e "Files")
(dnd/has-type? e "application/x-moz-file"))
(dom/prevent-default e)
(reset! dragging? false)
(import-files (.-files (.-dataTransfer e))))))]
[:section.dashboard-grid {:on-drag-enter on-drag-enter
:on-drag-over on-drag-over
:on-drag-leave on-drag-leave
:on-drop on-drop}
(cond (cond
(nil? files) (nil? files)
[:& loading-placeholder] [:& loading-placeholder]
(seq files) (seq files)
[:div.grid-row [:div.grid-row
(when @dragging?
[:div.grid-item])
(for [item files] (for [item files]
[:& grid-item [:& grid-item
{:file item {:file item
@ -225,7 +272,7 @@
:navigate? true}])] :navigate? true}])]
:else :else
[:& empty-placeholder])]) [:& empty-placeholder])]))
(mf/defc line-grid-row (mf/defc line-grid-row
[{:keys [files selected-files on-load-more dragging?] :as props}] [{:keys [files selected-files on-load-more dragging?] :as props}]
@ -285,10 +332,17 @@
(mf/defc line-grid (mf/defc line-grid
[{:keys [project-id team-id files on-load-more] :as props}] [{:keys [project-id team-id files on-load-more] :as props}]
(let [dragging? (mf/use-state false) (let [dragging? (mf/use-state false)
selected-files (mf/deref refs/dashboard-selected-files) selected-files (mf/deref refs/dashboard-selected-files)
selected-project (mf/deref refs/dashboard-selected-project) selected-project (mf/deref refs/dashboard-selected-project)
on-finish-import
(mf/use-callback
(fn []
(st/emit! (dd/fetch-recent-files)
(dd/clear-selected-files))))
import-files (use-import-file project-id on-finish-import)
on-drag-enter on-drag-enter
(mf/use-callback (mf/use-callback
(mf/deps selected-project) (mf/deps selected-project)
@ -298,12 +352,19 @@
(when-not (or (dnd/from-child? e) (when-not (or (dnd/from-child? e)
(dnd/broken-event? e)) (dnd/broken-event? e))
(when (not= selected-project project-id) (when (not= selected-project project-id)
(reset! dragging? true)))))) (reset! dragging? true))))
(when (or (dnd/has-type? e "Files")
(dnd/has-type? e "application/x-moz-file"))
(dom/prevent-default e)
(reset! dragging? true))))
on-drag-over on-drag-over
(mf/use-callback (mf/use-callback
(fn [e] (fn [e]
(when (dnd/has-type? e "penpot/files") (when (or (dnd/has-type? e "penpot/files")
(dnd/has-type? e "Files")
(dnd/has-type? e "application/x-moz-file"))
(dom/prevent-default e)))) (dom/prevent-default e))))
on-drag-leave on-drag-leave
@ -321,13 +382,20 @@
on-drop on-drop
(mf/use-callback (mf/use-callback
(mf/deps files selected-files) (mf/deps files selected-files)
(fn [_] (fn [e]
(when (or (dnd/has-type? e "Files")
(dnd/has-type? e "application/x-moz-file"))
(dom/prevent-default e)
(reset! dragging? false)
(import-files (.-files (.-dataTransfer e))))
(when (dnd/has-type? e "penpot/files")
(reset! dragging? false) (reset! dragging? false)
(when (not= selected-project project-id) (when (not= selected-project project-id)
(let [data {:ids (into #{} (keys selected-files)) (let [data {:ids (into #{} (keys selected-files))
:project-id project-id} :project-id project-id}
mdata {:on-success on-drop-success}] mdata {:on-success on-drop-success}]
(st/emit! (dd/move-files (with-meta data mdata)))))))] (st/emit! (dd/move-files (with-meta data mdata))))))))]
[:section.dashboard-grid {:on-drag-enter on-drag-enter [:section.dashboard-grid {:on-drag-enter on-drag-enter
:on-drag-over on-drag-over :on-drag-over on-drag-over

View file

@ -20,7 +20,7 @@
[rumext.alpha :as mf])) [rumext.alpha :as mf]))
(mf/defc project-menu (mf/defc project-menu
[{:keys [project show? on-edit on-menu-close top left] :as props}] [{:keys [project show? on-edit on-menu-close top left on-import] :as props}]
(assert (some? project) "missing `project` prop") (assert (some? project) "missing `project` prop")
(assert (boolean? show?) "missing `show?` prop") (assert (boolean? show?) "missing `show?` prop")
(assert (fn? on-edit) "missing `on-edit` prop") (assert (fn? on-edit) "missing `on-edit` prop")
@ -84,8 +84,7 @@
on-finish-import on-finish-import
(mf/use-callback (mf/use-callback
(fn [] (fn []
(st/emit! (dd/fetch-recent-files) (when (some? on-import) (on-import))))]
(dd/clear-selected-files))))]
[:* [:*
[:& udi/import-form {:ref file-input [:& udi/import-form {:ref file-input
@ -106,7 +105,8 @@
[(tr "dashboard.move-to") nil [(tr "dashboard.move-to") nil
(for [team teams] (for [team teams]
[(:name team) (on-move (:id team))])]) [(:name team) (on-move (:id team))])])
[(tr "dashboard.import") on-import-files] (when (some? on-import)
[(tr "dashboard.import") on-import-files])
[:separator] [:separator]
[(tr "labels.delete") on-delete]]}]])) [(tr "labels.delete") on-delete]]}]]))

View file

@ -93,7 +93,13 @@
(fn [] (fn []
(let [mdata {:on-success on-file-created} (let [mdata {:on-success on-file-created}
params {:project-id (:id project)}] params {:project-id (:id project)}]
(st/emit! (dd/create-file (with-meta params mdata))))))] (st/emit! (dd/create-file (with-meta params mdata))))))
on-import
(mf/use-callback
(fn []
(st/emit! (dd/fetch-recent-files)
(dd/clear-selected-files))))]
[:div.dashboard-project-row {:class (when first? "first")} [:div.dashboard-project-row {:class (when first? "first")}
[:div.project [:div.project
@ -111,7 +117,8 @@
:left (:x (:menu-pos @local)) :left (:x (:menu-pos @local))
:top (:y (:menu-pos @local)) :top (:y (:menu-pos @local))
:on-edit on-edit-open :on-edit on-edit-open
:on-menu-close on-menu-close}] :on-menu-close on-menu-close
:on-import on-import}]
[:span.info (str file-count " files")] [:span.info (str file-count " files")]
(when (> file-count 0) (when (> file-count 0)

View file

@ -6,12 +6,14 @@
(ns app.main.ui.workspace.header (ns app.main.ui.workspace.header
(:require (:require
[app.common.data :as d]
[app.common.math :as mth] [app.common.math :as mth]
[app.config :as cfg] [app.config :as cfg]
[app.main.data.modal :as modal] [app.main.data.modal :as modal]
[app.main.data.workspace :as dw] [app.main.data.workspace :as dw]
[app.main.data.workspace.shortcuts :as sc] [app.main.data.workspace.shortcuts :as sc]
[app.main.refs :as refs] [app.main.refs :as refs]
[app.main.repo :as rp]
[app.main.store :as st] [app.main.store :as st]
[app.main.ui.components.dropdown :refer [dropdown]] [app.main.ui.components.dropdown :refer [dropdown]]
[app.main.ui.icons :as i] [app.main.ui.icons :as i]
@ -20,6 +22,7 @@
[app.util.i18n :as i18n :refer [tr]] [app.util.i18n :as i18n :refer [tr]]
[app.util.keyboard :as kbd] [app.util.keyboard :as kbd]
[app.util.router :as rt] [app.util.router :as rt]
[beicon.core :as rx]
[okulary.core :as l] [okulary.core :as l]
[rumext.alpha :as mf])) [rumext.alpha :as mf]))
@ -135,12 +138,23 @@
(reset! editing? true)) (reset! editing? true))
on-export-files on-export-files
(mf/use-callback
(mf/deps file team-id)
(fn [_] (fn [_]
(->> (rx/of file)
(rx/flat-map
(fn [file]
(->> (rp/query :file-libraries {:file-id (:id file)})
(rx/map #(assoc file :has-libraries? (d/not-empty? %))))))
(rx/reduce conj [])
(rx/subs
(fn [files]
(st/emit! (st/emit!
(modal/show (modal/show
{:type :export {:type :export
:team-id team-id :team-id team-id
:files [(:id file)]})))] :files (->> files (mapv :id))})))))))]
(mf/use-effect (mf/use-effect
(mf/deps @editing?) (mf/deps @editing?)
#(when @editing? #(when @editing?

View file

@ -201,7 +201,13 @@
(merge (add-attrs {} (:attrs svg-node)) node-attrs)) (merge (add-attrs {} (:attrs svg-node)) node-attrs))
(= type :svg-raw) (= type :svg-raw)
(->> node :content last) (let [svg-content (get-data node :penpot:svg-content)
tag (-> svg-content :attrs :penpot:tag keyword)
svg-node (if (= :svg tag)
(->> node :content last :content last)
(->> node :content last))]
(merge (add-attrs {} (:attrs svg-node)) node-attrs))
:else :else
node-attrs))) node-attrs)))