refactor: apply eslint

This commit is contained in:
sbplat
2024-01-04 20:17:54 -05:00
parent 5fd505d4f4
commit 9c1588d150
53 changed files with 647 additions and 636 deletions

View File

@@ -1,7 +1,7 @@
export function appendToFilename(inputPath: string, toAppend: string) {
const parts = inputPath.split('.');
const parts = inputPath.split(".");
if (parts.length > 1) {
parts[parts.length-2] = parts[parts.length-2] + toAppend;
}

View File

@@ -1,7 +1,7 @@
import { readBinaryFile, writeBinaryFile, removeDir, BaseDirectory } from '@tauri-apps/api/fs';
import { PdfFile,RepresentationType } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile'
import { runShell, isTauriAvailable } from './tauri-wrapper';
import { readBinaryFile, writeBinaryFile, removeDir, BaseDirectory } from "@tauri-apps/api/fs";
import { PdfFile,RepresentationType } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
import { runShell, isTauriAvailable } from "./tauri-wrapper";
export async function fileToPdf(byteArray: Uint8Array, filename: string): Promise<PdfFile> {
const randUuid = crypto.randomUUID();
@@ -18,9 +18,9 @@ export async function fileToPdf(byteArray: Uint8Array, filename: string): Promis
}
console.debug(`${stream}, ${randUuid}: ${message}`);
});
const lastMessage = messageList[messageList.length-1]
const lastMessage = messageList[messageList.length-1];
const outputFilePath = lastMessage.split(" -> ")[1].split(".pdf")[0]+".pdf";
const outputFilePathSplit = outputFilePath.toString().split("[\\/]")
const outputFilePathSplit = outputFilePath.toString().split("[\\/]");
const outputFileName = outputFilePathSplit[outputFilePathSplit.length-1];
const outputBytes = await readBinaryFile(outputFilePath);
@@ -42,7 +42,7 @@ export async function isLibreOfficeInstalled() {
} catch (error) {
return false;
}
console.log("messageList", messageList)
console.log("messageList", messageList);
const result = messageList[0].match("LibreOffice ([0-9]+\.){4}.*");
return result ? true : false;
}

View File

@@ -1,7 +1,7 @@
import SharedOperations, { OperationsType } from '@stirling-pdf/shared-operations/src'
import { ImposeParamsType } from '@stirling-pdf/shared-operations/src/functions/impose'
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile"
import SharedOperations, { OperationsType } from "@stirling-pdf/shared-operations/src";
import { ImposeParamsType } from "@stirling-pdf/shared-operations/src/functions/impose";
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
// Import injected libraries here!
import * as pdfcpuWrapper from "@stirling-pdf/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-browser.js";
@@ -13,5 +13,5 @@ async function impose(params: ImposeParamsType): Promise<PdfFile> {
const toExport: OperationsType = {
...SharedOperations,
impose,
}
};
export default toExport;

View File

@@ -1,9 +1,9 @@
import { open, save } from '@tauri-apps/api/dialog';
import { readBinaryFile, writeBinaryFile } from '@tauri-apps/api/fs';
import { Command } from '@tauri-apps/api/shell'
import { open, save } from "@tauri-apps/api/dialog";
import { readBinaryFile, writeBinaryFile } from "@tauri-apps/api/fs";
import { Command } from "@tauri-apps/api/shell";
export type TauriBrowserFile = {
export interface TauriBrowserFile {
name: string,
relativePath?: string,
data: Uint8Array,
@@ -29,13 +29,13 @@ export function isTauriAvailable() {
}
// [*] = Not available in browser
type SelectFilesDialogOptions = {
interface SelectFilesDialogOptions {
defaultPath?: string, // [*] the default path to open the dialog on
directory?: boolean, // should the dialog be a directory dialog
filters?: Array<{ // list of file type filters
filters?: { // list of file type filters
name: string, // category name eg. 'Images'
extensions: string[] // list of extensions eg ['png', 'jpeg', 'jpg']
}>,
}[],
multiple?: boolean, // allow multiple selections
recursive?: boolean, // [*] If directory is true, indicates that it will be read recursively later. Defines whether subdirectories will be allowed on the scope or not.
title?: string // [*] the title of the dialog
@@ -43,7 +43,7 @@ type SelectFilesDialogOptions = {
export function openFiles(options: SelectFilesDialogOptions): Promise<TauriBrowserFile[] | null> {
return new Promise(async (resolve) => {
if (isTauriAvailable()) {
var selected = await open(options);
let selected = await open(options);
if (!selected) {
resolve(null);
return;
@@ -65,15 +65,15 @@ export function openFiles(options: SelectFilesDialogOptions): Promise<TauriBrows
resolve(files);
return;
} else {
var input = document.createElement('input');
input.type = 'file';
const input = document.createElement("input");
input.type = "file";
if (options.directory) input.setAttribute("webkitdirectory", "");
if (options.filters) input.setAttribute("accept", options.filters.flatMap(f => f.extensions).map(ext => "."+ext).join(", "));
if (options.multiple) input.setAttribute("multiple", "");
input.onchange = async () => {
if (input.files && input.files.length) {
console.log("input.files", input.files)
console.log("input.files", input.files);
const files: TauriBrowserFile[] = [];
for (const f of input.files) {
const contents = new Uint8Array(await f.arrayBuffer());
@@ -91,8 +91,8 @@ export function openFiles(options: SelectFilesDialogOptions): Promise<TauriBrows
// detect the user clicking cancel
document.body.onfocus = () => {
setTimeout(()=>resolve(null), 200); // the timeout is needed because 'document.body.onfocus' is called before 'input.onchange'
}
setTimeout(()=>{ resolve(null) }, 200); // the timeout is needed because 'document.body.onfocus' is called before 'input.onchange'
};
input.click();
}
@@ -100,40 +100,40 @@ export function openFiles(options: SelectFilesDialogOptions): Promise<TauriBrows
}
// [*] = Not available in browser
type DownloadFilesDialogOptions = {
interface DownloadFilesDialogOptions {
defaultPath?: string, // the default path to open the dialog on
filters?: Array<{ // [*] list of file type filters
filters?: { // [*] list of file type filters
name: string, // category name eg. 'Images'
extensions: string[] // list of extensions eg ['png', 'jpeg', 'jpg']
}>,
}[],
title?: string // [*] the title of the dialog
}
export async function downloadFile(fileData: Uint8Array, options: DownloadFilesDialogOptions): Promise<undefined> {
if (isTauriAvailable()) {
const pathToSave = await save(options);
console.log("pathToSave", pathToSave)
console.log("pathToSave", pathToSave);
if (pathToSave) {
await writeBinaryFile(pathToSave, fileData);
}
} else {
const pdfBlob = new Blob([fileData], { type: 'application/pdf' });
const pdfBlob = new Blob([fileData], { type: "application/pdf" });
const url = URL.createObjectURL(pdfBlob);
const downloadOption = localStorage.getItem('downloadOption');
const downloadOption = localStorage.getItem("downloadOption");
// ensure filename is not a path
const separator = options.defaultPath?.includes("\\") ? "\\" : "/";
const filename = options.defaultPath?.split(separator).pop();
const filenameToUse = filename ? filename : 'edited.pdf';
const filenameToUse = filename ? filename : "edited.pdf";
if (downloadOption === 'sameWindow') {
if (downloadOption === "sameWindow") {
// Open the file in the same window
window.location.href = url;
} else if (downloadOption === 'newWindow') {
} else if (downloadOption === "newWindow") {
// Open the file in a new window
window.open(url, '_blank');
window.open(url, "_blank");
} else {
// Download the file
const downloadLink = document.createElement('a');
const downloadLink = document.createElement("a");
downloadLink.href = url;
downloadLink.download = filenameToUse;
downloadLink.click();
@@ -152,18 +152,18 @@ export function runShell(commandName: string, args: string[], callback: (message
return new Promise(async (resolve, reject) => {
const comm = new Command(commandName, args);
comm.on('close', data => {
comm.on("close", data => {
if (data.code === 0) {
resolve();
} else {
reject(new Error(`Command failed with exit code ${data.code} and signal ${data.signal}`));
}
});
comm.on('error', error => callback(error, "error"));
comm.stdout.on('data', line => callback(line, "stdout"));
comm.stderr.on('data', line => callback(line, "stderr"));
comm.on("error", error => { callback(error, "error") });
comm.stdout.on("data", line => { callback(line, "stdout") });
comm.stderr.on("data", line => { callback(line, "stderr") });
const child = await comm.spawn();
console.debug(`Started child process with pid: ${child.pid}`)
console.debug(`Started child process with pid: ${child.pid}`);
});
}