Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
ChugunovRoman committed Sep 12, 2019
2 parents 5bb948d + adfff99 commit 2a5bcf8
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 220 deletions.
6 changes: 6 additions & 0 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ declare namespace Electron {
addListener(event: 'log', listener: (data: any) => void): this;
removeListener(event: 'log', listener: (data: any) => void): this;
emit(event: 'log', data: any): boolean;

on(event: 'sign-out', listener: () => void): this;
once(event: 'sign-out', listener: () => void): this;
addListener(event: 'sign-out', listener: () => void): this;
removeListener(event: 'sign-out', listener: () => void): this;
emit(event: 'sign-out'): boolean;
}

interface IpcMain extends EventEmitter {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "figma-linux",
"version": "0.5.5",
"version": "0.5.6",
"description": "Figma is the first interface design tool based in the browser, making it easier for teams to create software.",
"main": "src/main/index.js",
"repository": "git@github.com:ChugunovRoman/figma-linux.git",
Expand Down
8 changes: 8 additions & 0 deletions src/constants/_other.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { parse } from 'url';


export const HOMEPAGE = 'https://www.figma.com';
export const PARSED_HOMEPAGE = parse('https://www.figma.com');

export const TOPPANELHEIGHT = 28;

Expand All @@ -8,3 +12,7 @@ export const MANIFEST_FILE_NAME = 'manifest.json';

export const CONFIGDIR = `${process.env.HOME}/.config/figma-linux`
export const RESOURCESDIR = `${process.env.HOME}/.config/figma-linux/resources`
export const REGEXP_APP_AUTH_GRANT = /^\/{0,2}app_auth\/[^\/]+\/grant/;
export const REGEXP_APP_AUTH_REDEEM = /^\/{0,2}app_auth\/redeem/;

export const FIGMA_SESSION_COOKIE_NAME = 'figma.st';
6 changes: 4 additions & 2 deletions src/main/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import * as Const from "Const";
import { cmd } from 'Utils/Main';
import Args from "./Args";
import WindowManager from "./window/WindowManager";
import Server from './server';
import { Session } from './Session';
import './events/app';

class App {
windowManager: WindowManager;
session: Session;

constructor() {
const isSingleInstance = E.app.requestSingleInstanceLock();
Expand Down Expand Up @@ -50,7 +51,7 @@ class App {
}
});

Server.start();
this.session = new Session();
}

this.appEvent();
Expand All @@ -73,6 +74,7 @@ class App {
const { figmaUrl } = Args();

this.windowManager = WindowManager.instance;
this.session.handleAppReady();

setTimeout(() => {
figmaUrl !== '' && this.windowManager.openUrl(figmaUrl);
Expand Down
17 changes: 0 additions & 17 deletions src/main/Replacers/domAccess.ts

This file was deleted.

41 changes: 0 additions & 41 deletions src/main/ResourceLoader.ts

This file was deleted.

40 changes: 40 additions & 0 deletions src/main/Session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as E from 'electron';

import * as Const from 'Const';
import { isSameCookieDomain } from 'Utils/Main';

export class Session {
private _hasFigmaSession: boolean;
private assessSessionTimer: NodeJS.Timer;

constructor() {
this._hasFigmaSession = null;
this.assessSessionTimer = null;
}

public hasFigmaSession = (): boolean => {
return this._hasFigmaSession;
}

public handleAppReady = () => {
E.session.defaultSession.cookies.get({ url: Const.HOMEPAGE }, (error, cookies) => {
E.session.defaultSession.cookies.on('changed', this.handleCookiesChanged);
if (error) {
console.error('[wm] failed to get cookies during handleAppReady:', Const.HOMEPAGE, error);
return;
}
this._hasFigmaSession = !!cookies.find((cookie) => {
return cookie.name === Const.FIGMA_SESSION_COOKIE_NAME;
});
console.log('[wm] already signed in?', this._hasFigmaSession);
});
};

private handleCookiesChanged = (event: E.Event, cookie: E.Cookie, cause: string, removed: boolean) => {
if (isSameCookieDomain(cookie.domain || '', Const.PARSED_HOMEPAGE.hostname || '')) {
if (cookie.name === Const.FIGMA_SESSION_COOKIE_NAME) {
console.log(`${cookie.name} cookie changed:`, cause, cookie.name, cookie.domain, removed ? 'removed' : '');
}
}
}
}
64 changes: 0 additions & 64 deletions src/main/server.ts

This file was deleted.

74 changes: 0 additions & 74 deletions src/main/window/Tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DEFAULT_SETTINGS, HOMEPAGE, RESOURCESDIR } from 'Const';
import { isDev } from 'Utils/Common';
import { Request, isFileBrowser } from 'Utils/Main';
import Fonts from "../Fonts";
import ResourceLoader from '../ResourceLoader';

export default class Tabs {
public static registeredCancelCallbackMap: Map<number, Function> = new Map();
Expand Down Expand Up @@ -53,79 +52,6 @@ export default class Tabs {
// tab.webContents.executeJavaScript(`(${dom})()`);
});

// TODO: Remove this shit later
tab.webContents.on('will-navigate', async (event, url) => {
console.log('will-navigate, ', url);

if (!isFileBrowser(url)) {
// let htmlProjectPage = '';
let page: Resources.Page;
const filePromises: Promise<Request.Responce | void>[] = [];
const resource = new ResourceLoader();

try {
page = await resource.loadFigmaRes(url)
} catch (error) {
console.error(`Caanot fetch url: ${url}, ERROR: `, error);
}

try {
fs.mkdirSync(RESOURCESDIR);
} catch (error) {}

for (const link of page.links) {
let url = link;

if (/^\//.test(link)) {
url = `${HOMEPAGE}${link}`;
}

let fileRes = Request(url)
.catch(error => console.error(`Cannot fetch resource: ${url}, ERROR: `, error));

filePromises.push(fileRes);
}

const resources = await Promise.all(filePromises);

filePromises.length = 0;
for (const res of resources) {
if (!res) continue;

let data = res.data;

const fileName = path.parse(res.url).base;

if (/.*figma_app.*\.js/.test(res.url)) {
data = data.replace('Failed DOM access check', 'ok');
data = data.replace(/(['"`])\/api/gi, `$1www.figma.com/api`);
// data = data.replace(/(window\.)?location.host(name)?/gi, `'www.figma.com'`);
}

page.data = page.data.replace(res.url.replace(/https?:\/\/(www)?.figma.com\/figbuild/, '/figbuild'), `http://localhost:12531/figbuild/${fileName}`);

filePromises.push(
fs.promises.writeFile(`${RESOURCESDIR}/${fileName}`, data)
.catch(error => console.log(`Resource file ${fileName} successful saved on the disk`))
);
}

page.data = page.data.replace(/(['"])\/api/gi, `$1${HOMEPAGE}/api`);
// page.data = page.data.replace(/<\/head>/gi, `<script>(() => { window.location.origin='https://www.figma.com'; })()</script></head>`);

await Promise.all(filePromises);

const filePath = `${RESOURCESDIR}/${path.parse(page.url).base}.html`;
try {
fs.writeFileSync(filePath, page.data, { flag: 'w' });
} catch (error) {
throw new Error(error);
}

// tab.webContents.loadURL(`file://${filePath}`);
}
});

isDev && tab.webContents.toggleDevTools();

Tabs.tabs.push(tab);
Expand Down
13 changes: 12 additions & 1 deletion src/main/window/WindowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ class WindowManager {
E.app.quit();
}, 1000);
});
E.app.on('sign-out', () => {
this.logoutAndRestart();
});
E.app.on('handle-command', (id: string) => {
switch (id) {
case 'scale-normal': {
Expand Down Expand Up @@ -324,7 +327,7 @@ class WindowManager {
return tab;
}

public logoutAndRestart = (event?: E.Event) => {
private logoutAndRestart = (event?: E.Event) => {
E.net.request(`${this.home}/logout`).on('response', response => {
response.on('data', data => { });
response.on('error', (err: Error) => {
Expand Down Expand Up @@ -389,6 +392,14 @@ class WindowManager {
if (to.pathname === '/logout') {
this.logoutAndRestart(event);
}

if (Const.REGEXP_APP_AUTH_REDEEM.test(from.pathname || '')) {
return;
}
if (to.search && to.search.match(/[\?\&]redirected=1/)) {
event.preventDefault();
return;
}
}

private openFileBrowser = () => {
Expand Down
8 changes: 0 additions & 8 deletions src/main/window/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ const init = (template?: E.MenuItemConstructorOptions[]) => {
menuItem.enabled = state.actionState ? !!state.actionState[action] : false;
}
});
// E.app.on('updateActionState', (actionState) => {
// if (!actionState) return;

// for (let action of Object.keys(menuItemMap)) {
// const menuItem: E.MenuItem = menuItemMap[action];
// menuItem.enabled = actionState ? !!actionState[action] : false;
// }
// });
};

export default init;
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "figma-linux",
"version": "0.5.5",
"version": "0.5.6",
"description": "Figma is the first interface design tool based in the browser, making it easier for teams to create software.",
"main": "main/main.js",
"repository": "git@github.com:ChugunovRoman/figma-linux.git",
Expand Down
7 changes: 1 addition & 6 deletions src/utils/Main/Menu/menuItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,7 @@ const HELP_MENU = {
SEPARATOR,
{
label: 'Sign Out',
click() {
// TODO: To do via event emitter
// const windowManager = WindowManager.instance;

// windowManager.logoutAndRestart();
},
click() { Commander.exec('sign-out'); },
},
SEPARATOR,
{
Expand Down
Loading

0 comments on commit 2a5bcf8

Please sign in to comment.