mirror of
https://github.com/penpot/penpot.git
synced 2025-01-23 15:09:10 -05:00
✨ More tests for snap data
This commit is contained in:
parent
39334b81ac
commit
d9b1c0e2e6
3 changed files with 263 additions and 30 deletions
|
@ -568,22 +568,6 @@
|
|||
(dissoc :current-component-id)
|
||||
(update :parent-stack pop))))
|
||||
|
||||
(defn add-guide
|
||||
[file guide]
|
||||
|
||||
(let [guide (cond-> guide
|
||||
(nil? (:id guide))
|
||||
(assoc :id (uuid/next)))
|
||||
page-id (:current-page-id file)
|
||||
old-guides (or (get-in file [:data :pages-index page-id :options :guides]) {})
|
||||
new-guides (assoc old-guides (:id guide) guide)]
|
||||
(commit-change
|
||||
file
|
||||
{:type :set-option
|
||||
:page-id page-id
|
||||
:option :guides
|
||||
:value new-guides})))
|
||||
|
||||
(defn delete-object
|
||||
[file id]
|
||||
(let [page-id (:current-page-id file)]
|
||||
|
@ -593,7 +577,69 @@
|
|||
:page-id page-id
|
||||
:id id})))
|
||||
|
||||
(defn update-object
|
||||
[file old-obj new-obj]
|
||||
(let [page-id (:current-page-id file)
|
||||
new-obj (setup-selrect new-obj)
|
||||
attrs (d/concat-set (keys old-obj) (keys new-obj))
|
||||
generate-operation
|
||||
(fn [changes attr]
|
||||
(let [old-val (get old-obj attr)
|
||||
new-val (get new-obj attr)]
|
||||
(if (= old-val new-val)
|
||||
changes
|
||||
(conj changes {:type :set :attr attr :val new-val}))))]
|
||||
(-> file
|
||||
(commit-change
|
||||
{:type :mod-obj
|
||||
:operations (reduce generate-operation [] attrs)
|
||||
:page-id page-id
|
||||
:id (:id old-obj)}))))
|
||||
|
||||
(defn get-current-page
|
||||
[file]
|
||||
(let [page-id (:current-page-id file)]
|
||||
(-> file (get-in [:data :pages-index page-id]))))
|
||||
|
||||
(defn add-guide
|
||||
[file guide]
|
||||
|
||||
(let [guide (cond-> guide
|
||||
(nil? (:id guide))
|
||||
(assoc :id (uuid/next)))
|
||||
page-id (:current-page-id file)
|
||||
old-guides (or (get-in file [:data :pages-index page-id :options :guides]) {})
|
||||
new-guides (assoc old-guides (:id guide) guide)]
|
||||
(-> file
|
||||
(commit-change
|
||||
{:type :set-option
|
||||
:page-id page-id
|
||||
:option :guides
|
||||
:value new-guides})
|
||||
(assoc :last-id (:id guide)))))
|
||||
|
||||
(defn delete-guide
|
||||
[file id]
|
||||
|
||||
(let [page-id (:current-page-id file)
|
||||
old-guides (or (get-in file [:data :pages-index page-id :options :guides]) {})
|
||||
new-guides (dissoc old-guides id)]
|
||||
(-> file
|
||||
(commit-change
|
||||
{:type :set-option
|
||||
:page-id page-id
|
||||
:option :guides
|
||||
:value new-guides}))))
|
||||
|
||||
(defn update-guide
|
||||
[file guide]
|
||||
|
||||
(let [page-id (:current-page-id file)
|
||||
old-guides (or (get-in file [:data :pages-index page-id :options :guides]) {})
|
||||
new-guides (assoc old-guides (:id guide) guide)]
|
||||
(-> file
|
||||
(commit-change
|
||||
{:type :set-option
|
||||
:page-id page-id
|
||||
:option :guides
|
||||
:value new-guides}))))
|
||||
|
|
|
@ -102,7 +102,6 @@
|
|||
:type :guide
|
||||
:id (:id guide)
|
||||
:pt %)))]
|
||||
|
||||
(if-let [frame-id (:frame-id guide)]
|
||||
;; Guide inside frame, we add the information only on that frame
|
||||
(-> page-data
|
||||
|
@ -111,7 +110,7 @@
|
|||
|
||||
;; Guide outside the frame. We add the information in the global guides data
|
||||
(-> page-data
|
||||
(assoc-in [:guides :objects-data (:id guide)] [guide-data])
|
||||
(assoc-in [:guides :objects-data (:id guide)] guide-data)
|
||||
(update-in [:guides (:axis guide)] (make-insert-tree-data guide-data (:axis guide)))))))
|
||||
|
||||
(defn remove-frame
|
||||
|
|
|
@ -37,7 +37,6 @@
|
|||
:height 100}))
|
||||
page (fb/get-current-page file)
|
||||
|
||||
;; frame-id (:last-id file)
|
||||
data (-> (sd/make-snap-data)
|
||||
(sd/add-page page))
|
||||
|
||||
|
@ -217,8 +216,7 @@
|
|||
data (-> (sd/make-snap-data)
|
||||
(sd/add-page page))
|
||||
|
||||
file (-> file
|
||||
(fb/delete-object shape-id))
|
||||
file (fb/delete-object file shape-id)
|
||||
|
||||
new-page (fb/get-current-page file)
|
||||
data (sd/update-page data page new-page)
|
||||
|
@ -230,14 +228,204 @@
|
|||
(t/is (= (count result-x) 0))
|
||||
(t/is (= (count result-y) 0))))
|
||||
|
||||
(t/testing "Create shape inside frame, then remove it")
|
||||
(t/testing "Create guide then remove it")
|
||||
(t/testing "Create shape inside frame, then remove it"
|
||||
(let [file (-> (fb/create-file "Test")
|
||||
(fb/add-page {:name "Page-1"})
|
||||
(fb/add-artboard
|
||||
{:x 0
|
||||
:y 0
|
||||
:width 100
|
||||
:height 100}))
|
||||
frame-id (:last-id file)
|
||||
|
||||
(t/testing "Update frame coordinates")
|
||||
(t/testing "Update shape coordinates")
|
||||
(t/testing "Update shape inside frame coordinates")
|
||||
(t/testing "Update global guide")
|
||||
(t/testing "Update frame guide")
|
||||
file (fb/create-rect file {:x 25 :y 25 :width 50 :height 50})
|
||||
shape-id (:last-id file)
|
||||
|
||||
(t/testing "Change shape frame")
|
||||
(t/testing "Change guide frame"))
|
||||
file (fb/close-artboard file)
|
||||
|
||||
page (fb/get-current-page file)
|
||||
data (-> (sd/make-snap-data)
|
||||
(sd/add-page page))
|
||||
|
||||
file (fb/delete-object file shape-id)
|
||||
new-page (fb/get-current-page file)
|
||||
|
||||
data (sd/update-page data page new-page)
|
||||
|
||||
result-zero-x (sd/query data (:id page) uuid/zero :x [0 100])
|
||||
result-frame-x (sd/query data (:id page) frame-id :x [0 100])]
|
||||
|
||||
(t/is (some? data))
|
||||
(t/is (= (count result-zero-x) 3))
|
||||
(t/is (= (count result-frame-x) 3))))
|
||||
|
||||
(t/testing "Create global guide then remove it"
|
||||
(let [file (-> (fb/create-file "Test")
|
||||
(fb/add-page {:name "Page-1"})
|
||||
(fb/add-guide {:position 50 :axis :x}))
|
||||
|
||||
guide-id (:last-id file)
|
||||
|
||||
file (-> (fb/add-artboard file {:x 200 :y 200 :width 100 :height 100})
|
||||
(fb/close-artboard))
|
||||
|
||||
frame-id (:last-id file)
|
||||
page (fb/get-current-page file)
|
||||
data (-> (sd/make-snap-data) (sd/add-page page))
|
||||
|
||||
new-page (-> (fb/delete-guide file guide-id)
|
||||
(fb/get-current-page))
|
||||
|
||||
data (sd/update-page data page new-page)
|
||||
|
||||
result-zero-x (sd/query data (:id page) uuid/zero :x [0 100])
|
||||
result-zero-y (sd/query data (:id page) uuid/zero :y [0 100])
|
||||
result-frame-x (sd/query data (:id page) frame-id :x [0 100])
|
||||
result-frame-y (sd/query data (:id page) frame-id :y [0 100])]
|
||||
|
||||
(t/is (some? data))
|
||||
;; We can snap in the root
|
||||
(t/is (= (count result-zero-x) 0))
|
||||
(t/is (= (count result-zero-y) 0))
|
||||
|
||||
;; We can snap in the frame
|
||||
(t/is (= (count result-frame-x) 0))
|
||||
(t/is (= (count result-frame-y) 0))))
|
||||
|
||||
(t/testing "Create frame guide then remove it"
|
||||
(let [file (-> (fb/create-file "Test")
|
||||
(fb/add-page {:name "Page-1"})
|
||||
(fb/add-artboard {:x 200 :y 200 :width 100 :height 100})
|
||||
(fb/close-artboard))
|
||||
|
||||
frame-id (:last-id file)
|
||||
file (fb/add-guide file {:position 50 :axis :x :frame-id frame-id})
|
||||
guide-id (:last-id file)
|
||||
|
||||
page (fb/get-current-page file)
|
||||
data (-> (sd/make-snap-data) (sd/add-page page))
|
||||
|
||||
new-page (-> (fb/delete-guide file guide-id)
|
||||
(fb/get-current-page))
|
||||
|
||||
data (sd/update-page data page new-page)
|
||||
|
||||
result-zero-x (sd/query data (:id page) uuid/zero :x [0 100])
|
||||
result-zero-y (sd/query data (:id page) uuid/zero :y [0 100])
|
||||
result-frame-x (sd/query data (:id page) frame-id :x [0 100])
|
||||
result-frame-y (sd/query data (:id page) frame-id :y [0 100])]
|
||||
(t/is (some? data))
|
||||
;; We can snap in the root
|
||||
(t/is (= (count result-zero-x) 0))
|
||||
(t/is (= (count result-zero-y) 0))
|
||||
|
||||
;; We can snap in the frame
|
||||
(t/is (= (count result-frame-x) 0))
|
||||
(t/is (= (count result-frame-y) 0))))
|
||||
|
||||
(t/testing "Update frame coordinates"
|
||||
(let [file (-> (fb/create-file "Test")
|
||||
(fb/add-page {:name "Page-1"})
|
||||
(fb/add-artboard
|
||||
{:x 0
|
||||
:y 0
|
||||
:width 100
|
||||
:height 100})
|
||||
(fb/close-artboard))
|
||||
|
||||
frame-id (:last-id file)
|
||||
page (fb/get-current-page file)
|
||||
data (-> (sd/make-snap-data) (sd/add-page page))
|
||||
|
||||
frame (fb/lookup-shape file frame-id)
|
||||
new-frame (-> frame
|
||||
(assoc :x 200 :y 200))
|
||||
|
||||
file (fb/update-object file frame new-frame)
|
||||
new-page (fb/get-current-page file)
|
||||
|
||||
data (sd/update-page data page new-page)
|
||||
|
||||
result-zero-x-1 (sd/query data (:id page) uuid/zero :x [0 100])
|
||||
result-frame-x-1 (sd/query data (:id page) frame-id :x [0 100])
|
||||
result-zero-x-2 (sd/query data (:id page) uuid/zero :x [200 300])
|
||||
result-frame-x-2 (sd/query data (:id page) frame-id :x [200 300])]
|
||||
|
||||
(t/is (some? data))
|
||||
(t/is (= (count result-zero-x-1) 0))
|
||||
(t/is (= (count result-frame-x-1) 0))
|
||||
(t/is (= (count result-zero-x-2) 3))
|
||||
(t/is (= (count result-frame-x-2) 3))))
|
||||
|
||||
(t/testing "Update shape coordinates"
|
||||
(let [file (-> (fb/create-file "Test")
|
||||
(fb/add-page {:name "Page-1"})
|
||||
(fb/create-rect
|
||||
{:x 0
|
||||
:y 0
|
||||
:width 100
|
||||
:height 100}))
|
||||
|
||||
shape-id (:last-id file)
|
||||
page (fb/get-current-page file)
|
||||
data (-> (sd/make-snap-data) (sd/add-page page))
|
||||
|
||||
shape (fb/lookup-shape file shape-id)
|
||||
new-shape (-> shape
|
||||
(assoc :x 200 :y 200))
|
||||
|
||||
file (fb/update-object file shape new-shape)
|
||||
new-page (fb/get-current-page file)
|
||||
|
||||
data (sd/update-page data page new-page)
|
||||
|
||||
result-zero-x-1 (sd/query data (:id page) uuid/zero :x [0 100])
|
||||
result-zero-x-2 (sd/query data (:id page) uuid/zero :x [200 300])]
|
||||
|
||||
(t/is (some? data))
|
||||
(t/is (= (count result-zero-x-1) 0))
|
||||
(t/is (= (count result-zero-x-2) 3))))
|
||||
|
||||
(t/testing "Update global guide"
|
||||
(let [guide {:position 50 :axis :x}
|
||||
file (-> (fb/create-file "Test")
|
||||
(fb/add-page {:name "Page-1"})
|
||||
(fb/add-guide guide))
|
||||
|
||||
guide-id (:last-id file)
|
||||
guide (assoc guide :id guide-id)
|
||||
|
||||
file (-> (fb/add-artboard file {:x 500 :y 500 :width 100 :height 100})
|
||||
(fb/close-artboard))
|
||||
|
||||
frame-id (:last-id file)
|
||||
page (fb/get-current-page file)
|
||||
data (-> (sd/make-snap-data) (sd/add-page page))
|
||||
|
||||
new-page (-> (fb/update-guide file (assoc guide :position 150))
|
||||
(fb/get-current-page))
|
||||
|
||||
data (sd/update-page data page new-page)
|
||||
|
||||
result-zero-x-1 (sd/query data (:id page) uuid/zero :x [0 100])
|
||||
result-zero-y-1 (sd/query data (:id page) uuid/zero :y [0 100])
|
||||
result-frame-x-1 (sd/query data (:id page) frame-id :x [0 100])
|
||||
result-frame-y-1 (sd/query data (:id page) frame-id :y [0 100])
|
||||
|
||||
result-zero-x-2 (sd/query data (:id page) uuid/zero :x [0 200])
|
||||
result-zero-y-2 (sd/query data (:id page) uuid/zero :y [0 200])
|
||||
result-frame-x-2 (sd/query data (:id page) frame-id :x [0 200])
|
||||
result-frame-y-2 (sd/query data (:id page) frame-id :y [0 200])
|
||||
]
|
||||
|
||||
(t/is (some? data))
|
||||
|
||||
(t/is (= (count result-zero-x-1) 0))
|
||||
(t/is (= (count result-zero-y-1) 0))
|
||||
(t/is (= (count result-frame-x-1) 0))
|
||||
(t/is (= (count result-frame-y-1) 0))
|
||||
|
||||
(t/is (= (count result-zero-x-2) 1))
|
||||
(t/is (= (count result-zero-y-2) 0))
|
||||
(t/is (= (count result-frame-x-2) 1))
|
||||
(t/is (= (count result-frame-y-2) 0)))))
|
||||
|
|
Loading…
Add table
Reference in a new issue