From e8980fbbfeddfb022d5877c4ea2525928f60f223 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Wed, 23 Mar 2022 14:20:54 +0100 Subject: [PATCH] :bug: Fix problem with copy/paste in Safari --- CHANGES.md | 1 + frontend/src/app/main/data/workspace.cljs | 40 ++++++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 891612f90..0af1b2b89 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -59,6 +59,7 @@ - Fix inconsistency with radius in SVG an CSS [#1587](https://github.com/penpot/penpot/issues/1587) - Fix clickable area in layers [#1680](https://github.com/penpot/penpot/issues/1680) - Fix problems with trackpad zoom and scroll in MacOS [#1161](https://github.com/penpot/penpot/issues/1161) +- Fix problem with copy/paste in Safari [#1209](https://github.com/penpot/penpot/issues/1209) ### :arrow_up: Deps updates ### :heart: Community contributions by (Thank you!) diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 214e274f5..ba6d274d3 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -1198,13 +1198,10 @@ [] (letfn [;; Sort objects so they have the same relative ordering ;; when pasted later. - (sort-selected [state data] - (let [selected (:selected data) - page-id (:current-page-id state) - objects (get-in state [:workspace-data - :pages-index - page-id - :objects])] + (sort-selected-async [state data] + (let [selected (wsh/lookup-selected state) + objects (wsh/lookup-page-objects state) + page-id (:current-page-id state)] (->> (uw/ask! {:cmd :selection/query-z-index :page-id page-id :objects objects @@ -1216,6 +1213,24 @@ (map first) (into (d/ordered-set))))))))) + ;; We cannot call to a remote procedure in Safari (for the copy) so we need + ;; to calculate it here instead of on the worker + (sort-selected-sync [state data] + (let [selected (wsh/lookup-selected state) + objects (wsh/lookup-page-objects state) + z-index (cp/calculate-z-index objects) + z-values (->> selected + (map #(vector % + (+ (get z-index %) + (get z-index (get-in objects [% :frame-id])))))) + selected + (->> z-values + (sort-by second) + (map first) + (into (d/ordered-set)))] + + (assoc data :selected selected))) + ;; Retrieve all ids of selected shapes with corresponding ;; children; this is needed because each shape should be ;; processed one by one because of async events (data url @@ -1277,11 +1292,18 @@ :file-id (:current-file-id state) :selected selected :objects {} - :images #{}}] + :images #{}} + + sort-results + (fn [obs] + ;; Safari doesn't allow asynchronous sorting on the copy + (if (cfg/check-browser? :safari) + (rx/map (partial sort-selected-sync state) obs) + (rx/mapcat (partial sort-selected-async state) obs)))] (->> (rx/from (seq (vals pdata))) (rx/merge-map (partial prepare-object objects selected)) (rx/reduce collect-data initial) - (rx/mapcat (partial sort-selected state)) + (sort-results) (rx/map t/encode-str) (rx/map wapi/write-to-clipboard) (rx/catch on-copy-error)