mirror of
https://github.com/penpot/penpot.git
synced 2025-02-08 08:09:14 -05:00
Merge pull request #4477 from penpot/comp-refactor-lib-helpers
♻️ Move and merge libraries_helpers
This commit is contained in:
commit
c85f76300a
7 changed files with 1796 additions and 1698 deletions
103
common/src/app/common/files/libraries_common_helpers.cljc
Normal file
103
common/src/app/common/files/libraries_common_helpers.cljc
Normal file
|
@ -0,0 +1,103 @@
|
|||
;; 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 INC
|
||||
|
||||
(ns app.common.files.libraries-common-helpers
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.files.changes-builder :as pcb]
|
||||
[app.common.files.helpers :as cfh]
|
||||
[app.common.types.component :as ctk]
|
||||
[app.common.types.container :as ctn]
|
||||
[app.common.uuid :as uuid]))
|
||||
|
||||
(defn generate-add-component-changes
|
||||
[changes root objects file-id page-id components-v2]
|
||||
(let [name (:name root)
|
||||
[path name] (cfh/parse-path-name name)
|
||||
|
||||
[root-shape new-shapes updated-shapes]
|
||||
(if-not components-v2
|
||||
(ctn/make-component-shape root objects file-id components-v2)
|
||||
(ctn/convert-shape-in-component root objects file-id))
|
||||
|
||||
changes (-> changes
|
||||
(pcb/add-component (:id root-shape)
|
||||
path
|
||||
name
|
||||
new-shapes
|
||||
updated-shapes
|
||||
(:id root)
|
||||
page-id))]
|
||||
[root-shape changes]))
|
||||
|
||||
(defn generate-add-component
|
||||
"If there is exactly one id, and it's a frame (or a group in v1), and not already a component,
|
||||
use it as root. Otherwise, create a frame (v2) or group (v1) that contains all ids. Then, make a
|
||||
component with it, and link all shapes to their corresponding one in the component."
|
||||
[it shapes objects page-id file-id components-v2 prepare-create-group prepare-create-board]
|
||||
|
||||
(let [changes (pcb/empty-changes it page-id)
|
||||
shapes-count (count shapes)
|
||||
first-shape (first shapes)
|
||||
|
||||
from-singe-frame?
|
||||
(and (= 1 shapes-count)
|
||||
(cfh/frame-shape? first-shape))
|
||||
|
||||
[root changes old-root-ids]
|
||||
(if (and (= shapes-count 1)
|
||||
(or (and (cfh/group-shape? first-shape)
|
||||
(not components-v2))
|
||||
(cfh/frame-shape? first-shape))
|
||||
(not (ctk/instance-head? first-shape)))
|
||||
[first-shape
|
||||
(-> (pcb/empty-changes it page-id)
|
||||
(pcb/with-objects objects))
|
||||
(:shapes first-shape)]
|
||||
|
||||
(let [root-name (if (= 1 shapes-count)
|
||||
(:name first-shape)
|
||||
"Component 1")
|
||||
|
||||
shape-ids (into (d/ordered-set) (map :id) shapes)
|
||||
|
||||
[root changes]
|
||||
(if-not components-v2
|
||||
(prepare-create-group it ; These functions needs to be passed as argument
|
||||
objects ; to avoid a circular dependence
|
||||
page-id
|
||||
shapes
|
||||
root-name
|
||||
(not (ctk/instance-head? first-shape)))
|
||||
(prepare-create-board changes
|
||||
(uuid/next)
|
||||
(:parent-id first-shape)
|
||||
objects
|
||||
shape-ids
|
||||
nil
|
||||
root-name
|
||||
true))]
|
||||
|
||||
[root changes shape-ids]))
|
||||
|
||||
changes
|
||||
(cond-> changes
|
||||
(not from-singe-frame?)
|
||||
(pcb/update-shapes
|
||||
(:shapes root)
|
||||
(fn [shape]
|
||||
(assoc shape :constraints-h :scale :constraints-v :scale))))
|
||||
|
||||
objects' (assoc objects (:id root) root)
|
||||
|
||||
[root-shape changes] (generate-add-component-changes changes root objects' file-id page-id components-v2)
|
||||
|
||||
changes (pcb/update-shapes changes
|
||||
old-root-ids
|
||||
#(dissoc % :component-root)
|
||||
[:component-root])]
|
||||
|
||||
[root (:id root-shape) changes]))
|
File diff suppressed because it is too large
Load diff
|
@ -29,7 +29,6 @@
|
|||
[app.main.data.workspace :as-alias dw]
|
||||
[app.main.data.workspace.changes :as dch]
|
||||
[app.main.data.workspace.groups :as dwg]
|
||||
[app.main.data.workspace.libraries-helpers :as dwlh]
|
||||
[app.main.data.workspace.notifications :as-alias dwn]
|
||||
[app.main.data.workspace.selection :as dws]
|
||||
[app.main.data.workspace.shapes :as dwsh]
|
||||
|
@ -54,6 +53,13 @@
|
|||
;; Change this to :info :debug or :trace to debug this module, or :warn to reset to default
|
||||
(log/set-level! :warn)
|
||||
|
||||
|
||||
(defn- pretty-file
|
||||
[file-id state]
|
||||
(if (= file-id (:current-file-id state))
|
||||
"<local>"
|
||||
(str "<" (get-in state [:workspace-libraries file-id :name]) ">")))
|
||||
|
||||
(defn- log-changes
|
||||
[changes file]
|
||||
(let [extract-change
|
||||
|
@ -472,7 +478,7 @@
|
|||
|
||||
[new-component-shape new-component-shapes ; <- null in components-v2
|
||||
new-main-instance-shape new-main-instance-shapes]
|
||||
(dwlh/duplicate-component component new-component-id (:data library))
|
||||
(cflh/duplicate-component component new-component-id (:data library))
|
||||
|
||||
changes (-> (pcb/empty-changes it nil)
|
||||
(pcb/with-page main-instance-page)
|
||||
|
@ -525,7 +531,7 @@
|
|||
current-page (dm/get-in state [:workspace-data :pages-index page-id])
|
||||
objects (wsh/lookup-page-objects state page-id)
|
||||
library-data (wsh/get-file state library-id)
|
||||
{:keys [changes shape]} (dwlh/prepare-restore-component library-data component-id current-page it)
|
||||
{:keys [changes shape]} (cflh/prepare-restore-component library-data component-id current-page it)
|
||||
parent-id (:parent-id shape)
|
||||
objects (cond-> (assoc objects (:id shape) shape)
|
||||
(not (nil? parent-id))
|
||||
|
@ -574,7 +580,7 @@
|
|||
(pcb/with-objects objects))
|
||||
|
||||
[new-shape changes]
|
||||
(dwlh/generate-instantiate-component changes
|
||||
(cflh/generate-instantiate-component changes
|
||||
objects
|
||||
file-id
|
||||
component-id
|
||||
|
@ -606,7 +612,7 @@
|
|||
changes (-> (pcb/empty-changes it)
|
||||
(pcb/with-container container)
|
||||
(pcb/with-objects (:objects container))
|
||||
(dwlh/generate-detach-instance container libraries id))]
|
||||
(cflh/generate-detach-instance container libraries id))]
|
||||
|
||||
(rx/of (dch/commit-changes changes))))))
|
||||
|
||||
|
@ -642,7 +648,7 @@
|
|||
changes (when can-detach?
|
||||
(reduce
|
||||
(fn [changes id]
|
||||
(dwlh/generate-detach-instance changes container libraries id))
|
||||
(cflh/generate-detach-instance changes container libraries id))
|
||||
(-> (pcb/empty-changes it)
|
||||
(pcb/with-container container)
|
||||
(pcb/with-objects objects))
|
||||
|
@ -731,7 +737,7 @@
|
|||
(-> (pcb/empty-changes it)
|
||||
(pcb/with-container container)
|
||||
(pcb/with-objects (:objects container))
|
||||
(dwlh/generate-sync-shape-direct file-full libraries container (:id head) false components-v2))]
|
||||
(cflh/generate-sync-shape-direct file-full libraries container (:id head) false components-v2))]
|
||||
|
||||
(log/debug :msg "SYNC-head finished" :js/rchanges (log-changes
|
||||
(:redo-changes changes)
|
||||
|
@ -767,7 +773,7 @@
|
|||
(-> (pcb/empty-changes it)
|
||||
(pcb/with-container container)
|
||||
(pcb/with-objects (:objects container))
|
||||
(dwlh/generate-sync-shape-direct file-full libraries container id true components-v2))]
|
||||
(cflh/generate-sync-shape-direct file-full libraries container id true components-v2))]
|
||||
|
||||
(log/debug :msg "RESET-COMPONENT finished" :js/rchanges (log-changes
|
||||
(:redo-changes changes)
|
||||
|
@ -823,7 +829,7 @@
|
|||
(-> (pcb/empty-changes it)
|
||||
(pcb/set-undo-group undo-group)
|
||||
(pcb/with-container container)
|
||||
(dwlh/generate-sync-shape-inverse full-file libraries container id components-v2))
|
||||
(cflh/generate-sync-shape-inverse full-file libraries container id components-v2))
|
||||
|
||||
file-id (:component-file shape)
|
||||
file (wsh/get-file state file-id)
|
||||
|
@ -947,7 +953,7 @@
|
|||
inside-comp? (ctn/in-any-component? objects parent)
|
||||
|
||||
[new-shape changes]
|
||||
(dwlh/generate-instantiate-component changes
|
||||
(cflh/generate-instantiate-component changes
|
||||
objects
|
||||
(:id file)
|
||||
id-new-component
|
||||
|
@ -978,7 +984,7 @@
|
|||
;; We need to set the same index as the original shape
|
||||
(pcb/change-parent (:parent-id shape) [new-shape] index {:component-swap true
|
||||
:ignore-touched true})
|
||||
(dwlh/change-touched new-shape
|
||||
(cflh/change-touched new-shape
|
||||
shape
|
||||
(ctn/make-container page :page)
|
||||
{}))]
|
||||
|
@ -1039,7 +1045,7 @@
|
|||
(watch [_ state _]
|
||||
(let [undo-id (js/Symbol)]
|
||||
(log/info :msg "COMPONENT-SWAP"
|
||||
:file (dwlh/pretty-file file-id state)
|
||||
:file (pretty-file file-id state)
|
||||
:id-new-component id-new-component
|
||||
:undo-id undo-id)
|
||||
(rx/concat
|
||||
|
@ -1092,12 +1098,15 @@
|
|||
(watch [it state _]
|
||||
(when (and (some? file-id) (some? library-id)) ; Prevent race conditions while navigating out of the file
|
||||
(log/info :msg "SYNC-FILE"
|
||||
:file (dwlh/pretty-file file-id state)
|
||||
:library (dwlh/pretty-file library-id state)
|
||||
:file (pretty-file file-id state)
|
||||
:library (pretty-file library-id state)
|
||||
:asset-type asset-type
|
||||
:asset-id asset-id
|
||||
:undo-group undo-group)
|
||||
(let [file (wsh/get-file state file-id)
|
||||
libraries (wsh/get-libraries state)
|
||||
current-file-id (:current-file-id state)
|
||||
|
||||
|
||||
sync-components? (or (nil? asset-type) (= asset-type :components))
|
||||
sync-colors? (or (nil? asset-type) (= asset-type :colors))
|
||||
|
@ -1108,22 +1117,22 @@
|
|||
(-> (pcb/empty-changes it)
|
||||
(pcb/set-undo-group undo-group))
|
||||
[(when sync-components?
|
||||
(dwlh/generate-sync-library it file-id :components asset-id library-id state))
|
||||
(cflh/generate-sync-library it file-id :components asset-id library-id libraries current-file-id))
|
||||
(when sync-colors?
|
||||
(dwlh/generate-sync-library it file-id :colors asset-id library-id state))
|
||||
(cflh/generate-sync-library it file-id :colors asset-id library-id libraries current-file-id))
|
||||
(when sync-typographies?
|
||||
(dwlh/generate-sync-library it file-id :typographies asset-id library-id state))])
|
||||
(cflh/generate-sync-library it file-id :typographies asset-id library-id libraries current-file-id))])
|
||||
|
||||
file-changes (reduce
|
||||
pcb/concat-changes
|
||||
(-> (pcb/empty-changes it)
|
||||
(pcb/set-undo-group undo-group))
|
||||
[(when sync-components?
|
||||
(dwlh/generate-sync-file it file-id :components asset-id library-id state))
|
||||
(cflh/generate-sync-file it file-id :components asset-id library-id libraries current-file-id))
|
||||
(when sync-colors?
|
||||
(dwlh/generate-sync-file it file-id :colors asset-id library-id state))
|
||||
(cflh/generate-sync-file it file-id :colors asset-id library-id libraries current-file-id))
|
||||
(when sync-typographies?
|
||||
(dwlh/generate-sync-file it file-id :typographies asset-id library-id state))])
|
||||
(cflh/generate-sync-file it file-id :typographies asset-id library-id libraries current-file-id))])
|
||||
|
||||
changes (pcb/concat-changes library-changes file-changes)
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -28,7 +28,6 @@
|
|||
[app.main.data.modal :as md]
|
||||
[app.main.data.workspace.changes :as dch]
|
||||
[app.main.data.workspace.collapse :as dwc]
|
||||
[app.main.data.workspace.libraries-helpers :as dwlh]
|
||||
[app.main.data.workspace.specialized-panel :as-alias dwsp]
|
||||
[app.main.data.workspace.state-helpers :as wsh]
|
||||
[app.main.data.workspace.undo :as dwu]
|
||||
|
@ -435,7 +434,7 @@
|
|||
(gpt/subtract (-> origin-frame :selrect gpt/point)))
|
||||
|
||||
instantiate-component
|
||||
#(dwlh/generate-instantiate-component changes
|
||||
#(cflh/generate-instantiate-component changes
|
||||
objects
|
||||
file-id
|
||||
(:component-id component-root)
|
||||
|
@ -448,7 +447,7 @@
|
|||
{})
|
||||
|
||||
restore-component
|
||||
#(let [restore (dwlh/prepare-restore-component changes library-data (:component-id component-root) it page delta (:id component-root) parent-id frame-id)]
|
||||
#(let [restore (cflh/prepare-restore-component changes library-data (:component-id component-root) it page delta (:id component-root) parent-id frame-id)]
|
||||
[(:shape restore) (:changes restore)])
|
||||
|
||||
[_shape changes]
|
||||
|
|
|
@ -123,6 +123,15 @@
|
|||
(get state :workspace-data)
|
||||
(dm/get-in state [:workspace-libraries file-id :data])))
|
||||
|
||||
(defn get-file-full
|
||||
"Get the data content of the given file (it may be the current file
|
||||
or one library)."
|
||||
[state file-id]
|
||||
(if (= file-id (:current-file-id state))
|
||||
(-> (get state :workspace-file)
|
||||
(assoc :data (get state :workspace-data)))
|
||||
(dm/get-in state [:workspace-libraries file-id :data])))
|
||||
|
||||
(defn get-libraries
|
||||
"Retrieve all libraries, including the local file."
|
||||
[state]
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
(ns frontend-tests.helpers.pages
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.files.changes :as cp]
|
||||
[app.common.files.changes-builder :as pcb]
|
||||
[app.common.files.helpers :as cfh]
|
||||
|
@ -17,8 +16,6 @@
|
|||
[app.common.uuid :as uuid]
|
||||
[app.main.data.workspace.groups :as dwg]
|
||||
[app.main.data.workspace.layout :as layout]
|
||||
[app.main.data.workspace.libraries-helpers :as dwlh]
|
||||
[app.main.data.workspace.shapes :as dwsh]
|
||||
[app.main.data.workspace.state-helpers :as wsh]))
|
||||
|
||||
;; ---- Helpers to manage pages and objects
|
||||
|
@ -161,7 +158,7 @@
|
|||
(pcb/with-objects objects))
|
||||
|
||||
[new-shape changes]
|
||||
(dwlh/generate-instantiate-component changes
|
||||
(cflh/generate-instantiate-component changes
|
||||
objects
|
||||
file-id
|
||||
component-id
|
||||
|
|
Loading…
Add table
Reference in a new issue