Skip to content

Commit aaed7ff

Browse files
committed
Implement to create a new workspace
1 parent 2dd6a93 commit aaed7ff

File tree

6 files changed

+109
-14
lines changed

6 files changed

+109
-14
lines changed

src/ipcEvents.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export enum IpcEvents {
2+
NEW_WORKSPACE = 'NEW_WORKSPACE',
3+
NEW_WORKSPACE_CONFIRM = 'NEW_WORKSPACE_CONFIRM',
24
NEW_BROWSER_TAB = 'NEW_BROWSER_TAB',
35
RELOAD_BROWSER_TAB = 'RELOAD_BROWSER_TAB',
46
STOP_BROWSER_TAB = 'STOP_BROWSER_TAB',
@@ -11,7 +13,8 @@ export enum IpcEvents {
1113
SAVE_TEXT_FILE = 'SAVE_TEXT_FILE',
1214
LOAD_TEXT_FILE = 'LOAD_TEXT_FILE',
1315
GET_CURRENT_WORKSPACE = 'GET_CURRENT_WORKSPACE',
14-
WORKSPACE_CHANGED = 'WORKSPACE_CHANGED',
16+
WORKSPACE_CHANGE = 'WORKSPACE_CHANGE',
17+
WORKSPACE_SWITCH = 'WORKSPACE_SWITCH',
1518
ENABLE_WORKSPACE = 'ENABLE_WORKSPACE',
1619
SET_WORKSPACE_VALUE = 'SET_WORKSPACE_VALUE',
1720
OPEN_SETTINGS = 'OPEN_SETTINGS',
@@ -23,12 +26,14 @@ export const ipcMainEvents = [
2326
IpcEvents.SET_MENU_ITEM_OPTIONS,
2427
IpcEvents.SAVE_TEXT_FILE,
2528
IpcEvents.LOAD_TEXT_FILE,
29+
IpcEvents.NEW_WORKSPACE,
2630
IpcEvents.GET_CURRENT_WORKSPACE,
2731
IpcEvents.ENABLE_WORKSPACE,
2832
IpcEvents.SET_WORKSPACE_VALUE,
2933
];
3034

3135
export const ipcRendererEvents = [
36+
IpcEvents.NEW_WORKSPACE_CONFIRM,
3237
IpcEvents.NEW_BROWSER_TAB,
3338
IpcEvents.MONACO_SAVE_FILE,
3439
IpcEvents.RELOAD_BROWSER_TAB,
@@ -37,5 +42,6 @@ export const ipcRendererEvents = [
3742
IpcEvents.TOGGLE_DEV_TOOLS,
3843
IpcEvents.SELECT_ALL_IN_EDITOR,
3944
IpcEvents.OPEN_SETTINGS,
40-
IpcEvents.WORKSPACE_CHANGED,
45+
IpcEvents.WORKSPACE_CHANGE,
46+
IpcEvents.WORKSPACE_SWITCH,
4147
];

src/main/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { setupDevTools } from './devtools';
1818
import { getOrCreateMainWindow } from './windows';
1919
import {
2020
initializeWorkspace,
21+
newWorkspace,
2122
getCurrentWorkspace,
2223
enableWorkspace,
2324
setWorkspaceValue,
@@ -80,6 +81,11 @@ function setupWorkspaceHandler() {
8081
saveTextFile(filePath, value);
8182
}
8283
);
84+
85+
ipcMainManager.handle(IpcEvents.NEW_WORKSPACE, async (_) => {
86+
newWorkspace();
87+
});
88+
8389
ipcMainManager.handle(IpcEvents.GET_CURRENT_WORKSPACE, async (_) => {
8490
return getCurrentWorkspace();
8591
});

src/main/menu.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,18 @@ function getDebugMenu(): MenuItemConstructorOptions {
153153
function getFileMenu(): MenuItemConstructorOptions {
154154
const fileMenu: Array<MenuItemConstructorOptions> = [
155155
{
156-
label: 'New Code',
157-
accelerator: 'CmdOrCtrl+N',
158-
enabled: false,
156+
label: 'New Workspace',
157+
click: () => {
158+
return ipcMainManager.send(IpcEvents.NEW_WORKSPACE_CONFIRM);
159+
},
159160
},
161+
// {
162+
// label: 'New Code',
163+
// accelerator: 'CmdOrCtrl+N',
164+
// enabled: false,
165+
// },
160166
{
161-
label: 'New Browser Tab',
167+
label: 'New Tab',
162168
click: () => {
163169
return ipcMainManager.send(IpcEvents.NEW_BROWSER_TAB);
164170
},

src/main/store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function setStoreValue(key, value) {
1010
store.set(key, value);
1111
}
1212

13-
export function deleteStoreKey(key) {
13+
export function deleteStoreValue(key) {
1414
store.delete(key);
1515
}
1616

src/main/workspace.js

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { app } from 'electron';
22
import { v4 as uuidv4 } from 'uuid';
3-
import fs from 'fs';
3+
import fs from 'fs-extra';
44
import path from 'path';
55
import {
66
getStoreValue,
77
setStoreValue,
8-
deleteStoreKey,
8+
deleteStoreValue,
99
subscribeKey,
1010
} from './store';
1111
import { IpcEvents } from '../ipcEvents';
@@ -15,7 +15,7 @@ import { getFileNameFromPath, getRelativePath } from './util';
1515
const userDataPath = app.getPath('userData');
1616

1717
export function initializeWorkspace() {
18-
//deleteStoreKey('workspace'); // DEBUGGING
18+
//deleteStoreValue('workspace'); // DEBUGGING
1919
if (!getStoreValue('workspace')) {
2020
setStoreValue('workspace', {});
2121
const id = createWorkspace();
@@ -50,8 +50,32 @@ export function createWorkspace() {
5050
return id;
5151
}
5252

53-
export function getCurrentWorkspace() {
54-
const workspaceId = getStoreValue('workspace.current');
53+
export function newWorkspace() {
54+
const oldWorkspaceId = getCurrentWorkspaceId();
55+
56+
const newWorkspaceId = createWorkspace();
57+
if (getWorkspace(newWorkspaceId)) {
58+
setCurrentWorkspaceId(newWorkspaceId);
59+
}
60+
setTimeout(() => {
61+
deleteWorkspace(oldWorkspaceId);
62+
}, 0);
63+
}
64+
65+
export function deleteWorkspace(id) {
66+
if (!id) return;
67+
68+
const currentWorkspaceId = getCurrentWorkspaceId();
69+
if (currentWorkspaceId === id) {
70+
setStoreValue(`workspace.current`, null);
71+
}
72+
deleteStoreValue(`workspace.${id}`);
73+
setTimeout(() => {
74+
deleteWorkspaceDirectories(id);
75+
}, 1000);
76+
}
77+
78+
export function getWorkspace(workspaceId) {
5579
if (!workspaceId) return null;
5680

5781
const workspace = getStoreValue(`workspace.${workspaceId}`);
@@ -60,6 +84,21 @@ export function getCurrentWorkspace() {
6084
return workspace;
6185
}
6286

87+
export function getCurrentWorkspace() {
88+
const workspaceId = getCurrentWorkspaceId();
89+
return getWorkspace(workspaceId);
90+
}
91+
92+
export function getCurrentWorkspaceId() {
93+
const workspaceId = getStoreValue('workspace.current');
94+
return workspaceId || null;
95+
}
96+
97+
export function setCurrentWorkspaceId(id) {
98+
if (!id) return;
99+
setStoreValue('workspace.current', id);
100+
}
101+
63102
export function enableWorkspace(workspaceId, value) {
64103
if (!workspaceId) return;
65104
setStoreValue(`workspace.${workspaceId}.enabled`, value);
@@ -85,6 +124,13 @@ function createWorkspaceDirectories(workspaceId) {
85124
createDirectory(project);
86125
}
87126

127+
function deleteWorkspaceDirectories(workspaceId) {
128+
if (!workspaceId) return;
129+
130+
const home = path.join(userDataPath, 'workspaces', workspaceId);
131+
fs.remove(home).catch((err) => console.error(err));
132+
}
133+
88134
function createProjectFiles(projectPath) {
89135
const mainJsPath = path.join(projectPath, 'main.js');
90136
if (!fs.existsSync(mainJsPath)) {
@@ -115,7 +161,11 @@ function getFileInfo(projectPath, localPath, remotePath) {
115161
}
116162

117163
function notifyWorkspaceChange(newValue, oldValue) {
118-
ipcMainManager.send(IpcEvents.WORKSPACE_CHANGED, [newValue]);
164+
if (oldValue['current'] !== newValue['current']) {
165+
ipcMainManager.send(IpcEvents.WORKSPACE_SWITCH, [newValue]);
166+
} else {
167+
ipcMainManager.send(IpcEvents.WORKSPACE_CHANGE, [newValue]);
168+
}
119169
}
120170

121171
const mainjs = `/**

src/renderer/workspace/useWorkspace.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import { useState, useMemo, useEffect } from 'react';
2+
import { ExclamationCircleOutlined } from '@ant-design/icons';
3+
import { Modal } from 'antd';
24
import { createContext } from '../hooks/context';
35
import { ipcRendererManager, useIpcRendererListener } from '../ipc';
46
import { IpcEvents } from '../../ipcEvents';
57

8+
const { confirm } = Modal;
9+
10+
const showNewWorkspaceConfirm = () => {
11+
confirm({
12+
title: 'Do you want to create a new workspace?',
13+
icon: <ExclamationCircleOutlined />,
14+
content: 'Your changes will be lost.',
15+
onOk() {
16+
ipcRendererManager.invoke(IpcEvents.NEW_WORKSPACE);
17+
},
18+
onCancel() {},
19+
});
20+
};
21+
622
export const [WorkspaceProvider, useWorkspaceContext] = createContext({
723
name: 'WorkspaceContext',
824
});
@@ -23,12 +39,23 @@ export function useWorkspaceProvider() {
2339
})();
2440
}, []);
2541

26-
useIpcRendererListener(IpcEvents.WORKSPACE_CHANGED, (newValue) => {
42+
useIpcRendererListener(IpcEvents.WORKSPACE_CHANGE, (newValue) => {
2743
if (workspace && newValue[workspace.id]) {
2844
setWorkspace(newValue[workspace.id]);
2945
}
3046
});
3147

48+
useIpcRendererListener(IpcEvents.WORKSPACE_SWITCH, (newValue) => {
49+
const currentWorkspaceId = newValue['current'];
50+
if (currentWorkspaceId) {
51+
setWorkspace(newValue[currentWorkspaceId]);
52+
}
53+
});
54+
55+
useIpcRendererListener(IpcEvents.NEW_WORKSPACE_CONFIRM, () => {
56+
showNewWorkspaceConfirm();
57+
});
58+
3259
const [scriptVersionId, setScriptVersionId] = useState(1);
3360
const [consoleLogs, setConsoleLogs] = useState([]);
3461
const [activityIndex, setActivityIndex] = useState(

0 commit comments

Comments
 (0)