🔧 Minor refactor on text shapes.

This commit is contained in:
Andrey Antukh 2019-07-22 12:40:34 +02:00
parent 2b81832b67
commit 54809d05e2

View file

@ -5,22 +5,22 @@
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz> ;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.main.ui.shapes.text (ns uxbox.main.ui.shapes.text
(:require [cuerdas.core :as str] (:require
[lentes.core :as l] [cuerdas.core :as str]
[goog.events :as events] [goog.events :as events]
[potok.core :as ptk] [lentes.core :as l]
[uxbox.main.store :as st] [rumext.core :as mx]
[rumext.alpha :as mf]
[uxbox.main.data.shapes :as uds]
[uxbox.main.geom :as geom] [uxbox.main.geom :as geom]
[uxbox.main.refs :as refs] [uxbox.main.refs :as refs]
[uxbox.main.data.shapes :as uds] [uxbox.main.store :as st]
[uxbox.main.ui.shapes.common :as common]
[uxbox.main.ui.shapes.attrs :as attrs] [uxbox.main.ui.shapes.attrs :as attrs]
[uxbox.main.ui.shapes.common :as common]
[uxbox.util.color :as color] [uxbox.util.color :as color]
[uxbox.util.data :refer [classnames normalize-props]] [uxbox.util.data :refer [classnames normalize-props]]
[uxbox.util.dom :as dom] [uxbox.util.dom :as dom]
[uxbox.util.geom.matrix :as gmt] [uxbox.util.geom.matrix :as gmt]))
[rumext.core :as mx :include-macros true])
(:import goog.events.EventType))
;; --- Events ;; --- Events
@ -39,9 +39,10 @@
(declare text-shape-wrapper) (declare text-shape-wrapper)
(declare text-shape-edit) (declare text-shape-edit)
(mx/defcs text-component (mf/def text-component
{:mixins [mx/static mx/reactive]} :mixins [mf/memo mx/reactive]
[own {:keys [id x1 y1 content group] :as shape}] :render
(fn [own {:keys [id x1 y1 content group] :as shape}]
(let [modifiers (mx/react (refs/selected-modifiers id)) (let [modifiers (mx/react (refs/selected-modifiers id))
selected (mx/react refs/selected-shapes) selected (mx/react refs/selected-shapes)
edition? (= (mx/react refs/selected-edition) id) edition? (= (mx/react refs/selected-edition) id)
@ -56,12 +57,11 @@
(dom/stop-propagation event) (dom/stop-propagation event)
(st/emit! (uds/start-edition-mode id)))] (st/emit! (uds/start-edition-mode id)))]
[:g.shape {:class (when selected? "selected") [:g.shape {:class (when selected? "selected")
:ref "main"
:on-double-click on-double-click :on-double-click on-double-click
:on-mouse-down on-mouse-down} :on-mouse-down on-mouse-down}
(if edition? (if edition?
(text-shape-edit shape) (text-shape-edit shape)
(text-shape-wrapper shape))]))) (text-shape-wrapper shape))]))))
;; --- Text Styles Helpers ;; --- Text Styles Helpers
@ -104,52 +104,62 @@
;; --- Text Shape Edit ;; --- Text Shape Edit
(defn- text-shape-edit-did-mount (mf/def text-shape-edit
[own] :mixins [mf/memo]
(let [[shape] (::mx/props own)
dom (mx/ref-node own "container")] :init
(fn [own props]
(assoc own ::container (mf/create-ref)))
:did-mount
(fn [own]
(let [shape (::mf/props own)
dom (mx/ref-node (::container own))]
(set! (.-textContent dom) (:content shape "")) (set! (.-textContent dom) (:content shape ""))
(.focus dom) (.focus dom)
own)) own))
(mx/defc text-shape-edit :render
{:did-mount text-shape-edit-did-mount (fn [own {:keys [id x1 y1 content] :as shape}]
:mixins [mx/static]}
[{:keys [id x1 y1 content] :as shape}]
(let [{:keys [width height]} (geom/size shape) (let [{:keys [width height]} (geom/size shape)
style (make-style shape) style (make-style shape)
props {:x x1 :y y1 :width width :height height}] on-input (fn [ev]
(letfn [(on-input [ev]
(let [content (dom/event->inner-text ev)] (let [content (dom/event->inner-text ev)]
(st/emit! (uds/update-text id content))))] (st/emit! (uds/update-text id content))))]
[:foreignObject props [:foreignObject {:x x1 :y y1 :width width :height height}
[:div {:style (normalize-props style) [:div {:style (normalize-props style)
:ref "container" :ref (::container own)
:on-input on-input :on-input on-input
:contentEditable true}]]))) :contentEditable true}]])))
;; --- Text Shape Wrapper ;; --- Text Shape Wrapper
;; NOTE: this is a hack for the browser rendering. (mf/def text-shape-wrapper
;; :mixins [mf/memo]
;; Without forcing rerender, when the shape is displaced :key-fn pr-str
;; and only x and y attrs are updated in dom, the whole content
;; of the foreignObject becomes sometimes partially or
;; completelly invisible. The complete dom rerender fixes that
;; problem.
(defn text-shape-wrapper-did-mount :init
[own] (fn [own props]
(let [[shape] (::mx/props own) (assoc own ::fobject (mf/create-ref)))
dom (mx/ref-node own "fobject")
;; NOTE: this is a hack for the browser rendering.
;;
;; Without forcing rerender, when the shape is displaced
;; and only x and y attrs are updated in dom, the whole content
;; of the foreignObject becomes sometimes partially or
;; completelly invisible. The complete dom rerender fixes that
;; problem.
:did-mount
(fn [own]
(let [shape (::mf/props own)
dom (mf/ref-node (::fobject own))
html (dom/render-to-html (text-shape-html shape))] html (dom/render-to-html (text-shape-html shape))]
(set! (.-innerHTML dom) html)) (set! (.-innerHTML dom) html))
own) own)
(mx/defc text-shape-wrapper :render
{:did-mount text-shape-wrapper-did-mount (fn [own {:keys [id modifiers] :as shape}]
:key-fn #(pr-str %1)}
[{:keys [id modifiers] :as shape}]
(let [{:keys [displacement resize]} modifiers (let [{:keys [displacement resize]} modifiers
xfmt (cond-> (gmt/matrix) xfmt (cond-> (gmt/matrix)
displacement (gmt/multiply displacement) displacement (gmt/multiply displacement)
@ -162,24 +172,47 @@
:y y1 :y y1
:class (classnames :move-cursor moving?) :class (classnames :move-cursor moving?)
:id (str id) :id (str id)
:ref "fobject" :ref (::fobject own)
:width width :width width
:height height}])) :height height}])))
;; --- Text Shape Html ;; --- Text Shape Html
(mx/defc text-shape-html (mf/def text-shape-html
[{:keys [content] :as shape}] :mixins [mf/memo]
:render
(fn [own {:keys [content] :as shape}]
(let [style (make-style shape)] (let [style (make-style shape)]
[:div {:style style} content])) [:div {:style style} content])))
;; --- Text Shape Html ;; --- Text Shape Html
(mx/defc text-shape (mf/def text-shape
{:mixins [mx/static] :mixins [mf/memo]
:did-mount text-shape-wrapper-did-mount :key-fn pr-str
:key-fn #(pr-str %1)}
[{:keys [id content modifiers] :as shape}] :init
(fn [own props]
(assoc own ::fobject (mf/create-ref)))
;; NOTE: this is a hack for the browser rendering.
;;
;; Without forcing rerender, when the shape is displaced
;; and only x and y attrs are updated in dom, the whole content
;; of the foreignObject becomes sometimes partially or
;; completelly invisible. The complete dom rerender fixes that
;; problem.
:did-mount
(fn [own]
(let [shape (::mf/props own)
dom (mf/ref-node (::fobject own))
html (dom/render-to-html (text-shape-html shape))]
(set! (.-innerHTML dom) html))
own)
:render
(fn [own {:keys [id content modifiers] :as shape}]
(let [{:keys [displacement resize]} modifiers (let [{:keys [displacement resize]} modifiers
xfmt (cond-> (gmt/matrix) xfmt (cond-> (gmt/matrix)
displacement (gmt/multiply displacement) displacement (gmt/multiply displacement)
@ -193,8 +226,8 @@
:y y1 :y y1
:class (classnames :move-cursor moving?) :class (classnames :move-cursor moving?)
:id (str id) :id (str id)
:ref "fobject" :ref (::fobject own)
:width width :width width
:height height} :height height}
[:div {:style style} [:div {:style style}
[:p content]]])) [:p content]]])))