0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-22 14:57:01 -05:00

Add safety checks for node on comment-input* component

This commit is contained in:
Andrey Antukh 2025-02-18 18:33:02 +01:00
parent 869a412c74
commit cd1eefb214

View file

@ -103,13 +103,16 @@
(defn- current-text-node
"Retrieves the text node and the offset that the cursor is positioned on"
([node] (current-text-node node (wapi/get-selection)))
([node selection]
(when (and node selection)
[node]
(assert (some? node) "expected valid node")
(when-let [selection (wapi/get-selection)]
(let [range (wapi/get-range selection 0)
anchor-node (wapi/range-start-container range)
anchor-offset (wapi/range-start-offset range)]
[(current-text-node* node anchor-node) anchor-offset]))))
offset (wapi/range-start-offset range)
span-node (current-text-node* node anchor-node)]
(when span-node
[span-node offset]))))
(defn- absolute-offset
[node child offset]
@ -285,9 +288,8 @@
(mf/use-fn
(mf/deps on-change)
(fn [data]
(let [node (mf/ref-val local-ref)
[span-node offset] (current-text-node node)]
(when span-node
(when-let [node (mf/ref-val local-ref)]
(when-let [[span-node offset] (current-text-node node)]
(let [node-text
(dom/get-text span-node)
@ -320,9 +322,8 @@
handle-insert-at-symbol
(mf/use-fn
(fn []
(let [node (mf/ref-val local-ref)
[span-node] (current-text-node node)]
(when span-node
(when-let [node (mf/ref-val local-ref)]
(when-let [[span-node] (current-text-node node)]
(let [node-text (dom/get-text span-node)
at-symbol (if (blank-content? node-text) "@" " @")]
@ -334,10 +335,8 @@
(mf/deps on-esc on-ctrl-enter handle-select handle-input)
(fn [event]
(handle-select event)
(let [node (mf/ref-val local-ref)
[span-node offset] (current-text-node node)]
(when-let [node (mf/ref-val local-ref)]
(when-let [[span-node offset] (current-text-node node)]
(cond
(and @cur-mention (kbd/enter? event))
(do (dom/prevent-default event)
@ -387,13 +386,11 @@
(= offset 1))
(dom/prevent-default event)
(dom/stop-propagation event)
(.remove prev-node)))))))]
(.remove prev-node))))))))]
(mf/use-layout-effect
(mf/deps autofocus)
(fn []
(when autofocus
(dom/focus! (mf/ref-val local-ref)))))
(mf/with-layout-effect [autofocus]
(when ^boolean autofocus
(dom/focus! (mf/ref-val local-ref))))
;; Creates the handlers for selection
(mf/with-effect [handle-select]
@ -417,12 +414,12 @@
;; Auto resize input to display the comment
(mf/with-layout-effect nil
(let [^js node (mf/ref-val local-ref)]
(when-let [^js node (mf/ref-val local-ref)]
(set! (.-height (.-style node)) "0")
(set! (.-height (.-style node)) (str (+ 2 (.-scrollHeight node)) "px"))))
(mf/with-effect [value prev-value]
(let [node (mf/ref-val local-ref)]
(when-let [node (mf/ref-val local-ref)]
(cond
(and (d/not-empty? prev-value) (empty? value))
(do (dom/set-html! node "")