Browse Source

refactor(assets): rename local-asset? to local-relative-asset? and update references

charlie 2 weeks ago
parent
commit
49407d561a

+ 10 - 1
deps/common/src/logseq/common/config.cljs

@@ -41,7 +41,7 @@
 (defonce library-page-name "Library")
 (defonce quick-add-page-name "Quick add")
 
-(defn local-asset?
+(defn local-relative-asset?
   [s]
   (and (string? s)
        (re-find (re-pattern (str "^[./]*" local-assets-dir)) s)))
@@ -51,6 +51,15 @@
   (when (string? s)
     (string/starts-with? s asset-protocol)))
 
+(defn protocol-path?
+  [s]
+  (try
+    (let [url (js/URL. s)]
+      (some? (.-protocol url)))
+    (catch :default _
+      false)))
+
+
 (defn remove-asset-protocol
   [s]
   (if (local-protocol-asset? s)

+ 2 - 2
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -52,7 +52,7 @@
                   (and
                    (= url-type "Page_ref")
                    (and (string? value)
-                        (not (or (common-config/local-asset? value)
+                        (not (or (common-config/local-relative-asset? value)
                                  (common-config/draw? value))))
                    value)
 
@@ -63,7 +63,7 @@
 
                   (and (= url-type "Search")
                        (= format :org)
-                       (not (common-config/local-asset? value))
+                       (not (common-config/local-relative-asset? value))
                        value)
 
                   (and

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -900,7 +900,7 @@
        (cond
          (and (vector? x)
               (= "Link" (first x))
-              (common-config/local-asset? (second (:url (second x)))))
+              (common-config/local-relative-asset? (second (:url (second x)))))
          (swap! results update :asset-links conj x)
          (and (vector? x)
               (= "Macro" (first x))

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser/mldoc.cljc

@@ -196,7 +196,7 @@
                 (common-config/draw? ref-value)
 
                 ;; 3. local asset link
-                (boolean (common-config/local-asset? ref-value))))))))
+                (boolean (common-config/local-relative-asset? ref-value))))))))
 
 (defn link?
   [format link]

File diff suppressed because it is too large
+ 374 - 367
src/main/frontend/components/block.cljs


+ 16 - 14
src/main/frontend/extensions/pdf/assets.cljs

@@ -35,26 +35,28 @@
 (defn get-in-repo-assets-full-filename
   [url]
   (let [repo-dir (config/get-repo-dir (state/get-current-repo))]
-    (when (some-> url (string/trim) (string/includes? repo-dir))
+    (if (some-> url (string/trim) (string/includes? repo-dir))
       (some-> (string/split url repo-dir)
               (last)
-              (string/replace-first "/assets/" "")))))
+              (string/replace-first "/assets/" ""))
+      url)))
 
 (defn inflate-asset
   [original-path & {:keys [href block]}]
   (let [web-link? (string/starts-with? original-path "http")
-        blob-res? (some-> href (string/starts-with? "blob"))
-        asset-res? (some-> href (string/starts-with? "assets"))
-        filename  (util/node-path.basename original-path)
-        ext-name  "pdf"
-        url       (if blob-res? href
-                      (assets-handler/normalize-asset-resource-url original-path))
-        filename' (if (or asset-res? web-link? blob-res?) filename
-                      (some-> url (js/decodeURIComponent)
-                              (get-in-repo-assets-full-filename)
-                              (string/replace '"/" "_")))
-        filekey   (gp-exporter/safe-sanitize-file-name
-                   (subs filename' 0 (- (count filename') (inc (count ext-name)))))]
+        protocol-link? (common-config/protocol-path? href)
+        filename (util/node-path.basename original-path)
+        ext-name "pdf"
+        url (if protocol-link?
+              href
+              (assets-handler/normalize-asset-resource-url original-path))
+        filename' (if protocol-link?
+                    filename
+                    (some-> url (js/decodeURIComponent)
+                            (get-in-repo-assets-full-filename)
+                            (string/replace '"/" "_")))
+        filekey (gp-exporter/safe-sanitize-file-name
+                 (subs filename' 0 (- (count filename') (inc (count ext-name)))))]
     (when-let [key (and (not (string/blank? filekey))
                         (if web-link?
                           (str filekey "__" (hash url))

+ 65 - 65
src/main/frontend/extensions/pdf/core.cljs

@@ -767,7 +767,7 @@
        :add-hl! add-hl!})]))
 
 (rum/defc ^:large-vars/data-var pdf-viewer
-  [_url ^js pdf-document {:keys [identity filename initial-hls initial-page initial-scale initial-error]} ops]
+  [_url ^js pdf-document {:keys [identity filename pdf-current initial-hls initial-page initial-scale initial-error]} ops]
   (let [*el-ref (rum/create-ref)
         [state, set-state!] (rum/use-state {:viewer nil :bus nil :link nil :el nil})
         [ano-state, set-ano-state!] (rum/use-state {:loaded-pages []})
@@ -879,7 +879,11 @@
        (when (and page-ready? viewer)
          [(when-not in-system-window?
             (rum/with-key (pdf-resizer viewer) "pdf-resizer"))
-          (rum/with-key (pdf-toolbar viewer {:on-external-window! #(open-external-win! (state/get-current-pdf))}) "pdf-toolbar")])])))
+          (rum/with-key
+           (pdf-toolbar viewer
+            {:on-external-window! #(open-external-win! (state/get-current-pdf))
+             :pdf-current pdf-current})
+           "pdf-toolbar")])])))
 
 (rum/defcs pdf-password-input <
   (rum/local "" ::password)
@@ -931,9 +935,10 @@
       "auto"))
 
 (rum/defc ^:large-vars/data-var pdf-loader
-  [{:keys [url hls-file identity filename] :as pdf-current}]
+  [{:keys [url hls-file identity filename block] :as pdf-current}]
   (let [repo           (state/get-current-repo)
         db-based?      (config/db-based-graph?)
+        file-based?    (not db-based?)
         *doc-ref       (rum/use-ref nil)
         [loader-state, set-loader-state!] (rum/use-state {:error nil :pdf-document nil :status nil})
         [hls-state, set-hls-state!] (rum/use-state {:initial-hls nil :latest-hls nil :extra nil :loaded false :error nil})
@@ -944,23 +949,20 @@
                          (set-hls-state! #(merge % {:initial-hls [] :latest-hls latest-hls})))
         set-hls-extra! (fn [extra]
                          (if db-based?
-                           (do
+                           (when block
                              (debounce-set-last-visit-scale! (:block pdf-current) (:scale extra))
                              (debounce-set-last-visit-page! (:block pdf-current) (:page extra)))
                            (set-hls-state! #(merge % {:extra extra}))))]
 
-    ;; current pdf effects
-    (when-not db-based?
-      (hooks/use-effect!
-       (fn []
+    (hooks/use-effect!
+     (fn []
+       (when file-based?
+          ;; ensure ref page
          (when pdf-current
            (pdf-assets/file-based-ensure-ref-page! pdf-current)))
-       [pdf-current]))
 
-    ;; load highlights
-    (if db-based?
-      (hooks/use-effect!
-       (fn []
+       (when file-based?
+         ;; load highlights
          (when pdf-current
            (let [pdf-block (:block pdf-current)]
              (p/let [data (db-async/<get-pdf-annotations repo (:db/id pdf-block))
@@ -970,53 +972,53 @@
                                    1))
                (set-initial-scale! (get-last-visit-scale pdf-block))
                (set-hls-state! {:initial-hls highlights :latest-hls highlights :loaded true})))))
-       [pdf-current])
-      (hooks/use-effect!
-       (fn []
-         (p/catch
-          (p/let [data (pdf-assets/file-based-load-hls-data$ pdf-current)
-                  {:keys [highlights extra]} data]
-            (set-initial-page! (or (when-let [page (:page extra)]
-                                     (util/safe-parse-int page)) 1))
-            (set-initial-scale! (or (:scale extra) "auto"))
-            (set-hls-state! {:initial-hls highlights :latest-hls highlights :extra extra :loaded true}))
-
-          ;; error
-          (fn [^js e]
-            (js/console.error "[load hls error]" e)
-
-            (let [msg (str (util/format "Error: failed to load the highlights file: \"%s\". \n"
-                                        (:hls-file pdf-current))
-                           e)]
-              (notification/show! msg :error)
-              (set-hls-state! {:loaded true :error e}))))
-
-         ;; cancel
-         #())
-       [hls-file]))
+       #())
+     [pdf-current])
 
-    ;; cache highlights
-    (when-not db-based?
-      (let [persist-hls-data!
-            (hooks/use-callback
-             (util/debounce
-              (fn [latest-hls extra]
-                (pdf-assets/file-based-persist-hls-data$
-                 pdf-current latest-hls extra))
-              4000) [pdf-current])]
-
-        (hooks/use-effect!
-         (fn []
-           (when (= :completed (:status loader-state))
+    (hooks/use-effect!
+     (fn []
+       (if file-based?
+         (-> (p/let [data (pdf-assets/file-based-load-hls-data$ pdf-current)
+                     {:keys [highlights extra]} data]
+               (set-initial-page! (or (when-let [page (:page extra)]
+                                        (util/safe-parse-int page)) 1))
+               (set-initial-scale! (or (:scale extra) "auto"))
+               (set-hls-state! {:initial-hls highlights :latest-hls highlights :extra extra :loaded true}))
              (p/catch
-              (when-not (:error hls-state)
-                (p/do! (persist-hls-data! (:latest-hls hls-state) (:extra hls-state))))
+              (fn [^js e]
+                (js/console.error "[load hls error]" e)
+                (let [msg (str (util/format "Error: failed to load the highlights file: \"%s\". \n"
+                                            (:hls-file pdf-current))
+                               e)]
+                  (notification/show! msg :error)
+                  (set-hls-state! {:loaded true :error e})))))
+         ;; for db-based, just mark loaded
+         (set-hls-state! {:loaded true}))
+       #())
+     [hls-file pdf-current])
 
-            ;; write hls file error
-              (fn [e]
-                (js/console.error "[write hls error]" e)))))
+    ;; cache highlights
+    (let [persist-hls-data!
+          (hooks/use-callback
+           (util/debounce
+            (fn [latest-hls extra]
+              (pdf-assets/file-based-persist-hls-data$
+               pdf-current latest-hls extra))
+            4000) [pdf-current])]
 
-         [(:latest-hls hls-state) (:extra hls-state)])))
+      (hooks/use-effect!
+       (fn []
+         ;; persist highlights
+         (when file-based?
+           (when (= :completed (:status loader-state))
+             (-> (when-not (:error hls-state)
+                   (p/do! (persist-hls-data! (:latest-hls hls-state) (:extra hls-state))))
+                 (p/catch
+                  (fn [e]
+                    (js/console.error "[write hls error]" e))))))
+         #())
+
+       [(:latest-hls hls-state) (:extra hls-state)]))
 
     ;; load document
     (hooks/use-effect!
@@ -1033,7 +1035,6 @@
                             :supportsMouseWheelZoomCtrlKey true
                             :supportsMouseWheelZoomMetaKey true}]
          (set-loader-state! {:status :loading})
-
          (-> (get-doc$ (clj->js opts))
              (p/then (fn [doc]
                        (set-loader-state! {:pdf-document doc :status :completed})))
@@ -1041,6 +1042,7 @@
          #()))
      [url doc-password])
 
+    ;; handle load errors
     (hooks/use-effect!
      (fn []
        (when-let [error (:error loader-state)]
@@ -1092,17 +1094,15 @@
             initial-error (:error hls-state)]
 
         (if (= status-doc :loading)
-
-          [:div.flex.justify-center.items-center.h-screen.text-gray-500.text-lg
-           svg/loading]
-
+          [:div.flex.justify-center.items-center.h-screen.text-gray-500.text-lg svg/loading]
           (when-let [pdf-document (and (:loaded hls-state) (:pdf-document loader-state))]
             [(rum/with-key (pdf-viewer
                             url pdf-document
-                            {:identity      identity
-                             :filename      filename
-                             :initial-hls   initial-hls
-                             :initial-page  initial-page
+                            {:identity identity
+                             :filename filename
+                             :pdf-current pdf-current
+                             :initial-hls initial-hls
+                             :initial-page initial-page
                              :initial-scale initial-scale
                              :initial-error initial-error}
                             {:set-dirty-hls! set-dirty-hls!

+ 8 - 5
src/main/frontend/extensions/pdf/toolbar.cljs

@@ -477,7 +477,7 @@
          (pdf-highlights-list viewer))]]]))
 
 (rum/defc ^:large-vars/cleanup-todo pdf-toolbar
-  [^js viewer {:keys [on-external-window!]}]
+  [^js viewer {:keys [on-external-window! pdf-current]}]
   (let [[area-mode?, set-area-mode!] (use-atom *area-mode?)
         [outline-visible?, set-outline-visible!] (rum/use-state false)
         [finder-visible?, set-finder-visible!] (rum/use-state false)
@@ -490,6 +490,8 @@
         group-id          (.-$groupIdentity viewer)
         in-system-window? (.-$inSystemWindow viewer)
         doc               (pdf-windows/resolve-own-document viewer)
+        ;; asset block container for db mode
+        asset-block (:block pdf-current)
         dispatch-extra-state!
         (fn []
           (js/setTimeout
@@ -594,10 +596,11 @@
          (svg/search2 19)]
 
         ;; annotations
-        [:a.button
-         {:title    "Annotations page"
-          :on-click #(pdf-assets/goto-annotations-page! (:pdf/current @state/state))}
-         (svg/annotations 16)]
+        (when asset-block
+          [:a.button
+           {:title "Annotations page"
+            :on-click #(pdf-assets/goto-annotations-page! (:pdf/current @state/state))}
+           (svg/annotations 16)])
 
         ;; system window
         [:a.button

+ 1 - 1
src/main/frontend/fs/watcher_handler.cljs

@@ -95,7 +95,7 @@
 
                 (and (= "change" type)
                      (= dir repo-dir)
-                     (not (common-config/local-asset? path)))
+                     (not (common-config/local-relative-asset? path)))
                 (handle-add-and-change! repo path content db-content ctime mtime (not global-dir)) ;; no backup for global dir
 
                 (and (= "unlink" type)

+ 34 - 31
src/main/frontend/handler/assets.cljs

@@ -87,8 +87,7 @@
 (defn normalize-asset-resource-url
   "try to convert resource file to url asset link"
   [path]
-  (let [protocol-link? (->> #{"file://" "http://" "https://" "assets://"}
-                            (some #(string/starts-with? (string/lower-case path) %)))]
+  (let [protocol-link? (common-config/protocol-path? path)]
     (cond
       protocol-link?
       path
@@ -152,35 +151,39 @@
 
 (defn <make-asset-url
   "Make asset URL for UI element, to fill img.src"
-  [path] ;; path start with "/assets"(editor) or compatible for "../assets"(whiteboards)
-  (if config/publishing?
-    ;; Relative path needed since assets are not under '/' if published graph is not under '/'
-    (string/replace-first path #"^/" "")
-    (let [repo      (state/get-current-repo)
-          repo-dir  (config/get-repo-dir repo)
-          ;; Hack for path calculation
-          path      (string/replace path #"^(\.\.)?/" "./")
-          full-path (path/path-join repo-dir path)
-          data-url? (string/starts-with? path "data:")]
-      (cond
-        data-url?
-        path ;; just return the original
-
-        (and (alias-enabled?)
-             (check-alias-path? path))
-        (resolve-asset-real-path-url (state/get-current-repo) path)
-
-        (util/electron?)
-        ;; fullpath will be encoded
-        (path/prepend-protocol "file:" full-path)
-
-        ;(mobile-util/native-platform?)
-        ;(mobile-util/convert-file-src full-path)
-
-        (config/db-based-graph? (state/get-current-repo)) ; memory fs
-        (p/let [binary (fs/read-file-raw repo-dir path {})
-                blob (js/Blob. (array binary) (clj->js {:type "image"}))]
-          (when blob (js/URL.createObjectURL blob)))))))
+  ([path] (<make-asset-url path (try (js/URL. path) (catch :default _ nil))))
+  ([path ^js js-url]
+   ;; path start with "/assets"(editor) or compatible for "../assets"(whiteboards)
+   (if config/publishing?
+     ;; Relative path needed since assets are not under '/' if published graph is not under '/'
+     (string/replace-first path #"^/" "")
+     (let [repo (state/get-current-repo)
+           repo-dir (config/get-repo-dir repo)
+           local-asset? (common-config/local-relative-asset? path)
+           ;; Hack for path calculation
+           path (string/replace path #"^(\.\.)?/" "./")
+           js-url? (not (nil? js-url))]
+       (cond
+         js-url?
+         path                                               ;; just return the original
+
+         (and (alias-enabled?)
+              (check-alias-path? path))
+         (resolve-asset-real-path-url (state/get-current-repo) path)
+
+         (util/electron?)
+         (let [full-path (if local-asset?
+                           (path/path-join repo-dir path) path)]
+           ;; fullpath will be encoded
+           (path/prepend-protocol "file:" full-path))
+
+         ;(mobile-util/native-platform?)
+         ;(mobile-util/convert-file-src full-path)
+
+         (config/db-based-graph? (state/get-current-repo))  ; memory fs
+         (p/let [binary (fs/read-file-raw repo-dir path {})
+                 blob (js/Blob. (array binary) (clj->js {:type "image"}))]
+           (when blob (js/URL.createObjectURL blob))))))))
 
 (defn get-file-checksum
   [^js/Blob file]

Some files were not shown because too many files changed in this diff