Enhance/pdf improvements (#6475)

Full-text search, highlights and assets alias support 
It also exposes a plugin API for highlight context menu   
    ```ts
    /**
       * Current it's only available for pdf viewer
       * @param label - displayed name of command
       * @param action - callback for the clickable item
       * @param opts - clearSelection: clear highlight selection when callback invoked
       */
      registerHighlightContextMenuItem: (
        label: string,
        action: SimpleCommandCallback,
        opts?: {
          clearSelection: boolean
        }
      ) => unknown
    ```
This commit is contained in:
Charlie
2022-10-09 20:44:18 +08:00
committed by GitHub
parent ad4fec8ce7
commit d53ac94bfc
50 changed files with 3863 additions and 1359 deletions

View File

@@ -3,14 +3,30 @@
[cljs-bean.core :as bean]
[frontend.util :as util]
["/frontend/extensions/pdf/utils" :as js-utils]
[frontend.db :as db]
[frontend.loader :refer [load]]
[datascript.core :as d]
[logseq.graph-parser.config :as gp-config]
[clojure.string :as string]))
(defonce MAX-SCALE 5.0)
(defonce MIN-SCALE 0.25)
(defonce DELTA_SCALE 1.05)
(defn clean-asset-path-prefix
[path]
(when (string? path)
(string/replace-first path #"^[.\/\\]*(assets)[\/\\]+" "")))
(defn get-area-block-asset-url
[block page]
(when-some [props (and block page (:block/properties block))]
(when-some [uuid (:block/uuid block)]
(when-some [stamp (:hl-stamp props)]
(let [group-key (string/replace-first (:block/original-name page) #"^hls__" "")
hl-page (:hl-page props)
encoded-chars? (boolean (re-find #"(?i)%[0-9a-f]{2}" group-key))
group-key (if encoded-chars? (js/encodeURI group-key) group-key)]
(str "./" gp-config/local-assets-dir "/" group-key "/" (str hl-page "_" uuid "_" stamp ".png")))))))
(defn get-bounding-rect
[rects]
(bean/->clj (js-utils/getBoundingRect (bean/->js rects))))
@@ -115,23 +131,12 @@
(mapv #(if (map? %) % (bean/->clj %)) its)))
(defn gen-uuid []
(db/new-block-id))
(defn js-load$
[url]
(p/create
(fn [resolve]
(load url resolve))))
(def PDFJS_ROOT
(if (= js/location.protocol "file:")
"./js"
"./static/js"))
(d/squuid))
(defn load-base-assets$
[]
(p/let [_ (js-load$ (str PDFJS_ROOT "/pdfjs/pdf.js"))
_ (js-load$ (str PDFJS_ROOT "/pdfjs/pdf_viewer.js"))]))
(p/let [_ (util/js-load$ (str util/JS_ROOT "/pdfjs/pdf.js"))
_ (util/js-load$ (str util/JS_ROOT "/pdfjs/pdf_viewer.js"))]))
(defn get-page-from-el
[^js/HTMLElement el]
@@ -181,6 +186,13 @@
(js-invoke js/window.lsPdfViewer "previousPage")
(catch :default _e nil)))
(defn open-finder
[]
(try
(when-let [^js el (js/document.querySelector ".extensions__pdf-toolbar a[title=Search]")]
(.click el))
(catch js/Error _e nil)))
(comment
(fix-selection-text-breakline "this is a\ntest paragraph")
(fix-selection-text-breakline "he is 1\n8 years old")