0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-06 14:50:20 -05:00
This commit is contained in:
Andrés Moya 2022-08-26 14:08:20 +02:00
parent d213bc96d4
commit 9f98e9c50c
3 changed files with 33 additions and 8 deletions

View file

@ -4,13 +4,20 @@
;;
;; Copyright (c) KALEIDOS INC
(ns app.common.types.component)
(ns app.common.types.component
(:require
[app.common.types.container :as ctn]))
(defn instance-root?
"An intance root is the shape, inside an instance, that has
the link to the component. Other shapes have :shape-ref but
not :component-id."
[shape]
(some? (:component-id shape)))
(defn instance-of?
"Check if the shape is the root of an instance of the
given component at the given file library."
[shape file-id component-id]
(and (some? (:component-id shape))
(some? (:component-file shape))
@ -18,6 +25,7 @@
(= (:component-file shape) file-id)))
(defn is-main-of?
"Check if the first shape is a near or remote main of the second one."
[shape-main shape-inst]
(and (:shape-ref shape-inst)
(or (= (:shape-ref shape-inst) (:id shape-main))
@ -29,12 +37,13 @@
(some? (:main-instance? shape)))
(defn is-main-instance?
"Check if this shape is the root of the main instance of the given component."
"Check if the shape in the page is the main instance of the component."
[shape-id page-id component]
(and (= shape-id (:main-instance-id component))
(= page-id (:main-instance-page component))))
(defn get-component-root
"Get the root shape of the component."
[component]
(get-in component [:objects (:id component)]))
@ -67,4 +76,11 @@
:shape-ref
:touched))
(defn standalone-instance?
"Check if the shape inside the container is not a subinstance
(an instance inside another one)."
[shape container]
(when (:component-id shape)
(if (ctn/page? container)
(:component-root? shape)
(nil? (:parent-id shape)))))

View file

@ -46,7 +46,7 @@
[potok.core :as ptk]))
;; Change this to :info :debug or :trace to debug this module, or :warn to reset to default
(log/set-level! :warn)
(log/set-level! :info)
(s/def ::file ::dd/file)

View file

@ -27,7 +27,7 @@
[clojure.set :as set]))
;; Change this to :info :debug or :trace to debug this module, or :warn to reset to default
(log/set-level! :warn)
(log/set-level! :info)
(declare generate-sync-container)
(declare generate-sync-shape)
@ -242,7 +242,11 @@
(log/debug :msg "Sync component in local library" :component-id (:id container)))
(let [linked-shapes (->> (vals (:objects container))
(filter #(uses-assets? asset-type asset-id % library-id (cph/component? container))))]
(filter #(uses-assets? asset-type
asset-id
%
library-id
(ctk/standalone-instance? % container))))]
(loop [shapes (seq linked-shapes)
changes (-> (pcb/empty-changes it)
(pcb/with-container container)
@ -263,14 +267,19 @@
(fn [asset-type _ _ _ _] asset-type))
(defmethod uses-assets? :components
[_ component-id shape library-id component?]
[_ component-id shape library-id standalone-instance?]
(when (and (if (nil? component-id)
(ctk/uses-library-components? shape library-id)
(ctk/instance-of? shape library-id component-id))
(not standalone-instance?))
(js/console.log "ignored shape" (clj->js shape)))
(and (if (nil? component-id)
(ctk/uses-library-components? shape library-id)
(ctk/instance-of? shape library-id component-id))
;; Subinstances are synced with the near component, not the remote.
;; So an instance that is not root, cannot be considered "using"
;; the component.
(or (:component-root? shape) component?)))
standalone-instance?))
(defmethod uses-assets? :colors
[_ color-id shape library-id _]