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

* navigation panel * navigation * navigation * fixes * remove unused svg + fixes * fixes * move logic update * move logic update * fixes * fixes * search * search * sidebar resize logic * sidebar resize logic * sidebar resize logic * sidebar resize logic * remove relations button * JS-1917: preloader update * JS-1917: preloader update * JS-897: delete logic update * JS-897: delete logic update * refactoring * remove unused logic * remove unused logic * small fixes * fix sentry crash * JS-1955: settings popup * JS-1955: settings popup * JS-1955: settings popup * refactoring * refactoring * refactoring * fix overflow * fix overflow * fixes * fix navigation when sidebar is on the right * JS-1686: fix * space icon * remove drag n drop from navigation * fixes * JS-2024: fixes * JS-2024: fixes * JS-2024: fixes * search fixes * JS-2024: fixes * JS-2053: fix * JS-2052: graph root highlight * JS-2052: graph root highlight * JS-2052: graph root highlight * sync status icons * object creation flow updates * JS-2046: fix * sidebar resize fixes * fix sentry crash * fix sidebar open/close * fixes * fix * fix * fix * resize navigation on popup close * popup fixes * JS-2048: fix * fix cursor * fix sidebar resize button * fix sidebar handle size * small refactoring * JS-2058: shortcuts * fix right click * JS-1477: fix * JS-2050: fix * fix cmd+n flags * fix search object * fix search object * remove debug * JS-2066: revert sync component * JS-2024 * JS-2083: shortcuts + refactoring * JS-2083: shortcuts + refactoring * fix toast position * JS-2094: fix * JS-2094: fix * JS-2068: fix * JS-2094: fix * JS-2101: fix * JS-2102: fix * JS-2095: fixes * JS-1931: fixes + refactoring * fix sidebar overflow * fix sidebar resize * JS-2097: fix * JS-2097: fix * fix titleWrap * fix list widget hovers * JS-2096: edit icon * JS-2096: fix * fix font size * fix * JS-2098: fixes * JS-2098: fixes * JS-2098: fixes * JS-2099: fixes * fix window bg on init * optimize resize * JS-2100: gallery reworked as list component * JS-2100: gallery reworked as list component * resize logic update * update resize logi * move form components from util * small gallery refactoring * small gallery refactoring * JS-2099: fix cards * JS-2046: fix * JS-2101: fix * GO-1156: fix csv import param * JS-2096: fix * JS-2113: fix * JS-1630: fix * JS-1630: fix * JS-2096: fixes * JS-2098: fix * JS-1904: fix * JS-1904: add preview to bookmark * JS-2126: fix * JS-2112: navigation popups resize * JS-2067: remove context behaviour from object creation * fix row highlight in dark mode * remove popup from resize logic * JS-2123: fixes * JS-2130: fix * fix edit icon * JS-2123: fix * JS-2117: fix * fix full popup * fixes * JS-2132: fix * JS-2118: gallery card size calculation * popup resize logic
205 lines
No EOL
4.7 KiB
JavaScript
205 lines
No EOL
4.7 KiB
JavaScript
const { app, shell, nativeTheme } = require('electron');
|
|
const { is } = require('electron-util');
|
|
const log = require('electron-log');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const sanitize = require('sanitize-filename');
|
|
const protocol = 'anytype';
|
|
const userPath = app.getPath('userData');
|
|
const ConfigManager = require('./config.js');
|
|
|
|
log.transports.rendererConsole.level = 'error';
|
|
|
|
class Util {
|
|
|
|
appPath = '';
|
|
|
|
setAppPath (value) {
|
|
this.appPath = value;
|
|
};
|
|
|
|
mkDir (value) {
|
|
if (value) {
|
|
try { fs.mkdirSync(value); } catch (e) {};
|
|
};
|
|
};
|
|
|
|
log (method, text) {
|
|
if (!log[method]) {
|
|
method = 'info';
|
|
};
|
|
|
|
log[method](text);
|
|
console.log(text);
|
|
};
|
|
|
|
dateForFile() {
|
|
return new Date().toISOString().replace(/:/g, '_').replace(/\..+/, '');
|
|
};
|
|
|
|
// MacOs 12.2 (M1): always returns false regardless current color theme
|
|
isDarkTheme () {
|
|
return nativeTheme.shouldUseDarkColors || nativeTheme.shouldUseHighContrastColors || nativeTheme.shouldUseInvertedColorScheme;
|
|
};
|
|
|
|
getRouteFromUrl (url) {
|
|
return url.replace(`${protocol}://`, '/');
|
|
};
|
|
|
|
getTheme () {
|
|
const { theme } = ConfigManager.config || {};
|
|
|
|
switch (theme) {
|
|
default:
|
|
return theme;
|
|
|
|
case 'system':
|
|
return this.isDarkTheme() ? 'dark' : '';
|
|
};
|
|
};
|
|
|
|
getBgColor (theme) {
|
|
let bg = {
|
|
'': '#fff',
|
|
dark: '#171717',
|
|
};
|
|
return bg[theme];
|
|
};
|
|
|
|
electronPath () {
|
|
return path.join(this.appPath, 'electron');
|
|
};
|
|
|
|
imagePath () {
|
|
return path.join(this.electronPath(), 'img');
|
|
};
|
|
|
|
dataPath () {
|
|
const { channel } = ConfigManager.config;
|
|
const dataPath = [];
|
|
|
|
if (process.env.DATA_PATH) {
|
|
this.mkDir(process.env.DATA_PATH);
|
|
dataPath.push(process.env.DATA_PATH);
|
|
} else {
|
|
dataPath.push(userPath);
|
|
|
|
if (is.development) {
|
|
dataPath.push('dev');
|
|
} else
|
|
if ([ 'alpha', 'beta' ].includes(channel)) {
|
|
dataPath.push(channel);
|
|
};
|
|
|
|
dataPath.push('data');
|
|
};
|
|
|
|
return path.join.apply(null, dataPath);
|
|
};
|
|
|
|
send () {
|
|
const args = [ ...arguments ];
|
|
const win = args[0];
|
|
|
|
if (win && win.webContents) {
|
|
args.shift();
|
|
win.webContents.send.apply(win.webContents, args);
|
|
};
|
|
};
|
|
|
|
printHtml (win, exportPath, name, options) {
|
|
const fn = `${name.replace(/\.html$/, '')}_files`;
|
|
const filesPath = path.join(exportPath, fn);
|
|
const exportName = path.join(exportPath, this.fileName(name));
|
|
|
|
win.webContents.savePage(exportName, 'HTMLComplete').then(() => {
|
|
let content = fs.readFileSync(exportName, 'utf8');
|
|
|
|
// Replace files loaded by url and copy them in page folder
|
|
try {
|
|
content = content.replace(/"(file:\/\/[^"]+)"/g, function (s, p, o) {
|
|
let a = p.split('app.asar/dist/');
|
|
let name = a[1].split('/');
|
|
|
|
name = name[name.length - 1];
|
|
|
|
let src = p.replace('file://', '').replace(/\?.*/, '');
|
|
let dst = path.join(filesPath, name).replace(/\?.*/, '');
|
|
|
|
fs.copyFileSync(src, dst);
|
|
return `./${fn}/${name}`;
|
|
});
|
|
} catch (e) {
|
|
this.log('info', e);
|
|
};
|
|
|
|
content = content.replace(/<script[^>]+><\/script>/g, '');
|
|
|
|
try {
|
|
const css = [ 'export' ];
|
|
const js = [ 'export', 'jquery' ];
|
|
const ap = app.getAppPath();
|
|
|
|
let replaceJs = '';
|
|
let replaceCss = '';
|
|
|
|
js.forEach(it => {
|
|
fs.copyFileSync(`${ap}/dist/js/${it}.js`, path.join(filesPath, it + '.js'));
|
|
replaceJs += `<script src="./${fn}/${it}.js" type="text/javascript"></script>`;
|
|
});
|
|
|
|
css.forEach(it => {
|
|
fs.copyFileSync(`${ap}/dist/css/${it}.css`, path.join(filesPath, it + '.css'));
|
|
replaceCss += `<link rel="stylesheet" href="./${fn}/${it}.css" type="text/css" />`;
|
|
});
|
|
|
|
content = content.replace('<!-- %REPLACE-JS% -->', replaceJs);
|
|
content = content.replace('</head>', replaceCss + '</head>');
|
|
} catch (e) {
|
|
this.log('info', e);
|
|
};
|
|
|
|
fs.writeFileSync(exportName, content);
|
|
|
|
try {
|
|
fs.unlinkSync(path.join(filesPath, 'main.js'));
|
|
fs.unlinkSync(path.join(filesPath, 'run.js'));
|
|
} catch (e) {
|
|
this.log('info', e);
|
|
};
|
|
|
|
shell.openPath(exportPath).catch(err => {
|
|
this.log('info', err);
|
|
});
|
|
|
|
this.send(win, 'command', 'saveAsHTMLSuccess');
|
|
}).catch(err => {
|
|
this.send(win, 'command', 'saveAsHTMLSuccess');
|
|
this.log('info', err);
|
|
});
|
|
};
|
|
|
|
printPdf (win, exportPath, name, options) {
|
|
win.webContents.printToPDF(options).then(data => {
|
|
fs.writeFile(path.join(exportPath, this.fileName(name)), data, (error) => {
|
|
if (error) throw error;
|
|
|
|
shell.openPath(exportPath).catch(err => {
|
|
this.log('info', err);
|
|
});
|
|
|
|
this.send(win, 'command', 'saveAsHTMLSuccess');
|
|
});
|
|
}).catch(err => {
|
|
this.send(win, 'command', 'saveAsHTMLSuccess');
|
|
this.log('info', err);
|
|
});
|
|
};
|
|
|
|
fileName (name) {
|
|
return sanitize(String(name || 'untitled').trim());
|
|
};
|
|
|
|
};
|
|
|
|
module.exports = new Util(); |