0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-16 01:31:22 -05:00

Detach colors when deleted in the file library

This commit is contained in:
Andrés Moya 2021-06-29 17:41:12 +02:00 committed by Alonso Torres
parent ea8bc687c0
commit dac7a6497f
2 changed files with 65 additions and 33 deletions

View file

@ -23,13 +23,13 @@
(defonce empty-changes [[] []])
(defonce color-sync-attrs
[[:fill-color-ref-id :color :fill-color]
[:fill-color-ref-id :gradient :fill-color-gradient]
[:fill-color-ref-id :opacity :fill-opacity]
[[:fill-color-ref-id :fill-color-ref-file :color :fill-color]
[:fill-color-ref-id :fill-color-ref-file :gradient :fill-color-gradient]
[:fill-color-ref-id :fill-color-ref-file :opacity :fill-opacity]
[:stroke-color-ref-id :color :stroke-color]
[:stroke-color-ref-id :gradient :stroke-color-gradient]
[:stroke-color-ref-id :opacity :stroke-opacity]])
[:stroke-color-ref-id :stroke-color-ref-file :color :stroke-color]
[:stroke-color-ref-id :stroke-color-ref-file :gradient :stroke-color-gradient]
[:stroke-color-ref-id :stroke-color-ref-file :opacity :stroke-opacity]])
(declare generate-sync-container)
(declare generate-sync-shape)
@ -368,7 +368,7 @@
attr-ref-file (keyword (str attr "-ref-file"))]
(and (get shape attr-ref-id)
(= library-id (get shape attr-ref-file))))
(map #(nth % 2) color-sync-attrs))))
(map #(nth % 3) color-sync-attrs))))
:typographies
(fn [shape]
@ -430,12 +430,14 @@
:fill-color (:color color)
:fill-opacity (:opacity color)
:fill-color-gradient (:gradient color))
node))]
(assoc node
:fill-color-ref-id nil
:fill-color-ref-file nil)))]
(generate-sync-text-shape shape container update-node))
(loop [attrs (seq color-sync-attrs)
roperations []
uoperations []]
(let [[attr-ref-id color-attr attr] (first attrs)]
(let [[attr-ref-id attr-ref-file color-attr attr] (first attrs)]
(if (nil? attr)
(if (empty? roperations)
empty-changes
@ -455,17 +457,37 @@
roperations
uoperations)
(let [color (get colors (get shape attr-ref-id))
roperation {:type :set
:attr attr
:val (color-attr color)
:ignore-touched true}
uoperation {:type :set
:attr attr
:val (get shape attr)
:ignore-touched true}]
roperations' (if color
[{:type :set
:attr attr
:val (color-attr color)
:ignore-touched true}]
;; If the referenced color does no longer exist in the library,
;; we must unlink the color in the shape
[{:type :set
:attr attr-ref-id
:val nil
:ignore-touched true}
{:type :set
:attr attr-ref-file
:val nil
:ignore-touched true}])
uoperations' (if color
[{:type :set
:attr attr
:val (get shape attr)
:ignore-touched true}]
[{:type :set
:attr attr-ref-id
:val (get shape attr-ref-id)
:ignore-touched true}
{:type :set
:attr attr-ref-file
:val (get shape attr-ref-file)
:ignore-touched true}])]
(recur (next attrs)
(conj roperations roperation)
(conj uoperations uoperation))))))))))
(concat roperations roperations')
(concat uoperations uoperations'))))))))))
(defmethod generate-sync-shape :typographies
[_ library-id state container shape]

View file

@ -369,8 +369,10 @@
(fn []
(if (or multi-components? multi-assets?)
(on-assets-delete)
(st/emit! (dwl/delete-component {:id (:component-id @state)})))
(st/emit! (dwl/sync-file file-id file-id))))
(st/emit! (dwu/start-undo-transaction)
(dwl/delete-component {:id (:component-id @state)})
(dwl/sync-file file-id file-id)
(dwu/commit-undo-transaction)))))
on-rename
(mf/use-callback
@ -801,11 +803,14 @@
delete-color
(mf/use-callback
(mf/deps @state multi-colors? multi-assets?)
(mf/deps @state multi-colors? multi-assets? file-id)
(fn []
(if (or multi-colors? multi-assets?)
(on-assets-delete)
(st/emit! (dwl/delete-color color)))))
(st/emit! (dwu/start-undo-transaction)
(dwl/delete-color color)
(dwl/sync-file file-id file-id)
(dwu/commit-undo-transaction)))))
rename-color-clicked
(fn [event]
@ -1489,16 +1494,21 @@
(mf/use-callback
(mf/deps @selected-assets)
(fn []
(st/emit! (dwu/start-undo-transaction))
(apply st/emit! (map #(dwl/delete-component {:id %})
(:components @selected-assets)))
(apply st/emit! (map #(dwl/delete-media {:id %})
(:graphics @selected-assets)))
(apply st/emit! (map #(dwl/delete-color {:id %})
(:colors @selected-assets)))
(apply st/emit! (map #(dwl/delete-typography %)
(:typographies @selected-assets)))
(st/emit! (dwu/commit-undo-transaction))))]
(let [selected-assets @selected-assets]
(st/emit! (dwu/start-undo-transaction))
(apply st/emit! (map #(dwl/delete-component {:id %})
(:components selected-assets)))
(apply st/emit! (map #(dwl/delete-media {:id %})
(:graphics selected-assets)))
(apply st/emit! (map #(dwl/delete-color {:id %})
(:colors selected-assets)))
(apply st/emit! (map #(dwl/delete-typography %)
(:typographies selected-assets)))
(when (or (d/not-empty? (:components selected-assets))
(d/not-empty? (:colors selected-assets))
(d/not-empty? (:typographies selected-assets)))
(st/emit! (dwl/sync-file (:id file) (:id file))))
(st/emit! (dwu/commit-undo-transaction)))))]
[:div.tool-window {:on-context-menu #(dom/prevent-default %)
:on-click unselect-all}