♻️ Move libraries_helpers to common

This commit is contained in:
Pablo Alba 2024-04-19 12:53:23 +02:00
parent e1cd6d04d9
commit 2e18ce9323
4 changed files with 71 additions and 58 deletions

View file

@ -4,7 +4,7 @@
;; ;;
;; Copyright (c) KALEIDOS INC ;; Copyright (c) KALEIDOS INC
(ns app.main.data.workspace.libraries-helpers (ns app.common.files.libraries-helpers
(:require (:require
[app.common.data :as d] [app.common.data :as d]
[app.common.data.macros :as dm] [app.common.data.macros :as dm]
@ -25,7 +25,6 @@
[app.common.types.shape-tree :as ctst] [app.common.types.shape-tree :as ctst]
[app.common.types.shape.layout :as ctl] [app.common.types.shape.layout :as ctl]
[app.common.types.typography :as cty] [app.common.types.typography :as cty]
[app.main.data.workspace.state-helpers :as wsh]
[cljs.spec.alpha :as s] [cljs.spec.alpha :as s]
[clojure.set :as set])) [clojure.set :as set]))
@ -37,7 +36,6 @@
(declare generate-sync-text-shape) (declare generate-sync-text-shape)
(declare uses-assets?) (declare uses-assets?)
(declare get-assets)
(declare generate-sync-shape-direct) (declare generate-sync-shape-direct)
(declare generate-sync-shape-direct-recursive) (declare generate-sync-shape-direct-recursive)
(declare generate-sync-shape-inverse) (declare generate-sync-shape-inverse)
@ -59,10 +57,10 @@
(declare make-change) (declare make-change)
(defn pretty-file (defn pretty-file
[file-id state] [file-id libraries current-file-id]
(if (= file-id (:current-file-id state)) (if (= file-id current-file-id)
"<local>" "<local>"
(str "<" (get-in state [:workspace-libraries file-id :name]) ">"))) (str "<" (get-in libraries [file-id :name]) ">")))
(defn pretty-uuid (defn pretty-uuid
[uuid] [uuid]
@ -304,7 +302,7 @@
If an asset id is given, only shapes linked to this particular asset will If an asset id is given, only shapes linked to this particular asset will
be synchronized." be synchronized."
[it file-id asset-type asset-id library-id state] [it file-id asset-type asset-id library-id libraries current-file-id]
(s/assert #{:colors :components :typographies} asset-type) (s/assert #{:colors :components :typographies} asset-type)
(s/assert (s/nilable ::us/uuid) asset-id) (s/assert (s/nilable ::us/uuid) asset-id)
(s/assert ::us/uuid file-id) (s/assert ::us/uuid file-id)
@ -313,10 +311,10 @@
(log/info :msg "Sync file with library" (log/info :msg "Sync file with library"
:asset-type asset-type :asset-type asset-type
:asset-id asset-id :asset-id asset-id
:file (pretty-file file-id state) :file (pretty-file file-id libraries current-file-id)
:library (pretty-file library-id state)) :library (pretty-file library-id libraries current-file-id))
(let [file (wsh/get-file state file-id) (let [file (get-in libraries [file-id :data])
components-v2 (get-in file [:options :components-v2])] components-v2 (get-in file [:options :components-v2])]
(loop [containers (ctf/object-containers-seq file) (loop [containers (ctf/object-containers-seq file)
changes (pcb/empty-changes it)] changes (pcb/empty-changes it)]
@ -329,9 +327,10 @@
asset-type asset-type
asset-id asset-id
library-id library-id
state
container container
components-v2)))) components-v2
libraries
current-file-id))))
changes)))) changes))))
(defn generate-sync-library (defn generate-sync-library
@ -341,7 +340,7 @@
If an asset id is given, only shapes linked to this particular asset will If an asset id is given, only shapes linked to this particular asset will
be synchronized." be synchronized."
[it file-id asset-type asset-id library-id state] [it file-id asset-type asset-id library-id libraries current-file-id]
(s/assert #{:colors :components :typographies} asset-type) (s/assert #{:colors :components :typographies} asset-type)
(s/assert (s/nilable ::us/uuid) asset-id) (s/assert (s/nilable ::us/uuid) asset-id)
(s/assert ::us/uuid file-id) (s/assert ::us/uuid file-id)
@ -350,10 +349,10 @@
(log/info :msg "Sync local components with library" (log/info :msg "Sync local components with library"
:asset-type asset-type :asset-type asset-type
:asset-id asset-id :asset-id asset-id
:file (pretty-file file-id state) :file (pretty-file file-id libraries current-file-id)
:library (pretty-file library-id state)) :library (pretty-file library-id libraries current-file-id))
(let [file (wsh/get-file state file-id) (let [file (get-in libraries [file-id :data])
components-v2 (get-in file [:options :components-v2])] components-v2 (get-in file [:options :components-v2])]
(loop [local-components (ctkl/components-seq file) (loop [local-components (ctkl/components-seq file)
changes (pcb/empty-changes it)] changes (pcb/empty-changes it)]
@ -365,15 +364,16 @@
asset-type asset-type
asset-id asset-id
library-id library-id
state
(cfh/make-container local-component :component) (cfh/make-container local-component :component)
components-v2))) components-v2
libraries
current-file-id)))
changes)))) changes))))
(defn- generate-sync-container (defn- generate-sync-container
"Generate changes to synchronize all shapes in a particular container (a page "Generate changes to synchronize all shapes in a particular container (a page
or a component) that use assets of the given type in the given library." or a component) that use assets of the given type in the given library."
[it asset-type asset-id library-id state container components-v2] [it asset-type asset-id library-id container components-v2 libraries current-file-id]
(if (cfh/page? container) (if (cfh/page? container)
(log/debug :msg "Sync page in local file" :page-id (:id container)) (log/debug :msg "Sync page in local file" :page-id (:id container))
@ -390,10 +390,11 @@
(generate-sync-shape asset-type (generate-sync-shape asset-type
changes changes
library-id library-id
state
container container
shape shape
components-v2)) components-v2
libraries
current-file-id))
changes)))) changes))))
(defmulti uses-assets? (defmulti uses-assets?
@ -421,33 +422,32 @@
(defmulti generate-sync-shape (defmulti generate-sync-shape
"Generate changes to synchronize one shape from all assets of the given type "Generate changes to synchronize one shape from all assets of the given type
that is using, in the given library." that is using, in the given library."
(fn [asset-type _changes _library-id _state _container _shape _components-v2] asset-type)) (fn [asset-type _changes _library-id _container _shape _components-v2 _libraries _current-file-id] asset-type))
(defmethod generate-sync-shape :components (defmethod generate-sync-shape :components
[_ changes _library-id state container shape components-v2] [_ changes _library-id container shape components-v2 libraries current-file-id]
(let [shape-id (:id shape) (let [shape-id (:id shape)
file (wsh/get-local-file-full state) file (get current-file-id libraries)]
libraries (wsh/get-libraries state)]
(generate-sync-shape-direct changes file libraries container shape-id false components-v2))) (generate-sync-shape-direct changes file libraries container shape-id false components-v2)))
(defmethod generate-sync-shape :colors (defmethod generate-sync-shape :colors
[_ changes library-id state _ shape _] [_ changes library-id _ shape _ libraries _]
(log/debug :msg "Sync colors of shape" :shape (:name shape)) (log/debug :msg "Sync colors of shape" :shape (:name shape))
;; Synchronize a shape that uses some colors of the library. The value of the ;; Synchronize a shape that uses some colors of the library. The value of the
;; color in the library is copied to the shape. ;; color in the library is copied to the shape.
(let [library-colors (get-assets library-id :colors state)] (let [library-colors (get-in libraries [library-id :data :colors])]
(pcb/update-shapes changes (pcb/update-shapes changes
[(:id shape)] [(:id shape)]
#(ctc/sync-shape-colors % library-id library-colors)))) #(ctc/sync-shape-colors % library-id library-colors))))
(defmethod generate-sync-shape :typographies (defmethod generate-sync-shape :typographies
[_ changes library-id state container shape _] [_ changes library-id container shape _ libraries _]
(log/debug :msg "Sync typographies of shape" :shape (:name shape)) (log/debug :msg "Sync typographies of shape" :shape (:name shape))
;; Synchronize a shape that uses some typographies of the library. The attributes ;; Synchronize a shape that uses some typographies of the library. The attributes
;; of the typography are copied to the shape." ;; of the typography are copied to the shape."
(let [typographies (get-assets library-id :typographies state) (let [typographies (get-in libraries [library-id :data :typographies])
update-node (fn [node] update-node (fn [node]
(if-let [typography (get typographies (:typography-ref-id node))] (if-let [typography (get typographies (:typography-ref-id node))]
(merge node (dissoc typography :name :id)) (merge node (dissoc typography :name :id))
@ -455,12 +455,6 @@
:typography-ref-file)))] :typography-ref-file)))]
(generate-sync-text-shape changes shape container update-node))) (generate-sync-text-shape changes shape container update-node)))
(defn- get-assets
[library-id asset-type state]
(if (= library-id (:current-file-id state))
(get-in state [:workspace-data asset-type])
(get-in state [:workspace-libraries library-id :data asset-type])))
(defn- generate-sync-text-shape (defn- generate-sync-text-shape
[changes shape container update-node] [changes shape container update-node]
(let [old-content (:content shape) (let [old-content (:content shape)

View file

@ -12,6 +12,7 @@
[app.common.files.changes-builder :as pcb] [app.common.files.changes-builder :as pcb]
[app.common.files.helpers :as cfh] [app.common.files.helpers :as cfh]
[app.common.files.libraries-common-helpers :as cflch] [app.common.files.libraries-common-helpers :as cflch]
[app.common.files.libraries-helpers :as cflh]
[app.common.files.shapes-helpers :as cfsh] [app.common.files.shapes-helpers :as cfsh]
[app.common.geom.point :as gpt] [app.common.geom.point :as gpt]
[app.common.logging :as log] [app.common.logging :as log]
@ -29,7 +30,6 @@
[app.main.data.workspace :as-alias dw] [app.main.data.workspace :as-alias dw]
[app.main.data.workspace.changes :as dch] [app.main.data.workspace.changes :as dch]
[app.main.data.workspace.groups :as dwg] [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.notifications :as-alias dwn]
[app.main.data.workspace.selection :as dws] [app.main.data.workspace.selection :as dws]
[app.main.data.workspace.shapes :as dwsh] [app.main.data.workspace.shapes :as dwsh]
@ -54,6 +54,13 @@
;; Change this to :info :debug or :trace to debug this module, or :warn to reset to default ;; Change this to :info :debug or :trace to debug this module, or :warn to reset to default
(log/set-level! :warn) (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 (defn- log-changes
[changes file] [changes file]
(let [extract-change (let [extract-change
@ -472,7 +479,7 @@
[new-component-shape new-component-shapes ; <- null in components-v2 [new-component-shape new-component-shapes ; <- null in components-v2
new-main-instance-shape new-main-instance-shapes] 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) changes (-> (pcb/empty-changes it nil)
(pcb/with-page main-instance-page) (pcb/with-page main-instance-page)
@ -525,7 +532,7 @@
current-page (dm/get-in state [:workspace-data :pages-index page-id]) current-page (dm/get-in state [:workspace-data :pages-index page-id])
objects (wsh/lookup-page-objects state page-id) objects (wsh/lookup-page-objects state page-id)
library-data (wsh/get-file state library-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) parent-id (:parent-id shape)
objects (cond-> (assoc objects (:id shape) shape) objects (cond-> (assoc objects (:id shape) shape)
(not (nil? parent-id)) (not (nil? parent-id))
@ -574,7 +581,7 @@
(pcb/with-objects objects)) (pcb/with-objects objects))
[new-shape changes] [new-shape changes]
(dwlh/generate-instantiate-component changes (cflh/generate-instantiate-component changes
objects objects
file-id file-id
component-id component-id
@ -606,7 +613,7 @@
changes (-> (pcb/empty-changes it) changes (-> (pcb/empty-changes it)
(pcb/with-container container) (pcb/with-container container)
(pcb/with-objects (:objects 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)))))) (rx/of (dch/commit-changes changes))))))
@ -642,7 +649,7 @@
changes (when can-detach? changes (when can-detach?
(reduce (reduce
(fn [changes id] (fn [changes id]
(dwlh/generate-detach-instance changes container libraries id)) (cflh/generate-detach-instance changes container libraries id))
(-> (pcb/empty-changes it) (-> (pcb/empty-changes it)
(pcb/with-container container) (pcb/with-container container)
(pcb/with-objects objects)) (pcb/with-objects objects))
@ -731,7 +738,7 @@
(-> (pcb/empty-changes it) (-> (pcb/empty-changes it)
(pcb/with-container container) (pcb/with-container container)
(pcb/with-objects (:objects 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 (log/debug :msg "SYNC-head finished" :js/rchanges (log-changes
(:redo-changes changes) (:redo-changes changes)
@ -767,7 +774,7 @@
(-> (pcb/empty-changes it) (-> (pcb/empty-changes it)
(pcb/with-container container) (pcb/with-container container)
(pcb/with-objects (:objects 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 (log/debug :msg "RESET-COMPONENT finished" :js/rchanges (log-changes
(:redo-changes changes) (:redo-changes changes)
@ -823,7 +830,7 @@
(-> (pcb/empty-changes it) (-> (pcb/empty-changes it)
(pcb/set-undo-group undo-group) (pcb/set-undo-group undo-group)
(pcb/with-container container) (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-id (:component-file shape)
file (wsh/get-file state file-id) file (wsh/get-file state file-id)
@ -947,7 +954,7 @@
inside-comp? (ctn/in-any-component? objects parent) inside-comp? (ctn/in-any-component? objects parent)
[new-shape changes] [new-shape changes]
(dwlh/generate-instantiate-component changes (cflh/generate-instantiate-component changes
objects objects
(:id file) (:id file)
id-new-component id-new-component
@ -978,7 +985,7 @@
;; We need to set the same index as the original shape ;; We need to set the same index as the original shape
(pcb/change-parent (:parent-id shape) [new-shape] index {:component-swap true (pcb/change-parent (:parent-id shape) [new-shape] index {:component-swap true
:ignore-touched true}) :ignore-touched true})
(dwlh/change-touched new-shape (cflh/change-touched new-shape
shape shape
(ctn/make-container page :page) (ctn/make-container page :page)
{}))] {}))]
@ -1039,7 +1046,7 @@
(watch [_ state _] (watch [_ state _]
(let [undo-id (js/Symbol)] (let [undo-id (js/Symbol)]
(log/info :msg "COMPONENT-SWAP" (log/info :msg "COMPONENT-SWAP"
:file (dwlh/pretty-file file-id state) :file (pretty-file file-id state)
:id-new-component id-new-component :id-new-component id-new-component
:undo-id undo-id) :undo-id undo-id)
(rx/concat (rx/concat
@ -1092,12 +1099,15 @@
(watch [it state _] (watch [it state _]
(when (and (some? file-id) (some? library-id)) ; Prevent race conditions while navigating out of the file (when (and (some? file-id) (some? library-id)) ; Prevent race conditions while navigating out of the file
(log/info :msg "SYNC-FILE" (log/info :msg "SYNC-FILE"
:file (dwlh/pretty-file file-id state) :file (pretty-file file-id state)
:library (dwlh/pretty-file library-id state) :library (pretty-file library-id state)
:asset-type asset-type :asset-type asset-type
:asset-id asset-id :asset-id asset-id
:undo-group undo-group) :undo-group undo-group)
(let [file (wsh/get-file state file-id) (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-components? (or (nil? asset-type) (= asset-type :components))
sync-colors? (or (nil? asset-type) (= asset-type :colors)) sync-colors? (or (nil? asset-type) (= asset-type :colors))
@ -1108,22 +1118,22 @@
(-> (pcb/empty-changes it) (-> (pcb/empty-changes it)
(pcb/set-undo-group undo-group)) (pcb/set-undo-group undo-group))
[(when sync-components? [(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? (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? (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 file-changes (reduce
pcb/concat-changes pcb/concat-changes
(-> (pcb/empty-changes it) (-> (pcb/empty-changes it)
(pcb/set-undo-group undo-group)) (pcb/set-undo-group undo-group))
[(when sync-components? [(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? (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? (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) changes (pcb/concat-changes library-changes file-changes)

View file

@ -12,6 +12,7 @@
[app.common.files.focus :as cpf] [app.common.files.focus :as cpf]
[app.common.files.helpers :as cfh] [app.common.files.helpers :as cfh]
[app.common.files.libraries-common-helpers :as cflch] [app.common.files.libraries-common-helpers :as cflch]
[app.common.files.libraries-helpers :as cflh]
[app.common.geom.point :as gpt] [app.common.geom.point :as gpt]
[app.common.geom.rect :as grc] [app.common.geom.rect :as grc]
[app.common.geom.shapes :as gsh] [app.common.geom.shapes :as gsh]
@ -28,7 +29,6 @@
[app.main.data.modal :as md] [app.main.data.modal :as md]
[app.main.data.workspace.changes :as dch] [app.main.data.workspace.changes :as dch]
[app.main.data.workspace.collapse :as dwc] [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.specialized-panel :as-alias dwsp]
[app.main.data.workspace.state-helpers :as wsh] [app.main.data.workspace.state-helpers :as wsh]
[app.main.data.workspace.undo :as dwu] [app.main.data.workspace.undo :as dwu]
@ -435,7 +435,7 @@
(gpt/subtract (-> origin-frame :selrect gpt/point))) (gpt/subtract (-> origin-frame :selrect gpt/point)))
instantiate-component instantiate-component
#(dwlh/generate-instantiate-component changes #(cflh/generate-instantiate-component changes
objects objects
file-id file-id
(:component-id component-root) (:component-id component-root)
@ -448,7 +448,7 @@
{}) {})
restore-component 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 restore) (:changes restore)])
[_shape changes] [_shape changes]

View file

@ -123,6 +123,15 @@
(get state :workspace-data) (get state :workspace-data)
(dm/get-in state [:workspace-libraries file-id :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 (defn get-libraries
"Retrieve all libraries, including the local file." "Retrieve all libraries, including the local file."
[state] [state]