♻️ Refactor penpot library

This commit is contained in:
Andrey Antukh 2025-05-08 09:51:25 +02:00
parent 8bdec66927
commit 0b7b6e2c23
18 changed files with 817 additions and 1034 deletions

View file

@ -13,13 +13,21 @@
[cljs.pprint :refer [pprint]]
[cljs.test :as t :include-macros true]))
(def uuid-counter 1)
(defn get-mocked-uuid
[]
(let [counter (atom 0)]
(fn []
(uuid/custom 123456789 (swap! counter inc)))))
(t/deftest test-create-index
(t/testing "Create empty data"
(let [data (sd/make-snap-data)]
(t/is (some? data))))
(t/testing "Add empty page (only root-frame)"
(let [page (-> (fb/create-file "Test")
(let [page (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/get-current-page))
@ -28,10 +36,11 @@
(t/is (some? data))))
(t/testing "Create simple shape on root"
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/create-rect
{:x 0
(fb/add-shape
{:type :rect
:x 0
:y 0
:width 100
:height 100}))
@ -57,7 +66,7 @@
(t/is (= (first (nth result-x 2)) 100))))
(t/testing "Add page with single empty frame"
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/add-artboard
{:x 0
@ -66,10 +75,10 @@
:height 100})
(fb/close-artboard))
frame-id (:last-id file)
frame-id (::fb/last-id file)
page (fb/get-current-page file)
;; frame-id (:last-id file)
;; frame-id (::fb/last-id file)
data (-> (sd/make-snap-data)
(sd/add-page page))
@ -81,47 +90,49 @@
(t/is (= (count result-frame-x) 3))))
(t/testing "Add page with some shapes inside frames"
(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)
(with-redefs [uuid/next (get-mocked-uuid)]
(let [file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/add-artboard
{:x 0
:y 0
:width 100
:height 100}))
file (-> file
(fb/create-rect
{:x 25
:y 25
:width 50
:height 50})
(fb/close-artboard))
frame-id (::fb/last-id file)
page (fb/get-current-page file)
file (-> file
(fb/add-shape
{:type :rect
:x 25
:y 25
:width 50
:height 50})
(fb/close-artboard))
;; frame-id (:last-id file)
data (-> (sd/make-snap-data)
(sd/add-page page))
page (fb/get-current-page file)
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])]
data (-> (sd/make-snap-data)
(sd/add-page page))
(t/is (some? data))
(t/is (= (count result-zero-x) 3))
(t/is (= (count result-frame-x) 5))))
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) 5)))))
(t/testing "Add a global guide"
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/add-guide {:position 50 :axis :x})
(fb/add-artboard {:x 200 :y 200 :width 100 :height 100})
(fb/close-artboard))
frame-id (:last-id file)
frame-id (::fb/last-id file)
page (fb/get-current-page file)
;; frame-id (:last-id file)
;; frame-id (::fb/last-id file)
data (-> (sd/make-snap-data)
(sd/add-page page))
@ -140,26 +151,26 @@
(t/is (= (count result-frame-y) 0))))
(t/testing "Add a frame guide"
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "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)
frame-id (::fb/last-id file)
file (-> file
(fb/add-guide {:position 50 :axis :x :frame-id frame-id}))
page (fb/get-current-page file)
;; frame-id (:last-id file)
data (-> (sd/make-snap-data)
(sd/add-page page))
data (-> (sd/make-snap-data)
(sd/add-page 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))
@ -171,7 +182,7 @@
(t/deftest test-update-index
(t/testing "Create frame on root and then remove it."
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/add-artboard
{:x 0
@ -180,15 +191,15 @@
:height 100})
(fb/close-artboard))
shape-id (:last-id file)
shape-id (::fb/last-id file)
page (fb/get-current-page file)
;; frame-id (:last-id file)
;; frame-id (::fb/last-id file)
data (-> (sd/make-snap-data)
(sd/add-page page))
file (-> file
(fb/delete-object shape-id))
(fb/delete-shape shape-id))
new-page (fb/get-current-page file)
data (sd/update-page data page new-page)
@ -201,22 +212,23 @@
(t/is (= (count result-y) 0))))
(t/testing "Create simple shape on root. Then remove it"
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/create-rect
{:x 0
(fb/add-shape
{:type :rect
:x 0
:y 0
:width 100
:height 100}))
shape-id (:last-id file)
shape-id (::fb/last-id file)
page (fb/get-current-page file)
;; frame-id (:last-id file)
;; frame-id (::fb/last-id file)
data (-> (sd/make-snap-data)
(sd/add-page page))
file (fb/delete-object file shape-id)
file (fb/delete-shape file shape-id)
new-page (fb/get-current-page file)
data (sd/update-page data page new-page)
@ -229,17 +241,17 @@
(t/is (= (count result-y) 0))))
(t/testing "Create shape inside frame, then remove it"
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/add-artboard
{:x 0
:y 0
:width 100
:height 100}))
frame-id (:last-id file)
frame-id (::fb/last-id file)
file (fb/create-rect file {:x 25 :y 25 :width 50 :height 50})
shape-id (:last-id file)
file (fb/add-shape file {:type :rect :x 25 :y 25 :width 50 :height 50})
shape-id (::fb/last-id file)
file (fb/close-artboard file)
@ -247,7 +259,7 @@
data (-> (sd/make-snap-data)
(sd/add-page page))
file (fb/delete-object file shape-id)
file (fb/delete-shape file shape-id)
new-page (fb/get-current-page file)
data (sd/update-page data page new-page)
@ -260,16 +272,16 @@
(t/is (= (count result-frame-x) 3))))
(t/testing "Create global guide then remove it"
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/add-guide {:position 50 :axis :x}))
guide-id (:last-id file)
guide-id (::fb/last-id file)
file (-> (fb/add-artboard file {:x 200 :y 200 :width 100 :height 100})
(fb/close-artboard))
frame-id (:last-id file)
frame-id (::fb/last-id file)
page (fb/get-current-page file)
data (-> (sd/make-snap-data) (sd/add-page page))
@ -293,14 +305,14 @@
(t/is (= (count result-frame-y) 0))))
(t/testing "Create frame guide then remove it"
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "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)
frame-id (::fb/last-id file)
file (fb/add-guide file {:position 50 :axis :x :frame-id frame-id})
guide-id (:last-id file)
guide-id (::fb/last-id file)
page (fb/get-current-page file)
data (-> (sd/make-snap-data) (sd/add-page page))
@ -324,7 +336,7 @@
(t/is (= (count result-frame-y) 0))))
(t/testing "Update frame coordinates"
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/add-artboard
{:x 0
@ -333,17 +345,18 @@
:height 100})
(fb/close-artboard))
frame-id (:last-id file)
frame-id (::fb/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
(dissoc :selrect :points)
(assoc :x 200 :y 200)
(cts/setup-shape))
file (fb/update-shape file frame-id
(fn [shape]
(-> shape
(dissoc :selrect :points)
(assoc :x 200 :y 200)
(cts/setup-shape))))
file (fb/update-object file frame new-frame)
new-page (fb/get-current-page file)
data (sd/update-page data page new-page)
@ -360,27 +373,30 @@
(t/is (= (count result-frame-x-2) 3))))
(t/testing "Update shape coordinates"
(let [file (-> (fb/create-file "Test")
(let [file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/create-rect
{:x 0
(fb/add-shape
{:type :rect
:x 0
:y 0
:width 100
:height 100}))
shape-id (:last-id file)
shape-id (::fb/last-id file)
page (fb/get-current-page file)
data (-> (sd/make-snap-data) (sd/add-page page))
data (-> (sd/make-snap-data)
(sd/add-page page))
shape (fb/lookup-shape file shape-id)
new-shape (-> shape
(dissoc :selrect :points)
(assoc :x 200 :y 200))
file (fb/update-shape file shape-id
(fn [shape]
(-> shape
(dissoc :selrect :points)
(assoc :x 200 :y 200)
(cts/setup-shape))))
file (fb/update-object file shape new-shape)
new-page (fb/get-current-page file)
data (sd/update-page data page new-page)
;; FIXME: update
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])]
@ -391,17 +407,17 @@
(t/testing "Update global guide"
(let [guide {:position 50 :axis :x}
file (-> (fb/create-file "Test")
file (-> (fb/create-file {:name "Test"})
(fb/add-page {:name "Page 1"})
(fb/add-guide guide))
guide-id (:last-id file)
guide-id (::fb/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)
frame-id (::fb/last-id file)
page (fb/get-current-page file)
data (-> (sd/make-snap-data) (sd/add-page page))