Repository: penpot/penpot
File: frontend/src/app/main/data/workspace/clipboard.cljs (ClojureScript)

Code before:
                                (map :id)
                                (pcb/resize-parents changes))

              orig-shapes  (map (d/getf all-objects) selected)

              selected     (into (d/ordered-set)
                                 (comp
                                  (filter add-obj?)
                                  (filter #(contains? selected (:old-id %)))
                                  (map :obj)
                                  (map :id))
                                 (:redo-changes changes))

              changes      (cond-> changes
                             (some? drop-cell)
                             (pcb/update-shapes [parent-id]
                                                #(ctl/add-children-to-cell % selected all-objects drop-cell)))

              undo-id      (js/Symbol)]

          (rx/concat
           (->> (rx/from orig-shapes)
                (rx/map (fn [shape]
                          (let [parent-type   (cfh/get-shape-type all-objects (:parent-id shape))
                                external-lib? (not= file-id (:component-file shape))
                                component     (get-in libraries [(:component-file shape) :data :components (:component-id shape)])
                                origin        "workspace:paste"]

                            ;; NOTE: we don't emit the create-shape event all the time for
                            ;; avoid send a lot of events (that are not necessary); this
                            ;; decision is made explicitly by the responsible team.
                            (if (ctc/instance-head? shape)
                              (ev/event {::ev/name "use-library-component"
                                         ::ev/origin origin
                                         :is-external-library external-lib?
                                         :type (get shape :type)
                                         :parent-type parent-type
                                         :is-variant (ctc/is-variant? component)})
                              (if (cfh/has-layout? objects (:parent-id shape))
                                (ev/event {::ev/name "layout-add-element"
                                           ::ev/origin origin
                                           :type (get shape :type)
                                           :parent-type parent-type})
                                (ev/event {::ev/name "create-shape"
                                           ::ev/origin origin
                                           :type (get shape :type)
                                           :parent-type parent-type})))))))

           (rx/of (dwu/start-undo-transaction undo-id)
                  (dch/commit-changes changes)
                  (dws/select-shapes selected)

Review comment (refactor):
This is something that should be done via a helper function (i think in `common.types.file` we have something). This way, if later we want to change the structure of components in a file it's a nightmare to find all places that we need to update.

Code after:
                                (pcb/resize-parents changes))

              orig-shapes  (map (d/getf all-objects) selected)

              selected     (into (d/ordered-set)
                                 (comp
                                  (filter add-obj?)
                                  (filter #(contains? selected (:old-id %)))
                                  (map :obj)
                                  (map :id))
                                 (:redo-changes changes))

              changes      (cond-> changes
                             (some? drop-cell)
                             (pcb/update-shapes [parent-id]
                                                #(ctl/add-children-to-cell % selected all-objects drop-cell)))

              undo-id      (js/Symbol)]

          (rx/concat
           (->> (rx/from orig-shapes)
                (rx/map (fn [shape]
                          (let [parent-type   (cfh/get-shape-type all-objects (:parent-id shape))
                                external-lib? (not= file-id (:component-file shape))
                                component     (ctn/get-component-from-shape shape libraries)
                                origin        "workspace:paste"]

                            ;; NOTE: we don't emit the create-shape event all the time for
                            ;; avoid send a lot of events (that are not necessary); this
                            ;; decision is made explicitly by the responsible team.
                            (if (ctc/instance-head? shape)
                              (ev/event {::ev/name "use-library-component"
                                         ::ev/origin origin
                                         :is-external-library external-lib?
                                         :type (get shape :type)
                                         :parent-type parent-type
                                         :is-variant (ctc/is-variant? component)})
                              (if (cfh/has-layout? objects (:parent-id shape))
                                (ev/event {::ev/name "layout-add-element"
                                           ::ev/origin origin
                                           :type (get shape :type)
                                           :parent-type parent-type})
                                (ev/event {::ev/name "create-shape"
                                           ::ev/origin origin
                                           :type (get shape :type)
                                           :parent-type parent-type})))))))

           (rx/of (dwu/start-undo-transaction undo-id)
                  (dch/commit-changes changes)
                  (dws/select-shapes selected)
                  (ptk/data-event :layout/update {:ids [frame-id]})