fix: Can't copy image in desktop

Addresses one of the issues in
https://github.com/logseq/db-test/issues/792
This commit is contained in:
Gabriel Horner
2026-04-16 13:26:48 -04:00
parent 8606a8b5f0
commit ea3a732095
2 changed files with 34 additions and 8 deletions

View File

@@ -259,10 +259,32 @@
[:<>
(let [handle-copy!
(fn [_e]
(-> (util/copy-image-to-clipboard image-src)
(p/then #(notification/show! "Copied!" :success))
(p/catch (fn [error]
(js/console.error error)))))
;; Electron renderer cannot fetch file:// URLs; read the
;; file via IPC and copy the blob directly.
(if (util/electron?)
(let [ext (some-> (util/get-file-ext image-src) string/lower-case)
;; Should support all exts in common-config/img-formats
ext->mime {"png" "image/png"
"jpg" "image/jpeg"
"jpeg" "image/jpeg"
"gif" "image/gif"
"webp" "image/webp"
"bmp" "image/bmp"
"svg" "image/svg+xml"
"ico" "image/x-icon"}
mime (get ext->mime ext)]
(if-not mime
(notification/show! (str "Copy image is not supported for ." ext " files") :warning)
(-> (p/let [binary (fs/read-file-raw nil image-src {})
blob (js/Blob. (array binary) (clj->js {:type mime}))]
(util/copy-image-blob-to-clipboard blob))
(p/then #(notification/show! "Copied!" :success))
(p/catch (fn [error]
(js/console.error error))))))
(-> (util/copy-image-to-clipboard src')
(p/then #(notification/show! "Copied!" :success))
(p/catch (fn [error]
(js/console.error error))))))
handle-delete!
(fn [_e]
(when-let [block-id (get-blockid)]

View File

@@ -1314,16 +1314,20 @@
(.catch (fn [err]
(js/console.error "Web clipboard failed" err)))))))))
#?(:cljs
(defn copy-image-blob-to-clipboard
[blob]
(if (= (.-type blob) "image/png")
(write-blob-to-clipboard blob)
(image-blob->png blob write-blob-to-clipboard))))
#?(:cljs
(defn copy-image-to-clipboard
[src]
(-> (js/fetch src)
(.then (fn [data]
(-> (.blob data)
(.then (fn [blob]
(if (= (.-type blob) "image/png")
(write-blob-to-clipboard blob)
(image-blob->png blob write-blob-to-clipboard))))
(.then copy-image-blob-to-clipboard)
(.catch js/console.error)))))))
(defn memoize-last