Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0520d1

Browse files
committed
Add Save and Run commands
1 parent 776bc32 commit a0520d1

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

‎src/ui/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<select id="problem-select">
1616
</select>
1717
<button id="run-button">▶ Run</button>
18+
<button id="save-button">Save</button>
1819
</div>
1920
<div class="panel">
2021
<!-- Left Panel -->

‎src/ui/index.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path')
22
const file = require('fs');
33
const amdLoader = require('monaco-editor/min/vs/loader.js');
44
const Split = require('split.js')
5+
const { ipcRenderer } = require('electron');
56

67
const amdRequire = amdLoader.require;
78
const amdDefine = amdLoader.require.define;
@@ -17,18 +18,22 @@ amdRequire.config({
1718
// workaround monaco-css not understanding the environment
1819
self.module = undefined;
1920

20-
function saveSolution(problemName,language, content) {
21-
if (!problemName) {
21+
function saveSolution(language, content) {
22+
if (!previousProblem) {
2223
return;
2324
}
2425

2526
const userSolutionFilename =
26-
directoryManager.getUserSolutionFilename(problemName);
27-
console.log("Saving problem " + problemName + " to " +
27+
directoryManager.getUserSolutionFilename(previousProblem);
28+
console.log("Saving problem " + previousProblem + " to " +
2829
userSolutionFilename);
2930
file.writeFileSync(userSolutionFilename, content);
3031
}
3132

33+
function run() {
34+
console.log("Running solution");
35+
}
36+
3237
function setDescription(problemName) {
3338
var element = document.querySelector('.markdown-content#description-content');
3439
element.innerHTML = directoryManager.getDescription(problemName);
@@ -48,14 +53,13 @@ function setUserSolution(problemName) {
4853

4954
var previousProblem;
5055
function onProblemSelected(problemName) {
51-
saveSolution(previousProblem,'cpp', editor.getValue());
56+
saveSolution('cpp', editor.getValue());
5257
previousProblem = problemName;
5358

5459
console.log(`Problem selected: ${problemName}`);
5560
setDescription(problemName);
5661
setSolution(problemName);
5762
setUserSolution(problemName);
58-
console.log(`previousProblem: ${previousProblem}`);
5963
}
6064

6165
function initializeProblemsCombo(problemNames) {
@@ -72,12 +76,40 @@ function initializeProblemsCombo(problemNames) {
7276
});
7377
}
7478

79+
function initializeSaveCommand() {
80+
ipcRenderer.on('save-command', () => {
81+
console.log('Received save command');
82+
saveSolution('cpp', editor.getValue());
83+
});
84+
85+
document.getElementById('save-button')
86+
.addEventListener('click', function() {
87+
console.log('Save button clicked');
88+
saveSolution('cpp', editor.getValue());
89+
});
90+
}
91+
92+
function initializeRunCommand() {
93+
ipcRenderer.on('run-command', () => {
94+
console.log('Received run command');
95+
run();
96+
});
97+
98+
document.getElementById('run-button')
99+
.addEventListener('click', function() {
100+
console.log('Run button clicked');
101+
run();
102+
});
103+
}
104+
75105
document.addEventListener('DOMContentLoaded', (event) => {
76106
var tabs = document.querySelectorAll('.tab');
77107

78108
// For now...
79109
const problemNames = directoryManager.getProblemNames();
80110
initializeProblemsCombo(problemNames);
111+
initializeSaveCommand();
112+
initializeRunCommand();
81113

82114
amdRequire(['vs/editor/editor.main'], function() {
83115
monaco.editor.setTheme('vs-dark');

‎src/ui/main.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { app, BrowserWindow } = require('electron');
1+
const { app, BrowserWindow, globalShortcut } = require('electron');
22
const path = require('path');
33
const DirectoryManager = require('./directory-manager.js');
44

@@ -44,6 +44,40 @@ function createWindow() {
4444
})
4545
}
4646

47+
function registerSaveCommand() {
48+
const ret = globalShortcut.register('CommandOrControl+S', () => {
49+
console.log('CommandOrControl+S is pressed')
50+
win.webContents.send('save-command')
51+
})
52+
53+
if (!ret) {
54+
console.log('Registration failed!')
55+
}
56+
57+
console.log("CommandOrControl+S registered: " +
58+
globalShortcut.isRegistered('CommandOrControl+S'))
59+
60+
}
61+
62+
function registerRunCommand() {
63+
const ret = globalShortcut.register('CommandOrControl+R', () => {
64+
console.log('CommandOrControl+R is pressed')
65+
win.webContents.send('run-command')
66+
})
67+
68+
if (!ret) {
69+
console.log('Registration failed!')
70+
}
71+
72+
console.log("CommandOrControl+R registered: " +
73+
globalShortcut.isRegistered('CommandOrControl+R'))
74+
}
75+
76+
function registerCommands() {
77+
registerSaveCommand();
78+
registerRunCommand();
79+
}
80+
4781
app.whenReady().then(() => {
4882
const argv = process.argv;
4983

@@ -53,6 +87,7 @@ app.whenReady().then(() => {
5387
createWindow()
5488
}
5589
})
90+
registerCommands();
5691
});
5792

5893
app.on('window-all-closed', () => {
@@ -65,4 +100,8 @@ app.on('activate', () => {
65100
if (win === null) {
66101
createWindow()
67102
}
103+
})
104+
105+
app.on('will-quit', () => {
106+
globalShortcut.unregisterAll()
68107
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /