mirror of
https://github.com/FranP-code/ChatGPT.git
synced 2025-10-13 00:13:25 +00:00
feat: optimize pdf size
This commit is contained in:
@@ -1,5 +1,10 @@
|
|||||||
# UPDATE LOG
|
# UPDATE LOG
|
||||||
|
|
||||||
|
## v0.5.2
|
||||||
|
|
||||||
|
feat:
|
||||||
|
- optimize the generated pdf file size
|
||||||
|
|
||||||
## v0.5.1
|
## v0.5.1
|
||||||
|
|
||||||
some optimization
|
some optimization
|
||||||
|
|||||||
105
src-tauri/src/assets/export.js
vendored
105
src-tauri/src/assets/export.js
vendored
@@ -1,6 +1,7 @@
|
|||||||
// *** Core Script - Export ***
|
// *** Core Script - Export ***
|
||||||
// @ref: https://github.com/liady/ChatGPT-pdf
|
// @ref: https://github.com/liady/ChatGPT-pdf
|
||||||
|
|
||||||
|
const buttonOuterHTMLFallback = `<button class="btn flex justify-center gap-2 btn-neutral" id="download-png-button">Try Again</button>`;
|
||||||
async function init() {
|
async function init() {
|
||||||
const chatConf = await invoke('get_chat_conf') || {};
|
const chatConf = await invoke('get_chat_conf') || {};
|
||||||
if (window.buttonsInterval) {
|
if (window.buttonsInterval) {
|
||||||
@@ -11,14 +12,15 @@ async function init() {
|
|||||||
if (!actionsArea) {
|
if (!actionsArea) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const buttons = actionsArea.querySelectorAll("button");
|
if (shouldAddButtons(actionsArea)) {
|
||||||
const hasTryAgainButton = Array.from(buttons).some((button) => {
|
let TryAgainButton = actionsArea.querySelector("button");
|
||||||
return !button.id?.includes("download");
|
if (!TryAgainButton) {
|
||||||
});
|
const parentNode = document.createElement("div");
|
||||||
if (hasTryAgainButton && buttons.length === 1) {
|
parentNode.innerHTML = buttonOuterHTMLFallback;
|
||||||
const TryAgainButton = actionsArea.querySelector("button");
|
TryAgainButton = parentNode.querySelector("button");
|
||||||
|
}
|
||||||
addActionsButtons(actionsArea, TryAgainButton, chatConf);
|
addActionsButtons(actionsArea, TryAgainButton, chatConf);
|
||||||
} else if (!hasTryAgainButton) {
|
} else if (shouldRemoveButtons()) {
|
||||||
removeButtons();
|
removeButtons();
|
||||||
}
|
}
|
||||||
}, 200);
|
}, 200);
|
||||||
@@ -29,32 +31,42 @@ const Format = {
|
|||||||
PDF: "pdf",
|
PDF: "pdf",
|
||||||
};
|
};
|
||||||
|
|
||||||
function addActionsButtons(actionsArea, TryAgainButton, chatConf) {
|
function shouldRemoveButtons() {
|
||||||
const downloadButton = TryAgainButton.cloneNode(true);
|
const isOpenScreen = document.querySelector("h1.text-4xl");
|
||||||
downloadButton.id = "download-png-button";
|
if(isOpenScreen){
|
||||||
downloadButton.innerText = "Generate PNG";
|
return true;
|
||||||
downloadButton.onclick = () => {
|
|
||||||
downloadThread();
|
|
||||||
};
|
|
||||||
actionsArea.appendChild(downloadButton);
|
|
||||||
|
|
||||||
const downloadPdfButton = TryAgainButton.cloneNode(true);
|
|
||||||
downloadPdfButton.id = "download-pdf-button";
|
|
||||||
downloadPdfButton.innerText = "Download PDF";
|
|
||||||
downloadPdfButton.onclick = () => {
|
|
||||||
downloadThread({ as: Format.PDF });
|
|
||||||
};
|
|
||||||
actionsArea.appendChild(downloadPdfButton);
|
|
||||||
|
|
||||||
if (new RegExp('//chat.openai.com').test(chatConf.origin)) {
|
|
||||||
const exportHtml = TryAgainButton.cloneNode(true);
|
|
||||||
exportHtml.id = "download-html-button";
|
|
||||||
exportHtml.innerText = "Share Link";
|
|
||||||
exportHtml.onclick = () => {
|
|
||||||
sendRequest();
|
|
||||||
};
|
|
||||||
actionsArea.appendChild(exportHtml);
|
|
||||||
}
|
}
|
||||||
|
const inConversation = document.querySelector("form button>div");
|
||||||
|
if(inConversation){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function shouldAddButtons(actionsArea) {
|
||||||
|
// first, check if there's a "Try Again" button and no other buttons
|
||||||
|
const buttons = actionsArea.querySelectorAll("button");
|
||||||
|
const hasTryAgainButton = Array.from(buttons).some((button) => {
|
||||||
|
return !button.id?.includes("download");
|
||||||
|
});
|
||||||
|
if (hasTryAgainButton && buttons.length === 1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise, check if open screen is not visible
|
||||||
|
const isOpenScreen = document.querySelector("h1.text-4xl");
|
||||||
|
if (isOpenScreen) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the conversation is finished and there are no share buttons
|
||||||
|
const finishedConversation = document.querySelector("form button>svg");
|
||||||
|
const hasShareButtons = actionsArea.querySelectorAll("button[share-ext]");
|
||||||
|
if (finishedConversation && !hasShareButtons.length) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeButtons() {
|
function removeButtons() {
|
||||||
@@ -72,6 +84,33 @@ function removeButtons() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addActionsButtons(actionsArea, TryAgainButton) {
|
||||||
|
const downloadButton = TryAgainButton.cloneNode(true);
|
||||||
|
downloadButton.id = "download-png-button";
|
||||||
|
downloadButton.setAttribute("share-ext", "true");
|
||||||
|
downloadButton.innerText = "Generate PNG";
|
||||||
|
downloadButton.onclick = () => {
|
||||||
|
downloadThread();
|
||||||
|
};
|
||||||
|
actionsArea.appendChild(downloadButton);
|
||||||
|
const downloadPdfButton = TryAgainButton.cloneNode(true);
|
||||||
|
downloadPdfButton.id = "download-pdf-button";
|
||||||
|
downloadButton.setAttribute("share-ext", "true");
|
||||||
|
downloadPdfButton.innerText = "Download PDF";
|
||||||
|
downloadPdfButton.onclick = () => {
|
||||||
|
downloadThread({ as: Format.PDF });
|
||||||
|
};
|
||||||
|
actionsArea.appendChild(downloadPdfButton);
|
||||||
|
const exportHtml = TryAgainButton.cloneNode(true);
|
||||||
|
exportHtml.id = "download-html-button";
|
||||||
|
downloadButton.setAttribute("share-ext", "true");
|
||||||
|
exportHtml.innerText = "Share Link";
|
||||||
|
exportHtml.onclick = () => {
|
||||||
|
sendRequest();
|
||||||
|
};
|
||||||
|
actionsArea.appendChild(exportHtml);
|
||||||
|
}
|
||||||
|
|
||||||
function downloadThread({ as = Format.PNG } = {}) {
|
function downloadThread({ as = Format.PNG } = {}) {
|
||||||
const elements = new Elements();
|
const elements = new Elements();
|
||||||
elements.fixLocation();
|
elements.fixLocation();
|
||||||
@@ -113,7 +152,7 @@ function handlePdf(imgData, canvas, pixelRatio) {
|
|||||||
]);
|
]);
|
||||||
var pdfWidth = pdf.internal.pageSize.getWidth();
|
var pdfWidth = pdf.internal.pageSize.getWidth();
|
||||||
var pdfHeight = pdf.internal.pageSize.getHeight();
|
var pdfHeight = pdf.internal.pageSize.getHeight();
|
||||||
pdf.addImage(imgData, "PNG", 0, 0, pdfWidth, pdfHeight);
|
pdf.addImage(imgData, "PNG", 0, 0, pdfWidth, pdfHeight, '', 'FAST');
|
||||||
|
|
||||||
const data = pdf.__private__.getArrayBuffer(pdf.__private__.buildDocument());
|
const data = pdf.__private__.getArrayBuffer(pdf.__private__.buildDocument());
|
||||||
invoke('download', { name: `chatgpt-${Date.now()}.pdf`, blob: Array.from(new Uint8Array(data)) });
|
invoke('download', { name: `chatgpt-${Date.now()}.pdf`, blob: Array.from(new Uint8Array(data)) });
|
||||||
|
|||||||
21
src/view/SyncPrompts/index.tsx
vendored
21
src/view/SyncPrompts/index.tsx
vendored
@@ -2,9 +2,8 @@ import { useEffect, useState } from 'react';
|
|||||||
import { Table, Button, message } from 'antd';
|
import { Table, Button, message } from 'antd';
|
||||||
import { invoke } from '@tauri-apps/api';
|
import { invoke } from '@tauri-apps/api';
|
||||||
import { fetch, ResponseType } from '@tauri-apps/api/http';
|
import { fetch, ResponseType } from '@tauri-apps/api/http';
|
||||||
import { writeTextFile, readTextFile } from '@tauri-apps/api/fs';
|
import { writeTextFile } from '@tauri-apps/api/fs';
|
||||||
|
|
||||||
import useInit from '@/hooks/useInit';
|
|
||||||
import useColumns from '@/hooks/useColumns';
|
import useColumns from '@/hooks/useColumns';
|
||||||
import useData from '@/hooks/useData';
|
import useData from '@/hooks/useData';
|
||||||
import useChatModel from '@/hooks/useChatModel';
|
import useChatModel from '@/hooks/useChatModel';
|
||||||
@@ -21,21 +20,13 @@ export default function LanguageModel() {
|
|||||||
const { opData, opInit, opReplace, opSafeKey } = useData([]);
|
const { opData, opInit, opReplace, opSafeKey } = useData([]);
|
||||||
const { columns, ...opInfo } = useColumns(modelColumns());
|
const { columns, ...opInfo } = useColumns(modelColumns());
|
||||||
|
|
||||||
// useInit(async () => {
|
|
||||||
// // const filename = await chatPromptsPath();
|
|
||||||
// // const data = await readTextFile(filename);
|
|
||||||
// // const list: Record<string, string>[] = await invoke('parse_prompt', { data });
|
|
||||||
// // const fileData: Record<string, any> = await invoke('metadata', { path: filename });
|
|
||||||
// // setLastUpdated(fileData.accessedAtMs);
|
|
||||||
// // opInit(list);
|
|
||||||
// console.log('«31» /view/SyncPrompts/index.tsx ~> ', modelJson);
|
|
||||||
|
|
||||||
// opInit([]);
|
|
||||||
// })
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!modelJson?.sys_sync_prompts) return;
|
if (!modelJson?.sys_sync_prompts) return;
|
||||||
opInit(modelJson?.sys_sync_prompts)
|
opInit(modelJson?.sys_sync_prompts);
|
||||||
|
(async () => {
|
||||||
|
const fileData: Record<string, any> = await invoke('metadata', { path: await chatPromptsPath() });
|
||||||
|
setLastUpdated(fileData.accessedAtMs);
|
||||||
|
})();
|
||||||
}, [modelJson?.sys_sync_prompts])
|
}, [modelJson?.sys_sync_prompts])
|
||||||
|
|
||||||
const handleSync = async () => {
|
const handleSync = async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user