From 9260c59afb5dac12f6b31801ef278fe6649a65f1 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 1 Dec 2020 13:53:36 +0100 Subject: [PATCH] :tada: Add improved export macro. This reverts commit 7ec30d1f5e155c37dcd6f69770a6c185ae2a21ca. --- backend/deps.edn | 1 + common/app/common/data.cljc | 41 +++++++++++++++++++ common/app/common/geom/shapes.cljc | 39 ++++++++---------- frontend/src/app/main/data/workspace.cljs | 50 +++++++++++------------ 4 files changed, 83 insertions(+), 48 deletions(-) diff --git a/backend/deps.edn b/backend/deps.edn index ce2109b50..8afc04fcd 100644 --- a/backend/deps.edn +++ b/backend/deps.edn @@ -4,6 +4,7 @@ "jcenter" {:url "https://jcenter.bintray.com/"}} :deps {org.clojure/clojure {:mvn/version "1.10.1"} + org.clojure/clojurescript {:mvn/version "1.10.773"} org.clojure/data.json {:mvn/version "1.0.0"} org.clojure/core.async {:mvn/version "1.3.610"} diff --git a/common/app/common/data.cljc b/common/app/common/data.cljc index be573163b..51e1f3a5a 100644 --- a/common/app/common/data.cljc +++ b/common/app/common/data.cljc @@ -7,10 +7,13 @@ (ns app.common.data "Data manipulation and query helper functions." (:refer-clojure :exclude [concat read-string hash-map]) + #?(:cljs + (:require-macros [app.common.data])) (:require [clojure.set :as set] [linked.set :as lks] [app.common.math :as mth] + #?(:clj [cljs.analyzer.api :as aapi]) #?(:cljs [cljs.reader :as r] :clj [clojure.edn :as r]) #?(:cljs [cljs.core :as core] @@ -281,3 +284,41 @@ valid and the number otherwise." [v] (if (or (not v) (mth/nan? v)) 0 v)) + + +(defmacro export + "A helper macro that allows reexport a var in a current namespace." + [v] + (if (boolean (:ns &env)) + + ;; Code for ClojureScript + (let [mdata (aapi/resolve &env v) + arglists (second (get-in mdata [:meta :arglists])) + sym (symbol (name v)) + andsym (symbol "&") + procarg #(if (= % andsym) % (gensym "param"))] + (if (pos? (count arglists)) + `(def + ~(with-meta sym (:meta mdata)) + (fn ~@(for [args arglists] + (let [args (map procarg args)] + (if (some #(= andsym %) args) + (let [[sargs dargs] (split-with #(not= andsym %) args)] + `([~@sargs ~@dargs] (apply ~v ~@sargs ~@(rest dargs)))) + `([~@args] (~v ~@args))))))) + `(def ~(with-meta sym (:meta mdata)) ~v))) + + ;; Code for Clojure + (let [vr (resolve v) + m (meta vr) + n (:name m) + n (with-meta n + (cond-> {} + (:dynamic m) (assoc :dynamic true) + (:protocol m) (assoc :protocol (:protocol m))))] + `(let [m# (meta ~vr)] + (def ~n (deref ~vr)) + (alter-meta! (var ~n) merge (dissoc m# :name)) + ;; (when (:macro m#) + ;; (.setMacro (var ~n))) + ~vr)))) diff --git a/common/app/common/geom/shapes.cljc b/common/app/common/geom/shapes.cljc index de210cdaa..10d7f6bb4 100644 --- a/common/app/common/geom/shapes.cljc +++ b/common/app/common/geom/shapes.cljc @@ -9,16 +9,16 @@ (ns app.common.geom.shapes (:require - [clojure.spec.alpha :as s] - [app.common.spec :as us] + [app.common.data :as d] [app.common.geom.matrix :as gmt] [app.common.geom.point :as gpt] [app.common.geom.shapes.common :as gco] - [app.common.geom.shapes.transforms :as gtr] - [app.common.geom.shapes.rect :as gpr] [app.common.geom.shapes.path :as gsp] + [app.common.geom.shapes.rect :as gpr] + [app.common.geom.shapes.transforms :as gtr] [app.common.math :as mth] - [app.common.data :as d])) + [app.common.spec :as us] + [clojure.spec.alpha :as s])) ;; --- Relative Movement @@ -252,22 +252,17 @@ ;; EXPORTS -(defn center-shape [shape] (gco/center-shape shape)) -(defn center-selrect [selrect] (gco/center-selrect selrect)) -(defn center-rect [rect] (gco/center-rect rect)) - -(defn rect->selrect [rect] (gpr/rect->selrect rect)) -(defn rect->points [rect] (gpr/rect->points rect)) -(defn points->selrect [points] (gpr/points->selrect points)) - -(defn transform-shape [shape] (gtr/transform-shape shape)) -(defn transform-matrix - ([shape] (gtr/transform-matrix shape)) - ([shape options] (gtr/transform-matrix shape options))) - -(defn transform-point-center [point center transform] (gtr/transform-point-center point center transform)) -(defn transform-rect [rect mtx] (gtr/transform-rect rect mtx)) +(d/export gco/center-shape) +(d/export gco/center-selrect) +(d/export gco/center-rect) +(d/export gpr/rect->selrect) +(d/export gpr/rect->points) +(d/export gpr/points->selrect) +(d/export gtr/transform-shape) +(d/export gtr/transform-matrix) +(d/export gtr/transform-point-center) +(d/export gtr/transform-rect) ;; PATHS -(defn content->points [content] (gsp/content->points content)) -(defn content->selrect [content] (gsp/content->selrect content)) +(d/export gsp/content->points) +(d/export gsp/content->selrect) diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index e66eb73d8..f85f2da3d 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -1576,38 +1576,36 @@ ;; Transform -(def start-rotate dwt/start-rotate) -(def start-resize dwt/start-resize) -(def start-move-selected dwt/start-move-selected) -(def move-selected dwt/move-selected) - -(def set-rotation dwt/set-rotation) -(def set-modifiers dwt/set-modifiers) -(def apply-modifiers dwt/apply-modifiers) +(d/export dwt/start-rotate) +(d/export dwt/start-resize) +(d/export dwt/start-move-selected) +(d/export dwt/move-selected) +(d/export dwt/set-rotation) +(d/export dwt/set-modifiers) +(d/export dwt/apply-modifiers) ;; Persistence -(def set-file-shared dwp/set-file-shared) -(def fetch-shared-files dwp/fetch-shared-files) -(def link-file-to-library dwp/link-file-to-library) -(def unlink-file-from-library dwp/unlink-file-from-library) -(def upload-media-objects dwp/upload-media-objects) +(d/export dwp/set-file-shared) +(d/export dwp/fetch-shared-files) +(d/export dwp/link-file-to-library) +(d/export dwp/unlink-file-from-library) +(d/export dwp/upload-media-objects) ;; Selection -(def select-shape dws/select-shape) -(def select-all dws/select-all) -(def deselect-all dws/deselect-all) -(def select-shapes dwc/select-shapes) -(def duplicate-selected dws/duplicate-selected) -(def handle-selection dws/handle-selection) -(def select-inside-group dws/select-inside-group) -(def select-for-drawing dwd/select-for-drawing) -(def clear-edition-mode dwc/clear-edition-mode) -(def add-shape dwc/add-shape) -(def start-edition-mode dwc/start-edition-mode) - -(defn start-path-edit [id] (dwdp/start-path-edit id)) +(d/export dws/select-shape) +(d/export dws/select-all) +(d/export dws/deselect-all) +(d/export dwc/select-shapes) +(d/export dws/duplicate-selected) +(d/export dws/handle-selection) +(d/export dws/select-inside-group) +(d/export dwd/select-for-drawing) +(d/export dwc/clear-edition-mode) +(d/export dwc/add-shape) +(d/export dwc/start-edition-mode) +(d/export dwdp/start-path-edit) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Shortcuts