Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { initBackend as initSQLBackend } from 'absurd-sql/dist/indexeddb-main-thread';
// eslint-disable-next-line import/no-webpack-loader-syntax
import BackendWorker from 'worker-loader!./browser-server';
// This file installs global variables that the app expects.
// Normally these are already provided by electron, but in a real
// browser environment this is where we initialize the backend and
// everything else.
let IS_DEV = process.env.NODE_ENV === 'development';
let IS_PERF_BUILD = process.env.PERF_BUILD != null;
let ACTUAL_VERSION = process.env.REACT_APP_ACTUAL_VERSION;
// *** Start the backend ***
let worker;
function createBackendWorker() {
worker = new BackendWorker();
initSQLBackend(worker);
worker.postMessage({
type: 'init',
version: ACTUAL_VERSION,
isDev: IS_DEV,
hash: process.env.REACT_APP_BACKEND_WORKER_HASH
});
if (IS_DEV || IS_PERF_BUILD) {
worker.addEventListener('message', e => {
if (e.data.type === '__actual:backend-running') {
let activity = document.querySelector('.debugger .activity');
if (activity) {
let original = window.getComputedStyle(activity)['background-color'];
activity.style.transition = 'none';
activity.style.backgroundColor = '#3EBD93';
setTimeout(() => {
activity.style.transition = 'background-color 1s';
activity.style.backgroundColor = original;
}, 100);
}
}
});
import('perf-deets/frontend').then(({ listenForPerfData }) => {
listenForPerfData(worker);
});
}
}
createBackendWorker();
if (IS_DEV || IS_PERF_BUILD) {
import('perf-deets/frontend').then(({ listenForPerfData }) => {
listenForPerfData(window);
global.__startProfile = () => {
window.postMessage({ type: '__perf-deets:start-profile' });
worker.postMessage({ type: '__perf-deets:start-profile' });
};
global.__stopProfile = () => {
window.postMessage({ type: '__perf-deets:stop-profile' });
worker.postMessage({ type: '__perf-deets:stop-profile' });
};
});
}
global.Actual = {
IS_DEV,
ACTUAL_VERSION,
IS_FAKE_WEB: true,
IS_BETA: process.env.REACT_APP_RELEASE_TYPE === 'beta',
logToTerminal: (...args) => {
console.log(...args);
},
relaunch: () => {
window.location.reload();
},
openFileDialog: async ({ filters = [], properties }) => {
return new Promise(resolve => {
let input = document.createElement('input');
input.type = 'file';
let filter = filters.find(filter => filter.extensions);
if (filter) {
input.accept = filter.extensions.map(ext => '.' + ext).join(',');
}
input.style.position = 'absolute';
input.style.top = '0px';
input.style.left = '0px';
input.dispatchEvent(
new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
})
);
input.addEventListener('change', e => {
let file = e.target.files[0];
let filename = file.name.replace(/.*(\.[^.]*)/, 'file$1');
if (file) {
var reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = async function(ev) {
let filepath = `/uploads/${filename}`;
window.__actionsForMenu
.uploadFile(filename, ev.target.result)
.then(() => resolve([filepath]));
};
reader.onerror = function(ev) {
alert('Error reading file');
};
}
});
});
},
saveFile: (contents, defaultFilename, dialogTitle) => {
const temp = document.createElement('a');
temp.style = 'display: none';
temp.download = defaultFilename;
temp.rel = 'noopener';
const blob = new Blob([contents]);
temp.href = URL.createObjectURL(blob);
temp.dispatchEvent(new MouseEvent('click'));
},
openURLInBrowser: url => {
window.open(url, '_blank');
},
onEventFromMain: (type, handler) => {},
applyAppUpdate: () => {},
updateAppMenu: isBudgetOpen => {},
ipcConnect: () => {},
getServerSocket: async () => {
return worker;
}
};
if (IS_DEV) {
global.Actual.reloadBackend = () => {
worker.postMessage({ type: '__actual:shutdown' });
createBackendWorker();
};
}
document.addEventListener('keydown', e => {
if (e.metaKey || e.ctrlKey) {
// Cmd/Ctrl+o
if (e.keyCode === 79) {
e.preventDefault();
window.__actionsForMenu.closeBudget();
}
// Cmd/Ctrl+z
else if (e.keyCode === 90) {
if (e.shiftKey) {
// Redo
window.__actionsForMenu.redo();
} else {
// Undo
window.__actionsForMenu.undo();
}
}
}
});