2
0
mirror of https://github.com/frappe/books.git synced 2025-01-03 07:12:21 +00:00

chore: fix eslint and typescript

This commit is contained in:
Mildred Ki'Lya 2024-11-21 17:05:40 +01:00
parent 8de91f74fe
commit 4981977313
2 changed files with 35 additions and 44 deletions

28
main.ts
View File

@ -6,7 +6,6 @@ require('source-map-support').install({
import { emitMainProcessError } from 'backend/helpers'; import { emitMainProcessError } from 'backend/helpers';
import { import {
shell,
app, app,
BrowserWindow, BrowserWindow,
BrowserWindowConstructorOptions, BrowserWindowConstructorOptions,
@ -23,19 +22,6 @@ import registerIpcMainActionListeners from './main/registerIpcMainActionListener
import registerIpcMainMessageListeners from './main/registerIpcMainMessageListeners'; import registerIpcMainMessageListeners from './main/registerIpcMainMessageListeners';
import registerProcessListeners from './main/registerProcessListeners'; import registerProcessListeners from './main/registerProcessListeners';
const EXTENSIONS = {
'.js': 'text/javascript',
'.css': 'text/css',
'.html': 'text/html',
'.svg': 'image/svg+xml',
'.json': 'application/json',
'.pdf': 'application/pdf',
};
const MIME_TYPES = Object.fromEntries(
Object.entries(EXTENSIONS).map(([ext, mime]) => [mime, ext])
);
export class Main { export class Main {
title = 'Frappe Books'; title = 'Frappe Books';
icon: string; icon: string;
@ -133,11 +119,6 @@ export class Main {
const options = this.getOptions(); const options = this.getOptions();
this.mainWindow = new BrowserWindow(options); this.mainWindow = new BrowserWindow(options);
this.mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
if (this.isDevelopment) { if (this.isDevelopment) {
this.setViteServerURL(); this.setViteServerURL();
} else { } else {
@ -208,7 +189,14 @@ function bufferProtocolCallback(
fs.readFile(filePath, (_, data) => { fs.readFile(filePath, (_, data) => {
const extension = path.extname(filePath).toLowerCase(); const extension = path.extname(filePath).toLowerCase();
const mimeType = EXTENSIONS[extension] ?? ''; const mimeType =
{
'.js': 'text/javascript',
'.css': 'text/css',
'.html': 'text/html',
'.svg': 'image/svg+xml',
'.json': 'application/json',
}[extension] ?? '';
callback({ mimeType, data }); callback({ mimeType, data });
}); });

View File

@ -5,36 +5,42 @@ import { Main } from '../main';
import { IPC_MESSAGES } from '../utils/messages'; import { IPC_MESSAGES } from '../utils/messages';
import { emitMainProcessError } from 'backend/helpers'; import { emitMainProcessError } from 'backend/helpers';
function parseDataURL(url) { type DataURLParseResult = {
mediaType: string;
contentType: string;
base64: string;
data: string;
encoding: string;
buffer: Buffer;
};
function parseDataURL(url: string): null | DataURLParseResult {
const regex = const regex =
/^data:([a-z]+\/[a-z0-9-+.]+(;[a-z0-9-.!#$%*+.{}|~`]+=[a-z0-9-.!#$%*+.{}()_|~`]+)*)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s<>]*?)$/i; /^data:([a-z]+\/[a-z0-9-+.]+(;[a-z0-9-.!#$%*+.{}|~`]+=[a-z0-9-.!#$%*+.{}()_|~`]+)*)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s<>]*?)$/i;
const parts = url.trim().match(regex); const parts = url.trim().match(regex);
if (!parts) return null; if (!parts) return null;
const parsed = {}; const mediaType = (parts[1] || 'text/plain;charset=us-ascii').toLowerCase();
parsed.mediaType = (parts[1] || 'text/plain;charset=us-ascii').toLowerCase(); const mediaTypeParts: string[] = mediaType
const mediaTypeParts = parsed.mediaType
.split(';') .split(';')
.map((x) => x.toLowerCase()); .map((x: string) => x.toLowerCase());
parsed.contentType = mediaTypeParts[0]; const contentType = mediaTypeParts[0];
mediaTypeParts.slice(1).forEach((attribute) => { mediaTypeParts.slice(1).forEach((attribute) => {
const p = attribute.split('='); const p = attribute.split('=');
parsed[p[0]] = p[1]; if (p.length >= 2) (parsed as object)[p[0]] = p[1];
}); });
parsed.base64 = !!parts[parts.length - 2]; const base64 = !!parts[parts.length - 2];
parsed.data = parts[parts.length - 1] || ''; const data = parts[parts.length - 1] || '';
parsed.encoding = parsed.base64 ? 'base64' : 'utf8'; const encoding = base64 ? 'base64' : 'utf8';
parsed.buffer = Buffer.from( const buffer = Buffer.from(
parsed.base64 ? parsed.data : decodeURIComponent(parsed.data), base64 ? data : decodeURIComponent(data),
parsed.encoding encoding
); );
return parsed; return { mediaType, contentType, base64, data, encoding, buffer };
} }
export default function registerIpcMainMessageListeners(main: Main) { export default function registerIpcMainMessageListeners(main: Main) {
@ -88,16 +94,13 @@ export default function registerIpcMainMessageListeners(main: Main) {
(_, { link, filename }: { link: string; filename: string }) => { (_, { link, filename }: { link: string; filename: string }) => {
const data = parseDataURL(link); const data = parseDataURL(link);
if (data) { if (data) {
const s = const s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; const temp = Array.from(Array(16), () =>
const temp = Array.apply(null, Array(16)) s.charAt(Math.floor(Math.random() * s.length))
.map(() => { ).join('');
return s.charAt(Math.floor(Math.random() * s.length)); const filepath = path.join(app.getPath('temp'), `${temp} ${filename}`);
})
.join('');
const filepath = path.join(app.getPath('temp'), temp + ' ' + filename);
fs.writeFileSync(filepath, data.buffer); fs.writeFileSync(filepath, data.buffer);
shell.openPath(filepath); void shell.openPath(filepath);
} }
} }
); );