diff --git a/frontend/src/arthur/events/ui.cljs b/frontend/src/arthur/events/ui.cljs index 90a0124..d67ce86 100644 --- a/frontend/src/arthur/events/ui.cljs +++ b/frontend/src/arthur/events/ui.cljs @@ -584,11 +584,25 @@ (edit/transaction (constantly (:clip r))) (select-results [])))))))) +(defn- paste-anchor + "The selection a paste goes by: the selection, unless it is still exactly + what the last paste made, and then whatever that paste went by. + + FIGMA'S RULE. A paste goes into what is selected and selects what it made, + so without this pasting a symbol twice put the second copy inside the first, + and a third inside that. Pasting again onto your own paste means another one + beside it; select anything else and paste follows the selection again." + [db] + (let [{:keys [made from]} (get-in db [:ui :paste])] + (if (and made (= made (current-selections db))) + from + (get-in db [:ui :selection])))) + (defn paste-destination "The effective creation context at the playhead, in the destination's frames." [db clip st] (creation/target clip st (get-in db [:ui :open]) - (get-in db [:ui :selection]) (editing-frame db clip))) + (paste-anchor db) (editing-frame db clip))) (rf/reg-event-db ::paste @@ -602,10 +616,11 @@ r (clipboard/paste clip payload sid frame {:fresh-id random-uuid})] (if-let [why (:refused r)] (command-refused db why) - (-> db - (edit/transaction (constantly (:clip r))) - (select-results - (mapv (fn [id] [:node sid id (conj path id)]) (:roots r)))))))))) + (let [made (mapv (fn [id] [:node sid id (conj path id)]) (:roots r))] + (-> db + (edit/transaction (constantly (:clip r))) + (select-results made) + (assoc-in [:ui :paste] {:from (paste-anchor db) :made made}))))))))) (defn- duplicate-selected [db unique?] (let [clip (:clip (store/entry (:clip/current db))) diff --git a/frontend/test/arthur/events/lane_test.cljs b/frontend/test/arthur/events/lane_test.cljs index aefaa67..37e5da8 100644 --- a/frontend/test/arthur/events/lane_test.cljs +++ b/frontend/test/arthur/events/lane_test.cljs @@ -1,5 +1,5 @@ (ns arthur.events.lane-test - (:require [cljs.test :refer [deftest is]] + (:require [cljs.test :refer [deftest is testing]] [arthur.domain.clip :as clip] [arthur.domain.creation :as creation] [arthur.domain.history :as history] @@ -316,6 +316,34 @@ (select-keys (ui/paste-destination db doc nil) [:sid :path :frame])) "an inactive selected symbol falls back to the open symbol"))) +(deftest pasting-onto-your-own-paste-goes-beside-it + (let [doc {:fps 24 :width 20 :height 20 + :symbols + {:main {:id :main :frames 40 + :nodes {:drawing {:id :drawing :kind :instance :z "a" + :span [0 10] :time {:at 0 :rate 1} + :source {:symbol :inside} + :playback {:in 0 :speed 1 :end :stop}}}} + :inside {:id :inside :frames 10 :nodes {}}}} + id (store/install! {:clip doc :store {}} "paste-beside") + instances #(count (get-in (store/entry id) [:clip :symbols % :nodes]))] + (reset! rf-db/app-db {:clip/current id :paint/revision 0 + :ui {:open :main :selection [:node :main :drawing [:drawing]]} + :playback {:frame 2}}) + (rf/dispatch-sync [::ui/copy]) + (rf/dispatch-sync [::ui/select [:node :main nil []]]) + (rf/dispatch-sync [::ui/paste]) + (is (= 2 (instances :main))) + (rf/dispatch-sync [::ui/paste]) + (rf/dispatch-sync [::ui/paste]) + (is (= 4 (instances :main)) + "the selected copy is a symbol under the playhead, and the paste still goes beside it") + (is (= 0 (instances :inside))) + (testing "selecting something else makes paste follow the selection again" + (rf/dispatch-sync [::ui/select [:node :main :drawing [:drawing]]]) + (is (= :inside (:sid (ui/paste-destination @rf-db/app-db + (:clip (store/entry id)) {}))))))) + (deftest multi-paste-uses-one-current-primary-target-and-selects-the-batch (let [doc (fixture/document) id (store/install! {:clip doc :store {}} "multi-paste-target")