From 9ca683026708e59d6518619c7880470cd610671e Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 30 Nov 2020 22:11:51 +0100 Subject: [PATCH] :sparkles: Add export macro helper for properly reexpor vars. --- common/app/common/data.cljc | 20 +++++++++ common/app/common/geom/shapes.cljc | 39 ++++++++---------- frontend/src/app/main/data/workspace.cljs | 50 +++++++++++------------ 3 files changed, 61 insertions(+), 48 deletions(-) diff --git a/common/app/common/data.cljc b/common/app/common/data.cljc index be573163b..b61d02c85 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,20 @@ 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] + (let [sym (symbol (name v)) + mdata (aapi/resolve &env v) + andsym (symbol "&") + arglists (second (get-in mdata [:meta :arglists]))] + (if (pos? (count arglists)) + `(def + ~(with-meta sym (:meta mdata)) + (fn ~@(for [args arglists] + (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)))) 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