Ver Fonte

fix: local file path is not working on MD and DB

related to https://github.com/logseq/db-test/issues/389
Tienson Qin há 2 meses atrás
pai
commit
37736741e6
2 ficheiros alterados com 25 adições e 34 exclusões
  1. 6 2
      resources/js/preload.js
  2. 19 32
      src/main/frontend/components/block.cljs

+ 6 - 2
resources/js/preload.js

@@ -1,5 +1,6 @@
 const fs = require('fs')
 const path = require('path')
+const os = require('os')
 const { ipcRenderer, contextBridge, shell, clipboard, webFrame } = require('electron')
 
 const IS_MAC = process.platform === 'darwin'
@@ -86,8 +87,11 @@ contextBridge.exposeInMainWorld('apis', {
     await shell.openExternal(url, options)
   },
 
-  async openPath (path) {
-    await shell.openPath(path)
+  async openPath (relativePath) {
+    const absolutePath = path.resolve(
+      relativePath.startsWith('~') ? path.join(os.homedir(), relativePath.slice(1)) : relativePath
+    );
+    await shell.openPath(absolutePath)
   },
 
   /**

+ 19 - 32
src/main/frontend/components/block.cljs

@@ -1533,7 +1533,7 @@
     (show-link? config metadata s full_text)
     (media-link config url s label metadata full_text)
 
-    (or (util/electron?) (config/db-based-graph? (state/get-current-repo)))
+    (util/electron?)
     (let [path (cond
                  (string/starts-with? s "file://")
                  (string/replace s "file://" "")
@@ -1591,7 +1591,7 @@
 
       :else
       (let [href (string-of-url url)
-            [protocol path] (or (and (= "Complex" (first url)) url)
+            [protocol path] (or (and (= "Complex" (first url)) [(:protocol (second url)) (:link (second url))])
                                 (and (= "File" (first url)) ["file" (second url)]))]
         (cond
           (and (= (get-in config [:block :block/format] :markdown) :org)
@@ -1609,36 +1609,23 @@
           (= protocol "file")
           (if (show-link? config metadata href full_text)
             (media-link config url href label metadata full_text)
-            (let [redirect-page-name (when (string? path) (text/get-page-name path))
-                  config (assoc config :redirect-page-name redirect-page-name)
-                  label-text (get-label-text label)
-                  page (if (string/blank? label-text)
-                         {:block/name (file-model/get-file-page (string/replace href "file:" "") false)}
-                         (get-page label))
-                  show-brackets? (state/show-brackets?)]
-              (if (and page
-                       (when-let [ext (util/get-file-ext href)]
-                         (common-config/mldoc-support? ext)))
-                [:span.page-reference
-                 (when show-brackets? [:span.text-gray-500 page-ref/left-brackets])
-                 (page-cp config page)
-                 (when show-brackets? [:span.text-gray-500 page-ref/right-brackets])]
-
-                (let [href* (if (util/electron?)
-                              (relative-assets-path->absolute-path href)
-                              href)]
-                  (->elem
-                   :a
-                   (cond-> (if (util/electron?)
-                             {:on-click (fn [e]
-                                          (util/stop e)
-                                          (js/window.apis.openPath path))
-                              :data-href href*}
-                             {:href      (path/path-join "file://" href*)
-                              :data-href href*
-                              :target    "_blank"})
-                     title (assoc :title title))
-                   (map-inline config label))))))
+            (let [href* (if (util/electron?)
+                          (relative-assets-path->absolute-path href)
+                          href)]
+              [:div.flex.flex-row.items-center
+               (ui/icon "file" {:class "opacity-50"})
+               (->elem
+                :a
+                (cond-> (if (util/electron?)
+                          {:on-click (fn [e]
+                                       (util/stop e)
+                                       (js/window.apis.openPath path))
+                           :data-href href*}
+                          {:href      (path/path-join "file://" href*)
+                           :data-href href*
+                           :target    "_blank"})
+                  title (assoc :title title))
+                (map-inline config label))]))
 
           (show-link? config metadata href full_text)
           (media-link config url href label metadata full_text)