From c38679ff45113ebb297da086634674b1ab332af9 Mon Sep 17 00:00:00 2001 From: charlie Date: Wed, 12 Jun 2024 13:45:46 +0800 Subject: [PATCH] fix(pdf): support browser to insert pdf highlight into page blocks --- src/main/frontend/components/block.cljs | 7 +-- src/main/frontend/extensions/pdf/assets.cljs | 36 +++++++------ src/main/frontend/extensions/pdf/core.cljs | 54 ++++++++++--------- src/main/frontend/extensions/pdf/toolbar.cljs | 11 ++-- src/main/frontend/extensions/pdf/utils.cljs | 7 +++ src/main/frontend/handler/editor.cljs | 33 ++++++------ 6 files changed, 81 insertions(+), 67 deletions(-) diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index b25e3687c..5ebff8494 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -1908,9 +1908,7 @@ level (:level config) slide? (boolean (:slide? config)) block-ref? (:block-ref? config) - block-type (or (keyword - (pu/lookup properties :logseq.property/ls-type)) - :default) + block-type (or (keyword (pu/lookup properties :logseq.property/ls-type)) :default) html-export? (:html-export? config) bg-color (pu/lookup properties :logseq.property/background-color) ;; `heading-level` is for backward compatibility, will remove it in later releases @@ -1943,8 +1941,7 @@ ;; children (let [area? (= :area (keyword (pu/lookup properties :logseq.property/hl-type))) - hl-ref #(when (and (or config/publishing? (util/electron?)) - (not (#{:default :whiteboard-shape} block-type))) + hl-ref #(when (not (#{:default :whiteboard-shape} block-type)) [:div.prefix-link {:on-pointer-down (fn [^js e] diff --git a/src/main/frontend/extensions/pdf/assets.cljs b/src/main/frontend/extensions/pdf/assets.cljs index 9a7ce44e5..09be846c8 100644 --- a/src/main/frontend/extensions/pdf/assets.cljs +++ b/src/main/frontend/extensions/pdf/assets.cljs @@ -183,9 +183,13 @@ url}}) (db-model/get-page page-name))) - ;; try to update file path (do - (property-handler/add-page-property! page-name (pu/get-pid :logseq.property.pdf/file-path) url) + ;; try to update file path + (when (nil? (some-> page + (:block/properties) + (:logseq.property.pdf/file-path))) + (property-handler/add-page-property! + page-name (pu/get-pid :logseq.property.pdf/file-path) url)) page))))) (defn ensure-ref-block! @@ -210,7 +214,7 @@ (pu/get-pid :logseq.property/hl-color) (:color properties)} (not (config/db-based-graph? (state/get-current-repo))) ;; force custom uuid - (assoc :id (str id))) + (assoc :id (if (string? id) (uuid id) id))) properties (wrap-props props)] (when (string? text) ;; FIXME: Handle properties for db graphs @@ -237,22 +241,24 @@ (defn open-block-ref! [block] - (let [id (:block/uuid block) - page (db-utils/pull (:db/id (:block/page block))) + (let [id (:block/uuid block) + page (db-utils/pull (:db/id (:block/page block))) page-name (:block/original-name page) file-path (pu/get-block-property-value block :logseq.property.pdf/file-path) - hl-page (pu/get-block-property-value block :logseq.property.pdf/hl-page)] + hl-page (pu/get-block-property-value block :logseq.property.pdf/hl-page) + db-base? (config/db-based-graph? (state/get-current-repo))] (when-let [target-key (and page-name (subs page-name 5))] (p/let [hls (resolve-hls-data-by-key$ target-key) - hls (and hls (:highlights hls))] - (let [file-path (or file-path (str "../assets/" target-key ".pdf"))] - (if-let [matched (or (and hls (medley/find-first #(= id (:id %)) hls)) - (and hl-page {:page hl-page}))] - (do - (state/set-state! :pdf/ref-highlight matched) - ;; open pdf viewer - (state/set-current-pdf! (inflate-asset file-path))) - (js/console.debug "[Unmatched highlight ref]" block))))))) + hls (and hls (:highlights hls)) + file-path (or file-path (str "../assets/" target-key ".pdf")) + href (and db-base? (assets-handler/make-asset-url file-path))] + (if-let [matched (or (and hls (medley/find-first #(= id (:id %)) hls)) + (and hl-page {:page hl-page}))] + (do + (state/set-state! :pdf/ref-highlight matched) + ;; open pdf viewer + (state/set-current-pdf! (inflate-asset file-path {:href href}))) + (js/console.debug "[Unmatched highlight ref]" block)))))) (defn goto-block-ref! [{:keys [id] :as hl}] diff --git a/src/main/frontend/extensions/pdf/core.cljs b/src/main/frontend/extensions/pdf/core.cljs index d26ff233c..daf7a0c0d 100644 --- a/src/main/frontend/extensions/pdf/core.cljs +++ b/src/main/frontend/extensions/pdf/core.cljs @@ -537,22 +537,22 @@ (rum/defc ^:large-vars/cleanup-todo pdf-highlights [^js el ^js viewer initial-hls loaded-pages {:keys [set-dirty-hls!]}] - (let [^js doc (.-ownerDocument el) - ^js win (.-defaultView doc) - *mounted (rum/use-ref false) + (let [^js doc (.-ownerDocument el) + ^js win (.-defaultView doc) + *mounted (rum/use-ref false) [sel-state, set-sel-state!] (rum/use-state {:selection nil :range nil :collapsed nil :point nil}) [highlights, set-highlights!] (rum/use-state initial-hls) [ctx-menu-state, set-ctx-menu-state!] (rum/use-state {:highlight nil :vw-pos nil :selection nil :point nil :reset-fn nil}) clear-ctx-menu! (rum/use-callback - #(let [reset-fn (:reset-fn ctx-menu-state)] - (set-ctx-menu-state! {}) - (and (fn? reset-fn) (reset-fn))) - [ctx-menu-state]) + #(let [reset-fn (:reset-fn ctx-menu-state)] + (set-ctx-menu-state! {}) + (and (fn? reset-fn) (reset-fn))) + [ctx-menu-state]) - show-ctx-menu! (fn [^js viewer hl point & ops] - (let [vw-pos (pdf-utils/scaled-to-vw-pos viewer (:position hl))] - (set-ctx-menu-state! (apply merge (list* {:highlight hl :vw-pos vw-pos :point point} ops))))) + show-ctx-menu! (fn [^js viewer hl point & ops] + (let [vw-pos (pdf-utils/scaled-to-vw-pos viewer (:position hl))] + (set-ctx-menu-state! (apply merge (list* {:highlight hl :vw-pos vw-pos :point point} ops))))) add-hl! (fn [hl] (when (:id hl) @@ -561,20 +561,21 @@ (set-highlights! (conj highlights hl))) (when-let [vw-pos (and (pdf-assets/area-highlight? hl) - (pdf-utils/scaled-to-vw-pos viewer (:position hl)))] + (pdf-utils/scaled-to-vw-pos viewer (:position hl)))] ;; exceptions (pdf-assets/persist-hl-area-image$ viewer (:pdf/current @state/state) - hl nil (:bounding vw-pos))))) + hl nil (:bounding vw-pos))))) - upd-hl! (fn [hl] - (let [highlights (pdf-utils/fix-nested-js highlights)] - (when-let [[target-idx] (medley/find-first - #(= (:id (second %)) (:id hl)) - (medley/indexed highlights))] - (set-highlights! (assoc-in highlights [target-idx] hl)) - (pdf-assets/update-hl-block! hl)))) + upd-hl! (fn [hl] + (let [highlights (pdf-utils/fix-nested-js highlights)] + (when-let [[target-idx] (medley/find-first + #(= (:id (second %)) (:id hl)) + (medley/indexed highlights))] + (set-highlights! (assoc-in highlights [target-idx] hl)) + (pdf-assets/update-hl-block! hl)))) - del-hl! (fn [hl] (when-let [id (:id hl)] (set-highlights! (into [] (remove #(= id (:id %)) highlights)))))] + del-hl! (fn [hl] (when-let [id (:id hl)] + (set-highlights! (into [] (remove #(= id (:id %)) highlights)))))] ;; consume dirtied (rum/use-effect! @@ -712,12 +713,13 @@ (pdf-page-finder viewer) ;; area selection container - (pdf-highlight-area-selection - viewer - {:clear-ctx-menu! clear-ctx-menu! - :show-ctx-menu! show-ctx-menu! - :add-hl! add-hl! - })])) + (when (pdf-utils/support-area?) + (pdf-highlight-area-selection + viewer + {:clear-ctx-menu! clear-ctx-menu! + :show-ctx-menu! show-ctx-menu! + :add-hl! add-hl! + }))])) (rum/defc ^:large-vars/data-var pdf-viewer [_url ^js pdf-document {:keys [identity filename initial-hls initial-page initial-error]} ops] diff --git a/src/main/frontend/extensions/pdf/toolbar.cljs b/src/main/frontend/extensions/pdf/toolbar.cljs index 1ac204a70..f34c18799 100644 --- a/src/main/frontend/extensions/pdf/toolbar.cljs +++ b/src/main/frontend/extensions/pdf/toolbar.cljs @@ -510,11 +510,12 @@ (svg/adjustments 18)] ;; selection - [:a.button - {:title (str "Area highlight (" (if util/mac? "⌘" "Shift") ")") - :class (when area-mode? "is-active") - :on-click #(set-area-mode! (not area-mode?))} - (svg/icon-area 18)] + (when (pdf-utils/support-area?) + [:a.button + {:title (str "Area highlight (" (if util/mac? "⌘" "Shift") ")") + :class (when area-mode? "is-active") + :on-click #(set-area-mode! (not area-mode?))} + (svg/icon-area 18)]) [:a.button {:title "Highlight mode" diff --git a/src/main/frontend/extensions/pdf/utils.cljs b/src/main/frontend/extensions/pdf/utils.cljs index 1e2510d04..8d7590169 100644 --- a/src/main/frontend/extensions/pdf/utils.cljs +++ b/src/main/frontend/extensions/pdf/utils.cljs @@ -2,6 +2,8 @@ (:require [promesa.core :as p] [cljs-bean.core :as bean] [frontend.util :as util] + [frontend.config :as config] + [frontend.state :as state] ["/frontend/extensions/pdf/utils" :as js-utils] [datascript.core :as d] [clojure.string :as string])) @@ -14,6 +16,11 @@ [filename] (and filename (string? filename) (string/starts-with? filename "hls__"))) +(defn support-area? + [] + (and (util/electron?) + (not (config/db-based-graph? (state/get-current-repo))))) + (defn get-bounding-rect [rects] (bean/->clj (js-utils/getBoundingRect (bean/->js rects)))) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 0ea1e787c..621c71b33 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -22,6 +22,7 @@ [frontend.handler.common :as common-handler] [frontend.handler.db-based.editor :as db-editor-handler] [frontend.handler.db-based.property.util :as db-pu] + [frontend.handler.db-based.property :as db-p] [frontend.handler.export.html :as export-html] [frontend.handler.export.text :as export-text] [frontend.handler.file-based.editor :as file-editor-handler] @@ -68,6 +69,7 @@ [logseq.outliner.core :as outliner-core] [promesa.core :as p] [rum.core :as rum] + [logseq.db.frontend.property.type :as db-property-type] [logseq.outliner.property :as outliner-property])) ;; FIXME: should support multiple images concurrently uploading @@ -528,8 +530,7 @@ sibling? (if before? true (if page false sibling?)) block (if page (db/get-page page) - (db/entity [:block/uuid block-uuid])) - db-based? (config/db-based-graph? repo)] + (db/entity [:block/uuid block-uuid]))] (when block (let [last-block (when (not sibling?) (let [children (:block/_parent block) @@ -574,22 +575,22 @@ ;; FIXME: assert :else - nil) - new-block' (if db-based? - (merge new-block properties) - new-block)] + nil)] (when block-m (p/do! - (outliner-insert-block! {} block-m new-block' {:sibling? sibling? - :keep-uuid? true - :ordered-list? ordered-list? - :replace-empty-target? replace-empty-target?}) - (when edit-block? - (if (and replace-empty-target? - (string/blank? (:block/content last-block))) - (edit-block! last-block :max) - (edit-block! new-block :max))) - new-block))))))) + (ui-outliner-tx/transact! + {:outliner-op :insert-blocks} + (outliner-insert-block! {} block-m new-block {:sibling? sibling? + :keep-uuid? true + :ordered-list? ordered-list? + :replace-empty-target? replace-empty-target?}) + (db-p/set-block-properties! (:block/uuid new-block) properties)) + (when edit-block? + (if (and replace-empty-target? + (string/blank? (:block/content last-block))) + (edit-block! last-block :max) + (edit-block! new-block :max))) + new-block))))))) (defn insert-first-page-block-if-not-exists! ([page-title]