♻️ Rename flag :component-swap to :allow-altering-copies

This commit is contained in:
Pablo Alba 2025-07-07 11:05:10 +02:00 committed by Pablo Alba
parent 469d47eaf3
commit cfec023585
7 changed files with 24 additions and 22 deletions

View file

@ -241,7 +241,7 @@
[:shapes ::sm/any] [:shapes ::sm/any]
[:index {:optional true} [:maybe :int]] [:index {:optional true} [:maybe :int]]
[:after-shape {:optional true} ::sm/any] [:after-shape {:optional true} ::sm/any]
[:component-swap {:optional true} :boolean]]] [:allow-altering-copies {:optional true} :boolean]]]
[:reorder-children [:reorder-children
[:map {:title "ReorderChildrenChange"} [:map {:title "ReorderChildrenChange"}
@ -761,7 +761,7 @@
(d/update-in-when data [:components component-id :objects] reg-objects)))) (d/update-in-when data [:components component-id :objects] reg-objects))))
(defmethod process-change :mov-objects (defmethod process-change :mov-objects
[data {:keys [parent-id shapes index page-id component-id ignore-touched after-shape component-swap syncing]}] [data {:keys [parent-id shapes index page-id component-id ignore-touched after-shape allow-altering-copies syncing]}]
(letfn [(calculate-invalid-targets [objects shape-id] (letfn [(calculate-invalid-targets [objects shape-id]
(let [reduce-fn #(into %1 (calculate-invalid-targets objects %2))] (let [reduce-fn #(into %1 (calculate-invalid-targets objects %2))]
(->> (get-in objects [shape-id :shapes]) (->> (get-in objects [shape-id :shapes])
@ -776,7 +776,7 @@
(and shape (and shape
(not (invalid-targets parent-id)) (not (invalid-targets parent-id))
(not (cfh/components-nesting-loop? objects shape-id parent-id)) (not (cfh/components-nesting-loop? objects shape-id parent-id))
(or component-swap ;; On a component swap it's allowed to change the structure of a copy (or allow-altering-copies ;; In some cases (like a component swap) it's allowed to change the structure of a copy
syncing ;; If we are syncing the changes of a main component, it's allowed to change the structure of a copy syncing ;; If we are syncing the changes of a main component, it's allowed to change the structure of a copy
(and (and
(not (ctk/in-component-copy? (get objects (:parent-id shape)))) ;; We don't want to change the structure of component copies (not (ctk/in-component-copy? (get objects (:parent-id shape)))) ;; We don't want to change the structure of component copies

View file

@ -464,8 +464,8 @@
(some? index) (some? index)
(assoc :index index) (assoc :index index)
(:component-swap options) (:allow-altering-copies options)
(assoc :component-swap true) (assoc :allow-altering-copies true)
(:ignore-touched options) (:ignore-touched options)
(assoc :ignore-touched true)) (assoc :ignore-touched true))
@ -479,8 +479,8 @@
:shapes [(:id shape)] :shapes [(:id shape)]
:after-shape prev-sibling :after-shape prev-sibling
:index 0} ; index is used in case there is no after-shape (moving bottom shapes) :index 0} ; index is used in case there is no after-shape (moving bottom shapes)
(:component-swap options) (:allow-altering-copies options)
(assoc :component-swap true))))) (assoc :allow-altering-copies true)))))
restore-touched-change restore-touched-change
{:type :mod-obj {:type :mod-obj

View file

@ -96,7 +96,7 @@
(log/dbg :hint "repairing shape :invalid-parent" :id (:id shape) :name (:name shape) :page-id page-id) (log/dbg :hint "repairing shape :invalid-parent" :id (:id shape) :name (:name shape) :page-id page-id)
(-> (pcb/empty-changes nil page-id) (-> (pcb/empty-changes nil page-id)
(pcb/with-file-data file-data) (pcb/with-file-data file-data)
(pcb/change-parent (:parent-id args) [shape] nil {:component-swap true}))) (pcb/change-parent (:parent-id args) [shape] nil {:allow-altering-copies true})))
(defmethod repair-error :frame-not-found (defmethod repair-error :frame-not-found
[_ {:keys [shape page-id] :as error} file-data _] [_ {:keys [shape page-id] :as error} file-data _]
@ -387,7 +387,7 @@
(-> (pcb/empty-changes nil page-id) (-> (pcb/empty-changes nil page-id)
(pcb/with-file-data file-data) (pcb/with-file-data file-data)
(pcb/update-shapes [(:id shape)] repair-shape) (pcb/update-shapes [(:id shape)] repair-shape)
(pcb/change-parent uuid/zero [shape] nil {:component-swap true})))) (pcb/change-parent uuid/zero [shape] nil {:allow-altering-copies true}))))
(defmethod repair-error :root-copy-not-allowed (defmethod repair-error :root-copy-not-allowed
[_ {:keys [shape page-id] :as error} file-data _] [_ {:keys [shape page-id] :as error} file-data _]

View file

@ -2249,7 +2249,7 @@
(pcb/update-shapes [(:id new-shape)] #(d/patch-object % keep-props-values)) (pcb/update-shapes [(:id new-shape)] #(d/patch-object % keep-props-values))
;; We need to set the same index as the original shape ;; We need to set the same index as the original shape
(pcb/change-parent (:parent-id shape) [new-shape] index {:component-swap true (pcb/change-parent (:parent-id shape) [new-shape] index {:allow-altering-copies true
:ignore-touched true}) :ignore-touched true})
(change-touched new-shape (change-touched new-shape
shape shape
@ -2258,11 +2258,11 @@
(defn generate-component-swap (defn generate-component-swap
[changes objects shape file page libraries id-new-component [changes objects shape file page libraries id-new-component
index target-cell keep-props-values keep-touched] index target-cell keep-props-values ignore-swapped?]
(let [;; When we keep the touched properties, we can't delete the (let [;; When we keep the touched properties, we can't delete the
;; swapped children (we will keep them too) ;; swapped children (we will keep them too)
ignore-swapped-fn ignore-swapped-fn
(if keep-touched (if ignore-swapped?
#(-> (get objects %) #(-> (get objects %)
(ctk/get-swap-slot)) (ctk/get-swap-slot))
(constantly false)) (constantly false))
@ -2271,7 +2271,7 @@
(-> changes (-> changes
(cls/generate-delete-shapes (cls/generate-delete-shapes
file page objects (d/ordered-set (:id shape)) file page objects (d/ordered-set (:id shape))
{:component-swap true :ignore-children-fn ignore-swapped-fn})) {:allow-altering-copies true :ignore-children-fn ignore-swapped-fn}))
[new-shape changes] [new-shape changes]
(-> changes (-> changes
(generate-new-shape-for-swap shape file page libraries id-new-component index target-cell keep-props-values))] (generate-new-shape-for-swap shape file page libraries id-new-component index target-cell keep-props-values))]

View file

@ -100,7 +100,7 @@
ids ids
options)) options))
([changes ids {:keys [ignore-touched ([changes ids {:keys [ignore-touched
component-swap allow-altering-copies
;; We will delete the shapes and its descendants. ;; We will delete the shapes and its descendants.
;; ignore-children-fn is used to ignore some descendants ;; ignore-children-fn is used to ignore some descendants
;; on the deletion process. It should receive a shape and ;; on the deletion process. It should receive a shape and
@ -119,11 +119,12 @@
;; Look for shapes that are inside a component copy, but are ;; Look for shapes that are inside a component copy, but are
;; not the root. In this case, they must not be deleted, ;; not the root. In this case, they must not be deleted,
;; but hidden (to be able to recover them more easily). ;; but hidden (to be able to recover them more easily).
;; Unless we are doing a component swap, in which case we want ;; If we want to specifically allow altering the copies, this is
;; a special case, like a component swap, in which case we want
;; to delete the old shape ;; to delete the old shape
(let [shape (get objects shape-id)] (let [shape (get objects shape-id)]
(and (ctn/has-any-copy-parent? objects shape) (and (ctn/has-any-copy-parent? objects shape)
(not component-swap)))) (not allow-altering-copies))))
[ids-to-delete ids-to-hide] [ids-to-delete ids-to-hide]
(loop [ids-seq (seq ids) (loop [ids-seq (seq ids)
@ -212,9 +213,10 @@
empty-parents empty-parents
;; Any parent whose children are all deleted, must be deleted too. ;; Any parent whose children are all deleted, must be deleted too.
;; Unless we are during a component swap: in this case we are replacing a shape by ;; If we want to specifically allow altering the copies, this is a special case,
;; for example during a component swap. in this case we are replacing a shape by
;; other one, so must not delete empty parents. ;; other one, so must not delete empty parents.
(if-not component-swap (if-not allow-altering-copies
(into (d/ordered-set) (find-all-empty-parents #{})) (into (d/ordered-set) (find-all-empty-parents #{}))
#{}) #{})

View file

@ -79,7 +79,7 @@
before-changes (-> (pcb/empty-changes) before-changes (-> (pcb/empty-changes)
(pcb/with-page page) (pcb/with-page page)
(pcb/with-objects (:objects page)) (pcb/with-objects (:objects page))
(pcb/change-parent uuid/zero [orig-swapped-child] 0 {:component-swap true})) (pcb/change-parent uuid/zero [orig-swapped-child] 0 {:allow-altering-copies true}))
objects (pcb/get-objects changes) objects (pcb/get-objects changes)
prev-swap-slot (ctk/get-swap-slot orig-swapped-child) prev-swap-slot (ctk/get-swap-slot orig-swapped-child)
@ -90,7 +90,7 @@
(-> (pcb/concat-changes before-changes changes) (-> (pcb/concat-changes before-changes changes)
;; Move the previous shape to the new parent ;; Move the previous shape to the new parent
(pcb/change-parent (:parent-id related-shape-in-new) [orig-swapped-child] pos {:component-swap true}) (pcb/change-parent (:parent-id related-shape-in-new) [orig-swapped-child] pos {:allow-altering-copies true})
;; We need to update the swap slot only when it pointed ;; We need to update the swap slot only when it pointed
;; to the swap-ref-id. Oterwise this is a swapped item ;; to the swap-ref-id. Oterwise this is a swapped item
@ -102,7 +102,7 @@
#(ctk/set-swap-slot % (:shape-ref related-shape-in-new)))) #(ctk/set-swap-slot % (:shape-ref related-shape-in-new))))
;; Delete new non-swapped item ;; Delete new non-swapped item
(cls/generate-delete-shapes ldata page objects (d/ordered-set (:id related-shape-in-new)) {:component-swap true}) (cls/generate-delete-shapes ldata page objects (d/ordered-set (:id related-shape-in-new)) {:allow-altering-copies true})
second))) second)))
(defn- child-of-swapped? (defn- child-of-swapped?

View file

@ -198,7 +198,7 @@
undo-id (or (:undo-id options) (js/Symbol)) undo-id (or (:undo-id options) (js/Symbol))
[all-parents changes] (-> (pcb/empty-changes it (:id page)) [all-parents changes] (-> (pcb/empty-changes it (:id page))
(cls/generate-delete-shapes fdata page objects ids (cls/generate-delete-shapes fdata page objects ids
{:ignore-touched (:component-swap options) {:ignore-touched (:allow-altering-copies options)
:undo-group (:undo-group options) :undo-group (:undo-group options)
:undo-id undo-id}))] :undo-id undo-id}))]