Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][Typescript Migration] browser-preload.browser.js --> browser-preload.browser.ts #2591

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type ChangeEvent } from 'react';

import { initBackend as initSQLBackend } from 'absurd-sql/dist/indexeddb-main-thread';

import * as Platform from 'loot-core/src/client/platform';
Expand Down Expand Up @@ -39,7 +41,8 @@ function createBackendWorker() {

createBackendWorker();

global.Actual = {
export type ActualType = typeof Actual;
const Actual = {
IS_DEV,
ACTUAL_VERSION,

Expand All @@ -51,21 +54,25 @@ global.Actual = {
window.location.reload();
},

openFileDialog: async ({ filters = [] }) => {
openFileDialog: async ({
filters = [],
}: {
filters: { name: string; extensions: string[] }[];
}) => {
return new Promise(resolve => {
let createdElement = false;
// Attempt to reuse an already-created file input.
let input = document.body.querySelector(
'input[id="open-file-dialog-input"]',
);
) as HTMLInputElement | null;
if (!input) {
createdElement = true;
input = document.createElement('input');
}

input.type = 'file';
input.id = 'open-file-dialog-input';
input.value = null;
input.value = null as unknown as string;

const filter = filters.find(filter => filter.extensions);
if (filter) {
Expand All @@ -78,17 +85,20 @@ global.Actual = {
input.style.display = 'none';

input.onchange = e => {
const file = e.target.files[0];
const event = e as unknown as ChangeEvent<HTMLInputElement>;
if (!event?.target?.files) return;
const file = event.target.files[0];
const filename = file.name.replace(/.*(\.[^.]*)/, 'file$1');

if (file) {
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = async function (ev) {
if (!ev.target?.result) return;
const filepath = `/uploads/${filename}`;

window.__actionsForMenu
.uploadFile(filename, ev.target.result)
.uploadFile(filename, ev.target.result as ArrayBuffer)
.then(() => resolve([filepath]));
};
reader.onerror = function () {
Expand All @@ -108,8 +118,8 @@ global.Actual = {
},

saveFile: (contents, defaultFilename) => {
const temp = document.createElement('a');
temp.style = 'display: none';
const temp = document.createElement('a') as HTMLAnchorElement;
temp.style.display = 'none';
temp.download = defaultFilename;
temp.rel = 'noopener';

Expand All @@ -135,7 +145,9 @@ global.Actual = {
},
};

document.addEventListener('keydown', e => {
global.Actual = Actual;

document.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.metaKey || e.ctrlKey) {
// Cmd/Ctrl+o
if (e.key === 'o') {
Expand All @@ -144,10 +156,11 @@ document.addEventListener('keydown', e => {
}
// Cmd/Ctrl+z
else if (e.key.toLowerCase() === 'z') {
if (!e?.target) return;
if (
e.target.tagName === 'INPUT' ||
e.target.tagName === 'TEXTAREA' ||
e.target.isContentEditable
(e.target as Element).tagName === 'INPUT' ||
(e.target as Element).tagName === 'TEXTAREA' ||
(e.target as HTMLElement).isContentEditable
) {
return;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/desktop-client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import { send } from 'loot-core/src/platform/client/fetch';
import { q } from 'loot-core/src/shared/query';

import { type ActualType } from './browser-preload.browser';
import { App } from './components/App';
import { ServerProvider } from './components/ServerContext';
import { handleGlobalEvents } from './global-events';
Expand Down Expand Up @@ -72,6 +73,7 @@
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface Window {
__actionsForMenu: BoundActions;
Actual: ActualType;

Check failure on line 76 in packages/desktop-client/src/index.tsx

View workflow job for this annotation

GitHub Actions / typecheck

All declarations of 'Actual' must have identical modifiers.

$send: typeof send;
$query: typeof runQuery;
Expand Down
Loading