Skip to content

Commit d0d93c7

Browse files
committed
Implement IpcEvents.MONACO_SAVE_FILE
1 parent 281b543 commit d0d93c7

File tree

7 files changed

+53
-53
lines changed

7 files changed

+53
-53
lines changed

src/ipcEvents.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export enum IpcEvents {
44
STOP_BROWSER_TAB = 'STOP_BROWSER_TAB',
55
SET_MENU_ITEM_OPTIONS = 'SET_MENU_ITEM_OPTIONS',
66
MONACO_TOGGLE_OPTION = 'MONACO_TOGGLE_OPTION',
7+
MONACO_SAVE_FILE = 'MONACO_SAVE_FILE',
78
TOGGLE_DEV_TOOLS = 'TOGGLE_DEV_TOOLS',
89
RELOAD_WINDOW = 'RELOAD_WINDOW',
910
SELECT_ALL_IN_EDITOR = 'SELECT_ALL_IN_EDITOR',
@@ -26,6 +27,7 @@ export const ipcMainEvents = [
2627

2728
export const ipcRendererEvents = [
2829
IpcEvents.NEW_BROWSER_TAB,
30+
IpcEvents.MONACO_SAVE_FILE,
2931
IpcEvents.RELOAD_BROWSER_TAB,
3032
IpcEvents.STOP_BROWSER_TAB,
3133
IpcEvents.MONACO_TOGGLE_OPTION,

src/main/menu.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ function getFileMenu(): MenuItemConstructorOptions {
169169
click: () => createMainWindow(),
170170
accelerator: 'CmdOrCtrl+Shift+N',
171171
},
172+
{ type: 'separator' },
173+
{
174+
label: 'Save',
175+
click: () => {
176+
return ipcMainManager.send(IpcEvents.MONACO_SAVE_FILE);
177+
},
178+
accelerator: 'CmdOrCtrl+S',
179+
},
172180
];
173181

174182
// macOS has these items in the "CodingBrowser" menu

src/renderer/MainPage.jsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@ import HomeView from './views/HomeView';
44
import DevView from './views/DevView';
55
import RuntimeView from './views/RuntimeView';
66
import { ActivityBar } from './components/ActivityBar';
7+
import {
8+
activityItems,
9+
activeItemToIndex,
10+
useWorkspace,
11+
} from './workspace/useWorkspace';
12+
13+
const activityItems = ['home', 'play', 'files'];
14+
const activeItemToIndex = (name) => activityItems.indexOf(name);
715

816
export default function () {
9-
const [activity, setActivity] = useState(0);
17+
const { activityIndex, setActivityIndex } = useWorkspace();
1018

1119
return (
1220
<Allotment proportionalLayout={false}>
1321
<Allotment.Pane>
14-
<HomeView visible={activity == 0} />
15-
<RuntimeView visible={activity === 1} />
16-
<DevView visible={activity === 2} />
22+
<HomeView visible={activityIndex == activeItemToIndex('home')} />
23+
<RuntimeView visible={activityIndex === activeItemToIndex('play')} />
24+
<DevView visible={activityIndex === activeItemToIndex('files')} />
1725
</Allotment.Pane>
1826
<Allotment.Pane
1927
key="activityBar"
@@ -22,9 +30,9 @@ export default function () {
2230
visible={true}
2331
>
2432
<ActivityBar
25-
checked={activity}
26-
items={['home', 'play', 'files']}
27-
onClick={setActivity}
33+
checked={activityIndex}
34+
items={activityItems}
35+
onClick={setActivityIndex}
2836
/>
2937
</Allotment.Pane>
3038
</Allotment>

src/renderer/components/editor/Toolbar.jsx

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ import {
77
import { useToolbar } from './useCodeEditors';
88

99
export default function () {
10-
const { dirty, execution, save, start, stop } = useToolbar();
10+
const { dirty, save } = useToolbar();
1111

1212
const handleSave = async () => {
1313
save();
1414
};
1515

16-
const hanedleStart = async () => {
17-
start();
18-
};
19-
20-
const handleStop = async () => {
21-
stop();
22-
};
23-
2416
return (
2517
<div style={{ height: '34px' }}>
2618
<Row
@@ -29,27 +21,6 @@ export default function () {
2921
}}
3022
>
3123
<Col flex="300px" style={{ padding: 1 }}>
32-
{execution === 'start' ? (
33-
<Button
34-
type="text"
35-
icon={<StopOutlined style={{ color: '#cc0000' }} />}
36-
size="middle"
37-
style={{ color: '#525252', fontWeight: 'normal' }}
38-
onClick={handleStop}
39-
>
40-
Stop
41-
</Button>
42-
) : (
43-
<Button
44-
type="text"
45-
icon={<PlayCircleOutlined style={{ color: 'green' }} />}
46-
size="middle"
47-
style={{ color: '#525252', fontWeight: 'normal' }}
48-
onClick={hanedleStart}
49-
>
50-
Start
51-
</Button>
52-
)}
5324
<Button
5425
type="text"
5526
icon={<SaveOutlined />}

src/renderer/components/editor/useCodeEditors.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createContext } from '../../hooks/context';
44
import { defaultOption, toggleEditorOption } from './manacoOption';
55
import { ipcRendererManager, useIpcRendererListener } from '../../ipc';
66
import { IpcEvents } from '../../../ipcEvents';
7-
import { useWorkspace } from '../../workspace/useWorkspace';
7+
import { activeItemToIndex, useWorkspace } from '../../workspace/useWorkspace';
88

99
function getDefaultScript(filepath) {
1010
switch (filepath) {
@@ -62,7 +62,7 @@ export function useCodeEditors() {
6262
const [tabs, setTabs] = useState([]);
6363
const [monacoOption, setMonacoOption] = useState(defaultOption);
6464
const [activeTabKey, setActiveTabKey] = useState('');
65-
const { startAll, stopAll } = useWorkspace();
65+
const { startAll, stopAll, activityIndex } = useWorkspace();
6666

6767
useEffect(() => {
6868
initializeDefaultTabs();
@@ -75,6 +75,15 @@ export function useCodeEditors() {
7575
}
7676
});
7777

78+
useIpcRendererListener(
79+
IpcEvents.MONACO_SAVE_FILE,
80+
useCallback(() => {
81+
if (activeItemToIndex('files') === activityIndex) {
82+
save(activeTabKey);
83+
}
84+
}, [activityIndex, activeTabKey])
85+
);
86+
7887
const initializeDefaultTabs = () => {
7988
const defaultTabs = [
8089
createTab({
@@ -106,15 +115,6 @@ export function useCodeEditors() {
106115
});
107116
};
108117

109-
const start = async () => {
110-
await saveAll();
111-
startAll();
112-
};
113-
114-
const stop = async () => {
115-
stopAll();
116-
};
117-
118118
const save = useCallback(
119119
async (tabKey) => {
120120
const tab = tabs.find((tab) => tab.key === tabKey);
@@ -173,8 +173,6 @@ export function useCodeEditors() {
173173
activeTabKey,
174174
setActiveTabKey,
175175
setDirty,
176-
start,
177-
stop,
178176
save,
179177
load,
180178
register,
@@ -183,16 +181,15 @@ export function useCodeEditors() {
183181
}
184182

185183
export function useToolbar() {
186-
const { tabs, activeTabKey, save, start, stop } = useCodeEditorsContext();
184+
const { tabs, activeTabKey, save } = useCodeEditorsContext();
187185
const activeTab = tabs.find((tab) => tab.key === activeTabKey);
188186
const dirty = !!activeTab?.dirty;
189-
const { execution } = useWorkspace();
190187

191188
const saveCallback = useCallback(() => {
192189
save(activeTabKey);
193190
}, [activeTabKey]);
194191

195-
return { dirty, execution, save: saveCallback, start, stop };
192+
return { dirty, save: saveCallback };
196193
}
197194

198195
export function useCodeEditorTabs() {

src/renderer/workspace/useWorkspace.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import { useWorkspaceContext } from './useWorkspaceProvider';
33
import { ipcRendererManager } from '../ipc';
44
import { IpcEvents } from '../../ipcEvents';
55

6+
export { activityItems, activeItemToIndex } from './useWorkspaceProvider';
7+
68
export function useWorkspace() {
79
const {
810
rootPath,
911
mainScript,
1012
preloadScript,
1113
execution,
14+
activityIndex,
15+
setActivityIndex,
1216
startWorkspace,
1317
stopWorkspace,
1418
} = useWorkspaceContext();
@@ -49,8 +53,10 @@ export function useWorkspace() {
4953
mainScript,
5054
preloadScript,
5155
execution,
56+
activityIndex,
5257
startAll,
5358
stopAll,
59+
setActivityIndex,
5460
};
5561
}
5662

src/renderer/workspace/useWorkspaceProvider.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ export const [WorkspaceProvider, useWorkspaceContext] = createContext({
55
name: 'WorkspaceContext',
66
});
77

8+
export const activityItems = ['home', 'play', 'files'];
9+
export const activeItemToIndex = (name) => activityItems.indexOf(name);
10+
811
export function useWorkspaceProvider() {
912
const [rootPath, setRootPath] = useState('memory://');
1013
const [preloadScript, setPreloadScript] = useState(null);
1114
const [mainScript, setMainScript] = useState(null);
1215
const [execution, setExecution] = useState('stop'); // 'stop', 'start'
1316
const [consoleLogs, setConsoleLogs] = useState([]);
17+
const [activityIndex, setActivityIndex] = useState(
18+
activeItemToIndex('files')
19+
);
1420

1521
const startWorkspace = (main, preload) => {
1622
setMainScript(main);
@@ -34,8 +40,10 @@ export function useWorkspaceProvider() {
3440
preloadScript,
3541
execution,
3642
consoleLogs,
43+
activityIndex,
3744
startWorkspace,
3845
stopWorkspace,
3946
addConsoleLog,
47+
setActivityIndex,
4048
};
4149
}

0 commit comments

Comments
 (0)