From b4bc30e56f08d242014e9f1624874dca9801fa3a Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 24 Jan 2022 23:11:30 +0100 Subject: [PATCH] :sparkles: Improve shape specs and add missing specs for content. --- common/src/app/common/pages.cljc | 1 - common/src/app/common/pages/spec.cljc | 64 +++++++++++++++++------ frontend/src/app/main/data/workspace.cljs | 7 ++- 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/common/src/app/common/pages.cljc b/common/src/app/common/pages.cljc index 3df51409d..4c6248b86 100644 --- a/common/src/app/common/pages.cljc +++ b/common/src/app/common/pages.cljc @@ -95,7 +95,6 @@ (s/def ::color ::spec/color) (s/def ::data ::spec/data) (s/def ::media-object ::spec/media-object) -(s/def ::minimal-shape ::spec/minimal-shape) (s/def ::page ::spec/page) (s/def ::recent-color ::spec/recent-color) (s/def ::shape-attrs ::spec/shape-attrs) diff --git a/common/src/app/common/pages/spec.cljc b/common/src/app/common/pages/spec.cljc index cc74339ed..cb642c7fc 100644 --- a/common/src/app/common/pages/spec.cljc +++ b/common/src/app/common/pages/spec.cljc @@ -173,7 +173,6 @@ ;; Page Data related (s/def :internal.shape/blocked boolean?) (s/def :internal.shape/collapsed boolean?) -(s/def :internal.shape/content any?) (s/def :internal.shape/fill-color string?) (s/def :internal.shape/fill-opacity ::us/safe-number) @@ -277,17 +276,21 @@ :luminosity}) (s/def ::shape-attrs - (s/keys :opt-un [:internal.shape/selrect + (s/keys :req-un [::type ::name] + :opt-un [::id + ::component-id + ::component-file + ::component-root? + ::shape-ref + :internal.shape/selrect :internal.shape/points :internal.shape/blocked :internal.shape/collapsed - :internal.shape/content :internal.shape/fill-color :internal.shape/fill-opacity :internal.shape/fill-color-gradient :internal.shape/fill-color-ref-file :internal.shape/fill-color-ref-id - :internal.shape/hide-fill-on-export ;; only for frames :internal.shape/font-family :internal.shape/font-size :internal.shape/font-style @@ -332,22 +335,50 @@ :internal.shape/opacity :internal.shape/blend-mode])) +(s/def :internal.shape.text/type #{"root" "paragraph-set" "paragraph"}) +(s/def :internal.shape.text/children + (s/coll-of :internal.shape.text/content + :kind vector? + :min-count 1)) -;; shapes-group is handled differently +(s/def :internal.shape.text/text string?) +(s/def :internal.shape.text/key string?) -(s/def ::minimal-shape - (s/keys :req-un [::type ::name] - :opt-un [::id])) +(s/def :internal.shape.text/content + (s/or :container + (s/keys :req-un [:internal.shape.text/type + :internal.shape.text/children] + :opt-un [:internal.shape.text/key]) + :content + (s/keys :req-un [:internal.shape.text/text]))) -(s/def ::shape - (s/and ::minimal-shape - ::shape-attrs - (s/keys :opt-un [::id - ::component-id - ::component-file - ::component-root? - ::shape-ref]))) +(s/def :internal.shape.path/command keyword?) +(s/def :internal.shape.path/params (s/map-of keyword? any?)) +(s/def :internal.shape.path/command-item + (s/keys :req-un [:internal.shape.path/command + :internal.shape.path/params])) +(s/def :internal.shape.path/content + (s/coll-of :internal.shape.path/command-item :kind vector?)) + +(defmulti shape-spec :type) + +(defmethod shape-spec :default [_] + (s/spec ::shape-attrs)) + +(defmethod shape-spec :text [_] + (s/and ::shape-attrs + (s/keys :opt-un [:internal.shape.text/content]))) + +(defmethod shape-spec :path [_] + (s/and ::shape-attrs + (s/keys :opt-un [:internal.shape.path/content]))) + +(defmethod shape-spec :frame [_] + (s/and ::shape-attrs + (s/keys :opt-un [:internal.shape/hide-fill-on-export]))) + +(s/def ::shape (s/multi-spec shape-spec :type)) (s/def :internal.page/objects (s/map-of uuid? ::shape)) (s/def ::page @@ -356,7 +387,6 @@ ::cto/options :internal.page/objects])) - (s/def ::recent-color (s/keys :opt-un [:internal.color/value :internal.color/color diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 292693c6a..2cb718f2a 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -1243,7 +1243,7 @@ (defn update-shape-flags [ids {:keys [blocked hidden] :as flags}] (us/verify (s/coll-of ::us/uuid) ids) - (s/assert ::shape-attrs flags) + (us/assert ::shape-attrs flags) (ptk/reify ::update-shape-flags ptk/WatchEvent (watch [_ state _] @@ -1445,7 +1445,6 @@ (defn show-shape-context-menu [{:keys [shape] :as params}] - (us/verify (s/nilable ::cp/minimal-shape) shape) (ptk/reify ::show-shape-context-menu ptk/WatchEvent (watch [_ state _] @@ -1854,7 +1853,7 @@ (defn paste-text [text] - (s/assert string? text) + (us/assert string? text) (ptk/reify ::paste-text ptk/WatchEvent (watch [_ state _] @@ -1883,7 +1882,7 @@ (defn- paste-svg [text] - (s/assert string? text) + (us/assert string? text) (ptk/reify ::paste-svg ptk/WatchEvent (watch [_ state _]