From 0a5484158501b4260fa6995686391a22b3c5fad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9B=AE=E6=A3=83?= Date: Thu, 29 Feb 2024 15:16:05 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=9B=BE=E7=89=87=E6=B5=AE?= =?UTF-8?q?=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/post/tp-image.vue | 12 ++- src/components/post/tpo-image.vue | 156 ++++++++++++++++++++++++++++++ src/utils/TGShare.ts | 33 +++++-- 3 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 src/components/post/tpo-image.vue diff --git a/src/components/post/tp-image.vue b/src/components/post/tp-image.vue index 8e55c468..340f3cfc 100644 --- a/src/components/post/tp-image.vue +++ b/src/components/post/tp-image.vue @@ -1,5 +1,5 @@ + diff --git a/src/utils/TGShare.ts b/src/utils/TGShare.ts index ab86afb4..457b21a6 100644 --- a/src/utils/TGShare.ts +++ b/src/utils/TGShare.ts @@ -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} 无返回值 */ -async function saveCanvasImg(buffer: Uint8Array, filename: string): Promise { +export async function saveCanvasImg( + buffer: Uint8Array, + filename: string, + format?: string, +): Promise { + 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 { }); } +/** + * @description 返回图片 buffer + * @since Beta v0.4.4 + * @param {string} url - 图片链接 + * @returns {Promise} 图片 buffer + */ +export async function getImageBuffer(url: string): Promise { + return await http + .fetch(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} 无返回值 */ -async function copyToClipboard(buffer: Uint8Array): Promise { +export async function copyToClipboard(buffer: Uint8Array): Promise { const blob = new Blob([buffer], { type: "image/png" }); const url = URL.createObjectURL(blob); await navigator.clipboard.write([