mirror of
https://github.com/penpot/penpot.git
synced 2026-11-21 01:22:01 +00:00
✨ Generate moves, renames and annotations of components
Three kinds join the structure programs, and the programs built on them: - move: a shape a main holds, or a nested copy head, moves to the page, into a board or group of its own main or of another. The model gives an override of its constraints the defaults of its new parent, as the move resets them. - rename: the assets panel's rename, with names that carry a path. The component must take the normalized path and name, and its main root the whole name. - annotate: a component's annotation is set or removed. Only that edit, or its undo or redo, may change an annotation. The frontend interpreter realises the rename and the annotation through their workspace events. Harness only; the benchmark is not run. Also in this commit: - Ignore the layout-given size of a hidden layout child in the oracle. A hidden shape leaves its layout's flow, so it keeps the size the layout last gave it along its fill or auto axes, while a sync from its main can give it the main's. The layout gives it its size again once it is shown. The oracle expected the main's size, and the design snapshots of a settled and a fully synced world differed by it. Those sizes are now nobody's to keep, as the place of a hidden layout child already is. AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
d862f99bb9
commit
bca17d5dac
@ -251,6 +251,74 @@
|
||||
[target property value]
|
||||
(tm/assign-id (->ChangeProperty target property value)))
|
||||
|
||||
;; ---------------------------------------------------------------------------
|
||||
;; rename-component — the assets panel's rename of a component
|
||||
;;
|
||||
;; As the frontend's `rename-component-and-main-instance`: the component takes
|
||||
;; the normalized name, split into its path and name, and its main root the
|
||||
;; whole normalized name.
|
||||
;; ---------------------------------------------------------------------------
|
||||
|
||||
(defrecord RenameComponent [target value]
|
||||
IComponentOperation
|
||||
(op-kind [_] :user)
|
||||
|
||||
tm/IOperation
|
||||
(apply-to [this situation]
|
||||
(let [the-file (tm/file situation)
|
||||
shape-id (tm/target-shape-id situation target)
|
||||
page (thf/current-page the-file)
|
||||
shape (get-in page [:objects shape-id])
|
||||
name (cpn/normalize-path-name value)
|
||||
changes (-> (pcb/empty-changes nil (:id page))
|
||||
(cll/generate-rename-component (:component-id shape) name (:data the-file))
|
||||
(cls/generate-update-shapes #{shape-id} #(assoc % :name name) (:objects page) {}))
|
||||
file' (thf/apply-changes the-file changes)]
|
||||
(-> situation
|
||||
(tm/with-file file')
|
||||
(record-changes changes)
|
||||
(tm/record-application this {:target target :value value})))))
|
||||
|
||||
(defn rename-component
|
||||
"Constructor for the rename-component operation: rename the component of
|
||||
the main root `target` names to `value`, from the assets panel."
|
||||
[target value]
|
||||
(tm/assign-id (->RenameComponent target value)))
|
||||
|
||||
;; ---------------------------------------------------------------------------
|
||||
;; annotate-component — set or remove a component's annotation
|
||||
;;
|
||||
;; As the frontend's `update-component-annotation`: a nil annotation removes
|
||||
;; it.
|
||||
;; ---------------------------------------------------------------------------
|
||||
|
||||
(defrecord AnnotateComponent [target annotation]
|
||||
IComponentOperation
|
||||
(op-kind [_] :user)
|
||||
|
||||
tm/IOperation
|
||||
(apply-to [this situation]
|
||||
(let [the-file (tm/file situation)
|
||||
shape (tm/shape-by-id situation (tm/target-shape-id situation target))
|
||||
changes (-> (pcb/empty-changes nil)
|
||||
(pcb/with-library-data (:data the-file))
|
||||
(pcb/update-component (:component-id shape)
|
||||
#(when %
|
||||
(if (nil? annotation)
|
||||
(dissoc % :annotation)
|
||||
(assoc % :annotation annotation)))))
|
||||
file' (thf/apply-changes the-file changes)]
|
||||
(-> situation
|
||||
(tm/with-file file')
|
||||
(record-changes changes)
|
||||
(tm/record-application this {:target target :annotation annotation})))))
|
||||
|
||||
(defn annotate-component
|
||||
"Constructor for the annotate-component operation: set the annotation of
|
||||
the component of the main root `target` names, or remove it when nil."
|
||||
[target annotation]
|
||||
(tm/assign-id (->AnnotateComponent target annotation)))
|
||||
|
||||
(declare libraries)
|
||||
|
||||
(defrecord EditGrid [target action value]
|
||||
@ -1371,9 +1439,9 @@
|
||||
(tm/record-application this {:copy copy :into into :cell cell})))))
|
||||
|
||||
(defn nest-copy
|
||||
"Constructor for the nest-copy operation: move the copy head `copy` names
|
||||
under the shape `into` names, as its last child, or into the grid cell
|
||||
`cell` ([row column]) when given."
|
||||
"Constructor for the nest-copy operation: move the shape `copy` names (a
|
||||
copy head, or any shape a main holds) under the shape `into` names, as its
|
||||
last child, or into the grid cell `cell` ([row column]) when given."
|
||||
[copy into & {:keys [cell]}]
|
||||
(tm/assign-id (->NestCopy copy into cell)))
|
||||
|
||||
|
||||
@ -403,6 +403,15 @@
|
||||
:duplicate (and (top-copy shape) (< (count objects) (get situation :shape-budget 120))
|
||||
(cld/allow-duplicate? objects shape))
|
||||
:nest (and (top-copy shape) (not (overridden (:id shape))))
|
||||
;; a shape a main holds, other than its root: a plain shape
|
||||
;; or a nested copy head, which no copy around it holds
|
||||
:rename (and (ctk/main-instance? shape) (not (:variant-id shape))
|
||||
(live-component shape))
|
||||
:annotate (and (ctk/main-instance? shape) (live-component shape))
|
||||
:move (and (not (ctk/main-instance? shape))
|
||||
(or (nil? (:shape-ref shape)) (ctk/instance-head? shape))
|
||||
(not (ctn/has-any-copy-parent? objects shape))
|
||||
(some ctk/main-instance? (cfh/get-parents objects (:id shape))))
|
||||
:delete (ctk/main-instance? shape)
|
||||
(:swap :switch) (and (copy-head shape) (live-component shape))
|
||||
:variant-property (ctk/is-variant-container? shape)
|
||||
@ -465,6 +474,22 @@
|
||||
(not (ctn/invalid-structure-for-component?
|
||||
objects parent-shape [(:shape entry)] false libraries)))]
|
||||
(assoc entry :into (:path parent) :parent (:shape parent))))
|
||||
;; into a board or group of the same main, of another, or of the page,
|
||||
;; or to the page itself
|
||||
:move (vec (for [entry eligible
|
||||
:let [shape (:shape entry)
|
||||
own (set (cfh/get-children-ids-with-self objects (:id shape)))]
|
||||
parent (cons {:path [:root] :shape (get objects uuid/zero)} (tree situation))
|
||||
:let [parent-shape (:shape parent)]
|
||||
:when (and (#{:frame :group} (:type parent-shape))
|
||||
(not (contains? own (:id parent-shape)))
|
||||
(not= (:parent-id shape) (:id parent-shape))
|
||||
(not (ctk/in-component-copy? parent-shape))
|
||||
(not (ctn/has-any-copy-parent? objects parent-shape))
|
||||
(not (ctk/is-variant-container? parent-shape))
|
||||
(not (ctn/invalid-structure-for-component?
|
||||
objects parent-shape [shape] false libraries)))]
|
||||
(assoc entry :into (:path parent) :parent parent-shape)))
|
||||
eligible)))
|
||||
|
||||
(def library-source-kinds
|
||||
@ -612,6 +637,18 @@
|
||||
[{:align-self :end} {:justify-self :center} {:align-self :center :justify-self :end}
|
||||
{:align-self :auto :justify-self :auto}])
|
||||
|
||||
(def rename-values
|
||||
"Names the assets panel gives a component: the name typed, the main root's
|
||||
name, and the component's path and name."
|
||||
[["Group / Label" "Group / Label" "Group" "Label"]
|
||||
["Label" "Label" "" "Label"]
|
||||
[" Other/Label " "Other / Label" "Other" "Label"]
|
||||
["Group/Sub / Label" "Group / Sub / Label" "Group / Sub" "Label"]])
|
||||
|
||||
(def annotations
|
||||
"Annotations a component is given; nil removes it."
|
||||
["Note" "Another note" nil])
|
||||
|
||||
(def structure-edits
|
||||
"Structure actions: the action and, for a bool, its type."
|
||||
[[:group] [:mask] [:ungroup] [:unmask] [:bool :union] [:bool :difference]
|
||||
@ -848,7 +885,11 @@
|
||||
:unapply-token (n/unapply-token target token-name)
|
||||
:remove-child (n/remove-child target)
|
||||
:duplicate (n/duplicate target)
|
||||
:nest (n/nest-copy target (constantly (get-in selected [:parent :id])))
|
||||
(:nest :move) (n/nest-copy target (constantly (get-in selected [:parent :id])))
|
||||
:rename (n/rename-component target (first (nth rename-values (mod (or (nth tuple 3 nil) 0)
|
||||
(count rename-values)))))
|
||||
:annotate (n/annotate-component target (nth annotations (mod (or (nth tuple 3 nil) 0)
|
||||
(count annotations))))
|
||||
:delete (n/delete-component target)
|
||||
:swap (n/swap-shape target (constantly (get-in selected [:destination :shape :component-id])))
|
||||
:switch (n/switch-variant target (:variant-value selected) :pos (:variant-pos selected))
|
||||
@ -1080,6 +1121,11 @@
|
||||
:token-value (shadow/edit-token-value model objects token-name token-value)
|
||||
:update-main (shadow/update-main model objects (all-objects after) id)
|
||||
:detach (shadow/detach model objects (all-objects after) id)
|
||||
;; a move resets the shape's constraints to its new parent's defaults,
|
||||
;; which an override of them now holds
|
||||
:move (cond-> model
|
||||
(get-in model [id :constraints-group])
|
||||
(assoc-in [id :constraints-group] (shadow/values (get (all-objects after) id) :constraints-group)))
|
||||
:nest (let [parent (get objects (tm/target-shape-id before (:into op)))]
|
||||
(if (:blocked parent)
|
||||
(shadow/edit model shape :blocked true)
|
||||
@ -1328,6 +1374,29 @@
|
||||
(let [head (tm/shape-by-id after (get-in resolved [:shape :id]))]
|
||||
(when-not (= (get-in resolved [:destination :component-id]) (:component-id head))
|
||||
(oracle/fail :I9 {:phase :requested-component :kind kind}))))
|
||||
(when (#{:rename :annotate} kind)
|
||||
(let [head (tm/shape-by-id after (get-in resolved [:shape :id]))
|
||||
component (get-in (tm/file after) [:data :components (:component-id head)])]
|
||||
(if (= kind :rename)
|
||||
(let [[_ shape-name path name] (d/seek #(= (first %) (get-in resolved [:op :value])) rename-values)]
|
||||
(when-not (= [shape-name path name] [(:name head) (:path component) (:name component)])
|
||||
(oracle/fail :I9 {:phase :requested-rename :expected [shape-name path name]
|
||||
:actual [(:name head) (:path component) (:name component)]})))
|
||||
(when-not (= (get-in resolved [:op :annotation]) (:annotation component))
|
||||
(oracle/fail :I9 {:phase :requested-annotation :expected (get-in resolved [:op :annotation])
|
||||
:actual (:annotation component)})))))
|
||||
;; only an annotation edit, or its undo or redo, changes an annotation
|
||||
(when-not (#{:annotate :undo :redo :library-undo :library-redo} kind)
|
||||
(let [annotations (fn [situation]
|
||||
(into {} (for [file (cons (tm/file situation) (vals (tm/aux-files situation)))
|
||||
[id component] (get-in file [:data :components])]
|
||||
[[(:id file) id] (:annotation component)])))
|
||||
before (annotations situation)
|
||||
kept (annotations after)
|
||||
changed (filterv #(and (contains? kept %) (not= (get before %) (get kept %))) (keys before))]
|
||||
(when (seq changed)
|
||||
(oracle/fail :I9 {:phase :kept-annotation :components changed
|
||||
:before (select-keys before changed) :after (select-keys kept changed)}))))
|
||||
(when (#{:text :text-style :library-text :library-text-style} kind)
|
||||
(let [after (if library-id (source-context after library-id) after)
|
||||
actual (tm/shape-by-id after (get-in resolved [:shape :id]))
|
||||
@ -1585,7 +1654,7 @@
|
||||
(def structure-kinds
|
||||
[:edit :reset :update-main :detach :sync :undo :redo :text :text-style :add-child
|
||||
:remove-child :duplicate :nest :delete :restore :resize :rotate :style :transform
|
||||
:grid-direction :grid-cell])
|
||||
:grid-direction :grid-cell :move :rename :annotate])
|
||||
|
||||
(def structure-program-gen
|
||||
(gen/let [components (gen/choose 2 3)
|
||||
|
||||
@ -251,6 +251,14 @@
|
||||
(and free-head-placement (:component-id shape) (:shape-ref shape)
|
||||
(= 1 (count (ctn/get-parent-copy-heads objects shape)))))
|
||||
(into #{:x :y :points :selrect}))))
|
||||
;; a hidden layout child keeps the size its layout last gave
|
||||
;; it, which the layout gives again once it is shown
|
||||
(cond-> (and (untracked-position? objects shape)
|
||||
(or (#{:fill :auto} (:layout-item-h-sizing shape))
|
||||
(#{:fill :auto} (:layout-item-v-sizing shape))))
|
||||
(as-> snapshot (cond-> (dissoc snapshot :transform :transform-inverse)
|
||||
(#{:fill :auto} (:layout-item-h-sizing shape)) (dissoc :width)
|
||||
(#{:fill :auto} (:layout-item-v-sizing shape)) (dissoc :height))))
|
||||
(update :constraints-h #(or % (gsh/default-constraints-h shape)))
|
||||
(update :constraints-v #(or % (gsh/default-constraints-v shape)))
|
||||
;; a deleted component keeps its objects as stored, and sync
|
||||
@ -863,8 +871,12 @@
|
||||
(when-not (or (contains? touched :geometry-group) (#{:group :bool} (:type shape)))
|
||||
(let [expected (expected-geometry file container libraries shape
|
||||
#(cf/reference-libraries situation %))
|
||||
untracked? (untracked-position? (:objects page) shape)
|
||||
attrs (cond-> (geometry-differences (:objects page) shape expected)
|
||||
(untracked-position? (:objects page) shape) (disj :points :position))]
|
||||
untracked? (disj :points :position)
|
||||
;; nor is the size its layout gives it, which it gives again once shown
|
||||
(and untracked? (#{:fill :auto} (:layout-item-h-sizing shape))) (disj :width)
|
||||
(and untracked? (#{:fill :auto} (:layout-item-v-sizing shape))) (disj :height))]
|
||||
(when (seq attrs)
|
||||
(fail :I4 {:shape (:name shape) :group :geometry-group :attrs attrs
|
||||
:expected (select-keys expected [:width :height :points])
|
||||
|
||||
@ -4227,3 +4227,29 @@
|
||||
:rich-variants false :layout-children true :grid-empty false
|
||||
:layout-copies false :text false}]
|
||||
:ops [[:remove-child 7] [:grid-direction 13 0 7 1] [:grid-cell 0 0 0 1] [:update-main 17]]}))))
|
||||
|
||||
(t/deftest generated-moves-take-shapes-between-parents-of-mains
|
||||
;; a shape a main holds moves to the page, into another main, or within
|
||||
;; its own; copies follow on sync, and undo and redo restore it
|
||||
(t/is (map? (cg/run {:world [2 1 {:text true :layout nil}]
|
||||
:ops [[:move 0 0 0] [:sync] [:move 3 0 0] [:undo] [:redo]]})))
|
||||
(t/is (map? (cg/run {:world [2 1 {:text true :layout :flex :layout-children true}]
|
||||
:ops [[:add-child 0] [:move 5 0 0] [:move 11 0 0] [:sync]]}))))
|
||||
|
||||
(t/deftest generated-renames-give-a-component-a-path
|
||||
;; the assets panel's rename splits the normalized name into the
|
||||
;; component's path and name, and gives the main root the whole name
|
||||
(t/is (map? (cg/run {:world [2 1 {:text true :layout nil}]
|
||||
:ops [[:rename 0 0 0] [:rename 1 0 2] [:rename 0 0 3] [:undo] [:sync]]}))))
|
||||
|
||||
(t/deftest generated-annotations-survive-delete-and-restore
|
||||
;; only an annotation edit changes a component's annotation
|
||||
(t/is (map? (cg/run {:world [2 1 {:text true :layout nil}]
|
||||
:ops [[:annotate 0 0 0] [:delete 0] [:restore 0] [:annotate 1 0 2] [:undo] [:redo]]}))))
|
||||
|
||||
(t/deftest generated-a-hidden-fill-child-of-a-grid-copy-keeps-the-size-it-was-given
|
||||
;; a hidden layout child keeps the size its layout last gave it, which
|
||||
;; sync may give back to its main's: the layout gives it again once shown
|
||||
(t/is (map? (cg/run {:world [3 1 {:text true :text-styles false :layout :grid :layout-children true
|
||||
:layout-item-margins false :grid-square false :grid-empty false}]
|
||||
:ops [[:delete 8] [:style 0 29 1] [:style 4 1 3]]}))))
|
||||
|
||||
@ -340,6 +340,15 @@
|
||||
[(dwt/move-shapes-to-frame #{copy-id} parent-id index cell)]
|
||||
[(dwsh/relocate-shapes #{copy-id} parent-id index)]))
|
||||
|
||||
(instance? n/RenameComponent op)
|
||||
;; the assets panel's rename
|
||||
(let [shape (tm/shape-by-id situation (tm/target-shape-id situation (:target op)))]
|
||||
[(dwl/rename-component-and-main-instance (:component-id shape) (:value op))])
|
||||
|
||||
(instance? n/AnnotateComponent op)
|
||||
(let [shape (tm/shape-by-id situation (tm/target-shape-id situation (:target op)))]
|
||||
[(dw/update-component-annotation (:component-id shape) (:annotation op))])
|
||||
|
||||
(instance? n/RestoreComponent op)
|
||||
[(dwl/restore-component (:id (tm/file situation))
|
||||
(n/deleted-component-id situation (:deletion op)))]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user