图片浮窗

This commit is contained in:
目棃
2024-02-29 15:16:05 +08:00
parent 3f483293f5
commit 0a54841585
3 changed files with 191 additions and 10 deletions

View File

@@ -1,7 +1,7 @@
/**
* @file utils/TGShare.ts
* @description 生成分享截图并保存到本地
* @since Beta v0.4.2
* @since Beta v0.4.4
*/
import { dialog, fs, http, path } from "@tauri-apps/api";
@@ -14,17 +14,23 @@ import showSnackbar from "../components/func/snackbar";
/**
* @description 保存图片-canvas
* @since Beta v0.4.2
* @since Beta v0.4.4
* @param {Uint8Array} buffer - 图片数据
* @param {string} filename - 文件名
* @param {string} format - 文件格式
* @returns {Promise<void>} 无返回值
*/
async function saveCanvasImg(buffer: Uint8Array, filename: string): Promise<void> {
export async function saveCanvasImg(
buffer: Uint8Array,
filename: string,
format?: string,
): Promise<void> {
if (format === undefined) format = "png";
await dialog
.save({
title: "保存图片",
filters: [{ name: "图片", extensions: ["png"] }],
defaultPath: `${await path.downloadDir()}${path.sep}${filename}.png`,
filters: [{ name: "图片", extensions: [format] }],
defaultPath: `${await path.downloadDir()}${path.sep}${filename}.${format}`,
})
.then(async (res) => {
if (res === null) return;
@@ -55,6 +61,21 @@ export async function saveImgLocal(url: string): Promise<string> {
});
}
/**
* @description 返回图片 buffer
* @since Beta v0.4.4
* @param {string} url - 图片链接
* @returns {Promise<Uint8Array>} 图片 buffer
*/
export async function getImageBuffer(url: string): Promise<Uint8Array> {
return await http
.fetch<ArrayBuffer>(url, {
method: "GET",
responseType: http.ResponseType.Binary,
})
.then((res) => new Uint8Array(res.data));
}
/**
* @description 获取分享截图背景色
* @since Beta v0.3.2
@@ -151,7 +172,7 @@ export async function generateShareImg(
* @param {Uint8Array} buffer - 图片数据
* @returns {Promise<void>} 无返回值
*/
async function copyToClipboard(buffer: Uint8Array): Promise<void> {
export async function copyToClipboard(buffer: Uint8Array): Promise<void> {
const blob = new Blob([buffer], { type: "image/png" });
const url = URL.createObjectURL(blob);
await navigator.clipboard.write([