Skip to content

Commit 622c3b5

Browse files
committed
Added Automatic Code Execution option.
Code can now be run automatically when changes are made by selecting the option in the settings panel.
1 parent 88631bc commit 622c3b5

File tree

10 files changed

+306
-197
lines changed

10 files changed

+306
-197
lines changed

resources/build/mainElectron.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ app.on("ready", () => {
170170
Menu.setApplicationMenu(menu);
171171

172172
//Menu Items Enabled State
173-
const runMenuItem = menu.items[0 + macMenuIndex].submenu.items[1];
173+
const runMenuItem = menu.items[0 + macMenuIndex].submenu.items[2];
174174

175175
const layoutHorizontalMenuItem = menu.items[2 + macMenuIndex].submenu.items[0];
176176
const layoutVerticalMenuItem = menu.items[2 + macMenuIndex].submenu.items[1];

resources/source/js/application/app.js

+43-34
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export {
3535
loadDataPixelsClassCode,
3636
setErrorMessage,
3737
setFrameViewHasImage,
38-
setMode,
38+
setCodeEditorMode,
3939
setTheme,
4040
toggleLayout,
4141
updateAutoCode
@@ -46,7 +46,7 @@ export {
4646
* <ul>
4747
* <li> DataPixelsClassCode </li>
4848
* <li> DataPixelsClassCodeInternal </li>
49-
* <li> IsExecuting </li>
49+
* <li> ExecuteAfterLoad </li>
5050
* </ul>
5151
*
5252
* @private
@@ -57,7 +57,7 @@ const M = {
5757

5858
DataPixelsClassCode: undefined,
5959
DataPixelsClassCodeInternal: undefined,
60-
IsExecuting: undefined
60+
ExecuteAfterLoad: undefined
6161
};
6262

6363
/**
@@ -85,18 +85,19 @@ function readStates() {
8585

8686
S.Alignment = (localStorage.length) ? JSON.parse(localStorage.getItem(C.Persistence.ALIGNMENT)) : true;
8787
S.AutoCode = DataPixelsCodeFactory.fromJSON(localStorage.getItem(C.Persistence.AUTO_CODE)) || null;
88+
S.AutoExecute = (localStorage.length) ? JSON.parse(localStorage.getItem(C.Persistence.AUTO_EXECUTE)) : false;
8889
S.Code = (localStorage.length) ? localStorage.getItem(C.Persistence.CODE) : Examples.basic;
8990
S.CodeEditorFlexGrow = localStorage.getItem(C.Persistence.CODE_EDITOR_FLEX_GROW) || 0.65;
90-
S.Description = (localStorage.length) ? JSON.parse(localStorage.getItem(C.Persistence.DESCRIPTION)) : false;
91+
S.Description = (localStorage.length) ? JSON.parse(localStorage.getItem(C.Persistence.DESCRIPTION)) : true;
9192
S.FrameViewFlexGrow = localStorage.getItem(C.Persistence.FRAME_VIEW_FLEX_GROW) || 0.35;
92-
S.Mode = localStorage.getItem(C.Persistence.MODE) || C.Mode.MANUAL;
93+
S.CodeEditorMode = localStorage.getItem(C.Persistence.MODE) || C.Mode.MANUAL;
9394
S.Indentation = localStorage.getItem(C.Persistence.INDENTATION) || C.Indentation.INDENT_4;
9495
S.Orientation = localStorage.getItem(C.Persistence.ORIENTATION) || C.Orientation.VERTICAL;
9596
S.Theme = localStorage.getItem(C.Persistence.THEME) || C.Theme.DARK;
9697

9798
window.addEventListener(C.Event.BEFORE_UNLOAD, writeStates);
9899

99-
setMode(S.Mode);
100+
setCodeEditorMode(S.CodeEditorMode);
100101
setTheme(S.Theme);
101102
}
102103

@@ -113,12 +114,13 @@ function writeStates() {
113114
try {
114115

115116
localStorage.setItem(C.Persistence.ALIGNMENT, S.Alignment);
116-
localStorage.setItem(C.Persistence.AUTO_CODE, (S.Mode === C.Mode.AUTO) ? JSON.stringify(S.AutoCode) : null);
117+
localStorage.setItem(C.Persistence.AUTO_CODE, (S.CodeEditorMode === C.Mode.AUTO) ? JSON.stringify(S.AutoCode) : null);
118+
localStorage.setItem(C.Persistence.AUTO_EXECUTE, S.AutoExecute);
117119
localStorage.setItem(C.Persistence.CODE_EDITOR_FLEX_GROW, S.CodeEditorFlexGrow);
118120
localStorage.setItem(C.Persistence.CODE, C.HTMLElement.TEXT_AREA.value);
119121
localStorage.setItem(C.Persistence.DESCRIPTION, S.Description);
120122
localStorage.setItem(C.Persistence.FRAME_VIEW_FLEX_GROW, S.FrameViewFlexGrow);
121-
localStorage.setItem(C.Persistence.MODE, S.Mode);
123+
localStorage.setItem(C.Persistence.MODE, S.CodeEditorMode);
122124
localStorage.setItem(C.Persistence.INDENTATION, S.Indentation);
123125
localStorage.setItem(C.Persistence.ORIENTATION, S.Orientation);
124126
localStorage.setItem(C.Persistence.THEME, S.Theme);
@@ -130,23 +132,23 @@ function writeStates() {
130132
}
131133

132134
/**
133-
* @description The mode determines which formatting options are available to the automatically generated program code written in the Code Editor.
134-
* @param {number} mode - The mode value is either 0 (Automatic) or 1 (Manual).
135+
* @description The code editor mode determines which formatting options are available to the automatically generated program code written in the Code Editor.
136+
* @param {number} mode - The mode value must be either C.Mode.AUTO or C.Mode.MANUAL.
135137
* @private
136138
* @function
137139
*
138140
*/
139-
function setMode(mode) {
141+
function setCodeEditorMode(mode) {
140142

141-
S.Mode = mode;
143+
S.CodeEditorMode = mode;
142144

143-
if (S.Mode === C.Mode.MANUAL) {
145+
if (S.CodeEditorMode === C.Mode.AUTO) {
144146

145-
C.HTMLElement.TEXT_AREA.removeEventListener(C.Event.INPUT, textInputManualModeHandler);
147+
C.HTMLElement.TEXT_AREA.addEventListener(C.Event.INPUT, textInputManualModeHandler);
146148
}
147-
else if (S.Mode === C.Mode.AUTO) {
149+
else if (S.CodeEditorMode === C.Mode.MANUAL) {
148150

149-
C.HTMLElement.TEXT_AREA.addEventListener(C.Event.INPUT, textInputManualModeHandler);
151+
C.HTMLElement.TEXT_AREA.removeEventListener(C.Event.INPUT, textInputManualModeHandler);
150152
}
151153
}
152154

@@ -158,12 +160,12 @@ function setMode(mode) {
158160
*/
159161
function textInputManualModeHandler() {
160162

161-
setMode(C.Mode.MANUAL);
163+
setCodeEditorMode(C.Mode.MANUAL);
162164
}
163165

164166
/**
165167
* @description The set theme applies a cohesive visual style throughout the application.
166-
* @param {string} theme - The theme value is either "Dark" or "Light".
168+
* @param {string} theme - The theme value must be either C.Theme.DARK or C.Theme.LIGHT.
167169
* @private
168170
* @function
169171
*
@@ -212,13 +214,16 @@ function displayCode(code, autoMode = true) {
212214

213215
document.execCommand(C.TextArea.COMMAND_INSERT, false, code);
214216

217+
215218
if (autoMode) {
216219

217-
setMode(C.Mode.AUTO);
220+
setCodeEditorMode(C.Mode.AUTO);
218221
}
219222

220-
Content.updateLineNumbers();
221-
Controls.updateExecuteButton();
223+
if (!S.AutoExecute) {
224+
225+
Controls.updateExecuteButton();
226+
}
222227
}
223228

224229
/**
@@ -240,7 +245,7 @@ function executeCode() {
240245
setErrorMessage(error);
241246
};
242247

243-
M.IsExecuting = true;
248+
M.ExecuteAfterLoad = true;
244249

245250
loadDataPixelsClassCode();
246251

@@ -263,19 +268,23 @@ function executeCode() {
263268
setErrorMessage(null);
264269

265270
let code = C.HTMLElement.TEXT_AREA.value;
266-
code = code.replace(/import .*?[;|\n]/gmi, "");
267271

268-
const runtimeScript = document.createElement(C.HTML.SCRIPT);
269-
runtimeScript.type = C.HTML.SCRIPT_TYPE;
270-
runtimeScript.text = `try{ (function(){ ${M.DataPixelsClassCodeInternal}${code} })(); }catch(error){ parent.setErrorMessageDelegate(error); }`;
272+
if (code) {
273+
274+
code = code.replace(/import .*?[;|\n]/gmi, "");
271275

272-
headTag.appendChild(runtimeScript);
273-
headTag.removeChild(runtimeScript);
276+
const runtimeScript = document.createElement(C.HTML.SCRIPT);
277+
runtimeScript.type = C.HTML.SCRIPT_TYPE;
278+
runtimeScript.text = `try{ (function(){ ${M.DataPixelsClassCodeInternal}${code} })(); }catch(error){ parent.setErrorMessageDelegate(error); }`;
274279

275-
frameViewBody.firstChild.style.pointerEvents = C.CSS.NONE;
276-
frameViewBody.firstChild.style.userSelect = C.CSS.NONE;
280+
headTag.appendChild(runtimeScript);
281+
headTag.removeChild(runtimeScript);
277282

278-
setFrameViewHasImage(true);
283+
frameViewBody.firstChild.style.pointerEvents = C.CSS.NONE;
284+
frameViewBody.firstChild.style.userSelect = C.CSS.NONE;
285+
286+
setFrameViewHasImage(true);
287+
}
279288
}
280289

281290
/**
@@ -354,9 +363,9 @@ function XHRLoadHandler(event) {
354363
M.DataPixelsClassCode = xhr.responseText;
355364
M.DataPixelsClassCodeInternal = xhr.responseText.replace(/(export default DataPixels;)/gi, "");
356365

357-
if (M.IsExecuting) {
366+
if (M.ExecuteAfterLoad) {
358367

359-
M.IsExecuting = false;
368+
M.ExecuteAfterLoad = false;
360369

361370
executeCode();
362371
}
@@ -390,7 +399,7 @@ function XHRErrorHandler(event) {
390399
*/
391400
function updateAutoCode() {
392401

393-
if (S.Mode === C.Mode.AUTO && S.AutoCode) {
402+
if (S.CodeEditorMode === C.Mode.AUTO && S.AutoCode) {
394403

395404
S.AutoCode.formatCode(S.Alignment, S.Description);
396405
S.AutoCode.updateIndentation(S.Indentation);

0 commit comments

Comments
 (0)