diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 4994b1c084..b7ee391642 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -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)] diff --git a/src/main/frontend/util.cljc b/src/main/frontend/util.cljc index b1b43fcf3f..87d45cddb6 100644 --- a/src/main/frontend/util.cljc +++ b/src/main/frontend/util.cljc @@ -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