From 304cf9f692791620abfd5814c6efa9978bd7957e Mon Sep 17 00:00:00 2001 From: Charlie Date: Thu, 14 Sep 2023 22:50:46 +0800 Subject: [PATCH] fix(plugin): incorrect mouse point payload for the plugin hook of input selection (#10175) --- src/main/frontend/ui.cljs | 40 +++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/main/frontend/ui.cljs b/src/main/frontend/ui.cljs index 8b27f290d7..f206e0aa14 100644 --- a/src/main/frontend/ui.cljs +++ b/src/main/frontend/ui.cljs @@ -86,20 +86,24 @@ (rum/defc ls-textarea < rum/reactive {:did-mount (fn [state] - (let [^js el (rum/dom-node state)] + (let [^js el (rum/dom-node state) + *mouse-point (volatile! nil)] ;; Passing aria-label as a prop to TextareaAutosize removes the dash (.setAttribute el "aria-label" "editing block") - (. el addEventListener "select" - #(let [start (util/get-selection-start el) - end (util/get-selection-end el)] - (when (and start end) - (when-let [e (and (not= start end) - {:caret (cursor/get-caret-pos el) - :start start :end end - :text (. (.-value el) substring start end) - :point {:x (.-x %) :y (.-y %)}})] - - (plugin-handler/hook-plugin-editor :input-selection-end (bean/->js e))))))) + (doto el + (.addEventListener "select" + #(let [start (util/get-selection-start el) + end (util/get-selection-end el)] + (when (and start end) + (when-let [e (and (not= start end) + (let [caret-pos (cursor/get-caret-pos el)] + {:caret caret-pos + :start start :end end + :text (. (.-value el) substring start end) + :point (select-keys (or @*mouse-point caret-pos) [:x :y])}))] + (plugin-handler/hook-plugin-editor :input-selection-end (bean/->js e)) + (vreset! *mouse-point nil))))) + (.addEventListener "mouseup" #(vreset! *mouse-point {:x (.-x %) :y (.-y %)})))) state)} [{:keys [on-change] :as props}] (let [skip-composition? (state/sub :editor/action) @@ -112,16 +116,16 @@ (on-change e)) (state/set-editor-in-composition! true)))) props (assoc props - :on-change (fn [e] (when-not (state/editor-in-composition?) - (on-change e))) - :on-composition-start on-composition - :on-composition-update on-composition - :on-composition-end on-composition)] + :on-change (fn [e] (when-not (state/editor-in-composition?) + (on-change e))) + :on-composition-start on-composition + :on-composition-update on-composition + :on-composition-end on-composition)] (textarea props))) (rum/defc dropdown-content-wrapper < {:did-mount (fn [state] - (let [k (inc (count (state/sub :modal/dropdowns))) + (let [k (inc (count (state/sub :modal/dropdowns))) args (:rum/args state)] (state/set-state! [:modal/dropdowns k] (second args)) (assoc state ::k k)))