From 3f1430890887dcc7063a065249d6063e2bf7d097 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 20 Jun 2023 12:54:18 +0200 Subject: [PATCH] :sparkles: Move fressian and transit impl for geom objects to respective nss --- common/src/app/common/fressian.clj | 40 ------------------ common/src/app/common/geom/matrix.cljc | 41 ++++++++++++++++++- common/src/app/common/geom/point.cljc | 23 ++++++++++- .../app/common/geom/shapes/transforms.cljc | 6 +-- common/src/app/common/transit.cljc | 21 ---------- 5 files changed, 63 insertions(+), 68 deletions(-) diff --git a/common/src/app/common/fressian.clj b/common/src/app/common/fressian.clj index f42f9130e..282620698 100644 --- a/common/src/app/common/fressian.clj +++ b/common/src/app/common/fressian.clj @@ -7,12 +7,8 @@ (ns app.common.fressian (:require [app.common.data :as d] - [app.common.geom.matrix :as gmt] - [app.common.geom.point :as gpt] [clojure.data.fressian :as fres]) (:import - app.common.geom.matrix.Matrix - app.common.geom.point.Point clojure.lang.Ratio java.io.ByteArrayInputStream java.io.ByteArrayOutputStream @@ -297,39 +293,3 @@ [data] (with-open [^ByteArrayInputStream input (ByteArrayInputStream. ^bytes data)] (-> input reader read!))) - -;; --- ADDITIONAL - -(add-handlers! - {:name "penpot/point" - :class app.common.geom.point.Point - :wfn (fn [n w ^Point o] - (write-tag! w n 1) - (write-list! w (List/of (.-x o) (.-y o)))) - :rfn (fn [^Reader rdr] - (let [^List x (read-object! rdr)] - (Point. (.get x 0) (.get x 1))))} - - {:name "penpot/matrix" - :class app.common.geom.matrix.Matrix - :wfn (fn [^String n ^Writer w o] - (write-tag! w n 1) - (write-list! w (List/of (.-a ^Matrix o) - (.-b ^Matrix o) - (.-c ^Matrix o) - (.-d ^Matrix o) - (.-e ^Matrix o) - (.-f ^Matrix o)))) - :rfn (fn [^Reader rdr] - (let [^List x (read-object! rdr)] - (Matrix. (.get x 0) (.get x 1) (.get x 2) (.get x 3) (.get x 4) (.get x 5))))}) - - -;; Backward compatibility for 1.19 with v1.20; - -(add-handlers! - {:name "penpot/geom/rect" - :rfn read-map-like} - {:name "penpot/shape" - :rfn read-map-like}) - diff --git a/common/src/app/common/geom/matrix.cljc b/common/src/app/common/geom/matrix.cljc index c298154b0..d755580c3 100644 --- a/common/src/app/common/geom/matrix.cljc +++ b/common/src/app/common/geom/matrix.cljc @@ -8,6 +8,7 @@ (:require #?(:cljs [cljs.pprint :as pp] :clj [clojure.pprint :as pp]) + #?(:clj [app.common.fressian :as fres]) [app.common.data :as d] [app.common.data.macros :as dm] [app.common.geom.point :as gpt] @@ -16,7 +17,12 @@ [app.common.schema.generators :as sg] [app.common.schema.openapi :as-alias oapi] [app.common.spec :as us] - [clojure.spec.alpha :as s])) + [app.common.transit :as t] + [clojure.spec.alpha :as s]) + #?(:clj + (:import + java.util.List))) + (def precision 6) @@ -376,3 +382,36 @@ (mth/almost-zero? b) (mth/almost-zero? c) (mth/almost-zero? (- d 1)))) + +#?(:clj + (fres/add-handlers! + {:name "penpot/matrix" + :class Matrix + :wfn (fn [n w o] + (fres/write-tag! w n 1) + (fres/write-list! w (List/of (.-a ^Matrix o) + (.-b ^Matrix o) + (.-c ^Matrix o) + (.-d ^Matrix o) + (.-e ^Matrix o) + (.-f ^Matrix o)))) + :rfn (fn [rdr] + (let [^List x (fres/read-object! rdr)] + (map->Matrix {:a (.get x 0) + :b (.get x 1) + :c (.get x 2) + :d (.get x 3) + :e (.get x 4) + :f (.get x 5)})))})) + +(t/add-handlers! + {:id "matrix" + :class Matrix + :wfn #(into {} %) + :rfn (fn [m] + (map->Matrix {:a (get m :a) + :b (get m :b) + :c (get m :c) + :d (get m :d) + :e (get m :e) + :f (get m :f)}))}) diff --git a/common/src/app/common/geom/point.cljc b/common/src/app/common/geom/point.cljc index 6f9f36fdb..71f4a8014 100644 --- a/common/src/app/common/geom/point.cljc +++ b/common/src/app/common/geom/point.cljc @@ -11,6 +11,7 @@ :clj [clojure.pprint :as pp]) #?(:cljs [cljs.core :as c] :clj [clojure.core :as c]) + #?(:clj [app.common.fressian :as fres]) [app.common.data :as d] [app.common.data.macros :as dm] [app.common.exceptions :as ex] @@ -19,8 +20,12 @@ [app.common.schema.generators :as sg] [app.common.schema.openapi :as-alias oapi] [app.common.spec :as us] + [app.common.transit :as t] [clojure.spec.alpha :as s] - [cuerdas.core :as str])) + [cuerdas.core :as str]) + #?(:clj + (:import + java.util.List))) ;; --- Point Impl @@ -466,3 +471,19 @@ (defmethod pp/simple-dispatch Point [obj] (pr obj)) +#?(:clj + (fres/add-handlers! + {:name "penpot/point" + :class Point + :wfn (fn [n w ^Point o] + (fres/write-tag! w n 1) + (fres/write-list! w (List/of (.-x o) (.-y o)))) + :rfn (fn [rdr] + (let [^List x (fres/read-object! rdr)] + (pos->Point (.get x 0) (.get x 1))))})) + +(t/add-handlers! + {:id "point" + :class Point + :wfn #(into {} %) + :rfn map->Point}) diff --git a/common/src/app/common/geom/shapes/transforms.cljc b/common/src/app/common/geom/shapes/transforms.cljc index 2399c1a7c..aa8f3be9b 100644 --- a/common/src/app/common/geom/shapes/transforms.cljc +++ b/common/src/app/common/geom/shapes/transforms.cljc @@ -51,11 +51,7 @@ (if ^boolean (d/num? x) (+ dx x) x) (if ^boolean (d/num? y) (+ dy y) y) w - h - (if ^boolean (d/num? x1) (+ dx x1) x1) - (if ^boolean (d/num? y1) (+ dy y1) y1) - (if ^boolean (d/num? x2) (+ dx x2) x2) - (if ^boolean (d/num? y2) (+ dy y2) y2))) + h)) selrect)) (defn- move-points diff --git a/common/src/app/common/transit.cljc b/common/src/app/common/transit.cljc index 9d698cd3a..057a1593d 100644 --- a/common/src/app/common/transit.cljc +++ b/common/src/app/common/transit.cljc @@ -7,8 +7,6 @@ (ns app.common.transit (:require [app.common.data :as d] - [app.common.geom.matrix :as gmt] - [app.common.geom.point :as gpt] [app.common.uri :as uri] [cognitect.transit :as t] [lambdaisland.uri :as luri] @@ -18,8 +16,6 @@ #?(:cljs ["luxon" :as lxn])) #?(:clj (:import - app.common.geom.matrix.Matrix - app.common.geom.point.Point java.io.ByteArrayInputStream java.io.ByteArrayOutputStream java.io.File @@ -122,23 +118,6 @@ {:id "u" :rfn parse-uuid}) - {:id "point" - :class #?(:clj Point :cljs gpt/Point) - :wfn #(into {} %) - :rfn gpt/map->Point} - - {:id "matrix" - :class #?(:clj Matrix :cljs gmt/Matrix) - :wfn #(into {} %) - :rfn #?(:cljs gmt/map->Matrix - :clj (fn [{:keys [a b c d e f]}] - (gmt/matrix (double a) - (double b) - (double c) - (double d) - (double e) - (double f))))} - {:id "ordered-set" :class #?(:clj LinkedSet :cljs lks/LinkedSet) :wfn vec