📚 Update code samples

This commit is contained in:
Andrés Moya 2025-04-28 14:35:17 +02:00
parent 2ddcd0ce15
commit 057bf9bf25

View file

@ -63,20 +63,19 @@ Each structure in this module has:
* A **schema spec** that defines the structure of the type and its values: * A **schema spec** that defines the structure of the type and its values:
```clojure ```clojure
(def ::fill (def schema:fill
[:map {:title "Fill"} [:map {:title "Fill"}
[:fill-color {:optional true} ::ctc/rgb-color] [:fill-color {:optional true} ::ctc/rgb-color]
[:fill-opacity {:optional true} ::sm/safe-number] [:fill-opacity {:optional true} ::sm/safe-number]
...) ...)
(def ::shape-attrs (def schema:shape-base-attrs
[:map {:title "ShapeAttrs"} [:map {:title "ShapeMinimalRecord"}
[:name {:optional true} :string] [:id ::sm/uuid]
[:selrect {:optional true} ::grc/rect] [:name :string]
[:points {:optional true} ::points] [:type [::sm/one-of shape-types]]
[:blocked {:optional true} :boolean] [:selrect ::grc/rect]
[:fills {:optional true} [:points schema:points]
[:vector {:gen/max 2} ::fill]]
...) ...)
(def schema:token-set-attrs (def schema:token-set-attrs
@ -93,7 +92,8 @@ Each structure in this module has:
* A **protocol** that define the external interface to be used for this entity. * A **protocol** that define the external interface to be used for this entity.
(NOTE: this is currently only implemented in Design Tokens subsystem). (NOTE: this is currently only implemented in some subsystems like Design Tokens
and new path handling).
```clojure ```clojure
(defprotocol ITokenSet (defprotocol ITokenSet
@ -199,20 +199,15 @@ domain entities inside a file.**
:include-deleted? include-deleted?))) :include-deleted? include-deleted?)))
(defn delete-component (defn delete-component
"Mark a component as deleted and store the main instance shapes inside it, to "Mark a component as deleted and store the main instance shapes iside it, to
be able to be recovered later." be able to be recovered later."
[file-data component-id skip-undelete? Main-instance] [file-data component-id skip-undelete? delta]
(let [components-v2 (dm/get-in file-data [:options :components-v2])] (let [delta (or delta (gpt/point 0 0))]
(if (or (not components-v2) skip-undelete?) (if skip-undelete?
(ctkl/delete-component file-data component-id) (ctkl/delete-component file-data component-id)
(let [set-main-instance ;; If there is a saved main-instance, restore it. (-> file-data
#(if main-instance (ctkl/update-component component-id #(load-component-objects file-data % delta))
(assoc-in % [:objects (:main-instance-id %)] main-instance) (ctkl/mark-component-deleted component-id)))))
%)]
(-> file-data
(ctkl/update-component component-id load-component-objects)
(ctkl/update-component component-id set-main-instance)
(ctkl/mark-component-deleted component-id))))))
``` ```
> This module is still needing an important refactor. Mainly to take functions > This module is still needing an important refactor. Mainly to take functions
@ -251,21 +246,6 @@ There exists a <code class="language-clojure">changes-builder</code> module
with helper functions to conveniently build changes objects, and to with helper functions to conveniently build changes objects, and to
automatically calculate the reverse undo change. automatically calculate the reverse undo change.
```clojure
(sm/define! ::changes
[:map {:title "changes"}
[:redo-changes vector?]
[:undo-changes seq?]
[:origin {:optional true} any?]
[:save-undo? {:optional true} boolean?]
[:stack-undo? {:optional true} boolean?]
[:undo-group {:optional true} any?]])
(defmethod process-change :add-component
[file-data params]
(ctkl/add-component file-data params))
```
> IMPORTANT RULES > IMPORTANT RULES
> >
> All changes must satisfy two properties: > All changes must satisfy two properties:
@ -303,27 +283,35 @@ has an applied token.
```clojure ```clojure
(defn generate-instantiate-component (defn generate-instantiate-component
"Generate changes to create a new instance from a component." "Generate changes to create a new instance from a component."
[changes objects file-id component-id position page libraries old-id parent-id ([changes objects file-id component-id position page libraries]
frame-id {:keys [force-frame?] :or {force-frame? False}}] (generate-instantiate-component changes objects file-id component-id position page libraries nil nil nil {}))
(let [component (ctf/get-component libraries file-id component-id) ([changes objects file-id component-id position page libraries old-id parent-id frame-id
parent (when parent-id (get objects parent-id)) {:keys [force-frame?]
library (get libraries file-id) :or {force-frame? false}}]
components-v2 (dm/get-in library [:data :options :components-v2]) (let [component (ctf/get-component libraries file-id component-id)
[new-shape new-shapes]º library (get libraries file-id)
(ctn/make-component-instance page parent (when parent-id (get objects parent-id))
Component
(:data library) [...]
Position
Components-v2 [new-shape new-shapes]
(cond-> {} (ctn/make-component-instance page
force-frame? (assoc :force-frame-id frame-id))) component
changes (cond-> (pcb/add-object changes first-shape {:ignore-touched true}) (:data library)
(some? old-id) (pcb/amend-last-change #(assoc % :old-id old-id))) position
changes (reduce #(pcb/add-object %1 %2 {:ignore-touched true}) (cond-> {}
changes force-frame?
(rest new-shapes))] (assoc :force-frame-id frame-id)))
[new-shape changes]))
[...]
changes
(reduce #(pcb/add-object %1 %2 {:ignore-touched true})
changes
(rest new-shapes))]
[new-shape changes])))
``` ```
## Data events ## Data events