From c8b8bd3ef649f8fe10b63a00b66960f500161a31 Mon Sep 17 00:00:00 2001 From: rcmerci Date: Sat, 23 May 2026 02:55:19 +0800 Subject: [PATCH] fix: limit mobile comment toolbar (#12713) * fix: limit mobile comment toolbar * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Tienson Qin Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../mobile/components/editor_toolbar.cljs | 63 ++++++++++--------- src/test/mobile/editor_toolbar_test.cljs | 37 +++++++++++ 2 files changed, 70 insertions(+), 30 deletions(-) create mode 100644 src/test/mobile/editor_toolbar_test.cljs diff --git a/src/main/mobile/components/editor_toolbar.cljs b/src/main/mobile/components/editor_toolbar.cljs index 28257343d5..0cb62ad6bd 100644 --- a/src/main/mobile/components/editor_toolbar.cljs +++ b/src/main/mobile/components/editor_toolbar.cljs @@ -39,23 +39,21 @@ (defn- insert-page-ref! [] - (let [{:keys [block]} (editor-handler/get-state)] - (when block - (let [input (state/get-input)] - (state/clear-editor-action!) - (let [selection (editor-handler/get-selection-and-format) - {:keys [selection-start selection-end selection]} selection] - (if selection - (do - (editor-handler/delete-and-update input selection-start selection-end) - (editor-handler/insert (page-ref/->page-ref selection))) - (insert-text page-ref/left-and-right-brackets - {:backward-pos 2 - :check-fn (fn [_ _ _] - (let [input (state/get-input) - new-pos (cursor/get-caret-pos input)] - (state/set-editor-action-data! {:pos new-pos}) - (commands/handle-step [:editor/search-page])))}))))))) + (when-let [input (state/get-input)] + (state/clear-editor-action!) + (let [selection (editor-handler/get-selection-and-format) + {:keys [selection-start selection-end selection]} selection] + (if selection + (do + (editor-handler/delete-and-update input selection-start selection-end) + (editor-handler/insert (page-ref/->page-ref selection))) + (insert-text page-ref/left-and-right-brackets + {:backward-pos 2 + :check-fn (fn [_ _ _] + (let [input (state/get-input) + new-pos (cursor/get-caret-pos input)] + (state/set-editor-action-data! {:pos new-pos}) + (commands/handle-step [:editor/search-page])))}))))) (defn- indent-outdent-action [indent?] @@ -151,19 +149,24 @@ (defn- toolbar-actions [] - (let [keyboard (keyboard-action) - main-actions [(undo-action) - (todo-action) - (indent-outdent-action false) - (indent-outdent-action true) - (tag-action) - (camera-action) - (page-ref-action) - (audio-action) - (slash-action) - (redo-action)]] - {:main main-actions - :trailing keyboard})) + (if (:comment-editor? (last (state/get-editor-args))) + {:main [(page-ref-action) + (camera-action) + (audio-action)] + :trailing nil} + (let [keyboard (keyboard-action) + main-actions [(undo-action) + (todo-action) + (indent-outdent-action false) + (indent-outdent-action true) + (tag-action) + (camera-action) + (page-ref-action) + (audio-action) + (slash-action) + (redo-action)]] + {:main main-actions + :trailing keyboard}))) (defn- action->native [{:keys [id title system-icon]}] diff --git a/src/test/mobile/editor_toolbar_test.cljs b/src/test/mobile/editor_toolbar_test.cljs new file mode 100644 index 0000000000..7511ca6b6b --- /dev/null +++ b/src/test/mobile/editor_toolbar_test.cljs @@ -0,0 +1,37 @@ +(ns mobile.editor-toolbar-test + (:require [cljs.test :refer [deftest is testing]] + [frontend.commands :as commands] + [frontend.handler.editor :as editor-handler] + [frontend.state :as state] + [frontend.util.cursor :as cursor] + [goog.dom :as gdom] + [logseq.common.util.page-ref :as page-ref] + [mobile.components.editor-toolbar :as editor-toolbar])) + +(deftest comment-toolbar-actions-are-limited + (testing "comment editing only exposes reference, photo, and audio" + (with-redefs [state/get-editor-args (constantly [{} "edit-block-comment" {:comment-editor? true}])] + (let [{:keys [main trailing]} (#'editor-toolbar/toolbar-actions)] + (is (= ["page-ref" "camera" "audio"] + (mapv :id main))) + (is (nil? trailing)))))) + +(deftest comment-reference-action-inserts-page-ref + (testing "comment reference action works without regular block editor state" + (let [input-id "edit-block-comment" + input #js {:value ""} + insertions (atom [])] + (with-redefs [editor-handler/get-state (constantly nil) + editor-handler/get-selection-and-format (constantly nil) + state/get-edit-input-id (constantly input-id) + state/get-input (constantly input) + gdom/getElement (fn [id] + (when (= id input-id) + input)) + cursor/pos (constantly 0) + commands/simple-insert! (fn [id value opts] + (swap! insertions conj + [id value (:backward-pos opts) (fn? (:check-fn opts))]))] + ((:handler (#'editor-toolbar/page-ref-action))) + (is (= [[input-id page-ref/left-and-right-brackets 2 true]] + @insertions))))))