Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to run the code by pressing CMD/Ctrl+Enter #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions client/components/codeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,27 @@ const _onChange = event => {
dispatchEvent('codeEditor:change');
};

let MacCMDPressed = false;

const MacCommandKeys = ['MetaLeft', 'MetaRight'];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So for mac if you want to run the code using the Command key you cannot use the normal event.ctrlKey + enter to run the command.

The key to the left and right Command keys on MAC are MetaLeft/Right so if you pressed the Command key it will set MacCMDPressed to true and if your next keystroke is enter it will run the command.

On any key that is not MetaLeft/Right it will set MacCMDPressed back to false


const _onKeydown = event => {
if ((event.ctrlKey || MacCMDPressed) && event.keyCode === 13) {
dispatchEvent('codeEditor:runcode');
}

if (MacCommandKeys.includes(event.code)) {
MacCMDPressed = true;
} else {
MacCMDPressed = false;
}
};

//init
(() => {
//Setup Ace Editor
editor = ace.edit('editor');
editor.container.addEventListener('keydown', _onKeydown);

editor.setTheme('ace/theme/solarized_dark');

Expand Down
22 changes: 3 additions & 19 deletions client/components/submitProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import CodeEditorComp from './codeEditor';
import { addEvent, dispatchEvent } from '../utilities/eventBus';

//Define Variables
let submitProgramElem, language, input, sourceCode, editorElement;
let submitProgramElem, language, input, sourceCode;

//Public Methods

Expand Down Expand Up @@ -36,6 +36,8 @@ addEvent('langSel:change', currentLang => {
language = currentLang;
});

addEvent('codeEditor:runcode', _onClick);

//Private Functions

async function _onClick(event) {
Expand All @@ -52,31 +54,13 @@ async function _onClick(event) {
dispatchEvent('submitProgram:output', output);
}

let MacCMDPressed = false;

const MacCommandKeys = ['MetaLeft', 'MetaRight'];

function _onKeyDown(event) {
if ((event.ctrlKey || MacCMDPressed) && event.keyCode === 13) {
_onClick(event);
}

if (MacCommandKeys.includes(event.code)) {
MacCMDPressed = true;
} else {
MacCMDPressed = false;
}
}

//init
(() => {
//DOM binding
submitProgramElem = document.getElementById('submitBtn');
editorElement = document.getElementById('editor');

//Event Bindings
submitProgramElem.addEventListener('click', _onClick);
editorElement.addEventListener('keydown', _onKeyDown);
})();

//Expose Component
Expand Down