Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
20 changes: 19 additions & 1 deletion 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;
let submitProgramElem, language, input, sourceCode, editorElement;

//Public Methods

Expand Down Expand Up @@ -52,13 +52,31 @@ 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');
Copy link
Owner

@itaditya itaditya Oct 6, 2019

Choose a reason for hiding this comment

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

@EricTurf in this project I'm using pub-sub design pattern to enable proper Separation of Concerns. Each component is responsible to deal with their own DOM. submitProgram component shouldn't querySelect the codeEditor component.

The components interact with each other by dispatching events and subscribing to events.

What you have to do is in the codeEditor component, add the _keyDown event listener and the whole CMD + P logic. When you detect that it was indeed CMD + P you dispatch an event say codeEditor:runcode.
Then in the submitProgram component, listen to the codeEditor:runcode event using addEvent helper. When the codeEditor:runcode is triggered, you have to call the _onClick method.

Copy link
Owner

Choose a reason for hiding this comment

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

For reference see how submitProgram and langSel components interact with each other.


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

//Expose Component
Expand Down
Loading