mirror of
https://github.com/penpot/penpot.git
synced 2026-11-26 21:00:32 +00:00
🐛 Validate and repair variant properties as a set
Validation compared each variant's property names with the first child of its board, so it missed forbidden characters, too-long text and repeated names, and never checked labels in deleted components. Repair cut every variant down to the first child's names, which dropped values, and left the file invalid. Validation now checks the members of each board as one set: cleaned text, and the same names in the same order, in layers-panel order as in migration 0038. Repeated value combinations stay valid, as the editor allows them. It also checks boards that exist only in a component's objects, variant components that belong to no board, and the whole board when a save touches only one of its members. Repair writes the aligned properties and labels on each component and its main. A live variant main outside its board is reported as :variant-main-outside-board and detached, as the editor does, instead of turning its parent into a board. :main-instance-not-a-variant now repairs the main of a deleted component inside its objects. Moving a shape or component into a board now cuts property values taken from its name or path to 60 characters, as the schema requires. Closes #13238 AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
2b67de0e29
commit
571ee80a4b
@ -12,6 +12,7 @@
|
||||
[app.common.files.changes-builder :as pcb]
|
||||
[app.common.files.helpers :as cfh]
|
||||
[app.common.files.tokens :as cfo]
|
||||
[app.common.files.variant :as cfv]
|
||||
[app.common.logging :as log]
|
||||
[app.common.path-names :as cpn]
|
||||
[app.common.types.component :as ctk]
|
||||
@ -712,17 +713,24 @@
|
||||
|
||||
(defmethod repair-error :main-instance-not-a-variant
|
||||
[_ {:keys [shape page-id args]} file-data _]
|
||||
(let [repair-shape
|
||||
(let [variant-id (:variant-id args)
|
||||
component (get-in file-data [:components (:component-id args)])
|
||||
repair-shape
|
||||
(fn [shape]
|
||||
(let [variant-id (:variant-id args)]
|
||||
;; Set the desired variant-id
|
||||
(log/debug :hint (str " -> set variant-id to " variant-id))
|
||||
(assoc shape :variant-id variant-id)))]
|
||||
;; Set the desired variant-id
|
||||
(log/debug :hint (str " -> set variant-id to " variant-id))
|
||||
(assoc shape :variant-id variant-id))]
|
||||
|
||||
(log/debug :hint "repairing shape :main-instance-not-a-variant" :id (:id shape) :name (:name shape) :page-id page-id)
|
||||
(-> (pcb/empty-changes nil page-id)
|
||||
(pcb/with-file-data file-data)
|
||||
(pcb/update-shapes [(:id shape)] repair-shape))))
|
||||
(if (:deleted component)
|
||||
;; The main of a deleted component lives in the component's objects
|
||||
(-> (pcb/empty-changes nil)
|
||||
(pcb/with-library-data file-data)
|
||||
(pcb/update-component (:id component)
|
||||
#(update-in % [:objects (:id shape)] repair-shape)))
|
||||
(-> (pcb/empty-changes nil page-id)
|
||||
(pcb/with-file-data file-data)
|
||||
(pcb/update-shapes [(:id shape)] repair-shape)))))
|
||||
|
||||
(defmethod repair-error :main-instance-invalid-variant-id
|
||||
[_ {:keys [shape page-id args]} file-data _]
|
||||
@ -739,32 +747,98 @@
|
||||
(pcb/with-file-data file-data)
|
||||
(pcb/update-shapes [(:id shape)] repair-shape))))
|
||||
|
||||
(defn- repair-variant-member-changes
|
||||
[file-data member]
|
||||
(let [component (:component member)
|
||||
props (:props member)
|
||||
variant-name (ctv/properties-to-name props)
|
||||
changed? (cfv/variant-props-changed? component props)
|
||||
main-id (:main-instance-id component)
|
||||
page-id (:main-instance-page component)
|
||||
deleted? (:deleted component)
|
||||
shape (cfv/get-variant-main-shape file-data component)
|
||||
label-differs? (cfv/variant-main-label-changed? file-data component props)
|
||||
comp-changes
|
||||
(when (or changed?
|
||||
(and deleted? label-differs?))
|
||||
(log/debug :hint " -> write variant properties"
|
||||
:component-id (:id component)
|
||||
:changed changed?
|
||||
:label-differs label-differs?)
|
||||
(-> (pcb/empty-changes nil)
|
||||
(pcb/with-library-data file-data)
|
||||
(pcb/update-component
|
||||
(:id component)
|
||||
(fn [component]
|
||||
(cond-> component
|
||||
changed?
|
||||
(assoc :variant-properties props)
|
||||
|
||||
(and (not deleted?) changed?)
|
||||
(dissoc :objects)
|
||||
|
||||
(and deleted? (nil? (:objects component)))
|
||||
(assoc :objects {})
|
||||
|
||||
(and deleted?
|
||||
label-differs?
|
||||
(map? (get-in component [:objects main-id])))
|
||||
(assoc-in [:objects main-id :variant-name] variant-name))))))
|
||||
shape-changes
|
||||
(when (and (not deleted?) label-differs? (map? shape))
|
||||
(log/debug :hint " -> set variant-name" :variant-name variant-name :page-id page-id)
|
||||
(-> (pcb/empty-changes nil page-id)
|
||||
(pcb/with-file-data file-data)
|
||||
(pcb/update-shapes [main-id]
|
||||
(fn [shape]
|
||||
(assoc shape :variant-name variant-name)))))]
|
||||
(cond-> (or comp-changes (pcb/empty-changes nil))
|
||||
shape-changes (pcb/concat-changes shape-changes))))
|
||||
|
||||
(defn- repair-variant-property-set
|
||||
[file-data shape page-id owner-id]
|
||||
(let [objects (if (some? owner-id)
|
||||
(get-in file-data [:components owner-id :objects])
|
||||
(get-in file-data [:pages-index page-id :objects]))
|
||||
;; An earlier repair of this pass may have changed the board
|
||||
board (get objects (:id shape) shape)
|
||||
members (cfv/get-variant-members-props file-data objects board ctv/align-properties-set)]
|
||||
(reduce (fn [changes member]
|
||||
(pcb/concat-changes changes
|
||||
(repair-variant-member-changes file-data member)))
|
||||
(pcb/empty-changes nil)
|
||||
members)))
|
||||
|
||||
(defn- repair-solo-variant-properties
|
||||
[file-data component]
|
||||
(repair-variant-member-changes
|
||||
file-data
|
||||
{:component component
|
||||
:props (ctv/sanitize-properties (or (:variant-properties component) []))}))
|
||||
|
||||
(defmethod repair-error :invalid-variant-properties
|
||||
[_ {:keys [shape page-id args]} file-data _]
|
||||
(let [prop-names (:prop-names args)
|
||||
(log/debug :hint "repairing :invalid-variant-properties" :id (:id shape) :page-id page-id)
|
||||
(if (ctk/is-variant-container? shape)
|
||||
(repair-variant-property-set file-data shape page-id (:component-id args))
|
||||
(repair-solo-variant-properties file-data
|
||||
(get-in file-data [:components (:id shape)]))))
|
||||
|
||||
component (get-in file-data [:components (:component-id shape)])
|
||||
prop-values (into {} (map (juxt :name :value)) (:variant-properties component))
|
||||
properties' (mapv (fn [name] {:name name :value (get prop-values name "")}) prop-names)
|
||||
variant-name (ctv/properties-to-name properties')
|
||||
|
||||
repair-component
|
||||
(fn [component]
|
||||
;; Rebuild component properties, removing any extra ones and adding missing ones with empty value
|
||||
(log/debug :hint " -> rebuild properties" :component-id (:id component) :prop-names (str prop-names))
|
||||
(assoc component :variant-properties properties'))
|
||||
|
||||
repair-shape
|
||||
(fn [shape]
|
||||
(log/debug :hint " -> set variant-name" :variant-name variant-name)
|
||||
(assoc shape :variant-name variant-name))]
|
||||
|
||||
(log/debug :hint "repairing shape :invalid-variant-properties" :id (:id shape) :name (:name shape) :page-id page-id)
|
||||
(defmethod repair-error :variant-main-outside-board
|
||||
[_ {:keys [args]} file-data _]
|
||||
;; Detach the variant, as the editor does when a variant leaves its board.
|
||||
;; The error comes from the main shape or from its component.
|
||||
(let [component (get-in file-data [:components (:component-id args)])
|
||||
page-id (:main-instance-page component)
|
||||
main (cfv/get-variant-main-shape file-data component)
|
||||
props (ctv/sanitize-properties (or (:variant-properties component) []))
|
||||
new-name (ctv/variant-name-to-name
|
||||
(assoc main :variant-name (ctv/properties-to-name props)))]
|
||||
(log/debug :hint "repairing component :variant-main-outside-board" :id (:id component) :page-id page-id)
|
||||
(-> (pcb/empty-changes nil page-id)
|
||||
(pcb/with-file-data file-data)
|
||||
(pcb/with-library-data file-data)
|
||||
(pcb/update-component (:component-id shape) repair-component)
|
||||
(pcb/update-shapes [(:id shape)] repair-shape))))
|
||||
(cfv/generate-detach-variant main new-name))))
|
||||
|
||||
(defmethod repair-error :variant-not-main
|
||||
[_ {:keys [shape page-id]} file-data _]
|
||||
@ -835,10 +909,29 @@
|
||||
(pcb/with-file-data file-data)
|
||||
(pcb/update-shapes [(:id shape)] repair-fn))))
|
||||
|
||||
(defn- get-rewritten-variant-props
|
||||
"Props the property repair would store for this main. On its board,
|
||||
the props of its member in the board's set, or nil when it is not a
|
||||
qualifying member, so the caller keeps the stored vector. Elsewhere,
|
||||
its own cleaned props. Reads the main as earlier repairs of this pass
|
||||
left it."
|
||||
[file-data shape component page-id]
|
||||
(let [objects (get-in file-data [:pages-index page-id :objects])
|
||||
shape (get objects (:id shape) shape)
|
||||
parent (get objects (:parent-id shape))]
|
||||
(if (and (ctk/is-variant-container? parent)
|
||||
(= (:variant-id shape) (:id parent)))
|
||||
(some (fn [member]
|
||||
(when (= (:id component) (:component-id member))
|
||||
(:props member)))
|
||||
(cfv/get-variant-members-props file-data objects parent ctv/align-properties-set))
|
||||
(ctv/sanitize-properties (or (:variant-properties component) [])))))
|
||||
|
||||
(defmethod repair-error :variant-main-bad-variant-name
|
||||
[_ {:keys [shape page-id]} file-data _]
|
||||
(let [component (get-in file-data [:components (:component-id shape)])
|
||||
variant-name (ctv/properties-to-name (:variant-properties component))
|
||||
props (get-rewritten-variant-props file-data shape component page-id)
|
||||
variant-name (ctv/properties-to-name (or props (:variant-properties component)))
|
||||
repair-fn
|
||||
(fn [shape]
|
||||
(log/debug :hint " -> set :variant-name" :variant-name variant-name)
|
||||
|
||||
@ -76,6 +76,7 @@
|
||||
:variant-main-bad-variant-name
|
||||
:variant-component-bad-name
|
||||
:variant-component-bad-id
|
||||
:variant-main-outside-board
|
||||
:missing-tokens-status})
|
||||
|
||||
(def ^:private schema:error
|
||||
@ -109,6 +110,11 @@
|
||||
;; a page's validation pass.
|
||||
(def ^:dynamic ^:private *children-sets* nil)
|
||||
|
||||
;; Variant-component ids already checked in this pass: qualifying members
|
||||
;; of a walked board, and components whose main the page walk reported
|
||||
;; outside its board. `check-component` skips the solo check for them.
|
||||
(def ^:dynamic ^:private *checked-variant-component-ids* nil)
|
||||
|
||||
(defn- build-children-sets
|
||||
"Return a {parent-id → #{child-ids}} map built from `objects` in a single
|
||||
`reduce-kv` pass. Only shapes that have at least one child get an entry."
|
||||
@ -576,10 +582,51 @@
|
||||
(check-empty-swap-slot shape file page)
|
||||
(run! #(check-shape % file page libraries :context :not-component) (:shapes shape)))
|
||||
|
||||
(defn- record-checked-variant-components
|
||||
[component-ids]
|
||||
(when-let [ids *checked-variant-component-ids*]
|
||||
(vswap! ids into component-ids)))
|
||||
|
||||
(defn- variant-member-props-changed?
|
||||
[member]
|
||||
(cfv/variant-props-changed? (:component member) (:props member)))
|
||||
|
||||
(defn- variant-member-label-changed?
|
||||
[data member]
|
||||
(cfv/variant-main-label-changed? data (:component member) (:props member)))
|
||||
|
||||
(defn- variant-properties-need-repair?
|
||||
[data members compare-label]
|
||||
(boolean
|
||||
(some (fn [member]
|
||||
(or (variant-member-props-changed? member)
|
||||
(and compare-label
|
||||
(variant-member-label-changed? data member))))
|
||||
members)))
|
||||
|
||||
(defn- check-variant-property-set
|
||||
"Check the members of the board `shape` as one set. Skip a board that
|
||||
was already checked in this pass: its members are recorded, and a
|
||||
member belongs to one board only."
|
||||
[shape file page objects data & {:keys [compare-label owner-id]}]
|
||||
(let [members (cfv/get-variant-members-props data objects shape ctv/align-properties-set)
|
||||
checked (some-> *checked-variant-component-ids* deref)]
|
||||
(when-not (some #(contains? checked (:component-id %)) members)
|
||||
(record-checked-variant-components (map :component-id members))
|
||||
(when (variant-properties-need-repair? data members compare-label)
|
||||
(if (some? owner-id)
|
||||
(report-error :invalid-variant-properties
|
||||
(str/ffmt "Variant container % has invalid properties" (:id shape))
|
||||
shape file page
|
||||
:component-id owner-id)
|
||||
(report-error :invalid-variant-properties
|
||||
(str/ffmt "Variant container % has invalid properties" (:id shape))
|
||||
shape file page))))))
|
||||
|
||||
(defn- check-variant-container
|
||||
"Shape is a variant container, so:
|
||||
-all its children should be variants with variant-id equals to the shape-id
|
||||
-all the components should have the same properties
|
||||
-the stored properties should match the shared rewrite
|
||||
-the name of the container should be normalized for paths (separator / with one space at each side)."
|
||||
[shape file page]
|
||||
(let [shape-id (:id shape)
|
||||
@ -587,8 +634,6 @@
|
||||
shapes (:shapes shape)
|
||||
objects (:objects page)
|
||||
file-data (:data file)
|
||||
first-child (get objects (first shapes))
|
||||
prop-names (cfv/extract-properties-names first-child file-data)
|
||||
clean-path (cpn/clean-path container-name)
|
||||
clean-path (if (str/blank? clean-path)
|
||||
"(empty)" ;; this may occur if the name contains only separators
|
||||
@ -605,18 +650,66 @@
|
||||
(str/ffmt "Main instance shape % should be a variant" (:id child))
|
||||
child file page
|
||||
:variant-id shape-id)
|
||||
(do
|
||||
(when (not= (:variant-id child) shape-id)
|
||||
(report-error :main-instance-invalid-variant-id
|
||||
(str/ffmt "Main instance in variant % should have the variant-id of the container but has %" (:id child) (:variant-id child))
|
||||
child file page
|
||||
:variant-id shape-id))
|
||||
(when (not= prop-names (cfv/extract-properties-names child file-data))
|
||||
(report-error :invalid-variant-properties
|
||||
(str/ffmt "Variant % has invalid properties %" (:id child) (vec prop-names))
|
||||
child file page
|
||||
:prop-names prop-names))))))
|
||||
shapes)))
|
||||
(when (not= (:variant-id child) shape-id)
|
||||
(report-error :main-instance-invalid-variant-id
|
||||
(str/ffmt "Main instance in variant % should have the variant-id of the container but has %" (:id child) (:variant-id child))
|
||||
child file page
|
||||
:variant-id shape-id)))))
|
||||
shapes)
|
||||
(check-variant-property-set shape file page objects file-data
|
||||
:compare-label false)))
|
||||
|
||||
(defn- parent-is-board?
|
||||
"True when `parent` is the board of the variant main `shape`: a variant
|
||||
container, or the shape whose id is the shape's :variant-id."
|
||||
[shape parent]
|
||||
(and (some? parent)
|
||||
(or (ctk/is-variant-container? parent)
|
||||
(= (:variant-id shape) (:id parent)))))
|
||||
|
||||
(defn- find-variant-board
|
||||
"Find the board of `component` by its :variant-id: on the page of its
|
||||
main, or else in some component's `:objects`. Only a shape flagged as a
|
||||
variant container counts, as in the page walk and migration 0038. Return {:objects
|
||||
:container :page} or {:objects :container :owner-id}."
|
||||
[data component]
|
||||
(let [board-id (:variant-id component)
|
||||
page (get-in data [:pages-index (:main-instance-page component)])
|
||||
board (get-in page [:objects board-id])]
|
||||
(if (ctk/is-variant-container? board)
|
||||
{:objects (:objects page) :container board :page page}
|
||||
(some (fn [other]
|
||||
(let [board (get-in other [:objects board-id])]
|
||||
(when (ctk/is-variant-container? board)
|
||||
{:objects (:objects other)
|
||||
:container board
|
||||
:owner-id (:id other)})))
|
||||
(vals (:components data))))))
|
||||
|
||||
(defn- variant-board-member?
|
||||
[data {:keys [objects container]} component]
|
||||
(some #(= (:id component) (:component-id %))
|
||||
(cfv/qualifying-variant-members data objects container)))
|
||||
|
||||
(defn- variant-component-checked?
|
||||
"True when `component` was already checked in this pass, or is a
|
||||
qualifying member of its board."
|
||||
[data component]
|
||||
(or (contains? (some-> *checked-variant-component-ids* deref) (:id component))
|
||||
(when-let [board (find-variant-board data component)]
|
||||
(variant-board-member? data board component))))
|
||||
|
||||
(defn- main-outside-board?
|
||||
"True when `component` is live and its main is on its page, is a
|
||||
variant, and sits under a parent that is not a board. A main that lost
|
||||
its :variant-id is left to :main-instance-not-a-variant first."
|
||||
[data component]
|
||||
(when-not (:deleted component)
|
||||
(when-let [main (cfv/get-variant-main-shape data component)]
|
||||
(when (ctk/is-variant? main)
|
||||
(let [parent (get-in data [:pages-index (:main-instance-page component)
|
||||
:objects (:parent-id main)])]
|
||||
(not (parent-is-board? main parent)))))))
|
||||
|
||||
(defn- renamed-container?
|
||||
[container]
|
||||
@ -626,44 +719,61 @@
|
||||
(defn- check-variant
|
||||
"Shape is a variant, so
|
||||
-it should be a main component
|
||||
-its parent should be a variant-container
|
||||
-the parent is the board when it is a variant container, or when
|
||||
the shape's :variant-id equals the parent's id
|
||||
-:parent-not-variant, :variant-main-bad-name,
|
||||
:variant-component-bad-name, and :variant-component-bad-id
|
||||
report only then
|
||||
-otherwise, when it is the main of a live component that belongs
|
||||
to no board, :variant-main-outside-board reports it
|
||||
-its variant-name is derived from the properties
|
||||
-its name should be the same as its parent's
|
||||
"
|
||||
[shape file page]
|
||||
(let [parent (ctst/get-shape page (:parent-id shape))
|
||||
component (ctkl/get-component (:data file) (:component-id shape) true)
|
||||
variant-name (ctv/properties-to-name (:variant-properties component))]
|
||||
(let [parent (ctst/get-shape page (:parent-id shape))
|
||||
component (ctkl/get-component (:data file) (:component-id shape) true)
|
||||
variant-name (ctv/properties-to-name (:variant-properties component))
|
||||
on-board? (parent-is-board? shape parent)]
|
||||
(when-not (ctk/main-instance? shape)
|
||||
(report-error :variant-not-main
|
||||
(str/ffmt "Variant % is not a main instance" (:id shape))
|
||||
shape file page))
|
||||
(when-not (ctk/is-variant-container? parent)
|
||||
(report-error :parent-not-variant
|
||||
(str/ffmt "Variant % has an invalid parent" (:id shape))
|
||||
shape file page))
|
||||
(when-not (= variant-name (:variant-name shape))
|
||||
(report-error :variant-main-bad-variant-name
|
||||
(str/ffmt "Variant % has an invalid variant-name" (:id shape))
|
||||
shape file page
|
||||
:variant-name variant-name))
|
||||
;; If the container is going to be renamed all main and components will be renamed, too
|
||||
(when-not (renamed-container? parent)
|
||||
(when-not (= (:name parent) (:name shape))
|
||||
(report-error :variant-main-bad-name
|
||||
(str/ffmt "Main instance inside variant % has an invalid name" (:id shape))
|
||||
(when on-board?
|
||||
(when-not (ctk/is-variant-container? parent)
|
||||
(report-error :parent-not-variant
|
||||
(str/ffmt "Variant % has an invalid parent" (:id shape))
|
||||
shape file page))
|
||||
;; If the container is going to be renamed all main and components will be renamed, too
|
||||
(when-not (renamed-container? parent)
|
||||
(when-not (= (:name parent) (:name shape))
|
||||
(report-error :variant-main-bad-name
|
||||
(str/ffmt "Main instance inside variant % has an invalid name" (:id shape))
|
||||
shape file page
|
||||
:variant-name (:name parent)))
|
||||
(when-not (= (:name parent) (cpn/merge-path-item (:path component) (:name component)))
|
||||
(report-error :variant-component-bad-name
|
||||
(str/ffmt "Component % has an invalid name" (:id shape))
|
||||
shape file page
|
||||
:variant-container-name (:name parent))))
|
||||
(when-not (= (:variant-id component) (:variant-id shape))
|
||||
(report-error :variant-component-bad-id
|
||||
(str/ffmt "Variant % has a :variant-id that does not point to the container" (:id shape))
|
||||
shape file page
|
||||
:variant-name (:name parent)))
|
||||
(when-not (= (:name parent) (cpn/merge-path-item (:path component) (:name component)))
|
||||
(report-error :variant-component-bad-name
|
||||
(str/ffmt "Component % has an invalid name" (:id shape))
|
||||
shape file page
|
||||
:variant-container-name (:name parent))))
|
||||
(when-not (= (:variant-id component) (:variant-id shape))
|
||||
(report-error :variant-component-bad-id
|
||||
(str/ffmt "Variant % has a :variant-id that does not point to the container" (:id shape))
|
||||
:variant-id (:parent-id shape))))
|
||||
(when (and (not on-board?)
|
||||
(= (:id shape) (:main-instance-id component))
|
||||
(main-outside-board? (:data file) component)
|
||||
(not (variant-component-checked? (:data file) component)))
|
||||
(record-checked-variant-components [(:id component)])
|
||||
(report-error :variant-main-outside-board
|
||||
(str/ffmt "Variant % is not on a variant board" (:id shape))
|
||||
shape file page
|
||||
:variant-id (:parent-id shape)))))
|
||||
:component-id (:id component)))))
|
||||
|
||||
(defn- check-shape
|
||||
"Validate referential integrity and semantic coherence of
|
||||
@ -777,9 +887,8 @@
|
||||
component file nil :cycles-ids cycles-ids))))
|
||||
|
||||
(defn- check-variant-component
|
||||
"Component is a variant, so:
|
||||
-Its main should be a variant
|
||||
-It should have at least one variant property"
|
||||
"Component is a variant, so its main should be a variant. A deleted
|
||||
component's main is the one inside its `:objects`."
|
||||
[component file]
|
||||
(let [component-page (ctf/get-component-page (:data file) component)
|
||||
main-instance (if (:deleted component)
|
||||
@ -790,7 +899,8 @@
|
||||
(report-error :main-instance-not-a-variant
|
||||
(str/ffmt "Main instance shape % should be a variant" (:id main-instance))
|
||||
main-instance file component-page
|
||||
:variant-id (:variant-id component)))))
|
||||
:variant-id (:variant-id component)
|
||||
:component-id (:id component)))))
|
||||
|
||||
(defn- check-main-inside-main
|
||||
[component file]
|
||||
@ -813,6 +923,65 @@
|
||||
"A non-deleted component cannot have shapes inside"
|
||||
component file nil)))
|
||||
|
||||
(defn- board-on-members-page?
|
||||
"True when the board `shape`, found in `objects`, is also a shape on the
|
||||
page of one of its qualifying members' mains. The page walk checks
|
||||
that board. Only those pages are read, never every page of the file."
|
||||
[data objects shape]
|
||||
(some (fn [{:keys [component]}]
|
||||
(some? (get-in data [:pages-index (:main-instance-page component)
|
||||
:objects (:id shape)])))
|
||||
(cfv/qualifying-variant-members data objects shape)))
|
||||
|
||||
(defn- check-objects-only-variant-boards
|
||||
"Walk variant boards that exist only in this component's `:objects`."
|
||||
[component file]
|
||||
(when-let [objects (:objects component)]
|
||||
(when-let [boards (seq (filter ctk/is-variant-container? (vals objects)))]
|
||||
(let [data (:data file)]
|
||||
(doseq [shape boards]
|
||||
(when-not (board-on-members-page? data objects shape)
|
||||
(check-variant-property-set shape file nil objects data
|
||||
:compare-label true
|
||||
:owner-id (:id component))))))))
|
||||
|
||||
(defn- check-solo-variant-component
|
||||
"A variant component that belongs to no board. A live one whose main
|
||||
sits under a parent that is not a board is reported so the repair
|
||||
detaches it. Otherwise, its properties are cleaned on their own and
|
||||
its main's label must match them."
|
||||
[component file]
|
||||
(let [data (:data file)]
|
||||
(if (main-outside-board? data component)
|
||||
(report-error :variant-main-outside-board
|
||||
(str/ffmt "Variant component % is not on a variant board" (:id component))
|
||||
component file nil
|
||||
:component-id (:id component))
|
||||
(let [member {:component component
|
||||
:props (ctv/sanitize-properties
|
||||
(or (:variant-properties component) []))}]
|
||||
(when (or (variant-member-props-changed? member)
|
||||
(variant-member-label-changed? data member))
|
||||
(report-error :invalid-variant-properties
|
||||
(str/ffmt "Variant component % has invalid properties" (:id component))
|
||||
component file nil))))))
|
||||
|
||||
(defn- check-variant-component-properties
|
||||
"Check the properties of a variant component that no board check of this
|
||||
pass covered. A qualifying member is checked with its whole board, so a
|
||||
save that touches only the component still checks the set. The board
|
||||
is not walked in this pass, so labels are compared too. Any other
|
||||
variant component is checked on its own."
|
||||
[component file]
|
||||
(let [data (:data file)]
|
||||
(when-not (contains? (some-> *checked-variant-component-ids* deref) (:id component))
|
||||
(let [board (find-variant-board data component)]
|
||||
(if (and (some? board) (variant-board-member? data board component))
|
||||
(check-variant-property-set (:container board) file (:page board) (:objects board) data
|
||||
:compare-label true
|
||||
:owner-id (:owner-id board))
|
||||
(check-solo-variant-component component file))))))
|
||||
|
||||
(defn- check-component
|
||||
"Validate semantic coherence of a component. Report all errors found."
|
||||
[component file]
|
||||
@ -828,7 +997,8 @@
|
||||
(check-ref-cycles component file))
|
||||
|
||||
(when (ctk/is-variant? component)
|
||||
(check-variant-component component file)))
|
||||
(check-variant-component component file)
|
||||
(check-variant-component-properties component file)))
|
||||
|
||||
(defn- get-orphan-shapes
|
||||
"Return the ids of shapes unreachable from the root traversal: shapes
|
||||
@ -875,7 +1045,8 @@
|
||||
Return a list of errors or `nil`"
|
||||
[{:keys [data features] :as file} libraries]
|
||||
(when (contains? features "components/v2")
|
||||
(binding [*errors* (volatile! [])]
|
||||
(binding [*errors* (volatile! [])
|
||||
*checked-variant-component-ids* (volatile! #{})]
|
||||
|
||||
;; `reduce-kv` is used throughout this function (and `validate-file-affected`)
|
||||
;; instead of `(run! f (vals m))` or `(doseq [[k v] m])` because persistent
|
||||
@ -898,7 +1069,8 @@
|
||||
|
||||
(reduce-kv
|
||||
(fn [_ _ component]
|
||||
(check-component component file))
|
||||
(check-component component file)
|
||||
(check-objects-only-variant-boards component file))
|
||||
nil
|
||||
(:components data))
|
||||
|
||||
@ -986,7 +1158,8 @@
|
||||
[{:keys [data features] :as file} libraries changes]
|
||||
(when (contains? features "components/v2")
|
||||
(let [{:keys [page-ids component-ids]} (extract-affected-ids changes)]
|
||||
(binding [*errors* (volatile! [])]
|
||||
(binding [*errors* (volatile! [])
|
||||
*checked-variant-component-ids* (volatile! #{})]
|
||||
|
||||
(reduce-kv
|
||||
(fn [_ page-id page]
|
||||
@ -1006,7 +1179,8 @@
|
||||
(reduce-kv
|
||||
(fn [_ id component]
|
||||
(when (contains? component-ids id)
|
||||
(check-component component file)))
|
||||
(check-component component file)
|
||||
(check-objects-only-variant-boards component file)))
|
||||
nil
|
||||
(:components data))
|
||||
|
||||
|
||||
@ -6,6 +6,8 @@
|
||||
(ns app.common.files.variant
|
||||
(:require
|
||||
[app.common.data.macros :as dm]
|
||||
[app.common.files.changes-builder :as pcb]
|
||||
[app.common.path-names :as cpn]
|
||||
[app.common.types.component :as ctc]
|
||||
[app.common.types.components-list :as ctcl]
|
||||
[app.common.types.variant :as ctv]))
|
||||
@ -81,6 +83,22 @@
|
||||
(and (seq shapes)
|
||||
(not= (:main-instance-id component) (last shapes)))))
|
||||
|
||||
(defn generate-detach-variant
|
||||
"Add to `changes` the edits that turn the variant main `shape` into a
|
||||
plain component main named `new-name`: drop the variant fields from the
|
||||
shape and its component, and take the component's path and name from
|
||||
`new-name`."
|
||||
[changes shape new-name]
|
||||
(let [[cpath cname] (cpn/split-group-name new-name)]
|
||||
(-> changes
|
||||
(pcb/update-component (:component-id shape)
|
||||
#(-> (dissoc % :variant-id :variant-properties)
|
||||
(assoc :name cname :path cpath))
|
||||
{:apply-changes-local-library? true})
|
||||
(pcb/update-shapes [(:id shape)]
|
||||
#(-> (dissoc % :variant-id :variant-name)
|
||||
(assoc :name new-name))))))
|
||||
|
||||
(defn get-primary-variant
|
||||
[data component]
|
||||
(let [page-id (:main-instance-page component)
|
||||
@ -133,6 +151,13 @@
|
||||
(not= (ctv/properties-to-name props)
|
||||
(:variant-name shape)))))
|
||||
|
||||
(defn variant-props-changed?
|
||||
"True when the stored `:variant-properties` of `component` differ from
|
||||
`props`. A missing property list counts as empty."
|
||||
[component props]
|
||||
(not= (vec (or (:variant-properties component) []))
|
||||
(vec props)))
|
||||
|
||||
(defn- qualifying-variant-member
|
||||
[data objects container child-id]
|
||||
(when-let [child (get objects child-id)]
|
||||
@ -168,21 +193,26 @@
|
||||
(ctv/properties-to-name props))
|
||||
data)))
|
||||
|
||||
(defn get-variant-members-props
|
||||
"Return the qualifying members of `container` in layers-panel order
|
||||
(top first, the reverse of `:shapes`). Each member also gets `:props`,
|
||||
its vector in the result of `f` over all their property vectors. `f`
|
||||
takes and returns a vector of property vectors."
|
||||
[data objects container f]
|
||||
(let [members (vec (rseq (qualifying-variant-members data objects container)))
|
||||
props-seq (f (mapv #(vec (or (get-in % [:component :variant-properties]) []))
|
||||
members))]
|
||||
(mapv #(assoc %1 :props %2) members props-seq)))
|
||||
|
||||
(defn update-variant-container-properties
|
||||
"Apply `f` to the property vectors of the members of `container`, in
|
||||
layers-panel order (top first, the reverse of `:shapes`), and write each
|
||||
result with `update-variant-properties`. `f` takes and returns a vector
|
||||
of property vectors."
|
||||
layers-panel order, and write each result with
|
||||
`update-variant-properties`. See `get-variant-members-props`."
|
||||
[data objects container f]
|
||||
(let [components (->> (qualifying-variant-members data objects container)
|
||||
(map :component)
|
||||
(reverse)
|
||||
(vec))
|
||||
props-seq (f (mapv #(vec (or (:variant-properties %) [])) components))]
|
||||
(reduce (fn [data [component props]]
|
||||
(update-variant-properties data component (constantly props)))
|
||||
data
|
||||
(map vector components props-seq))))
|
||||
(reduce (fn [data {:keys [component props]}]
|
||||
(update-variant-properties data component (constantly props)))
|
||||
data
|
||||
(get-variant-members-props data objects container f)))
|
||||
|
||||
(defn sanitize-variant-component
|
||||
"Sanitize the properties of one variant component on its own."
|
||||
|
||||
@ -166,17 +166,7 @@
|
||||
|
||||
(defn- generate-make-shape-no-variant
|
||||
[changes shape]
|
||||
(let [new-name (ctv/variant-name-to-name shape)
|
||||
[cpath cname] (cpn/split-group-name new-name)]
|
||||
(-> changes
|
||||
(pcb/update-component (:component-id shape)
|
||||
#(-> (dissoc % :variant-id :variant-properties)
|
||||
(assoc :name cname
|
||||
:path cpath))
|
||||
{:apply-changes-local-library? true})
|
||||
(pcb/update-shapes [(:id shape)]
|
||||
#(-> (dissoc % :variant-id :variant-name)
|
||||
(assoc :name new-name))))))
|
||||
(cfv/generate-detach-variant changes shape (ctv/variant-name-to-name shape)))
|
||||
|
||||
(defn generate-make-shapes-no-variant
|
||||
[changes shapes]
|
||||
@ -196,7 +186,7 @@
|
||||
props (ctv/add-new-props props (repeat new-props ""))]
|
||||
|
||||
(if add-name?
|
||||
(ctv/add-new-prop props (or (ctv/sanitize-property-text (:name component)) ""))
|
||||
(ctv/add-new-prop props (ctv/sanitize-property-value (:name component)))
|
||||
props)))
|
||||
|
||||
(defn- create-new-properties-from-non-variant
|
||||
|
||||
@ -134,6 +134,8 @@
|
||||
:value v}))]
|
||||
(into props xf values)))
|
||||
|
||||
(declare sanitize-property-value)
|
||||
|
||||
(defn path-to-properties
|
||||
"From a list of properties and a name with path, assign each token of the
|
||||
path as value of a different property"
|
||||
@ -141,7 +143,7 @@
|
||||
(path-to-properties path properties 0))
|
||||
([path properties min-props]
|
||||
(let [cpath (->> (cpn/split-path path)
|
||||
(mapv #(or (sanitize-property-text %) "")))
|
||||
(mapv sanitize-property-value))
|
||||
total-props (max (count cpath) min-props)
|
||||
assigned (mapv #(assoc % :value (nth cpath %2 "")) properties (range))
|
||||
;; Add empty strings to the end of cpath to reach the minimum number of properties
|
||||
@ -341,19 +343,23 @@
|
||||
taken (map #(nth % idx "") seen)]
|
||||
(update-in props [idx :value] #(unique-property-text taken %))))))
|
||||
|
||||
(defn sanitize-properties-set
|
||||
(defn align-properties-set
|
||||
"Sanitize the property vectors of all the variants of one set, given in
|
||||
layers-panel order (top first). Every vector gets the same names in the
|
||||
same order, and no two vectors share a value combination. Earlier
|
||||
vectors keep their values."
|
||||
same order. Two vectors may keep the same value combination."
|
||||
[props-seq]
|
||||
(let [cleaned (mapv sanitize-properties props-seq)
|
||||
names (properties-names cleaned)]
|
||||
(reduce (fn [acc props]
|
||||
(conj acc (unique-values-tuple (align-properties props names)
|
||||
(map #(mapv :value %) acc))))
|
||||
[]
|
||||
cleaned)))
|
||||
(mapv #(align-properties % names) cleaned)))
|
||||
|
||||
(defn sanitize-properties-set
|
||||
"Like `align-properties-set`, and also no two vectors share a value
|
||||
combination. Earlier vectors keep their values."
|
||||
[props-seq]
|
||||
(reduce (fn [acc props]
|
||||
(conj acc (unique-values-tuple props (map #(mapv :value %) acc))))
|
||||
[]
|
||||
(align-properties-set props-seq)))
|
||||
|
||||
(defn find-index-for-property-name
|
||||
"Finds the index of a name in a property map"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -109,6 +109,17 @@
|
||||
(:variant-properties (thc/get-component file' :c02))))
|
||||
(t/is (= "ab (1)" (:variant-name (ths/get-shape file' :m02))))))
|
||||
|
||||
(t/deftest test-get-variant-members-props-returns-the-top-member-first
|
||||
;; :shapes of the board is [m02 m01], so m01 is the top variant.
|
||||
(let [file (variant-file)
|
||||
page (thf/current-page file)
|
||||
container (ths/get-shape file :v01)
|
||||
members (cfv/get-variant-members-props (:data file) (:objects page) container identity)]
|
||||
|
||||
(t/is (= [(thi/id :c01) (thi/id :c02)] (mapv :component-id members)))
|
||||
(t/is (= (:variant-properties (thc/get-component file :c01))
|
||||
(:props (first members))))))
|
||||
|
||||
(t/deftest test-sanitize-variant-component
|
||||
(let [file (-> (variant-file)
|
||||
(thc/update-component :c01 {:variant-properties [{:name "Size=Big" :value "a,b"}]}))
|
||||
|
||||
@ -482,6 +482,29 @@
|
||||
|
||||
(t/is (contains? values "Bigred"))))
|
||||
|
||||
(t/deftest test-make-shapes-variant-clips-long-component-name-as-value
|
||||
;; A component name over the property length limit becomes a value cut to
|
||||
;; that limit, so the moved variant passes validation.
|
||||
(let [long-name (apply str (repeat 70 "n"))
|
||||
file (-> (thf/sample-file :file1)
|
||||
(thv/add-variant :v01 :c01 :m01 :c02 :m02)
|
||||
(thv/add-variant :v02 :c03 :m03 :c04 :m04)
|
||||
(thc/update-component :c03 {:name long-name}))
|
||||
page (thf/current-page file)
|
||||
container (ths/get-shape file :v01)
|
||||
shape (ths/get-shape file :m03)
|
||||
changes (-> (pcb/empty-changes nil)
|
||||
(pcb/with-page-id (:id page))
|
||||
(pcb/with-library-data (:data file))
|
||||
(pcb/with-objects (:objects page))
|
||||
(clvp/generate-make-shapes-variant [shape] container)
|
||||
(pcb/change-parent (:id container) [shape] 0))
|
||||
file' (thf/apply-changes file changes)
|
||||
values (map :value (:variant-properties (thc/get-component file' :c03)))]
|
||||
|
||||
(t/is (every? #(<= (count %) ctv/property-max-length) values))
|
||||
(t/is (some #(= (subs long-name 0 ctv/property-max-length) %) values))))
|
||||
|
||||
(t/deftest test-make-shapes-variant-same-name-components-get-unique-values
|
||||
;; Mirrors combine-as-variants with components that share the container
|
||||
;; name: the first main defines the container, the rest are moved into it.
|
||||
|
||||
@ -215,6 +215,24 @@
|
||||
(t/is (= [{:name "S" :value "(1)"}]
|
||||
(ctv/unique-values-tuple [{:name "S" :value ""}] [[""]])))))
|
||||
|
||||
(t/deftest align-properties-set
|
||||
(t/testing "text is cleaned and every vector gets the same names"
|
||||
(t/is (= [[{:name "SizeBig" :value "one"} {:name "Color" :value ""}]
|
||||
[{:name "SizeBig" :value ""} {:name "Color" :value "red"}]]
|
||||
(ctv/align-properties-set [[{:name "Size=Big" :value "one"}]
|
||||
[{:name "Color" :value "red"}]]))))
|
||||
|
||||
(t/testing "a repeated value tuple is kept"
|
||||
(let [props-seq [[{:name "Size" :value "big"}]
|
||||
[{:name "Size" :value "big"}]]]
|
||||
(t/is (= props-seq (ctv/align-properties-set props-seq)))))
|
||||
|
||||
(t/testing "a second run changes nothing"
|
||||
(let [once (ctv/align-properties-set [[{:name "a=b" :value long-text} {:name "a=b" :value ""}]
|
||||
[{:name "ab" :value long-text}]
|
||||
[{:name "=" :value 5}]])]
|
||||
(t/is (= once (ctv/align-properties-set once))))))
|
||||
|
||||
(t/deftest sanitize-properties-set
|
||||
(t/testing "a colliding value tuple is numbered"
|
||||
(t/is (= [[{:name "Size" :value "ab"}]
|
||||
|
||||
@ -219,3 +219,8 @@
|
||||
result (ctv/path-to-properties "Button / Big, red" props)]
|
||||
(t/is (= "Button" (-> result first :value)))
|
||||
(t/is (= "Big red" (-> result second :value)))))
|
||||
|
||||
(t/deftest path-to-properties-clips-long-values
|
||||
(let [segment (apply str (repeat 70 "s"))
|
||||
result (ctv/path-to-properties (str "Button / " segment) [{:name "Property 1" :value ""}])]
|
||||
(t/is (= (subs segment 0 ctv/property-max-length) (-> result second :value)))))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user