Skip to content

Commit 715f0fa

Browse files
Merge pull request #11 from TerribleCoding/dev_1.4
Expression editor feature
2 parents 0b47e0e + 034de72 commit 715f0fa

19 files changed

Lines changed: 742 additions & 28 deletions

README.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ This product is intended for digital use only. It is prohibited to copy or distr
1010

1111
## Usage
1212

13+
#### The basics
14+
1315
Choose the shape you want to calculate features by clicking the corresponding image or name.
1416
You'll be redirected to the dedicated page where you can insert available data on the *_Dimensional data_* and *_Material data_* tables (blue tables).
1517

1618
Wether results will be available while inserting data, they will show in the bottom tables (orange tables).
17-
e.g.: If you're looking for the area of a rectangle as the base **and** the height will be filled in you'll have the result otherwise the field will be null.
19+
e.g.: If you're looking for the area of a rectangle as the base **and** the height will be filled in you'll have the result otherwise the field will be empty.
1820

1921
You can use the arrows on the right side of any input field to raise or decrease it by 1 unit, for your convenience.
20-
For a quicker input process, is suggested to use the _Tab_ key to jump on the next input field.
22+
For a quicker input process, it is suggested to use the _Tab_ key to jump on the next input field.
2123

2224
Results updates trigger whenever **one** of these events occur:
2325

@@ -32,23 +34,60 @@ All texts and numbers on the pages are able to be copied and pasted anywhere you
3234

3335
A button for getting back to the main page is provided on every profile page, directly below its title.
3436

37+
#### Equation page
38+
3539
The equations page is intuitive to use: clicking the shape from the list on the left makes the math for that shape visible.
3640
Some other extra features from MathJax library are also available when right-clicking on a equation, like the ability of zooming the expression or copying the LaTex string for further use.
3741

42+
#### Expression Editor
43+
44+
The core new feature from version 1.4 is the Expression Editor.
45+
This editor allows the user to quickly calculate the result of a mathematical expression and use it as a data input for the shape calculation process.
46+
47+
Any numerical input field from the *_Dimensional data_* and *_Material data_* tables (blue ones) can be used as targets for the expression calculation, in any of the shape pages.
48+
The Editor can be opened by pressing the "=" key on the keyboard while the related numerical input is selected.
49+
50+
A popup with the Editor itself will appear over the content, showing:
51+
- the variable selected to be the target for the result,
52+
- an empty text field for the expression,
53+
- the result preview of the expression,
54+
- an icon indicating if any errors are detected in the expression
55+
56+
Start typing the math expression in the text field and the preview will be updated in real time. Characters allowed for a correct calculation are digits from 0 to 9 and _. + - * / ( )_. Use the dot _"."_ as the decimal separator.
57+
58+
The editor can be left anytime by clicking the top-right X icon or pressing "Esc" on the keyboard and no action will be performed.
59+
60+
If the result of the typed expression is valid (green check icon after the result preview) it can be pasted in the related data input of the shape page. To use it, click the _"Use result"_ button on the bottom of the popup, or type "Enter" on the keyboard when the expression field is selected.
61+
62+
The result will be pasted into the table and the popup disappears. This can be used multiple times in any page.
63+
3864
## Known bugs and issues
3965

4066
#### Ellipse perimeter not exact
4167

4268
The formula used to get the perimeter is not an exact calculation, however is roughly the best approximation available.
4369
Notice that the more "squished" the ellipse, the more imprecise the result.
44-
The formula has been updated to a better one - this one - in version 1.2 and the approximation is totally suitable for the intended use of this tool.
70+
The formula has been updated to a better one - the actual one - in version 1.2 and the approximation is totally suitable for the intended use of this tool.
4571
This might mean that it won't be upgraded to an even more precise calculation (infinite series are not infinite in programming).
4672

4773
#### Truncated material names
4874

4975
When a material is selected from the dropdown menu, often the name overflows the space available.
5076
The issue will be solved in a future release.
5177

78+
#### Expression Editor
79+
80+
When the Editor is opened trough the "=" key, the expression text input is meant to be focused automatically.
81+
This should avoid moving hands to get back to the mouse and to the keyboard again, but something didn't work as expected.
82+
83+
If something is typed in the expression input field, if all characters in it are deleted, the result will be marked as "undefined".
84+
This result can also be pasted into the related data input, but nothing will happen as they're numerical inputs.
85+
86+
When confirming the result of the editor to be pasted in the data, results are not calculated with the new number.
87+
The page starts calculating again when another data is entered by directly typing the number, the ìn pressing Enter, Tab or clicking away.
88+
89+
All these issues will be fixed in a future patch.
90+
5291
## Next improvements to be added
5392

5493
Note: _The order of this list might not match the order these features will be added through next versions._
@@ -62,6 +101,15 @@ Note: _The order of this list might not match the order these features will be a
62101

63102
## Versions log
64103

104+
#### v1.4
105+
106+
_Release date: October 22, 2021_
107+
108+
_Work Time: 8h_
109+
110+
- Added the Expression Editor, where math expressions can be calculated and the result can be pasted as a data input
111+
- Some bugs related to the development of the Editor are still unfixed, but anyway it can be used correctly
112+
65113
#### v1.3
66114

67115
_Release date: July 25, 2021_

assets/vid/exp-editor.mp4

396 KB
Binary file not shown.

index.html

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ <h1>
1818
</h1>
1919
<img src="assets/ico/tool-logo.png" alt="AnySection" class="tool-logo">
2020
<h3>Select a profile shape</h3>
21+
<button class="new-feature">New in v1.4!</button>
22+
<div class="new-feature">
23+
<p style="font-weight: bold;">Calculate simple math with the new editor</p>
24+
<video src="assets/vid/exp-editor.mp4" autoplay loop controls></video>
25+
<ol>
26+
<li>Select any of the data inputs (blue)</li>
27+
<li>Press the "=" key</li>
28+
<li>Digit your expression</li>
29+
<li>Confirm by clicking the button or "Enter" on your keyboard</li>
30+
<li>The result will be pasted into the input field</li>
31+
</ol>
32+
</div>
2133
</div>
2234

2335
<div id="main">
@@ -33,13 +45,13 @@ <h3>Select a profile shape</h3>
3345
</div>
3446
</div>
3547

36-
<div id="credits" style="display: none;" class="popup">
48+
<div id="credits" style="display: none;" class="popup blur">
3749
<div class="outer">
3850
<div class="content">
3951
<close onclick="closeCredits()"><i class="fi-rr-cross"></i></close>
4052
<img src="assets/ico/tool-logo.png" alt="AnySection" height="50">
4153
<a href="https://github.com/TerribleCoding/AnySection/releases" target="_blank"
42-
style="position: relative; left: 0.5em; bottom: 12px;">v1.3</a>
54+
style="position: relative; left: 0.5em; bottom: 12px;">v1.4</a>
4355
<br>
4456
<span>This project is developed by</span>
4557
<a href="https://terriblecoding.github.io" target="_blank">

pages/c-profile.html

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
<head>
55
<title>C - U Profile</title>
66
<script src="https://cdn.jsdelivr.net/npm/p5@1.3.0/lib/p5.js"></script>
7-
<script src="common.js"></script>
7+
<script src="common.js" defer></script>
88
<script src="c-profile.js"></script>
99
<link rel="stylesheet" type="text/css" href="../style.css">
10+
<link rel="stylesheet" type="text/css" href="../popup.css">
11+
<link rel='stylesheet' href='https://cdn-uicons.flaticon.com/uicons-regular-rounded/css/uicons-regular-rounded.css'>
1012
<link rel="icon" type="image/x-icon" href="../assets/ico/favicon.ico">
1113
</head>
1214

@@ -145,6 +147,35 @@ <h3 class="back">Back to index</h3>
145147
</div>
146148

147149
<div class="right" id="image"></div>
150+
151+
<div id="calc" style="display: none;" class="popup auto-width shadow">
152+
<div class="outer">
153+
<div class="content">
154+
<close onclick="closeEditor()"><i class="fi-rr-cross"></i></close>
155+
<h4>Expression editor</h4>
156+
<p>Enter a valid expression for the variable:</p>
157+
<p class="big">
158+
<span id="var">D</span>
159+
<span>=</span>
160+
<input type="text" name="expression" id="expression" placeholder="0">
161+
<span>=</span>
162+
<span id="result">
163+
<num>0</num>
164+
<icon>
165+
<i class="fi-rr-check">
166+
<span>Expression OK</span>
167+
</i>
168+
<i class="fi-rr-exclamation invisible">
169+
<span></span>
170+
</i>
171+
</icon>
172+
</span>
173+
</p>
174+
<button style="display: block; margin: auto;"><i class="fi-rr-check"></i>Use result</button>
175+
</div>
176+
</div>
177+
</div>
178+
148179
</body>
149180

150181
</html>

pages/common.js

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var combo, selector;
55
var imageSpace;
66
var pageName;
77

8-
// dimensions = list of all dimensions called in all pages, update while coding pages
8+
// dimensions = list of all dimensions called in all pages
99
var dimensions = ['L', 'D', 't', 'B', 'b', 'H', 'R', 'A', 'h'];
1010
var dim = {};
1111
var matProp = ['density', 'brake', 'yeld'];
@@ -84,6 +84,9 @@ function setup() {
8484

8585
// set values when selecting a material
8686
selector.changed(updateMatProp);
87+
88+
// add event listeners to open equation editor
89+
addEquationKeyListeners();
8790
}
8891

8992
function updateMatProp() {
@@ -177,4 +180,106 @@ function Traction() {
177180
k = 4;
178181
}
179182
return R / k * a;
180-
}
183+
}
184+
185+
186+
187+
// EXPRESSION EDITOR FUNCTIONS
188+
189+
var editor = document.querySelector('#calc');
190+
var property = document.querySelector('#var');
191+
var dataInput;
192+
var textfield = document.querySelector('#expression');
193+
var result = document.querySelector('#result num');
194+
var icons = document.querySelectorAll('#result icon > *');
195+
var button = document.querySelector('#calc button');
196+
var errors = {
197+
char: 'Only numbers or +-*/(). characters are allowed.',
198+
par: 'Some parentheses are missing.',
199+
syntax: 'Syntax error detected.'
200+
}
201+
202+
function openEditor() {
203+
editor.style.display = 'grid';
204+
}
205+
206+
function closeEditor() {
207+
editor.style.display = 'none';
208+
}
209+
210+
function addEquationKeyListeners() {
211+
var target = document.querySelectorAll('table input[type=number]');
212+
target.forEach(item => item.addEventListener('keyup', openEditorForVariable));
213+
}
214+
215+
function openEditorForVariable(event) {
216+
// press '=' to open editor when closed and focus on textfield
217+
if (event.key == '=' && editor.style.display == 'none') {
218+
dataInput = event.target;
219+
property.innerHTML = event.target.parentNode.previousElementSibling.innerHTML;
220+
textfield.value = dataInput.value;
221+
textfield.focus();
222+
result.innerText = '0';
223+
openEditor();
224+
}
225+
}
226+
227+
editor.addEventListener('keyup', function (event) {
228+
// do something only if the editor is open
229+
if (editor.style.display !== 'none') {
230+
// press 'Esc' to close editor
231+
if (event.key == 'Escape') closeEditor();
232+
// press 'Enter' to act as pressing the button
233+
if (event.key == 'Enter') button.click();
234+
}
235+
});
236+
237+
// check for errors while typing
238+
textfield.addEventListener('input', checkErrors);
239+
240+
function checkErrors(event) {
241+
// convert the input to string, split in an array, remove spaces
242+
var inp = event.target.value.toString().split('').filter(x => x != " ");
243+
// check if every character is allowed => 0123456789.+-*/()
244+
var charTest = inp.every(char => char.match(/[0-9|\+|\-|\*|\/|(|)|\.]/));
245+
// check if parentheses match
246+
var parTest = inp.filter(char => char == '(').length == inp.filter(char => char == ')').length;
247+
248+
// preview result
249+
var validExpression = true;
250+
try {
251+
eval(inp.join(''));
252+
} catch (e) {
253+
if (e instanceof SyntaxError) validExpression = false;
254+
} finally {
255+
if (validExpression && charTest && parTest) result.innerText = eval(inp.join(''));
256+
}
257+
258+
// update warning tooltip
259+
var tooltip = [];
260+
if (!charTest) tooltip.push(errors.char);
261+
if (!parTest) tooltip.push(errors.par);
262+
if (!validExpression) tooltip.push(errors.syntax);
263+
icons[1].querySelector('span').innerText = tooltip.join('\n');
264+
265+
// update icon and button
266+
if (charTest && parTest && validExpression) {
267+
button.disabled = false;
268+
showIcon(0);
269+
} else {
270+
button.disabled = true;
271+
showIcon(1);
272+
}
273+
274+
function showIcon(index) {
275+
icons.forEach(e => e.classList.add('invisible'));
276+
icons[index].classList.remove('invisible');
277+
}
278+
}
279+
280+
// paste result in the field if confirmed
281+
button.addEventListener('click', function() {
282+
dataInput.value = result.innerText;
283+
closeEditor();
284+
dataInput.focus();
285+
});

pages/cylinder.html

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
<head>
55
<title>Cylinder / Round bar</title>
66
<script src="https://cdn.jsdelivr.net/npm/p5@1.3.0/lib/p5.js"></script>
7-
<script src="common.js"></script>
7+
<script src="common.js" defer></script>
88
<script src="cylinder.js"></script>
99
<link rel="stylesheet" type="text/css" href="../style.css">
10+
<link rel="stylesheet" type="text/css" href="../popup.css">
11+
<link rel='stylesheet' href='https://cdn-uicons.flaticon.com/uicons-regular-rounded/css/uicons-regular-rounded.css'>
1012
<link rel="icon" type="image/x-icon" href="../assets/ico/favicon.ico">
1113
</head>
1214

@@ -117,6 +119,35 @@ <h3 class="back">Back to index</h3>
117119
</div>
118120

119121
<div class="right" id="image"></div>
122+
123+
<div id="calc" style="display: none;" class="popup auto-width shadow">
124+
<div class="outer">
125+
<div class="content">
126+
<close onclick="closeEditor()"><i class="fi-rr-cross"></i></close>
127+
<h4>Expression editor</h4>
128+
<p>Enter a valid expression for the variable:</p>
129+
<p class="big">
130+
<span id="var">D</span>
131+
<span>=</span>
132+
<input type="text" name="expression" id="expression" placeholder="0">
133+
<span>=</span>
134+
<span id="result">
135+
<num>0</num>
136+
<icon>
137+
<i class="fi-rr-check">
138+
<span>Expression OK</span>
139+
</i>
140+
<i class="fi-rr-exclamation invisible">
141+
<span></span>
142+
</i>
143+
</icon>
144+
</span>
145+
</p>
146+
<button style="display: block; margin: auto;"><i class="fi-rr-check"></i>Use result</button>
147+
</div>
148+
</div>
149+
</div>
150+
120151
</body>
121152

122153
</html>

0 commit comments

Comments
 (0)