mirror of
https://github.com/penpot/penpot.git
synced 2025-06-14 20:51:38 +02:00
🎉 Add comments to viewer.
This commit is contained in:
parent
e1db6d3a37
commit
64a6ba1949
45 changed files with 1629 additions and 1074 deletions
|
@ -9,31 +9,182 @@
|
|||
|
||||
(ns app.main.ui.viewer
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.exceptions :as ex]
|
||||
[app.common.geom.matrix :as gmt]
|
||||
[app.common.geom.point :as gpt]
|
||||
[app.common.geom.shapes :as geom]
|
||||
[app.common.pages-helpers :as cph]
|
||||
[app.main.data.viewer :as dv]
|
||||
[app.main.data.comments :as dcm]
|
||||
[app.main.refs :as refs]
|
||||
[app.main.store :as st]
|
||||
[app.main.ui.components.dropdown :refer [dropdown]]
|
||||
[app.main.ui.components.fullscreen :as fs]
|
||||
[app.main.ui.hooks :as hooks]
|
||||
[app.main.ui.icons :as i]
|
||||
[app.main.ui.keyboard :as kbd]
|
||||
[app.main.ui.viewer.header :refer [header]]
|
||||
[app.main.ui.viewer.shapes :refer [frame-svg]]
|
||||
[app.main.ui.viewer.shapes :as shapes :refer [frame-svg]]
|
||||
[app.main.ui.viewer.thumbnails :refer [thumbnails-panel]]
|
||||
[app.util.data :refer [classnames]]
|
||||
[app.main.ui.comments :as cmt]
|
||||
[app.util.dom :as dom]
|
||||
[app.util.i18n :as i18n :refer [t tr]]
|
||||
[beicon.core :as rx]
|
||||
[goog.events :as events]
|
||||
[okulary.core :as l]
|
||||
[rumext.alpha :as mf])
|
||||
(:import goog.events.EventType))
|
||||
[rumext.alpha :as mf]))
|
||||
|
||||
(defn- frame-contains?
|
||||
[{:keys [x y width height]} {px :x py :y}]
|
||||
(let [x2 (+ x width)
|
||||
y2 (+ y height)]
|
||||
(and (<= x px x2)
|
||||
(<= y py y2))))
|
||||
|
||||
(def threads-ref
|
||||
(l/derived :comment-threads st/state))
|
||||
|
||||
(def comments-local-ref
|
||||
(l/derived :comments-local st/state))
|
||||
|
||||
(mf/defc comments-layer
|
||||
[{:keys [width height zoom frame data] :as props}]
|
||||
(let [profile (mf/deref refs/profile)
|
||||
|
||||
modifier1 (-> (gpt/point (:x frame) (:y frame))
|
||||
(gpt/negate)
|
||||
(gmt/translate-matrix))
|
||||
|
||||
modifier2 (-> (gpt/point (:x frame) (:y frame))
|
||||
(gmt/translate-matrix))
|
||||
|
||||
threads-map (->> (mf/deref threads-ref)
|
||||
(d/mapm #(update %2 :position gpt/transform modifier1)))
|
||||
|
||||
cstate (mf/deref refs/comments-local)
|
||||
|
||||
mframe (geom/transform-shape frame)
|
||||
threads (->> (vals threads-map)
|
||||
(dcm/apply-filters cstate profile)
|
||||
(filter (fn [{:keys [seqn position]}]
|
||||
(frame-contains? mframe position))))
|
||||
|
||||
on-bubble-click
|
||||
(mf/use-callback
|
||||
(mf/deps cstate)
|
||||
(fn [thread]
|
||||
(if (= (:open cstate) (:id thread))
|
||||
(st/emit! (dcm/close-thread))
|
||||
(st/emit! (dcm/open-thread thread)))))
|
||||
|
||||
on-click
|
||||
(mf/use-callback
|
||||
(mf/deps cstate data)
|
||||
(fn [event]
|
||||
(dom/stop-propagation event)
|
||||
(if (some? (:open cstate))
|
||||
(st/emit! (dcm/close-thread))
|
||||
(let [event (.-nativeEvent ^js event)
|
||||
position (-> (dom/get-offset-position event)
|
||||
(gpt/transform modifier2))
|
||||
params {:position position
|
||||
:page-id (get-in data [:page :id])
|
||||
:file-id (get-in data [:file :id])}]
|
||||
(st/emit! (dcm/create-draft params))))))
|
||||
|
||||
on-draft-cancel
|
||||
(mf/use-callback
|
||||
(mf/deps cstate)
|
||||
(st/emitf (dcm/close-thread)))
|
||||
|
||||
on-draft-submit
|
||||
(mf/use-callback
|
||||
(fn [draft]
|
||||
(let [params (update draft :position gpt/transform modifier2)]
|
||||
;; (prn "on-draft-submit" params)
|
||||
|
||||
(st/emit! (dcm/create-thread params)
|
||||
(dcm/close-thread)))))
|
||||
]
|
||||
|
||||
[:div.viewer-comments {:on-click on-click}
|
||||
[:div.comments-layer
|
||||
[:div.threads
|
||||
(for [item threads]
|
||||
[:& cmt/thread-bubble {:thread item
|
||||
:zoom zoom
|
||||
:on-click on-bubble-click
|
||||
:open? (= (:id item) (:open cstate))
|
||||
:key (:seqn item)}])
|
||||
|
||||
(when-let [id (:open cstate)]
|
||||
(when-let [thread (get threads-map id)]
|
||||
[:& cmt/thread-comments {:thread thread
|
||||
:users (:users data)
|
||||
:zoom zoom}]))
|
||||
|
||||
(when-let [draft (:draft cstate)]
|
||||
[:& cmt/draft-thread {:draft (update draft :position gpt/transform modifier1)
|
||||
:on-cancel on-draft-cancel
|
||||
:on-submit on-draft-submit
|
||||
:zoom zoom}])]]]))
|
||||
|
||||
|
||||
|
||||
(mf/defc viewport
|
||||
{::mf/wrap [mf/memo]}
|
||||
[{:keys [state data index section] :or {zoom 1} :as props}]
|
||||
(let [zoom (:zoom state)
|
||||
objects (:objects data)
|
||||
|
||||
frame (get-in data [:frames index])
|
||||
frame-id (:id frame)
|
||||
|
||||
modifier (-> (gpt/point (:x frame) (:y frame))
|
||||
(gpt/negate)
|
||||
(gmt/translate-matrix))
|
||||
|
||||
update-fn #(assoc-in %1 [%2 :modifiers :displacement] modifier)
|
||||
|
||||
objects (->> (d/concat [frame-id] (cph/get-children frame-id objects))
|
||||
(reduce update-fn objects))
|
||||
|
||||
interactions? (:interactions-show? state)
|
||||
wrapper (mf/use-memo (mf/deps objects) #(shapes/frame-container-factory objects interactions?))
|
||||
|
||||
;; Retrieve frame again with correct modifier
|
||||
frame (get objects frame-id)
|
||||
|
||||
width (* (:width frame) zoom)
|
||||
height (* (:height frame) zoom)
|
||||
vbox (str "0 0 " (:width frame 0) " " (:height frame 0))]
|
||||
|
||||
[:div.viewport-container
|
||||
{:style {:width width
|
||||
:height height
|
||||
:state state
|
||||
:position "relative"}}
|
||||
|
||||
(when (= section :comments)
|
||||
[:& comments-layer {:width width
|
||||
:height height
|
||||
:frame frame
|
||||
:data data
|
||||
:zoom zoom}])
|
||||
|
||||
[:svg {:view-box vbox
|
||||
:width width
|
||||
:height height
|
||||
:version "1.1"
|
||||
:xmlnsXlink "http://www.w3.org/1999/xlink"
|
||||
:xmlns "http://www.w3.org/2000/svg"}
|
||||
[:& wrapper {:shape frame
|
||||
:show-interactions? interactions?
|
||||
:view-box vbox}]]]))
|
||||
|
||||
(mf/defc main-panel
|
||||
[{:keys [data local index]}]
|
||||
[{:keys [data state index section]}]
|
||||
(let [locale (mf/deref i18n/locale)
|
||||
frames (:frames data [])
|
||||
objects (:objects data)
|
||||
frames (:frames data)
|
||||
frame (get frames index)]
|
||||
[:section.viewer-preview
|
||||
(cond
|
||||
|
@ -45,22 +196,20 @@
|
|||
[:section.empty-state
|
||||
[:span (t locale "viewer.frame-not-found")]]
|
||||
|
||||
:else
|
||||
[:& frame-svg {:frame frame
|
||||
:show-interactions? (:show-interactions? local)
|
||||
:zoom (:zoom local)
|
||||
:objects objects}])]))
|
||||
(some? state)
|
||||
[:& viewport
|
||||
{:data data
|
||||
:section section
|
||||
:index index
|
||||
:state state
|
||||
}])]))
|
||||
|
||||
(mf/defc viewer-content
|
||||
[{:keys [data local index] :as props}]
|
||||
(let [container (mf/use-ref)
|
||||
|
||||
[toggle-fullscreen fullscreen?] (hooks/use-fullscreen container)
|
||||
|
||||
on-click
|
||||
[{:keys [data state index section] :as props}]
|
||||
(let [on-click
|
||||
(fn [event]
|
||||
(dom/stop-propagation event)
|
||||
(let [mode (get local :interactions-mode)]
|
||||
(let [mode (get state :interactions-mode)]
|
||||
(when (= mode :show-on-click)
|
||||
(st/emit! dv/flash-interactions))))
|
||||
|
||||
|
@ -73,49 +222,63 @@
|
|||
(st/emit! dv/decrease-zoom)
|
||||
(st/emit! dv/increase-zoom)))))
|
||||
|
||||
on-click
|
||||
(fn [event]
|
||||
(st/emit! (dcm/close-thread)))
|
||||
|
||||
on-key-down
|
||||
(fn [event]
|
||||
(when (kbd/esc? event)
|
||||
(st/emit! (dcm/close-thread))))
|
||||
|
||||
on-mount
|
||||
(fn []
|
||||
;; bind with passive=false to allow the event to be cancelled
|
||||
;; https://stackoverflow.com/a/57582286/3219895
|
||||
(let [key1 (events/listen goog/global EventType.WHEEL
|
||||
on-mouse-wheel #js {"passive" false})]
|
||||
(let [key1 (events/listen goog/global "wheel" on-mouse-wheel #js {"passive" false})
|
||||
key2 (events/listen js/document "keydown" on-key-down)
|
||||
key3 (events/listen js/document "click" on-click)]
|
||||
(fn []
|
||||
(events/unlistenByKey key1))))]
|
||||
(events/unlistenByKey key1)
|
||||
(events/unlistenByKey key2)
|
||||
(events/unlistenByKey key3))))]
|
||||
|
||||
(mf/use-effect on-mount)
|
||||
(hooks/use-shortcuts dv/shortcuts)
|
||||
|
||||
[:div.viewer-layout {:class (classnames :fullscreen fullscreen?)
|
||||
:ref container}
|
||||
[:& fs/fullscreen-wrapper {}
|
||||
[:div.viewer-layout
|
||||
[:& header
|
||||
{:data data
|
||||
:state state
|
||||
:section section
|
||||
:index index}]
|
||||
|
||||
[:& header {:data data
|
||||
:toggle-fullscreen toggle-fullscreen
|
||||
:fullscreen? fullscreen?
|
||||
:local local
|
||||
:index index
|
||||
:screen :viewer}]
|
||||
[:div.viewer-content {:on-click on-click}
|
||||
(when (:show-thumbnails local)
|
||||
[:& thumbnails-panel {:screen :viewer
|
||||
:index index
|
||||
:data data}])
|
||||
[:& main-panel {:data data
|
||||
:local local
|
||||
:index index}]]]))
|
||||
[:div.viewer-content {:on-click on-click}
|
||||
(when (:show-thumbnails state)
|
||||
[:& thumbnails-panel {:screen :viewer
|
||||
:index index
|
||||
:data data}])
|
||||
[:& main-panel {:data data
|
||||
:section section
|
||||
:state state
|
||||
:index index}]]]]))
|
||||
|
||||
|
||||
;; --- Component: Viewer Page
|
||||
|
||||
(mf/defc viewer-page
|
||||
[{:keys [file-id page-id index token] :as props}]
|
||||
[{:keys [file-id page-id index token section] :as props}]
|
||||
|
||||
(mf/use-effect
|
||||
(mf/deps file-id page-id token)
|
||||
(fn []
|
||||
(st/emit! (dv/initialize props))))
|
||||
(st/emitf (dv/initialize props)))
|
||||
|
||||
(let [data (mf/deref refs/viewer-data)
|
||||
local (mf/deref refs/viewer-local)]
|
||||
(when data
|
||||
[:& viewer-content {:index index
|
||||
:local local
|
||||
:data data}])))
|
||||
(let [data (mf/deref refs/viewer-data)
|
||||
state (mf/deref refs/viewer-local)]
|
||||
(when (and data state)
|
||||
[:& viewer-content
|
||||
{:index index
|
||||
:section section
|
||||
:state state
|
||||
:data data}])))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue