1
0
Fork 0
mirror of https://github.com/anyproto/anytype-ts.git synced 2025-06-08 05:57:02 +09:00

remove window.require, preload.js progress

This commit is contained in:
Andrew Simachev 2022-07-07 21:41:17 +03:00
parent 4ee6a8ca05
commit 07c981c1ac
23 changed files with 72 additions and 99 deletions

View file

@ -89,8 +89,10 @@ nativeTheme.on('updated', () => {
function createWindow () {
ipcMain.handle('Api', (e, cmd, args) => {
console.log(arguments);
return 'Api return';
args = args || [];
args.unshift(e.sender);
Api[cmd].apply(Api, args);
});
mainWindow = WindowManager.createMain({ route: Util.getRouteFromUrl(deeplinkingUrl), isChild: false });

View file

@ -1,13 +1,35 @@
const { ipcRenderer, contextBridge } = require('electron');
const { app, getCurrentWindow, getGlobal } = require('@electron/remote');
const { app, getCurrentWindow, getGlobal, dialog, BrowserWindow, process } = require('@electron/remote');
const fs = require('fs');
const os = require('os');
const path = require('path');
const readChunk = require('read-chunk');
const fileType = require('file-type');
const userPath = app.getPath('userData');
contextBridge.exposeInMainWorld('Electron', {
version: {
app: app.getVersion(),
os: os.release(),
system: process.getSystemVersion(),
},
platform: os.platform(),
arch: process.arch,
currentWindow: getCurrentWindow(),
version: app.getVersion(),
isPackaged: app.isPackaged,
userPath: app.getPath('userData'),
isMaximized: () => getCurrentWindow.isMaximized(),
userPath,
tmpPath: path.join(userPath, 'tmp'),
getPath: (fp, fn) => path.join(fp, fn),
isMaximized: () => BrowserWindow.getFocusedWindow().isMaximized(),
getGlobal: (key) => getGlobal(key),
showOpenDialog: dialog.showOpenDialog,
fs,
readChunk,
fileType,
on: ipcRenderer.on,
removeAllListeners: ipcRenderer.removeAllListeners,

View file

@ -155,7 +155,6 @@ import 'scss/theme/dark/common.scss';
const $ = require('jquery');
const path = require('path');
const { dialog } = window.require('@electron/remote');
const hs = require('history');
const memoryHistory = hs.createMemoryHistory;
const history = memoryHistory();
@ -220,7 +219,7 @@ enableLogging({
*/
Sentry.init({
release: window.Electron.version,
release: window.Electron.version.app,
environment: (window.Electron.isPackaged ? 'production' : 'development'),
dsn: Constant.sentry,
maxBreadcrumbs: 0,
@ -337,14 +336,16 @@ class App extends React.Component<Props, State> {
init () {
Util.init(history);
dispatcher.init();
keyboard.init();
analytics.init();
this.registerIpcEvents();
Renderer.send('appOnLoad');
console.log('[Process] os version:', process.getSystemVersion(), 'arch:', process.arch);
console.log('[App] version:', window.Electron.version, 'isPackaged', window.Electron.isPackaged);
console.log('[Process] os version:', window.Electron.version.system, 'arch:', window.Electron.arch);
console.log('[App] version:', window.Electron.version.app, 'isPackaged', window.Electron.isPackaged);
};
initStorage () {
@ -579,7 +580,7 @@ class App extends React.Component<Props, State> {
popupStore.open('confirm', {
data: {
title: 'You are up-to-date',
text: Util.sprintf('You are on the latest version: %s', window.Electron.version),
text: Util.sprintf('You are on the latest version: %s', window.Electron.version.app),
textConfirm: 'Great!',
canCancel: false,
},
@ -643,7 +644,7 @@ class App extends React.Component<Props, State> {
case 'exportTemplates':
options.properties = [ 'openDirectory' ];
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;
@ -662,7 +663,7 @@ class App extends React.Component<Props, State> {
case 'exportLocalstore':
options.properties = [ 'openDirectory' ];
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;

View file

@ -18,7 +18,6 @@ interface State {
const $ = require('jquery');
const Constant = require('json/constant.json');
const Url = require('json/url.json');
const { dialog } = window.require('@electron/remote');
const BlockCover = observer(class BlockCover extends React.Component<Props, State> {
@ -224,7 +223,7 @@ const BlockCover = observer(class BlockCover extends React.Component<Props, Stat
filters: [ { name: '', extensions: Constant.extension.cover } ]
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;

View file

@ -11,7 +11,6 @@ interface State {
};
const Constant = require('json/constant.json');
const { dialog } = window.require('@electron/remote');
const BlockIconUser = observer(class BlockIconUser extends React.Component<Props, State> {
@ -83,7 +82,7 @@ const BlockIconUser = observer(class BlockIconUser extends React.Component<Props
filters: [ { name: '', extensions: Constant.extension.cover } ]
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;

View file

@ -11,9 +11,6 @@ pdfjs.GlobalWorkerOptions.workerSrc = 'workers/pdf.min.js';
interface Props extends I.BlockComponent {};
const { app } = window.require('@electron/remote');
const userPath = app.getPath('userData');
const path = window.require('path');
const Constant = require('json/constant.json');
interface State {
@ -182,7 +179,7 @@ const BlockPdf = observer(class BlockPdf extends React.Component<Props, State> {
const { content } = block;
const { hash } = content;
C.FileDownload(hash, path.join(userPath, 'tmp'), (message: any) => {
C.FileDownload(hash, window.Electron.tmpPath, (message: any) => {
if (message.path) {
Renderer.send('pathOpen', message.path);
};

View file

@ -17,14 +17,10 @@ interface Props extends RouteComponentProps<any> {
onOpen?(): void;
};
const { app } = window.require('@electron/remote');
const Constant = require('json/constant.json');
const Errors = require('json/error.json');
const $ = require('jquery');
const raf = require('raf');
const fs = window.require('fs');
const path = window.require('path');
const userPath = app.getPath('userData');
const THROTTLE = 20;
const BUTTON_OFFSET = 10;
@ -1518,7 +1514,7 @@ const EditorPage = observer(class EditorPage extends React.Component<Props, {}>
const { dataset, rootId } = this.props;
const { selection } = dataset || {};
const { focused, range } = focus.state;
const filePath = path.join(userPath, 'tmp');
const filePath = window.Electron.tmpPath;
const currentFrom = range.from;
const currentTo = range.to;
@ -1559,12 +1555,12 @@ const EditorPage = observer(class EditorPage extends React.Component<Props, {}>
commonStore.progressSet({ status: translate('commonProgress'), current: 0, total: files.length });
for (let file of files) {
const fn = path.join(filePath, file.name);
const fn = window.Electron.getPath(filePath, file.name);
const reader = new FileReader();
reader.readAsBinaryString(file);
reader.onloadend = () => {
fs.writeFile(fn, reader.result, 'binary', (err: any) => {
window.Electron.fs.writeFile(fn, reader.result, 'binary', (err: any) => {
if (err) {
console.error(err);
commonStore.progressSet({ status: translate('commonProgress'), current: 0, total: 0 });

View file

@ -19,7 +19,6 @@ interface State {
loading: boolean;
};
const { dialog } = window.require('@electron/remote');
const Constant = require('json/constant.json');
const $ = require('jquery');
@ -198,7 +197,7 @@ const MenuBlockCover = observer(class MenuBlockCover extends React.Component<Pro
filters: [ { name: '', extensions: Constant.extension.cover } ]
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;

View file

@ -10,7 +10,6 @@ import arrayMove from 'array-move';
interface Props extends I.Menu {}
const $ = require('jquery');
const { dialog } = window.require('@electron/remote');
const Constant = require('json/constant.json');
const MENU_ID = 'dataviewFileList';
@ -186,7 +185,7 @@ const MenuDataviewFileValues = observer(class MenuDataviewFileValues extends Rea
filters: [ ]
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
const file = files && files.length ? files[0] : '';

View file

@ -14,7 +14,6 @@ interface State {
const $ = require('jquery');
const EmojiData = require('json/emoji.json');
const Constant = require('json/constant.json');
const { dialog } = window.require('@electron/remote');
const LIMIT_RECENT = 18;
const LIMIT_ROW = 9;
@ -314,7 +313,7 @@ class MenuSmile extends React.Component<Props, State> {
close();
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;

View file

@ -10,7 +10,6 @@ interface State {
error: string;
}
const { dialog } = window.require('@electron/remote');
const Constant = require('json/constant.json');
const PageAuthRegister = observer(class PageAuthRegister extends React.Component<Props, State> {
@ -77,7 +76,7 @@ const PageAuthRegister = observer(class PageAuthRegister extends React.Component
filters: [ { name: '', extensions: Constant.extension.image } ]
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;
@ -97,7 +96,7 @@ const PageAuthRegister = observer(class PageAuthRegister extends React.Component
properties: [ 'openDirectory' ],
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;

View file

@ -21,7 +21,6 @@ interface State {
loading: boolean;
};
const { dialog } = window.require('@electron/remote');
const Constant = require('json/constant.json');
const $ = require('jquery');
@ -140,7 +139,7 @@ const Controls = observer(class Controls extends React.Component<Props, State> {
filters: [ { name: '', extensions: Constant.extension.image } ]
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;

View file

@ -16,9 +16,6 @@ interface State {
};
const $ = require('jquery');
const { app } = window.require('@electron/remote')
const path = window.require('path');
const userPath = app.getPath('userData');
const Errors = require('json/error.json');
const MAX_HEIGHT = 396;
@ -240,7 +237,7 @@ const PageMainMedia = observer(class PageMainMedia extends React.Component<Props
const block = blocks.find((it: I.Block) => { return it.isFile(); });
const { content } = block;
C.FileDownload(content.hash, path.join(userPath, 'tmp'), (message: any) => {
C.FileDownload(content.hash, window.Electron.tmpPath, (message: any) => {
if (message.path) {
Renderer.send('pathOpen', message.path);
};

View file

@ -20,7 +20,6 @@ interface State {
};
const Constant: any = require('json/constant.json');
const { dialog } = window.require('@electron/remote');
const PopupSettingsPageAccount = observer(class PopupSettingsPageAccount extends React.Component<Props, State> {
@ -200,7 +199,7 @@ const PopupSettingsPageAccount = observer(class PopupSettingsPageAccount extends
properties: [ 'openDirectory' ],
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;

View file

@ -13,7 +13,6 @@ interface Props extends I.Popup, RouteComponentProps<any> {
setLoading: (v: boolean) => void;
};
const { dialog } = window.require('@electron/remote');
const Constant = require('json/constant.json');
const PopupSettingsPageWallpaper = observer(class PopupSettingsPageWallpaper extends React.Component<Props, {}> {
@ -88,7 +87,7 @@ const PopupSettingsPageWallpaper = observer(class PopupSettingsPageWallpaper ext
filters: [ { name: '', extensions: Constant.extension.cover } ]
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;

View file

@ -28,7 +28,6 @@ interface State {
loading: boolean;
};
const { dialog } = window.require('@electron/remote');
const $ = require('jquery');
const Components: any = {
@ -180,7 +179,7 @@ const PopupSettings = observer(class PopupSettings extends React.Component<Props
options.properties.push('openDirectory');
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
if ((files == undefined) || !files.length) {
return;

View file

@ -21,7 +21,6 @@ interface State {
size: Size;
};
const { dialog } = window.require('@electron/remote');
const $ = require('jquery');
const raf = require('raf');
const SMALL_WIDTH = 248;
@ -263,7 +262,7 @@ class InputWithFile extends React.Component<Props, State> {
];
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const files = result.filePaths;
const file = files && files.length ? files[0] : '';

View file

@ -2,7 +2,6 @@ import { I, C, focus, analytics, Renderer } from 'ts/lib';
import { commonStore, authStore, blockStore, detailStore, dbStore } from 'ts/store';
const Constant = require('json/constant.json');
const { dialog } = window.require('@electron/remote');
class Action {
@ -120,7 +119,7 @@ class Action {
properties: [ 'openDirectory' ],
};
dialog.showOpenDialog(options).then((result: any) => {
window.Electron.showOpenDialog(options).then((result: any) => {
const paths = result.filePaths;
if ((paths == undefined) || !paths.length) {
return;

View file

@ -1,12 +1,8 @@
import * as amplitude from 'amplitude-js';
import { I, C, Util, Storage } from 'ts/lib';
import { authStore, commonStore } from 'ts/store';
import { commonStore } from 'ts/store';
const Constant = require('json/constant.json');
const { app } = window.require('@electron/remote');
const isProduction = app.isPackaged;
const version = app.getVersion();
const os = window.require('os');
const KEYS = [
'method', 'id', 'action', 'style', 'code', 'route', 'format', 'color',
@ -44,11 +40,11 @@ class Analytics {
platform: platform,
});
this.instance.setVersionName(version);
this.instance.setVersionName(window.Electron.version.app);
this.instance.setUserProperties({
deviceType: 'Desktop',
platform: Util.getPlatform(),
osVersion: os.release(),
osVersion: window.Electron.version.os,
});
console.log('[Analytics].init', this.instance);
@ -57,7 +53,7 @@ class Analytics {
};
profile (account: any) {
if (!this.instance || (!isProduction && !this.debug()) || !account) {
if (!this.instance || (!window.Electron.isPackaged && !this.debug()) || !account) {
return;
};
if (this.debug()) {
@ -67,7 +63,7 @@ class Analytics {
};
device (id: string) {
if (!this.instance || (!isProduction && !this.debug())) {
if (!this.instance || (!window.Electron.isPackaged && !this.debug())) {
return;
};
@ -90,7 +86,7 @@ class Analytics {
event (code: string, data?: any) {
data = data || {};
if (!this.instance || (!isProduction && !this.debug()) || !code) {
if (!this.instance || (!window.Electron.isPackaged && !this.debug()) || !code) {
return;
};

View file

@ -31,12 +31,13 @@ class Dispatcher {
timeoutEvent: any = {};
reconnects: number = 0;
constructor () {
let serverAddr = window.Electron.getGlobal('serverAddr');
console.log('[Dispatcher] Server address: ', serverAddr);
this.service = new Service.ClientCommandsClient(serverAddr, null, null);
init () {
const serverAddr = window.Electron.getGlobal('serverAddr');
this.service = new Service.ClientCommandsClient(serverAddr, null, null);
this.listenEvents();
console.log('[Dispatcher].init Server address: ', serverAddr);
};
listenEvents () {

View file

@ -1,8 +1,5 @@
import { Util } from 'ts/lib';
const fs = window.require('fs');
const readChunk = window.require('read-chunk');
const fileType = window.require('file-type');
const Constant = require('json/constant.json');
const loadImage = require('blueimp-load-image');
@ -10,9 +7,9 @@ class FileUtil {
fromPath (path: string) {
let fn = path.split('/');
let stat = fs.statSync(path);
let buffer = readChunk.sync(path, 0, stat.size);
let type = fileType(buffer);
let stat = window.Electron.fs.statSync(path);
let buffer = window.Electron.readChunk.sync(path, 0, stat.size);
let type = window.Electron.fileType(buffer);
let file = new File([ new Blob([ buffer ]) ], fn[fn.length - 1], { type: type.mime });
return file;
};

View file

@ -1,12 +1,7 @@
class Renderer {
send (...args: any[]) {
const cmd = args[0];
args.shift();
args.unshift(window.Electron.currentWindow);
window.Electron.Api(cmd, args);
window.Electron.Api(args);
};
on (event: string, callBack: any) {

View file

@ -3,11 +3,8 @@ import { commonStore, popupStore, menuStore } from 'ts/store';
import { translate } from '.';
const $ = require('jquery');
const fs = window.require('fs');
const Constant = require('json/constant.json');
const Errors = require('json/error.json');
const os = window.require('os');
const path = window.require('path');
const Cover = require('json/cover.json');
class Util {
@ -842,7 +839,7 @@ class Util {
};
getPlatform () {
return Constant.platforms[os.platform()];
return Constant.platforms[window.Electron.platform];
};
checkError (code: number) {
@ -952,22 +949,6 @@ class Util {
return this.getPlatform() == I.Platform.Windows ? 68 : 52;
};
deleteFolderRecursive (p: string) {
if (!fs.existsSync(p) ) {
return;
};
fs.readdirSync(p).forEach((file: any) => {
const cp = path.join(p, file);
if (fs.lstatSync(cp).isDirectory()) {
this.deleteFolderRecursive(cp);
} else {
fs.unlinkSync(cp);
};
});
fs.rmdirSync(p);
};
searchParam (url: string): any {
var a = url.replace(/^\?/, '').split('&');
var param: any = {};