0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-12 07:41:43 -05:00

Preserve origin component in nested instances

This commit is contained in:
Andrés Moya 2020-10-05 11:01:14 +02:00
parent 42ba0d7b7a
commit a3eb634740
7 changed files with 83 additions and 56 deletions

View file

@ -46,6 +46,7 @@
(<= % max-safe-int)))
(s/def ::component-id uuid?)
(s/def ::component-file uuid?)
(s/def ::component-root? boolean?)
(s/def ::shape-ref uuid?)
(s/def ::safe-number
@ -122,7 +123,6 @@
(s/def :internal.shape/line-height ::safe-number)
(s/def :internal.shape/locked boolean?)
(s/def :internal.shape/page-id uuid?)
(s/def :internal.shape/component-id uuid?)
(s/def :internal.shape/proportion ::safe-number)
(s/def :internal.shape/proportion-lock boolean?)
(s/def :internal.shape/rx ::safe-number)
@ -255,6 +255,7 @@
(s/keys :opt-un [::id
::component-id
::component-file
::component-root?
::shape-ref])))
(s/def :internal.page/objects (s/map-of uuid? ::shape))

View file

@ -35,7 +35,7 @@
"Get the root shape linked to the component for this shape, if any"
[id objects]
(let [obj (get objects id)]
(if-let [component-id (:component-id obj)]
(if-let [component-id (:component-root? obj)]
id
(if-let [parent-id (:parent-id obj)]
(get-root-component parent-id objects)

View file

@ -1149,11 +1149,9 @@
(let [page-id (:current-page-id state)
objects (dwc/lookup-page-objects state page-id)
root-id (cph/get-root-component (:id shape) objects)
root-shape (get objects root-id)
mdata {:position position
:shape shape
:root-shape root-shape
:selected (get-in state [:workspace-local :selected])}]
(-> state
(assoc-in [:workspace-local :context-menu] mdata))))

View file

@ -155,6 +155,9 @@
{:type :set
:attr :component-file
:val nil}
{:type :set
:attr :component-root?
:val (:component-root? updated-shape)}
{:type :set
:attr :shape-ref
:val (:shape-ref updated-shape)}]})
@ -176,6 +179,9 @@
{:type :set
:attr :component-file
:val (:component-file original-shape)}
{:type :set
:attr :component-root?
:val (:component-root? original-shape)}
{:type :set
:attr :shape-ref
:val (:shape-ref original-shape)}]}))
@ -242,7 +248,8 @@
(assoc $ :shape-ref (:id original-shape)))
(nil? (:parent-id original-shape))
(assoc :component-id (:id original-shape))
(assoc :component-id (:id original-shape)
:component-root? true)
(and (nil? (:parent-id original-shape)) (some? file-id))
(assoc :component-file file-id)
@ -251,7 +258,7 @@
(dissoc :component-file)
(some? (:parent-id original-shape))
(dissoc :component-id :component-file))))
(dissoc :component-root?))))
[new-shape new-shapes _]
(cph/clone-object component-shape
@ -285,9 +292,7 @@
(watch [_ state stream]
(let [page-id (:current-page-id state)
objects (dwc/lookup-page-objects state page-id)
root-id (cph/get-root-component id objects)
shapes (cph/get-object-with-children root-id objects)
shapes (cph/get-object-with-children id objects)
rchanges (map (fn [obj]
{:type :mod-obj
@ -354,7 +359,6 @@
(let [page-id (:current-page-id state)
page (get-in state [:workspace-data :pages-index page-id])
objects (dwc/lookup-page-objects state page-id)
root-id (cph/get-root-component id objects)
root-shape (get objects id)
file-id (get root-shape :component-file)
@ -381,7 +385,6 @@
(watch [_ state stream]
(let [page-id (:current-page-id state)
objects (dwc/lookup-page-objects state page-id)
root-id (cph/get-root-component id objects)
root-shape (get objects id)
component-id (get root-shape :component-id)
@ -483,7 +486,10 @@
(ptk/reify ::sync-file-2nd-stage
ptk/WatchEvent
(watch [_ state stream]
(let [[rchanges uchanges] (dwlh/generate-sync-file :components nil state)]
(let [[rchanges1 uchanges1] (dwlh/generate-sync-file :components nil state)
[rchanges2 uchanges2] (dwlh/generate-sync-library :components file-id state)
rchanges (d/concat rchanges1 rchanges2)
uchanges (d/concat uchanges1 uchanges2)]
(when rchanges
(rx/of (dwc/commit-changes rchanges uchanges {:commit-local? true})))))))

View file

@ -97,7 +97,7 @@
[asset-type library-id]
(case asset-type
:components
(fn [shape] (and (some? (:component-id shape))
(fn [shape] (and (:component-root? shape)
(= (:component-file shape) library-id)))
:colors
@ -256,12 +256,20 @@
from parent and frame. Update the original shapes to have links
to the new ones."
[shape objects]
(assert (nil? (:component-id shape)))
(assert (nil? (:component-file shape)))
(assert (nil? (:shape-ref shape)))
(let [update-new-shape (fn [new-shape original-shape]
(assoc new-shape :frame-id nil))
(cond-> new-shape
true
(assoc :frame-id nil)
(nil? (:parent-id new-shape))
(assoc :component-root? true)))
;; Make the original shape an instance of the new component.
;; If one of the original shape children already was a component
;; instance, the 'instanceness' is copied into the new component,
;; and the original shape now points to the new component.
;; instance, the 'instanceness' is copied into the new component.
update-original-shape (fn [original-shape new-shape]
(cond-> original-shape
true
@ -269,11 +277,11 @@
(nil? (:parent-id new-shape))
(assoc :component-id (:id new-shape)
:component-file nil)
:component-file nil
:component-root? true)
(some? (:parent-id new-shape))
(assoc :component-id nil
:component-file nil)))]
(dissoc :component-root?)))]
(cph/clone-object shape nil objects update-new-shape update-original-shape)))

View file

@ -89,7 +89,9 @@
(letfn [(show-shape [shape-id level objects]
(let [shape (get objects shape-id)]
(println (str/pad (str (str/repeat " " level)
(:name shape))
(:name shape)
(when (seq (filter #(not= :position-group %)
(:touched shape))) "*"))
{:length 20
:type :right})
(show-component shape objects))
@ -102,24 +104,35 @@
(show-shape shape-id (inc level) objects))))))
(show-component [shape objects]
(if (nil? (:shape-ref shape))
""
(let [root-id (cph/get-root-component (:id shape) objects)
root-shape (when root-id (get objects root-id))
component-id (when root-shape (:component-id root-shape))
component-file-id (when root-shape (:component-file root-shape))
component-file (when component-file-id (get libraries component-file-id))
shape-ref (:shape-ref shape)
component (when component-id
(if component-file
(get-in component-file [:data :components component-id])
(get components component-id)))
component-shape (when (and component shape-ref)
(get-in component [:objects shape-ref]))]
(if component-shape
(str/format " %s--> %s%s"
(if (:component-id shape) "#" "-")
component-shape (when (and component (:shape-ref shape))
(get-in component [:objects (:shape-ref shape)]))]
(str/format " %s--> %s%s%s"
(if (:component-root? shape) "#" "-")
(when component-file (str/format "<%s> " (:name component-file)))
(:name component-shape))
"")))]
(:name component-shape)
(if (or (:component-root? shape)
(nil? (:component-id shape)))
""
(let [component-id (:component-id shape)
component-file-id (:component-file shape)
component-file (when component-file-id (get libraries component-file-id))
component (if component-file
(get-in component-file [:data :components component-id])
(get components component-id))]
(str/format " (%s%s)"
(when component-file (str/format "<%s> " (:name component-file)))
(:name component))))))))]
(println "[Workspace]")
(show-shape (:id root) 0 objects)

View file

@ -46,7 +46,6 @@
[{:keys [mdata] :as props}]
(let [{:keys [id] :as shape} (:shape mdata)
selected (:selected mdata)
root-shape (:root-shape mdata)
do-duplicate #(st/emit! dw/duplicate-selected)
do-delete #(st/emit! dw/delete-selected)
@ -69,7 +68,7 @@
(st/emit! (dwl/update-component id))
(st/emit! (dwl/sync-file nil)))
do-navigate-component-file #(st/emit! (dwl/nav-to-component-file
(:component-file root-shape)))]
(:component-file shape)))]
[:*
[:& menu-entry {:title "Copy"
:shortcut "Ctrl + c"
@ -117,18 +116,21 @@
[:& menu-entry {:title "Lock"
:on-click do-lock-shape}])
(when (nil? (:shape-ref shape))
[:*
[:& menu-separator]
(if (nil? (:shape-ref shape))
[:& menu-entry {:title "Create component"
:shortcut "Ctrl + K"
:on-click do-add-component}]
:on-click do-add-component}]])
(when (:component-id shape)
[:*
[:& menu-separator]
[:& menu-entry {:title "Detach instance"
:on-click do-detach-component}]
[:& menu-entry {:title "Reset overrides"
:on-click do-reset-component}]
(if (nil? (:component-file root-shape))
(if (nil? (:component-file shape))
[:& menu-entry {:title "Update master component"
:on-click do-update-component}]
[:& menu-entry {:title "Go to master component file"
@ -137,8 +139,7 @@
[:& menu-separator]
[:& menu-entry {:title "Delete"
:shortcut "Supr"
:on-click do-delete}]
]))
:on-click do-delete}]]))
(mf/defc viewport-context-menu
[{:keys [mdata] :as props}]