fix(pdf): support browser to insert pdf highlight into page blocks

experiment/tanstack-table
charlie 2024-06-12 13:45:46 +08:00
parent d4f21e4c22
commit c38679ff45
6 changed files with 81 additions and 67 deletions

View File

@ -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]

View File

@ -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
@ -241,18 +245,20 @@
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"))]
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)))
(js/console.debug "[Unmatched highlight ref]" block)))))))
(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}]

View File

@ -574,7 +574,8 @@
(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
(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]

View File

@ -510,11 +510,12 @@
(svg/adjustments 18)]
;; selection
(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)]
(svg/icon-area 18)])
[:a.button
{:title "Highlight mode"

View File

@ -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))))

View File

@ -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,16 +575,16 @@
;; 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?
(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)))