Pārlūkot izejas kodu

feat(pdf): add toggle option for the feature of auto opening the highlight context menu from new text selection (#10390)

* enhance(pdf): add auto open context menu option for new highlight selection
* refactor(pdf): improve code readability
* chore(pdf): i18n
Charlie 2 gadi atpakaļ
vecāks
revīzija
6751e28c30

+ 51 - 46
src/main/frontend/extensions/pdf/core.cljs

@@ -121,63 +121,66 @@
   ;;page-bounding (and highlight (pdf-utils/get-page-bounding viewer (:page highlight)))
   ;;page-bounding (and highlight (pdf-utils/get-page-bounding viewer (:page highlight)))
   ;;])
   ;;])
 
 
-  (let [*el         (rum/use-ref nil)
-        ^js cnt     (.-container viewer)
+  (let [*el (rum/use-ref nil)
+        ^js cnt (.-container viewer)
         head-height 0                                       ;; 48 temp
         head-height 0                                       ;; 48 temp
-        top         (- (+ (:y point) (.-scrollTop cnt)) head-height)
-        left        (+ (:x point) (.-scrollLeft cnt))
-        id          (:id highlight)
-        new?        (nil? id)
-        content     (:content highlight)
-        area?       (not (string/blank? (:image content)))
-        action-fn!  (fn [action clear?]
-                      (when-let [action (and action (name action))]
-                        (let [highlight (if (fn? highlight) (highlight) highlight)
-                              content   (:content highlight)]
-                          (case action
-                            "ref"
-                            (pdf-assets/copy-hl-ref! highlight viewer)
-
-                            "copy"
-                            (do
-                              (util/copy-to-clipboard!
+        top (- (+ (:y point) (.-scrollTop cnt)) head-height)
+        left (+ (:x point) (.-scrollLeft cnt))
+        id (:id highlight)
+        new? (nil? id)
+        new-&-highlight-mode? (and @*highlight-mode? new?)
+        show-ctx-menu? (and (not new-&-highlight-mode?)
+                            (or (not selection) (and selection (state/sub :pdf/auto-open-ctx-menu?))))
+        content (:content highlight)
+        area? (not (string/blank? (:image content)))
+        action-fn! (fn [action clear?]
+                     (when-let [action (and action (name action))]
+                       (let [highlight (if (fn? highlight) (highlight) highlight)
+                             content (:content highlight)]
+                         (case action
+                           "ref"
+                           (pdf-assets/copy-hl-ref! highlight viewer)
+
+                           "copy"
+                           (do
+                             (util/copy-to-clipboard!
                                (or (:text content) (pdf-utils/fix-selection-text-breakline (.toString selection)))
                                (or (:text content) (pdf-utils/fix-selection-text-breakline (.toString selection)))
                                :owner-window (pdf-windows/resolve-own-window viewer))
                                :owner-window (pdf-windows/resolve-own-window viewer))
-                              (pdf-utils/clear-all-selection))
+                             (pdf-utils/clear-all-selection))
 
 
-                            "link"
-                            (pdf-assets/goto-block-ref! highlight)
+                           "link"
+                           (pdf-assets/goto-block-ref! highlight)
 
 
-                            "del"
-                            (do
-                              (del-hl! highlight)
-                              (pdf-assets/del-ref-block! highlight)
-                              (pdf-assets/unlink-hl-area-image$ viewer (:pdf/current @state/state) highlight))
+                           "del"
+                           (do
+                             (del-hl! highlight)
+                             (pdf-assets/del-ref-block! highlight)
+                             (pdf-assets/unlink-hl-area-image$ viewer (:pdf/current @state/state) highlight))
 
 
-                            "hook"
-                            :dune
+                           "hook"
+                           :dune
 
 
-                            ;; colors
-                            (let [properties {:color action}]
-                              (if-not id
-                                ;; add highlight
-                                (let [highlight (merge highlight
-                                                       {:id         (pdf-utils/gen-uuid)
-                                                        :properties properties})]
-                                  (add-hl! highlight)
-                                  (pdf-utils/clear-all-selection)
-                                  (pdf-assets/copy-hl-ref! highlight viewer))
+                           ;; colors
+                           (let [properties {:color action}]
+                             (if-not id
+                               ;; add highlight
+                               (let [highlight (merge highlight
+                                                      {:id         (pdf-utils/gen-uuid)
+                                                       :properties properties})]
+                                 (add-hl! highlight)
+                                 (pdf-utils/clear-all-selection)
+                                 (pdf-assets/copy-hl-ref! highlight viewer))
 
 
-                                ;; update highlight
-                                (upd-hl! (assoc highlight :properties properties)))
+                               ;; update highlight
+                               (upd-hl! (assoc highlight :properties properties)))
 
 
-                              (reset! *highlight-last-color (keyword action)))))
+                             (reset! *highlight-last-color (keyword action)))))
 
 
-                        (and clear? (js/setTimeout #(clear-ctx-menu!) 68))))]
+                       (and clear? (js/setTimeout #(clear-ctx-menu!) 68))))]
 
 
     (rum/use-effect!
     (rum/use-effect!
      (fn []
      (fn []
-       (if (and @*highlight-mode? new?)
+       (if new-&-highlight-mode?
          ;; wait for selection cleared ...
          ;; wait for selection cleared ...
          (js/setTimeout #(action-fn! @*highlight-last-color true) 300)
          (js/setTimeout #(action-fn! @*highlight-last-color true) 300)
          (let [^js el (rum/deref *el)
          (let [^js el (rum/deref *el)
@@ -189,7 +192,9 @@
 
 
     [:ul.extensions__pdf-hls-ctx-menu
     [:ul.extensions__pdf-hls-ctx-menu
      {:ref      *el
      {:ref      *el
-      :style    {:top top :left left :visibility (if (and @*highlight-mode? new?) "hidden" "visible")}
+      :style    {:top top
+                 :left left
+                 :visibility (if show-ctx-menu? "visible" "hidden")}
       :on-click (fn [^js/MouseEvent e]
       :on-click (fn [^js/MouseEvent e]
                   (.stopPropagation e)
                   (.stopPropagation e)
                   (when-let [action (.. e -target -dataset -action)]
                   (when-let [action (.. e -target -dataset -action)]
@@ -502,7 +507,7 @@
                                                       :content    {:text "[:span]" :image (js/Date.now)}
                                                       :content    {:text "[:span]" :image (js/Date.now)}
                                                       :properties {}}]
                                                       :properties {}}]
 
 
-                                     ;; ctx tips
+                                     ;; ctx tips for area
                                      (show-ctx-menu! viewer hl point {:reset-fn #(reset-coords!)}))
                                      (show-ctx-menu! viewer hl point {:reset-fn #(reset-coords!)}))
 
 
                                    (set-area-mode! false))
                                    (set-area-mode! false))

+ 2 - 2
src/main/frontend/extensions/pdf/pdf.css

@@ -274,8 +274,8 @@ input::-webkit-inner-spin-button {
   &-settings {
   &-settings {
     &-inner.hls-popup-box {
     &-inner.hls-popup-box {
       position: absolute;
       position: absolute;
-      right: 345px;
-      width: 178px;
+      right: 375px;
+      width: 235px;
       padding: 15px 10px;
       padding: 15px 10px;
     }
     }
 
 

+ 13 - 1
src/main/frontend/extensions/pdf/toolbar.cljs

@@ -29,7 +29,8 @@
 
 
   (let [*el-popup (rum/use-ref nil)
   (let [*el-popup (rum/use-ref nil)
         [area-dashed? set-area-dashed?] (use-atom *area-dashed?)
         [area-dashed? set-area-dashed?] (use-atom *area-dashed?)
-        [hl-block-colored? set-hl-block-colored?] (rum/use-state (state/sub :pdf/block-highlight-colored?))]
+        [hl-block-colored? set-hl-block-colored?] (rum/use-state (state/sub :pdf/block-highlight-colored?))
+        [auto-open-ctx-menu? set-auto-open-ctx-menu!] (rum/use-state (state/sub :pdf/auto-open-ctx-menu?))]
 
 
     (rum/use-effect!
     (rum/use-effect!
      (fn []
      (fn []
@@ -54,6 +55,13 @@
          (storage/set "ls-pdf-hl-block-is-colored" b)))
          (storage/set "ls-pdf-hl-block-is-colored" b)))
      [hl-block-colored?])
      [hl-block-colored?])
 
 
+    (rum/use-effect!
+      (fn []
+        (let [b (boolean auto-open-ctx-menu?)]
+          (state/set-state! :pdf/auto-open-ctx-menu? b)
+          (storage/set "ls-pdf-auto-open-ctx-menu" b)))
+      [auto-open-ctx-menu?])
+
     (rum/use-effect!
     (rum/use-effect!
      (fn []
      (fn []
        (let [cb  #(let [^js target (.-target %)]
        (let [cb  #(let [^js target (.-target %)]
@@ -85,6 +93,10 @@
        [:label (t :pdf/hl-block-colored)]
        [:label (t :pdf/hl-block-colored)]
        (ui/toggle hl-block-colored? #(set-hl-block-colored? (not hl-block-colored?)) true)]
        (ui/toggle hl-block-colored? #(set-hl-block-colored? (not hl-block-colored?)) true)]
 
 
+      [:div.extensions__pdf-settings-item.toggle-input.is-between
+       [:label (t :pdf/auto-open-context-menu)]
+       (ui/toggle auto-open-ctx-menu? #(set-auto-open-ctx-menu! (not auto-open-ctx-menu?)) true)]
+
       [:div.extensions__pdf-settings-item.toggle-input
       [:div.extensions__pdf-settings-item.toggle-input
        [:a.is-info.w-full.text-gray-500
        [:a.is-info.w-full.text-gray-500
         {:title    (t :pdf/doc-metadata)
         {:title    (t :pdf/doc-metadata)

+ 1 - 0
src/main/frontend/state.cljs

@@ -220,6 +220,7 @@
      :pdf/current                           nil
      :pdf/current                           nil
      :pdf/ref-highlight                     nil
      :pdf/ref-highlight                     nil
      :pdf/block-highlight-colored?          (or (storage/get "ls-pdf-hl-block-is-colored") true)
      :pdf/block-highlight-colored?          (or (storage/get "ls-pdf-hl-block-is-colored") true)
+     :pdf/auto-open-ctx-menu?               (not= false (storage/get "ls-pdf-auto-open-ctx-menu"))
 
 
      ;; all notification contents as k-v pairs
      ;; all notification contents as k-v pairs
      :notification/contents                 {}
      :notification/contents                 {}

+ 1 - 0
src/resources/dicts/en.edn

@@ -608,6 +608,7 @@
  :pdf/linked-ref "Linked references"
  :pdf/linked-ref "Linked references"
  :pdf/toggle-dashed "Dashed style for area highlight"
  :pdf/toggle-dashed "Dashed style for area highlight"
  :pdf/hl-block-colored "Colored label for highlight block"
  :pdf/hl-block-colored "Colored label for highlight block"
+ :pdf/auto-open-context-menu "Auto open context menu for selections"
  :pdf/doc-metadata "Document metadata"
  :pdf/doc-metadata "Document metadata"
 
 
  :updater/new-version-install "A new version has been downloaded."
  :updater/new-version-install "A new version has been downloaded."

+ 1 - 0
src/resources/dicts/ja.edn

@@ -603,6 +603,7 @@
  :pdf/linked-ref "リンクありの参照元"
  :pdf/linked-ref "リンクありの参照元"
  :pdf/toggle-dashed "領域のハイライトに破線を使う"
  :pdf/toggle-dashed "領域のハイライトに破線を使う"
  :pdf/hl-block-colored "ブロックをハイライトするための色付きラベル"
  :pdf/hl-block-colored "ブロックをハイライトするための色付きラベル"
+ :pdf/auto-open-context-menu "自動でコンテキストメニューを開く"
  :pdf/doc-metadata "ドキュメントのメタデータ"
  :pdf/doc-metadata "ドキュメントのメタデータ"
 
 
  :updater/new-version-install "新しいバージョンがダウンロードされました。"
  :updater/new-version-install "新しいバージョンがダウンロードされました。"

+ 3 - 1
src/resources/dicts/ko.edn

@@ -210,6 +210,9 @@
  :pdf/copy-text "텍스트 복사하기"
  :pdf/copy-text "텍스트 복사하기"
  :pdf/linked-ref "링크된 레퍼런스"
  :pdf/linked-ref "링크된 레퍼런스"
  :pdf/toggle-dashed "영역 하이라이트를 위해 Dashed style 사용"
  :pdf/toggle-dashed "영역 하이라이트를 위해 Dashed style 사용"
+ :pdf/hl-block-colored "색상으로 강조된 블록입니다"
+ :pdf/auto-open-context-menu "자동으로 컨텍스트 메뉴를 엽니다"
+ :pdf/doc-metadata "메타데이터"
 
 
  :updater/new-version-install "새 버전이 다운로드되었습니다."
  :updater/new-version-install "새 버전이 다운로드되었습니다."
  :updater/quit-and-install "다시 시작하여 설치하십시오."
  :updater/quit-and-install "다시 시작하여 설치하십시오."
@@ -289,7 +292,6 @@
  :page/something-went-wrong "문제가 발생하였습니다."
  :page/something-went-wrong "문제가 발생하였습니다."
  :page/step "스텝 {1}"
  :page/step "스텝 {1}"
  :page/try "시도"
  :page/try "시도"
- :pdf/doc-metadata "메타데이터"
  :plugin.install-from-file/title "plugins.edn에서 설치"
  :plugin.install-from-file/title "plugins.edn에서 설치"
  :plugin.install-from-file/notice "다음의 플러그인들이 교체될 것입니다:"
  :plugin.install-from-file/notice "다음의 플러그인들이 교체될 것입니다:"
  :plugin.install-from-file/success "플러그인 설치 성공!"
  :plugin.install-from-file/success "플러그인 설치 성공!"

+ 1 - 0
src/resources/dicts/zh-cn.edn

@@ -357,6 +357,7 @@
  :pdf/linked-ref "转到注解"
  :pdf/linked-ref "转到注解"
  :pdf/toggle-dashed "区域选取为虚线"
  :pdf/toggle-dashed "区域选取为虚线"
  :pdf/hl-block-colored "颜色标识高亮块"
  :pdf/hl-block-colored "颜色标识高亮块"
+ :pdf/auto-open-context-menu "为选中文本自动打开上下文菜单"
  :pdf/doc-metadata "查看文档元数据"
  :pdf/doc-metadata "查看文档元数据"
 
 
  :asset/open-in-browser "在浏览器打开"
  :asset/open-in-browser "在浏览器打开"

+ 1 - 0
src/resources/dicts/zh-hant.edn

@@ -301,6 +301,7 @@
  :pdf/linked-ref "已連結的引用"
  :pdf/linked-ref "已連結的引用"
  :pdf/toggle-dashed "使用線條顯示高亮區塊"
  :pdf/toggle-dashed "使用線條顯示高亮區塊"
  :pdf/hl-block-colored "使用有色標籤顯示高亮區塊"
  :pdf/hl-block-colored "使用有色標籤顯示高亮區塊"
+ :pdf/auto-open-context-menu "為選中文本自動打開上下文菜單"
  :pdf/doc-metadata "metadata 文件"
  :pdf/doc-metadata "metadata 文件"
 
 
  :updater/new-version-install "已下載新版本。"
  :updater/new-version-install "已下載新版本。"