🔧 Refactor delete/restore components

This commit is contained in:
Andrés Moya 2023-03-10 15:43:26 +01:00
parent b91f1959b4
commit 7391a4086a
27 changed files with 378 additions and 328 deletions

View file

@ -9,6 +9,7 @@
[app.common.data :as d]
[app.common.file-builder :as fb]
[app.common.media :as cm]
[app.common.types.components-list :as ctkl]
[app.common.uuid :as uuid]
[app.util.dom :as dom]
[app.util.json :as json]
@ -108,7 +109,7 @@
components-stream
(->> files-stream
(rx/flat-map vals)
(rx/filter #(d/not-empty? (get-in % [:data :components])))
(rx/filter #(d/not-empty? (ctkl/components-seq (:data %))))
(rx/flat-map e/parse-library-components))
pages-stream

View file

@ -34,7 +34,6 @@
(declare commit-changes)
(defn- add-group-id
[changes state]
(let [undo (:workspace-undo state)
@ -215,7 +214,7 @@
add-page-id
(fn [{:keys [id type page] :as change}]
(cond-> change
(page-change? type)
(and (page-change? type) (nil? (:page-id change)))
(assoc :page-id (or id (:id page)))))
changes-by-pages

View file

@ -22,7 +22,6 @@
[app.common.types.container :as ctn]
[app.common.types.file :as ctf]
[app.common.types.file.media-object :as ctfm]
[app.common.types.pages-list :as ctpl]
[app.common.types.typography :as ctt]
[app.common.uuid :as uuid]
[app.main.data.dashboard :as dd]
@ -421,12 +420,12 @@
(let [data (get state :workspace-data)]
(if (features/active-feature? state :components-v2)
(let [component (ctkl/get-component data id)
page (ctpl/get-page data (:main-instance-page component))
shape (ctn/get-shape page (:main-instance-id component))]
(rx/of (dwsh/delete-shapes (:id page) #{(:id shape)})))
page (ctf/get-component-page data component)
shape (ctf/get-component-root data component)]
(rx/of (dwsh/delete-shapes (:id page) #{(:id shape)}))) ;; Deleting main root triggers component delete
(let [changes (-> (pcb/empty-changes it)
(pcb/with-library-data data)
(pcb/delete-component id false))]
(pcb/delete-component id))]
(rx/of (dch/commit-changes changes))))))))
(defn restore-component
@ -437,39 +436,18 @@
(ptk/reify ::restore-component
ptk/WatchEvent
(watch [it state _]
(assert "Restore component not implemented") ; until we allow a :deleted flag in shapes
(let [file-data (wsh/get-file state library-id)
component (ctf/get-deleted-component file-data component-id)
page (ctpl/get-page file-data (:main-instance-page component))
components-v2
(features/active-feature? state :components-v2)
; Make a new main instance, with the same id of the original
[_main-instance shapes]
(ctn/make-component-instance page
component
file-data
(gpt/point (:main-instance-x component)
(:main-instance-y component))
components-v2
{:main-instance? true
:force-id (:main-instance-id component)})
changes (-> (pcb/empty-changes it)
(pcb/with-library-data file-data)
(pcb/with-page page))
changes (reduce #(pcb/add-object %1 %2 {:ignore-touched true})
changes
shapes)
; restore-component change needs to be done after add main instance
; because when undo changes, the orden is inverse
changes (pcb/restore-component changes component-id)]
(rx/of (dch/commit-changes (assoc changes :file-id library-id)))))))
(let [library-data (wsh/get-file state library-id)
component (ctkl/get-deleted-component library-data component-id)
page (ctf/get-component-page library-data component)
shapes (cph/get-children-with-self (:objects component) (:main-instance-id component))
changes (-> (pcb/empty-changes it)
(pcb/with-library-data library-data)
(pcb/with-page page))
changes (reduce #(pcb/add-object %1 %2 {:ignore-touched true})
changes
shapes)
changes (pcb/restore-component changes component-id)]
(rx/of (dch/commit-changes changes))))))
(defn instantiate-component
"Create a new shape in the current page, from the component with the given id

View file

@ -241,7 +241,7 @@
(let [file (wsh/get-file state file-id)
components-v2 (get-in file [:options :components-v2])]
(loop [local-components (vals (get file :components))
(loop [local-components (ctkl/components-seq file)
changes (pcb/empty-changes it)]
(if-let [local-component (first local-components)]
(recur (next local-components)
@ -480,7 +480,7 @@
(let [library (dm/get-in libraries [(:component-file shape-inst) :data])
component (or (ctkl/get-component library (:component-id shape-inst))
(and reset?
(ctf/get-deleted-component library (:component-id shape-inst))))
(ctkl/get-deleted-component library (:component-id shape-inst))))
shape-main (when component
(ctf/get-ref-shape library component shape-inst))

View file

@ -292,7 +292,7 @@
changes (reduce (fn [changes component-id]
;; It's important to delete the component before the main instance, because we
;; need to store the instance position if we want to restore it later.
(pcb/delete-component changes component-id components-v2))
(pcb/delete-component changes component-id))
changes
components-to-delete)

View file

@ -40,10 +40,6 @@
([state page-id]
(dm/get-in state [:workspace-data :pages-index page-id :options])))
(defn lookup-component-objects
([state component-id]
(dm/get-in state [:workspace-data :components component-id :objects])))
(defn lookup-local-components
([state]
(dm/get-in state [:workspace-data :components])))

View file

@ -7,6 +7,7 @@
(ns app.main.ui.workspace.libraries
(:require
[app.common.data :as d]
[app.common.types.components-list :as ctkl]
[app.main.data.dashboard :as dd]
[app.main.data.modal :as modal]
[app.main.data.workspace.libraries :as dwl]
@ -44,7 +45,7 @@
(defn local-library-str
[library]
(let [components-count (count (get-in library [:data :components] []))
(let [components-count (count (or (ctkl/components-seq (:data library)) []))
graphics-count (count (get-in library [:data :media] []))
colors-count (count (get-in library [:data :colors] []))
typography-count (count (get-in library [:data :typographies] []))]

View file

@ -12,6 +12,7 @@
[app.common.pages.helpers :as cph]
[app.common.spec :as us]
[app.common.text :as txt]
[app.common.types.components-list :as ctkl]
[app.common.types.file :as ctf]
[app.config :as cf]
[app.main.data.events :as ev]
@ -374,8 +375,11 @@
components-v2 (mf/use-ctx ctx/components-v2)
file (or (:data file) file)
file (or (:data file) file)
root-shape (ctf/get-component-root file component)
component-container (if components-v2
(ctf/get-component-page file component)
component)
unselect-all
(mf/use-fn
(fn []
@ -444,26 +448,26 @@
:on-drag-over on-drag-over
:on-drop on-drop}
[:& component-svg {:root-shape (ctf/get-component-root file component)
:objects (:objects (if components-v2
(ctf/get-component-page file component)
component))}]
(let [renaming? (= renaming (:id component))]
(when (and (some? root-shape) (some? component-container))
[:*
[:& editable-label
{:class-name (dom/classnames
:cell-name listing-thumbs?
:item-name (not listing-thumbs?)
:editing renaming?)
:value (cph/merge-path-item (:path component) (:name component))
:tooltip (cph/merge-path-item (:path component) (:name component))
:display-value (:name component)
:editing? renaming?
:disable-dbl-click? true
:on-change do-rename
:on-cancel cancel-rename}]
(when @dragging?
[:div.dragging])])]))
[:& component-svg {:root-shape root-shape
:objects (:objects component-container)}]
(let [renaming? (= renaming (:id component))]
[:*
[:& editable-label
{:class-name (dom/classnames
:cell-name listing-thumbs?
:item-name (not listing-thumbs?)
:editing renaming?)
:value (cph/merge-path-item (:path component) (:name component))
:tooltip (cph/merge-path-item (:path component) (:name component))
:display-value (:name component)
:editing? renaming?
:disable-dbl-click? true
:on-change do-rename
:on-cancel cancel-rename}]
(when @dragging?
[:div.dragging])])])]))
(mf/defc components-group
[{:keys [file prefix groups open-groups renaming listing-thumbs? selected-components on-asset-click
@ -1938,8 +1942,8 @@
(l/derived (fn [state]
(let [wfile (:workspace-data state)]
(if (= (:id wfile) id)
(vals (get wfile :components))
(vals (get-in state [:workspace-libraries id :data :components])))))
(ctkl/components-seq wfile)
(ctkl/components-seq (get-in state [:workspace-libraries id :data])))))
st/state =))
(defn file-typography-ref

View file

@ -11,6 +11,7 @@
[app.common.logging :as l]
[app.common.math :as mth]
[app.common.spec :as us]
[app.common.types.components-list :as ctkl]
[app.common.uri :as u]
[app.main.data.fonts :as df]
[app.main.features :as features]
@ -247,7 +248,7 @@
:border "0px solid black"}]]])]
[:ul.nav
(for [[id data] (get-in file [:data :components])]
(for [[id data] (ctkl/components (:data file))]
(let [on-click (fn [event]
(dom/prevent-default event)
(swap! state assoc :component-id id))]

View file

@ -9,6 +9,7 @@
[app.common.data :as d]
[app.common.media :as cm]
[app.common.text :as ct]
[app.common.types.components-list :as ctkl]
[app.config :as cfg]
[app.main.render :as r]
[app.main.repo :as rp]
@ -51,7 +52,7 @@
:version current-version
:libraries (->> (:libraries file) (into #{}) (mapv str))
:exportType (d/name export-type)
:hasComponents (d/not-empty? (get-in file [:data :components]))
:hasComponents (d/not-empty? (ctkl/components-seq (:data file)))
:hasDeletedComponents (d/not-empty? (get-in file [:data :deleted-components]))
:hasMedia (d/not-empty? (get-in file [:data :media]))
:hasColors (d/not-empty? (get-in file [:data :colors]))
@ -329,7 +330,7 @@
(select-keys (get external-refs (:id file))))
media (-> (get-in file [:data :media])
(select-keys (get external-refs (:id file))))
components (-> (get-in file [:data :components])
components (-> (ctkl/components (:data file))
(select-keys (get external-refs (:id file))))]
(cond-> target
(d/not-empty? colors)
@ -434,7 +435,7 @@
components-stream
(->> files-stream
(rx/flat-map vals)
(rx/filter #(d/not-empty? (get-in % [:data :components])))
(rx/filter #(d/not-empty? (ctkl/components-seq (:data %))))
(rx/flat-map parse-library-components))
deleted-components-stream