mirror of
https://github.com/penpot/penpot.git
synced 2025-02-13 10:38:13 -05:00
♻️ Replace frontend transit ns usage with common transit.
This commit is contained in:
parent
2ec769981a
commit
068c94da4e
18 changed files with 43 additions and 179 deletions
|
@ -24,7 +24,6 @@
|
|||
[app.main.worker :as uw]
|
||||
[app.util.router :as rt]
|
||||
[app.util.timers :as ts]
|
||||
[app.util.transit :as t]
|
||||
[app.util.webapi :as wapi]
|
||||
[beicon.core :as rx]
|
||||
[cljs.spec.alpha :as s]
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
[app.common.pages :as cp]
|
||||
[app.common.pages.helpers :as cph]
|
||||
[app.common.spec :as us]
|
||||
[app.common.transit :as t]
|
||||
[app.common.uuid :as uuid]
|
||||
[app.config :as cfg]
|
||||
[app.main.data.messages :as dm]
|
||||
|
@ -39,7 +40,6 @@
|
|||
[app.util.i18n :as i18n]
|
||||
[app.util.logging :as log]
|
||||
[app.util.router :as rt]
|
||||
[app.util.transit :as t]
|
||||
[app.util.webapi :as wapi]
|
||||
[beicon.core :as rx]
|
||||
[cljs.spec.alpha :as s]
|
||||
|
@ -1400,7 +1400,7 @@
|
|||
(rx/merge-map (partial prepare-object objects selected))
|
||||
(rx/reduce collect-data initial)
|
||||
(rx/mapcat (partial sort-selected state))
|
||||
(rx/map t/encode)
|
||||
(rx/map t/encode-str)
|
||||
(rx/map wapi/write-to-clipboard)
|
||||
(rx/catch on-copy-error)
|
||||
(rx/ignore)))))))
|
||||
|
@ -1420,7 +1420,7 @@
|
|||
paste-transit-str
|
||||
(->> clipboard-str
|
||||
(rx/filter t/transit?)
|
||||
(rx/map t/decode)
|
||||
(rx/map t/decode-str)
|
||||
(rx/filter #(= :copied-shapes (:type %)))
|
||||
(rx/map #(select-keys % [:selected :objects]))
|
||||
(rx/map paste-shape))
|
||||
|
@ -1459,7 +1459,7 @@
|
|||
image-data (wapi/extract-images paste-data)
|
||||
text-data (wapi/extract-text paste-data)
|
||||
decoded-data (and (t/transit? text-data)
|
||||
(t/decode text-data))
|
||||
(t/decode-str text-data))
|
||||
|
||||
edit-id (get-in state [:workspace-local :edition])
|
||||
is-editing-text? (and edit-id (= :text (get-in objects [edit-id :type])))]
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
[app.common.geom.point :as gpt]
|
||||
[app.common.pages :as cp]
|
||||
[app.common.spec :as us]
|
||||
[app.common.transit :as t]
|
||||
[app.common.uri :as u]
|
||||
[app.config :as cf]
|
||||
[app.main.data.workspace.changes :as dch]
|
||||
|
@ -21,7 +22,6 @@
|
|||
[app.util.avatars :as avatars]
|
||||
[app.util.i18n :as i18n :refer [tr]]
|
||||
[app.util.time :as dt]
|
||||
[app.util.transit :as t]
|
||||
[app.util.websockets :as ws]
|
||||
[beicon.core :as rx]
|
||||
[cljs.spec.alpha :as s]
|
||||
|
@ -74,7 +74,7 @@
|
|||
;; Process all incoming messages.
|
||||
(->> (ws/-stream wsession)
|
||||
(rx/filter ws/message?)
|
||||
(rx/map (comp t/decode :payload))
|
||||
(rx/map (comp t/decode-str :payload))
|
||||
(rx/filter #(s/valid? ::message %))
|
||||
(rx/map process-message))
|
||||
|
||||
|
|
|
@ -32,7 +32,6 @@
|
|||
[app.util.object :as obj]
|
||||
[app.util.router :as rt]
|
||||
[app.util.time :as dt]
|
||||
[app.util.transit :as t]
|
||||
[app.util.uri :as uu]
|
||||
[beicon.core :as rx]
|
||||
[cljs.spec.alpha :as s]
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
[app.config :as cfg]
|
||||
[app.util.http :as http]
|
||||
[app.util.time :as dt]
|
||||
[app.util.transit :as t]
|
||||
[beicon.core :as rx]
|
||||
[cuerdas.core :as str]))
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
[app.util.logging :as log]
|
||||
[app.util.object :as obj]
|
||||
[app.util.timers :as ts]
|
||||
[app.util.transit :as t]
|
||||
[app.util.webapi :as wapi]
|
||||
[beicon.core :as rx]
|
||||
[cljs.spec.alpha :as s]
|
||||
|
|
|
@ -7,9 +7,9 @@
|
|||
(ns app.util.dom.dnd
|
||||
"Drag & Drop interop helpers."
|
||||
(:require
|
||||
[cuerdas.core :as str]
|
||||
[app.common.transit :as t]
|
||||
[app.util.data :refer (read-string)]
|
||||
[app.util.transit :as t]))
|
||||
[cuerdas.core :as str]))
|
||||
|
||||
;; This is the official documentation for the dnd API:
|
||||
;; https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API
|
||||
|
@ -59,7 +59,7 @@
|
|||
(let [dt (.-dataTransfer e)]
|
||||
(if (or (str/starts-with? data-type "application")
|
||||
(str/starts-with? data-type "penpot"))
|
||||
(.setData dt data-type (t/encode data))
|
||||
(.setData dt data-type (t/encode-str data))
|
||||
(.setData dt data-type data))
|
||||
e)))
|
||||
|
||||
|
@ -112,7 +112,7 @@
|
|||
(let [dt (.-dataTransfer e)]
|
||||
(if (or (str/starts-with? data-type "penpot")
|
||||
(= data-type "application/json"))
|
||||
(t/decode (.getData dt data-type))
|
||||
(t/decode-str (.getData dt data-type))
|
||||
(.getData dt data-type)))))
|
||||
|
||||
(defn get-files
|
||||
|
|
|
@ -8,13 +8,13 @@
|
|||
"A http client with rx streams interface."
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.transit :as t]
|
||||
[app.common.uri :as u]
|
||||
[app.config :as cfg]
|
||||
[app.util.cache :as c]
|
||||
[app.util.globals :as globals]
|
||||
[app.util.object :as obj]
|
||||
[app.util.time :as dt]
|
||||
[app.util.transit :as t]
|
||||
[app.util.webapi :as wapi]
|
||||
[beicon.core :as rx]
|
||||
[cuerdas.core :as str]
|
||||
|
@ -129,7 +129,7 @@
|
|||
(defn transit-data
|
||||
[data]
|
||||
(reify IBodyData
|
||||
(-get-body-data [_] (t/encode data))
|
||||
(-get-body-data [_] (t/encode-str data))
|
||||
(-update-headers [_ headers]
|
||||
(assoc headers "content-type" "application/transit+json"))))
|
||||
|
||||
|
@ -138,7 +138,7 @@
|
|||
(let [contentype (get headers "content-type")]
|
||||
(if (and (str/starts-with? contentype "application/transit+json")
|
||||
(pos? (count body)))
|
||||
(assoc response :body (t/decode body))
|
||||
(assoc response :body (t/decode-str body))
|
||||
response)))
|
||||
|
||||
(defn success?
|
||||
|
|
|
@ -9,9 +9,8 @@
|
|||
(:require
|
||||
[app.config :as cfg]
|
||||
[app.util.globals :as globals]
|
||||
[app.util.storage :refer [storage]]
|
||||
[app.util.object :as obj]
|
||||
[app.util.transit :as t]
|
||||
[app.util.storage :refer [storage]]
|
||||
[beicon.core :as rx]
|
||||
[cuerdas.core :as str]
|
||||
[goog.object :as gobj]
|
||||
|
|
|
@ -6,10 +6,10 @@
|
|||
|
||||
(ns app.util.storage
|
||||
(:require
|
||||
[app.util.transit :as t]
|
||||
[app.util.timers :as tm]
|
||||
[app.common.exceptions :as ex]
|
||||
[app.common.transit :as t]
|
||||
[app.util.globals :as g]
|
||||
[app.common.exceptions :as ex]))
|
||||
[app.util.timers :as tm]))
|
||||
|
||||
(defn- ^boolean is-worker?
|
||||
[]
|
||||
|
@ -18,8 +18,7 @@
|
|||
|
||||
(defn- decode
|
||||
[v]
|
||||
(ex/ignoring (t/decode v)))
|
||||
|
||||
(ex/ignoring (t/decode-str v)))
|
||||
|
||||
(defn- persist
|
||||
[storage prev curr]
|
||||
|
@ -29,8 +28,8 @@
|
|||
(when (not= curr* prev*)
|
||||
(tm/schedule-on-idle
|
||||
#(if (some? curr*)
|
||||
(.setItem ^js storage (t/encode key) (t/encode curr*))
|
||||
(.removeItem ^js storage (t/encode key)))))))
|
||||
(.setItem ^js storage (t/encode-str key) (t/encode-str curr*))
|
||||
(.removeItem ^js storage (t/encode-str key)))))))
|
||||
|
||||
(into #{} (concat (keys curr)
|
||||
(keys prev)))))
|
||||
|
@ -43,7 +42,7 @@
|
|||
(let [key (.key ^js storage index)
|
||||
val (.getItem ^js storage key)]
|
||||
(try
|
||||
(assoc res (t/decode key) (t/decode val))
|
||||
(assoc res (t/decode-str key) (t/decode-str val))
|
||||
(catch :default e
|
||||
res))))
|
||||
{}
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
[app.common.uuid :as uuid]
|
||||
[app.util.array :as arr]
|
||||
[app.util.object :as obj]
|
||||
[app.util.transit :as t]
|
||||
[clojure.walk :as walk]
|
||||
[cuerdas.core :as str]))
|
||||
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
[goog.object :as gobj]
|
||||
[app.config :as cfg]
|
||||
[app.util.dom :as dom]
|
||||
[app.util.transit :as t]
|
||||
[app.util.storage :refer [storage]]))
|
||||
|
||||
(defonce theme (get @storage ::theme cfg/default-theme))
|
||||
|
|
|
@ -1,127 +0,0 @@
|
|||
;; 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.transit
|
||||
"A lightweight abstraction for transit serialization."
|
||||
(:require
|
||||
[cognitect.transit :as t]
|
||||
[linked.core :as lk]
|
||||
[linked.set :as lks]
|
||||
[app.common.data :as d]
|
||||
[app.common.geom.point :as gpt]
|
||||
[app.common.geom.matrix :as gmt]
|
||||
[app.util.time :as dt]))
|
||||
|
||||
(deftype Blob [content]
|
||||
IDeref
|
||||
(-deref [_] content))
|
||||
|
||||
(defn blob?
|
||||
[v]
|
||||
(instance? Blob v))
|
||||
|
||||
(def blob-write-handler
|
||||
(t/write-handler
|
||||
(constantly "jsonblob")
|
||||
(fn [v] (js/JSON.stringify @v))))
|
||||
|
||||
(def blob-read-handler
|
||||
(t/read-handler
|
||||
(fn [value]
|
||||
(->Blob (js/JSON.parse value)))))
|
||||
|
||||
;; --- Transit adapters
|
||||
|
||||
(def bigint-read-handler
|
||||
(t/read-handler
|
||||
(fn [value]
|
||||
(js/parseInt value 10))))
|
||||
|
||||
(def point-write-handler
|
||||
(t/write-handler
|
||||
(constantly "point")
|
||||
(fn [v] (into {} v))))
|
||||
|
||||
(def point-read-handler
|
||||
(t/read-handler
|
||||
(fn [value]
|
||||
(gpt/map->Point value))))
|
||||
|
||||
(def matrix-write-handler
|
||||
(t/write-handler
|
||||
(constantly "matrix")
|
||||
(fn [v] (into {} v))))
|
||||
|
||||
(def matrix-read-handler
|
||||
(t/read-handler
|
||||
(fn [value]
|
||||
(gmt/map->Matrix value))))
|
||||
|
||||
(def ordered-set-write-handler
|
||||
(t/write-handler
|
||||
(constantly "ordered-set")
|
||||
(fn [v] (vec v))))
|
||||
|
||||
(def ordered-set-read-handler
|
||||
(t/read-handler #(into (lk/set) %)))
|
||||
|
||||
(def date-read-handler
|
||||
(t/read-handler (fn [value] (-> value (js/parseInt 10) (dt/datetime)))))
|
||||
|
||||
(def duration-read-handler
|
||||
(t/read-handler (fn [value] (dt/duration value))))
|
||||
|
||||
(def date-write-handler
|
||||
(t/write-handler
|
||||
(constantly "m")
|
||||
(fn [v] (str (inst-ms v)))))
|
||||
|
||||
(def duration-write-handler
|
||||
(t/write-handler
|
||||
(constantly "duration")
|
||||
(fn [v] (inst-ms v))))
|
||||
|
||||
;; --- Transit Handlers
|
||||
|
||||
(def ^:privare +read-handlers+
|
||||
{"u" uuid
|
||||
"n" bigint-read-handler
|
||||
"ordered-set" ordered-set-read-handler
|
||||
"jsonblob" blob-read-handler
|
||||
"matrix" matrix-read-handler
|
||||
"m" date-read-handler
|
||||
"duration" duration-read-handler
|
||||
"point" point-read-handler})
|
||||
|
||||
(def ^:privare +write-handlers+
|
||||
{gmt/Matrix matrix-write-handler
|
||||
Blob blob-write-handler
|
||||
dt/DateTime date-write-handler
|
||||
dt/Duration duration-write-handler
|
||||
lks/LinkedSet ordered-set-write-handler
|
||||
gpt/Point point-write-handler})
|
||||
|
||||
;; --- Public Api
|
||||
|
||||
(defn decode
|
||||
[data]
|
||||
(let [r (t/reader :json {:handlers +read-handlers+})]
|
||||
(t/read r data)))
|
||||
|
||||
(defn encode
|
||||
[data]
|
||||
(try
|
||||
(let [w (t/writer :json-verbose {:handlers +write-handlers+})]
|
||||
(t/write w data))
|
||||
(catch :default e
|
||||
(throw e))))
|
||||
|
||||
(defn transit?
|
||||
"Checks if a string can be decoded with transit"
|
||||
[str]
|
||||
(try
|
||||
(-> str decode nil? not)
|
||||
(catch js/SyntaxError e false)))
|
|
@ -10,7 +10,6 @@
|
|||
[app.common.data :as d]
|
||||
[app.common.exceptions :as ex]
|
||||
[app.util.object :as obj]
|
||||
[app.util.transit :as t]
|
||||
[beicon.core :as rx]
|
||||
[cuerdas.core :as str]
|
||||
[promesa.core :as p]))
|
||||
|
|
|
@ -7,9 +7,9 @@
|
|||
(ns app.util.websockets
|
||||
"A interface to webworkers exposed functionality."
|
||||
(:require
|
||||
[app.common.transit :as t]
|
||||
[app.common.uri :as u]
|
||||
[app.config :as cfg]
|
||||
[app.util.transit :as t]
|
||||
[beicon.core :as rx]
|
||||
[goog.events :as ev]
|
||||
[potok.core :as ptk])
|
||||
|
@ -55,4 +55,4 @@
|
|||
|
||||
(defn send!
|
||||
[ws msg]
|
||||
(-send ws (t/encode msg)))
|
||||
(-send ws (t/encode-str msg)))
|
||||
|
|
|
@ -7,9 +7,9 @@
|
|||
(ns app.util.worker
|
||||
"A lightweight layer on top of webworkers api."
|
||||
(:require
|
||||
[beicon.core :as rx]
|
||||
[app.common.transit :as t]
|
||||
[app.common.uuid :as uuid]
|
||||
[app.util.transit :as t]))
|
||||
[beicon.core :as rx]))
|
||||
|
||||
(declare handle-response)
|
||||
(defrecord Worker [instance stream])
|
||||
|
@ -25,7 +25,7 @@
|
|||
(rx/take-while #(not (:completed %)) ob)
|
||||
(rx/take 1 ob)))
|
||||
|
||||
data (t/encode message)
|
||||
data (t/encode-str message)
|
||||
instance (:instance worker)]
|
||||
|
||||
(.postMessage instance data)
|
||||
|
@ -67,7 +67,7 @@
|
|||
handle-message
|
||||
(fn [event]
|
||||
(let [data (.-data event)
|
||||
data (t/decode data)]
|
||||
data (t/decode-str data)]
|
||||
(if (:error data)
|
||||
(on-error (:error data))
|
||||
(rx/push! bus data))))
|
||||
|
|
|
@ -6,20 +6,20 @@
|
|||
|
||||
(ns app.worker
|
||||
(:require
|
||||
[cljs.spec.alpha :as s]
|
||||
[promesa.core :as p]
|
||||
[beicon.core :as rx]
|
||||
[cuerdas.core :as str]
|
||||
[app.common.exceptions :as ex]
|
||||
[app.common.spec :as us]
|
||||
[app.common.transit :as t]
|
||||
[app.common.uuid :as uuid]
|
||||
[app.util.object :as obj]
|
||||
[app.util.worker :as w]
|
||||
[app.worker.impl :as impl]
|
||||
[app.worker.selection]
|
||||
[app.worker.thumbnails]
|
||||
[app.worker.snaps]
|
||||
[app.util.object :as obj]
|
||||
[app.util.transit :as t]
|
||||
[app.util.worker :as w]))
|
||||
[app.worker.thumbnails]
|
||||
[beicon.core :as rx]
|
||||
[cljs.spec.alpha :as s]
|
||||
[cuerdas.core :as str]
|
||||
[promesa.core :as p]))
|
||||
|
||||
;; --- Messages Handling
|
||||
|
||||
|
@ -44,7 +44,7 @@
|
|||
[{:keys [sender-id payload] :as message}]
|
||||
(us/assert ::message message)
|
||||
(letfn [(post [msg]
|
||||
(let [msg (-> msg (assoc :reply-to sender-id) (t/encode))]
|
||||
(let [msg (-> msg (assoc :reply-to sender-id) (t/encode-str))]
|
||||
(.postMessage js/self msg)))
|
||||
|
||||
(reply [result]
|
||||
|
@ -84,8 +84,8 @@
|
|||
"Sends to the client a notifiction that its messages have been dropped"
|
||||
[{:keys [sender-id payload] :as message}]
|
||||
(us/assert ::message message)
|
||||
(.postMessage js/self (t/encode {:reply-to sender-id
|
||||
:dropped true})))
|
||||
(.postMessage js/self (t/encode-str {:reply-to sender-id
|
||||
:dropped true})))
|
||||
|
||||
(defn subscribe-buffer-messages
|
||||
"Creates a subscription to process the buffer messages"
|
||||
|
@ -143,7 +143,7 @@
|
|||
[event]
|
||||
(when (nil? (.-source event))
|
||||
(let [message (.-data event)
|
||||
message (t/decode message)]
|
||||
message (t/decode-str message)]
|
||||
(if (:buffer? message)
|
||||
(rx/push! buffer message)
|
||||
(handle-message message)))))
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
|
||||
(ns app.worker.impl
|
||||
(:require
|
||||
[okulary.core :as l]
|
||||
[app.util.transit :as t]
|
||||
[app.common.pages.changes :as ch]))
|
||||
[app.common.pages.changes :as ch]
|
||||
[app.common.transit :as t]
|
||||
[okulary.core :as l]))
|
||||
|
||||
(enable-console-print!)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue