mirror of
https://github.com/penpot/penpot.git
synced 2026-11-27 13:32:28 +00:00
🐛 Undo a cut and the paste that follows it in one step
Moving a main to another page with cut and paste took two Ctrl+Z to undo: the first one only removed the pasted main, leaving the file with no main on any page and its component marked as deleted, and the second one brought the main back. The cut (`cut-selected`, now behind Ctrl+X and the context menu Cut) deletes the shapes with an undo group of its own and remembers it. A paste of those same shapes joins that group while the cut is still the last undo step, so one undo reverts both. A second paste, or a paste after another change, stays its own undo step. Found by the components Qase run (PENPOT-1376). Test: `cut-paste-undo-test`. Checked in the browser: cutting a main on Page 1, pasting it on Page 2 and pressing Ctrl+Z once puts the main back on Page 1 and leaves Page 2 empty. AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
1380d49da8
commit
306862b11c
@ -872,6 +872,24 @@
|
||||
(rx/of (dwgu/remove-guides hover-guides)))))))
|
||||
|
||||
|
||||
(defn cut-selected
|
||||
"Copy the selected shapes to the clipboard and delete them. The delete
|
||||
takes an undo group that the paste of these shapes joins while the cut is
|
||||
the last undo step, so one undo reverts the cut and the paste."
|
||||
[]
|
||||
(ptk/reify ::cut-selected
|
||||
ptk/WatchEvent
|
||||
(watch [_ state _]
|
||||
(let [selected (dsh/lookup-selected state)]
|
||||
(if (d/not-empty? selected)
|
||||
(let [undo-group (uuid/next)]
|
||||
(rx/of (dwcp/copy-selected)
|
||||
(dwcp/remember-cut undo-group)
|
||||
(dwsh/delete-shapes nil selected {:undo-group undo-group})
|
||||
(dws/deselect-all)))
|
||||
(rx/of (dwcp/copy-selected)
|
||||
(delete-selected)))))))
|
||||
|
||||
;; --- Start renaming selected shape
|
||||
|
||||
(defn start-rename-selected
|
||||
|
||||
@ -239,6 +239,31 @@
|
||||
(rx/catch on-copy-error)
|
||||
(rx/ignore))))))))))
|
||||
|
||||
(defn remember-cut
|
||||
"Record the selected shapes as cut, with the undo group of their delete,
|
||||
so that their paste can join it (see `cut-undo-group`)."
|
||||
[undo-group]
|
||||
(ptk/reify ::remember-cut
|
||||
ptk/UpdateEvent
|
||||
(update [_ state]
|
||||
(let [objects (dsh/lookup-page-objects state)]
|
||||
(assoc state ::cut {:undo-group undo-group
|
||||
:file-id (:current-file-id state)
|
||||
:ids (set (cfh/clean-loops objects (dsh/lookup-selected state)))})))))
|
||||
|
||||
(defn- cut-undo-group
|
||||
"The undo group of the cut that `pdata` pastes, while that cut is still the
|
||||
last step of the undo history."
|
||||
[state pdata]
|
||||
(let [{:keys [undo-group file-id ids]} (get state ::cut)
|
||||
{:keys [items index]} (:workspace-undo state)
|
||||
last-step (get items (or index (dec (count items))))]
|
||||
(when (and (some? undo-group)
|
||||
(= file-id (:file-id pdata))
|
||||
(= ids (set (:selected pdata)))
|
||||
(= undo-group (:undo-group last-step)))
|
||||
undo-group)))
|
||||
|
||||
(declare ^:private paste-transit-shapes)
|
||||
(declare ^:private paste-transit-props)
|
||||
(declare ^:private paste-html-text)
|
||||
@ -954,7 +979,11 @@
|
||||
drop-cell (when (ctl/grid-layout? all-objects parent-id)
|
||||
(gslg/get-drop-cell frame-id all-objects position))
|
||||
|
||||
;; Pasting what was just cut joins its undo step
|
||||
cut-group (cut-undo-group state pdata)
|
||||
|
||||
changes (-> (pcb/empty-changes it)
|
||||
(cond-> (some? cut-group) (pcb/set-undo-group cut-group))
|
||||
(cld/generate-duplicate-changes all-objects page selected delta
|
||||
libraries ldata file-id {:variant-props variant-props})
|
||||
(pcb/amend-changes (partial process-rchange media-idx))
|
||||
@ -1022,7 +1051,8 @@
|
||||
:type (get shape :type)
|
||||
:parent-type parent-type})))))))
|
||||
|
||||
(rx/of (dwu/start-undo-transaction undo-id)
|
||||
(rx/of #(dissoc % ::cut)
|
||||
(dwu/start-undo-transaction undo-id)
|
||||
(dch/commit-changes changes)
|
||||
(dws/select-shapes selected)
|
||||
(ptk/data-event :layout/update {:ids [frame-id]})
|
||||
|
||||
@ -118,9 +118,7 @@
|
||||
:command (ds/c-mod "x")
|
||||
:subsections [:edit]
|
||||
:section [:workspace]
|
||||
:fn #(emit-when-no-readonly
|
||||
(dw/copy-selected)
|
||||
(dw/delete-selected))}
|
||||
:fn #(emit-when-no-readonly (dw/cut-selected))}
|
||||
|
||||
:paste {:tooltip (ds/meta "V")
|
||||
:label (fn [] (tr "shortcuts.paste"))
|
||||
|
||||
@ -167,8 +167,7 @@
|
||||
do-copy-link #(st/emit! (dw/copy-link-to-clipboard))
|
||||
do-copy-id #(st/emit! (dw/copy-id-to-clipboard (-> shapes first :id str)))
|
||||
|
||||
do-cut #(st/emit! (dw/copy-selected)
|
||||
(dw/delete-selected))
|
||||
do-cut #(st/emit! (dw/cut-selected))
|
||||
do-paste #(st/emit! (dw/paste-from-clipboard))
|
||||
do-duplicate #(st/emit! (dwv/duplicate-or-add-variant))
|
||||
|
||||
|
||||
154
frontend/test/frontend_tests/logic/cut_paste_undo_test.cljs
Normal file
154
frontend/test/frontend_tests/logic/cut_paste_undo_test.cljs
Normal file
@ -0,0 +1,154 @@
|
||||
;; 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 SUBSIDIARY SL
|
||||
|
||||
(ns frontend-tests.logic.cut-paste-undo-test
|
||||
"Undoing shapes moved with cut and paste."
|
||||
(:require
|
||||
[app.common.geom.point :as gpt]
|
||||
[app.common.test-helpers.compositions :as ctho]
|
||||
[app.common.test-helpers.files :as cthf]
|
||||
[app.common.test-helpers.ids-map :as cthi]
|
||||
[app.main.data.changes :as dch]
|
||||
[app.main.data.workspace :as dw]
|
||||
[app.main.data.workspace.clipboard :as dwcp]
|
||||
[app.main.data.workspace.selection :as dws]
|
||||
[app.main.data.workspace.undo :as dwu]
|
||||
[app.main.streams :as ms]
|
||||
[beicon.v2.core :as rx]
|
||||
[cljs.test :as t :include-macros true]
|
||||
[frontend-tests.helpers.async :as async]
|
||||
[frontend-tests.helpers.pages :as thp]
|
||||
[frontend-tests.helpers.state :as ths]
|
||||
[potok.v2.core :as ptk]))
|
||||
|
||||
(defonce ^:private copy-selected dwcp/copy-selected)
|
||||
|
||||
(t/use-fixtures :each
|
||||
{:before (fn []
|
||||
(thp/reset-idmap!)
|
||||
(cthi/reset-idmap!)
|
||||
(rx/push! ms/mouse-position (gpt/point 300 300))
|
||||
;; Writing the system clipboard needs a browser
|
||||
(set! dwcp/copy-selected #(ptk/data-event ::copied)))
|
||||
:after (fn []
|
||||
(rx/push! ms/mouse-position nil)
|
||||
(set! dwcp/copy-selected copy-selected))})
|
||||
|
||||
(defn- setup-file
|
||||
"A file whose first page holds :rect1 and whose second page is empty."
|
||||
[]
|
||||
(-> (cthf/sample-file :file1 :page-label :page1)
|
||||
(cthf/add-sample-page :page2)
|
||||
(vary-meta assoc :current-page-id (cthi/id :page1))
|
||||
(ctho/add-rect :rect1 :x 100 :y 200 :width 50 :height 50)))
|
||||
|
||||
(defn- copied-rect
|
||||
"The clipboard data of copying :rect1."
|
||||
[file]
|
||||
(let [page (cthf/current-page file)]
|
||||
(thp/simulate-copy-shape #{(cthi/id :rect1)} (:objects page) {(:id file) file} page file #{} 67)))
|
||||
|
||||
(defn- page-rects
|
||||
[state page-label]
|
||||
(let [file (ths/get-file-from-state state)]
|
||||
(->> (get-in file [:data :pages-index (cthi/id page-label) :objects])
|
||||
(vals)
|
||||
(filter #(= :rect (:type %))))))
|
||||
|
||||
(defn- undo-steps
|
||||
[store]
|
||||
(count (get-in @store [:workspace-undo :items])))
|
||||
|
||||
(defn- ^:async step!
|
||||
"Emits `event` and waits until it adds an undo step."
|
||||
[store event]
|
||||
(let [before (undo-steps store)]
|
||||
(ptk/emit! store event)
|
||||
(await (async/wait-for #(< before (undo-steps store)) "the undo step of the event"))))
|
||||
|
||||
(defn- ^:async paste!
|
||||
"Pastes `pdata` on the second page and waits for the pasted shape. A paste
|
||||
that joins its cut adds no undo step of its own."
|
||||
[store pdata]
|
||||
(let [before (count (page-rects @store :page2))]
|
||||
(ptk/emit! store (dw/paste-shapes pdata))
|
||||
(await (async/wait-for #(< before (count (page-rects @store :page2))) "the pasted shape"))
|
||||
(await (async/settle))))
|
||||
|
||||
(defn- ^:async with-store
|
||||
"Runs `f` with a store of `file` whose edit watchers record the undo
|
||||
history, then disposes it."
|
||||
[file f]
|
||||
(let [store (ths/setup-store file)]
|
||||
(ptk/emit! store (dw/initialize-edit-watchers))
|
||||
(try
|
||||
(await (f store))
|
||||
(finally
|
||||
(ptk/emit! store (dw/finalize-edit-watchers))
|
||||
(rx/dispose! store)))))
|
||||
|
||||
(defn- ^:async cut-to-page2!
|
||||
"Cuts :rect1 on the first page and moves to the second one."
|
||||
[store]
|
||||
(ptk/emit! store (dws/select-shape (cthi/id :rect1)))
|
||||
(await (step! store (dw/cut-selected)))
|
||||
(ptk/emit! store #(assoc % :current-page-id (cthi/id :page2))))
|
||||
|
||||
(defn- ^:async undo!
|
||||
[store]
|
||||
(ptk/emit! store dwu/undo)
|
||||
(await (async/settle)))
|
||||
|
||||
;; Scenario: a shape is cut on one page and pasted on another. Proves: one
|
||||
;; undo puts it back where it was, with no pasted copy left behind.
|
||||
(t/deftest ^:async one-undo-reverts-a-cut-and-its-paste
|
||||
(let [file (setup-file)
|
||||
pdata (copied-rect file)]
|
||||
(await
|
||||
(with-store
|
||||
file
|
||||
(^:async fn [store]
|
||||
(await (cut-to-page2! store))
|
||||
(await (paste! store pdata))
|
||||
(await (undo! store))
|
||||
(t/is (empty? (page-rects @store :page2)) "the pasted copy is gone")
|
||||
(t/is (= [(cthi/id :rect1)] (map :id (page-rects @store :page1)))
|
||||
"the cut shape is back on its page")
|
||||
(t/is (= 100 (:x (first (page-rects @store :page1))))))))))
|
||||
|
||||
;; Scenario: the cut shapes are pasted twice. Proves: only the first paste
|
||||
;; joins the cut, so one undo reverts the second paste alone.
|
||||
(t/deftest ^:async a-second-paste-of-a-cut-undoes-on-its-own
|
||||
(let [file (setup-file)
|
||||
pdata (copied-rect file)]
|
||||
(await
|
||||
(with-store
|
||||
file
|
||||
(^:async fn [store]
|
||||
(await (cut-to-page2! store))
|
||||
(await (paste! store pdata))
|
||||
(await (paste! store pdata))
|
||||
(await (undo! store))
|
||||
(t/is (= 1 (count (page-rects @store :page2))) "the first paste stays")
|
||||
(t/is (empty? (page-rects @store :page1)) "the cut stays"))))))
|
||||
|
||||
;; Scenario: something else is done between the cut and the paste. Proves:
|
||||
;; the paste is then its own undo step.
|
||||
(t/deftest ^:async a-paste-after-another-change-undoes-on-its-own
|
||||
(let [file (setup-file)
|
||||
pdata (copied-rect file)]
|
||||
(await
|
||||
(with-store
|
||||
file
|
||||
(^:async fn [store]
|
||||
(await (cut-to-page2! store))
|
||||
(await (step! store (dch/commit-changes
|
||||
{:redo-changes [{:type :mod-page :id (cthi/id :page1) :name "Renamed"}]
|
||||
:undo-changes [{:type :mod-page :id (cthi/id :page1) :name "Page 1"}]})))
|
||||
(await (paste! store pdata))
|
||||
(await (undo! store))
|
||||
(t/is (empty? (page-rects @store :page2)) "the paste is undone")
|
||||
(t/is (empty? (page-rects @store :page1)) "the cut stays"))))))
|
||||
@ -93,6 +93,7 @@
|
||||
[frontend-tests.logic.components-and-tokens]
|
||||
[frontend-tests.logic.copy-paste-typography-test]
|
||||
[frontend-tests.logic.copying-and-duplicating-test]
|
||||
[frontend-tests.logic.cut-paste-undo-test]
|
||||
[frontend-tests.logic.drag-on-save-failure-test]
|
||||
[frontend-tests.logic.frame-guides-test]
|
||||
[frontend-tests.logic.groups-test]
|
||||
@ -353,6 +354,7 @@
|
||||
'frontend-tests.logic.components-and-tokens
|
||||
'frontend-tests.logic.copy-paste-typography-test
|
||||
'frontend-tests.logic.copying-and-duplicating-test
|
||||
'frontend-tests.logic.cut-paste-undo-test
|
||||
'frontend-tests.logic.drag-on-save-failure-test
|
||||
'frontend-tests.logic.frame-guides-test
|
||||
'frontend-tests.logic.groups-test
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user