From 51fa5e5a00f5634dd64f5e0365c120afad764f83 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 17 Nov 2026 11:47:22 +0100 Subject: [PATCH] :sparkles: Generate interactions and pastes out of copies, pages and files The structure programs now cover what their worlds lacked: - `:interact` gives a shape on a main, a copy head or a copy child its prototype interactions (open-URL actions, so no board ties them to deletions), through the prototype panel's add, edit and remove events in the frontend replay. Interactions are no sync attribute: the target must end with what was set, no other operation may change those of a shape that stays, and a swap or a switch keeps a head's. - `:paste` also takes a nested copy head a copy holds, which the clipboard advances to its component's main. The model gives the pasted shapes the overrides of the references they skip, and drops the swap of a swapped head, now a plain copy of its component. - Worlds with `:pages` hold a second page that a paste can land on, and `:library-paste` pastes library mains and copies into the consumer, where both become copies of the library's components. Off the current page or file, the node and the frontend put the pasted corner at the same free point; the frontend pastes with nothing selected and the pointer there. - A transform no longer targets a shape without area, as a union of hidden shapes, which no transform can size. Samples of 150 structure, swap and scale programs found F588, F589 and F590 (fixed) and are then clean. Harness only; the benchmark is not run. AI-assisted-by: claude-opus-5-5 --- .../test_helpers/composable/comp/nodes.cljc | 80 +++++++-- .../common/test_helpers/composable/gen.cljc | 155 ++++++++++++++++-- .../logic/composable_gen_test.cljc | 68 ++++++++ .../composable_tests/generated_test.cljs | 34 ++++ .../composable_tests/interpreter.cljs | 67 +++++++- 5 files changed, 372 insertions(+), 32 deletions(-) diff --git a/common/src/app/common/test_helpers/composable/comp/nodes.cljc b/common/src/app/common/test_helpers/composable/comp/nodes.cljc index 8e59a7bdda..ef3b9c3e68 100644 --- a/common/src/app/common/test_helpers/composable/comp/nodes.cljc +++ b/common/src/app/common/test_helpers/composable/comp/nodes.cljc @@ -342,6 +342,38 @@ [target annotation] (tm/assign-id (->AnnotateComponent target annotation))) +;; --------------------------------------------------------------------------- +;; set-interactions — give a shape its prototype interactions +;; +;; As the prototype panel's add, edit and remove of an interaction: the shape +;; `target` names ends with `interactions`. Interactions are no sync +;; attribute: sync leaves them, and a swap or a switch keeps a head's. +;; --------------------------------------------------------------------------- + +(defrecord SetInteractions [target interactions] + IComponentOperation + (op-kind [_] :user) + + tm/IOperation + (apply-to [this situation] + (let [the-file (tm/file situation) + page (thf/current-page the-file) + id (tm/target-shape-id situation target) + changes (-> (pcb/empty-changes nil (:id page)) + (pcb/with-objects (:objects page)) + (pcb/update-shapes [id] #(assoc % :interactions interactions))) + file' (thf/apply-changes the-file changes)] + (-> situation + (tm/with-file file') + (record-changes changes) + (tm/record-application this {:target target :interactions interactions}))))) + +(defn set-interactions + "Constructor for the set-interactions operation: give the shape `target` + names the prototype `interactions`, a vector." + [target interactions] + (tm/assign-id (->SetInteractions target interactions))) + (declare libraries) (defrecord EditGrid [target action value] @@ -1389,14 +1421,22 @@ (cfh/get-children-with-self (:objects page) id)) [id])) -(defrecord PasteToRoot [target] +(defn paste-point + "Where a paste on another page puts the corner of the shapes it pastes: + to the right of the root shapes `page` already holds, one step for each." + [page] + (gpt/point (+ 10000 (* 1000 (count (get-in page [:objects uuid/zero :shapes])))) 0)) + +(defrecord PasteToRoot [target to-page from-file] IComponentOperation (op-kind [_] :user) IAppliedResult (applied-result [_ file-before file-after] - {:new-head (new-subtree-head (thf/current-page file-before) - (thf/current-page file-after))}) + (let [page (fn [file] (if to-page + (get-in file [:data :pages-index to-page]) + (thf/current-page file)))] + {:new-head (new-subtree-head (page file-before) (page file-after))})) tm/IOperation (apply-to [this situation] @@ -1404,13 +1444,23 @@ page (thf/current-page the-file) libs (libraries situation) id (tm/target-shape-id situation target) - copied (-> (copied-objects the-file page libs id) + ;; the file it is copied from: `from-file`, a library, or the current one + source (if from-file (get libs from-file) the-file) + copied (-> (copied-objects source (thf/current-page source) libs id) (update id assoc :parent-id uuid/zero :frame-id uuid/zero)) - changes (-> (pcb/empty-changes nil (:id page)) - (cld/generate-duplicate-changes (merge (:objects page) copied) - page + ;; the page it is pasted on: `to-page`, or the current one + dest (if to-page (get-in the-file [:data :pages-index to-page]) page) + ;; on another page or from another file, its corner goes past the + ;; shapes already there + delta (if (or to-page from-file) + (let [bounds (gsh/shapes->rect [(get copied id)])] + (gpt/subtract (paste-point dest) (gpt/point (:x1 bounds) (:y1 bounds)))) + (gpt/point 0 0)) + changes (-> (pcb/empty-changes nil (:id dest)) + (cld/generate-duplicate-changes (merge (:objects dest) copied) + dest #{id} - (gpt/point 0 0) + delta libs (:data the-file) (:id the-file)) @@ -1425,13 +1475,19 @@ (-> situation (tm/with-file file') (record-changes changes) - (tm/record-application this (merge {:target target} + (tm/record-application this (merge (cond-> {:target target} + to-page (assoc :to-page to-page) + from-file (assoc :from-file from-file)) (applied-result this the-file file'))))))) (defn paste-to-root - "Copy the shape `target` names and paste it at the page root." - [target] - (tm/assign-id (->PasteToRoot target))) + "Copy the shape `target` names and paste it at the root of its page, or + of the page `to-page`; with `from-file`, copy it from that library's + current page into the current file's." + ([target] (paste-to-root target nil nil)) + ([target to-page] (paste-to-root target to-page nil)) + ([target to-page from-file] + (tm/assign-id (->PasteToRoot target to-page from-file)))) ;; --------------------------------------------------------------------------- ;; nest-copy — move an existing copy into a main diff --git a/common/src/app/common/test_helpers/composable/gen.cljc b/common/src/app/common/test_helpers/composable/gen.cljc index 44483934c1..802ee75f41 100644 --- a/common/src/app/common/test_helpers/composable/gen.cljc +++ b/common/src/app/common/test_helpers/composable/gen.cljc @@ -266,6 +266,30 @@ (assoc id groups))) before model))) +(defn- inherit-skipped-groups + "`model` with each shape of the copy `new-head`, pasted from `source-id` of + `objects` out of `levels` copy levels into `after-objects`, also holding + the groups the references it skips author: the clipboard keeps the values + they gave it. The pasted shape's own groups win over theirs, and a nearer + reference's over a farther one's." + [model objects source-id after-objects new-head levels] + (letfn [(ref-at [id n] (nth (iterate #(:shape-ref (get objects %)) id) n)) + (walk [model source-id copy-id] + (let [source (get objects source-id) + copy (get after-objects copy-id) + pairs (map vector (:shapes source) (:shapes copy)) + model (reduce (fn [model level] + (let [ref-id (ref-at source-id level) + ;; the children of the reference, as the pasted shape's + mapping (into {} (map (fn [[child pasted]] [(ref-at child level) pasted])) pairs) + groups (grid/remap-groups (get model ref-id) mapping)] + (cond-> model + (seq groups) (update copy-id #(merge groups %))))) + model + (range 1 (inc levels)))] + (reduce (fn [model [child pasted]] (walk model child pasted)) model pairs)))] + (walk model source-id new-head))) + (defn- settle-model [model situation synced?] (let [references (when (some :layout-grid-cells (vals model)) @@ -351,7 +375,17 @@ (:library opts) (with-library opts) (:stale-libraries opts) (cf/freeze-libraries) (:rich-variants opts) (as-> s (assoc s :shape-budget (+ 80 (count (tree s))))) - (:scale opts) (as-> s (assoc s :shape-budget (+ 120 (count (tree s))))))) + (:scale opts) (as-> s (assoc s :shape-budget (+ 120 (count (tree s))))) + ;; a second page, empty, for pastes from the first + (:pages opts) (as-> s (tm/with-file s (let [file (tm/file s)] + (-> (thf/add-sample-page file :generated-page) + (vary-meta assoc :current-page-id (thf/current-page-id file)))))))) + +(defn- other-page-id + "The page of `situation`'s file other than its current one, if any." + [situation] + (let [file (tm/file situation)] + (first (remove #{(thf/current-page-id file)} (get-in file [:data :pages]))))) (defn candidates [situation model kind scope] @@ -386,6 +420,9 @@ :detach (top-copy shape) (:resize :rotate) (top-copy shape) :transform (and (not= :text (:type shape)) + ;; a bool of hidden shapes has no area to transform + (< 0.5 (get-in shape [:selrect :width])) + (< 0.5 (get-in shape [:selrect :height])) (case (mod (or scope 0) 5) 0 (ctk/main-instance? shape) 1 (and (nil? (:shape-ref shape)) @@ -409,16 +446,20 @@ :nest (and (top-copy shape) (not (overridden (:id shape)))) ;; a shape a main holds, other than its root: a plain shape ;; or a nested copy head, which no copy around it holds - ;; a top copy, or a plain shape a main holds: nothing in it - ;; leaves a copy level, so the clipboard advances no ref + ;; a top copy, a plain shape a main holds, or a nested copy + ;; head a copy holds, which leaves its copy's level: the + ;; clipboard advances its refs to the component it copies :paste (and (< (count objects) (get situation :shape-budget 120)) (or (top-copy shape) + (and (ctk/instance-head? shape) (:shape-ref shape) + (ctn/has-any-copy-parent? objects shape)) (and (nil? (:shape-ref shape)) (not (ctk/main-instance? shape)) (some ctk/main-instance? (cfh/get-parents objects (:id shape)))))) :rename (and (ctk/main-instance? shape) (not (:variant-id shape)) (live-component shape)) :annotate (and (ctk/main-instance? shape) (live-component shape)) + :interact true :move (and (not (ctk/main-instance? shape)) (or (nil? (:shape-ref shape)) (ctk/instance-head? shape)) (not (ctn/has-any-copy-parent? objects shape)) @@ -508,6 +549,26 @@ :library-undo :library-redo :library-accept :library-transform :library-text :library-text-style :library-variant-property :library-add-variant :library-make-variant :library-combine}) +(defn- library-paste-candidates + "The shapes of the libraries' current pages that the consumer can paste: + main roots of live components, which it pastes as components of its own, + and top copies, which stay copies of their components." + [situation] + (vec + (for [[library-index [library-id library]] (map-indexed vector (ordered-files (tm/aux-files situation))) + :let [objects (:objects (thf/current-page library))] + entry (tree (tm/with-file situation library)) + :let [shape (:shape entry)] + :when (and (< (count (:objects (thf/current-page (tm/file situation)))) + (get situation :shape-budget 120)) + (or (and (ctk/main-instance? shape) + (not (:variant-id shape)) + (let [component (get-in library [:data :components (:component-id shape)])] + (and component (not (:deleted component))))) + (and (ctk/instance-head? shape) (:shape-ref shape) + (not (ctn/has-any-copy-parent? objects shape)))))] + (assoc entry :path (into [:library library-index] (:path entry)) :from-library library-id)))) + (def ^:private local-variant-kind "The local kind of each library variant kind." {:library-variant-property :variant-property :library-add-variant :add-variant @@ -660,6 +721,13 @@ "Annotations a component is given; nil removes it." ["Note" "Another note" nil]) +(def interaction-values + "Prototype interactions a shape is given; empty removes them. They open a + URL, so no board they lead to ties them to deletions." + [[{:event-type :click :action-type :open-url :url "https://example.com/a"}] + [{:event-type :click :action-type :open-url :url "https://example.com/b"}] + []]) + (def structure-edits "Structure actions: the action and, for a bool, its type." [[:group] [:mask] [:ungroup] [:unmask] [:bool :union] [:bool :difference] @@ -746,8 +814,14 @@ [situation model [kind index property value scope :as tuple]] (let [index (or index 0) [style-property style-values] (nth style-edits (mod (or property 0) (count style-edits))) - items (cond->> (if (contains? library-source-kinds kind) + items (cond->> (cond + (contains? library-source-kinds kind) (library-candidates situation kind scope) + + (= kind :library-paste) + (library-paste-candidates situation) + + :else (candidates situation model (if (#{:style :grid-cell :grid-direction :grid-track} kind) :edit kind) (if (= kind :grid-track) true scope))) (#{:grid-cell :grid-direction :grid-track} kind) @@ -902,11 +976,17 @@ :remove-child (n/remove-child target) :duplicate (n/duplicate target) (:nest :move) (n/nest-copy target (constantly (get-in selected [:parent :id]))) - :paste (n/paste-to-root target) + ;; an odd choice pastes on the other page, when there is one + :library-paste (n/paste-to-root (constantly (get-in selected [:shape :id])) nil + (:from-library selected)) + :paste (n/paste-to-root target (when (odd? (or (nth tuple 4 nil) 0)) + (other-page-id situation))) :rename (n/rename-component target (first (nth rename-values (mod (or (nth tuple 3 nil) 0) (count rename-values))))) :annotate (n/annotate-component target (nth annotations (mod (or (nth tuple 3 nil) 0) (count annotations)))) + :interact (n/set-interactions target (nth interaction-values + (mod (or (nth tuple 3 nil) 0) (count interaction-values)))) :delete (n/delete-component target) :swap (n/swap-shape target (constantly (get-in selected [:destination :shape :component-id]))) :switch (n/switch-variant target (:variant-value selected) :pos (:variant-pos selected)) @@ -1162,7 +1242,7 @@ model)) (:text :text-style) (shadow/edit-text model shape (:content (n/text-edit-commit shape (:edit op) (:value op))) (some? (:typography op))) - (:duplicate :paste) + (:duplicate :paste :library-paste) (let [after-objects (all-objects after) new-head (:new-head (n/applied-result op (tm/file before) (tm/file after))) head (get after-objects new-head) @@ -1172,12 +1252,35 @@ (get (into {} (mapcat cf/object-trees) (vals (cf/reference-libraries after head))) (:shape-ref head))) (get after-objects (:shape-ref head))) - model (shadow/duplicate model objects id (:objects (thf/current-page (tm/file after))) new-head)] + model (shadow/duplicate model objects id + (if-let [page-id (:to-page op)] + (get-in (tm/file after) [:data :pages-index page-id :objects]) + (:objects (thf/current-page (tm/file after)))) + new-head) + ;; a nested copy pasted out of its copies takes the overrides of the + ;; references it leaves behind + page-objects (:objects (thf/current-page (tm/file before))) + ;; the references it skips to reach the main of its component + main-id (get-in (cf/libraries before) [(:component-file shape) :data :components + (:component-id shape) :main-instance-id]) + levels (if (and (= kind :paste) (get page-objects id) (ctn/has-any-copy-parent? page-objects shape)) + (->> (iterate #(:shape-ref (get objects %)) (:shape-ref shape)) + (take-while some?) + (take 16) + (take-while #(not= % main-id)) + count) + 0) + model (cond-> model + (and new-head (pos? levels)) + (-> (inherit-skipped-groups objects id after-objects new-head levels) + ;; out of every copy level, a swapped head is a plain copy of + ;; the component it shows + (update new-head dissoc :swap-group)))] ;; a copy pasted on the page keeps the geometry it showed: what it ;; authored, now where it lands, and the orientation and size another ;; parent gave it, which its main's need not be (cond-> model - (and (= kind :paste) head main (:shape-ref head) + (and (#{:paste :library-paste} kind) head main (:shape-ref head) (or (get-in model [new-head :geometry-group]) (and (not (cfh/root? (get objects (:parent-id shape)))) (or (not (gmt/close? (or (:transform head) (gmt/matrix)) (or (:transform main) (gmt/matrix)))) @@ -1435,6 +1538,32 @@ (when-not (= (get-in resolved [:op :annotation]) (:annotation component)) (oracle/fail :I9 {:phase :requested-annotation :expected (get-in resolved [:op :annotation]) :actual (:annotation component)}))))) + (when (= kind :interact) + (let [actual (:interactions (tm/shape-by-id after (get-in resolved [:shape :id])))] + (when-not (= (vec (get-in resolved [:op :interactions])) (vec actual)) + (oracle/fail :I9 {:phase :requested-interactions :expected (get-in resolved [:op :interactions]) + :actual actual})))) + ;; interactions are no sync attribute: only their edit, or its undo or + ;; redo, changes those of a shape that stays; a swap or a switch gives + ;; the new head those of the head it replaces + (when-not (#{:interact :undo :redo :library-undo :library-redo} kind) + (let [interactions (fn [situation] + (into {} (for [file (cons (tm/file situation) (vals (tm/aux-files situation))) + page (vals (get-in file [:data :pages-index])) + shape (vals (:objects page))] + [(:id shape) (vec (:interactions shape))]))) + before (interactions situation) + kept (interactions after) + changed (filterv #(and (contains? kept %) (not= (get before %) (get kept %))) (keys before)) + head (get-in resolved [:shape :id]) + new-head (when (and (#{:swap :switch} kind) (get-in resolved [:op])) + (:id (tm/shape-by-id after head)))] + (when (seq changed) + (oracle/fail :I9 {:phase :kept-interactions :shapes changed + :before (select-keys before changed) :after (select-keys kept changed)})) + (when (and new-head (not= (get before head) (get kept new-head))) + (oracle/fail :I9 {:phase :swapped-head-interactions :kind kind + :before (get before head) :after (get kept new-head)})))) ;; only an annotation edit, or its undo or redo, changes an annotation (when-not (#{:annotate :undo :redo :library-undo :library-redo} kind) (let [annotations (fn [situation] @@ -1704,7 +1833,7 @@ (def structure-kinds [:edit :reset :update-main :detach :sync :undo :redo :text :text-style :add-child :remove-child :duplicate :nest :delete :restore :resize :rotate :style :transform - :grid-direction :grid-cell :move :rename :annotate :paste]) + :grid-direction :grid-cell :move :rename :annotate :paste :interact]) (def structure-program-gen (gen/let [components (gen/choose 2 3) @@ -1713,9 +1842,10 @@ layout-children gen/boolean layout-item-margins gen/boolean grid (gen/elements [nil :square :empty]) + pages gen/boolean ops (operation-gen structure-kinds)] {:world [components depth {:text true :text-styles true :layout layout :layout-children layout-children :layout-item-margins layout-item-margins - :grid-square (= grid :square) :grid-empty (= grid :empty)}] + :grid-square (= grid :square) :grid-empty (= grid :empty) :pages pages}] :ops ops})) (def swap-kinds (into structure-kinds [:swap :switch])) @@ -1727,11 +1857,12 @@ layout-children gen/boolean layout-item-margins gen/boolean grid (gen/elements [nil :square :empty]) + pages gen/boolean ops (operation-gen swap-kinds)] {:world [components 2 {:variants true :variant-diff differ? :rich-variants true :text true :text-styles true :layout layout :layout-copies true :layout-children layout-children :layout-item-margins layout-item-margins - :grid-square (= grid :square) :grid-empty (= grid :empty)}] + :grid-square (= grid :square) :grid-empty (= grid :empty) :pages pages}] :ops ops})) (def variant-kinds (into swap-kinds [:variant-property :add-variant :make-variant :combine])) @@ -1834,7 +1965,7 @@ :ops ops})) (def library-kinds - (into structure-kinds [:swap :switch :library-sync])) + (into structure-kinds [:swap :switch :library-sync :library-paste])) (def library-program-gen (gen/let [depth (gen/choose 0 2) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 20a9a31ac6..4b2596d87e 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -4470,7 +4470,75 @@ :ops [[:remove-child 0 0 0 0] [:add-child 1] [:structure 0 4 0] [:transform 0 16 0 3] [:rotate 0 0 0 0] [:rotate 1 0 0 0]]})))) +(t/deftest interactions-stay-as-set-through-sync-swap-and-switch + ;; interactions are no sync attribute: sync, reset and update main leave + ;; them, and a swap or a switch keeps a head's + (doseq [program [{:world [2 1 {:variants true :rich-variants true}] + :ops [[:interact 0 0 0 0] [:interact 5 0 0 1] [:interact 7 0 0 0] [:sync 0] [:swap 1] + [:switch 0] [:reset 2] [:update-main 3] [:undo] [:redo] [:interact 7 0 0 2]]} + {:world [2 2 {:variants true :layout :grid :layout-copies true :text true}] + :ops [[:interact 3 0 0 0] [:edit 3 0 1 1] [:detach 3] [:interact 9 0 0 1] [:switch 1] + [:swap 4] [:duplicate 2] [:paste 2] [:undo] [:undo] [:redo]]}]] + (let [result (cg/run program)] + (t/is (pos? (get-in result [:executed :interact] 0)))))) + +(t/deftest generated-a-nested-copy-pasted-out-keeps-the-overrides-of-the-references-it-skips + ;; a nested copy pasted out of its copy refers to its component's main and + ;; overrides what the references it leaves behind override + (t/is (map? (cg/run {:world [4 4 {:variants true :layout :grid :rich-variants true :grid-empty true :text true}] + :ops [[:edit 19 0 1 1] [:remove-child 10 6 4 27] [:paste 26 25 23 15]]})))) + +(t/deftest pastes-on-another-page-sync-and-undo-there + ;; copies pasted on a second page sync from their mains and undo there + (doseq [program [{:world [2 1 {:pages true :variants true}] + :ops [[:paste 3 0 0 1] [:edit 3 0 1 1] [:sync 0] [:paste 5 0 0 1] [:undo] [:redo]]} + {:world [2 2 {:pages true :variants true :rich-variants true :text true}] + :ops [[:edit 9 0 1 1] [:paste 9 0 0 1] [:paste 2 0 0 1] [:update-main 2] [:swap 2] + [:undo] [:undo] [:redo]]}]] + (let [result (cg/run program) + file (tm/file (:situation (cg/settle-state (reduce cg/step (cg/initial-state (:world program)) + (:ops program))))) + other (second (get-in file [:data :pages]))] + (t/is (= 2 (get-in result [:executed :paste]))) + (t/is (< 1 (count (get-in file [:data :pages-index other :objects]))))))) + +(t/deftest library-pastes-become-copies-of-the-library-components + ;; library mains and copies pasted into the consumer become copies of the + ;; library's components, which sync from it + (doseq [program [{:world [2 1 {:library true :variants true :rich-variants true :text true}] + :ops [[:library-paste 0] [:library-paste 1] [:library-paste 2] [:edit 0 0 1 1] + [:library-sync 0] [:undo] [:redo]]} + {:world [2 2 {:library true :variants true :text true :layout :flex :layout-copies true}] + :ops [[:library-paste 2] [:library-paste 5] [:swap 1] [:undo] [:undo] [:redo] [:library-paste 7]]}]] + (let [result (cg/run program)] + (t/is (= 3 (get-in result [:executed :library-paste])))))) + (t/deftest generated-an-update-main-into-a-nested-copy-authors-the-sizing-it-fixes ;; a copy child filling a row pushes a transform into the nested copy its ;; main holds, which takes it with its sizing fixed as its own override (t/is (map? (cg/run {:world [3 2 {:text true :layout :flex}] :ops [[:nest 24 11 9 6] [:delete 1] [:restore 1] [:add-child 15 16 4 29] [:move 23 24 12 20] [:edit 25 25 27 25] [:undo 23 17 15 25] [:style 23 29 25 20] [:transform 29 11 7 27] [:update-main 2 19 2 29]]})))) + +(t/deftest generated-a-paste-out-of-a-copy-follows-a-swap-in-its-main + ;; a nested copy pasted out of its copy, whose main swapped the head it + ;; refers to, becomes a plain copy of the component swapped in + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout :flex :variant-diff true + :layout-item-margins true :rich-variants true :layout-children true + :grid-empty true :layout-copies true :text true}] + :ops [[:text-style 7 4 0 10] [:switch 28 12 25 15] [:edit 25 1 1 :copy-head] [:reset 25] + [:variant-property 7 7 24 6] [:make-variant 12 15 4 25] [:text 16 8 1] [:combine 11] + [:switch 20] [:paste 18 15 27 2] [:make-variant 27 18 6 12] [:transform 25 1 26 25]]})))) + +(t/deftest generated-a-swapped-nested-head-pasted-out-is-a-plain-copy + ;; a swapped nested head pasted out of every copy level is a plain copy of + ;; the component it shows, with no swap of its own + (t/is (map? (cg/run {:world [4 3 {:variants true :rich-variants true :text true}] + :ops [[:combine 21] [:switch 15] [:delete 24] [:make-variant 19 5 5 30] + [:remove-child 11 11 16 13] [:combine 2] [:switch 3] [:nest 27 10 18 3] + [:switch 1 16 9 19] [:combine 0 25 21 2] [:paste 17 19 20 30]]})))) + +(t/deftest generated-a-union-of-hidden-shapes-is-left-untransformed + ;; a union of hidden shapes has no area, and no transform targets it + (t/is (map? (cg/run {:world [2 0 {:variants true :rich-variants true :text true :layout-copies true}] + :ops [[:style 1 1 1 0] [:add-child 0] [:structure 0 0 0] [:add-child 0] + [:structure 1 4 0] [:transform 1 1 0 1]]})))) + diff --git a/frontend/test/frontend_tests/composable_tests/generated_test.cljs b/frontend/test/frontend_tests/composable_tests/generated_test.cljs index e3b3ee122d..f19ccbf2cb 100644 --- a/frontend/test/frontend_tests/composable_tests/generated_test.cljs +++ b/frontend/test/frontend_tests/composable_tests/generated_test.cljs @@ -1192,7 +1192,41 @@ (select-keys before [:pass :fail :error])))))) (recur (inc index))))))) +(t/deftest ^:async interactions-replay-through-the-prototype-panel + ;; interactions set on mains, copy heads and children through the prototype + ;; panel stay as set through sync, swaps, switches, resets and update main + (await (replay {:world [2 1 {:variants true :rich-variants true}] + :ops [[:interact 0 0 0 0] [:interact 5 0 0 1] [:interact 7 0 0 0] [:sync 0] [:swap 1] + [:switch 0] [:reset 2] [:update-main 3] [:undo] [:redo] [:interact 7 0 0 2]]})) + (await (replay {:world [2 2 {:variants true :layout :grid :layout-copies true :text true}] + :ops [[:interact 3 0 0 0] [:edit 3 0 1 1] [:detach 3] [:interact 9 0 0 1] [:switch 1] + [:swap 4] [:duplicate 2] [:paste 2] [:undo] [:undo] [:redo]]}))) + +(t/deftest ^:async generated-a-nested-copy-pasted-out-keeps-the-overrides-of-the-references-it-skips + ;; the clipboard advances a nested copy pasted out of its copy to its + ;; component's main, with the overrides of the references it leaves behind + (await (replay {:world [4 4 {:variants true :layout :grid :rich-variants true :grid-empty true :text true}] + :ops [[:edit 19 0 1 1] [:remove-child 10 6 4 27] [:paste 26 25 23 15]]}))) + +(t/deftest ^:async pastes-on-another-page-replay-through-the-frontend + ;; copies pasted on a second page sync from their mains and undo there + (await (replay {:world [2 1 {:pages true :variants true}] + :ops [[:paste 3 0 0 1] [:edit 3 0 1 1] [:sync 0] [:paste 5 0 0 1] [:undo] [:redo]]})) + (await (replay {:world [2 2 {:pages true :variants true :rich-variants true :text true}] + :ops [[:edit 9 0 1 1] [:paste 9 0 0 1] [:paste 2 0 0 1] [:update-main 2] [:swap 2] + [:undo] [:undo] [:redo]]}))) + +(t/deftest ^:async library-pastes-replay-through-the-frontend + ;; library mains and copies pasted into the consumer become copies of the + ;; library's components, which sync from it + (await (replay {:world [2 1 {:library true :variants true :rich-variants true :text true}] + :ops [[:library-paste 0] [:library-paste 1] [:library-paste 2] [:edit 0 0 1 1] + [:library-sync 0] [:undo] [:redo]]})) + (await (replay {:world [2 2 {:library true :variants true :text true :layout :flex :layout-copies true}] + :ops [[:library-paste 2] [:library-paste 5] [:swap 1] [:undo] [:undo] [:redo] [:library-paste 7]]}))) + (t/deftest ^:async generated-an-update-main-into-a-nested-copy-authors-the-sizing-it-fixes ;; a copy child filling a row pushes a transform into the nested copy its ;; main holds, which takes it with its sizing fixed as its own override (await (replay {:world [3 2 {:text true :layout :flex}] :ops [[:nest 24 11 9 6] [:delete 1] [:restore 1] [:add-child 15 16 4 29] [:move 23 24 12 20] [:edit 25 25 27 25] [:undo 23 17 15 25] [:style 23 29 25 20] [:transform 29 11 7 27] [:update-main 2 19 2 29]]}))) + diff --git a/frontend/test/frontend_tests/composable_tests/interpreter.cljs b/frontend/test/frontend_tests/composable_tests/interpreter.cljs index 912011f4b2..53aed5d8ed 100644 --- a/frontend/test/frontend_tests/composable_tests/interpreter.cljs +++ b/frontend/test/frontend_tests/composable_tests/interpreter.cljs @@ -35,6 +35,7 @@ [app.main.data.workspace :as dw] [app.main.data.workspace.bool :as dwb] [app.main.data.workspace.groups :as dwg] + [app.main.data.workspace.interactions :as dwi] [app.main.data.workspace.libraries :as dwl] [app.main.data.workspace.path.shapes-to-path :as dwps] [app.main.data.workspace.reflow :as wrf] @@ -52,6 +53,7 @@ [app.main.features :as features] [app.main.repo :as rp] [app.main.store :as st] + [app.main.streams :as ms] [app.plugins.reflow :as pwrf] [app.util.dom :as dom] [beicon.v2.core :as rx] @@ -67,6 +69,13 @@ (defonce ^:private original-update-thumbnail (atom nil)) +(defn- show-page + "The workspace showing the page `page-id`, as the pages panel leaves it." + [page-id] + (ptk/reify ::show-page + ptk/UpdateEvent + (update [_ state] (assoc state :current-page-id page-id)))) + (defn- noop-thumbnail-event [] (ptk/reify ::noop-thumbnail ptk/WatchEvent @@ -359,14 +368,34 @@ :change-selection? false)] (instance? n/PasteToRoot op) - ;; the clipboard's copy of the shape, pasted with the root selected - (let [file (current-file) - page (get-in file [:data :pages-index (:current-page-id @st/state)]) - data (thp/simulate-copy-shape #{(tm/target-shape-id situation (:target op))} - (:objects page) {(:id file) file} page file - #{"components/v2"} 46)] - [(dws/select-shape uuid/zero) - (dw/paste-shapes data)]) + ;; the clipboard's copy of the shape, from its file's page that holds it, + ;; pasted with the root selected; on another page or from another file, + ;; with nothing selected and the pointer where the node puts the corner + (let [file (current-file) + page (get-in file [:data :pages-index (:current-page-id @st/state)]) + id (tm/target-shape-id situation (:target op)) + source (if-let [from (:from-file op)] (get-in @st/state [:files from]) file) + src-page (or (d/seek #(contains? (:objects %) id) (vals (get-in source [:data :pages-index]))) page) + data (thp/simulate-copy-shape #{id} (:objects src-page) (:files @st/state) src-page source + #{"components/v2"} 46) + dest (if-let [to-page (:to-page op)] (get-in file [:data :pages-index to-page]) page)] + (cond + (:to-page op) + ;; pasted on another page, which the workspace shows for it + (do (rx/push! ms/mouse-position (n/paste-point dest)) + [(show-page (:id dest)) + (dws/deselect-all) + (dw/paste-shapes data) + (show-page (:id page))]) + + (:from-file op) + (do (rx/push! ms/mouse-position (n/paste-point dest)) + [(dws/deselect-all) + (dw/paste-shapes data)]) + + :else + [(dws/select-shape uuid/zero) + (dw/paste-shapes data)])) (instance? n/NestCopy op) (let [parent-id (tm/target-shape-id situation (:into op)) @@ -382,6 +411,22 @@ (let [shape (tm/shape-by-id situation (tm/target-shape-id situation (:target op)))] [(dwl/rename-component-and-main-instance (:component-id shape) (:value op))]) + (instance? n/SetInteractions op) + ;; the prototype panel: edit the first interaction or add one, and remove + ;; the rest + (let [shape (tm/shape-by-id situation (tm/target-shape-id situation (:target op))) + current (vec (:interactions shape)) + [wanted] (:interactions op)] + (cond-> [] + (and wanted (seq current)) + (conj (dwi/update-interaction shape 0 (constantly wanted))) + + (and wanted (empty? current)) + (conj (dwi/add-interaction nil (:id shape) wanted)) + + :always + (into (repeatedly (- (count current) (if wanted 1 0)) #(dwi/remove-interaction shape (if wanted 1 0)))))) + (instance? n/AnnotateComponent op) (let [shape (tm/shape-by-id situation (tm/target-shape-id situation (:target op)))] [(dw/update-component-annotation (:component-id shape) (:annotation op))]) @@ -567,6 +612,12 @@ (and (instance? n/SyncFromLibrary op) (nil? (:source op))) (dissoc :source) + ;; a paste on its own page of its own file names neither + (instance? n/PasteToRoot op) + (as-> record (cond-> record + (nil? (:to-page op)) (dissoc :to-page) + (nil? (:from-file op)) (dissoc :from-file))) + (satisfies? n/IAppliedResult op) (merge (n/applied-result op before (tm/file situation))))] (tm/record-application situation op record)))