Compare commits

...
Author SHA1 Message Date
Andrés Moya 47962cef52 wip 2026-08-27 12:21:18 +02:00
Andrés Moya 9c40563e27 wip 2026-08-27 12:14:53 +02:00
5 changed files with 146 additions and 3 deletions

No files matched your search

+14
View File
@@ -1167,6 +1167,20 @@
(check-shape parent-id [parent-id])
shapes))))
(defmethod components-changed :reorder-children
[file-data {:keys [page-id component-id parent-id]}]
(when (or page-id component-id)
(let [container (if page-id
(ctpl/get-page file-data page-id)
(get-in file-data [:components component-id]))
objects (:objects container)
xform (comp (filter :main-instance)
(map :component-id))]
(when (some? container)
(into #{} xform
(map #(ctn/get-shape container %)
(cons parent-id (cfh/get-parent-ids objects parent-id))))))))
(defmethod components-changed :add-obj
[file-data {:keys [parent-id page-id _component-id] :as change}]
(when page-id
+4 -2
View File
@@ -42,7 +42,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! :trace)
;; Add uuids here to filter logs to only show specific shapes or containers (and all shapes
;; contained in them).
@@ -1192,7 +1192,9 @@
(defn- compare-children
[changes shape-inst children-inst children-main container-inst container-main file libraries only-inst-cb only-main-cb both-cb swapped-cb moved-cb inverse? reset?]
(shape-log :trace (:id shape-inst) container-inst :msg "Compare children")
(shape-log :trace (:id shape-inst) container-inst :msg "Compare children"
:children-inst (map :id children-inst)
:children-main (map :id children-main))
(loop [children-inst (seq (or children-inst []))
children-main (seq (or children-main []))
changes changes]
@@ -0,0 +1,57 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC Sucursal en España SL
(ns common-tests.files.changes-test
(:require
[app.common.files.changes :as ch]
[app.common.uuid :as uuid]
[clojure.test :as t]))
(defn- make-file
[page-id main-id copy-id]
(let [comp-id (uuid/next)
child1 (uuid/next)
child2 (uuid/next)]
{:pages-index {page-id {:id page-id
:objects {main-id {:id main-id
:type :frame
:main-instance true
:component-id comp-id
:shapes [child1 child2]}
child1 {:id child1 :parent-id main-id}
child2 {:id child2 :parent-id main-id}
copy-id {:id copy-id
:type :frame
:component-root true
:component-id comp-id
:shapes [(uuid/next) (uuid/next)]}}}}
:components {comp-id {:id comp-id
:main-instance-id main-id
:main-instance-page page-id}}}))
(t/deftest components-changed-reorder-children
(t/testing "reorder-children in a main instance triggers component sync"
(let [page-id (uuid/next)
main-id (uuid/next)
copy-id (uuid/next)
file-data (make-file page-id main-id copy-id)
comp-id (-> file-data :components first key)
change {:type :reorder-children
:page-id page-id
:parent-id main-id
:shapes [(uuid/next) (uuid/next)]}]
(t/is (= #{comp-id} (ch/components-changed file-data change)))))
(t/testing "reorder-children inside a plain copy does not trigger component sync"
(let [page-id (uuid/next)
main-id (uuid/next)
copy-id (uuid/next)
file-data (make-file page-id main-id copy-id)
change {:type :reorder-children
:page-id page-id
:parent-id copy-id
:shapes [(uuid/next) (uuid/next)]}]
(t/is (= #{} (ch/components-changed file-data change))))))
@@ -539,3 +539,67 @@
;; A position-only change in the main component must not propagate to copies
;; and therefore must produce no redo-changes.
(t/is (empty? (:redo-changes sync-changes)))))
(t/deftest test-sync-reorder-main-children-via-mod-obj
(let [;; ==== Setup
file (-> (thf/sample-file :file1)
(tho/add-frame-with-child :main-nested-root :main-nested-child)
(thc/make-component :nested-comp :main-nested-root)
(tho/add-frame :main-top-root)
(thc/instantiate-component :nested-comp :nested-instance :parent-label :main-top-root)
(ths/add-sample-shape :top-group {:type :group :parent-label :main-top-root})
(thc/make-component :top-comp :main-top-root)
(thc/instantiate-component :top-comp :top-copy))
page (thf/current-page file)
main-root (ths/get-shape file :main-top-root)
copy-root (ths/get-shape file :top-copy)
main-order (:shapes main-root)
copy-order (:shapes copy-root)
;; ==== Action
changes {:redo-changes [{:type :mod-obj
:page-id (:id page)
:id (:id main-root)
:operations [{:type :set
:attr :shapes
:val (vec (reverse main-order))}]}]
:undo-changes []}
file' (thf/apply-changes file changes)]
;; ==== Check
;; The copy children must be reordered to keep the component referential integrity.
(t/is (= (vec (reverse main-order))
(get-in file' [:data :pages-index (:id page) :objects (:id main-root) :shapes])))
(t/is (= (vec (reverse copy-order))
(get-in file' [:data :pages-index (:id page) :objects (:id copy-root) :shapes])))))
(t/deftest test-sync-reorder-main-children-via-reorder-children
(let [;; ==== Setup
file (-> (thf/sample-file :file1)
(tho/add-frame-with-child :main-nested-root :main-nested-child)
(thc/make-component :nested-comp :main-nested-root)
(tho/add-frame :main-top-root)
(thc/instantiate-component :nested-comp :nested-instance :parent-label :main-top-root)
(ths/add-sample-shape :top-group {:type :group :parent-label :main-top-root})
(thc/make-component :top-comp :main-top-root)
(thc/instantiate-component :top-comp :top-copy))
page (thf/current-page file)
main-root (ths/get-shape file :main-top-root)
copy-root (ths/get-shape file :top-copy)
main-order (:shapes main-root)
copy-order (:shapes copy-root)
;; ==== Action
changes {:redo-changes [{:type :reorder-children
:page-id (:id page)
:parent-id (:id main-root)
:shapes (vec (reverse main-order))}]
:undo-changes []}
file' (thf/apply-changes file changes)]
;; ==== Check
;; The copy children must be reordered to keep the component referential integrity.
(t/is (= (vec (reverse main-order))
(get-in file' [:data :pages-index (:id page) :objects (:id main-root) :shapes])))
(t/is (= (vec (reverse copy-order))
(get-in file' [:data :pages-index (:id page) :objects (:id copy-root) :shapes])))))
@@ -63,7 +63,7 @@
[potok.v2.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! :trace)
(defn- debug-pretty-file
[file-id state]
@@ -1459,6 +1459,12 @@
(into #{}
(mapcat (partial ch/components-changed old-data))
changes))]
(log/trace :hint "processing changes"
:js/rchanges (log-changes
changes
old-data))
(log/trace :hint "changed components"
:components (str changed-components))
(cond
(empty? changed-components)
(rx/empty)