mirror of
https://github.com/penpot/penpot.git
synced 2025-06-05 20:32:59 +02:00
♻️ Redone the snap calculation and added guides
This commit is contained in:
parent
0766938f98
commit
64e7cad292
11 changed files with 726 additions and 185 deletions
243
frontend/test/app/util/snap_data_test.cljs
Normal file
243
frontend/test/app/util/snap_data_test.cljs
Normal file
|
@ -0,0 +1,243 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) UXBOX Labs SL
|
||||
|
||||
(ns app.util.snap-data-test
|
||||
(:require
|
||||
[app.common.uuid :as uuid]
|
||||
[cljs.test :as t :include-macros true]
|
||||
[cljs.pprint :refer [pprint]]
|
||||
[app.common.pages.init :as init]
|
||||
[app.common.file-builder :as fb]
|
||||
[app.util.snap-data :as sd]))
|
||||
|
||||
(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")
|
||||
(fb/add-page {:name "Page-1"})
|
||||
(fb/get-current-page))
|
||||
|
||||
data (-> (sd/make-snap-data)
|
||||
(sd/add-page page))]
|
||||
(t/is (some? data))))
|
||||
|
||||
(t/testing "Create simple shape on root"
|
||||
(let [file (-> (fb/create-file "Test")
|
||||
(fb/add-page {:name "Page-1"})
|
||||
(fb/create-rect
|
||||
{:x 0
|
||||
:y 0
|
||||
:width 100
|
||||
:height 100}))
|
||||
page (fb/get-current-page file)
|
||||
|
||||
;; frame-id (:last-id file)
|
||||
data (-> (sd/make-snap-data)
|
||||
(sd/add-page page))
|
||||
|
||||
result-x (sd/query data (:id page) uuid/zero :x [0 100])]
|
||||
|
||||
(t/is (some? data))
|
||||
|
||||
;; 3 = left side, center and right side
|
||||
(t/is (= (count result-x) 3))
|
||||
|
||||
;; Left side: two points
|
||||
(t/is (= (first (nth result-x 0)) 0))
|
||||
|
||||
;; Center one point
|
||||
(t/is (= (first (nth result-x 1)) 50))
|
||||
|
||||
;; Right side two points
|
||||
(t/is (= (first (nth result-x 2)) 100))))
|
||||
|
||||
(t/testing "Add page with single empty frame"
|
||||
(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)
|
||||
|
||||
;; frame-id (:last-id file)
|
||||
data (-> (sd/make-snap-data)
|
||||
(sd/add-page 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 "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)
|
||||
|
||||
file (-> file
|
||||
(fb/create-rect
|
||||
{:x 25
|
||||
:y 25
|
||||
:width 50
|
||||
:height 50})
|
||||
(fb/close-artboard))
|
||||
|
||||
page (fb/get-current-page file)
|
||||
|
||||
;; frame-id (:last-id file)
|
||||
data (-> (sd/make-snap-data)
|
||||
(sd/add-page 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) 5))))
|
||||
|
||||
(t/testing "Add a global guide"
|
||||
(let [file (-> (fb/create-file "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)
|
||||
page (fb/get-current-page file)
|
||||
|
||||
;; frame-id (:last-id file)
|
||||
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) 1))
|
||||
(t/is (= (count result-zero-y) 0))
|
||||
|
||||
;; We can snap in the frame
|
||||
(t/is (= (count result-frame-x) 1))
|
||||
(t/is (= (count result-frame-y) 0))))
|
||||
|
||||
(t/testing "Add a frame guide"
|
||||
(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 (-> 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))
|
||||
|
||||
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) 1))
|
||||
(t/is (= (count result-frame-y) 0)))))
|
||||
|
||||
(t/deftest test-update-index
|
||||
(t/testing "Create frame on root and 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})
|
||||
(fb/close-artboard))
|
||||
|
||||
shape-id (:last-id file)
|
||||
page (fb/get-current-page file)
|
||||
|
||||
;; frame-id (:last-id file)
|
||||
data (-> (sd/make-snap-data)
|
||||
(sd/add-page page))
|
||||
|
||||
file (-> file
|
||||
(fb/delete-object shape-id))
|
||||
|
||||
new-page (fb/get-current-page file)
|
||||
data (sd/update-page data page new-page)
|
||||
|
||||
result-x (sd/query data (:id page) uuid/zero :x [0 100])
|
||||
result-y (sd/query data (:id page) uuid/zero :y [0 100])]
|
||||
|
||||
(t/is (some? data))
|
||||
(t/is (= (count result-x) 0))
|
||||
(t/is (= (count result-y) 0))))
|
||||
|
||||
(t/testing "Create simple shape on root. Then remove it"
|
||||
(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)
|
||||
|
||||
;; frame-id (:last-id file)
|
||||
data (-> (sd/make-snap-data)
|
||||
(sd/add-page page))
|
||||
|
||||
file (-> file
|
||||
(fb/delete-object shape-id))
|
||||
|
||||
new-page (fb/get-current-page file)
|
||||
data (sd/update-page data page new-page)
|
||||
|
||||
result-x (sd/query data (:id page) uuid/zero :x [0 100])
|
||||
result-y (sd/query data (:id page) uuid/zero :y [0 100])]
|
||||
|
||||
(t/is (some? data))
|
||||
(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 "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")
|
||||
|
||||
(t/testing "Change shape frame")
|
||||
(t/testing "Change guide frame"))
|
Loading…
Add table
Add a link
Reference in a new issue